blob: 4848a59fc6d346ecb4af143c306d69f8465a9fda [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
19#include "../CompilerUtility.h"
20#include "../CompilerIR.h"
21#include "../Dataflow.h"
22#include "Ralloc.h"
23
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbee67bf8852011-08-17 17:51:35 -070026/*
27 * Free all allocated temps in the temp pools. Note that this does
28 * not affect the "liveness" of a temp register, which will stay
29 * live until it is either explicitly killed or reallocated.
30 */
31extern void oatResetRegPool(CompilationUnit* cUnit)
32{
Bill Buzbeea114add2012-05-03 15:00:40 -070033 int i;
34 for (i=0; i < cUnit->regPool->numCoreRegs; i++) {
35 if (cUnit->regPool->coreRegs[i].isTemp)
36 cUnit->regPool->coreRegs[i].inUse = false;
37 }
38 for (i=0; i < cUnit->regPool->numFPRegs; i++) {
39 if (cUnit->regPool->FPRegs[i].isTemp)
40 cUnit->regPool->FPRegs[i].inUse = false;
41 }
buzbee67bf8852011-08-17 17:51:35 -070042}
43
buzbeee3acd072012-02-25 17:03:10 -080044 /*
45 * Set up temp & preserved register pools specialized by target.
46 * Note: numRegs may be zero.
47 */
buzbee67bf8852011-08-17 17:51:35 -070048extern void oatInitPool(RegisterInfo* regs, int* regNums, int num)
49{
Bill Buzbeea114add2012-05-03 15:00:40 -070050 int i;
51 for (i=0; i < num; i++) {
52 regs[i].reg = regNums[i];
53 regs[i].inUse = false;
54 regs[i].isTemp = false;
55 regs[i].pair = false;
56 regs[i].live = false;
57 regs[i].dirty = false;
58 regs[i].sReg = INVALID_SREG;
59 }
buzbee67bf8852011-08-17 17:51:35 -070060}
61
buzbee31a4a6f2012-02-28 15:36:15 -080062void dumpRegPool(RegisterInfo* p, int numRegs)
buzbee67bf8852011-08-17 17:51:35 -070063{
Bill Buzbeea114add2012-05-03 15:00:40 -070064 LOG(INFO) << "================================================";
65 for (int i = 0; i < numRegs; i++) {
66 LOG(INFO) << StringPrintf(
67 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x",
68 p[i].reg, p[i].isTemp, p[i].inUse, p[i].pair, p[i].partner,
69 p[i].live, p[i].dirty, p[i].sReg,(int)p[i].defStart,
70 (int)p[i].defEnd);
71 }
72 LOG(INFO) << "================================================";
buzbee67bf8852011-08-17 17:51:35 -070073}
74
buzbee6181f792011-09-29 11:14:04 -070075void oatDumpCoreRegPool(CompilationUnit* cUnit)
76{
Bill Buzbeea114add2012-05-03 15:00:40 -070077 dumpRegPool(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs);
buzbee6181f792011-09-29 11:14:04 -070078}
79
80void oatDumpFpRegPool(CompilationUnit* cUnit)
81{
Bill Buzbeea114add2012-05-03 15:00:40 -070082 dumpRegPool(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs);
buzbee6181f792011-09-29 11:14:04 -070083}
84
buzbee67bf8852011-08-17 17:51:35 -070085/* Mark a temp register as dead. Does not affect allocation state. */
buzbee5abfa3e2012-01-31 17:01:43 -080086static inline void clobberBody(CompilationUnit *cUnit, RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -070087{
Bill Buzbeea114add2012-05-03 15:00:40 -070088 if (p->isTemp) {
89 DCHECK(!(p->live && p->dirty)) << "Live & dirty temp in clobber";
90 p->live = false;
91 p->sReg = INVALID_SREG;
92 p->defStart = NULL;
93 p->defEnd = NULL;
94 if (p->pair) {
95 p->pair = false;
96 oatClobber(cUnit, p->partner);
buzbee67bf8852011-08-17 17:51:35 -070097 }
Bill Buzbeea114add2012-05-03 15:00:40 -070098 }
buzbee67bf8852011-08-17 17:51:35 -070099}
100
buzbee5abfa3e2012-01-31 17:01:43 -0800101/* Mark a temp register as dead. Does not affect allocation state. */
102void oatClobber(CompilationUnit* cUnit, int reg)
103{
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 clobberBody(cUnit, oatGetRegInfo(cUnit, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800105}
106
buzbee31a4a6f2012-02-28 15:36:15 -0800107void clobberSRegBody(RegisterInfo* p, int numRegs, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700108{
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 int i;
110 for (i=0; i< numRegs; i++) {
111 if (p[i].sReg == sReg) {
112 if (p[i].isTemp) {
113 p[i].live = false;
114 }
115 p[i].defStart = NULL;
116 p[i].defEnd = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700117 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 }
buzbee67bf8852011-08-17 17:51:35 -0700119}
120
121/* Clobber any temp associated with an sReg. Could be in either class */
122extern void oatClobberSReg(CompilationUnit* cUnit, int sReg)
123{
buzbee3d661942012-03-14 17:37:27 -0700124#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 /* Reset live temp tracking sanity checker */
126 if (sReg == cUnit->liveSReg) {
127 cUnit->liveSReg = INVALID_SREG;
128 }
buzbee3d661942012-03-14 17:37:27 -0700129#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 clobberSRegBody(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs, sReg);
131 clobberSRegBody(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs, sReg);
buzbee67bf8852011-08-17 17:51:35 -0700132}
133
buzbee9c044ce2012-03-18 13:24:07 -0700134/*
135 * SSA names associated with the initial definitions of Dalvik
136 * registers are the same as the Dalvik register number (and
137 * thus take the same position in the promotionMap. However,
138 * the special Method* and compiler temp resisters use negative
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700139 * vReg numbers to distinguish them and can have an arbitrary
buzbee9c044ce2012-03-18 13:24:07 -0700140 * ssa name (above the last original Dalvik register). This function
141 * maps SSA names to positions in the promotionMap array.
142 */
143int SRegToPMap(CompilationUnit* cUnit, int sReg)
buzbeee1965672012-03-11 18:39:19 -0700144{
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 DCHECK_LT(sReg, cUnit->numSSARegs);
146 DCHECK_GE(sReg, 0);
147 int vReg = SRegToVReg(cUnit, sReg);
148 if (vReg >= 0) {
149 DCHECK_LT(vReg, cUnit->numDalvikRegisters);
150 return vReg;
151 } else {
152 int pos = std::abs(vReg) - std::abs(SSA_METHOD_BASEREG);
153 DCHECK_LE(pos, cUnit->numCompilerTemps);
154 return cUnit->numDalvikRegisters + pos;
155 }
buzbeee1965672012-03-11 18:39:19 -0700156}
157
buzbeeca7a5e42012-08-20 11:12:18 -0700158void oatRecordCorePromotion(CompilationUnit* cUnit, int reg, int sReg)
159{
160 int pMapIdx = SRegToPMap(cUnit, sReg);
161 int vReg = SRegToVReg(cUnit, sReg);
162 oatGetRegInfo(cUnit, reg)->inUse = true;
163 cUnit->coreSpillMask |= (1 << reg);
164 // Include reg for later sort
165 cUnit->coreVmapTable.push_back(reg << VREG_NUM_WIDTH |
166 (vReg & ((1 << VREG_NUM_WIDTH) - 1)));
167 cUnit->numCoreSpills++;
168 cUnit->promotionMap[pMapIdx].coreLocation = kLocPhysReg;
169 cUnit->promotionMap[pMapIdx].coreReg = reg;
170}
171
buzbee67bf8852011-08-17 17:51:35 -0700172/* Reserve a callee-save register. Return -1 if none available */
173extern int oatAllocPreservedCoreReg(CompilationUnit* cUnit, int sReg)
174{
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 int res = -1;
176 RegisterInfo* coreRegs = cUnit->regPool->coreRegs;
177 for (int i = 0; i < cUnit->regPool->numCoreRegs; i++) {
178 if (!coreRegs[i].isTemp && !coreRegs[i].inUse) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 res = coreRegs[i].reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700180 oatRecordCorePromotion(cUnit, res, sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 break;
buzbee67bf8852011-08-17 17:51:35 -0700182 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 }
184 return res;
buzbee67bf8852011-08-17 17:51:35 -0700185}
186
buzbeeca7a5e42012-08-20 11:12:18 -0700187void oatRecordFpPromotion(CompilationUnit* cUnit, int reg, int sReg)
188{
189 int pMapIdx = SRegToPMap(cUnit, sReg);
190 int vReg = SRegToVReg(cUnit, sReg);
191 oatGetRegInfo(cUnit, reg)->inUse = true;
192 oatMarkPreservedSingle(cUnit, vReg, reg);
193 cUnit->promotionMap[pMapIdx].fpLocation = kLocPhysReg;
194 cUnit->promotionMap[pMapIdx].fpReg = reg;
195}
196
buzbee67bf8852011-08-17 17:51:35 -0700197/*
198 * Reserve a callee-save fp single register. Try to fullfill request for
199 * even/odd allocation, but go ahead and allocate anything if not
200 * available. If nothing's available, return -1.
201 */
buzbee31a4a6f2012-02-28 15:36:15 -0800202int allocPreservedSingle(CompilationUnit* cUnit, int sReg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700203{
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 int res = -1;
205 RegisterInfo* FPRegs = cUnit->regPool->FPRegs;
206 for (int i = 0; i < cUnit->regPool->numFPRegs; i++) {
207 if (!FPRegs[i].isTemp && !FPRegs[i].inUse &&
208 ((FPRegs[i].reg & 0x1) == 0) == even) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 res = FPRegs[i].reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700210 oatRecordFpPromotion(cUnit, res, sReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 break;
buzbee67bf8852011-08-17 17:51:35 -0700212 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 }
214 return res;
buzbee67bf8852011-08-17 17:51:35 -0700215}
216
217/*
218 * Somewhat messy code here. We want to allocate a pair of contiguous
219 * physical single-precision floating point registers starting with
220 * an even numbered reg. It is possible that the paired sReg (sReg+1)
221 * has already been allocated - try to fit if possible. Fail to
222 * allocate if we can't meet the requirements for the pair of
223 * sReg<=sX[even] & (sReg+1)<= sX+1.
224 */
buzbee31a4a6f2012-02-28 15:36:15 -0800225int allocPreservedDouble(CompilationUnit* cUnit, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700226{
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 int res = -1; // Assume failure
228 int vReg = SRegToVReg(cUnit, sReg);
229 int pMapIdx = SRegToPMap(cUnit, sReg);
230 if (cUnit->promotionMap[pMapIdx+1].fpLocation == kLocPhysReg) {
231 // Upper reg is already allocated. Can we fit?
232 int highReg = cUnit->promotionMap[pMapIdx+1].fpReg;
233 if ((highReg & 1) == 0) {
234 // High reg is even - fail.
235 return res;
236 }
237 // Is the low reg of the pair free?
238 RegisterInfo* p = oatGetRegInfo(cUnit, highReg-1);
239 if (p->inUse || p->isTemp) {
240 // Already allocated or not preserved - fail.
241 return res;
242 }
243 // OK - good to go.
244 res = p->reg;
245 p->inUse = true;
246 DCHECK_EQ((res & 1), 0);
247 oatMarkPreservedSingle(cUnit, vReg, res);
248 } else {
249 RegisterInfo* FPRegs = cUnit->regPool->FPRegs;
250 for (int i = 0; i < cUnit->regPool->numFPRegs; i++) {
251 if (!FPRegs[i].isTemp && !FPRegs[i].inUse &&
252 ((FPRegs[i].reg & 0x1) == 0x0) &&
253 !FPRegs[i+1].isTemp && !FPRegs[i+1].inUse &&
254 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
255 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
256 res = FPRegs[i].reg;
257 FPRegs[i].inUse = true;
buzbee9c044ce2012-03-18 13:24:07 -0700258 oatMarkPreservedSingle(cUnit, vReg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 FPRegs[i+1].inUse = true;
260 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
261 oatMarkPreservedSingle(cUnit, vReg+1, res+1);
262 break;
263 }
buzbee67bf8852011-08-17 17:51:35 -0700264 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 }
266 if (res != -1) {
267 cUnit->promotionMap[pMapIdx].fpLocation = kLocPhysReg;
268 cUnit->promotionMap[pMapIdx].fpReg = res;
269 cUnit->promotionMap[pMapIdx+1].fpLocation = kLocPhysReg;
270 cUnit->promotionMap[pMapIdx+1].fpReg = res + 1;
271 }
272 return res;
buzbee67bf8852011-08-17 17:51:35 -0700273}
274
275
276/*
277 * Reserve a callee-save fp register. If this register can be used
278 * as the first of a double, attempt to allocate an even pair of fp
279 * single regs (but if can't still attempt to allocate a single, preferring
280 * first to allocate an odd register.
281 */
282extern int oatAllocPreservedFPReg(CompilationUnit* cUnit, int sReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 bool doubleStart)
buzbee67bf8852011-08-17 17:51:35 -0700284{
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 int res = -1;
286 if (doubleStart) {
287 res = allocPreservedDouble(cUnit, sReg);
288 }
289 if (res == -1) {
290 res = allocPreservedSingle(cUnit, sReg, false /* try odd # */);
291 }
292 if (res == -1)
293 res = allocPreservedSingle(cUnit, sReg, true /* try even # */);
294 return res;
buzbee67bf8852011-08-17 17:51:35 -0700295}
296
buzbee31a4a6f2012-02-28 15:36:15 -0800297int allocTempBody(CompilationUnit* cUnit, RegisterInfo* p, int numRegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 int* nextTemp, 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) {
306 oatClobber(cUnit, p[next].reg);
307 p[next].inUse = true;
308 p[next].pair = false;
309 *nextTemp = next + 1;
310 return p[next].reg;
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) {
319 oatClobber(cUnit, p[next].reg);
320 p[next].inUse = true;
321 p[next].pair = false;
322 *nextTemp = next + 1;
323 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700324 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 next++;
326 }
327 if (required) {
328 oatCodegenDump(cUnit);
329 dumpRegPool(cUnit->regPool->coreRegs,
330 cUnit->regPool->numCoreRegs);
331 LOG(FATAL) << "No free temp registers";
332 }
333 return -1; // No register available
buzbee67bf8852011-08-17 17:51:35 -0700334}
335
336//REDO: too many assumptions.
337extern int oatAllocTempDouble(CompilationUnit* cUnit)
338{
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)) {
350 oatClobber(cUnit, p[next].reg);
351 oatClobber(cUnit, p[next+1].reg);
352 p[next].inUse = true;
353 p[next+1].inUse = true;
354 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
355 DCHECK_EQ((p[next].reg & 0x1), 0);
356 cUnit->regPool->nextFPReg = next + 2;
357 if (cUnit->regPool->nextFPReg >= numRegs) {
358 cUnit->regPool->nextFPReg = 0;
359 }
360 return p[next].reg;
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) {
372 oatClobber(cUnit, p[next].reg);
373 oatClobber(cUnit, p[next+1].reg);
374 p[next].inUse = true;
375 p[next+1].inUse = true;
376 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
377 DCHECK_EQ((p[next].reg & 0x1), 0);
378 cUnit->regPool->nextFPReg = next + 2;
379 if (cUnit->regPool->nextFPReg >= numRegs) {
380 cUnit->regPool->nextFPReg = 0;
381 }
382 return p[next].reg;
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 */
391extern int oatAllocFreeTemp(CompilationUnit* cUnit)
392{
Bill Buzbeea114add2012-05-03 15:00:40 -0700393 return allocTempBody(cUnit, cUnit->regPool->coreRegs,
394 cUnit->regPool->numCoreRegs,
395 &cUnit->regPool->nextCoreReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700396}
397
398extern int oatAllocTemp(CompilationUnit* cUnit)
399{
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 return allocTempBody(cUnit, cUnit->regPool->coreRegs,
401 cUnit->regPool->numCoreRegs,
402 &cUnit->regPool->nextCoreReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700403}
404
405extern int oatAllocTempFloat(CompilationUnit* cUnit)
406{
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 return allocTempBody(cUnit, cUnit->regPool->FPRegs,
408 cUnit->regPool->numFPRegs,
409 &cUnit->regPool->nextFPReg, true);
buzbee67bf8852011-08-17 17:51:35 -0700410}
411
buzbee31a4a6f2012-02-28 15:36:15 -0800412RegisterInfo* 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
buzbee31a4a6f2012-02-28 15:36:15 -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:
432 res = allocLiveBody(cUnit->regPool->FPRegs,
433 cUnit->regPool->numFPRegs, sReg);
434 if (res)
435 break;
436 /* Intentional fallthrough */
437 case kCoreReg:
438 res = allocLiveBody(cUnit->regPool->coreRegs,
439 cUnit->regPool->numCoreRegs, sReg);
440 break;
441 case kFPReg:
442 res = allocLiveBody(cUnit->regPool->FPRegs,
443 cUnit->regPool->numFPRegs, sReg);
444 break;
445 default:
446 LOG(FATAL) << "Invalid register type";
447 }
448 return res;
buzbee67bf8852011-08-17 17:51:35 -0700449}
450
451extern void oatFreeTemp(CompilationUnit* cUnit, int reg)
452{
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
479extern RegisterInfo* oatIsLive(CompilationUnit* cUnit, int reg)
480{
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
499extern RegisterInfo* oatIsTemp(CompilationUnit* cUnit, int reg)
500{
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 RegisterInfo* p = oatGetRegInfo(cUnit, reg);
502 return (p->isTemp) ? p : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700503}
504
buzbeeb29e4d12011-09-26 15:05:48 -0700505extern RegisterInfo* oatIsPromoted(CompilationUnit* cUnit, int reg)
506{
Bill Buzbeea114add2012-05-03 15:00:40 -0700507 RegisterInfo* p = oatGetRegInfo(cUnit, reg);
508 return (p->isTemp) ? NULL : p;
buzbeeb29e4d12011-09-26 15:05:48 -0700509}
510
buzbee67bf8852011-08-17 17:51:35 -0700511extern bool oatIsDirty(CompilationUnit* cUnit, int reg)
512{
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 RegisterInfo* p = oatGetRegInfo(cUnit, reg);
514 return p->dirty;
buzbee67bf8852011-08-17 17:51:35 -0700515}
516
517/*
518 * Similar to oatAllocTemp(), but forces the allocation of a specific
519 * register. No check is made to see if the register was previously
520 * allocated. Use with caution.
521 */
522extern void oatLockTemp(CompilationUnit* cUnit, int reg)
523{
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
buzbee5abfa3e2012-01-31 17:01:43 -0800548static inline 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
buzbee5abfa3e2012-01-31 17:01:43 -0800554extern void oatResetDef(CompilationUnit* cUnit, int reg)
555{
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 resetDefBody(oatGetRegInfo(cUnit, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800557}
558
buzbee31a4a6f2012-02-28 15:36:15 -0800559void nullifyRange(CompilationUnit* cUnit, LIR *start, LIR *finish,
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 int sReg1, int sReg2)
buzbee67bf8852011-08-17 17:51:35 -0700561{
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 if (start && finish) {
563 LIR *p;
564 DCHECK_EQ(sReg1, sReg2);
565 for (p = start; ;p = p->next) {
566 oatNopLIR(p);
567 if (p == finish)
568 break;
buzbee67bf8852011-08-17 17:51:35 -0700569 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 }
buzbee67bf8852011-08-17 17:51:35 -0700571}
572
573/*
574 * Mark the beginning and end LIR of a def sequence. Note that
575 * on entry start points to the LIR prior to the beginning of the
576 * sequence.
577 */
578extern void oatMarkDef(CompilationUnit* cUnit, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700579 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700580{
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 DCHECK(!rl.wide);
582 DCHECK(start && start->next);
583 DCHECK(finish);
584 RegisterInfo* p = oatGetRegInfo(cUnit, rl.lowReg);
585 p->defStart = start->next;
586 p->defEnd = finish;
buzbee67bf8852011-08-17 17:51:35 -0700587}
588
589/*
590 * Mark the beginning and end LIR of a def sequence. Note that
591 * on entry start points to the LIR prior to the beginning of the
592 * sequence.
593 */
594extern void oatMarkDefWide(CompilationUnit* cUnit, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700596{
Bill Buzbeea114add2012-05-03 15:00:40 -0700597 DCHECK(rl.wide);
598 DCHECK(start && start->next);
599 DCHECK(finish);
600 RegisterInfo* p = oatGetRegInfo(cUnit, rl.lowReg);
601 oatResetDef(cUnit, rl.highReg); // Only track low of pair
602 p->defStart = start->next;
603 p->defEnd = finish;
buzbee67bf8852011-08-17 17:51:35 -0700604}
605
buzbee31a4a6f2012-02-28 15:36:15 -0800606extern RegLocation oatWideToNarrow(CompilationUnit* cUnit, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700607{
Bill Buzbeea114add2012-05-03 15:00:40 -0700608 DCHECK(rl.wide);
609 if (rl.location == kLocPhysReg) {
610 RegisterInfo* infoLo = oatGetRegInfo(cUnit, rl.lowReg);
611 RegisterInfo* infoHi = oatGetRegInfo(cUnit, rl.highReg);
612 if (infoLo->isTemp) {
613 infoLo->pair = false;
614 infoLo->defStart = NULL;
615 infoLo->defEnd = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700616 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 if (infoHi->isTemp) {
618 infoHi->pair = false;
619 infoHi->defStart = NULL;
620 infoHi->defEnd = NULL;
621 }
622 }
623 rl.wide = false;
624 return rl;
buzbee67bf8852011-08-17 17:51:35 -0700625}
626
627extern void oatResetDefLoc(CompilationUnit* cUnit, RegLocation rl)
628{
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 DCHECK(!rl.wide);
630 RegisterInfo* p = oatIsTemp(cUnit, rl.lowReg);
631 if (p && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
632 DCHECK(!p->pair);
633 nullifyRange(cUnit, p->defStart, p->defEnd, p->sReg, rl.sRegLow);
634 }
635 oatResetDef(cUnit, rl.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700636}
637
638extern void oatResetDefLocWide(CompilationUnit* cUnit, RegLocation rl)
639{
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 DCHECK(rl.wide);
641 RegisterInfo* pLow = oatIsTemp(cUnit, rl.lowReg);
642 RegisterInfo* pHigh = oatIsTemp(cUnit, rl.highReg);
643 if (pLow && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
644 DCHECK(pLow->pair);
645 nullifyRange(cUnit, pLow->defStart, pLow->defEnd, pLow->sReg, rl.sRegLow);
646 }
647 if (pHigh && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
648 DCHECK(pHigh->pair);
649 }
650 oatResetDef(cUnit, rl.lowReg);
651 oatResetDef(cUnit, rl.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700652}
653
654extern void oatResetDefTracking(CompilationUnit* cUnit)
655{
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 int i;
657 for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
658 resetDefBody(&cUnit->regPool->coreRegs[i]);
659 }
660 for (i=0; i< cUnit->regPool->numFPRegs; i++) {
661 resetDefBody(&cUnit->regPool->FPRegs[i]);
662 }
buzbee67bf8852011-08-17 17:51:35 -0700663}
664
665extern void oatClobberAllRegs(CompilationUnit* cUnit)
666{
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 int i;
668 for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
669 clobberBody(cUnit, &cUnit->regPool->coreRegs[i]);
670 }
671 for (i=0; i< cUnit->regPool->numFPRegs; i++) {
672 clobberBody(cUnit, &cUnit->regPool->FPRegs[i]);
673 }
buzbee67bf8852011-08-17 17:51:35 -0700674}
675
buzbee67bf8852011-08-17 17:51:35 -0700676// Make sure nothing is live and dirty
buzbee31a4a6f2012-02-28 15:36:15 -0800677void flushAllRegsBody(CompilationUnit* cUnit, RegisterInfo* info,
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 int numRegs)
buzbee67bf8852011-08-17 17:51:35 -0700679{
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 int i;
681 for (i=0; i < numRegs; i++) {
682 if (info[i].live && info[i].dirty) {
683 if (info[i].pair) {
684 oatFlushRegWide(cUnit, info[i].reg, info[i].partner);
685 } else {
686 oatFlushReg(cUnit, info[i].reg);
687 }
buzbee67bf8852011-08-17 17:51:35 -0700688 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700689 }
buzbee67bf8852011-08-17 17:51:35 -0700690}
691
692extern void oatFlushAllRegs(CompilationUnit* cUnit)
693{
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 flushAllRegsBody(cUnit, cUnit->regPool->coreRegs,
695 cUnit->regPool->numCoreRegs);
696 flushAllRegsBody(cUnit, cUnit->regPool->FPRegs,
697 cUnit->regPool->numFPRegs);
698 oatClobberAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700699}
700
701
702//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbee31a4a6f2012-02-28 15:36:15 -0800703bool regClassMatches(int regClass, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700704{
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 if (regClass == kAnyReg) {
706 return true;
707 } else if (regClass == kCoreReg) {
708 return !oatIsFpReg(reg);
709 } else {
710 return oatIsFpReg(reg);
711 }
buzbee67bf8852011-08-17 17:51:35 -0700712}
713
714extern void oatMarkLive(CompilationUnit* cUnit, int reg, int sReg)
715{
Bill Buzbeea114add2012-05-03 15:00:40 -0700716 RegisterInfo* info = oatGetRegInfo(cUnit, reg);
717 if ((info->reg == reg) && (info->sReg == sReg) && info->live) {
718 return; /* already live */
719 } else if (sReg != INVALID_SREG) {
720 oatClobberSReg(cUnit, sReg);
721 if (info->isTemp) {
722 info->live = true;
buzbee67bf8852011-08-17 17:51:35 -0700723 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 } else {
725 /* Can't be live if no associated sReg */
726 DCHECK(info->isTemp);
727 info->live = false;
728 }
729 info->sReg = sReg;
buzbee67bf8852011-08-17 17:51:35 -0700730}
731
732extern void oatMarkTemp(CompilationUnit* cUnit, int reg)
733{
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 RegisterInfo* info = oatGetRegInfo(cUnit, reg);
735 info->isTemp = true;
buzbee67bf8852011-08-17 17:51:35 -0700736}
737
buzbee9e0f9b02011-08-24 15:32:46 -0700738extern void oatUnmarkTemp(CompilationUnit* cUnit, int reg)
739{
Bill Buzbeea114add2012-05-03 15:00:40 -0700740 RegisterInfo* info = oatGetRegInfo(cUnit, reg);
741 info->isTemp = false;
buzbee9e0f9b02011-08-24 15:32:46 -0700742}
743
buzbee67bf8852011-08-17 17:51:35 -0700744extern void oatMarkPair(CompilationUnit* cUnit, int lowReg, int highReg)
745{
Bill Buzbeea114add2012-05-03 15:00:40 -0700746 RegisterInfo* infoLo = oatGetRegInfo(cUnit, lowReg);
747 RegisterInfo* infoHi = oatGetRegInfo(cUnit, highReg);
748 infoLo->pair = infoHi->pair = true;
749 infoLo->partner = highReg;
750 infoHi->partner = lowReg;
buzbee67bf8852011-08-17 17:51:35 -0700751}
752
753extern void oatMarkClean(CompilationUnit* cUnit, RegLocation loc)
754{
Bill Buzbeea114add2012-05-03 15:00:40 -0700755 RegisterInfo* info = oatGetRegInfo(cUnit, loc.lowReg);
756 info->dirty = false;
757 if (loc.wide) {
758 info = oatGetRegInfo(cUnit, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700759 info->dirty = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700760 }
buzbee67bf8852011-08-17 17:51:35 -0700761}
762
763extern void oatMarkDirty(CompilationUnit* cUnit, RegLocation loc)
764{
Bill Buzbeea114add2012-05-03 15:00:40 -0700765 if (loc.home) {
766 // If already home, can't be dirty
767 return;
768 }
769 RegisterInfo* info = oatGetRegInfo(cUnit, loc.lowReg);
770 info->dirty = true;
771 if (loc.wide) {
772 info = oatGetRegInfo(cUnit, loc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700773 info->dirty = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700774 }
buzbee67bf8852011-08-17 17:51:35 -0700775}
776
777extern void oatMarkInUse(CompilationUnit* cUnit, int reg)
778{
Bill Buzbeea114add2012-05-03 15:00:40 -0700779 RegisterInfo* info = oatGetRegInfo(cUnit, reg);
780 info->inUse = true;
buzbee67bf8852011-08-17 17:51:35 -0700781}
782
buzbee31a4a6f2012-02-28 15:36:15 -0800783void copyRegInfo(CompilationUnit* cUnit, int newReg, int oldReg)
buzbee67bf8852011-08-17 17:51:35 -0700784{
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 RegisterInfo* newInfo = oatGetRegInfo(cUnit, newReg);
786 RegisterInfo* oldInfo = oatGetRegInfo(cUnit, oldReg);
787 // Target temp status must not change
788 bool isTemp = newInfo->isTemp;
789 *newInfo = *oldInfo;
790 // Restore target's temp status
791 newInfo->isTemp = isTemp;
792 newInfo->reg = newReg;
buzbee67bf8852011-08-17 17:51:35 -0700793}
794
795/*
796 * Return an updated location record with current in-register status.
797 * If the value lives in live temps, reflect that fact. No code
buzbeeb29e4d12011-09-26 15:05:48 -0700798 * is generated. If the live value is part of an older pair,
buzbee67bf8852011-08-17 17:51:35 -0700799 * clobber both low and high.
800 * TUNING: clobbering both is a bit heavy-handed, but the alternative
801 * is a bit complex when dealing with FP regs. Examine code to see
802 * if it's worthwhile trying to be more clever here.
803 */
804
805extern RegLocation oatUpdateLoc(CompilationUnit* cUnit, RegLocation loc)
806{
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 DCHECK(!loc.wide);
808 DCHECK(oatCheckCorePoolSanity(cUnit));
809 if (loc.location != kLocPhysReg) {
810 DCHECK((loc.location == kLocDalvikFrame) ||
811 (loc.location == kLocCompilerTemp));
812 RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg);
813 if (infoLo) {
814 if (infoLo->pair) {
815 oatClobber(cUnit, infoLo->reg);
816 oatClobber(cUnit, infoLo->partner);
817 oatFreeTemp(cUnit, infoLo->reg);
818 } else {
819 loc.lowReg = infoLo->reg;
820 loc.location = kLocPhysReg;
821 }
buzbee67bf8852011-08-17 17:51:35 -0700822 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700823 }
buzbee67bf8852011-08-17 17:51:35 -0700824
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700826}
827
buzbee6181f792011-09-29 11:14:04 -0700828bool oatCheckCorePoolSanity(CompilationUnit* cUnit)
829{
830 for (static int i = 0; i < cUnit->regPool->numCoreRegs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700831 if (cUnit->regPool->coreRegs[i].pair) {
832 static int myReg = cUnit->regPool->coreRegs[i].reg;
833 static int mySreg = cUnit->regPool->coreRegs[i].sReg;
834 static int partnerReg = cUnit->regPool->coreRegs[i].partner;
835 static RegisterInfo* partner = oatGetRegInfo(cUnit, partnerReg);
836 DCHECK(partner != NULL);
837 DCHECK(partner->pair);
838 DCHECK_EQ(myReg, partner->partner);
839 static int partnerSreg = partner->sReg;
840 if (mySreg == INVALID_SREG) {
841 DCHECK_EQ(partnerSreg, INVALID_SREG);
842 } else {
843 int diff = mySreg - partnerSreg;
844 DCHECK((diff == -1) || (diff == 1));
buzbee6181f792011-09-29 11:14:04 -0700845 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 }
847 if (!cUnit->regPool->coreRegs[i].live) {
848 DCHECK(cUnit->regPool->coreRegs[i].defStart == NULL);
849 DCHECK(cUnit->regPool->coreRegs[i].defEnd == NULL);
850 }
buzbee6181f792011-09-29 11:14:04 -0700851 }
852 return true;
853}
854
buzbee67bf8852011-08-17 17:51:35 -0700855/* see comments for updateLoc */
buzbee31a4a6f2012-02-28 15:36:15 -0800856extern RegLocation oatUpdateLocWide(CompilationUnit* cUnit, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700857{
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 DCHECK(loc.wide);
859 DCHECK(oatCheckCorePoolSanity(cUnit));
860 if (loc.location != kLocPhysReg) {
861 DCHECK((loc.location == kLocDalvikFrame) ||
862 (loc.location == kLocCompilerTemp));
863 // Are the dalvik regs already live in physical registers?
864 RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg);
865 RegisterInfo* infoHi = allocLive(cUnit,
866 oatSRegHi(loc.sRegLow), kAnyReg);
867 bool match = true;
868 match = match && (infoLo != NULL);
869 match = match && (infoHi != NULL);
870 // Are they both core or both FP?
871 match = match && (oatIsFpReg(infoLo->reg) == oatIsFpReg(infoHi->reg));
872 // If a pair of floating point singles, are they properly aligned?
873 if (match && oatIsFpReg(infoLo->reg)) {
874 match &= ((infoLo->reg & 0x1) == 0);
875 match &= ((infoHi->reg - infoLo->reg) == 1);
buzbee67bf8852011-08-17 17:51:35 -0700876 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700877 // If previously used as a pair, it is the same pair?
878 if (match && (infoLo->pair || infoHi->pair)) {
879 match = (infoLo->pair == infoHi->pair);
880 match &= ((infoLo->reg == infoHi->partner) &&
881 (infoHi->reg == infoLo->partner));
882 }
883 if (match) {
884 // Can reuse - update the register usage info
885 loc.lowReg = infoLo->reg;
886 loc.highReg = infoHi->reg;
887 loc.location = kLocPhysReg;
888 oatMarkPair(cUnit, loc.lowReg, loc.highReg);
889 DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
890 return loc;
891 }
892 // Can't easily reuse - clobber and free any overlaps
893 if (infoLo) {
894 oatClobber(cUnit, infoLo->reg);
895 oatFreeTemp(cUnit, infoLo->reg);
896 if (infoLo->pair)
897 oatClobber(cUnit, infoLo->partner);
898 }
899 if (infoHi) {
900 oatClobber(cUnit, infoHi->reg);
901 oatFreeTemp(cUnit, infoHi->reg);
902 if (infoHi->pair)
903 oatClobber(cUnit, infoHi->partner);
904 }
905 }
906 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700907}
908
buzbeeed3e9302011-09-23 17:34:19 -0700909
910/* For use in cases we don't know (or care) width */
buzbee31a4a6f2012-02-28 15:36:15 -0800911extern RegLocation oatUpdateRawLoc(CompilationUnit* cUnit, RegLocation loc)
buzbeeed3e9302011-09-23 17:34:19 -0700912{
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 if (loc.wide)
914 return oatUpdateLocWide(cUnit, loc);
915 else
916 return oatUpdateLoc(cUnit, loc);
buzbeeed3e9302011-09-23 17:34:19 -0700917}
918
buzbee31a4a6f2012-02-28 15:36:15 -0800919RegLocation evalLocWide(CompilationUnit* cUnit, RegLocation loc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 int regClass, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700921{
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 DCHECK(loc.wide);
923 int newRegs;
924 int lowReg;
925 int highReg;
buzbee67bf8852011-08-17 17:51:35 -0700926
Bill Buzbeea114add2012-05-03 15:00:40 -0700927 loc = oatUpdateLocWide(cUnit, loc);
buzbee67bf8852011-08-17 17:51:35 -0700928
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 /* If already in registers, we can assume proper form. Right reg class? */
930 if (loc.location == kLocPhysReg) {
931 DCHECK_EQ(oatIsFpReg(loc.lowReg), oatIsFpReg(loc.highReg));
buzbeee3acd072012-02-25 17:03:10 -0800932 DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700933 if (!regClassMatches(regClass, loc.lowReg)) {
934 /* Wrong register class. Reallocate and copy */
935 newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass);
936 lowReg = newRegs & 0xff;
937 highReg = (newRegs >> 8) & 0xff;
938 oatRegCopyWide(cUnit, lowReg, highReg, loc.lowReg,
939 loc.highReg);
940 copyRegInfo(cUnit, lowReg, loc.lowReg);
941 copyRegInfo(cUnit, highReg, loc.highReg);
942 oatClobber(cUnit, loc.lowReg);
943 oatClobber(cUnit, loc.highReg);
944 loc.lowReg = lowReg;
945 loc.highReg = highReg;
946 oatMarkPair(cUnit, loc.lowReg, loc.highReg);
947 DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
948 }
buzbee67bf8852011-08-17 17:51:35 -0700949 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 }
951
952 DCHECK_NE(loc.sRegLow, INVALID_SREG);
953 DCHECK_NE(oatSRegHi(loc.sRegLow), INVALID_SREG);
954
955 newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass);
956 loc.lowReg = newRegs & 0xff;
957 loc.highReg = (newRegs >> 8) & 0xff;
958
959 oatMarkPair(cUnit, loc.lowReg, loc.highReg);
960 if (update) {
961 loc.location = kLocPhysReg;
962 oatMarkLive(cUnit, loc.lowReg, loc.sRegLow);
963 oatMarkLive(cUnit, loc.highReg, oatSRegHi(loc.sRegLow));
964 }
965 DCHECK(!oatIsFpReg(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
966 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700967}
968
969extern RegLocation oatEvalLoc(CompilationUnit* cUnit, RegLocation loc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700970 int regClass, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700971{
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 int newReg;
buzbee67bf8852011-08-17 17:51:35 -0700973
Bill Buzbeea114add2012-05-03 15:00:40 -0700974 if (loc.wide)
975 return evalLocWide(cUnit, loc, regClass, update);
buzbee67bf8852011-08-17 17:51:35 -0700976
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 loc = oatUpdateLoc(cUnit, loc);
buzbee67bf8852011-08-17 17:51:35 -0700978
Bill Buzbeea114add2012-05-03 15:00:40 -0700979 if (loc.location == kLocPhysReg) {
980 if (!regClassMatches(regClass, loc.lowReg)) {
981 /* Wrong register class. Realloc, copy and transfer ownership */
982 newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass);
983 oatRegCopy(cUnit, newReg, loc.lowReg);
984 copyRegInfo(cUnit, newReg, loc.lowReg);
985 oatClobber(cUnit, loc.lowReg);
986 loc.lowReg = newReg;
buzbee67bf8852011-08-17 17:51:35 -0700987 }
988 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 }
990
991 DCHECK_NE(loc.sRegLow, INVALID_SREG);
992
993 newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass);
994 loc.lowReg = newReg;
995
996 if (update) {
997 loc.location = kLocPhysReg;
998 oatMarkLive(cUnit, loc.lowReg, loc.sRegLow);
999 }
1000 return loc;
buzbee67bf8852011-08-17 17:51:35 -07001001}
1002
buzbee15bf9802012-06-12 17:49:27 -07001003extern RegLocation oatGetRawSrc(CompilationUnit* cUnit, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001004{
buzbee15bf9802012-06-12 17:49:27 -07001005 DCHECK(num < mir->ssaRep->numUses);
1006 RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]];
buzbee15bf9802012-06-12 17:49:27 -07001007 return res;
1008}
1009extern RegLocation oatGetRawDest(CompilationUnit* cUnit, MIR* mir)
1010{
Elliott Hughes74847412012-06-20 18:10:21 -07001011 DCHECK_GT(mir->ssaRep->numDefs, 0);
buzbee15bf9802012-06-12 17:49:27 -07001012 RegLocation res = cUnit->regLocation[mir->ssaRep->defs[0]];
buzbee15bf9802012-06-12 17:49:27 -07001013 return res;
1014}
1015extern RegLocation oatGetDest(CompilationUnit* cUnit, MIR* mir)
1016{
1017 RegLocation res = oatGetRawDest(cUnit, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 DCHECK(!res.wide);
1019 return res;
buzbee67bf8852011-08-17 17:51:35 -07001020}
1021extern RegLocation oatGetSrc(CompilationUnit* cUnit, MIR* mir, int num)
1022{
buzbee15bf9802012-06-12 17:49:27 -07001023 RegLocation res = oatGetRawSrc(cUnit, mir, num);
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 DCHECK(!res.wide);
1025 return res;
buzbeee9a72f62011-09-04 17:59:07 -07001026}
buzbee15bf9802012-06-12 17:49:27 -07001027extern RegLocation oatGetDestWide(CompilationUnit* cUnit, MIR* mir)
buzbeee9a72f62011-09-04 17:59:07 -07001028{
buzbee15bf9802012-06-12 17:49:27 -07001029 RegLocation res = oatGetRawDest(cUnit, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 DCHECK(res.wide);
1031 return res;
buzbee67bf8852011-08-17 17:51:35 -07001032}
1033
1034extern RegLocation oatGetSrcWide(CompilationUnit* cUnit, MIR* mir,
buzbee15bf9802012-06-12 17:49:27 -07001035 int low)
buzbee67bf8852011-08-17 17:51:35 -07001036{
buzbee15bf9802012-06-12 17:49:27 -07001037 RegLocation res = oatGetRawSrc(cUnit, mir, low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001038 DCHECK(res.wide);
1039 return res;
buzbee67bf8852011-08-17 17:51:35 -07001040}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001041
buzbeee3acd072012-02-25 17:03:10 -08001042/* USE SSA names to count references of base Dalvik vRegs. */
1043void oatCountRefs(CompilationUnit *cUnit, BasicBlock* bb,
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 RefCounts* coreCounts, RefCounts* fpCounts)
buzbeee3acd072012-02-25 17:03:10 -08001045{
Bill Buzbeea114add2012-05-03 15:00:40 -07001046 if ((cUnit->disableOpt & (1 << kPromoteRegs)) ||
1047 !((bb->blockType == kEntryBlock) || (bb->blockType == kExitBlock) ||
1048 (bb->blockType == kDalvikByteCode))) {
1049 return;
1050 }
1051 for (int i = 0; i < cUnit->numSSARegs;) {
1052 RegLocation loc = cUnit->regLocation[i];
1053 RefCounts* counts = loc.fp ? fpCounts : coreCounts;
1054 int pMapIdx = SRegToPMap(cUnit, loc.sRegLow);
1055 if (loc.defined) {
1056 counts[pMapIdx].count += cUnit->useCounts.elemList[i];
buzbee239c4e72012-03-16 08:42:29 -07001057 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 if (loc.wide) {
1059 if (loc.defined) {
1060 if (loc.fp) {
1061 counts[pMapIdx].doubleStart = true;
1062 counts[pMapIdx+1].count += cUnit->useCounts.elemList[i+1];
buzbee239c4e72012-03-16 08:42:29 -07001063 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001064 }
1065 i += 2;
1066 } else {
1067 i++;
buzbeee3acd072012-02-25 17:03:10 -08001068 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001069 }
buzbeee3acd072012-02-25 17:03:10 -08001070}
1071
1072/* qsort callback function, sort descending */
1073int oatSortCounts(const void *val1, const void *val2)
1074{
Bill Buzbeea114add2012-05-03 15:00:40 -07001075 const RefCounts* op1 = (const RefCounts*)val1;
1076 const RefCounts* op2 = (const RefCounts*)val2;
1077 return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
buzbeee3acd072012-02-25 17:03:10 -08001078}
1079
1080void oatDumpCounts(const RefCounts* arr, int size, const char* msg)
1081{
Bill Buzbeea114add2012-05-03 15:00:40 -07001082 LOG(INFO) << msg;
1083 for (int i = 0; i < size; i++) {
1084 LOG(INFO) << "sReg[" << arr[i].sReg << "]: " << arr[i].count;
1085 }
buzbeee3acd072012-02-25 17:03:10 -08001086}
1087
1088/*
1089 * Note: some portions of this code required even if the kPromoteRegs
1090 * optimization is disabled.
1091 */
1092extern void oatDoPromotion(CompilationUnit* cUnit)
1093{
Bill Buzbeea114add2012-05-03 15:00:40 -07001094 int regBias = cUnit->numCompilerTemps + 1;
1095 int dalvikRegs = cUnit->numDalvikRegisters;
1096 int numRegs = dalvikRegs + regBias;
1097 const int promotionThreshold = 2;
buzbeee3acd072012-02-25 17:03:10 -08001098
Bill Buzbeea114add2012-05-03 15:00:40 -07001099 // Allow target code to add any special registers
1100 oatAdjustSpillMask(cUnit);
buzbeee3acd072012-02-25 17:03:10 -08001101
Bill Buzbeea114add2012-05-03 15:00:40 -07001102 /*
1103 * Simple register promotion. Just do a static count of the uses
1104 * of Dalvik registers. Note that we examine the SSA names, but
1105 * count based on original Dalvik register name. Count refs
1106 * separately based on type in order to give allocation
1107 * preference to fp doubles - which must be allocated sequential
1108 * physical single fp registers started with an even-numbered
1109 * reg.
1110 * TUNING: replace with linear scan once we have the ability
1111 * to describe register live ranges for GC.
1112 */
1113 RefCounts *coreRegs = (RefCounts *)
1114 oatNew(cUnit, sizeof(RefCounts) * numRegs, true, kAllocRegAlloc);
1115 RefCounts *fpRegs = (RefCounts *)
1116 oatNew(cUnit, sizeof(RefCounts) * numRegs, true, kAllocRegAlloc);
1117 // Set ssa names for original Dalvik registers
1118 for (int i = 0; i < dalvikRegs; i++) {
1119 coreRegs[i].sReg = fpRegs[i].sReg = i;
1120 }
1121 // Set ssa name for Method*
1122 coreRegs[dalvikRegs].sReg = cUnit->methodSReg;
1123 fpRegs[dalvikRegs].sReg = cUnit->methodSReg; // For consistecy
1124 // Set ssa names for compilerTemps
1125 for (int i = 1; i <= cUnit->numCompilerTemps; i++) {
1126 CompilerTemp* ct = (CompilerTemp*)cUnit->compilerTemps.elemList[i];
1127 coreRegs[dalvikRegs + i].sReg = ct->sReg;
1128 fpRegs[dalvikRegs + i].sReg = ct->sReg;
1129 }
1130
1131 GrowableListIterator iterator;
1132 oatGrowableListIteratorInit(&cUnit->blockList, &iterator);
1133 while (true) {
1134 BasicBlock* bb;
1135 bb = (BasicBlock*)oatGrowableListIteratorNext(&iterator);
1136 if (bb == NULL) break;
1137 oatCountRefs(cUnit, bb, coreRegs, fpRegs);
1138 }
1139
1140 /*
1141 * Ideally, we'd allocate doubles starting with an even-numbered
1142 * register. Bias the counts to try to allocate any vreg that's
1143 * used as the start of a pair first.
1144 */
1145 for (int i = 0; i < numRegs; i++) {
1146 if (fpRegs[i].doubleStart) {
1147 fpRegs[i].count *= 2;
buzbeee3acd072012-02-25 17:03:10 -08001148 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001149 }
1150
1151 // Sort the count arrays
1152 qsort(coreRegs, numRegs, sizeof(RefCounts), oatSortCounts);
1153 qsort(fpRegs, numRegs, sizeof(RefCounts), oatSortCounts);
1154
1155 if (cUnit->printMe) {
1156 oatDumpCounts(coreRegs, numRegs, "Core regs after sort");
1157 oatDumpCounts(fpRegs, numRegs, "Fp regs after sort");
1158 }
1159
1160 if (!(cUnit->disableOpt & (1 << kPromoteRegs))) {
1161 // Promote fpRegs
1162 for (int i = 0; (i < numRegs) &&
1163 (fpRegs[i].count >= promotionThreshold ); i++) {
1164 int pMapIdx = SRegToPMap(cUnit, fpRegs[i].sReg);
1165 if (cUnit->promotionMap[pMapIdx].fpLocation != kLocPhysReg) {
1166 int reg = oatAllocPreservedFPReg(cUnit, fpRegs[i].sReg,
1167 fpRegs[i].doubleStart);
1168 if (reg < 0) {
1169 break; // No more left
1170 }
1171 }
buzbee239c4e72012-03-16 08:42:29 -07001172 }
buzbee9c044ce2012-03-18 13:24:07 -07001173
Bill Buzbeea114add2012-05-03 15:00:40 -07001174 // Promote core regs
1175 for (int i = 0; (i < numRegs) &&
1176 (coreRegs[i].count > promotionThreshold); i++) {
1177 int pMapIdx = SRegToPMap(cUnit, coreRegs[i].sReg);
1178 if (cUnit->promotionMap[pMapIdx].coreLocation !=
1179 kLocPhysReg) {
1180 int reg = oatAllocPreservedCoreReg(cUnit, coreRegs[i].sReg);
1181 if (reg < 0) {
1182 break; // No more left
1183 }
1184 }
buzbeee3acd072012-02-25 17:03:10 -08001185 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001186 } else if (cUnit->qdMode) {
1187 oatAllocPreservedCoreReg(cUnit, cUnit->methodSReg);
buzbeee3acd072012-02-25 17:03:10 -08001188 for (int i = 0; i < numRegs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001189 int reg = oatAllocPreservedCoreReg(cUnit, i);
1190 if (reg < 0) {
1191 break; // No more left
1192 }
buzbeee3acd072012-02-25 17:03:10 -08001193 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001194 }
buzbeee3acd072012-02-25 17:03:10 -08001195
buzbeee3acd072012-02-25 17:03:10 -08001196
Bill Buzbeea114add2012-05-03 15:00:40 -07001197 // Now, update SSA names to new home locations
1198 for (int i = 0; i < cUnit->numSSARegs; i++) {
1199 RegLocation *curr = &cUnit->regLocation[i];
1200 int pMapIdx = SRegToPMap(cUnit, curr->sRegLow);
1201 if (!curr->wide) {
1202 if (curr->fp) {
1203 if (cUnit->promotionMap[pMapIdx].fpLocation == kLocPhysReg) {
1204 curr->location = kLocPhysReg;
1205 curr->lowReg = cUnit->promotionMap[pMapIdx].fpReg;
1206 curr->home = true;
1207 }
1208 } else {
1209 if (cUnit->promotionMap[pMapIdx].coreLocation == kLocPhysReg) {
1210 curr->location = kLocPhysReg;
1211 curr->lowReg = cUnit->promotionMap[pMapIdx].coreReg;
1212 curr->home = true;
1213 }
1214 }
1215 curr->highReg = INVALID_REG;
1216 } else {
1217 if (curr->highWord) {
1218 continue;
1219 }
1220 if (curr->fp) {
1221 if ((cUnit->promotionMap[pMapIdx].fpLocation == kLocPhysReg) &&
1222 (cUnit->promotionMap[pMapIdx+1].fpLocation ==
1223 kLocPhysReg)) {
1224 int lowReg = cUnit->promotionMap[pMapIdx].fpReg;
1225 int highReg = cUnit->promotionMap[pMapIdx+1].fpReg;
1226 // Doubles require pair of singles starting at even reg
1227 if (((lowReg & 0x1) == 0) && ((lowReg + 1) == highReg)) {
1228 curr->location = kLocPhysReg;
1229 curr->lowReg = lowReg;
1230 curr->highReg = highReg;
1231 curr->home = true;
1232 }
1233 }
1234 } else {
1235 if ((cUnit->promotionMap[pMapIdx].coreLocation == kLocPhysReg)
1236 && (cUnit->promotionMap[pMapIdx+1].coreLocation ==
1237 kLocPhysReg)) {
1238 curr->location = kLocPhysReg;
1239 curr->lowReg = cUnit->promotionMap[pMapIdx].coreReg;
1240 curr->highReg = cUnit->promotionMap[pMapIdx+1].coreReg;
1241 curr->home = true;
1242 }
1243 }
buzbeee3acd072012-02-25 17:03:10 -08001244 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001245 }
buzbeeca7a5e42012-08-20 11:12:18 -07001246 if (cUnit->printMe) {
1247 oatDumpPromotionMap(cUnit);
1248 }
buzbeee3acd072012-02-25 17:03:10 -08001249}
1250
1251/* Returns sp-relative offset in bytes for a VReg */
1252extern int oatVRegOffset(CompilationUnit* cUnit, int vReg)
1253{
Ian Rogers0399dde2012-06-06 17:09:28 -07001254 return StackVisitor::GetVRegOffset(cUnit->code_item, cUnit->coreSpillMask,
1255 cUnit->fpSpillMask, cUnit->frameSize, vReg);
buzbeee3acd072012-02-25 17:03:10 -08001256}
1257
1258/* Returns sp-relative offset in bytes for a SReg */
1259extern int oatSRegOffset(CompilationUnit* cUnit, int sReg)
1260{
Bill Buzbeea114add2012-05-03 15:00:40 -07001261 return oatVRegOffset(cUnit, SRegToVReg(cUnit, sReg));
buzbeee3acd072012-02-25 17:03:10 -08001262}
1263
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001264} // namespace art