buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * This file contains register alloction support and is intended to be |
| 19 | * included by: |
| 20 | * |
| 21 | * Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "../CompilerUtility.h" |
| 26 | #include "../CompilerIR.h" |
| 27 | #include "../Dataflow.h" |
| 28 | #include "Ralloc.h" |
| 29 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 30 | namespace art { |
| 31 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 32 | #define SREG(c, s) ((c)->regLocation[(s)].sRegLow) |
| 33 | /* |
| 34 | * Get the "real" sreg number associated with an sReg slot. In general, |
| 35 | * sReg values passed through codegen are the SSA names created by |
| 36 | * dataflow analysis and refer to slot numbers in the cUnit->regLocation |
| 37 | * array. However, renaming is accomplished by simply replacing RegLocation |
| 38 | * entries in the cUnit->reglocation[] array. Therefore, when location |
| 39 | * records for operands are first created, we need to ask the locRecord |
| 40 | * identified by the dataflow pass what it's new name is. |
| 41 | */ |
| 42 | |
| 43 | /* |
| 44 | * Free all allocated temps in the temp pools. Note that this does |
| 45 | * not affect the "liveness" of a temp register, which will stay |
| 46 | * live until it is either explicitly killed or reallocated. |
| 47 | */ |
| 48 | extern void oatResetRegPool(CompilationUnit* cUnit) |
| 49 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 50 | int i; |
| 51 | for (i=0; i < cUnit->regPool->numCoreRegs; i++) { |
| 52 | if (cUnit->regPool->coreRegs[i].isTemp) |
| 53 | cUnit->regPool->coreRegs[i].inUse = false; |
| 54 | } |
| 55 | for (i=0; i < cUnit->regPool->numFPRegs; i++) { |
| 56 | if (cUnit->regPool->FPRegs[i].isTemp) |
| 57 | cUnit->regPool->FPRegs[i].inUse = false; |
| 58 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 61 | /* |
| 62 | * Set up temp & preserved register pools specialized by target. |
| 63 | * Note: numRegs may be zero. |
| 64 | */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 65 | extern void oatInitPool(RegisterInfo* regs, int* regNums, int num) |
| 66 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 67 | int i; |
| 68 | for (i=0; i < num; i++) { |
| 69 | regs[i].reg = regNums[i]; |
| 70 | regs[i].inUse = false; |
| 71 | regs[i].isTemp = false; |
| 72 | regs[i].pair = false; |
| 73 | regs[i].live = false; |
| 74 | regs[i].dirty = false; |
| 75 | regs[i].sReg = INVALID_SREG; |
| 76 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 77 | } |
| 78 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 79 | void dumpRegPool(RegisterInfo* p, int numRegs) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 80 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 81 | LOG(INFO) << "================================================"; |
| 82 | for (int i = 0; i < numRegs; i++) { |
| 83 | LOG(INFO) << StringPrintf( |
| 84 | "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x", |
| 85 | p[i].reg, p[i].isTemp, p[i].inUse, p[i].pair, p[i].partner, |
| 86 | p[i].live, p[i].dirty, p[i].sReg,(int)p[i].defStart, |
| 87 | (int)p[i].defEnd); |
| 88 | } |
| 89 | LOG(INFO) << "================================================"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 90 | } |
| 91 | |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 92 | void oatDumpCoreRegPool(CompilationUnit* cUnit) |
| 93 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 94 | dumpRegPool(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs); |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void oatDumpFpRegPool(CompilationUnit* cUnit) |
| 98 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 99 | dumpRegPool(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs); |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 102 | /* Mark a temp register as dead. Does not affect allocation state. */ |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 103 | static inline void clobberBody(CompilationUnit *cUnit, RegisterInfo* p) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 104 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 105 | if (p->isTemp) { |
| 106 | DCHECK(!(p->live && p->dirty)) << "Live & dirty temp in clobber"; |
| 107 | p->live = false; |
| 108 | p->sReg = INVALID_SREG; |
| 109 | p->defStart = NULL; |
| 110 | p->defEnd = NULL; |
| 111 | if (p->pair) { |
| 112 | p->pair = false; |
| 113 | oatClobber(cUnit, p->partner); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 114 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 115 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 118 | /* Mark a temp register as dead. Does not affect allocation state. */ |
| 119 | void oatClobber(CompilationUnit* cUnit, int reg) |
| 120 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 121 | clobberBody(cUnit, oatGetRegInfo(cUnit, reg)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 122 | } |
| 123 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 124 | void clobberSRegBody(RegisterInfo* p, int numRegs, int sReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 125 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 126 | int i; |
| 127 | for (i=0; i< numRegs; i++) { |
| 128 | if (p[i].sReg == sReg) { |
| 129 | if (p[i].isTemp) { |
| 130 | p[i].live = false; |
| 131 | } |
| 132 | p[i].defStart = NULL; |
| 133 | p[i].defEnd = NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 134 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 135 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Clobber any temp associated with an sReg. Could be in either class */ |
| 139 | extern void oatClobberSReg(CompilationUnit* cUnit, int sReg) |
| 140 | { |
buzbee | 3d66194 | 2012-03-14 17:37:27 -0700 | [diff] [blame] | 141 | #ifndef NDEBUG |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 142 | /* Reset live temp tracking sanity checker */ |
| 143 | if (sReg == cUnit->liveSReg) { |
| 144 | cUnit->liveSReg = INVALID_SREG; |
| 145 | } |
buzbee | 3d66194 | 2012-03-14 17:37:27 -0700 | [diff] [blame] | 146 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 147 | clobberSRegBody(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs, sReg); |
| 148 | clobberSRegBody(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs, sReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 149 | } |
| 150 | |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 151 | /* |
| 152 | * SSA names associated with the initial definitions of Dalvik |
| 153 | * registers are the same as the Dalvik register number (and |
| 154 | * thus take the same position in the promotionMap. However, |
| 155 | * the special Method* and compiler temp resisters use negative |
Elliott Hughes | bdf6c3d | 2012-03-20 13:43:53 -0700 | [diff] [blame] | 156 | * vReg numbers to distinguish them and can have an arbitrary |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 157 | * ssa name (above the last original Dalvik register). This function |
| 158 | * maps SSA names to positions in the promotionMap array. |
| 159 | */ |
| 160 | int SRegToPMap(CompilationUnit* cUnit, int sReg) |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 161 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 162 | DCHECK_LT(sReg, cUnit->numSSARegs); |
| 163 | DCHECK_GE(sReg, 0); |
| 164 | int vReg = SRegToVReg(cUnit, sReg); |
| 165 | if (vReg >= 0) { |
| 166 | DCHECK_LT(vReg, cUnit->numDalvikRegisters); |
| 167 | return vReg; |
| 168 | } else { |
| 169 | int pos = std::abs(vReg) - std::abs(SSA_METHOD_BASEREG); |
| 170 | DCHECK_LE(pos, cUnit->numCompilerTemps); |
| 171 | return cUnit->numDalvikRegisters + pos; |
| 172 | } |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 173 | } |
| 174 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 175 | /* Reserve a callee-save register. Return -1 if none available */ |
| 176 | extern int oatAllocPreservedCoreReg(CompilationUnit* cUnit, int sReg) |
| 177 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 178 | int res = -1; |
| 179 | RegisterInfo* coreRegs = cUnit->regPool->coreRegs; |
| 180 | for (int i = 0; i < cUnit->regPool->numCoreRegs; i++) { |
| 181 | if (!coreRegs[i].isTemp && !coreRegs[i].inUse) { |
| 182 | int vReg = SRegToVReg(cUnit, sReg); |
| 183 | int pMapIdx = SRegToPMap(cUnit, sReg); |
| 184 | res = coreRegs[i].reg; |
| 185 | coreRegs[i].inUse = true; |
| 186 | cUnit->coreSpillMask |= (1 << res); |
| 187 | cUnit->coreVmapTable.push_back(vReg); |
| 188 | cUnit->numCoreSpills++; |
| 189 | cUnit->promotionMap[pMapIdx].coreLocation = kLocPhysReg; |
| 190 | cUnit->promotionMap[pMapIdx].coreReg = res; |
| 191 | break; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 192 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 193 | } |
| 194 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Reserve a callee-save fp single register. Try to fullfill request for |
| 199 | * even/odd allocation, but go ahead and allocate anything if not |
| 200 | * available. If nothing's available, return -1. |
| 201 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 202 | int allocPreservedSingle(CompilationUnit* cUnit, int sReg, bool even) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 203 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 204 | int res = -1; |
| 205 | RegisterInfo* FPRegs = cUnit->regPool->FPRegs; |
| 206 | for (int i = 0; i < cUnit->regPool->numFPRegs; i++) { |
| 207 | if (!FPRegs[i].isTemp && !FPRegs[i].inUse && |
| 208 | ((FPRegs[i].reg & 0x1) == 0) == even) { |
| 209 | int vReg = SRegToVReg(cUnit, sReg); |
| 210 | int pMapIdx = SRegToPMap(cUnit, sReg); |
| 211 | res = FPRegs[i].reg; |
| 212 | FPRegs[i].inUse = true; |
| 213 | oatMarkPreservedSingle(cUnit, vReg, res); |
| 214 | cUnit->promotionMap[pMapIdx].fpLocation = kLocPhysReg; |
| 215 | cUnit->promotionMap[pMapIdx].fpReg = res; |
| 216 | break; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 217 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 218 | } |
| 219 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Somewhat messy code here. We want to allocate a pair of contiguous |
| 224 | * physical single-precision floating point registers starting with |
| 225 | * an even numbered reg. It is possible that the paired sReg (sReg+1) |
| 226 | * has already been allocated - try to fit if possible. Fail to |
| 227 | * allocate if we can't meet the requirements for the pair of |
| 228 | * sReg<=sX[even] & (sReg+1)<= sX+1. |
| 229 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 230 | int allocPreservedDouble(CompilationUnit* cUnit, int sReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 231 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 232 | int res = -1; // Assume failure |
| 233 | int vReg = SRegToVReg(cUnit, sReg); |
| 234 | int pMapIdx = SRegToPMap(cUnit, sReg); |
| 235 | if (cUnit->promotionMap[pMapIdx+1].fpLocation == kLocPhysReg) { |
| 236 | // Upper reg is already allocated. Can we fit? |
| 237 | int highReg = cUnit->promotionMap[pMapIdx+1].fpReg; |
| 238 | if ((highReg & 1) == 0) { |
| 239 | // High reg is even - fail. |
| 240 | return res; |
| 241 | } |
| 242 | // Is the low reg of the pair free? |
| 243 | RegisterInfo* p = oatGetRegInfo(cUnit, highReg-1); |
| 244 | if (p->inUse || p->isTemp) { |
| 245 | // Already allocated or not preserved - fail. |
| 246 | return res; |
| 247 | } |
| 248 | // OK - good to go. |
| 249 | res = p->reg; |
| 250 | p->inUse = true; |
| 251 | DCHECK_EQ((res & 1), 0); |
| 252 | oatMarkPreservedSingle(cUnit, vReg, res); |
| 253 | } else { |
| 254 | RegisterInfo* FPRegs = cUnit->regPool->FPRegs; |
| 255 | for (int i = 0; i < cUnit->regPool->numFPRegs; i++) { |
| 256 | if (!FPRegs[i].isTemp && !FPRegs[i].inUse && |
| 257 | ((FPRegs[i].reg & 0x1) == 0x0) && |
| 258 | !FPRegs[i+1].isTemp && !FPRegs[i+1].inUse && |
| 259 | ((FPRegs[i+1].reg & 0x1) == 0x1) && |
| 260 | (FPRegs[i].reg + 1) == FPRegs[i+1].reg) { |
| 261 | res = FPRegs[i].reg; |
| 262 | FPRegs[i].inUse = true; |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 263 | oatMarkPreservedSingle(cUnit, vReg, res); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 264 | FPRegs[i+1].inUse = true; |
| 265 | DCHECK_EQ(res + 1, FPRegs[i+1].reg); |
| 266 | oatMarkPreservedSingle(cUnit, vReg+1, res+1); |
| 267 | break; |
| 268 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 269 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 270 | } |
| 271 | if (res != -1) { |
| 272 | cUnit->promotionMap[pMapIdx].fpLocation = kLocPhysReg; |
| 273 | cUnit->promotionMap[pMapIdx].fpReg = res; |
| 274 | cUnit->promotionMap[pMapIdx+1].fpLocation = kLocPhysReg; |
| 275 | cUnit->promotionMap[pMapIdx+1].fpReg = res + 1; |
| 276 | } |
| 277 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | /* |
| 282 | * Reserve a callee-save fp register. If this register can be used |
| 283 | * as the first of a double, attempt to allocate an even pair of fp |
| 284 | * single regs (but if can't still attempt to allocate a single, preferring |
| 285 | * first to allocate an odd register. |
| 286 | */ |
| 287 | extern int oatAllocPreservedFPReg(CompilationUnit* cUnit, int sReg, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 288 | bool doubleStart) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 289 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 290 | int res = -1; |
| 291 | if (doubleStart) { |
| 292 | res = allocPreservedDouble(cUnit, sReg); |
| 293 | } |
| 294 | if (res == -1) { |
| 295 | res = allocPreservedSingle(cUnit, sReg, false /* try odd # */); |
| 296 | } |
| 297 | if (res == -1) |
| 298 | res = allocPreservedSingle(cUnit, sReg, true /* try even # */); |
| 299 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 300 | } |
| 301 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 302 | int allocTempBody(CompilationUnit* cUnit, RegisterInfo* p, int numRegs, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 303 | int* nextTemp, bool required) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 304 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 305 | int i; |
| 306 | int next = *nextTemp; |
| 307 | for (i=0; i< numRegs; i++) { |
| 308 | if (next >= numRegs) |
| 309 | next = 0; |
| 310 | if (p[next].isTemp && !p[next].inUse && !p[next].live) { |
| 311 | oatClobber(cUnit, p[next].reg); |
| 312 | p[next].inUse = true; |
| 313 | p[next].pair = false; |
| 314 | *nextTemp = next + 1; |
| 315 | return p[next].reg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 316 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 317 | next++; |
| 318 | } |
| 319 | next = *nextTemp; |
| 320 | for (i=0; i< numRegs; i++) { |
| 321 | if (next >= numRegs) |
| 322 | next = 0; |
| 323 | if (p[next].isTemp && !p[next].inUse) { |
| 324 | oatClobber(cUnit, p[next].reg); |
| 325 | p[next].inUse = true; |
| 326 | p[next].pair = false; |
| 327 | *nextTemp = next + 1; |
| 328 | return p[next].reg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 329 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 330 | next++; |
| 331 | } |
| 332 | if (required) { |
| 333 | oatCodegenDump(cUnit); |
| 334 | dumpRegPool(cUnit->regPool->coreRegs, |
| 335 | cUnit->regPool->numCoreRegs); |
| 336 | LOG(FATAL) << "No free temp registers"; |
| 337 | } |
| 338 | return -1; // No register available |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | //REDO: too many assumptions. |
| 342 | extern int oatAllocTempDouble(CompilationUnit* cUnit) |
| 343 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 344 | RegisterInfo* p = cUnit->regPool->FPRegs; |
| 345 | int numRegs = cUnit->regPool->numFPRegs; |
| 346 | /* Start looking at an even reg */ |
| 347 | int next = cUnit->regPool->nextFPReg & ~0x1; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 348 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 349 | // First try to avoid allocating live registers |
| 350 | for (int i=0; i < numRegs; i+=2) { |
| 351 | if (next >= numRegs) |
| 352 | next = 0; |
| 353 | if ((p[next].isTemp && !p[next].inUse && !p[next].live) && |
| 354 | (p[next+1].isTemp && !p[next+1].inUse && !p[next+1].live)) { |
| 355 | oatClobber(cUnit, p[next].reg); |
| 356 | oatClobber(cUnit, p[next+1].reg); |
| 357 | p[next].inUse = true; |
| 358 | p[next+1].inUse = true; |
| 359 | DCHECK_EQ((p[next].reg+1), p[next+1].reg); |
| 360 | DCHECK_EQ((p[next].reg & 0x1), 0); |
| 361 | cUnit->regPool->nextFPReg = next + 2; |
| 362 | if (cUnit->regPool->nextFPReg >= numRegs) { |
| 363 | cUnit->regPool->nextFPReg = 0; |
| 364 | } |
| 365 | return p[next].reg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 366 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 367 | next += 2; |
| 368 | } |
| 369 | next = cUnit->regPool->nextFPReg & ~0x1; |
buzbee | a50638b | 2011-11-02 15:15:06 -0700 | [diff] [blame] | 370 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 371 | // No choice - find a pair and kill it. |
| 372 | for (int i=0; i < numRegs; i+=2) { |
| 373 | if (next >= numRegs) |
| 374 | next = 0; |
| 375 | if (p[next].isTemp && !p[next].inUse && p[next+1].isTemp && |
| 376 | !p[next+1].inUse) { |
| 377 | oatClobber(cUnit, p[next].reg); |
| 378 | oatClobber(cUnit, p[next+1].reg); |
| 379 | p[next].inUse = true; |
| 380 | p[next+1].inUse = true; |
| 381 | DCHECK_EQ((p[next].reg+1), p[next+1].reg); |
| 382 | DCHECK_EQ((p[next].reg & 0x1), 0); |
| 383 | cUnit->regPool->nextFPReg = next + 2; |
| 384 | if (cUnit->regPool->nextFPReg >= numRegs) { |
| 385 | cUnit->regPool->nextFPReg = 0; |
| 386 | } |
| 387 | return p[next].reg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 388 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 389 | next += 2; |
| 390 | } |
| 391 | LOG(FATAL) << "No free temp registers (pair)"; |
| 392 | return -1; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Return a temp if one is available, -1 otherwise */ |
| 396 | extern int oatAllocFreeTemp(CompilationUnit* cUnit) |
| 397 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 398 | return allocTempBody(cUnit, cUnit->regPool->coreRegs, |
| 399 | cUnit->regPool->numCoreRegs, |
| 400 | &cUnit->regPool->nextCoreReg, true); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | extern int oatAllocTemp(CompilationUnit* cUnit) |
| 404 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 405 | return allocTempBody(cUnit, cUnit->regPool->coreRegs, |
| 406 | cUnit->regPool->numCoreRegs, |
| 407 | &cUnit->regPool->nextCoreReg, true); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | extern int oatAllocTempFloat(CompilationUnit* cUnit) |
| 411 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 412 | return allocTempBody(cUnit, cUnit->regPool->FPRegs, |
| 413 | cUnit->regPool->numFPRegs, |
| 414 | &cUnit->regPool->nextFPReg, true); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 415 | } |
| 416 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 417 | RegisterInfo* allocLiveBody(RegisterInfo* p, int numRegs, int sReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 418 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 419 | int i; |
| 420 | if (sReg == -1) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 421 | return NULL; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 422 | for (i=0; i < numRegs; i++) { |
| 423 | if (p[i].live && (p[i].sReg == sReg)) { |
| 424 | if (p[i].isTemp) |
| 425 | p[i].inUse = true; |
| 426 | return &p[i]; |
| 427 | } |
| 428 | } |
| 429 | return NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 430 | } |
| 431 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 432 | RegisterInfo* allocLive(CompilationUnit* cUnit, int sReg, int regClass) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 433 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 434 | RegisterInfo* res = NULL; |
| 435 | switch (regClass) { |
| 436 | case kAnyReg: |
| 437 | res = allocLiveBody(cUnit->regPool->FPRegs, |
| 438 | cUnit->regPool->numFPRegs, sReg); |
| 439 | if (res) |
| 440 | break; |
| 441 | /* Intentional fallthrough */ |
| 442 | case kCoreReg: |
| 443 | res = allocLiveBody(cUnit->regPool->coreRegs, |
| 444 | cUnit->regPool->numCoreRegs, sReg); |
| 445 | break; |
| 446 | case kFPReg: |
| 447 | res = allocLiveBody(cUnit->regPool->FPRegs, |
| 448 | cUnit->regPool->numFPRegs, sReg); |
| 449 | break; |
| 450 | default: |
| 451 | LOG(FATAL) << "Invalid register type"; |
| 452 | } |
| 453 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | extern void oatFreeTemp(CompilationUnit* cUnit, int reg) |
| 457 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 458 | RegisterInfo* p = cUnit->regPool->coreRegs; |
| 459 | int numRegs = cUnit->regPool->numCoreRegs; |
| 460 | int i; |
| 461 | for (i=0; i< numRegs; i++) { |
| 462 | if (p[i].reg == reg) { |
| 463 | if (p[i].isTemp) { |
| 464 | p[i].inUse = false; |
| 465 | } |
| 466 | p[i].pair = false; |
| 467 | return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 468 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 469 | } |
| 470 | p = cUnit->regPool->FPRegs; |
| 471 | numRegs = cUnit->regPool->numFPRegs; |
| 472 | for (i=0; i< numRegs; i++) { |
| 473 | if (p[i].reg == reg) { |
| 474 | if (p[i].isTemp) { |
| 475 | p[i].inUse = false; |
| 476 | } |
| 477 | p[i].pair = false; |
| 478 | return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 479 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 480 | } |
| 481 | LOG(FATAL) << "Tried to free a non-existant temp: r" << reg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | extern RegisterInfo* oatIsLive(CompilationUnit* cUnit, int reg) |
| 485 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 486 | RegisterInfo* p = cUnit->regPool->coreRegs; |
| 487 | int numRegs = cUnit->regPool->numCoreRegs; |
| 488 | int i; |
| 489 | for (i=0; i< numRegs; i++) { |
| 490 | if (p[i].reg == reg) { |
| 491 | return p[i].live ? &p[i] : NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 492 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 493 | } |
| 494 | p = cUnit->regPool->FPRegs; |
| 495 | numRegs = cUnit->regPool->numFPRegs; |
| 496 | for (i=0; i< numRegs; i++) { |
| 497 | if (p[i].reg == reg) { |
| 498 | return p[i].live ? &p[i] : NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 499 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 500 | } |
| 501 | return NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | extern RegisterInfo* oatIsTemp(CompilationUnit* cUnit, int reg) |
| 505 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 506 | RegisterInfo* p = oatGetRegInfo(cUnit, reg); |
| 507 | return (p->isTemp) ? p : NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 508 | } |
| 509 | |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 510 | extern RegisterInfo* oatIsPromoted(CompilationUnit* cUnit, int reg) |
| 511 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 512 | RegisterInfo* p = oatGetRegInfo(cUnit, reg); |
| 513 | return (p->isTemp) ? NULL : p; |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 514 | } |
| 515 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 516 | extern bool oatIsDirty(CompilationUnit* cUnit, int reg) |
| 517 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 518 | RegisterInfo* p = oatGetRegInfo(cUnit, reg); |
| 519 | return p->dirty; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Similar to oatAllocTemp(), but forces the allocation of a specific |
| 524 | * register. No check is made to see if the register was previously |
| 525 | * allocated. Use with caution. |
| 526 | */ |
| 527 | extern void oatLockTemp(CompilationUnit* cUnit, int reg) |
| 528 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 529 | RegisterInfo* p = cUnit->regPool->coreRegs; |
| 530 | int numRegs = cUnit->regPool->numCoreRegs; |
| 531 | int i; |
| 532 | for (i=0; i< numRegs; i++) { |
| 533 | if (p[i].reg == reg) { |
| 534 | DCHECK(p[i].isTemp); |
| 535 | p[i].inUse = true; |
| 536 | p[i].live = false; |
| 537 | return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 538 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 539 | } |
| 540 | p = cUnit->regPool->FPRegs; |
| 541 | numRegs = cUnit->regPool->numFPRegs; |
| 542 | for (i=0; i< numRegs; i++) { |
| 543 | if (p[i].reg == reg) { |
| 544 | DCHECK(p[i].isTemp); |
| 545 | p[i].inUse = true; |
| 546 | p[i].live = false; |
| 547 | return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 548 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 549 | } |
| 550 | LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 551 | } |
| 552 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 553 | static inline void resetDefBody(RegisterInfo* p) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 554 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 555 | p->defStart = NULL; |
| 556 | p->defEnd = NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 557 | } |
| 558 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 559 | extern void oatResetDef(CompilationUnit* cUnit, int reg) |
| 560 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 561 | resetDefBody(oatGetRegInfo(cUnit, reg)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 562 | } |
| 563 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 564 | void nullifyRange(CompilationUnit* cUnit, LIR *start, LIR *finish, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 565 | int sReg1, int sReg2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 566 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 567 | if (start && finish) { |
| 568 | LIR *p; |
| 569 | DCHECK_EQ(sReg1, sReg2); |
| 570 | for (p = start; ;p = p->next) { |
| 571 | oatNopLIR(p); |
| 572 | if (p == finish) |
| 573 | break; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 574 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 575 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Mark the beginning and end LIR of a def sequence. Note that |
| 580 | * on entry start points to the LIR prior to the beginning of the |
| 581 | * sequence. |
| 582 | */ |
| 583 | extern void oatMarkDef(CompilationUnit* cUnit, RegLocation rl, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 584 | LIR *start, LIR *finish) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 585 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 586 | DCHECK(!rl.wide); |
| 587 | DCHECK(start && start->next); |
| 588 | DCHECK(finish); |
| 589 | RegisterInfo* p = oatGetRegInfo(cUnit, rl.lowReg); |
| 590 | p->defStart = start->next; |
| 591 | p->defEnd = finish; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Mark the beginning and end LIR of a def sequence. Note that |
| 596 | * on entry start points to the LIR prior to the beginning of the |
| 597 | * sequence. |
| 598 | */ |
| 599 | extern void oatMarkDefWide(CompilationUnit* cUnit, RegLocation rl, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 600 | LIR *start, LIR *finish) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 601 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 602 | DCHECK(rl.wide); |
| 603 | DCHECK(start && start->next); |
| 604 | DCHECK(finish); |
| 605 | RegisterInfo* p = oatGetRegInfo(cUnit, rl.lowReg); |
| 606 | oatResetDef(cUnit, rl.highReg); // Only track low of pair |
| 607 | p->defStart = start->next; |
| 608 | p->defEnd = finish; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 609 | } |
| 610 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 611 | extern RegLocation oatWideToNarrow(CompilationUnit* cUnit, RegLocation rl) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 612 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 613 | DCHECK(rl.wide); |
| 614 | if (rl.location == kLocPhysReg) { |
| 615 | RegisterInfo* infoLo = oatGetRegInfo(cUnit, rl.lowReg); |
| 616 | RegisterInfo* infoHi = oatGetRegInfo(cUnit, rl.highReg); |
| 617 | if (infoLo->isTemp) { |
| 618 | infoLo->pair = false; |
| 619 | infoLo->defStart = NULL; |
| 620 | infoLo->defEnd = NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 621 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 622 | if (infoHi->isTemp) { |
| 623 | infoHi->pair = false; |
| 624 | infoHi->defStart = NULL; |
| 625 | infoHi->defEnd = NULL; |
| 626 | } |
| 627 | } |
| 628 | rl.wide = false; |
| 629 | return rl; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | extern void oatResetDefLoc(CompilationUnit* cUnit, RegLocation rl) |
| 633 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 634 | DCHECK(!rl.wide); |
| 635 | RegisterInfo* p = oatIsTemp(cUnit, rl.lowReg); |
| 636 | if (p && !(cUnit->disableOpt & (1 << kSuppressLoads))) { |
| 637 | DCHECK(!p->pair); |
| 638 | nullifyRange(cUnit, p->defStart, p->defEnd, p->sReg, rl.sRegLow); |
| 639 | } |
| 640 | oatResetDef(cUnit, rl.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | extern void oatResetDefLocWide(CompilationUnit* cUnit, RegLocation rl) |
| 644 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 645 | DCHECK(rl.wide); |
| 646 | RegisterInfo* pLow = oatIsTemp(cUnit, rl.lowReg); |
| 647 | RegisterInfo* pHigh = oatIsTemp(cUnit, rl.highReg); |
| 648 | if (pLow && !(cUnit->disableOpt & (1 << kSuppressLoads))) { |
| 649 | DCHECK(pLow->pair); |
| 650 | nullifyRange(cUnit, pLow->defStart, pLow->defEnd, pLow->sReg, rl.sRegLow); |
| 651 | } |
| 652 | if (pHigh && !(cUnit->disableOpt & (1 << kSuppressLoads))) { |
| 653 | DCHECK(pHigh->pair); |
| 654 | } |
| 655 | oatResetDef(cUnit, rl.lowReg); |
| 656 | oatResetDef(cUnit, rl.highReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | extern void oatResetDefTracking(CompilationUnit* cUnit) |
| 660 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 661 | int i; |
| 662 | for (i=0; i< cUnit->regPool->numCoreRegs; i++) { |
| 663 | resetDefBody(&cUnit->regPool->coreRegs[i]); |
| 664 | } |
| 665 | for (i=0; i< cUnit->regPool->numFPRegs; i++) { |
| 666 | resetDefBody(&cUnit->regPool->FPRegs[i]); |
| 667 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | extern void oatClobberAllRegs(CompilationUnit* cUnit) |
| 671 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 672 | int i; |
| 673 | for (i=0; i< cUnit->regPool->numCoreRegs; i++) { |
| 674 | clobberBody(cUnit, &cUnit->regPool->coreRegs[i]); |
| 675 | } |
| 676 | for (i=0; i< cUnit->regPool->numFPRegs; i++) { |
| 677 | clobberBody(cUnit, &cUnit->regPool->FPRegs[i]); |
| 678 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 679 | } |
| 680 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 681 | // Make sure nothing is live and dirty |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 682 | void flushAllRegsBody(CompilationUnit* cUnit, RegisterInfo* info, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 683 | int numRegs) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 684 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 685 | int i; |
| 686 | for (i=0; i < numRegs; i++) { |
| 687 | if (info[i].live && info[i].dirty) { |
| 688 | if (info[i].pair) { |
| 689 | oatFlushRegWide(cUnit, info[i].reg, info[i].partner); |
| 690 | } else { |
| 691 | oatFlushReg(cUnit, info[i].reg); |
| 692 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 693 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 694 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | extern void oatFlushAllRegs(CompilationUnit* cUnit) |
| 698 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 699 | flushAllRegsBody(cUnit, cUnit->regPool->coreRegs, |
| 700 | cUnit->regPool->numCoreRegs); |
| 701 | flushAllRegsBody(cUnit, cUnit->regPool->FPRegs, |
| 702 | cUnit->regPool->numFPRegs); |
| 703 | oatClobberAllRegs(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
| 707 | //TUNING: rewrite all of this reg stuff. Probably use an attribute table |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 708 | bool regClassMatches(int regClass, int reg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 709 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 710 | if (regClass == kAnyReg) { |
| 711 | return true; |
| 712 | } else if (regClass == kCoreReg) { |
| 713 | return !oatIsFpReg(reg); |
| 714 | } else { |
| 715 | return oatIsFpReg(reg); |
| 716 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | extern void oatMarkLive(CompilationUnit* cUnit, int reg, int sReg) |
| 720 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 721 | RegisterInfo* info = oatGetRegInfo(cUnit, reg); |
| 722 | if ((info->reg == reg) && (info->sReg == sReg) && info->live) { |
| 723 | return; /* already live */ |
| 724 | } else if (sReg != INVALID_SREG) { |
| 725 | oatClobberSReg(cUnit, sReg); |
| 726 | if (info->isTemp) { |
| 727 | info->live = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 728 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 729 | } else { |
| 730 | /* Can't be live if no associated sReg */ |
| 731 | DCHECK(info->isTemp); |
| 732 | info->live = false; |
| 733 | } |
| 734 | info->sReg = sReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | extern void oatMarkTemp(CompilationUnit* cUnit, int reg) |
| 738 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 739 | RegisterInfo* info = oatGetRegInfo(cUnit, reg); |
| 740 | info->isTemp = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 741 | } |
| 742 | |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 743 | extern void oatUnmarkTemp(CompilationUnit* cUnit, int reg) |
| 744 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 745 | RegisterInfo* info = oatGetRegInfo(cUnit, reg); |
| 746 | info->isTemp = false; |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 747 | } |
| 748 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 749 | extern void oatMarkPair(CompilationUnit* cUnit, int lowReg, int highReg) |
| 750 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 751 | RegisterInfo* infoLo = oatGetRegInfo(cUnit, lowReg); |
| 752 | RegisterInfo* infoHi = oatGetRegInfo(cUnit, highReg); |
| 753 | infoLo->pair = infoHi->pair = true; |
| 754 | infoLo->partner = highReg; |
| 755 | infoHi->partner = lowReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | extern void oatMarkClean(CompilationUnit* cUnit, RegLocation loc) |
| 759 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 760 | RegisterInfo* info = oatGetRegInfo(cUnit, loc.lowReg); |
| 761 | info->dirty = false; |
| 762 | if (loc.wide) { |
| 763 | info = oatGetRegInfo(cUnit, loc.highReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 764 | info->dirty = false; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 765 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | extern void oatMarkDirty(CompilationUnit* cUnit, RegLocation loc) |
| 769 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 770 | if (loc.home) { |
| 771 | // If already home, can't be dirty |
| 772 | return; |
| 773 | } |
| 774 | RegisterInfo* info = oatGetRegInfo(cUnit, loc.lowReg); |
| 775 | info->dirty = true; |
| 776 | if (loc.wide) { |
| 777 | info = oatGetRegInfo(cUnit, loc.highReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 778 | info->dirty = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 779 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | extern void oatMarkInUse(CompilationUnit* cUnit, int reg) |
| 783 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 784 | RegisterInfo* info = oatGetRegInfo(cUnit, reg); |
| 785 | info->inUse = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 786 | } |
| 787 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 788 | void copyRegInfo(CompilationUnit* cUnit, int newReg, int oldReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 789 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 790 | RegisterInfo* newInfo = oatGetRegInfo(cUnit, newReg); |
| 791 | RegisterInfo* oldInfo = oatGetRegInfo(cUnit, oldReg); |
| 792 | // Target temp status must not change |
| 793 | bool isTemp = newInfo->isTemp; |
| 794 | *newInfo = *oldInfo; |
| 795 | // Restore target's temp status |
| 796 | newInfo->isTemp = isTemp; |
| 797 | newInfo->reg = newReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Return an updated location record with current in-register status. |
| 802 | * If the value lives in live temps, reflect that fact. No code |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 803 | * is generated. If the live value is part of an older pair, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 804 | * clobber both low and high. |
| 805 | * TUNING: clobbering both is a bit heavy-handed, but the alternative |
| 806 | * is a bit complex when dealing with FP regs. Examine code to see |
| 807 | * if it's worthwhile trying to be more clever here. |
| 808 | */ |
| 809 | |
| 810 | extern RegLocation oatUpdateLoc(CompilationUnit* cUnit, RegLocation loc) |
| 811 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 812 | DCHECK(!loc.wide); |
| 813 | DCHECK(oatCheckCorePoolSanity(cUnit)); |
| 814 | if (loc.location != kLocPhysReg) { |
| 815 | DCHECK((loc.location == kLocDalvikFrame) || |
| 816 | (loc.location == kLocCompilerTemp)); |
| 817 | RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg); |
| 818 | if (infoLo) { |
| 819 | if (infoLo->pair) { |
| 820 | oatClobber(cUnit, infoLo->reg); |
| 821 | oatClobber(cUnit, infoLo->partner); |
| 822 | oatFreeTemp(cUnit, infoLo->reg); |
| 823 | } else { |
| 824 | loc.lowReg = infoLo->reg; |
| 825 | loc.location = kLocPhysReg; |
| 826 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 827 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 828 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 829 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 830 | return loc; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 831 | } |
| 832 | |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 833 | bool oatCheckCorePoolSanity(CompilationUnit* cUnit) |
| 834 | { |
| 835 | for (static int i = 0; i < cUnit->regPool->numCoreRegs; i++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 836 | if (cUnit->regPool->coreRegs[i].pair) { |
| 837 | static int myReg = cUnit->regPool->coreRegs[i].reg; |
| 838 | static int mySreg = cUnit->regPool->coreRegs[i].sReg; |
| 839 | static int partnerReg = cUnit->regPool->coreRegs[i].partner; |
| 840 | static RegisterInfo* partner = oatGetRegInfo(cUnit, partnerReg); |
| 841 | DCHECK(partner != NULL); |
| 842 | DCHECK(partner->pair); |
| 843 | DCHECK_EQ(myReg, partner->partner); |
| 844 | static int partnerSreg = partner->sReg; |
| 845 | if (mySreg == INVALID_SREG) { |
| 846 | DCHECK_EQ(partnerSreg, INVALID_SREG); |
| 847 | } else { |
| 848 | int diff = mySreg - partnerSreg; |
| 849 | DCHECK((diff == -1) || (diff == 1)); |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 850 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 851 | } |
| 852 | if (!cUnit->regPool->coreRegs[i].live) { |
| 853 | DCHECK(cUnit->regPool->coreRegs[i].defStart == NULL); |
| 854 | DCHECK(cUnit->regPool->coreRegs[i].defEnd == NULL); |
| 855 | } |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 856 | } |
| 857 | return true; |
| 858 | } |
| 859 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 860 | /* see comments for updateLoc */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 861 | extern RegLocation oatUpdateLocWide(CompilationUnit* cUnit, RegLocation loc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 862 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 863 | DCHECK(loc.wide); |
| 864 | DCHECK(oatCheckCorePoolSanity(cUnit)); |
| 865 | if (loc.location != kLocPhysReg) { |
| 866 | DCHECK((loc.location == kLocDalvikFrame) || |
| 867 | (loc.location == kLocCompilerTemp)); |
| 868 | // Are the dalvik regs already live in physical registers? |
| 869 | RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg); |
| 870 | RegisterInfo* infoHi = allocLive(cUnit, |
| 871 | oatSRegHi(loc.sRegLow), kAnyReg); |
| 872 | bool match = true; |
| 873 | match = match && (infoLo != NULL); |
| 874 | match = match && (infoHi != NULL); |
| 875 | // Are they both core or both FP? |
| 876 | match = match && (oatIsFpReg(infoLo->reg) == oatIsFpReg(infoHi->reg)); |
| 877 | // If a pair of floating point singles, are they properly aligned? |
| 878 | if (match && oatIsFpReg(infoLo->reg)) { |
| 879 | match &= ((infoLo->reg & 0x1) == 0); |
| 880 | match &= ((infoHi->reg - infoLo->reg) == 1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 881 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 882 | // If previously used as a pair, it is the same pair? |
| 883 | if (match && (infoLo->pair || infoHi->pair)) { |
| 884 | match = (infoLo->pair == infoHi->pair); |
| 885 | match &= ((infoLo->reg == infoHi->partner) && |
| 886 | (infoHi->reg == infoLo->partner)); |
| 887 | } |
| 888 | if (match) { |
| 889 | // Can reuse - update the register usage info |
| 890 | loc.lowReg = infoLo->reg; |
| 891 | loc.highReg = infoHi->reg; |
| 892 | loc.location = kLocPhysReg; |
| 893 | oatMarkPair(cUnit, loc.lowReg, loc.highReg); |
| 894 | DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0)); |
| 895 | return loc; |
| 896 | } |
| 897 | // Can't easily reuse - clobber and free any overlaps |
| 898 | if (infoLo) { |
| 899 | oatClobber(cUnit, infoLo->reg); |
| 900 | oatFreeTemp(cUnit, infoLo->reg); |
| 901 | if (infoLo->pair) |
| 902 | oatClobber(cUnit, infoLo->partner); |
| 903 | } |
| 904 | if (infoHi) { |
| 905 | oatClobber(cUnit, infoHi->reg); |
| 906 | oatFreeTemp(cUnit, infoHi->reg); |
| 907 | if (infoHi->pair) |
| 908 | oatClobber(cUnit, infoHi->partner); |
| 909 | } |
| 910 | } |
| 911 | return loc; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 912 | } |
| 913 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 914 | |
| 915 | /* For use in cases we don't know (or care) width */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 916 | extern RegLocation oatUpdateRawLoc(CompilationUnit* cUnit, RegLocation loc) |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 917 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 918 | if (loc.wide) |
| 919 | return oatUpdateLocWide(cUnit, loc); |
| 920 | else |
| 921 | return oatUpdateLoc(cUnit, loc); |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 922 | } |
| 923 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 924 | RegLocation evalLocWide(CompilationUnit* cUnit, RegLocation loc, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 925 | int regClass, bool update) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 926 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 927 | DCHECK(loc.wide); |
| 928 | int newRegs; |
| 929 | int lowReg; |
| 930 | int highReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 931 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 932 | loc = oatUpdateLocWide(cUnit, loc); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 933 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 934 | /* If already in registers, we can assume proper form. Right reg class? */ |
| 935 | if (loc.location == kLocPhysReg) { |
| 936 | DCHECK_EQ(oatIsFpReg(loc.lowReg), oatIsFpReg(loc.highReg)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 937 | DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 938 | if (!regClassMatches(regClass, loc.lowReg)) { |
| 939 | /* Wrong register class. Reallocate and copy */ |
| 940 | newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass); |
| 941 | lowReg = newRegs & 0xff; |
| 942 | highReg = (newRegs >> 8) & 0xff; |
| 943 | oatRegCopyWide(cUnit, lowReg, highReg, loc.lowReg, |
| 944 | loc.highReg); |
| 945 | copyRegInfo(cUnit, lowReg, loc.lowReg); |
| 946 | copyRegInfo(cUnit, highReg, loc.highReg); |
| 947 | oatClobber(cUnit, loc.lowReg); |
| 948 | oatClobber(cUnit, loc.highReg); |
| 949 | loc.lowReg = lowReg; |
| 950 | loc.highReg = highReg; |
| 951 | oatMarkPair(cUnit, loc.lowReg, loc.highReg); |
| 952 | DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0)); |
| 953 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 954 | return loc; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | DCHECK_NE(loc.sRegLow, INVALID_SREG); |
| 958 | DCHECK_NE(oatSRegHi(loc.sRegLow), INVALID_SREG); |
| 959 | |
| 960 | newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass); |
| 961 | loc.lowReg = newRegs & 0xff; |
| 962 | loc.highReg = (newRegs >> 8) & 0xff; |
| 963 | |
| 964 | oatMarkPair(cUnit, loc.lowReg, loc.highReg); |
| 965 | if (update) { |
| 966 | loc.location = kLocPhysReg; |
| 967 | oatMarkLive(cUnit, loc.lowReg, loc.sRegLow); |
| 968 | oatMarkLive(cUnit, loc.highReg, oatSRegHi(loc.sRegLow)); |
| 969 | } |
| 970 | DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0)); |
| 971 | return loc; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | extern RegLocation oatEvalLoc(CompilationUnit* cUnit, RegLocation loc, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 975 | int regClass, bool update) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 976 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 977 | int newReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 978 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 979 | if (loc.wide) |
| 980 | return evalLocWide(cUnit, loc, regClass, update); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 981 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 982 | loc = oatUpdateLoc(cUnit, loc); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 983 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 984 | if (loc.location == kLocPhysReg) { |
| 985 | if (!regClassMatches(regClass, loc.lowReg)) { |
| 986 | /* Wrong register class. Realloc, copy and transfer ownership */ |
| 987 | newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass); |
| 988 | oatRegCopy(cUnit, newReg, loc.lowReg); |
| 989 | copyRegInfo(cUnit, newReg, loc.lowReg); |
| 990 | oatClobber(cUnit, loc.lowReg); |
| 991 | loc.lowReg = newReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 992 | } |
| 993 | return loc; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | DCHECK_NE(loc.sRegLow, INVALID_SREG); |
| 997 | |
| 998 | newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass); |
| 999 | loc.lowReg = newReg; |
| 1000 | |
| 1001 | if (update) { |
| 1002 | loc.location = kLocPhysReg; |
| 1003 | oatMarkLive(cUnit, loc.lowReg, loc.sRegLow); |
| 1004 | } |
| 1005 | return loc; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1008 | extern RegLocation oatGetRawSrc(CompilationUnit* cUnit, MIR* mir, int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1009 | { |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1010 | DCHECK(num < mir->ssaRep->numUses); |
| 1011 | RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]]; |
| 1012 | DCHECK(!res.wide || num < (mir->ssaRep->numUses - 1)); |
| 1013 | return res; |
| 1014 | } |
| 1015 | extern RegLocation oatGetRawDest(CompilationUnit* cUnit, MIR* mir) |
| 1016 | { |
| 1017 | DCHECK(mir->ssaRep->numDefs > 0); |
| 1018 | RegLocation res = cUnit->regLocation[mir->ssaRep->defs[0]]; |
| 1019 | DCHECK(!res.wide || mir->ssaRep->numDefs == 2); |
| 1020 | return res; |
| 1021 | } |
| 1022 | extern RegLocation oatGetDest(CompilationUnit* cUnit, MIR* mir) |
| 1023 | { |
| 1024 | RegLocation res = oatGetRawDest(cUnit, mir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1025 | DCHECK(!res.wide); |
| 1026 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1027 | } |
| 1028 | extern RegLocation oatGetSrc(CompilationUnit* cUnit, MIR* mir, int num) |
| 1029 | { |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1030 | RegLocation res = oatGetRawSrc(cUnit, mir, num); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1031 | DCHECK(!res.wide); |
| 1032 | return res; |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 1033 | } |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1034 | extern RegLocation oatGetDestWide(CompilationUnit* cUnit, MIR* mir) |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 1035 | { |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1036 | RegLocation res = oatGetRawDest(cUnit, mir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1037 | DCHECK(res.wide); |
| 1038 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | extern RegLocation oatGetSrcWide(CompilationUnit* cUnit, MIR* mir, |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1042 | int low) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1043 | { |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 1044 | RegLocation res = oatGetRawSrc(cUnit, mir, low); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1045 | DCHECK(res.wide); |
| 1046 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1047 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 1048 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1049 | /* USE SSA names to count references of base Dalvik vRegs. */ |
| 1050 | void oatCountRefs(CompilationUnit *cUnit, BasicBlock* bb, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1051 | RefCounts* coreCounts, RefCounts* fpCounts) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1052 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1053 | if ((cUnit->disableOpt & (1 << kPromoteRegs)) || |
| 1054 | !((bb->blockType == kEntryBlock) || (bb->blockType == kExitBlock) || |
| 1055 | (bb->blockType == kDalvikByteCode))) { |
| 1056 | return; |
| 1057 | } |
| 1058 | for (int i = 0; i < cUnit->numSSARegs;) { |
| 1059 | RegLocation loc = cUnit->regLocation[i]; |
| 1060 | RefCounts* counts = loc.fp ? fpCounts : coreCounts; |
| 1061 | int pMapIdx = SRegToPMap(cUnit, loc.sRegLow); |
| 1062 | if (loc.defined) { |
| 1063 | counts[pMapIdx].count += cUnit->useCounts.elemList[i]; |
buzbee | 239c4e7 | 2012-03-16 08:42:29 -0700 | [diff] [blame] | 1064 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1065 | if (loc.wide) { |
| 1066 | if (loc.defined) { |
| 1067 | if (loc.fp) { |
| 1068 | counts[pMapIdx].doubleStart = true; |
| 1069 | counts[pMapIdx+1].count += cUnit->useCounts.elemList[i+1]; |
buzbee | 239c4e7 | 2012-03-16 08:42:29 -0700 | [diff] [blame] | 1070 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1071 | } |
| 1072 | i += 2; |
| 1073 | } else { |
| 1074 | i++; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1075 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1076 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* qsort callback function, sort descending */ |
| 1080 | int oatSortCounts(const void *val1, const void *val2) |
| 1081 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1082 | const RefCounts* op1 = (const RefCounts*)val1; |
| 1083 | const RefCounts* op2 = (const RefCounts*)val2; |
| 1084 | return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | void oatDumpCounts(const RefCounts* arr, int size, const char* msg) |
| 1088 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1089 | LOG(INFO) << msg; |
| 1090 | for (int i = 0; i < size; i++) { |
| 1091 | LOG(INFO) << "sReg[" << arr[i].sReg << "]: " << arr[i].count; |
| 1092 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Note: some portions of this code required even if the kPromoteRegs |
| 1097 | * optimization is disabled. |
| 1098 | */ |
| 1099 | extern void oatDoPromotion(CompilationUnit* cUnit) |
| 1100 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1101 | int regBias = cUnit->numCompilerTemps + 1; |
| 1102 | int dalvikRegs = cUnit->numDalvikRegisters; |
| 1103 | int numRegs = dalvikRegs + regBias; |
| 1104 | const int promotionThreshold = 2; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1105 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1106 | // Allow target code to add any special registers |
| 1107 | oatAdjustSpillMask(cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1108 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1109 | /* |
| 1110 | * Simple register promotion. Just do a static count of the uses |
| 1111 | * of Dalvik registers. Note that we examine the SSA names, but |
| 1112 | * count based on original Dalvik register name. Count refs |
| 1113 | * separately based on type in order to give allocation |
| 1114 | * preference to fp doubles - which must be allocated sequential |
| 1115 | * physical single fp registers started with an even-numbered |
| 1116 | * reg. |
| 1117 | * TUNING: replace with linear scan once we have the ability |
| 1118 | * to describe register live ranges for GC. |
| 1119 | */ |
| 1120 | RefCounts *coreRegs = (RefCounts *) |
| 1121 | oatNew(cUnit, sizeof(RefCounts) * numRegs, true, kAllocRegAlloc); |
| 1122 | RefCounts *fpRegs = (RefCounts *) |
| 1123 | oatNew(cUnit, sizeof(RefCounts) * numRegs, true, kAllocRegAlloc); |
| 1124 | // Set ssa names for original Dalvik registers |
| 1125 | for (int i = 0; i < dalvikRegs; i++) { |
| 1126 | coreRegs[i].sReg = fpRegs[i].sReg = i; |
| 1127 | } |
| 1128 | // Set ssa name for Method* |
| 1129 | coreRegs[dalvikRegs].sReg = cUnit->methodSReg; |
| 1130 | fpRegs[dalvikRegs].sReg = cUnit->methodSReg; // For consistecy |
| 1131 | // Set ssa names for compilerTemps |
| 1132 | for (int i = 1; i <= cUnit->numCompilerTemps; i++) { |
| 1133 | CompilerTemp* ct = (CompilerTemp*)cUnit->compilerTemps.elemList[i]; |
| 1134 | coreRegs[dalvikRegs + i].sReg = ct->sReg; |
| 1135 | fpRegs[dalvikRegs + i].sReg = ct->sReg; |
| 1136 | } |
| 1137 | |
| 1138 | GrowableListIterator iterator; |
| 1139 | oatGrowableListIteratorInit(&cUnit->blockList, &iterator); |
| 1140 | while (true) { |
| 1141 | BasicBlock* bb; |
| 1142 | bb = (BasicBlock*)oatGrowableListIteratorNext(&iterator); |
| 1143 | if (bb == NULL) break; |
| 1144 | oatCountRefs(cUnit, bb, coreRegs, fpRegs); |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Ideally, we'd allocate doubles starting with an even-numbered |
| 1149 | * register. Bias the counts to try to allocate any vreg that's |
| 1150 | * used as the start of a pair first. |
| 1151 | */ |
| 1152 | for (int i = 0; i < numRegs; i++) { |
| 1153 | if (fpRegs[i].doubleStart) { |
| 1154 | fpRegs[i].count *= 2; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1155 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | // Sort the count arrays |
| 1159 | qsort(coreRegs, numRegs, sizeof(RefCounts), oatSortCounts); |
| 1160 | qsort(fpRegs, numRegs, sizeof(RefCounts), oatSortCounts); |
| 1161 | |
| 1162 | if (cUnit->printMe) { |
| 1163 | oatDumpCounts(coreRegs, numRegs, "Core regs after sort"); |
| 1164 | oatDumpCounts(fpRegs, numRegs, "Fp regs after sort"); |
| 1165 | } |
| 1166 | |
| 1167 | if (!(cUnit->disableOpt & (1 << kPromoteRegs))) { |
| 1168 | // Promote fpRegs |
| 1169 | for (int i = 0; (i < numRegs) && |
| 1170 | (fpRegs[i].count >= promotionThreshold ); i++) { |
| 1171 | int pMapIdx = SRegToPMap(cUnit, fpRegs[i].sReg); |
| 1172 | if (cUnit->promotionMap[pMapIdx].fpLocation != kLocPhysReg) { |
| 1173 | int reg = oatAllocPreservedFPReg(cUnit, fpRegs[i].sReg, |
| 1174 | fpRegs[i].doubleStart); |
| 1175 | if (reg < 0) { |
| 1176 | break; // No more left |
| 1177 | } |
| 1178 | } |
buzbee | 239c4e7 | 2012-03-16 08:42:29 -0700 | [diff] [blame] | 1179 | } |
buzbee | 9c044ce | 2012-03-18 13:24:07 -0700 | [diff] [blame] | 1180 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1181 | // Promote core regs |
| 1182 | for (int i = 0; (i < numRegs) && |
| 1183 | (coreRegs[i].count > promotionThreshold); i++) { |
| 1184 | int pMapIdx = SRegToPMap(cUnit, coreRegs[i].sReg); |
| 1185 | if (cUnit->promotionMap[pMapIdx].coreLocation != |
| 1186 | kLocPhysReg) { |
| 1187 | int reg = oatAllocPreservedCoreReg(cUnit, coreRegs[i].sReg); |
| 1188 | if (reg < 0) { |
| 1189 | break; // No more left |
| 1190 | } |
| 1191 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1192 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1193 | } else if (cUnit->qdMode) { |
| 1194 | oatAllocPreservedCoreReg(cUnit, cUnit->methodSReg); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1195 | for (int i = 0; i < numRegs; i++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1196 | int reg = oatAllocPreservedCoreReg(cUnit, i); |
| 1197 | if (reg < 0) { |
| 1198 | break; // No more left |
| 1199 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1200 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1201 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1202 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1203 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1204 | // Now, update SSA names to new home locations |
| 1205 | for (int i = 0; i < cUnit->numSSARegs; i++) { |
| 1206 | RegLocation *curr = &cUnit->regLocation[i]; |
| 1207 | int pMapIdx = SRegToPMap(cUnit, curr->sRegLow); |
| 1208 | if (!curr->wide) { |
| 1209 | if (curr->fp) { |
| 1210 | if (cUnit->promotionMap[pMapIdx].fpLocation == kLocPhysReg) { |
| 1211 | curr->location = kLocPhysReg; |
| 1212 | curr->lowReg = cUnit->promotionMap[pMapIdx].fpReg; |
| 1213 | curr->home = true; |
| 1214 | } |
| 1215 | } else { |
| 1216 | if (cUnit->promotionMap[pMapIdx].coreLocation == kLocPhysReg) { |
| 1217 | curr->location = kLocPhysReg; |
| 1218 | curr->lowReg = cUnit->promotionMap[pMapIdx].coreReg; |
| 1219 | curr->home = true; |
| 1220 | } |
| 1221 | } |
| 1222 | curr->highReg = INVALID_REG; |
| 1223 | } else { |
| 1224 | if (curr->highWord) { |
| 1225 | continue; |
| 1226 | } |
| 1227 | if (curr->fp) { |
| 1228 | if ((cUnit->promotionMap[pMapIdx].fpLocation == kLocPhysReg) && |
| 1229 | (cUnit->promotionMap[pMapIdx+1].fpLocation == |
| 1230 | kLocPhysReg)) { |
| 1231 | int lowReg = cUnit->promotionMap[pMapIdx].fpReg; |
| 1232 | int highReg = cUnit->promotionMap[pMapIdx+1].fpReg; |
| 1233 | // Doubles require pair of singles starting at even reg |
| 1234 | if (((lowReg & 0x1) == 0) && ((lowReg + 1) == highReg)) { |
| 1235 | curr->location = kLocPhysReg; |
| 1236 | curr->lowReg = lowReg; |
| 1237 | curr->highReg = highReg; |
| 1238 | curr->home = true; |
| 1239 | } |
| 1240 | } |
| 1241 | } else { |
| 1242 | if ((cUnit->promotionMap[pMapIdx].coreLocation == kLocPhysReg) |
| 1243 | && (cUnit->promotionMap[pMapIdx+1].coreLocation == |
| 1244 | kLocPhysReg)) { |
| 1245 | curr->location = kLocPhysReg; |
| 1246 | curr->lowReg = cUnit->promotionMap[pMapIdx].coreReg; |
| 1247 | curr->highReg = cUnit->promotionMap[pMapIdx+1].coreReg; |
| 1248 | curr->home = true; |
| 1249 | } |
| 1250 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1251 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1252 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /* Returns sp-relative offset in bytes for a VReg */ |
| 1256 | extern int oatVRegOffset(CompilationUnit* cUnit, int vReg) |
| 1257 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1258 | return Frame::GetVRegOffset(cUnit->code_item, cUnit->coreSpillMask, |
| 1259 | cUnit->fpSpillMask, cUnit->frameSize, vReg); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /* Returns sp-relative offset in bytes for a SReg */ |
| 1263 | extern int oatSRegOffset(CompilationUnit* cUnit, int sReg) |
| 1264 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1265 | return oatVRegOffset(cUnit, SRegToVReg(cUnit, sReg)); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 1268 | } // namespace art |