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