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