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