blob: 687571815453fc9b8a4ab719447be895cad3d368 [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
17/*
18 * This file contains register alloction support and is intended to be
19 * included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 */
24
25#include "../CompilerUtility.h"
26#include "../CompilerIR.h"
27#include "../Dataflow.h"
28#include "Ralloc.h"
29
30#define SREG(c, s) ((c)->regLocation[(s)].sRegLow)
31/*
32 * Get the "real" sreg number associated with an sReg slot. In general,
33 * sReg values passed through codegen are the SSA names created by
34 * dataflow analysis and refer to slot numbers in the cUnit->regLocation
35 * array. However, renaming is accomplished by simply replacing RegLocation
36 * entries in the cUnit->reglocation[] array. Therefore, when location
37 * records for operands are first created, we need to ask the locRecord
38 * identified by the dataflow pass what it's new name is.
39 */
40
41/*
42 * Free all allocated temps in the temp pools. Note that this does
43 * not affect the "liveness" of a temp register, which will stay
44 * live until it is either explicitly killed or reallocated.
45 */
46extern void oatResetRegPool(CompilationUnit* cUnit)
47{
48 int i;
49 for (i=0; i < cUnit->regPool->numCoreRegs; i++) {
50 if (cUnit->regPool->coreRegs[i].isTemp)
51 cUnit->regPool->coreRegs[i].inUse = false;
52 }
53 for (i=0; i < cUnit->regPool->numFPRegs; i++) {
54 if (cUnit->regPool->FPRegs[i].isTemp)
55 cUnit->regPool->FPRegs[i].inUse = false;
56 }
57}
58
59 /* Set up temp & preserved register pools specialized by target */
60extern void oatInitPool(RegisterInfo* regs, int* regNums, int num)
61{
62 int i;
63 for (i=0; i < num; i++) {
64 regs[i].reg = regNums[i];
65 regs[i].inUse = false;
66 regs[i].isTemp = false;
67 regs[i].pair = false;
68 regs[i].live = false;
69 regs[i].dirty = false;
70 regs[i].sReg = INVALID_SREG;
71 }
72}
73
buzbeeed3e9302011-09-23 17:34:19 -070074STATIC void dumpRegPool(RegisterInfo* p, int numRegs)
buzbee67bf8852011-08-17 17:51:35 -070075{
76 int i;
77 LOG(INFO) << "================================================";
78 for (i=0; i < numRegs; i++ ){
79 char buf[100];
80 snprintf(buf, 100,
81 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x",
82 p[i].reg, p[i].isTemp, p[i].inUse, p[i].pair, p[i].partner,
83 p[i].live, p[i].dirty, p[i].sReg,(int)p[i].defStart,
84 (int)p[i].defEnd);
85 LOG(INFO) << buf;
86 }
87 LOG(INFO) << "================================================";
88}
89
90/* Get info for a reg. */
buzbeeed3e9302011-09-23 17:34:19 -070091STATIC RegisterInfo* getRegInfo(CompilationUnit* cUnit, int reg)
buzbee67bf8852011-08-17 17:51:35 -070092{
93 int numRegs = cUnit->regPool->numCoreRegs;
94 RegisterInfo* p = cUnit->regPool->coreRegs;
95 int i;
96 for (i=0; i< numRegs; i++) {
97 if (p[i].reg == reg) {
98 return &p[i];
99 }
100 }
101 p = cUnit->regPool->FPRegs;
102 numRegs = cUnit->regPool->numFPRegs;
103 for (i=0; i< numRegs; i++) {
104 if (p[i].reg == reg) {
105 return &p[i];
106 }
107 }
108 LOG(FATAL) << "Tried to get info on a non-existant reg :r" << reg;
109 return NULL; // Quiet gcc
110}
111
112void oatFlushRegWide(CompilationUnit* cUnit, int reg1, int reg2)
113{
114 RegisterInfo* info1 = getRegInfo(cUnit, reg1);
115 RegisterInfo* info2 = getRegInfo(cUnit, reg2);
buzbeeed3e9302011-09-23 17:34:19 -0700116 DCHECK(info1 && info2 && info1->pair && info2->pair &&
buzbee67bf8852011-08-17 17:51:35 -0700117 (info1->partner == info2->reg) &&
118 (info2->partner == info1->reg));
119 if ((info1->live && info1->dirty) || (info2->live && info2->dirty)) {
120 if (!(info1->isTemp && info2->isTemp)) {
121 /* Should not happen. If it does, there's a problem in evalLoc */
122 LOG(FATAL) << "Long half-temp, half-promoted";
123 }
124
125 info1->dirty = false;
126 info2->dirty = false;
127 if (oatS2VReg(cUnit, info2->sReg) <
128 oatS2VReg(cUnit, info1->sReg))
129 info1 = info2;
130 int vReg = oatS2VReg(cUnit, info1->sReg);
131 oatFlushRegWideImpl(cUnit, rSP,
132 oatVRegOffset(cUnit, vReg),
133 info1->reg, info1->partner);
134 }
135}
136
137void oatFlushReg(CompilationUnit* cUnit, int reg)
138{
139 RegisterInfo* info = getRegInfo(cUnit, reg);
140 if (info->live && info->dirty) {
141 info->dirty = false;
142 int vReg = oatS2VReg(cUnit, info->sReg);
143 oatFlushRegImpl(cUnit, rSP,
144 oatVRegOffset(cUnit, vReg),
145 reg, kWord);
146 }
147}
148
149/* return true if found reg to clobber */
buzbeeed3e9302011-09-23 17:34:19 -0700150STATIC bool clobberRegBody(CompilationUnit* cUnit, RegisterInfo* p,
buzbee67bf8852011-08-17 17:51:35 -0700151 int numRegs, int reg)
152{
153 int i;
154 for (i=0; i< numRegs; i++) {
155 if (p[i].reg == reg) {
156 if (p[i].isTemp) {
157 if (p[i].isTemp && p[i].live && p[i].dirty) {
158 if (p[i].pair) {
159 oatFlushRegWide(cUnit, p[i].reg, p[i].partner);
160 } else {
161 oatFlushReg(cUnit, p[i].reg);
162 }
163 }
164 p[i].live = false;
165 p[i].sReg = INVALID_SREG;
166 }
167 p[i].defStart = NULL;
168 p[i].defEnd = NULL;
169 if (p[i].pair) {
170 p[i].pair = false;
171 /* partners should be in same pool */
172 clobberRegBody(cUnit, p, numRegs, p[i].partner);
173 }
174 return true;
175 }
176 }
177 return false;
178}
179
180/* Mark a temp register as dead. Does not affect allocation state. */
181void oatClobber(CompilationUnit* cUnit, int reg)
182{
183 if (!clobberRegBody(cUnit, cUnit->regPool->coreRegs,
184 cUnit->regPool->numCoreRegs, reg)) {
185 clobberRegBody(cUnit, cUnit->regPool->FPRegs,
186 cUnit->regPool->numFPRegs, reg);
187 }
188}
189
buzbeeed3e9302011-09-23 17:34:19 -0700190STATIC void clobberSRegBody(RegisterInfo* p, int numRegs, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700191{
192 int i;
193 for (i=0; i< numRegs; i++) {
194 if (p[i].sReg == sReg) {
195 if (p[i].isTemp) {
196 p[i].live = false;
197 }
198 p[i].defStart = NULL;
199 p[i].defEnd = NULL;
200 }
201 }
202}
203
204/* Clobber any temp associated with an sReg. Could be in either class */
205extern void oatClobberSReg(CompilationUnit* cUnit, int sReg)
206{
207 clobberSRegBody(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs,
208 sReg);
209 clobberSRegBody(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs,
210 sReg);
211}
212
213/* Reserve a callee-save register. Return -1 if none available */
214extern int oatAllocPreservedCoreReg(CompilationUnit* cUnit, int sReg)
215{
216 int res = -1;
217 RegisterInfo* coreRegs = cUnit->regPool->coreRegs;
218 for (int i = 0; i < cUnit->regPool->numCoreRegs; i++) {
219 if (!coreRegs[i].isTemp && !coreRegs[i].inUse) {
220 res = coreRegs[i].reg;
221 coreRegs[i].inUse = true;
222 cUnit->coreSpillMask |= (1 << res);
buzbeec41e5b52011-09-23 12:46:19 -0700223 cUnit->coreVmapTable.push_back(sReg);
buzbee67bf8852011-08-17 17:51:35 -0700224 cUnit->numSpills++;
225 cUnit->regLocation[sReg].location = kLocPhysReg;
226 cUnit->regLocation[sReg].lowReg = res;
227 cUnit->regLocation[sReg].home = true;
228 break;
229 }
230 }
231 return res;
232}
233
234/*
235 * Reserve a callee-save fp single register. Try to fullfill request for
236 * even/odd allocation, but go ahead and allocate anything if not
237 * available. If nothing's available, return -1.
238 */
buzbeeed3e9302011-09-23 17:34:19 -0700239STATIC int allocPreservedSingle(CompilationUnit* cUnit, int sReg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700240{
241 int res = -1;
242 RegisterInfo* FPRegs = cUnit->regPool->FPRegs;
243 for (int i = 0; i < cUnit->regPool->numFPRegs; i++) {
244 if (!FPRegs[i].isTemp && !FPRegs[i].inUse &&
245 ((FPRegs[i].reg & 0x1) == 0) == even) {
246 res = FPRegs[i].reg;
247 FPRegs[i].inUse = true;
248 cUnit->fpSpillMask |= (1 << (res & FP_REG_MASK));
buzbeec41e5b52011-09-23 12:46:19 -0700249 cUnit->fpVmapTable.push_back(sReg);
buzbee67bf8852011-08-17 17:51:35 -0700250 cUnit->numSpills++;
251 cUnit->numFPSpills++;
252 cUnit->regLocation[sReg].fpLocation = kLocPhysReg;
253 cUnit->regLocation[sReg].fpLowReg = res;
254 cUnit->regLocation[sReg].home = true;
255 break;
256 }
257 }
258 return res;
259}
260
261/*
262 * Somewhat messy code here. We want to allocate a pair of contiguous
263 * physical single-precision floating point registers starting with
264 * an even numbered reg. It is possible that the paired sReg (sReg+1)
265 * has already been allocated - try to fit if possible. Fail to
266 * allocate if we can't meet the requirements for the pair of
267 * sReg<=sX[even] & (sReg+1)<= sX+1.
268 */
buzbeeed3e9302011-09-23 17:34:19 -0700269STATIC int allocPreservedDouble(CompilationUnit* cUnit, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700270{
271 int res = -1; // Assume failure
272 if (cUnit->regLocation[sReg+1].fpLocation == kLocPhysReg) {
273 // Upper reg is already allocated. Can we fit?
274 int highReg = cUnit->regLocation[sReg+1].fpLowReg;
275 if ((highReg & 1) == 0) {
276 // High reg is even - fail.
277 return res;
278 }
279 // Is the low reg of the pair free?
280 RegisterInfo* p = getRegInfo(cUnit, highReg-1);
281 if (p->inUse || p->isTemp) {
282 // Already allocated or not preserved - fail.
283 return res;
284 }
285 // OK - good to go.
286 res = p->reg;
287 p->inUse = true;
buzbeeed3e9302011-09-23 17:34:19 -0700288 DCHECK_EQ((res & 1), 0);
buzbee67bf8852011-08-17 17:51:35 -0700289 cUnit->fpSpillMask |= (1 << (res & FP_REG_MASK));
buzbee042946d2011-09-23 15:49:27 -0700290 cUnit->fpVmapTable.push_back(sReg);
buzbee67bf8852011-08-17 17:51:35 -0700291 cUnit->numSpills++;
292 cUnit->numFPSpills ++;
293 } else {
294 RegisterInfo* FPRegs = cUnit->regPool->FPRegs;
295 for (int i = 0; i < cUnit->regPool->numFPRegs; i++) {
296 if (!FPRegs[i].isTemp && !FPRegs[i].inUse &&
297 ((FPRegs[i].reg & 0x1) == 0x0) &&
298 !FPRegs[i+1].isTemp && !FPRegs[i+1].inUse &&
299 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
300 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
301 res = FPRegs[i].reg;
302 FPRegs[i].inUse = true;
303 cUnit->fpSpillMask |= (1 << (res & FP_REG_MASK));
buzbee042946d2011-09-23 15:49:27 -0700304 cUnit->fpVmapTable.push_back(sReg);
buzbee67bf8852011-08-17 17:51:35 -0700305 FPRegs[i+1].inUse = true;
306 cUnit->fpSpillMask |= (1 << ((res+1) & FP_REG_MASK));
buzbee042946d2011-09-23 15:49:27 -0700307 cUnit->fpVmapTable.push_back(sReg);
buzbee67bf8852011-08-17 17:51:35 -0700308 cUnit->numSpills += 2;
309 cUnit->numFPSpills += 2;
310 break;
311 }
312 }
313 }
314 if (res != -1) {
315 cUnit->regLocation[sReg].fpLocation = kLocPhysReg;
316 cUnit->regLocation[sReg].fpLowReg = res;
317 cUnit->regLocation[sReg].home = true;
318 cUnit->regLocation[sReg+1].fpLocation = kLocPhysReg;
319 cUnit->regLocation[sReg+1].fpLowReg = res + 1;
320 cUnit->regLocation[sReg+1].home = true;
321 }
322 return res;
323}
324
325
326/*
327 * Reserve a callee-save fp register. If this register can be used
328 * as the first of a double, attempt to allocate an even pair of fp
329 * single regs (but if can't still attempt to allocate a single, preferring
330 * first to allocate an odd register.
331 */
332extern int oatAllocPreservedFPReg(CompilationUnit* cUnit, int sReg,
333 bool doubleStart)
334{
335 int res = -1;
336 if (doubleStart) {
337 res = allocPreservedDouble(cUnit, sReg);
338 } else {
339 }
340 if (res == -1) {
341 res = allocPreservedSingle(cUnit, sReg, false /* try odd # */);
342 }
343 if (res == -1)
344 res = allocPreservedSingle(cUnit, sReg, true /* try even # */);
345 return res;
346}
347
buzbeeed3e9302011-09-23 17:34:19 -0700348STATIC int allocTempBody(CompilationUnit* cUnit, RegisterInfo* p, int numRegs,
buzbee67bf8852011-08-17 17:51:35 -0700349 int* nextTemp, bool required)
350{
351 int i;
352 int next = *nextTemp;
353 for (i=0; i< numRegs; i++) {
354 if (next >= numRegs)
355 next = 0;
356 if (p[next].isTemp && !p[next].inUse && !p[next].live) {
357 oatClobber(cUnit, p[next].reg);
358 p[next].inUse = true;
359 p[next].pair = false;
360 *nextTemp = next + 1;
361 return p[next].reg;
362 }
363 next++;
364 }
365 next = *nextTemp;
366 for (i=0; i< numRegs; i++) {
367 if (next >= numRegs)
368 next = 0;
369 if (p[next].isTemp && !p[next].inUse) {
370 oatClobber(cUnit, p[next].reg);
371 p[next].inUse = true;
372 p[next].pair = false;
373 *nextTemp = next + 1;
374 return p[next].reg;
375 }
376 next++;
377 }
378 if (required) {
379 dumpRegPool(cUnit->regPool->coreRegs,
380 cUnit->regPool->numCoreRegs);
381 LOG(FATAL) << "No free temp registers";
382 }
383 return -1; // No register available
384}
385
386//REDO: too many assumptions.
387extern int oatAllocTempDouble(CompilationUnit* cUnit)
388{
389 RegisterInfo* p = cUnit->regPool->FPRegs;
390 int numRegs = cUnit->regPool->numFPRegs;
391 int next = cUnit->regPool->nextFPReg;
392 int i;
393
394 for (i=0; i < numRegs; i+=2) {
395 /* Cleanup - not all targets need aligned regs */
396 if (next & 1)
397 next++;
398 if (next >= numRegs)
399 next = 0;
400 if ((p[next].isTemp && !p[next].inUse && !p[next].live) &&
401 (p[next+1].isTemp && !p[next+1].inUse && !p[next+1].live)) {
402 oatClobber(cUnit, p[next].reg);
403 oatClobber(cUnit, p[next+1].reg);
404 p[next].inUse = true;
405 p[next+1].inUse = true;
buzbeeed3e9302011-09-23 17:34:19 -0700406 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
407 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbee67bf8852011-08-17 17:51:35 -0700408 cUnit->regPool->nextFPReg += 2;
409 return p[next].reg;
410 }
411 next += 2;
412 }
413 next = cUnit->regPool->nextFPReg;
414 for (i=0; i < numRegs; i+=2) {
415 if (next >= numRegs)
416 next = 0;
417 if (p[next].isTemp && !p[next].inUse && p[next+1].isTemp &&
418 !p[next+1].inUse) {
419 oatClobber(cUnit, p[next].reg);
420 oatClobber(cUnit, p[next+1].reg);
421 p[next].inUse = true;
422 p[next+1].inUse = true;
buzbeeed3e9302011-09-23 17:34:19 -0700423 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
424 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbee67bf8852011-08-17 17:51:35 -0700425 cUnit->regPool->nextFPReg += 2;
426 return p[next].reg;
427 }
428 next += 2;
429 }
430 LOG(FATAL) << "No free temp registers";
431 return -1;
432}
433
434/* Return a temp if one is available, -1 otherwise */
435extern int oatAllocFreeTemp(CompilationUnit* cUnit)
436{
437 return allocTempBody(cUnit, cUnit->regPool->coreRegs,
438 cUnit->regPool->numCoreRegs,
439 &cUnit->regPool->nextCoreReg, true);
440}
441
442extern int oatAllocTemp(CompilationUnit* cUnit)
443{
444 return allocTempBody(cUnit, cUnit->regPool->coreRegs,
445 cUnit->regPool->numCoreRegs,
446 &cUnit->regPool->nextCoreReg, true);
447}
448
449extern int oatAllocTempFloat(CompilationUnit* cUnit)
450{
451 return allocTempBody(cUnit, cUnit->regPool->FPRegs,
452 cUnit->regPool->numFPRegs,
453 &cUnit->regPool->nextFPReg, true);
454}
455
buzbeeed3e9302011-09-23 17:34:19 -0700456STATIC RegisterInfo* allocLiveBody(RegisterInfo* p, int numRegs, int sReg)
buzbee67bf8852011-08-17 17:51:35 -0700457{
458 int i;
459 if (sReg == -1)
460 return NULL;
461 for (i=0; i < numRegs; i++) {
462 if (p[i].live && (p[i].sReg == sReg)) {
463 if (p[i].isTemp)
464 p[i].inUse = true;
465 return &p[i];
466 }
467 }
468 return NULL;
469}
470
buzbeeed3e9302011-09-23 17:34:19 -0700471STATIC RegisterInfo* allocLive(CompilationUnit* cUnit, int sReg,
buzbee67bf8852011-08-17 17:51:35 -0700472 int regClass)
473{
474 RegisterInfo* res = NULL;
475 switch(regClass) {
476 case kAnyReg:
477 res = allocLiveBody(cUnit->regPool->FPRegs,
478 cUnit->regPool->numFPRegs, sReg);
479 if (res)
480 break;
481 /* Intentional fallthrough */
482 case kCoreReg:
483 res = allocLiveBody(cUnit->regPool->coreRegs,
484 cUnit->regPool->numCoreRegs, sReg);
485 break;
486 case kFPReg:
487 res = allocLiveBody(cUnit->regPool->FPRegs,
488 cUnit->regPool->numFPRegs, sReg);
489 break;
490 default:
491 LOG(FATAL) << "Invalid register type";
492 }
493 return res;
494}
495
496extern void oatFreeTemp(CompilationUnit* cUnit, int reg)
497{
498 RegisterInfo* p = cUnit->regPool->coreRegs;
499 int numRegs = cUnit->regPool->numCoreRegs;
500 int i;
501 for (i=0; i< numRegs; i++) {
502 if (p[i].reg == reg) {
503 if (p[i].isTemp) {
504 p[i].inUse = false;
505 }
506 p[i].pair = false;
507 return;
508 }
509 }
510 p = cUnit->regPool->FPRegs;
511 numRegs = cUnit->regPool->numFPRegs;
512 for (i=0; i< numRegs; i++) {
513 if (p[i].reg == reg) {
514 if (p[i].isTemp) {
515 p[i].inUse = false;
516 }
517 p[i].pair = false;
518 return;
519 }
520 }
521 LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
522}
523
524extern RegisterInfo* oatIsLive(CompilationUnit* cUnit, int reg)
525{
526 RegisterInfo* p = cUnit->regPool->coreRegs;
527 int numRegs = cUnit->regPool->numCoreRegs;
528 int i;
529 for (i=0; i< numRegs; i++) {
530 if (p[i].reg == reg) {
531 return p[i].live ? &p[i] : NULL;
532 }
533 }
534 p = cUnit->regPool->FPRegs;
535 numRegs = cUnit->regPool->numFPRegs;
536 for (i=0; i< numRegs; i++) {
537 if (p[i].reg == reg) {
538 return p[i].live ? &p[i] : NULL;
539 }
540 }
541 return NULL;
542}
543
544extern RegisterInfo* oatIsTemp(CompilationUnit* cUnit, int reg)
545{
546 RegisterInfo* p = getRegInfo(cUnit, reg);
547 return (p->isTemp) ? p : NULL;
548}
549
550extern bool oatIsDirty(CompilationUnit* cUnit, int reg)
551{
552 RegisterInfo* p = getRegInfo(cUnit, reg);
553 return p->dirty;
554}
555
556/*
557 * Similar to oatAllocTemp(), but forces the allocation of a specific
558 * register. No check is made to see if the register was previously
559 * allocated. Use with caution.
560 */
561extern void oatLockTemp(CompilationUnit* cUnit, int reg)
562{
563 RegisterInfo* p = cUnit->regPool->coreRegs;
564 int numRegs = cUnit->regPool->numCoreRegs;
565 int i;
566 for (i=0; i< numRegs; i++) {
567 if (p[i].reg == reg) {
buzbeeed3e9302011-09-23 17:34:19 -0700568 DCHECK(p[i].isTemp);
buzbee67bf8852011-08-17 17:51:35 -0700569 p[i].inUse = true;
570 p[i].live = false;
571 return;
572 }
573 }
574 p = cUnit->regPool->FPRegs;
575 numRegs = cUnit->regPool->numFPRegs;
576 for (i=0; i< numRegs; i++) {
577 if (p[i].reg == reg) {
buzbeeed3e9302011-09-23 17:34:19 -0700578 DCHECK(p[i].isTemp);
buzbee67bf8852011-08-17 17:51:35 -0700579 p[i].inUse = true;
580 p[i].live = false;
581 return;
582 }
583 }
584 LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
585}
586
587extern void oatResetDef(CompilationUnit* cUnit, int reg)
588{
589 RegisterInfo* p = getRegInfo(cUnit, reg);
590 p->defStart = NULL;
591 p->defEnd = NULL;
592}
593
buzbeeed3e9302011-09-23 17:34:19 -0700594STATIC void nullifyRange(CompilationUnit* cUnit, LIR *start, LIR *finish,
buzbee67bf8852011-08-17 17:51:35 -0700595 int sReg1, int sReg2)
596{
597 if (start && finish) {
598 LIR *p;
buzbeeed3e9302011-09-23 17:34:19 -0700599 DCHECK_EQ(sReg1, sReg2);
buzbee67bf8852011-08-17 17:51:35 -0700600 for (p = start; ;p = p->next) {
601 ((ArmLIR *)p)->flags.isNop = true;
602 if (p == finish)
603 break;
604 }
605 }
606}
607
608/*
609 * Mark the beginning and end LIR of a def sequence. Note that
610 * on entry start points to the LIR prior to the beginning of the
611 * sequence.
612 */
613extern void oatMarkDef(CompilationUnit* cUnit, RegLocation rl,
614 LIR *start, LIR *finish)
615{
buzbeeed3e9302011-09-23 17:34:19 -0700616 DCHECK(!rl.wide);
617 DCHECK(start && start->next);
618 DCHECK(finish);
buzbee67bf8852011-08-17 17:51:35 -0700619 RegisterInfo* p = getRegInfo(cUnit, rl.lowReg);
620 p->defStart = start->next;
621 p->defEnd = finish;
622}
623
624/*
625 * Mark the beginning and end LIR of a def sequence. Note that
626 * on entry start points to the LIR prior to the beginning of the
627 * sequence.
628 */
629extern void oatMarkDefWide(CompilationUnit* cUnit, RegLocation rl,
630 LIR *start, LIR *finish)
631{
buzbeeed3e9302011-09-23 17:34:19 -0700632 DCHECK(rl.wide);
633 DCHECK(start && start->next);
634 DCHECK(finish);
buzbee67bf8852011-08-17 17:51:35 -0700635 RegisterInfo* p = getRegInfo(cUnit, rl.lowReg);
636 oatResetDef(cUnit, rl.highReg); // Only track low of pair
637 p->defStart = start->next;
638 p->defEnd = finish;
639}
640
641extern RegLocation oatWideToNarrow(CompilationUnit* cUnit,
642 RegLocation rl)
643{
buzbeeed3e9302011-09-23 17:34:19 -0700644 DCHECK(rl.wide);
buzbee67bf8852011-08-17 17:51:35 -0700645 if (rl.location == kLocPhysReg) {
646 RegisterInfo* infoLo = getRegInfo(cUnit, rl.lowReg);
647 RegisterInfo* infoHi = getRegInfo(cUnit, rl.highReg);
buzbee0c7f26d2011-09-07 12:28:51 -0700648 if (infoLo->isTemp) {
649 infoLo->pair = false;
650 infoLo->defStart = NULL;
651 infoLo->defEnd = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700652 }
buzbee0c7f26d2011-09-07 12:28:51 -0700653 if (infoHi->isTemp) {
654 infoHi->pair = false;
655 infoHi->defStart = NULL;
656 infoHi->defEnd = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700657 }
buzbee67bf8852011-08-17 17:51:35 -0700658 }
659 rl.wide = false;
660 return rl;
661}
662
663extern void oatResetDefLoc(CompilationUnit* cUnit, RegLocation rl)
664{
buzbeeed3e9302011-09-23 17:34:19 -0700665 DCHECK(!rl.wide);
buzbee67bf8852011-08-17 17:51:35 -0700666 if (!(cUnit->disableOpt & (1 << kSuppressLoads))) {
667 RegisterInfo* p = getRegInfo(cUnit, rl.lowReg);
buzbeeed3e9302011-09-23 17:34:19 -0700668 DCHECK(!p->pair);
buzbee67bf8852011-08-17 17:51:35 -0700669 nullifyRange(cUnit, p->defStart, p->defEnd,
670 p->sReg, rl.sRegLow);
671 }
672 oatResetDef(cUnit, rl.lowReg);
673}
674
675extern void oatResetDefLocWide(CompilationUnit* cUnit, RegLocation rl)
676{
buzbeeed3e9302011-09-23 17:34:19 -0700677 DCHECK(rl.wide);
buzbee67bf8852011-08-17 17:51:35 -0700678 if (!(cUnit->disableOpt & (1 << kSuppressLoads))) {
679 RegisterInfo* p = getRegInfo(cUnit, rl.lowReg);
buzbeeed3e9302011-09-23 17:34:19 -0700680 DCHECK(p->pair);
buzbee67bf8852011-08-17 17:51:35 -0700681 nullifyRange(cUnit, p->defStart, p->defEnd,
682 p->sReg, rl.sRegLow);
683 }
684 oatResetDef(cUnit, rl.lowReg);
685 oatResetDef(cUnit, rl.highReg);
686}
687
688extern void oatResetDefTracking(CompilationUnit* cUnit)
689{
690 int i;
691 for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
692 oatResetDef(cUnit, cUnit->regPool->coreRegs[i].reg);
693 }
694 for (i=0; i< cUnit->regPool->numFPRegs; i++) {
695 oatResetDef(cUnit, cUnit->regPool->FPRegs[i].reg);
696 }
697}
698
699extern void oatClobberAllRegs(CompilationUnit* cUnit)
700{
701 int i;
702 for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
703 oatClobber(cUnit, cUnit->regPool->coreRegs[i].reg);
704 }
705 for (i=0; i< cUnit->regPool->numFPRegs; i++) {
706 oatClobber(cUnit, cUnit->regPool->FPRegs[i].reg);
707 }
708}
709
710/* To be used when explicitly managing register use */
buzbee2e748f32011-08-29 21:02:19 -0700711extern void oatLockCallTemps(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700712{
buzbee2e748f32011-08-29 21:02:19 -0700713 //TODO: Arm specific - move to target dependent code
714 oatLockTemp(cUnit, r0);
715 oatLockTemp(cUnit, r1);
716 oatLockTemp(cUnit, r2);
717 oatLockTemp(cUnit, r3);
buzbee67bf8852011-08-17 17:51:35 -0700718}
719
buzbee0d966cf2011-09-08 17:34:58 -0700720/* To be used when explicitly managing register use */
721extern void oatFreeCallTemps(CompilationUnit* cUnit)
722{
723 //TODO: Arm specific - move to target dependent code
724 oatFreeTemp(cUnit, r0);
725 oatFreeTemp(cUnit, r1);
726 oatFreeTemp(cUnit, r2);
727 oatFreeTemp(cUnit, r3);
728}
729
buzbee67bf8852011-08-17 17:51:35 -0700730// Make sure nothing is live and dirty
buzbeeed3e9302011-09-23 17:34:19 -0700731STATIC void flushAllRegsBody(CompilationUnit* cUnit, RegisterInfo* info,
buzbee67bf8852011-08-17 17:51:35 -0700732 int numRegs)
733{
734 int i;
735 for (i=0; i < numRegs; i++) {
736 if (info[i].live && info[i].dirty) {
737 if (info[i].pair) {
738 oatFlushRegWide(cUnit, info[i].reg, info[i].partner);
739 } else {
740 oatFlushReg(cUnit, info[i].reg);
741 }
742 }
743 }
744}
745
746extern void oatFlushAllRegs(CompilationUnit* cUnit)
747{
748 flushAllRegsBody(cUnit, cUnit->regPool->coreRegs,
749 cUnit->regPool->numCoreRegs);
750 flushAllRegsBody(cUnit, cUnit->regPool->FPRegs,
751 cUnit->regPool->numFPRegs);
752 oatClobberAllRegs(cUnit);
753}
754
755
756//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbeeed3e9302011-09-23 17:34:19 -0700757STATIC bool regClassMatches(int regClass, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700758{
759 if (regClass == kAnyReg) {
760 return true;
761 } else if (regClass == kCoreReg) {
762 return !FPREG(reg);
763 } else {
764 return FPREG(reg);
765 }
766}
767
768extern void oatMarkLive(CompilationUnit* cUnit, int reg, int sReg)
769{
770 RegisterInfo* info = getRegInfo(cUnit, reg);
771 if ((info->reg == reg) && (info->sReg == sReg) && info->live) {
772 return; /* already live */
773 } else if (sReg != INVALID_SREG) {
774 oatClobberSReg(cUnit, sReg);
775 if (info->isTemp) {
776 info->live = true;
777 }
778 } else {
779 /* Can't be live if no associated sReg */
buzbeeed3e9302011-09-23 17:34:19 -0700780 DCHECK(info->isTemp);
buzbee67bf8852011-08-17 17:51:35 -0700781 info->live = false;
782 }
783 info->sReg = sReg;
784}
785
786extern void oatMarkTemp(CompilationUnit* cUnit, int reg)
787{
788 RegisterInfo* info = getRegInfo(cUnit, reg);
789 info->isTemp = true;
790}
791
buzbee9e0f9b02011-08-24 15:32:46 -0700792extern void oatUnmarkTemp(CompilationUnit* cUnit, int reg)
793{
794 RegisterInfo* info = getRegInfo(cUnit, reg);
795 info->isTemp = false;
796}
797
buzbee67bf8852011-08-17 17:51:35 -0700798extern void oatMarkPair(CompilationUnit* cUnit, int lowReg, int highReg)
799{
800 RegisterInfo* infoLo = getRegInfo(cUnit, lowReg);
801 RegisterInfo* infoHi = getRegInfo(cUnit, highReg);
802 infoLo->pair = infoHi->pair = true;
803 infoLo->partner = highReg;
804 infoHi->partner = lowReg;
805}
806
807extern void oatMarkClean(CompilationUnit* cUnit, RegLocation loc)
808{
809 RegisterInfo* info = getRegInfo(cUnit, loc.lowReg);
810 info->dirty = false;
811 if (loc.wide) {
812 info = getRegInfo(cUnit, loc.highReg);
813 info->dirty = false;
814 }
815}
816
817extern void oatMarkDirty(CompilationUnit* cUnit, RegLocation loc)
818{
819 if (loc.home) {
820 // If already home, can't be dirty
821 return;
822 }
823 RegisterInfo* info = getRegInfo(cUnit, loc.lowReg);
824 info->dirty = true;
825 if (loc.wide) {
826 info = getRegInfo(cUnit, loc.highReg);
827 info->dirty = true;
828 }
829}
830
831extern void oatMarkInUse(CompilationUnit* cUnit, int reg)
832{
833 RegisterInfo* info = getRegInfo(cUnit, reg);
834 info->inUse = true;
835}
836
buzbeeed3e9302011-09-23 17:34:19 -0700837STATIC void copyRegInfo(CompilationUnit* cUnit, int newReg, int oldReg)
buzbee67bf8852011-08-17 17:51:35 -0700838{
839 RegisterInfo* newInfo = getRegInfo(cUnit, newReg);
840 RegisterInfo* oldInfo = getRegInfo(cUnit, oldReg);
buzbeeec5adf32011-09-11 15:25:43 -0700841 // Target temp status must not change
842 bool isTemp = newInfo->isTemp;
buzbee67bf8852011-08-17 17:51:35 -0700843 *newInfo = *oldInfo;
buzbeeec5adf32011-09-11 15:25:43 -0700844 // Restore target's temp status
845 newInfo->isTemp = isTemp;
buzbee67bf8852011-08-17 17:51:35 -0700846 newInfo->reg = newReg;
847}
848
849/*
850 * Return an updated location record with current in-register status.
851 * If the value lives in live temps, reflect that fact. No code
852 * is generated. The the live value is part of an older pair,
853 * clobber both low and high.
854 * TUNING: clobbering both is a bit heavy-handed, but the alternative
855 * is a bit complex when dealing with FP regs. Examine code to see
856 * if it's worthwhile trying to be more clever here.
857 */
858
859extern RegLocation oatUpdateLoc(CompilationUnit* cUnit, RegLocation loc)
860{
buzbeeed3e9302011-09-23 17:34:19 -0700861 DCHECK(!loc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700862 if (loc.location == kLocDalvikFrame) {
863 RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg);
864 if (infoLo) {
865 if (infoLo->pair) {
866 oatClobber(cUnit, infoLo->reg);
867 oatClobber(cUnit, infoLo->partner);
868 } else {
869 loc.lowReg = infoLo->reg;
870 loc.location = kLocPhysReg;
871 }
872 }
873 }
874
875 return loc;
876}
877
878/* see comments for updateLoc */
879extern RegLocation oatUpdateLocWide(CompilationUnit* cUnit,
880 RegLocation loc)
881{
buzbeeed3e9302011-09-23 17:34:19 -0700882 DCHECK(loc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700883 if (loc.location == kLocDalvikFrame) {
884 // Are the dalvik regs already live in physical registers?
885 RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg);
886 RegisterInfo* infoHi = allocLive(cUnit,
887 oatSRegHi(loc.sRegLow), kAnyReg);
888 bool match = true;
889 match = match && (infoLo != NULL);
890 match = match && (infoHi != NULL);
891 // Are they both core or both FP?
892 match = match && (FPREG(infoLo->reg) == FPREG(infoHi->reg));
893 // If a pair of floating point singles, are they properly aligned?
894 if (match && FPREG(infoLo->reg)) {
895 match &= ((infoLo->reg & 0x1) == 0);
896 match &= ((infoHi->reg - infoLo->reg) == 1);
897 }
898 // If previously used as a pair, it is the same pair?
899 if (match && (infoLo->pair || infoHi->pair)) {
900 match = (infoLo->pair == infoHi->pair);
901 match &= ((infoLo->reg == infoHi->partner) &&
902 (infoHi->reg == infoLo->partner));
903 }
904 if (match) {
905 // Can reuse - update the register usage info
906 loc.lowReg = infoLo->reg;
907 loc.highReg = infoHi->reg;
908 loc.location = kLocPhysReg;
909 oatMarkPair(cUnit, loc.lowReg, loc.highReg);
buzbeeed3e9302011-09-23 17:34:19 -0700910 DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
buzbee67bf8852011-08-17 17:51:35 -0700911 return loc;
912 }
913 // Can't easily reuse - clobber any overlaps
914 if (infoLo) {
915 oatClobber(cUnit, infoLo->reg);
916 if (infoLo->pair)
917 oatClobber(cUnit, infoLo->partner);
918 }
919 if (infoHi) {
920 oatClobber(cUnit, infoHi->reg);
921 if (infoHi->pair)
922 oatClobber(cUnit, infoHi->partner);
923 }
924 }
925 return loc;
926}
927
buzbeeed3e9302011-09-23 17:34:19 -0700928
929/* For use in cases we don't know (or care) width */
930extern RegLocation oatUpdateRawLoc(CompilationUnit* cUnit,
931 RegLocation loc)
932{
933 if (loc.wide)
934 return oatUpdateLocWide(cUnit, loc);
935 else
936 return oatUpdateLoc(cUnit, loc);
937}
938
939STATIC RegLocation evalLocWide(CompilationUnit* cUnit, RegLocation loc,
buzbee67bf8852011-08-17 17:51:35 -0700940 int regClass, bool update)
941{
buzbeeed3e9302011-09-23 17:34:19 -0700942 DCHECK(loc.wide);
buzbee67bf8852011-08-17 17:51:35 -0700943 int newRegs;
944 int lowReg;
945 int highReg;
946
947 loc = oatUpdateLocWide(cUnit, loc);
948
949 /* If already in registers, we can assume proper form. Right reg class? */
950 if (loc.location == kLocPhysReg) {
buzbeeed3e9302011-09-23 17:34:19 -0700951 DCHECK_EQ(FPREG(loc.lowReg), FPREG(loc.highReg));
952 DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
buzbee67bf8852011-08-17 17:51:35 -0700953 if (!regClassMatches(regClass, loc.lowReg)) {
954 /* Wrong register class. Reallocate and copy */
955 newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass);
956 lowReg = newRegs & 0xff;
957 highReg = (newRegs >> 8) & 0xff;
958 oatRegCopyWide(cUnit, lowReg, highReg, loc.lowReg,
959 loc.highReg);
960 copyRegInfo(cUnit, lowReg, loc.lowReg);
961 copyRegInfo(cUnit, highReg, loc.highReg);
962 oatClobber(cUnit, loc.lowReg);
963 oatClobber(cUnit, loc.highReg);
964 loc.lowReg = lowReg;
965 loc.highReg = highReg;
966 oatMarkPair(cUnit, loc.lowReg, loc.highReg);
buzbeeed3e9302011-09-23 17:34:19 -0700967 DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
buzbee67bf8852011-08-17 17:51:35 -0700968 }
969 return loc;
970 }
971
buzbeeed3e9302011-09-23 17:34:19 -0700972 DCHECK_NE(loc.sRegLow, INVALID_SREG);
973 DCHECK_NE(oatSRegHi(loc.sRegLow), INVALID_SREG);
buzbee67bf8852011-08-17 17:51:35 -0700974
975 newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass);
976 loc.lowReg = newRegs & 0xff;
977 loc.highReg = (newRegs >> 8) & 0xff;
978
979 oatMarkPair(cUnit, loc.lowReg, loc.highReg);
980 if (update) {
981 loc.location = kLocPhysReg;
982 oatMarkLive(cUnit, loc.lowReg, loc.sRegLow);
983 oatMarkLive(cUnit, loc.highReg, oatSRegHi(loc.sRegLow));
984 }
buzbeeed3e9302011-09-23 17:34:19 -0700985 DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
buzbee67bf8852011-08-17 17:51:35 -0700986 return loc;
987}
988
989extern RegLocation oatEvalLoc(CompilationUnit* cUnit, RegLocation loc,
990 int regClass, bool update)
991{
992 int newReg;
993
994 if (loc.wide)
995 return evalLocWide(cUnit, loc, regClass, update);
996
997 loc = oatUpdateLoc(cUnit, loc);
998
999 if (loc.location == kLocPhysReg) {
1000 if (!regClassMatches(regClass, loc.lowReg)) {
1001 /* Wrong register class. Realloc, copy and transfer ownership */
1002 newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass);
1003 oatRegCopy(cUnit, newReg, loc.lowReg);
1004 copyRegInfo(cUnit, newReg, loc.lowReg);
1005 oatClobber(cUnit, loc.lowReg);
1006 loc.lowReg = newReg;
1007 }
1008 return loc;
1009 }
1010
buzbeeed3e9302011-09-23 17:34:19 -07001011 DCHECK_NE(loc.sRegLow, INVALID_SREG);
buzbee67bf8852011-08-17 17:51:35 -07001012
1013 newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass);
1014 loc.lowReg = newReg;
1015
1016 if (update) {
1017 loc.location = kLocPhysReg;
1018 oatMarkLive(cUnit, loc.lowReg, loc.sRegLow);
1019 }
1020 return loc;
1021}
1022
1023extern RegLocation oatGetDest(CompilationUnit* cUnit, MIR* mir, int num)
1024{
buzbeee9a72f62011-09-04 17:59:07 -07001025 RegLocation res = cUnit->regLocation[mir->ssaRep->defs[num]];
buzbeeed3e9302011-09-23 17:34:19 -07001026 DCHECK(!res.wide);
buzbeee9a72f62011-09-04 17:59:07 -07001027 return res;
buzbee67bf8852011-08-17 17:51:35 -07001028}
1029extern RegLocation oatGetSrc(CompilationUnit* cUnit, MIR* mir, int num)
1030{
buzbeee9a72f62011-09-04 17:59:07 -07001031 RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]];
buzbeeed3e9302011-09-23 17:34:19 -07001032 DCHECK(!res.wide);
buzbeee9a72f62011-09-04 17:59:07 -07001033 return res;
1034}
1035extern RegLocation oatGetRawSrc(CompilationUnit* cUnit, MIR* mir, int num)
1036{
1037 RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]];
1038 return res;
buzbee67bf8852011-08-17 17:51:35 -07001039}
1040extern RegLocation oatGetDestWide(CompilationUnit* cUnit, MIR* mir,
1041 int low, int high)
1042{
buzbeee9a72f62011-09-04 17:59:07 -07001043 RegLocation res = cUnit->regLocation[mir->ssaRep->defs[low]];
buzbeeed3e9302011-09-23 17:34:19 -07001044 DCHECK(res.wide);
buzbeee9a72f62011-09-04 17:59:07 -07001045 return res;
buzbee67bf8852011-08-17 17:51:35 -07001046}
1047
1048extern RegLocation oatGetSrcWide(CompilationUnit* cUnit, MIR* mir,
1049 int low, int high)
1050{
buzbeee9a72f62011-09-04 17:59:07 -07001051 RegLocation res = cUnit->regLocation[mir->ssaRep->uses[low]];
buzbeeed3e9302011-09-23 17:34:19 -07001052 DCHECK(res.wide);
buzbeee9a72f62011-09-04 17:59:07 -07001053 return res;
buzbee67bf8852011-08-17 17:51:35 -07001054}