blob: afd49768d0e9120d7ba82d228ecdb7873934f141 [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
buzbeeeaf09bc2012-11-15 14:51:41 -080019#include "codegen_util.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080020#include "compiler/compiler_ir.h"
21#include "compiler/compiler_utility.h"
22#include "compiler/dataflow.h"
23#include "ralloc_util.h"
buzbee67bf8852011-08-17 17:51:35 -070024
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
buzbee02031b12012-11-23 09:41:35 -080027static const RegLocation bad_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
28 INVALID_REG, INVALID_REG, INVALID_SREG,
29 INVALID_SREG};
30
buzbee67bf8852011-08-17 17:51:35 -070031/*
32 * Free all allocated temps in the temp pools. Note that this does
33 * not affect the "liveness" of a temp register, which will stay
34 * live until it is either explicitly killed or reallocated.
35 */
buzbeefa57c472012-11-21 12:06:18 -080036void ResetRegPool(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -070037{
Bill Buzbeea114add2012-05-03 15:00:40 -070038 int i;
buzbeefa57c472012-11-21 12:06:18 -080039 for (i=0; i < cu->reg_pool->num_core_regs; i++) {
40 if (cu->reg_pool->core_regs[i].is_temp)
41 cu->reg_pool->core_regs[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070042 }
buzbeefa57c472012-11-21 12:06:18 -080043 for (i=0; i < cu->reg_pool->num_fp_regs; i++) {
44 if (cu->reg_pool->FPRegs[i].is_temp)
45 cu->reg_pool->FPRegs[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070046 }
buzbee67bf8852011-08-17 17:51:35 -070047}
48
buzbeee3acd072012-02-25 17:03:10 -080049 /*
50 * Set up temp & preserved register pools specialized by target.
buzbeefa57c472012-11-21 12:06:18 -080051 * Note: num_regs may be zero.
buzbeee3acd072012-02-25 17:03:10 -080052 */
buzbeefa57c472012-11-21 12:06:18 -080053void CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num)
buzbee67bf8852011-08-17 17:51:35 -070054{
Bill Buzbeea114add2012-05-03 15:00:40 -070055 int i;
56 for (i=0; i < num; i++) {
buzbeefa57c472012-11-21 12:06:18 -080057 regs[i].reg = reg_nums[i];
58 regs[i].in_use = false;
59 regs[i].is_temp = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070060 regs[i].pair = false;
61 regs[i].live = false;
62 regs[i].dirty = false;
buzbeefa57c472012-11-21 12:06:18 -080063 regs[i].s_reg = INVALID_SREG;
Bill Buzbeea114add2012-05-03 15:00:40 -070064 }
buzbee67bf8852011-08-17 17:51:35 -070065}
66
buzbeefa57c472012-11-21 12:06:18 -080067static void DumpRegPool(RegisterInfo* p, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -070068{
Bill Buzbeea114add2012-05-03 15:00:40 -070069 LOG(INFO) << "================================================";
buzbeefa57c472012-11-21 12:06:18 -080070 for (int i = 0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -070071 LOG(INFO) << StringPrintf(
72 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x",
buzbeefa57c472012-11-21 12:06:18 -080073 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
74 p[i].live, p[i].dirty, p[i].s_reg, reinterpret_cast<uintptr_t>(p[i].def_start),
75 reinterpret_cast<uintptr_t>(p[i].def_end));
Bill Buzbeea114add2012-05-03 15:00:40 -070076 }
77 LOG(INFO) << "================================================";
buzbee67bf8852011-08-17 17:51:35 -070078}
79
buzbeefa57c472012-11-21 12:06:18 -080080void DumpCoreRegPool(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -070081{
buzbeefa57c472012-11-21 12:06:18 -080082 DumpRegPool(cu->reg_pool->core_regs, cu->reg_pool->num_core_regs);
buzbee6181f792011-09-29 11:14:04 -070083}
84
buzbeefa57c472012-11-21 12:06:18 -080085void DumpFpRegPool(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -070086{
buzbeefa57c472012-11-21 12:06:18 -080087 DumpRegPool(cu->reg_pool->FPRegs, cu->reg_pool->num_fp_regs);
buzbee6181f792011-09-29 11:14:04 -070088}
89
buzbee67bf8852011-08-17 17:51:35 -070090/* Mark a temp register as dead. Does not affect allocation state. */
buzbeefa57c472012-11-21 12:06:18 -080091static void ClobberBody(CompilationUnit *cu, RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -070092{
buzbeefa57c472012-11-21 12:06:18 -080093 if (p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -070094 DCHECK(!(p->live && p->dirty)) << "Live & dirty temp in clobber";
95 p->live = false;
buzbeefa57c472012-11-21 12:06:18 -080096 p->s_reg = INVALID_SREG;
97 p->def_start = NULL;
98 p->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -070099 if (p->pair) {
100 p->pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800101 Clobber(cu, p->partner);
buzbee67bf8852011-08-17 17:51:35 -0700102 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 }
buzbee67bf8852011-08-17 17:51:35 -0700104}
105
buzbee5abfa3e2012-01-31 17:01:43 -0800106/* Mark a temp register as dead. Does not affect allocation state. */
buzbeefa57c472012-11-21 12:06:18 -0800107void Clobber(CompilationUnit* cu, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800108{
buzbee02031b12012-11-23 09:41:35 -0800109 Codegen* cg = cu->cg.get();
110 ClobberBody(cu, cg->GetRegInfo(cu, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800111}
112
buzbeefa57c472012-11-21 12:06:18 -0800113static void ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700114{
Bill Buzbeea114add2012-05-03 15:00:40 -0700115 int i;
buzbeefa57c472012-11-21 12:06:18 -0800116 for (i=0; i< num_regs; i++) {
117 if (p[i].s_reg == s_reg) {
118 if (p[i].is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 p[i].live = false;
120 }
buzbeefa57c472012-11-21 12:06:18 -0800121 p[i].def_start = NULL;
122 p[i].def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700123 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700124 }
buzbee67bf8852011-08-17 17:51:35 -0700125}
126
buzbee078fa452012-12-03 15:51:33 -0800127/*
128 * Break the association between a Dalvik vreg and a physical temp register of either register
129 * class.
130 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
131 * in the register utilities, is is also used by code gen routines to work around a deficiency in
132 * local register allocation, which fails to distinguish between the "in" and "out" identities
133 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
134 * is used both as the source and destination register of an operation in which the type
135 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
136 * addressed.
137 */
buzbeefa57c472012-11-21 12:06:18 -0800138void ClobberSReg(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700139{
buzbee3d661942012-03-14 17:37:27 -0700140#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 /* Reset live temp tracking sanity checker */
buzbeefa57c472012-11-21 12:06:18 -0800142 if (s_reg == cu->live_sreg) {
143 cu->live_sreg = INVALID_SREG;
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 }
buzbee3d661942012-03-14 17:37:27 -0700145#endif
buzbeefa57c472012-11-21 12:06:18 -0800146 ClobberSRegBody(cu->reg_pool->core_regs, cu->reg_pool->num_core_regs, s_reg);
147 ClobberSRegBody(cu->reg_pool->FPRegs, cu->reg_pool->num_fp_regs, s_reg);
buzbee67bf8852011-08-17 17:51:35 -0700148}
149
buzbee9c044ce2012-03-18 13:24:07 -0700150/*
151 * SSA names associated with the initial definitions of Dalvik
152 * registers are the same as the Dalvik register number (and
buzbeefa57c472012-11-21 12:06:18 -0800153 * thus take the same position in the promotion_map. However,
buzbee9c044ce2012-03-18 13:24:07 -0700154 * the special Method* and compiler temp resisters use negative
buzbeefa57c472012-11-21 12:06:18 -0800155 * v_reg numbers to distinguish them and can have an arbitrary
buzbee9c044ce2012-03-18 13:24:07 -0700156 * ssa name (above the last original Dalvik register). This function
buzbeefa57c472012-11-21 12:06:18 -0800157 * maps SSA names to positions in the promotion_map array.
buzbee9c044ce2012-03-18 13:24:07 -0700158 */
buzbee5f61f672012-11-28 17:22:17 -0800159int SRegToPMap(CompilationUnit* cu, int s_reg)
buzbeee1965672012-03-11 18:39:19 -0700160{
buzbeefa57c472012-11-21 12:06:18 -0800161 DCHECK_LT(s_reg, cu->num_ssa_regs);
162 DCHECK_GE(s_reg, 0);
163 int v_reg = SRegToVReg(cu, s_reg);
164 if (v_reg >= 0) {
165 DCHECK_LT(v_reg, cu->num_dalvik_registers);
166 return v_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 } else {
buzbeefa57c472012-11-21 12:06:18 -0800168 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
169 DCHECK_LE(pos, cu->num_compiler_temps);
170 return cu->num_dalvik_registers + pos;
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 }
buzbeee1965672012-03-11 18:39:19 -0700172}
173
buzbeefa57c472012-11-21 12:06:18 -0800174void RecordCorePromotion(CompilationUnit* cu, int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700175{
buzbee02031b12012-11-23 09:41:35 -0800176 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800177 int p_map_idx = SRegToPMap(cu, s_reg);
178 int v_reg = SRegToVReg(cu, s_reg);
buzbee02031b12012-11-23 09:41:35 -0800179 cg->GetRegInfo(cu, reg)->in_use = true;
buzbeefa57c472012-11-21 12:06:18 -0800180 cu->core_spill_mask |= (1 << reg);
buzbeeca7a5e42012-08-20 11:12:18 -0700181 // Include reg for later sort
buzbeefa57c472012-11-21 12:06:18 -0800182 cu->core_vmap_table.push_back(reg << VREG_NUM_WIDTH |
183 (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
184 cu->num_core_spills++;
185 cu->promotion_map[p_map_idx].core_location = kLocPhysReg;
186 cu->promotion_map[p_map_idx].core_reg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700187}
188
buzbee67bf8852011-08-17 17:51:35 -0700189/* Reserve a callee-save register. Return -1 if none available */
buzbeefa57c472012-11-21 12:06:18 -0800190static int AllocPreservedCoreReg(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700191{
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800193 RegisterInfo* core_regs = cu->reg_pool->core_regs;
194 for (int i = 0; i < cu->reg_pool->num_core_regs; i++) {
195 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
196 res = core_regs[i].reg;
197 RecordCorePromotion(cu, res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 break;
buzbee67bf8852011-08-17 17:51:35 -0700199 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 }
201 return res;
buzbee67bf8852011-08-17 17:51:35 -0700202}
203
buzbeefa57c472012-11-21 12:06:18 -0800204void RecordFpPromotion(CompilationUnit* cu, int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700205{
buzbee02031b12012-11-23 09:41:35 -0800206 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800207 int p_map_idx = SRegToPMap(cu, s_reg);
208 int v_reg = SRegToVReg(cu, s_reg);
buzbee02031b12012-11-23 09:41:35 -0800209 cg->GetRegInfo(cu, reg)->in_use = true;
210 cg->MarkPreservedSingle(cu, v_reg, reg);
buzbeefa57c472012-11-21 12:06:18 -0800211 cu->promotion_map[p_map_idx].fp_location = kLocPhysReg;
212 cu->promotion_map[p_map_idx].FpReg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700213}
214
buzbee67bf8852011-08-17 17:51:35 -0700215/*
216 * Reserve a callee-save fp single register. Try to fullfill request for
217 * even/odd allocation, but go ahead and allocate anything if not
218 * available. If nothing's available, return -1.
219 */
buzbeefa57c472012-11-21 12:06:18 -0800220static int AllocPreservedSingle(CompilationUnit* cu, int s_reg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700221{
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800223 RegisterInfo* FPRegs = cu->reg_pool->FPRegs;
224 for (int i = 0; i < cu->reg_pool->num_fp_regs; i++) {
225 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 ((FPRegs[i].reg & 0x1) == 0) == even) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800228 RecordFpPromotion(cu, res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 break;
buzbee67bf8852011-08-17 17:51:35 -0700230 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 }
232 return res;
buzbee67bf8852011-08-17 17:51:35 -0700233}
234
235/*
236 * Somewhat messy code here. We want to allocate a pair of contiguous
237 * physical single-precision floating point registers starting with
buzbeefa57c472012-11-21 12:06:18 -0800238 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
buzbee67bf8852011-08-17 17:51:35 -0700239 * has already been allocated - try to fit if possible. Fail to
240 * allocate if we can't meet the requirements for the pair of
buzbeefa57c472012-11-21 12:06:18 -0800241 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
buzbee67bf8852011-08-17 17:51:35 -0700242 */
buzbeefa57c472012-11-21 12:06:18 -0800243static int AllocPreservedDouble(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700244{
buzbee02031b12012-11-23 09:41:35 -0800245 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 int res = -1; // Assume failure
buzbeefa57c472012-11-21 12:06:18 -0800247 int v_reg = SRegToVReg(cu, s_reg);
248 int p_map_idx = SRegToPMap(cu, s_reg);
249 if (cu->promotion_map[p_map_idx+1].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 // Upper reg is already allocated. Can we fit?
buzbeefa57c472012-11-21 12:06:18 -0800251 int high_reg = cu->promotion_map[p_map_idx+1].FpReg;
252 if ((high_reg & 1) == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 // High reg is even - fail.
254 return res;
255 }
256 // Is the low reg of the pair free?
buzbee02031b12012-11-23 09:41:35 -0800257 RegisterInfo* p = cg->GetRegInfo(cu, high_reg-1);
buzbeefa57c472012-11-21 12:06:18 -0800258 if (p->in_use || p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 // Already allocated or not preserved - fail.
260 return res;
261 }
262 // OK - good to go.
263 res = p->reg;
buzbeefa57c472012-11-21 12:06:18 -0800264 p->in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 DCHECK_EQ((res & 1), 0);
buzbee02031b12012-11-23 09:41:35 -0800266 cg->MarkPreservedSingle(cu, v_reg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 } else {
buzbeefa57c472012-11-21 12:06:18 -0800268 RegisterInfo* FPRegs = cu->reg_pool->FPRegs;
269 for (int i = 0; i < cu->reg_pool->num_fp_regs; i++) {
270 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 ((FPRegs[i].reg & 0x1) == 0x0) &&
buzbeefa57c472012-11-21 12:06:18 -0800272 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
274 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
275 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800276 FPRegs[i].in_use = true;
buzbee02031b12012-11-23 09:41:35 -0800277 cg->MarkPreservedSingle(cu, v_reg, res);
buzbeefa57c472012-11-21 12:06:18 -0800278 FPRegs[i+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
buzbee02031b12012-11-23 09:41:35 -0800280 cg->MarkPreservedSingle(cu, v_reg+1, res+1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 break;
282 }
buzbee67bf8852011-08-17 17:51:35 -0700283 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 }
285 if (res != -1) {
buzbeefa57c472012-11-21 12:06:18 -0800286 cu->promotion_map[p_map_idx].fp_location = kLocPhysReg;
287 cu->promotion_map[p_map_idx].FpReg = res;
288 cu->promotion_map[p_map_idx+1].fp_location = kLocPhysReg;
289 cu->promotion_map[p_map_idx+1].FpReg = res + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700290 }
291 return res;
buzbee67bf8852011-08-17 17:51:35 -0700292}
293
294
295/*
296 * Reserve a callee-save fp register. If this register can be used
297 * as the first of a double, attempt to allocate an even pair of fp
298 * single regs (but if can't still attempt to allocate a single, preferring
299 * first to allocate an odd register.
300 */
buzbeefa57c472012-11-21 12:06:18 -0800301static int AllocPreservedFPReg(CompilationUnit* cu, int s_reg, bool double_start)
buzbee67bf8852011-08-17 17:51:35 -0700302{
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800304 if (double_start) {
305 res = AllocPreservedDouble(cu, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 }
307 if (res == -1) {
buzbeefa57c472012-11-21 12:06:18 -0800308 res = AllocPreservedSingle(cu, s_reg, false /* try odd # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 }
310 if (res == -1)
buzbeefa57c472012-11-21 12:06:18 -0800311 res = AllocPreservedSingle(cu, s_reg, true /* try even # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 return res;
buzbee67bf8852011-08-17 17:51:35 -0700313}
314
buzbeefa57c472012-11-21 12:06:18 -0800315static int AllocTempBody(CompilationUnit* cu, RegisterInfo* p, int num_regs, int* next_temp,
buzbeeaad94382012-11-21 07:40:50 -0800316 bool required)
buzbee67bf8852011-08-17 17:51:35 -0700317{
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 int i;
buzbeefa57c472012-11-21 12:06:18 -0800319 int next = *next_temp;
320 for (i=0; i< num_regs; i++) {
321 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700322 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800323 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
324 Clobber(cu, p[next].reg);
325 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700326 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800327 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700328 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700329 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 next++;
331 }
buzbeefa57c472012-11-21 12:06:18 -0800332 next = *next_temp;
333 for (i=0; i< num_regs; i++) {
334 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700335 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800336 if (p[next].is_temp && !p[next].in_use) {
337 Clobber(cu, p[next].reg);
338 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800340 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700342 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 next++;
344 }
345 if (required) {
buzbeefa57c472012-11-21 12:06:18 -0800346 CodegenDump(cu);
347 DumpRegPool(cu->reg_pool->core_regs,
348 cu->reg_pool->num_core_regs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 LOG(FATAL) << "No free temp registers";
350 }
351 return -1; // No register available
buzbee67bf8852011-08-17 17:51:35 -0700352}
353
354//REDO: too many assumptions.
buzbeefa57c472012-11-21 12:06:18 -0800355int AllocTempDouble(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700356{
buzbeefa57c472012-11-21 12:06:18 -0800357 RegisterInfo* p = cu->reg_pool->FPRegs;
358 int num_regs = cu->reg_pool->num_fp_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700359 /* Start looking at an even reg */
buzbeefa57c472012-11-21 12:06:18 -0800360 int next = cu->reg_pool->next_fp_reg & ~0x1;
buzbee67bf8852011-08-17 17:51:35 -0700361
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 // First try to avoid allocating live registers
buzbeefa57c472012-11-21 12:06:18 -0800363 for (int i=0; i < num_regs; i+=2) {
364 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800366 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
367 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
368 Clobber(cu, p[next].reg);
369 Clobber(cu, p[next+1].reg);
370 p[next].in_use = true;
371 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
373 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800374 cu->reg_pool->next_fp_reg = next + 2;
375 if (cu->reg_pool->next_fp_reg >= num_regs) {
376 cu->reg_pool->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 }
378 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700379 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 next += 2;
381 }
buzbeefa57c472012-11-21 12:06:18 -0800382 next = cu->reg_pool->next_fp_reg & ~0x1;
buzbeea50638b2011-11-02 15:15:06 -0700383
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 // No choice - find a pair and kill it.
buzbeefa57c472012-11-21 12:06:18 -0800385 for (int i=0; i < num_regs; i+=2) {
386 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800388 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
389 !p[next+1].in_use) {
390 Clobber(cu, p[next].reg);
391 Clobber(cu, p[next+1].reg);
392 p[next].in_use = true;
393 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
395 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800396 cu->reg_pool->next_fp_reg = next + 2;
397 if (cu->reg_pool->next_fp_reg >= num_regs) {
398 cu->reg_pool->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700399 }
400 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700401 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 next += 2;
403 }
404 LOG(FATAL) << "No free temp registers (pair)";
405 return -1;
buzbee67bf8852011-08-17 17:51:35 -0700406}
407
408/* Return a temp if one is available, -1 otherwise */
buzbeefa57c472012-11-21 12:06:18 -0800409int AllocFreeTemp(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700410{
buzbeefa57c472012-11-21 12:06:18 -0800411 return AllocTempBody(cu, cu->reg_pool->core_regs,
412 cu->reg_pool->num_core_regs,
413 &cu->reg_pool->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700414}
415
buzbeefa57c472012-11-21 12:06:18 -0800416int AllocTemp(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700417{
buzbeefa57c472012-11-21 12:06:18 -0800418 return AllocTempBody(cu, cu->reg_pool->core_regs,
419 cu->reg_pool->num_core_regs,
420 &cu->reg_pool->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700421}
422
buzbeefa57c472012-11-21 12:06:18 -0800423int AllocTempFloat(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700424{
buzbeefa57c472012-11-21 12:06:18 -0800425 return AllocTempBody(cu, cu->reg_pool->FPRegs,
426 cu->reg_pool->num_fp_regs,
427 &cu->reg_pool->next_fp_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700428}
429
buzbeefa57c472012-11-21 12:06:18 -0800430static RegisterInfo* AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700431{
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 int i;
buzbeefa57c472012-11-21 12:06:18 -0800433 if (s_reg == -1)
buzbee67bf8852011-08-17 17:51:35 -0700434 return NULL;
buzbeefa57c472012-11-21 12:06:18 -0800435 for (i=0; i < num_regs; i++) {
436 if (p[i].live && (p[i].s_reg == s_reg)) {
437 if (p[i].is_temp)
438 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 return &p[i];
440 }
441 }
442 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700443}
444
buzbeefa57c472012-11-21 12:06:18 -0800445RegisterInfo* AllocLive(CompilationUnit* cu, int s_reg, int reg_class)
buzbee67bf8852011-08-17 17:51:35 -0700446{
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 RegisterInfo* res = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800448 switch (reg_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 case kAnyReg:
buzbeefa57c472012-11-21 12:06:18 -0800450 res = AllocLiveBody(cu->reg_pool->FPRegs,
451 cu->reg_pool->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 if (res)
453 break;
454 /* Intentional fallthrough */
455 case kCoreReg:
buzbeefa57c472012-11-21 12:06:18 -0800456 res = AllocLiveBody(cu->reg_pool->core_regs,
457 cu->reg_pool->num_core_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 break;
459 case kFPReg:
buzbeefa57c472012-11-21 12:06:18 -0800460 res = AllocLiveBody(cu->reg_pool->FPRegs,
461 cu->reg_pool->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 break;
463 default:
464 LOG(FATAL) << "Invalid register type";
465 }
466 return res;
buzbee67bf8852011-08-17 17:51:35 -0700467}
468
buzbeefa57c472012-11-21 12:06:18 -0800469void FreeTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700470{
buzbeefa57c472012-11-21 12:06:18 -0800471 RegisterInfo* p = cu->reg_pool->core_regs;
472 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700473 int i;
buzbeefa57c472012-11-21 12:06:18 -0800474 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800476 if (p[i].is_temp) {
477 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 }
479 p[i].pair = false;
480 return;
buzbee67bf8852011-08-17 17:51:35 -0700481 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 }
buzbeefa57c472012-11-21 12:06:18 -0800483 p = cu->reg_pool->FPRegs;
484 num_regs = cu->reg_pool->num_fp_regs;
485 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800487 if (p[i].is_temp) {
488 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 }
490 p[i].pair = false;
491 return;
buzbee67bf8852011-08-17 17:51:35 -0700492 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 }
494 LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700495}
496
buzbeefa57c472012-11-21 12:06:18 -0800497RegisterInfo* IsLive(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700498{
buzbeefa57c472012-11-21 12:06:18 -0800499 RegisterInfo* p = cu->reg_pool->core_regs;
500 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 int i;
buzbeefa57c472012-11-21 12:06:18 -0800502 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 if (p[i].reg == reg) {
504 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700505 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 }
buzbeefa57c472012-11-21 12:06:18 -0800507 p = cu->reg_pool->FPRegs;
508 num_regs = cu->reg_pool->num_fp_regs;
509 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 if (p[i].reg == reg) {
511 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700512 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 }
514 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700515}
516
buzbeefa57c472012-11-21 12:06:18 -0800517RegisterInfo* IsTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700518{
buzbee02031b12012-11-23 09:41:35 -0800519 Codegen* cg = cu->cg.get();
520 RegisterInfo* p = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800521 return (p->is_temp) ? p : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700522}
523
buzbeefa57c472012-11-21 12:06:18 -0800524RegisterInfo* IsPromoted(CompilationUnit* cu, int reg)
buzbeeb29e4d12011-09-26 15:05:48 -0700525{
buzbee02031b12012-11-23 09:41:35 -0800526 Codegen* cg = cu->cg.get();
527 RegisterInfo* p = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800528 return (p->is_temp) ? NULL : p;
buzbeeb29e4d12011-09-26 15:05:48 -0700529}
530
buzbeefa57c472012-11-21 12:06:18 -0800531bool IsDirty(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700532{
buzbee02031b12012-11-23 09:41:35 -0800533 Codegen* cg = cu->cg.get();
534 RegisterInfo* p = cg->GetRegInfo(cu, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 return p->dirty;
buzbee67bf8852011-08-17 17:51:35 -0700536}
537
538/*
buzbee52a77fc2012-11-20 19:50:46 -0800539 * Similar to AllocTemp(), but forces the allocation of a specific
buzbee67bf8852011-08-17 17:51:35 -0700540 * register. No check is made to see if the register was previously
541 * allocated. Use with caution.
542 */
buzbeefa57c472012-11-21 12:06:18 -0800543void LockTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700544{
buzbeefa57c472012-11-21 12:06:18 -0800545 RegisterInfo* p = cu->reg_pool->core_regs;
546 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700547 int i;
buzbeefa57c472012-11-21 12:06:18 -0800548 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800550 DCHECK(p[i].is_temp);
551 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700552 p[i].live = false;
553 return;
buzbee67bf8852011-08-17 17:51:35 -0700554 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700555 }
buzbeefa57c472012-11-21 12:06:18 -0800556 p = cu->reg_pool->FPRegs;
557 num_regs = cu->reg_pool->num_fp_regs;
558 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800560 DCHECK(p[i].is_temp);
561 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 p[i].live = false;
563 return;
buzbee67bf8852011-08-17 17:51:35 -0700564 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 }
566 LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700567}
568
buzbeeaad94382012-11-21 07:40:50 -0800569static void ResetDefBody(RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -0700570{
buzbeefa57c472012-11-21 12:06:18 -0800571 p->def_start = NULL;
572 p->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700573}
574
buzbeefa57c472012-11-21 12:06:18 -0800575void ResetDef(CompilationUnit* cu, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800576{
buzbee02031b12012-11-23 09:41:35 -0800577 Codegen* cg = cu->cg.get();
578 ResetDefBody(cg->GetRegInfo(cu, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800579}
580
buzbeefa57c472012-11-21 12:06:18 -0800581static void NullifyRange(CompilationUnit* cu, LIR *start, LIR *finish, int s_reg1, int s_reg2)
buzbee67bf8852011-08-17 17:51:35 -0700582{
Bill Buzbeea114add2012-05-03 15:00:40 -0700583 if (start && finish) {
584 LIR *p;
buzbeefa57c472012-11-21 12:06:18 -0800585 DCHECK_EQ(s_reg1, s_reg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700586 for (p = start; ;p = p->next) {
buzbee52a77fc2012-11-20 19:50:46 -0800587 NopLIR(p);
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 if (p == finish)
589 break;
buzbee67bf8852011-08-17 17:51:35 -0700590 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 }
buzbee67bf8852011-08-17 17:51:35 -0700592}
593
594/*
595 * Mark the beginning and end LIR of a def sequence. Note that
596 * on entry start points to the LIR prior to the beginning of the
597 * sequence.
598 */
buzbeefa57c472012-11-21 12:06:18 -0800599void MarkDef(CompilationUnit* cu, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700601{
Bill Buzbeea114add2012-05-03 15:00:40 -0700602 DCHECK(!rl.wide);
603 DCHECK(start && start->next);
604 DCHECK(finish);
buzbee02031b12012-11-23 09:41:35 -0800605 Codegen* cg = cu->cg.get();
606 RegisterInfo* p = cg->GetRegInfo(cu, rl.low_reg);
buzbeefa57c472012-11-21 12:06:18 -0800607 p->def_start = start->next;
608 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700609}
610
611/*
612 * Mark the beginning and end LIR of a def sequence. Note that
613 * on entry start points to the LIR prior to the beginning of the
614 * sequence.
615 */
buzbeefa57c472012-11-21 12:06:18 -0800616void MarkDefWide(CompilationUnit* cu, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700618{
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 DCHECK(rl.wide);
620 DCHECK(start && start->next);
621 DCHECK(finish);
buzbee02031b12012-11-23 09:41:35 -0800622 Codegen* cg = cu->cg.get();
623 RegisterInfo* p = cg->GetRegInfo(cu, rl.low_reg);
buzbeefa57c472012-11-21 12:06:18 -0800624 ResetDef(cu, rl.high_reg); // Only track low of pair
625 p->def_start = start->next;
626 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700627}
628
buzbeefa57c472012-11-21 12:06:18 -0800629RegLocation WideToNarrow(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700630{
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 DCHECK(rl.wide);
buzbee02031b12012-11-23 09:41:35 -0800632 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 if (rl.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800634 RegisterInfo* info_lo = cg->GetRegInfo(cu, rl.low_reg);
635 RegisterInfo* info_hi = cg->GetRegInfo(cu, rl.high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800636 if (info_lo->is_temp) {
637 info_lo->pair = false;
638 info_lo->def_start = NULL;
639 info_lo->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700640 }
buzbeefa57c472012-11-21 12:06:18 -0800641 if (info_hi->is_temp) {
642 info_hi->pair = false;
643 info_hi->def_start = NULL;
644 info_hi->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 }
646 }
647 rl.wide = false;
648 return rl;
buzbee67bf8852011-08-17 17:51:35 -0700649}
650
buzbeefa57c472012-11-21 12:06:18 -0800651void ResetDefLoc(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700652{
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 DCHECK(!rl.wide);
buzbeefa57c472012-11-21 12:06:18 -0800654 RegisterInfo* p = IsTemp(cu, rl.low_reg);
655 if (p && !(cu->disable_opt & (1 << kSuppressLoads))) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 DCHECK(!p->pair);
buzbeefa57c472012-11-21 12:06:18 -0800657 NullifyRange(cu, p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 }
buzbeefa57c472012-11-21 12:06:18 -0800659 ResetDef(cu, rl.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700660}
661
buzbeefa57c472012-11-21 12:06:18 -0800662void ResetDefLocWide(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700663{
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 DCHECK(rl.wide);
buzbeefa57c472012-11-21 12:06:18 -0800665 RegisterInfo* p_low = IsTemp(cu, rl.low_reg);
666 RegisterInfo* p_high = IsTemp(cu, rl.high_reg);
667 if (p_low && !(cu->disable_opt & (1 << kSuppressLoads))) {
668 DCHECK(p_low->pair);
669 NullifyRange(cu, p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 }
buzbeefa57c472012-11-21 12:06:18 -0800671 if (p_high && !(cu->disable_opt & (1 << kSuppressLoads))) {
672 DCHECK(p_high->pair);
Bill Buzbeea114add2012-05-03 15:00:40 -0700673 }
buzbeefa57c472012-11-21 12:06:18 -0800674 ResetDef(cu, rl.low_reg);
675 ResetDef(cu, rl.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700676}
677
buzbeefa57c472012-11-21 12:06:18 -0800678void ResetDefTracking(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700679{
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 int i;
buzbeefa57c472012-11-21 12:06:18 -0800681 for (i=0; i< cu->reg_pool->num_core_regs; i++) {
682 ResetDefBody(&cu->reg_pool->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 }
buzbeefa57c472012-11-21 12:06:18 -0800684 for (i=0; i< cu->reg_pool->num_fp_regs; i++) {
685 ResetDefBody(&cu->reg_pool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700686 }
buzbee67bf8852011-08-17 17:51:35 -0700687}
688
buzbeefa57c472012-11-21 12:06:18 -0800689void ClobberAllRegs(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700690{
Bill Buzbeea114add2012-05-03 15:00:40 -0700691 int i;
buzbeefa57c472012-11-21 12:06:18 -0800692 for (i=0; i< cu->reg_pool->num_core_regs; i++) {
693 ClobberBody(cu, &cu->reg_pool->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 }
buzbeefa57c472012-11-21 12:06:18 -0800695 for (i=0; i< cu->reg_pool->num_fp_regs; i++) {
696 ClobberBody(cu, &cu->reg_pool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 }
buzbee67bf8852011-08-17 17:51:35 -0700698}
699
buzbee67bf8852011-08-17 17:51:35 -0700700// Make sure nothing is live and dirty
buzbeefa57c472012-11-21 12:06:18 -0800701static void FlushAllRegsBody(CompilationUnit* cu, RegisterInfo* info, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -0700702{
buzbee02031b12012-11-23 09:41:35 -0800703 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700704 int i;
buzbeefa57c472012-11-21 12:06:18 -0800705 for (i=0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700706 if (info[i].live && info[i].dirty) {
707 if (info[i].pair) {
buzbee02031b12012-11-23 09:41:35 -0800708 cg->FlushRegWide(cu, info[i].reg, info[i].partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 } else {
buzbee02031b12012-11-23 09:41:35 -0800710 cg->FlushReg(cu, info[i].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700711 }
buzbee67bf8852011-08-17 17:51:35 -0700712 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700713 }
buzbee67bf8852011-08-17 17:51:35 -0700714}
715
buzbeefa57c472012-11-21 12:06:18 -0800716void FlushAllRegs(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700717{
buzbeefa57c472012-11-21 12:06:18 -0800718 FlushAllRegsBody(cu, cu->reg_pool->core_regs,
719 cu->reg_pool->num_core_regs);
720 FlushAllRegsBody(cu, cu->reg_pool->FPRegs,
721 cu->reg_pool->num_fp_regs);
722 ClobberAllRegs(cu);
buzbee67bf8852011-08-17 17:51:35 -0700723}
724
725
726//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbee02031b12012-11-23 09:41:35 -0800727static bool RegClassMatches(CompilationUnit* cu, int reg_class, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700728{
buzbee02031b12012-11-23 09:41:35 -0800729 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800730 if (reg_class == kAnyReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 return true;
buzbeefa57c472012-11-21 12:06:18 -0800732 } else if (reg_class == kCoreReg) {
buzbee02031b12012-11-23 09:41:35 -0800733 return !cg->IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 } else {
buzbee02031b12012-11-23 09:41:35 -0800735 return cg->IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 }
buzbee67bf8852011-08-17 17:51:35 -0700737}
738
buzbeefa57c472012-11-21 12:06:18 -0800739void MarkLive(CompilationUnit* cu, int reg, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700740{
buzbee02031b12012-11-23 09:41:35 -0800741 Codegen* cg = cu->cg.get();
742 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800743 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700744 return; /* already live */
buzbeefa57c472012-11-21 12:06:18 -0800745 } else if (s_reg != INVALID_SREG) {
746 ClobberSReg(cu, s_reg);
747 if (info->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700748 info->live = true;
buzbee67bf8852011-08-17 17:51:35 -0700749 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700750 } else {
buzbeefa57c472012-11-21 12:06:18 -0800751 /* Can't be live if no associated s_reg */
752 DCHECK(info->is_temp);
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 info->live = false;
754 }
buzbeefa57c472012-11-21 12:06:18 -0800755 info->s_reg = s_reg;
buzbee67bf8852011-08-17 17:51:35 -0700756}
757
buzbeefa57c472012-11-21 12:06:18 -0800758void MarkTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700759{
buzbee02031b12012-11-23 09:41:35 -0800760 Codegen* cg = cu->cg.get();
761 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800762 info->is_temp = true;
buzbee67bf8852011-08-17 17:51:35 -0700763}
764
buzbeefa57c472012-11-21 12:06:18 -0800765void UnmarkTemp(CompilationUnit* cu, int reg)
buzbee9e0f9b02011-08-24 15:32:46 -0700766{
buzbee02031b12012-11-23 09:41:35 -0800767 Codegen* cg = cu->cg.get();
768 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800769 info->is_temp = false;
buzbee9e0f9b02011-08-24 15:32:46 -0700770}
771
buzbeefa57c472012-11-21 12:06:18 -0800772void MarkPair(CompilationUnit* cu, int low_reg, int high_reg)
buzbee67bf8852011-08-17 17:51:35 -0700773{
buzbee02031b12012-11-23 09:41:35 -0800774 Codegen* cg = cu->cg.get();
775 RegisterInfo* info_lo = cg->GetRegInfo(cu, low_reg);
776 RegisterInfo* info_hi = cg->GetRegInfo(cu, high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800777 info_lo->pair = info_hi->pair = true;
778 info_lo->partner = high_reg;
779 info_hi->partner = low_reg;
buzbee67bf8852011-08-17 17:51:35 -0700780}
781
buzbeefa57c472012-11-21 12:06:18 -0800782void MarkClean(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700783{
buzbee02031b12012-11-23 09:41:35 -0800784 Codegen* cg = cu->cg.get();
785 RegisterInfo* info = cg->GetRegInfo(cu, loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700786 info->dirty = false;
787 if (loc.wide) {
buzbee02031b12012-11-23 09:41:35 -0800788 info = cg->GetRegInfo(cu, loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700789 info->dirty = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 }
buzbee67bf8852011-08-17 17:51:35 -0700791}
792
buzbeefa57c472012-11-21 12:06:18 -0800793void MarkDirty(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700794{
Bill Buzbeea114add2012-05-03 15:00:40 -0700795 if (loc.home) {
796 // If already home, can't be dirty
797 return;
798 }
buzbee02031b12012-11-23 09:41:35 -0800799 Codegen* cg = cu->cg.get();
800 RegisterInfo* info = cg->GetRegInfo(cu, loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 info->dirty = true;
802 if (loc.wide) {
buzbee02031b12012-11-23 09:41:35 -0800803 info = cg->GetRegInfo(cu, loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700804 info->dirty = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 }
buzbee67bf8852011-08-17 17:51:35 -0700806}
807
buzbeefa57c472012-11-21 12:06:18 -0800808void MarkInUse(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700809{
buzbee02031b12012-11-23 09:41:35 -0800810 Codegen* cg = cu->cg.get();
811 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800812 info->in_use = true;
buzbee67bf8852011-08-17 17:51:35 -0700813}
814
buzbeefa57c472012-11-21 12:06:18 -0800815static void CopyRegInfo(CompilationUnit* cu, int new_reg, int old_reg)
buzbee67bf8852011-08-17 17:51:35 -0700816{
buzbee02031b12012-11-23 09:41:35 -0800817 Codegen* cg = cu->cg.get();
818 RegisterInfo* new_info = cg->GetRegInfo(cu, new_reg);
819 RegisterInfo* old_info = cg->GetRegInfo(cu, old_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700820 // Target temp status must not change
buzbeefa57c472012-11-21 12:06:18 -0800821 bool is_temp = new_info->is_temp;
822 *new_info = *old_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700823 // Restore target's temp status
buzbeefa57c472012-11-21 12:06:18 -0800824 new_info->is_temp = is_temp;
825 new_info->reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700826}
827
buzbeefa57c472012-11-21 12:06:18 -0800828static bool CheckCorePoolSanity(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -0700829{
buzbee02031b12012-11-23 09:41:35 -0800830 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800831 for (static int i = 0; i < cu->reg_pool->num_core_regs; i++) {
832 if (cu->reg_pool->core_regs[i].pair) {
833 static int my_reg = cu->reg_pool->core_regs[i].reg;
834 static int my_sreg = cu->reg_pool->core_regs[i].s_reg;
835 static int partner_reg = cu->reg_pool->core_regs[i].partner;
buzbee02031b12012-11-23 09:41:35 -0800836 static RegisterInfo* partner = cg->GetRegInfo(cu, partner_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 DCHECK(partner != NULL);
838 DCHECK(partner->pair);
buzbeefa57c472012-11-21 12:06:18 -0800839 DCHECK_EQ(my_reg, partner->partner);
840 static int partner_sreg = partner->s_reg;
841 if (my_sreg == INVALID_SREG) {
842 DCHECK_EQ(partner_sreg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700843 } else {
buzbeefa57c472012-11-21 12:06:18 -0800844 int diff = my_sreg - partner_sreg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 DCHECK((diff == -1) || (diff == 1));
buzbee6181f792011-09-29 11:14:04 -0700846 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 }
buzbeefa57c472012-11-21 12:06:18 -0800848 if (!cu->reg_pool->core_regs[i].live) {
849 DCHECK(cu->reg_pool->core_regs[i].def_start == NULL);
850 DCHECK(cu->reg_pool->core_regs[i].def_end == NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700851 }
buzbee6181f792011-09-29 11:14:04 -0700852 }
853 return true;
854}
855
buzbeeaad94382012-11-21 07:40:50 -0800856/*
857 * Return an updated location record with current in-register status.
858 * If the value lives in live temps, reflect that fact. No code
859 * is generated. If the live value is part of an older pair,
860 * clobber both low and high.
861 * TUNING: clobbering both is a bit heavy-handed, but the alternative
862 * is a bit complex when dealing with FP regs. Examine code to see
863 * if it's worthwhile trying to be more clever here.
864 */
865
buzbeefa57c472012-11-21 12:06:18 -0800866RegLocation UpdateLoc(CompilationUnit* cu, RegLocation loc)
buzbeeaad94382012-11-21 07:40:50 -0800867{
868 DCHECK(!loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800869 DCHECK(CheckCorePoolSanity(cu));
buzbeeaad94382012-11-21 07:40:50 -0800870 if (loc.location != kLocPhysReg) {
871 DCHECK((loc.location == kLocDalvikFrame) ||
872 (loc.location == kLocCompilerTemp));
buzbeefa57c472012-11-21 12:06:18 -0800873 RegisterInfo* info_lo = AllocLive(cu, loc.s_reg_low, kAnyReg);
874 if (info_lo) {
875 if (info_lo->pair) {
876 Clobber(cu, info_lo->reg);
877 Clobber(cu, info_lo->partner);
878 FreeTemp(cu, info_lo->reg);
buzbeeaad94382012-11-21 07:40:50 -0800879 } else {
buzbeefa57c472012-11-21 12:06:18 -0800880 loc.low_reg = info_lo->reg;
buzbeeaad94382012-11-21 07:40:50 -0800881 loc.location = kLocPhysReg;
882 }
883 }
884 }
885
886 return loc;
887}
888
buzbeefa57c472012-11-21 12:06:18 -0800889/* see comments for update_loc */
890RegLocation UpdateLocWide(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700891{
Bill Buzbeea114add2012-05-03 15:00:40 -0700892 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800893 DCHECK(CheckCorePoolSanity(cu));
buzbee02031b12012-11-23 09:41:35 -0800894 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 if (loc.location != kLocPhysReg) {
896 DCHECK((loc.location == kLocDalvikFrame) ||
897 (loc.location == kLocCompilerTemp));
898 // Are the dalvik regs already live in physical registers?
buzbeefa57c472012-11-21 12:06:18 -0800899 RegisterInfo* info_lo = AllocLive(cu, loc.s_reg_low, kAnyReg);
900 RegisterInfo* info_hi = AllocLive(cu,
901 GetSRegHi(loc.s_reg_low), kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 bool match = true;
buzbeefa57c472012-11-21 12:06:18 -0800903 match = match && (info_lo != NULL);
904 match = match && (info_hi != NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700905 // Are they both core or both FP?
buzbee02031b12012-11-23 09:41:35 -0800906 match = match && (cg->IsFpReg(info_lo->reg) == cg->IsFpReg(info_hi->reg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700907 // If a pair of floating point singles, are they properly aligned?
buzbee02031b12012-11-23 09:41:35 -0800908 if (match && cg->IsFpReg(info_lo->reg)) {
buzbeefa57c472012-11-21 12:06:18 -0800909 match &= ((info_lo->reg & 0x1) == 0);
910 match &= ((info_hi->reg - info_lo->reg) == 1);
buzbee67bf8852011-08-17 17:51:35 -0700911 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 // If previously used as a pair, it is the same pair?
buzbeefa57c472012-11-21 12:06:18 -0800913 if (match && (info_lo->pair || info_hi->pair)) {
914 match = (info_lo->pair == info_hi->pair);
915 match &= ((info_lo->reg == info_hi->partner) &&
916 (info_hi->reg == info_lo->partner));
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 }
918 if (match) {
919 // Can reuse - update the register usage info
buzbeefa57c472012-11-21 12:06:18 -0800920 loc.low_reg = info_lo->reg;
921 loc.high_reg = info_hi->reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800923 MarkPair(cu, loc.low_reg, loc.high_reg);
buzbee02031b12012-11-23 09:41:35 -0800924 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700925 return loc;
926 }
927 // Can't easily reuse - clobber and free any overlaps
buzbeefa57c472012-11-21 12:06:18 -0800928 if (info_lo) {
929 Clobber(cu, info_lo->reg);
930 FreeTemp(cu, info_lo->reg);
931 if (info_lo->pair)
932 Clobber(cu, info_lo->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700933 }
buzbeefa57c472012-11-21 12:06:18 -0800934 if (info_hi) {
935 Clobber(cu, info_hi->reg);
936 FreeTemp(cu, info_hi->reg);
937 if (info_hi->pair)
938 Clobber(cu, info_hi->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700939 }
940 }
941 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700942}
943
buzbeeed3e9302011-09-23 17:34:19 -0700944
945/* For use in cases we don't know (or care) width */
buzbeefa57c472012-11-21 12:06:18 -0800946RegLocation UpdateRawLoc(CompilationUnit* cu, RegLocation loc)
buzbeeed3e9302011-09-23 17:34:19 -0700947{
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 if (loc.wide)
buzbeefa57c472012-11-21 12:06:18 -0800949 return UpdateLocWide(cu, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 else
buzbeefa57c472012-11-21 12:06:18 -0800951 return UpdateLoc(cu, loc);
buzbeeed3e9302011-09-23 17:34:19 -0700952}
953
buzbeefa57c472012-11-21 12:06:18 -0800954RegLocation EvalLocWide(CompilationUnit* cu, RegLocation loc, int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700955{
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800957 int new_regs;
958 int low_reg;
959 int high_reg;
buzbee02031b12012-11-23 09:41:35 -0800960 Codegen* cg = cu->cg.get();
buzbee67bf8852011-08-17 17:51:35 -0700961
buzbeefa57c472012-11-21 12:06:18 -0800962 loc = UpdateLocWide(cu, loc);
buzbee67bf8852011-08-17 17:51:35 -0700963
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 /* If already in registers, we can assume proper form. Right reg class? */
965 if (loc.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800966 DCHECK_EQ(cg->IsFpReg(loc.low_reg), cg->IsFpReg(loc.high_reg));
967 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
968 if (!RegClassMatches(cu, reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 /* Wrong register class. Reallocate and copy */
buzbee02031b12012-11-23 09:41:35 -0800970 new_regs = cg->AllocTypedTempPair(cu, loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800971 low_reg = new_regs & 0xff;
972 high_reg = (new_regs >> 8) & 0xff;
buzbee02031b12012-11-23 09:41:35 -0800973 cg->OpRegCopyWide(cu, low_reg, high_reg, loc.low_reg, loc.high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800974 CopyRegInfo(cu, low_reg, loc.low_reg);
975 CopyRegInfo(cu, high_reg, loc.high_reg);
976 Clobber(cu, loc.low_reg);
977 Clobber(cu, loc.high_reg);
978 loc.low_reg = low_reg;
979 loc.high_reg = high_reg;
980 MarkPair(cu, loc.low_reg, loc.high_reg);
buzbee02031b12012-11-23 09:41:35 -0800981 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700982 }
buzbee67bf8852011-08-17 17:51:35 -0700983 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700984 }
985
buzbeefa57c472012-11-21 12:06:18 -0800986 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
987 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700988
buzbee02031b12012-11-23 09:41:35 -0800989 new_regs = cg->AllocTypedTempPair(cu, loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800990 loc.low_reg = new_regs & 0xff;
991 loc.high_reg = (new_regs >> 8) & 0xff;
Bill Buzbeea114add2012-05-03 15:00:40 -0700992
buzbeefa57c472012-11-21 12:06:18 -0800993 MarkPair(cu, loc.low_reg, loc.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700994 if (update) {
995 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800996 MarkLive(cu, loc.low_reg, loc.s_reg_low);
997 MarkLive(cu, loc.high_reg, GetSRegHi(loc.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 }
buzbee02031b12012-11-23 09:41:35 -0800999 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001000 return loc;
buzbee67bf8852011-08-17 17:51:35 -07001001}
1002
buzbeefa57c472012-11-21 12:06:18 -08001003RegLocation EvalLoc(CompilationUnit* cu, RegLocation loc,
1004 int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -07001005{
buzbeefa57c472012-11-21 12:06:18 -08001006 int new_reg;
buzbee67bf8852011-08-17 17:51:35 -07001007
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 if (loc.wide)
buzbeefa57c472012-11-21 12:06:18 -08001009 return EvalLocWide(cu, loc, reg_class, update);
buzbee67bf8852011-08-17 17:51:35 -07001010
buzbee02031b12012-11-23 09:41:35 -08001011 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08001012 loc = UpdateLoc(cu, loc);
buzbee67bf8852011-08-17 17:51:35 -07001013
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 if (loc.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -08001015 if (!RegClassMatches(cu, reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001016 /* Wrong register class. Realloc, copy and transfer ownership */
buzbee02031b12012-11-23 09:41:35 -08001017 new_reg = cg->AllocTypedTemp(cu, loc.fp, reg_class);
1018 cg->OpRegCopy(cu, new_reg, loc.low_reg);
buzbeefa57c472012-11-21 12:06:18 -08001019 CopyRegInfo(cu, new_reg, loc.low_reg);
1020 Clobber(cu, loc.low_reg);
1021 loc.low_reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -07001022 }
1023 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 }
1025
buzbeefa57c472012-11-21 12:06:18 -08001026 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001027
buzbee02031b12012-11-23 09:41:35 -08001028 new_reg = cg->AllocTypedTemp(cu, loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -08001029 loc.low_reg = new_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001030
1031 if (update) {
1032 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001033 MarkLive(cu, loc.low_reg, loc.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001034 }
1035 return loc;
buzbee67bf8852011-08-17 17:51:35 -07001036}
1037
buzbeefa57c472012-11-21 12:06:18 -08001038RegLocation GetRawSrc(CompilationUnit* cu, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001039{
buzbeefa57c472012-11-21 12:06:18 -08001040 DCHECK(num < mir->ssa_rep->num_uses);
1041 RegLocation res = cu->reg_location[mir->ssa_rep->uses[num]];
buzbee15bf9802012-06-12 17:49:27 -07001042 return res;
1043}
buzbeeeaf09bc2012-11-15 14:51:41 -08001044
buzbeefa57c472012-11-21 12:06:18 -08001045RegLocation GetRawDest(CompilationUnit* cu, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001046{
buzbeefa57c472012-11-21 12:06:18 -08001047 DCHECK_GT(mir->ssa_rep->num_defs, 0);
1048 RegLocation res = cu->reg_location[mir->ssa_rep->defs[0]];
buzbee15bf9802012-06-12 17:49:27 -07001049 return res;
1050}
buzbeeeaf09bc2012-11-15 14:51:41 -08001051
buzbeefa57c472012-11-21 12:06:18 -08001052RegLocation GetDest(CompilationUnit* cu, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001053{
buzbeefa57c472012-11-21 12:06:18 -08001054 RegLocation res = GetRawDest(cu, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001055 DCHECK(!res.wide);
1056 return res;
buzbee67bf8852011-08-17 17:51:35 -07001057}
buzbeeeaf09bc2012-11-15 14:51:41 -08001058
buzbeefa57c472012-11-21 12:06:18 -08001059RegLocation GetSrc(CompilationUnit* cu, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001060{
buzbeefa57c472012-11-21 12:06:18 -08001061 RegLocation res = GetRawSrc(cu, mir, num);
Bill Buzbeea114add2012-05-03 15:00:40 -07001062 DCHECK(!res.wide);
1063 return res;
buzbeee9a72f62011-09-04 17:59:07 -07001064}
buzbeeeaf09bc2012-11-15 14:51:41 -08001065
buzbeefa57c472012-11-21 12:06:18 -08001066RegLocation GetDestWide(CompilationUnit* cu, MIR* mir)
buzbeee9a72f62011-09-04 17:59:07 -07001067{
buzbeefa57c472012-11-21 12:06:18 -08001068 RegLocation res = GetRawDest(cu, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001069 DCHECK(res.wide);
1070 return res;
buzbee67bf8852011-08-17 17:51:35 -07001071}
1072
buzbeefa57c472012-11-21 12:06:18 -08001073RegLocation GetSrcWide(CompilationUnit* cu, MIR* mir,
buzbee15bf9802012-06-12 17:49:27 -07001074 int low)
buzbee67bf8852011-08-17 17:51:35 -07001075{
buzbeefa57c472012-11-21 12:06:18 -08001076 RegLocation res = GetRawSrc(cu, mir, low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001077 DCHECK(res.wide);
1078 return res;
buzbee67bf8852011-08-17 17:51:35 -07001079}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001080
buzbeefa57c472012-11-21 12:06:18 -08001081/* USE SSA names to count references of base Dalvik v_regs. */
1082static void CountRefs(CompilationUnit *cu, BasicBlock* bb, RefCounts* core_counts,
1083 RefCounts* fp_counts)
buzbeee3acd072012-02-25 17:03:10 -08001084{
buzbeefa57c472012-11-21 12:06:18 -08001085 if ((cu->disable_opt & (1 << kPromoteRegs)) ||
1086 !((bb->block_type == kEntryBlock) || (bb->block_type == kExitBlock) ||
1087 (bb->block_type == kDalvikByteCode))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001088 return;
1089 }
buzbeefa57c472012-11-21 12:06:18 -08001090 for (int i = 0; i < cu->num_ssa_regs;) {
1091 RegLocation loc = cu->reg_location[i];
1092 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1093 int p_map_idx = SRegToPMap(cu, loc.s_reg_low);
buzbeee6285f92012-12-06 15:57:46 -08001094 int sample_reg = loc.fp ? cu->reg_pool->FPRegs[0].reg : cu->reg_pool->core_regs[0].reg;
1095 bool simple_immediate = loc.is_const &&
1096 !cu->cg->InexpensiveConstant(sample_reg, cu->constant_values[loc.orig_sreg]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001097 if (loc.defined) {
buzbeee6285f92012-12-06 15:57:46 -08001098 // Don't count easily regenerated immediates
1099 if (!simple_immediate) {
1100 counts[p_map_idx].count += cu->use_counts.elem_list[i];
1101 }
buzbee239c4e72012-03-16 08:42:29 -07001102 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001103 if (loc.wide) {
1104 if (loc.defined) {
buzbeee6285f92012-12-06 15:57:46 -08001105 if (loc.fp && !simple_immediate) {
buzbeefa57c472012-11-21 12:06:18 -08001106 counts[p_map_idx].double_start = true;
1107 counts[p_map_idx+1].count += cu->use_counts.elem_list[i+1];
buzbee239c4e72012-03-16 08:42:29 -07001108 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001109 }
1110 i += 2;
1111 } else {
1112 i++;
buzbeee3acd072012-02-25 17:03:10 -08001113 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 }
buzbeee3acd072012-02-25 17:03:10 -08001115}
1116
1117/* qsort callback function, sort descending */
buzbeeaad94382012-11-21 07:40:50 -08001118static int SortCounts(const void *val1, const void *val2)
buzbeee3acd072012-02-25 17:03:10 -08001119{
buzbeecbd6d442012-11-17 14:11:25 -08001120 const RefCounts* op1 = reinterpret_cast<const RefCounts*>(val1);
1121 const RefCounts* op2 = reinterpret_cast<const RefCounts*>(val2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001122 return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
buzbeee3acd072012-02-25 17:03:10 -08001123}
1124
buzbeeaad94382012-11-21 07:40:50 -08001125static void DumpCounts(const RefCounts* arr, int size, const char* msg)
buzbeee3acd072012-02-25 17:03:10 -08001126{
Bill Buzbeea114add2012-05-03 15:00:40 -07001127 LOG(INFO) << msg;
1128 for (int i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001129 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
Bill Buzbeea114add2012-05-03 15:00:40 -07001130 }
buzbeee3acd072012-02-25 17:03:10 -08001131}
1132
1133/*
1134 * Note: some portions of this code required even if the kPromoteRegs
1135 * optimization is disabled.
1136 */
buzbeefa57c472012-11-21 12:06:18 -08001137void DoPromotion(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -08001138{
buzbee02031b12012-11-23 09:41:35 -08001139 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08001140 int reg_bias = cu->num_compiler_temps + 1;
1141 int dalvik_regs = cu->num_dalvik_registers;
1142 int num_regs = dalvik_regs + reg_bias;
1143 const int promotion_threshold = 2;
buzbeee3acd072012-02-25 17:03:10 -08001144
Bill Buzbeea114add2012-05-03 15:00:40 -07001145 // Allow target code to add any special registers
buzbee02031b12012-11-23 09:41:35 -08001146 cg->AdjustSpillMask(cu);
buzbeee3acd072012-02-25 17:03:10 -08001147
Bill Buzbeea114add2012-05-03 15:00:40 -07001148 /*
1149 * Simple register promotion. Just do a static count of the uses
1150 * of Dalvik registers. Note that we examine the SSA names, but
1151 * count based on original Dalvik register name. Count refs
1152 * separately based on type in order to give allocation
1153 * preference to fp doubles - which must be allocated sequential
1154 * physical single fp registers started with an even-numbered
1155 * reg.
1156 * TUNING: replace with linear scan once we have the ability
1157 * to describe register live ranges for GC.
1158 */
buzbeefa57c472012-11-21 12:06:18 -08001159 RefCounts *core_regs = static_cast<RefCounts*>(NewMem(cu, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001160 true, kAllocRegAlloc));
buzbeefa57c472012-11-21 12:06:18 -08001161 RefCounts *FpRegs = static_cast<RefCounts *>(NewMem(cu, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001162 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 // Set ssa names for original Dalvik registers
buzbeefa57c472012-11-21 12:06:18 -08001164 for (int i = 0; i < dalvik_regs; i++) {
1165 core_regs[i].s_reg = FpRegs[i].s_reg = i;
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 }
1167 // Set ssa name for Method*
buzbeefa57c472012-11-21 12:06:18 -08001168 core_regs[dalvik_regs].s_reg = cu->method_sreg;
1169 FpRegs[dalvik_regs].s_reg = cu->method_sreg; // For consistecy
1170 // Set ssa names for compiler_temps
1171 for (int i = 1; i <= cu->num_compiler_temps; i++) {
1172 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cu->compiler_temps.elem_list[i]);
1173 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
1174 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001175 }
1176
1177 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001178 GrowableListIteratorInit(&cu->block_list, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 while (true) {
1180 BasicBlock* bb;
buzbee52a77fc2012-11-20 19:50:46 -08001181 bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -07001182 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -08001183 CountRefs(cu, bb, core_regs, FpRegs);
Bill Buzbeea114add2012-05-03 15:00:40 -07001184 }
1185
1186 /*
1187 * Ideally, we'd allocate doubles starting with an even-numbered
1188 * register. Bias the counts to try to allocate any vreg that's
1189 * used as the start of a pair first.
1190 */
buzbeefa57c472012-11-21 12:06:18 -08001191 for (int i = 0; i < num_regs; i++) {
1192 if (FpRegs[i].double_start) {
buzbee52a77fc2012-11-20 19:50:46 -08001193 FpRegs[i].count *= 2;
buzbeee3acd072012-02-25 17:03:10 -08001194 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001195 }
1196
1197 // Sort the count arrays
buzbeefa57c472012-11-21 12:06:18 -08001198 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
1199 qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts);
Bill Buzbeea114add2012-05-03 15:00:40 -07001200
buzbeefa57c472012-11-21 12:06:18 -08001201 if (cu->verbose) {
1202 DumpCounts(core_regs, num_regs, "Core regs after sort");
1203 DumpCounts(FpRegs, num_regs, "Fp regs after sort");
Bill Buzbeea114add2012-05-03 15:00:40 -07001204 }
1205
buzbeefa57c472012-11-21 12:06:18 -08001206 if (!(cu->disable_opt & (1 << kPromoteRegs))) {
buzbee52a77fc2012-11-20 19:50:46 -08001207 // Promote FpRegs
buzbeefa57c472012-11-21 12:06:18 -08001208 for (int i = 0; (i < num_regs) &&
1209 (FpRegs[i].count >= promotion_threshold ); i++) {
1210 int p_map_idx = SRegToPMap(cu, FpRegs[i].s_reg);
1211 if (cu->promotion_map[p_map_idx].fp_location != kLocPhysReg) {
1212 int reg = AllocPreservedFPReg(cu, FpRegs[i].s_reg,
1213 FpRegs[i].double_start);
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 if (reg < 0) {
1215 break; // No more left
1216 }
1217 }
buzbee239c4e72012-03-16 08:42:29 -07001218 }
buzbee9c044ce2012-03-18 13:24:07 -07001219
Bill Buzbeea114add2012-05-03 15:00:40 -07001220 // Promote core regs
buzbeefa57c472012-11-21 12:06:18 -08001221 for (int i = 0; (i < num_regs) &&
1222 (core_regs[i].count > promotion_threshold); i++) {
1223 int p_map_idx = SRegToPMap(cu, core_regs[i].s_reg);
1224 if (cu->promotion_map[p_map_idx].core_location !=
Bill Buzbeea114add2012-05-03 15:00:40 -07001225 kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -08001226 int reg = AllocPreservedCoreReg(cu, core_regs[i].s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001227 if (reg < 0) {
1228 break; // No more left
1229 }
1230 }
buzbeee3acd072012-02-25 17:03:10 -08001231 }
buzbeefa57c472012-11-21 12:06:18 -08001232 } else if (cu->qd_mode) {
1233 AllocPreservedCoreReg(cu, cu->method_sreg);
1234 for (int i = 0; i < num_regs; i++) {
1235 int reg = AllocPreservedCoreReg(cu, i);
Bill Buzbeea114add2012-05-03 15:00:40 -07001236 if (reg < 0) {
1237 break; // No more left
1238 }
buzbeee3acd072012-02-25 17:03:10 -08001239 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001240 }
buzbeee3acd072012-02-25 17:03:10 -08001241
buzbeee3acd072012-02-25 17:03:10 -08001242
Bill Buzbeea114add2012-05-03 15:00:40 -07001243 // Now, update SSA names to new home locations
buzbeefa57c472012-11-21 12:06:18 -08001244 for (int i = 0; i < cu->num_ssa_regs; i++) {
1245 RegLocation *curr = &cu->reg_location[i];
1246 int p_map_idx = SRegToPMap(cu, curr->s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001247 if (!curr->wide) {
1248 if (curr->fp) {
buzbeefa57c472012-11-21 12:06:18 -08001249 if (cu->promotion_map[p_map_idx].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001250 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001251 curr->low_reg = cu->promotion_map[p_map_idx].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001252 curr->home = true;
1253 }
1254 } else {
buzbeefa57c472012-11-21 12:06:18 -08001255 if (cu->promotion_map[p_map_idx].core_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001256 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001257 curr->low_reg = cu->promotion_map[p_map_idx].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001258 curr->home = true;
1259 }
1260 }
buzbeefa57c472012-11-21 12:06:18 -08001261 curr->high_reg = INVALID_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -07001262 } else {
buzbeefa57c472012-11-21 12:06:18 -08001263 if (curr->high_word) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001264 continue;
1265 }
1266 if (curr->fp) {
buzbeefa57c472012-11-21 12:06:18 -08001267 if ((cu->promotion_map[p_map_idx].fp_location == kLocPhysReg) &&
1268 (cu->promotion_map[p_map_idx+1].fp_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001269 kLocPhysReg)) {
buzbeefa57c472012-11-21 12:06:18 -08001270 int low_reg = cu->promotion_map[p_map_idx].FpReg;
1271 int high_reg = cu->promotion_map[p_map_idx+1].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001272 // Doubles require pair of singles starting at even reg
buzbeefa57c472012-11-21 12:06:18 -08001273 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001274 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001275 curr->low_reg = low_reg;
1276 curr->high_reg = high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001277 curr->home = true;
1278 }
1279 }
1280 } else {
buzbeefa57c472012-11-21 12:06:18 -08001281 if ((cu->promotion_map[p_map_idx].core_location == kLocPhysReg)
1282 && (cu->promotion_map[p_map_idx+1].core_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001283 kLocPhysReg)) {
1284 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001285 curr->low_reg = cu->promotion_map[p_map_idx].core_reg;
1286 curr->high_reg = cu->promotion_map[p_map_idx+1].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001287 curr->home = true;
1288 }
1289 }
buzbeee3acd072012-02-25 17:03:10 -08001290 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001291 }
buzbeefa57c472012-11-21 12:06:18 -08001292 if (cu->verbose) {
1293 DumpPromotionMap(cu);
buzbeeca7a5e42012-08-20 11:12:18 -07001294 }
buzbeee3acd072012-02-25 17:03:10 -08001295}
1296
1297/* Returns sp-relative offset in bytes for a VReg */
buzbeefa57c472012-11-21 12:06:18 -08001298int VRegOffset(CompilationUnit* cu, int v_reg)
buzbeee3acd072012-02-25 17:03:10 -08001299{
buzbeefa57c472012-11-21 12:06:18 -08001300 return StackVisitor::GetVRegOffset(cu->code_item, cu->core_spill_mask,
1301 cu->fp_spill_mask, cu->frame_size, v_reg);
buzbeee3acd072012-02-25 17:03:10 -08001302}
1303
1304/* Returns sp-relative offset in bytes for a SReg */
buzbeefa57c472012-11-21 12:06:18 -08001305int SRegOffset(CompilationUnit* cu, int s_reg)
buzbeee3acd072012-02-25 17:03:10 -08001306{
buzbeefa57c472012-11-21 12:06:18 -08001307 return VRegOffset(cu, SRegToVReg(cu, s_reg));
buzbeee3acd072012-02-25 17:03:10 -08001308}
1309
buzbee02031b12012-11-23 09:41:35 -08001310RegLocation GetBadLoc()
1311{
1312 RegLocation res = bad_loc;
1313 return res;
1314}
1315
1316/* Mark register usage state and return long retloc */
1317RegLocation GetReturnWide(CompilationUnit* cu, bool is_double)
1318{
1319 Codegen* cg = cu->cg.get();
1320 RegLocation gpr_res = cg->LocCReturnWide();
1321 RegLocation fpr_res = cg->LocCReturnDouble();
1322 RegLocation res = is_double ? fpr_res : gpr_res;
1323 Clobber(cu, res.low_reg);
1324 Clobber(cu, res.high_reg);
1325 LockTemp(cu, res.low_reg);
1326 LockTemp(cu, res.high_reg);
1327 MarkPair(cu, res.low_reg, res.high_reg);
1328 return res;
1329}
1330
1331RegLocation GetReturn(CompilationUnit* cu, bool is_float)
1332{
1333 Codegen* cg = cu->cg.get();
1334 RegLocation gpr_res = cg->LocCReturn();
1335 RegLocation fpr_res = cg->LocCReturnFloat();
1336 RegLocation res = is_float ? fpr_res : gpr_res;
1337 Clobber(cu, res.low_reg);
1338 if (cu->instruction_set == kMips) {
1339 MarkInUse(cu, res.low_reg);
1340 } else {
1341 LockTemp(cu, res.low_reg);
1342 }
1343 return res;
1344}
1345
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001346} // namespace art