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 Arm-specific register alloction support. |
| 19 | */ |
| 20 | |
| 21 | #include "../../CompilerUtility.h" |
| 22 | #include "../../CompilerIR.h" |
| 23 | #include "../..//Dataflow.h" |
| 24 | #include "ArmLIR.h" |
| 25 | #include "Codegen.h" |
| 26 | #include "../Ralloc.h" |
| 27 | |
| 28 | /* |
| 29 | * Placeholder routine until we do proper register allocation. |
| 30 | */ |
| 31 | |
| 32 | typedef struct RefCounts { |
| 33 | int count; |
| 34 | int sReg; |
| 35 | bool doubleStart; // Starting vReg for a double |
| 36 | } RefCounts; |
| 37 | |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 38 | /* USE SSA names to count references of base Dalvik vRegs. */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 39 | STATIC void countRefs(CompilationUnit *cUnit, BasicBlock* bb, |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 40 | RefCounts* coreCounts, RefCounts* fpCounts) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 41 | { |
| 42 | MIR* mir; |
| 43 | if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock && |
| 44 | bb->blockType != kExitBlock) |
| 45 | return; |
| 46 | |
| 47 | for (mir = bb->firstMIRInsn; mir; mir = mir->next) { |
| 48 | SSARepresentation *ssaRep = mir->ssaRep; |
| 49 | if (ssaRep) { |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 50 | for (int i = 0; i < ssaRep->numDefs;) { |
| 51 | RegLocation loc = cUnit->regLocation[ssaRep->defs[i]]; |
| 52 | RefCounts* counts = loc.fp ? fpCounts : coreCounts; |
| 53 | int vReg = oatS2VReg(cUnit, ssaRep->defs[i]); |
| 54 | if (loc.defined) { |
| 55 | counts[vReg].count++; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 56 | } |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 57 | if (loc.wide) { |
| 58 | if (loc.defined) { |
| 59 | if (loc.fp) { |
| 60 | counts[vReg].doubleStart = true; |
| 61 | } |
| 62 | counts[vReg+1].count++; |
| 63 | } |
| 64 | i += 2; |
| 65 | } else { |
| 66 | i++; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 67 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 68 | } |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 69 | for (int i = 0; i < ssaRep->numUses;) { |
| 70 | RegLocation loc = cUnit->regLocation[ssaRep->uses[i]]; |
| 71 | RefCounts* counts = loc.fp ? fpCounts : coreCounts; |
| 72 | int vReg = oatS2VReg(cUnit, ssaRep->uses[i]); |
| 73 | if (loc.defined) { |
| 74 | counts[vReg].count++; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 75 | } |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 76 | if (loc.wide) { |
| 77 | if (loc.defined) { |
| 78 | if (loc.fp) { |
| 79 | counts[vReg].doubleStart = true; |
| 80 | } |
| 81 | counts[vReg+1].count++; |
| 82 | } |
| 83 | i += 2; |
| 84 | } else { |
| 85 | i++; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* qsort callback function, sort descending */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 93 | STATIC int sortCounts(const void *val1, const void *val2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 94 | { |
| 95 | const RefCounts* op1 = (const RefCounts*)val1; |
| 96 | const RefCounts* op2 = (const RefCounts*)val2; |
| 97 | return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1); |
| 98 | } |
| 99 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 100 | STATIC void dumpCounts(const RefCounts* arr, int size, const char* msg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 101 | { |
| 102 | LOG(INFO) << msg; |
| 103 | for (int i = 0; i < size; i++) { |
| 104 | LOG(INFO) << "sReg[" << arr[i].sReg << "]: " << arr[i].count; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Note: some portions of this code required even if the kPromoteRegs |
| 110 | * optimization is disabled. |
| 111 | */ |
| 112 | extern void oatDoPromotion(CompilationUnit* cUnit) |
| 113 | { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 114 | int numRegs = cUnit->method->NumRegisters(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 117 | * TUNING: is leaf? Can't just use "hasInvoke" to determine as some |
| 118 | * instructions might call out to C/assembly helper functions. Until |
| 119 | * machinery is in place, always spill lr. |
| 120 | */ |
| 121 | cUnit->coreSpillMask |= (1 << rLR); |
buzbee | bbaf894 | 2011-10-02 13:08:29 -0700 | [diff] [blame] | 122 | cUnit->numCoreSpills++; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Simple hack for testing register allocation. Just do a static |
| 125 | * count of the uses of Dalvik registers. Note that we examine |
| 126 | * the SSA names, but count based on original Dalvik register name. |
| 127 | * Count refs separately based on type in order to give allocation |
| 128 | * preference to fp doubles - which must be allocated sequential |
| 129 | * physical single fp registers started with an even-numbered |
| 130 | * reg. |
| 131 | */ |
| 132 | RefCounts *coreRegs = (RefCounts *) |
| 133 | oatNew(sizeof(RefCounts) * numRegs, true); |
| 134 | RefCounts *fpRegs = (RefCounts *) |
| 135 | oatNew(sizeof(RefCounts) * numRegs, true); |
| 136 | for (int i = 0; i < numRegs; i++) { |
| 137 | coreRegs[i].sReg = fpRegs[i].sReg = i; |
| 138 | } |
| 139 | GrowableListIterator iterator; |
| 140 | oatGrowableListIteratorInit(&cUnit->blockList, &iterator); |
| 141 | while (true) { |
| 142 | BasicBlock* bb; |
| 143 | bb = (BasicBlock*)oatGrowableListIteratorNext(&iterator); |
| 144 | if (bb == NULL) break; |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 145 | countRefs(cUnit, bb, coreRegs, fpRegs); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Ideally, we'd allocate doubles starting with an even-numbered |
| 150 | * register. Bias the counts to try to allocate any vreg that's |
| 151 | * used as the start of a pair first. |
| 152 | */ |
| 153 | for (int i = 0; i < numRegs; i++) { |
| 154 | if (fpRegs[i].doubleStart) { |
| 155 | fpRegs[i].count *= 2; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Sort the count arrays |
| 160 | qsort(coreRegs, numRegs, sizeof(RefCounts), sortCounts); |
| 161 | qsort(fpRegs, numRegs, sizeof(RefCounts), sortCounts); |
| 162 | |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 163 | if (cUnit->printMe) { |
| 164 | dumpCounts(coreRegs, numRegs, "Core regs after sort"); |
| 165 | dumpCounts(fpRegs, numRegs, "Fp regs after sort"); |
| 166 | } |
| 167 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 168 | if (!(cUnit->disableOpt & (1 << kPromoteRegs))) { |
| 169 | // Promote fpRegs |
| 170 | for (int i = 0; (fpRegs[i].count > 0) && (i < numRegs); i++) { |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 171 | if (cUnit->promotionMap[fpRegs[i].sReg].fpLocation != kLocPhysReg) { |
buzbee | a50638b | 2011-11-02 15:15:06 -0700 | [diff] [blame^] | 172 | if (fpRegs[i].sReg >= cUnit->numRegs) { |
| 173 | // don't promote arg regs |
| 174 | continue; |
| 175 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 176 | int reg = oatAllocPreservedFPReg(cUnit, fpRegs[i].sReg, |
| 177 | fpRegs[i].doubleStart); |
| 178 | if (reg < 0) { |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 179 | break; // No more left |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Promote core regs |
| 185 | for (int i = 0; (coreRegs[i].count > 0) && i < numRegs; i++) { |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 186 | if (cUnit->promotionMap[coreRegs[i].sReg].coreLocation != |
| 187 | kLocPhysReg) { |
buzbee | a50638b | 2011-11-02 15:15:06 -0700 | [diff] [blame^] | 188 | if (coreRegs[i].sReg >= cUnit->numRegs) { |
| 189 | // don't promote arg regs |
| 190 | continue; |
| 191 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 192 | int reg = oatAllocPreservedCoreReg(cUnit, coreRegs[i].sReg); |
| 193 | if (reg < 0) { |
| 194 | break; // No more left |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Now, update SSA names to new home locations |
| 201 | for (int i = 0; i < cUnit->numSSARegs; i++) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 202 | RegLocation *curr = &cUnit->regLocation[i]; |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 203 | int baseVReg = oatS2VReg(cUnit, curr->sRegLow); |
| 204 | if (!curr->wide) { |
| 205 | if (curr->fp) { |
| 206 | if (cUnit->promotionMap[baseVReg].fpLocation == kLocPhysReg) { |
| 207 | curr->location = kLocPhysReg; |
| 208 | curr->lowReg = cUnit->promotionMap[baseVReg].fpReg; |
| 209 | curr->home = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 210 | } |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 211 | } else { |
| 212 | if (cUnit->promotionMap[baseVReg].coreLocation == kLocPhysReg) { |
| 213 | curr->location = kLocPhysReg; |
| 214 | curr->lowReg = cUnit->promotionMap[baseVReg].coreReg; |
| 215 | curr->home = true; |
| 216 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 217 | } |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 218 | curr->highReg = INVALID_REG; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 219 | } else { |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 220 | if (curr->highWord) { |
| 221 | continue; |
| 222 | } |
| 223 | if (curr->fp) { |
| 224 | if ((cUnit->promotionMap[baseVReg].fpLocation == kLocPhysReg) && |
| 225 | (cUnit->promotionMap[baseVReg+1].fpLocation == |
| 226 | kLocPhysReg)) { |
| 227 | int lowReg = cUnit->promotionMap[baseVReg].fpReg; |
| 228 | int highReg = cUnit->promotionMap[baseVReg+1].fpReg; |
| 229 | // Doubles require pair of singles starting at even reg |
| 230 | if (((lowReg & 0x1) == 0) && ((lowReg + 1) == highReg)) { |
| 231 | curr->location = kLocPhysReg; |
| 232 | curr->lowReg = lowReg; |
| 233 | curr->highReg = highReg; |
| 234 | curr->home = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 235 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 236 | } |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 237 | } else { |
| 238 | if ((cUnit->promotionMap[baseVReg].coreLocation == kLocPhysReg) |
| 239 | && (cUnit->promotionMap[baseVReg+1].coreLocation == |
| 240 | kLocPhysReg)) { |
| 241 | curr->location = kLocPhysReg; |
| 242 | curr->lowReg = cUnit->promotionMap[baseVReg].coreReg; |
| 243 | curr->highReg = cUnit->promotionMap[baseVReg+1].coreReg; |
| 244 | curr->home = true; |
| 245 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 251 | /* Returns sp-relative offset in bytes for a VReg */ |
| 252 | extern int oatVRegOffset(CompilationUnit* cUnit, int vReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 253 | { |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 254 | return (vReg < cUnit->numRegs) ? cUnit->regsOffset + (vReg << 2) : |
| 255 | cUnit->insOffset + ((vReg - cUnit->numRegs) << 2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 256 | } |
| 257 | |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 258 | /* Returns sp-relative offset in bytes for a SReg */ |
| 259 | extern int oatSRegOffset(CompilationUnit* cUnit, int sReg) |
| 260 | { |
| 261 | return oatVRegOffset(cUnit, oatS2VReg(cUnit, sReg)); |
| 262 | } |
| 263 | |
| 264 | |
buzbee | c41e5b5 | 2011-09-23 12:46:19 -0700 | [diff] [blame] | 265 | /* Return sp-relative offset in bytes using Method* */ |
| 266 | extern int oatVRegOffsetFromMethod(Method* method, int reg) |
| 267 | { |
| 268 | int numIns = method->NumIns(); |
| 269 | int numRegs = method->NumRegisters() - numIns; |
| 270 | int numOuts = method->NumOuts(); |
| 271 | int numSpills = __builtin_popcount(method->GetCoreSpillMask()) + |
| 272 | __builtin_popcount(method->GetFpSpillMask()); |
| 273 | int numPadding = (STACK_ALIGN_WORDS - |
| 274 | (numSpills + numRegs + numOuts + 2)) & (STACK_ALIGN_WORDS-1); |
| 275 | int regsOffset = (numOuts + numPadding + 1) * 4; |
| 276 | int insOffset = method->GetFrameSizeInBytes() + 4; |
| 277 | return (reg < numRegs) ? regsOffset + (reg << 2) : |
| 278 | insOffset + ((reg - numRegs) << 2); |
| 279 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 280 | |
| 281 | /* Clobber all regs that might be used by an external C call */ |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 282 | extern void oatClobberCalleeSave(CompilationUnit *cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 283 | { |
| 284 | oatClobber(cUnit, r0); |
| 285 | oatClobber(cUnit, r1); |
| 286 | oatClobber(cUnit, r2); |
| 287 | oatClobber(cUnit, r3); |
| 288 | oatClobber(cUnit, r12); |
| 289 | oatClobber(cUnit, r14lr); |
| 290 | } |
| 291 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 292 | extern RegLocation oatGetReturnWide(CompilationUnit* cUnit) |
| 293 | { |
| 294 | RegLocation res = LOC_C_RETURN_WIDE; |
| 295 | oatClobber(cUnit, r0); |
| 296 | oatClobber(cUnit, r1); |
| 297 | oatMarkInUse(cUnit, r0); |
| 298 | oatMarkInUse(cUnit, r1); |
| 299 | oatMarkPair(cUnit, res.lowReg, res.highReg); |
| 300 | return res; |
| 301 | } |
| 302 | |
| 303 | extern RegLocation oatGetReturnWideAlt(CompilationUnit* cUnit) |
| 304 | { |
| 305 | RegLocation res = LOC_C_RETURN_WIDE; |
| 306 | res.lowReg = r2; |
| 307 | res.highReg = r3; |
| 308 | oatClobber(cUnit, r2); |
| 309 | oatClobber(cUnit, r3); |
| 310 | oatMarkInUse(cUnit, r2); |
| 311 | oatMarkInUse(cUnit, r3); |
| 312 | oatMarkPair(cUnit, res.lowReg, res.highReg); |
| 313 | return res; |
| 314 | } |
| 315 | |
| 316 | extern RegLocation oatGetReturn(CompilationUnit* cUnit) |
| 317 | { |
| 318 | RegLocation res = LOC_C_RETURN; |
| 319 | oatClobber(cUnit, r0); |
| 320 | oatMarkInUse(cUnit, r0); |
| 321 | return res; |
| 322 | } |
| 323 | |
| 324 | extern RegLocation oatGetReturnAlt(CompilationUnit* cUnit) |
| 325 | { |
| 326 | RegLocation res = LOC_C_RETURN; |
| 327 | res.lowReg = r1; |
| 328 | oatClobber(cUnit, r1); |
| 329 | oatMarkInUse(cUnit, r1); |
| 330 | return res; |
| 331 | } |
buzbee | 6825326 | 2011-10-07 14:02:25 -0700 | [diff] [blame] | 332 | |
| 333 | extern RegisterInfo* oatGetRegInfo(CompilationUnit* cUnit, int reg) |
| 334 | { |
| 335 | return FPREG(reg) ? &cUnit->regPool->FPRegs[reg & FP_REG_MASK] |
| 336 | : &cUnit->regPool->coreRegs[reg]; |
| 337 | } |