blob: e9a99c14ff436a3f190a0da802eed485eb780d5d [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
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
buzbeeb046e162012-10-30 15:48:42 -070017/* This file contains register alloction support. */
buzbee67bf8852011-08-17 17:51:35 -070018
buzbeeefc63692012-11-14 16:31:52 -080019#include "../compiler_utility.h"
20#include "../compiler_ir.h"
21#include "../dataflow.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080022#include "ralloc_util.h"
23#include "codegen_util.h"
buzbee67bf8852011-08-17 17:51:35 -070024
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
buzbee67bf8852011-08-17 17:51:35 -070027/*
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 */
buzbeeaad94382012-11-21 07:40:50 -080032void ResetRegPool(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -070033{
Bill Buzbeea114add2012-05-03 15:00:40 -070034 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 }
buzbee67bf8852011-08-17 17:51:35 -070043}
44
buzbeee3acd072012-02-25 17:03:10 -080045 /*
46 * Set up temp & preserved register pools specialized by target.
47 * Note: numRegs may be zero.
48 */
buzbeeaad94382012-11-21 07:40:50 -080049void CompilerInitPool(RegisterInfo* regs, int* regNums, int num)
buzbee67bf8852011-08-17 17:51:35 -070050{
Bill Buzbeea114add2012-05-03 15:00:40 -070051 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 }
buzbee67bf8852011-08-17 17:51:35 -070061}
62
buzbeeaad94382012-11-21 07:40:50 -080063static void DumpRegPool(RegisterInfo* p, int numRegs)
buzbee67bf8852011-08-17 17:51:35 -070064{
Bill Buzbeea114add2012-05-03 15:00:40 -070065 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,
buzbeecbd6d442012-11-17 14:11:25 -080070 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 Buzbeea114add2012-05-03 15:00:40 -070072 }
73 LOG(INFO) << "================================================";
buzbee67bf8852011-08-17 17:51:35 -070074}
75
buzbee52a77fc2012-11-20 19:50:46 -080076void DumpCoreRegPool(CompilationUnit* cUnit)
buzbee6181f792011-09-29 11:14:04 -070077{
buzbee52a77fc2012-11-20 19:50:46 -080078 DumpRegPool(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs);
buzbee6181f792011-09-29 11:14:04 -070079}
80
buzbee52a77fc2012-11-20 19:50:46 -080081void DumpFpRegPool(CompilationUnit* cUnit)
buzbee6181f792011-09-29 11:14:04 -070082{
buzbee52a77fc2012-11-20 19:50:46 -080083 DumpRegPool(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs);
buzbee6181f792011-09-29 11:14:04 -070084}
85
buzbee67bf8852011-08-17 17:51:35 -070086/* Mark a temp register as dead. Does not affect allocation state. */
buzbeeaad94382012-11-21 07:40:50 -080087static void ClobberBody(CompilationUnit *cUnit, RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -070088{
Bill Buzbeea114add2012-05-03 15:00:40 -070089 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;
buzbee52a77fc2012-11-20 19:50:46 -080097 Clobber(cUnit, p->partner);
buzbee67bf8852011-08-17 17:51:35 -070098 }
Bill Buzbeea114add2012-05-03 15:00:40 -070099 }
buzbee67bf8852011-08-17 17:51:35 -0700100}
101
buzbee5abfa3e2012-01-31 17:01:43 -0800102/* Mark a temp register as dead. Does not affect allocation state. */
buzbee52a77fc2012-11-20 19:50:46 -0800103void Clobber(CompilationUnit* cUnit, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800104{
buzbee52a77fc2012-11-20 19:50:46 -0800105 ClobberBody(cUnit, GetRegInfo(cUnit, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800106}
107
buzbeeaad94382012-11-21 07:40:50 -0800108static void ClobberSRegBody(RegisterInfo* p, int numRegs, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700109{
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 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;
buzbee67bf8852011-08-17 17:51:35 -0700118 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 }
buzbee67bf8852011-08-17 17:51:35 -0700120}
121
122/* Clobber any temp associated with an sReg. Could be in either class */
buzbeeaad94382012-11-21 07:40:50 -0800123void ClobberSReg(CompilationUnit* cUnit, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700124{
buzbee3d661942012-03-14 17:37:27 -0700125#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 /* Reset live temp tracking sanity checker */
127 if (sReg == cUnit->liveSReg) {
128 cUnit->liveSReg = INVALID_SREG;
129 }
buzbee3d661942012-03-14 17:37:27 -0700130#endif
buzbee52a77fc2012-11-20 19:50:46 -0800131 ClobberSRegBody(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs, sReg);
132 ClobberSRegBody(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs, sReg);
buzbee67bf8852011-08-17 17:51:35 -0700133}
134
buzbee9c044ce2012-03-18 13:24:07 -0700135/*
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 Hughesbdf6c3d2012-03-20 13:43:53 -0700140 * vReg numbers to distinguish them and can have an arbitrary
buzbee9c044ce2012-03-18 13:24:07 -0700141 * ssa name (above the last original Dalvik register). This function
142 * maps SSA names to positions in the promotionMap array.
143 */
buzbeeaad94382012-11-21 07:40:50 -0800144static int SRegToPMap(CompilationUnit* cUnit, int sReg)
buzbeee1965672012-03-11 18:39:19 -0700145{
Bill Buzbeea114add2012-05-03 15:00:40 -0700146 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 }
buzbeee1965672012-03-11 18:39:19 -0700157}
158
buzbee52a77fc2012-11-20 19:50:46 -0800159void RecordCorePromotion(CompilationUnit* cUnit, int reg, int sReg)
buzbeeca7a5e42012-08-20 11:12:18 -0700160{
161 int pMapIdx = SRegToPMap(cUnit, sReg);
162 int vReg = SRegToVReg(cUnit, sReg);
buzbee52a77fc2012-11-20 19:50:46 -0800163 GetRegInfo(cUnit, reg)->inUse = true;
buzbeeca7a5e42012-08-20 11:12:18 -0700164 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
buzbee67bf8852011-08-17 17:51:35 -0700173/* Reserve a callee-save register. Return -1 if none available */
buzbeeaad94382012-11-21 07:40:50 -0800174static int AllocPreservedCoreReg(CompilationUnit* cUnit, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700175{
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 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 Buzbeea114add2012-05-03 15:00:40 -0700180 res = coreRegs[i].reg;
buzbee52a77fc2012-11-20 19:50:46 -0800181 RecordCorePromotion(cUnit, res, sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 break;
buzbee67bf8852011-08-17 17:51:35 -0700183 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 }
185 return res;
buzbee67bf8852011-08-17 17:51:35 -0700186}
187
buzbee52a77fc2012-11-20 19:50:46 -0800188void RecordFpPromotion(CompilationUnit* cUnit, int reg, int sReg)
buzbeeca7a5e42012-08-20 11:12:18 -0700189{
190 int pMapIdx = SRegToPMap(cUnit, sReg);
191 int vReg = SRegToVReg(cUnit, sReg);
buzbee52a77fc2012-11-20 19:50:46 -0800192 GetRegInfo(cUnit, reg)->inUse = true;
193 MarkPreservedSingle(cUnit, vReg, reg);
buzbeeca7a5e42012-08-20 11:12:18 -0700194 cUnit->promotionMap[pMapIdx].fpLocation = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800195 cUnit->promotionMap[pMapIdx].FpReg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700196}
197
buzbee67bf8852011-08-17 17:51:35 -0700198/*
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 */
buzbeeaad94382012-11-21 07:40:50 -0800203static int AllocPreservedSingle(CompilationUnit* cUnit, int sReg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700204{
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 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 Buzbeea114add2012-05-03 15:00:40 -0700210 res = FPRegs[i].reg;
buzbee52a77fc2012-11-20 19:50:46 -0800211 RecordFpPromotion(cUnit, res, sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 break;
buzbee67bf8852011-08-17 17:51:35 -0700213 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 }
215 return res;
buzbee67bf8852011-08-17 17:51:35 -0700216}
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 */
buzbeeaad94382012-11-21 07:40:50 -0800226static int AllocPreservedDouble(CompilationUnit* cUnit, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700227{
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 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?
buzbee52a77fc2012-11-20 19:50:46 -0800233 int highReg = cUnit->promotionMap[pMapIdx+1].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 if ((highReg & 1) == 0) {
235 // High reg is even - fail.
236 return res;
237 }
238 // Is the low reg of the pair free?
buzbee52a77fc2012-11-20 19:50:46 -0800239 RegisterInfo* p = GetRegInfo(cUnit, highReg-1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 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);
buzbee52a77fc2012-11-20 19:50:46 -0800248 MarkPreservedSingle(cUnit, vReg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 } 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;
buzbee52a77fc2012-11-20 19:50:46 -0800259 MarkPreservedSingle(cUnit, vReg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 FPRegs[i+1].inUse = true;
261 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
buzbee52a77fc2012-11-20 19:50:46 -0800262 MarkPreservedSingle(cUnit, vReg+1, res+1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 break;
264 }
buzbee67bf8852011-08-17 17:51:35 -0700265 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 }
267 if (res != -1) {
268 cUnit->promotionMap[pMapIdx].fpLocation = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800269 cUnit->promotionMap[pMapIdx].FpReg = res;
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 cUnit->promotionMap[pMapIdx+1].fpLocation = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800271 cUnit->promotionMap[pMapIdx+1].FpReg = res + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 }
273 return res;
buzbee67bf8852011-08-17 17:51:35 -0700274}
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 */
buzbeeaad94382012-11-21 07:40:50 -0800283static int AllocPreservedFPReg(CompilationUnit* cUnit, int sReg, bool doubleStart)
buzbee67bf8852011-08-17 17:51:35 -0700284{
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 int res = -1;
286 if (doubleStart) {
buzbee52a77fc2012-11-20 19:50:46 -0800287 res = AllocPreservedDouble(cUnit, sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 }
289 if (res == -1) {
buzbee52a77fc2012-11-20 19:50:46 -0800290 res = AllocPreservedSingle(cUnit, sReg, false /* try odd # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 }
292 if (res == -1)
buzbee52a77fc2012-11-20 19:50:46 -0800293 res = AllocPreservedSingle(cUnit, sReg, true /* try even # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 return res;
buzbee67bf8852011-08-17 17:51:35 -0700295}
296
buzbeeaad94382012-11-21 07:40:50 -0800297static int AllocTempBody(CompilationUnit* cUnit, RegisterInfo* p, int numRegs, int* nextTemp,
298 bool required)
buzbee67bf8852011-08-17 17:51:35 -0700299{
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800306 Clobber(cUnit, p[next].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 p[next].inUse = true;
308 p[next].pair = false;
309 *nextTemp = next + 1;
310 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700311 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800319 Clobber(cUnit, p[next].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 p[next].inUse = true;
321 p[next].pair = false;
322 *nextTemp = next + 1;
323 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700324 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 next++;
326 }
327 if (required) {
buzbee52a77fc2012-11-20 19:50:46 -0800328 CodegenDump(cUnit);
329 DumpRegPool(cUnit->regPool->coreRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 cUnit->regPool->numCoreRegs);
331 LOG(FATAL) << "No free temp registers";
332 }
333 return -1; // No register available
buzbee67bf8852011-08-17 17:51:35 -0700334}
335
336//REDO: too many assumptions.
buzbeeaad94382012-11-21 07:40:50 -0800337int AllocTempDouble(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700338{
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 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;
buzbee67bf8852011-08-17 17:51:35 -0700343
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 // 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)) {
buzbee52a77fc2012-11-20 19:50:46 -0800350 Clobber(cUnit, p[next].reg);
351 Clobber(cUnit, p[next+1].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 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;
buzbee67bf8852011-08-17 17:51:35 -0700361 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 next += 2;
363 }
364 next = cUnit->regPool->nextFPReg & ~0x1;
buzbeea50638b2011-11-02 15:15:06 -0700365
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 // 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800372 Clobber(cUnit, p[next].reg);
373 Clobber(cUnit, p[next+1].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 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;
buzbee67bf8852011-08-17 17:51:35 -0700383 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 next += 2;
385 }
386 LOG(FATAL) << "No free temp registers (pair)";
387 return -1;
buzbee67bf8852011-08-17 17:51:35 -0700388}
389
390/* Return a temp if one is available, -1 otherwise */
buzbeeaad94382012-11-21 07:40:50 -0800391int AllocFreeTemp(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700392{
buzbee52a77fc2012-11-20 19:50:46 -0800393 return AllocTempBody(cUnit, cUnit->regPool->coreRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 cUnit->regPool->numCoreRegs,
395 &cUnit->regPool->nextCoreReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700396}
397
buzbeeaad94382012-11-21 07:40:50 -0800398int AllocTemp(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700399{
buzbee52a77fc2012-11-20 19:50:46 -0800400 return AllocTempBody(cUnit, cUnit->regPool->coreRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700401 cUnit->regPool->numCoreRegs,
402 &cUnit->regPool->nextCoreReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700403}
404
buzbeeaad94382012-11-21 07:40:50 -0800405int AllocTempFloat(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700406{
buzbee52a77fc2012-11-20 19:50:46 -0800407 return AllocTempBody(cUnit, cUnit->regPool->FPRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 cUnit->regPool->numFPRegs,
409 &cUnit->regPool->nextFPReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700410}
411
buzbeeaad94382012-11-21 07:40:50 -0800412static RegisterInfo* AllocLiveBody(RegisterInfo* p, int numRegs, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700413{
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 int i;
415 if (sReg == -1)
buzbee67bf8852011-08-17 17:51:35 -0700416 return NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 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;
buzbee67bf8852011-08-17 17:51:35 -0700425}
426
buzbee52a77fc2012-11-20 19:50:46 -0800427RegisterInfo* AllocLive(CompilationUnit* cUnit, int sReg, int regClass)
buzbee67bf8852011-08-17 17:51:35 -0700428{
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 RegisterInfo* res = NULL;
430 switch (regClass) {
431 case kAnyReg:
buzbee52a77fc2012-11-20 19:50:46 -0800432 res = AllocLiveBody(cUnit->regPool->FPRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 cUnit->regPool->numFPRegs, sReg);
434 if (res)
435 break;
436 /* Intentional fallthrough */
437 case kCoreReg:
buzbee52a77fc2012-11-20 19:50:46 -0800438 res = AllocLiveBody(cUnit->regPool->coreRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 cUnit->regPool->numCoreRegs, sReg);
440 break;
441 case kFPReg:
buzbee52a77fc2012-11-20 19:50:46 -0800442 res = AllocLiveBody(cUnit->regPool->FPRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 cUnit->regPool->numFPRegs, sReg);
444 break;
445 default:
446 LOG(FATAL) << "Invalid register type";
447 }
448 return res;
buzbee67bf8852011-08-17 17:51:35 -0700449}
450
buzbeeaad94382012-11-21 07:40:50 -0800451void FreeTemp(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700452{
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 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;
buzbee67bf8852011-08-17 17:51:35 -0700463 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700464 }
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;
buzbee67bf8852011-08-17 17:51:35 -0700474 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 }
476 LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700477}
478
buzbeeaad94382012-11-21 07:40:50 -0800479RegisterInfo* IsLive(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700480{
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 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;
buzbee67bf8852011-08-17 17:51:35 -0700487 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 }
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;
buzbee67bf8852011-08-17 17:51:35 -0700494 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 }
496 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700497}
498
buzbeeaad94382012-11-21 07:40:50 -0800499RegisterInfo* IsTemp(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700500{
buzbee52a77fc2012-11-20 19:50:46 -0800501 RegisterInfo* p = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700502 return (p->isTemp) ? p : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700503}
504
buzbeeaad94382012-11-21 07:40:50 -0800505RegisterInfo* IsPromoted(CompilationUnit* cUnit, int reg)
buzbeeb29e4d12011-09-26 15:05:48 -0700506{
buzbee52a77fc2012-11-20 19:50:46 -0800507 RegisterInfo* p = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 return (p->isTemp) ? NULL : p;
buzbeeb29e4d12011-09-26 15:05:48 -0700509}
510
buzbeeaad94382012-11-21 07:40:50 -0800511bool IsDirty(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700512{
buzbee52a77fc2012-11-20 19:50:46 -0800513 RegisterInfo* p = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 return p->dirty;
buzbee67bf8852011-08-17 17:51:35 -0700515}
516
517/*
buzbee52a77fc2012-11-20 19:50:46 -0800518 * Similar to AllocTemp(), but forces the allocation of a specific
buzbee67bf8852011-08-17 17:51:35 -0700519 * register. No check is made to see if the register was previously
520 * allocated. Use with caution.
521 */
buzbeeaad94382012-11-21 07:40:50 -0800522void LockTemp(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700523{
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 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;
buzbee67bf8852011-08-17 17:51:35 -0700533 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 }
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;
buzbee67bf8852011-08-17 17:51:35 -0700543 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 }
545 LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700546}
547
buzbeeaad94382012-11-21 07:40:50 -0800548static void ResetDefBody(RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -0700549{
Bill Buzbeea114add2012-05-03 15:00:40 -0700550 p->defStart = NULL;
551 p->defEnd = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700552}
553
buzbeeaad94382012-11-21 07:40:50 -0800554void ResetDef(CompilationUnit* cUnit, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800555{
buzbee52a77fc2012-11-20 19:50:46 -0800556 ResetDefBody(GetRegInfo(cUnit, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800557}
558
buzbeeaad94382012-11-21 07:40:50 -0800559static void NullifyRange(CompilationUnit* cUnit, LIR *start, LIR *finish, int sReg1, int sReg2)
buzbee67bf8852011-08-17 17:51:35 -0700560{
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 if (start && finish) {
562 LIR *p;
563 DCHECK_EQ(sReg1, sReg2);
564 for (p = start; ;p = p->next) {
buzbee52a77fc2012-11-20 19:50:46 -0800565 NopLIR(p);
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 if (p == finish)
567 break;
buzbee67bf8852011-08-17 17:51:35 -0700568 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700569 }
buzbee67bf8852011-08-17 17:51:35 -0700570}
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 */
buzbeeaad94382012-11-21 07:40:50 -0800577void MarkDef(CompilationUnit* cUnit, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700579{
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 DCHECK(!rl.wide);
581 DCHECK(start && start->next);
582 DCHECK(finish);
buzbee52a77fc2012-11-20 19:50:46 -0800583 RegisterInfo* p = GetRegInfo(cUnit, rl.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 p->defStart = start->next;
585 p->defEnd = finish;
buzbee67bf8852011-08-17 17:51:35 -0700586}
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 */
buzbeeaad94382012-11-21 07:40:50 -0800593void MarkDefWide(CompilationUnit* cUnit, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700595{
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 DCHECK(rl.wide);
597 DCHECK(start && start->next);
598 DCHECK(finish);
buzbee52a77fc2012-11-20 19:50:46 -0800599 RegisterInfo* p = GetRegInfo(cUnit, rl.lowReg);
600 ResetDef(cUnit, rl.highReg); // Only track low of pair
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 p->defStart = start->next;
602 p->defEnd = finish;
buzbee67bf8852011-08-17 17:51:35 -0700603}
604
buzbeeaad94382012-11-21 07:40:50 -0800605RegLocation WideToNarrow(CompilationUnit* cUnit, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700606{
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 DCHECK(rl.wide);
608 if (rl.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800609 RegisterInfo* infoLo = GetRegInfo(cUnit, rl.lowReg);
610 RegisterInfo* infoHi = GetRegInfo(cUnit, rl.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 if (infoLo->isTemp) {
612 infoLo->pair = false;
613 infoLo->defStart = NULL;
614 infoLo->defEnd = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700615 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 if (infoHi->isTemp) {
617 infoHi->pair = false;
618 infoHi->defStart = NULL;
619 infoHi->defEnd = NULL;
620 }
621 }
622 rl.wide = false;
623 return rl;
buzbee67bf8852011-08-17 17:51:35 -0700624}
625
buzbeeaad94382012-11-21 07:40:50 -0800626void ResetDefLoc(CompilationUnit* cUnit, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700627{
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 DCHECK(!rl.wide);
buzbee52a77fc2012-11-20 19:50:46 -0800629 RegisterInfo* p = IsTemp(cUnit, rl.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 if (p && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
631 DCHECK(!p->pair);
buzbee52a77fc2012-11-20 19:50:46 -0800632 NullifyRange(cUnit, p->defStart, p->defEnd, p->sReg, rl.sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 }
buzbee52a77fc2012-11-20 19:50:46 -0800634 ResetDef(cUnit, rl.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700635}
636
buzbeeaad94382012-11-21 07:40:50 -0800637void ResetDefLocWide(CompilationUnit* cUnit, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700638{
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 DCHECK(rl.wide);
buzbee52a77fc2012-11-20 19:50:46 -0800640 RegisterInfo* pLow = IsTemp(cUnit, rl.lowReg);
641 RegisterInfo* pHigh = IsTemp(cUnit, rl.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 if (pLow && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
643 DCHECK(pLow->pair);
buzbee52a77fc2012-11-20 19:50:46 -0800644 NullifyRange(cUnit, pLow->defStart, pLow->defEnd, pLow->sReg, rl.sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 }
646 if (pHigh && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
647 DCHECK(pHigh->pair);
648 }
buzbee52a77fc2012-11-20 19:50:46 -0800649 ResetDef(cUnit, rl.lowReg);
650 ResetDef(cUnit, rl.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700651}
652
buzbeeaad94382012-11-21 07:40:50 -0800653void ResetDefTracking(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700654{
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 int i;
656 for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800657 ResetDefBody(&cUnit->regPool->coreRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 }
659 for (i=0; i< cUnit->regPool->numFPRegs; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800660 ResetDefBody(&cUnit->regPool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700661 }
buzbee67bf8852011-08-17 17:51:35 -0700662}
663
buzbeeaad94382012-11-21 07:40:50 -0800664void ClobberAllRegs(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700665{
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 int i;
667 for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800668 ClobberBody(cUnit, &cUnit->regPool->coreRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700669 }
670 for (i=0; i< cUnit->regPool->numFPRegs; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800671 ClobberBody(cUnit, &cUnit->regPool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 }
buzbee67bf8852011-08-17 17:51:35 -0700673}
674
buzbee67bf8852011-08-17 17:51:35 -0700675// Make sure nothing is live and dirty
buzbeeaad94382012-11-21 07:40:50 -0800676static void FlushAllRegsBody(CompilationUnit* cUnit, RegisterInfo* info, int numRegs)
buzbee67bf8852011-08-17 17:51:35 -0700677{
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 int i;
679 for (i=0; i < numRegs; i++) {
680 if (info[i].live && info[i].dirty) {
681 if (info[i].pair) {
buzbee52a77fc2012-11-20 19:50:46 -0800682 FlushRegWide(cUnit, info[i].reg, info[i].partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800684 FlushReg(cUnit, info[i].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 }
buzbee67bf8852011-08-17 17:51:35 -0700686 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700687 }
buzbee67bf8852011-08-17 17:51:35 -0700688}
689
buzbeeaad94382012-11-21 07:40:50 -0800690void FlushAllRegs(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700691{
buzbee52a77fc2012-11-20 19:50:46 -0800692 FlushAllRegsBody(cUnit, cUnit->regPool->coreRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700693 cUnit->regPool->numCoreRegs);
buzbee52a77fc2012-11-20 19:50:46 -0800694 FlushAllRegsBody(cUnit, cUnit->regPool->FPRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 cUnit->regPool->numFPRegs);
buzbee52a77fc2012-11-20 19:50:46 -0800696 ClobberAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700697}
698
699
700//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbeeaad94382012-11-21 07:40:50 -0800701static bool RegClassMatches(int regClass, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700702{
Bill Buzbeea114add2012-05-03 15:00:40 -0700703 if (regClass == kAnyReg) {
704 return true;
705 } else if (regClass == kCoreReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800706 return !IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800708 return IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 }
buzbee67bf8852011-08-17 17:51:35 -0700710}
711
buzbeeaad94382012-11-21 07:40:50 -0800712void MarkLive(CompilationUnit* cUnit, int reg, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700713{
buzbee52a77fc2012-11-20 19:50:46 -0800714 RegisterInfo* info = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700715 if ((info->reg == reg) && (info->sReg == sReg) && info->live) {
716 return; /* already live */
717 } else if (sReg != INVALID_SREG) {
buzbee52a77fc2012-11-20 19:50:46 -0800718 ClobberSReg(cUnit, sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700719 if (info->isTemp) {
720 info->live = true;
buzbee67bf8852011-08-17 17:51:35 -0700721 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700722 } else {
723 /* Can't be live if no associated sReg */
724 DCHECK(info->isTemp);
725 info->live = false;
726 }
727 info->sReg = sReg;
buzbee67bf8852011-08-17 17:51:35 -0700728}
729
buzbeeaad94382012-11-21 07:40:50 -0800730void MarkTemp(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700731{
buzbee52a77fc2012-11-20 19:50:46 -0800732 RegisterInfo* info = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 info->isTemp = true;
buzbee67bf8852011-08-17 17:51:35 -0700734}
735
buzbeeaad94382012-11-21 07:40:50 -0800736void UnmarkTemp(CompilationUnit* cUnit, int reg)
buzbee9e0f9b02011-08-24 15:32:46 -0700737{
buzbee52a77fc2012-11-20 19:50:46 -0800738 RegisterInfo* info = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700739 info->isTemp = false;
buzbee9e0f9b02011-08-24 15:32:46 -0700740}
741
buzbeeaad94382012-11-21 07:40:50 -0800742void MarkPair(CompilationUnit* cUnit, int lowReg, int highReg)
buzbee67bf8852011-08-17 17:51:35 -0700743{
buzbee52a77fc2012-11-20 19:50:46 -0800744 RegisterInfo* infoLo = GetRegInfo(cUnit, lowReg);
745 RegisterInfo* infoHi = GetRegInfo(cUnit, highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700746 infoLo->pair = infoHi->pair = true;
747 infoLo->partner = highReg;
748 infoHi->partner = lowReg;
buzbee67bf8852011-08-17 17:51:35 -0700749}
750
buzbeeaad94382012-11-21 07:40:50 -0800751void MarkClean(CompilationUnit* cUnit, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700752{
buzbee52a77fc2012-11-20 19:50:46 -0800753 RegisterInfo* info = GetRegInfo(cUnit, loc.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700754 info->dirty = false;
755 if (loc.wide) {
buzbee52a77fc2012-11-20 19:50:46 -0800756 info = GetRegInfo(cUnit, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700757 info->dirty = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700758 }
buzbee67bf8852011-08-17 17:51:35 -0700759}
760
buzbeeaad94382012-11-21 07:40:50 -0800761void MarkDirty(CompilationUnit* cUnit, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700762{
Bill Buzbeea114add2012-05-03 15:00:40 -0700763 if (loc.home) {
764 // If already home, can't be dirty
765 return;
766 }
buzbee52a77fc2012-11-20 19:50:46 -0800767 RegisterInfo* info = GetRegInfo(cUnit, loc.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 info->dirty = true;
769 if (loc.wide) {
buzbee52a77fc2012-11-20 19:50:46 -0800770 info = GetRegInfo(cUnit, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700771 info->dirty = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 }
buzbee67bf8852011-08-17 17:51:35 -0700773}
774
buzbeeaad94382012-11-21 07:40:50 -0800775void MarkInUse(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700776{
buzbee52a77fc2012-11-20 19:50:46 -0800777 RegisterInfo* info = GetRegInfo(cUnit, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 info->inUse = true;
buzbee67bf8852011-08-17 17:51:35 -0700779}
780
buzbeeaad94382012-11-21 07:40:50 -0800781static void CopyRegInfo(CompilationUnit* cUnit, int newReg, int oldReg)
buzbee67bf8852011-08-17 17:51:35 -0700782{
buzbee52a77fc2012-11-20 19:50:46 -0800783 RegisterInfo* newInfo = GetRegInfo(cUnit, newReg);
784 RegisterInfo* oldInfo = GetRegInfo(cUnit, oldReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 // 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;
buzbee67bf8852011-08-17 17:51:35 -0700791}
792
buzbeeaad94382012-11-21 07:40:50 -0800793static bool CheckCorePoolSanity(CompilationUnit* cUnit)
buzbee6181f792011-09-29 11:14:04 -0700794{
795 for (static int i = 0; i < cUnit->regPool->numCoreRegs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700796 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;
buzbee52a77fc2012-11-20 19:50:46 -0800800 static RegisterInfo* partner = GetRegInfo(cUnit, partnerReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 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));
buzbee6181f792011-09-29 11:14:04 -0700810 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700811 }
812 if (!cUnit->regPool->coreRegs[i].live) {
813 DCHECK(cUnit->regPool->coreRegs[i].defStart == NULL);
814 DCHECK(cUnit->regPool->coreRegs[i].defEnd == NULL);
815 }
buzbee6181f792011-09-29 11:14:04 -0700816 }
817 return true;
818}
819
buzbeeaad94382012-11-21 07:40:50 -0800820/*
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
830RegLocation 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
buzbee67bf8852011-08-17 17:51:35 -0700853/* see comments for updateLoc */
buzbeeaad94382012-11-21 07:40:50 -0800854RegLocation UpdateLocWide(CompilationUnit* cUnit, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700855{
Bill Buzbeea114add2012-05-03 15:00:40 -0700856 DCHECK(loc.wide);
buzbee52a77fc2012-11-20 19:50:46 -0800857 DCHECK(CheckCorePoolSanity(cUnit));
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 if (loc.location != kLocPhysReg) {
859 DCHECK((loc.location == kLocDalvikFrame) ||
860 (loc.location == kLocCompilerTemp));
861 // Are the dalvik regs already live in physical registers?
buzbee52a77fc2012-11-20 19:50:46 -0800862 RegisterInfo* infoLo = AllocLive(cUnit, loc.sRegLow, kAnyReg);
863 RegisterInfo* infoHi = AllocLive(cUnit,
Bill Buzbeea114add2012-05-03 15:00:40 -0700864 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?
buzbee52a77fc2012-11-20 19:50:46 -0800869 match = match && (IsFpReg(infoLo->reg) == IsFpReg(infoHi->reg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 // If a pair of floating point singles, are they properly aligned?
buzbee52a77fc2012-11-20 19:50:46 -0800871 if (match && IsFpReg(infoLo->reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700872 match &= ((infoLo->reg & 0x1) == 0);
873 match &= ((infoHi->reg - infoLo->reg) == 1);
buzbee67bf8852011-08-17 17:51:35 -0700874 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700875 // 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;
buzbee52a77fc2012-11-20 19:50:46 -0800886 MarkPair(cUnit, loc.lowReg, loc.highReg);
887 DCHECK(!IsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700888 return loc;
889 }
890 // Can't easily reuse - clobber and free any overlaps
891 if (infoLo) {
buzbee52a77fc2012-11-20 19:50:46 -0800892 Clobber(cUnit, infoLo->reg);
893 FreeTemp(cUnit, infoLo->reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 if (infoLo->pair)
buzbee52a77fc2012-11-20 19:50:46 -0800895 Clobber(cUnit, infoLo->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 }
897 if (infoHi) {
buzbee52a77fc2012-11-20 19:50:46 -0800898 Clobber(cUnit, infoHi->reg);
899 FreeTemp(cUnit, infoHi->reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 if (infoHi->pair)
buzbee52a77fc2012-11-20 19:50:46 -0800901 Clobber(cUnit, infoHi->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 }
903 }
904 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700905}
906
buzbeeed3e9302011-09-23 17:34:19 -0700907
908/* For use in cases we don't know (or care) width */
buzbeeaad94382012-11-21 07:40:50 -0800909RegLocation UpdateRawLoc(CompilationUnit* cUnit, RegLocation loc)
buzbeeed3e9302011-09-23 17:34:19 -0700910{
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 if (loc.wide)
buzbee52a77fc2012-11-20 19:50:46 -0800912 return UpdateLocWide(cUnit, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 else
buzbee52a77fc2012-11-20 19:50:46 -0800914 return UpdateLoc(cUnit, loc);
buzbeeed3e9302011-09-23 17:34:19 -0700915}
916
buzbeeaad94382012-11-21 07:40:50 -0800917RegLocation EvalLocWide(CompilationUnit* cUnit, RegLocation loc, int regClass, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700918{
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 DCHECK(loc.wide);
920 int newRegs;
921 int lowReg;
922 int highReg;
buzbee67bf8852011-08-17 17:51:35 -0700923
buzbee52a77fc2012-11-20 19:50:46 -0800924 loc = UpdateLocWide(cUnit, loc);
buzbee67bf8852011-08-17 17:51:35 -0700925
Bill Buzbeea114add2012-05-03 15:00:40 -0700926 /* If already in registers, we can assume proper form. Right reg class? */
927 if (loc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800928 DCHECK_EQ(IsFpReg(loc.lowReg), IsFpReg(loc.highReg));
929 DCHECK(!IsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
930 if (!RegClassMatches(regClass, loc.lowReg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 /* Wrong register class. Reallocate and copy */
buzbee52a77fc2012-11-20 19:50:46 -0800932 newRegs = AllocTypedTempPair(cUnit, loc.fp, regClass);
Bill Buzbeea114add2012-05-03 15:00:40 -0700933 lowReg = newRegs & 0xff;
934 highReg = (newRegs >> 8) & 0xff;
buzbee52a77fc2012-11-20 19:50:46 -0800935 OpRegCopyWide(cUnit, lowReg, highReg, loc.lowReg,
buzbeeeaf09bc2012-11-15 14:51:41 -0800936 loc.highReg);
buzbee52a77fc2012-11-20 19:50:46 -0800937 CopyRegInfo(cUnit, lowReg, loc.lowReg);
938 CopyRegInfo(cUnit, highReg, loc.highReg);
939 Clobber(cUnit, loc.lowReg);
940 Clobber(cUnit, loc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 loc.lowReg = lowReg;
942 loc.highReg = highReg;
buzbee52a77fc2012-11-20 19:50:46 -0800943 MarkPair(cUnit, loc.lowReg, loc.highReg);
944 DCHECK(!IsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 }
buzbee67bf8852011-08-17 17:51:35 -0700946 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 }
948
949 DCHECK_NE(loc.sRegLow, INVALID_SREG);
950 DCHECK_NE(oatSRegHi(loc.sRegLow), INVALID_SREG);
951
buzbee52a77fc2012-11-20 19:50:46 -0800952 newRegs = AllocTypedTempPair(cUnit, loc.fp, regClass);
Bill Buzbeea114add2012-05-03 15:00:40 -0700953 loc.lowReg = newRegs & 0xff;
954 loc.highReg = (newRegs >> 8) & 0xff;
955
buzbee52a77fc2012-11-20 19:50:46 -0800956 MarkPair(cUnit, loc.lowReg, loc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 if (update) {
958 loc.location = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800959 MarkLive(cUnit, loc.lowReg, loc.sRegLow);
960 MarkLive(cUnit, loc.highReg, oatSRegHi(loc.sRegLow));
Bill Buzbeea114add2012-05-03 15:00:40 -0700961 }
buzbee52a77fc2012-11-20 19:50:46 -0800962 DCHECK(!IsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700963 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700964}
965
buzbeeaad94382012-11-21 07:40:50 -0800966RegLocation EvalLoc(CompilationUnit* cUnit, RegLocation loc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700967 int regClass, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700968{
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 int newReg;
buzbee67bf8852011-08-17 17:51:35 -0700970
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 if (loc.wide)
buzbee52a77fc2012-11-20 19:50:46 -0800972 return EvalLocWide(cUnit, loc, regClass, update);
buzbee67bf8852011-08-17 17:51:35 -0700973
buzbee52a77fc2012-11-20 19:50:46 -0800974 loc = UpdateLoc(cUnit, loc);
buzbee67bf8852011-08-17 17:51:35 -0700975
Bill Buzbeea114add2012-05-03 15:00:40 -0700976 if (loc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800977 if (!RegClassMatches(regClass, loc.lowReg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700978 /* Wrong register class. Realloc, copy and transfer ownership */
buzbee52a77fc2012-11-20 19:50:46 -0800979 newReg = AllocTypedTemp(cUnit, loc.fp, regClass);
980 OpRegCopy(cUnit, newReg, loc.lowReg);
981 CopyRegInfo(cUnit, newReg, loc.lowReg);
982 Clobber(cUnit, loc.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700983 loc.lowReg = newReg;
buzbee67bf8852011-08-17 17:51:35 -0700984 }
985 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 }
987
988 DCHECK_NE(loc.sRegLow, INVALID_SREG);
989
buzbee52a77fc2012-11-20 19:50:46 -0800990 newReg = AllocTypedTemp(cUnit, loc.fp, regClass);
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 loc.lowReg = newReg;
992
993 if (update) {
994 loc.location = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800995 MarkLive(cUnit, loc.lowReg, loc.sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 }
997 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700998}
999
buzbeeaad94382012-11-21 07:40:50 -08001000RegLocation GetRawSrc(CompilationUnit* cUnit, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001001{
buzbee15bf9802012-06-12 17:49:27 -07001002 DCHECK(num < mir->ssaRep->numUses);
1003 RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]];
buzbee15bf9802012-06-12 17:49:27 -07001004 return res;
1005}
buzbeeeaf09bc2012-11-15 14:51:41 -08001006
buzbeeaad94382012-11-21 07:40:50 -08001007RegLocation GetRawDest(CompilationUnit* cUnit, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001008{
Elliott Hughes74847412012-06-20 18:10:21 -07001009 DCHECK_GT(mir->ssaRep->numDefs, 0);
buzbee15bf9802012-06-12 17:49:27 -07001010 RegLocation res = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee15bf9802012-06-12 17:49:27 -07001011 return res;
1012}
buzbeeeaf09bc2012-11-15 14:51:41 -08001013
buzbeeaad94382012-11-21 07:40:50 -08001014RegLocation GetDest(CompilationUnit* cUnit, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001015{
buzbee52a77fc2012-11-20 19:50:46 -08001016 RegLocation res = GetRawDest(cUnit, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001017 DCHECK(!res.wide);
1018 return res;
buzbee67bf8852011-08-17 17:51:35 -07001019}
buzbeeeaf09bc2012-11-15 14:51:41 -08001020
buzbeeaad94382012-11-21 07:40:50 -08001021RegLocation GetSrc(CompilationUnit* cUnit, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001022{
buzbee52a77fc2012-11-20 19:50:46 -08001023 RegLocation res = GetRawSrc(cUnit, mir, num);
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 DCHECK(!res.wide);
1025 return res;
buzbeee9a72f62011-09-04 17:59:07 -07001026}
buzbeeeaf09bc2012-11-15 14:51:41 -08001027
buzbeeaad94382012-11-21 07:40:50 -08001028RegLocation GetDestWide(CompilationUnit* cUnit, MIR* mir)
buzbeee9a72f62011-09-04 17:59:07 -07001029{
buzbee52a77fc2012-11-20 19:50:46 -08001030 RegLocation res = GetRawDest(cUnit, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001031 DCHECK(res.wide);
1032 return res;
buzbee67bf8852011-08-17 17:51:35 -07001033}
1034
buzbeeaad94382012-11-21 07:40:50 -08001035RegLocation GetSrcWide(CompilationUnit* cUnit, MIR* mir,
buzbee15bf9802012-06-12 17:49:27 -07001036 int low)
buzbee67bf8852011-08-17 17:51:35 -07001037{
buzbee52a77fc2012-11-20 19:50:46 -08001038 RegLocation res = GetRawSrc(cUnit, mir, low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 DCHECK(res.wide);
1040 return res;
buzbee67bf8852011-08-17 17:51:35 -07001041}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001042
buzbeee3acd072012-02-25 17:03:10 -08001043/* USE SSA names to count references of base Dalvik vRegs. */
buzbeeaad94382012-11-21 07:40:50 -08001044static void CountRefs(CompilationUnit *cUnit, BasicBlock* bb, RefCounts* coreCounts,
1045 RefCounts* fpCounts)
buzbeee3acd072012-02-25 17:03:10 -08001046{
Bill Buzbeea114add2012-05-03 15:00:40 -07001047 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];
buzbee239c4e72012-03-16 08:42:29 -07001058 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 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];
buzbee239c4e72012-03-16 08:42:29 -07001064 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001065 }
1066 i += 2;
1067 } else {
1068 i++;
buzbeee3acd072012-02-25 17:03:10 -08001069 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001070 }
buzbeee3acd072012-02-25 17:03:10 -08001071}
1072
1073/* qsort callback function, sort descending */
buzbeeaad94382012-11-21 07:40:50 -08001074static int SortCounts(const void *val1, const void *val2)
buzbeee3acd072012-02-25 17:03:10 -08001075{
buzbeecbd6d442012-11-17 14:11:25 -08001076 const RefCounts* op1 = reinterpret_cast<const RefCounts*>(val1);
1077 const RefCounts* op2 = reinterpret_cast<const RefCounts*>(val2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001078 return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
buzbeee3acd072012-02-25 17:03:10 -08001079}
1080
buzbeeaad94382012-11-21 07:40:50 -08001081static void DumpCounts(const RefCounts* arr, int size, const char* msg)
buzbeee3acd072012-02-25 17:03:10 -08001082{
Bill Buzbeea114add2012-05-03 15:00:40 -07001083 LOG(INFO) << msg;
1084 for (int i = 0; i < size; i++) {
1085 LOG(INFO) << "sReg[" << arr[i].sReg << "]: " << arr[i].count;
1086 }
buzbeee3acd072012-02-25 17:03:10 -08001087}
1088
1089/*
1090 * Note: some portions of this code required even if the kPromoteRegs
1091 * optimization is disabled.
1092 */
buzbeeaad94382012-11-21 07:40:50 -08001093void DoPromotion(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -08001094{
Bill Buzbeea114add2012-05-03 15:00:40 -07001095 int regBias = cUnit->numCompilerTemps + 1;
1096 int dalvikRegs = cUnit->numDalvikRegisters;
1097 int numRegs = dalvikRegs + regBias;
1098 const int promotionThreshold = 2;
buzbeee3acd072012-02-25 17:03:10 -08001099
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 // Allow target code to add any special registers
buzbee52a77fc2012-11-20 19:50:46 -08001101 AdjustSpillMask(cUnit);
buzbeee3acd072012-02-25 17:03:10 -08001102
Bill Buzbeea114add2012-05-03 15:00:40 -07001103 /*
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 */
buzbee52a77fc2012-11-20 19:50:46 -08001114 RefCounts *coreRegs = static_cast<RefCounts*>(NewMem(cUnit, sizeof(RefCounts) * numRegs,
buzbeecbd6d442012-11-17 14:11:25 -08001115 true, kAllocRegAlloc));
buzbee52a77fc2012-11-20 19:50:46 -08001116 RefCounts *FpRegs = static_cast<RefCounts *>(NewMem(cUnit, sizeof(RefCounts) * numRegs,
buzbeecbd6d442012-11-17 14:11:25 -08001117 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -07001118 // Set ssa names for original Dalvik registers
1119 for (int i = 0; i < dalvikRegs; i++) {
buzbee52a77fc2012-11-20 19:50:46 -08001120 coreRegs[i].sReg = FpRegs[i].sReg = i;
Bill Buzbeea114add2012-05-03 15:00:40 -07001121 }
1122 // Set ssa name for Method*
1123 coreRegs[dalvikRegs].sReg = cUnit->methodSReg;
buzbee52a77fc2012-11-20 19:50:46 -08001124 FpRegs[dalvikRegs].sReg = cUnit->methodSReg; // For consistecy
Bill Buzbeea114add2012-05-03 15:00:40 -07001125 // Set ssa names for compilerTemps
1126 for (int i = 1; i <= cUnit->numCompilerTemps; i++) {
buzbeecbd6d442012-11-17 14:11:25 -08001127 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cUnit->compilerTemps.elemList[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001128 coreRegs[dalvikRegs + i].sReg = ct->sReg;
buzbee52a77fc2012-11-20 19:50:46 -08001129 FpRegs[dalvikRegs + i].sReg = ct->sReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001130 }
1131
1132 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -08001133 GrowableListIteratorInit(&cUnit->blockList, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001134 while (true) {
1135 BasicBlock* bb;
buzbee52a77fc2012-11-20 19:50:46 -08001136 bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -07001137 if (bb == NULL) break;
buzbee52a77fc2012-11-20 19:50:46 -08001138 CountRefs(cUnit, bb, coreRegs, FpRegs);
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 }
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++) {
buzbee52a77fc2012-11-20 19:50:46 -08001147 if (FpRegs[i].doubleStart) {
1148 FpRegs[i].count *= 2;
buzbeee3acd072012-02-25 17:03:10 -08001149 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001150 }
1151
1152 // Sort the count arrays
buzbee52a77fc2012-11-20 19:50:46 -08001153 qsort(coreRegs, numRegs, sizeof(RefCounts), SortCounts);
1154 qsort(FpRegs, numRegs, sizeof(RefCounts), SortCounts);
Bill Buzbeea114add2012-05-03 15:00:40 -07001155
1156 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -08001157 DumpCounts(coreRegs, numRegs, "Core regs after sort");
1158 DumpCounts(FpRegs, numRegs, "Fp regs after sort");
Bill Buzbeea114add2012-05-03 15:00:40 -07001159 }
1160
1161 if (!(cUnit->disableOpt & (1 << kPromoteRegs))) {
buzbee52a77fc2012-11-20 19:50:46 -08001162 // Promote FpRegs
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 for (int i = 0; (i < numRegs) &&
buzbee52a77fc2012-11-20 19:50:46 -08001164 (FpRegs[i].count >= promotionThreshold ); i++) {
1165 int pMapIdx = SRegToPMap(cUnit, FpRegs[i].sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 if (cUnit->promotionMap[pMapIdx].fpLocation != kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -08001167 int reg = AllocPreservedFPReg(cUnit, FpRegs[i].sReg,
1168 FpRegs[i].doubleStart);
Bill Buzbeea114add2012-05-03 15:00:40 -07001169 if (reg < 0) {
1170 break; // No more left
1171 }
1172 }
buzbee239c4e72012-03-16 08:42:29 -07001173 }
buzbee9c044ce2012-03-18 13:24:07 -07001174
Bill Buzbeea114add2012-05-03 15:00:40 -07001175 // 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) {
buzbee52a77fc2012-11-20 19:50:46 -08001181 int reg = AllocPreservedCoreReg(cUnit, coreRegs[i].sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001182 if (reg < 0) {
1183 break; // No more left
1184 }
1185 }
buzbeee3acd072012-02-25 17:03:10 -08001186 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001187 } else if (cUnit->qdMode) {
buzbee52a77fc2012-11-20 19:50:46 -08001188 AllocPreservedCoreReg(cUnit, cUnit->methodSReg);
buzbeee3acd072012-02-25 17:03:10 -08001189 for (int i = 0; i < numRegs; i++) {
buzbee52a77fc2012-11-20 19:50:46 -08001190 int reg = AllocPreservedCoreReg(cUnit, i);
Bill Buzbeea114add2012-05-03 15:00:40 -07001191 if (reg < 0) {
1192 break; // No more left
1193 }
buzbeee3acd072012-02-25 17:03:10 -08001194 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001195 }
buzbeee3acd072012-02-25 17:03:10 -08001196
buzbeee3acd072012-02-25 17:03:10 -08001197
Bill Buzbeea114add2012-05-03 15:00:40 -07001198 // 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;
buzbee52a77fc2012-11-20 19:50:46 -08001206 curr->lowReg = cUnit->promotionMap[pMapIdx].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001207 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)) {
buzbee52a77fc2012-11-20 19:50:46 -08001225 int lowReg = cUnit->promotionMap[pMapIdx].FpReg;
1226 int highReg = cUnit->promotionMap[pMapIdx+1].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001227 // 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 }
buzbeee3acd072012-02-25 17:03:10 -08001245 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001246 }
buzbeeca7a5e42012-08-20 11:12:18 -07001247 if (cUnit->printMe) {
buzbee52a77fc2012-11-20 19:50:46 -08001248 DumpPromotionMap(cUnit);
buzbeeca7a5e42012-08-20 11:12:18 -07001249 }
buzbeee3acd072012-02-25 17:03:10 -08001250}
1251
1252/* Returns sp-relative offset in bytes for a VReg */
buzbeeaad94382012-11-21 07:40:50 -08001253int VRegOffset(CompilationUnit* cUnit, int vReg)
buzbeee3acd072012-02-25 17:03:10 -08001254{
Ian Rogers0399dde2012-06-06 17:09:28 -07001255 return StackVisitor::GetVRegOffset(cUnit->code_item, cUnit->coreSpillMask,
1256 cUnit->fpSpillMask, cUnit->frameSize, vReg);
buzbeee3acd072012-02-25 17:03:10 -08001257}
1258
1259/* Returns sp-relative offset in bytes for a SReg */
buzbeeaad94382012-11-21 07:40:50 -08001260int SRegOffset(CompilationUnit* cUnit, int sReg)
buzbeee3acd072012-02-25 17:03:10 -08001261{
buzbee52a77fc2012-11-20 19:50:46 -08001262 return VRegOffset(cUnit, SRegToVReg(cUnit, sReg));
buzbeee3acd072012-02-25 17:03:10 -08001263}
1264
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001265} // namespace art