blob: 999c6527e96744411c338b64c8b6ddd84a44170c [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbeeb046e162012-10-30 15:48:42 -070017/* This file contains register alloction support. */
buzbee67bf8852011-08-17 17:51:35 -070018
buzbeeefc63692012-11-14 16:31:52 -080019#include "../compiler_utility.h"
20#include "../compiler_ir.h"
21#include "../dataflow.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080022#include "ralloc_util.h"
23#include "codegen_util.h"
buzbee67bf8852011-08-17 17:51:35 -070024
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
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
buzbeefa57c472012-11-21 12:06:18 -0800127/* Clobber any temp associated with an s_reg. Could be in either class */
128void ClobberSReg(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700129{
buzbee3d661942012-03-14 17:37:27 -0700130#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 /* Reset live temp tracking sanity checker */
buzbeefa57c472012-11-21 12:06:18 -0800132 if (s_reg == cu->live_sreg) {
133 cu->live_sreg = INVALID_SREG;
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 }
buzbee3d661942012-03-14 17:37:27 -0700135#endif
buzbeefa57c472012-11-21 12:06:18 -0800136 ClobberSRegBody(cu->reg_pool->core_regs, cu->reg_pool->num_core_regs, s_reg);
137 ClobberSRegBody(cu->reg_pool->FPRegs, cu->reg_pool->num_fp_regs, s_reg);
buzbee67bf8852011-08-17 17:51:35 -0700138}
139
buzbee9c044ce2012-03-18 13:24:07 -0700140/*
141 * SSA names associated with the initial definitions of Dalvik
142 * registers are the same as the Dalvik register number (and
buzbeefa57c472012-11-21 12:06:18 -0800143 * thus take the same position in the promotion_map. However,
buzbee9c044ce2012-03-18 13:24:07 -0700144 * the special Method* and compiler temp resisters use negative
buzbeefa57c472012-11-21 12:06:18 -0800145 * v_reg numbers to distinguish them and can have an arbitrary
buzbee9c044ce2012-03-18 13:24:07 -0700146 * ssa name (above the last original Dalvik register). This function
buzbeefa57c472012-11-21 12:06:18 -0800147 * maps SSA names to positions in the promotion_map array.
buzbee9c044ce2012-03-18 13:24:07 -0700148 */
buzbee5f61f672012-11-28 17:22:17 -0800149int SRegToPMap(CompilationUnit* cu, int s_reg)
buzbeee1965672012-03-11 18:39:19 -0700150{
buzbeefa57c472012-11-21 12:06:18 -0800151 DCHECK_LT(s_reg, cu->num_ssa_regs);
152 DCHECK_GE(s_reg, 0);
153 int v_reg = SRegToVReg(cu, s_reg);
154 if (v_reg >= 0) {
155 DCHECK_LT(v_reg, cu->num_dalvik_registers);
156 return v_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 } else {
buzbeefa57c472012-11-21 12:06:18 -0800158 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
159 DCHECK_LE(pos, cu->num_compiler_temps);
160 return cu->num_dalvik_registers + pos;
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 }
buzbeee1965672012-03-11 18:39:19 -0700162}
163
buzbeefa57c472012-11-21 12:06:18 -0800164void RecordCorePromotion(CompilationUnit* cu, int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700165{
buzbee02031b12012-11-23 09:41:35 -0800166 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800167 int p_map_idx = SRegToPMap(cu, s_reg);
168 int v_reg = SRegToVReg(cu, s_reg);
buzbee02031b12012-11-23 09:41:35 -0800169 cg->GetRegInfo(cu, reg)->in_use = true;
buzbeefa57c472012-11-21 12:06:18 -0800170 cu->core_spill_mask |= (1 << reg);
buzbeeca7a5e42012-08-20 11:12:18 -0700171 // Include reg for later sort
buzbeefa57c472012-11-21 12:06:18 -0800172 cu->core_vmap_table.push_back(reg << VREG_NUM_WIDTH |
173 (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
174 cu->num_core_spills++;
175 cu->promotion_map[p_map_idx].core_location = kLocPhysReg;
176 cu->promotion_map[p_map_idx].core_reg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700177}
178
buzbee67bf8852011-08-17 17:51:35 -0700179/* Reserve a callee-save register. Return -1 if none available */
buzbeefa57c472012-11-21 12:06:18 -0800180static int AllocPreservedCoreReg(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700181{
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800183 RegisterInfo* core_regs = cu->reg_pool->core_regs;
184 for (int i = 0; i < cu->reg_pool->num_core_regs; i++) {
185 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
186 res = core_regs[i].reg;
187 RecordCorePromotion(cu, res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 break;
buzbee67bf8852011-08-17 17:51:35 -0700189 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 }
191 return res;
buzbee67bf8852011-08-17 17:51:35 -0700192}
193
buzbeefa57c472012-11-21 12:06:18 -0800194void RecordFpPromotion(CompilationUnit* cu, int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700195{
buzbee02031b12012-11-23 09:41:35 -0800196 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800197 int p_map_idx = SRegToPMap(cu, s_reg);
198 int v_reg = SRegToVReg(cu, s_reg);
buzbee02031b12012-11-23 09:41:35 -0800199 cg->GetRegInfo(cu, reg)->in_use = true;
200 cg->MarkPreservedSingle(cu, v_reg, reg);
buzbeefa57c472012-11-21 12:06:18 -0800201 cu->promotion_map[p_map_idx].fp_location = kLocPhysReg;
202 cu->promotion_map[p_map_idx].FpReg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700203}
204
buzbee67bf8852011-08-17 17:51:35 -0700205/*
206 * Reserve a callee-save fp single register. Try to fullfill request for
207 * even/odd allocation, but go ahead and allocate anything if not
208 * available. If nothing's available, return -1.
209 */
buzbeefa57c472012-11-21 12:06:18 -0800210static int AllocPreservedSingle(CompilationUnit* cu, int s_reg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700211{
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800213 RegisterInfo* FPRegs = cu->reg_pool->FPRegs;
214 for (int i = 0; i < cu->reg_pool->num_fp_regs; i++) {
215 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 ((FPRegs[i].reg & 0x1) == 0) == even) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700217 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800218 RecordFpPromotion(cu, res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 break;
buzbee67bf8852011-08-17 17:51:35 -0700220 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 }
222 return res;
buzbee67bf8852011-08-17 17:51:35 -0700223}
224
225/*
226 * Somewhat messy code here. We want to allocate a pair of contiguous
227 * physical single-precision floating point registers starting with
buzbeefa57c472012-11-21 12:06:18 -0800228 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
buzbee67bf8852011-08-17 17:51:35 -0700229 * has already been allocated - try to fit if possible. Fail to
230 * allocate if we can't meet the requirements for the pair of
buzbeefa57c472012-11-21 12:06:18 -0800231 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
buzbee67bf8852011-08-17 17:51:35 -0700232 */
buzbeefa57c472012-11-21 12:06:18 -0800233static int AllocPreservedDouble(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700234{
buzbee02031b12012-11-23 09:41:35 -0800235 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 int res = -1; // Assume failure
buzbeefa57c472012-11-21 12:06:18 -0800237 int v_reg = SRegToVReg(cu, s_reg);
238 int p_map_idx = SRegToPMap(cu, s_reg);
239 if (cu->promotion_map[p_map_idx+1].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 // Upper reg is already allocated. Can we fit?
buzbeefa57c472012-11-21 12:06:18 -0800241 int high_reg = cu->promotion_map[p_map_idx+1].FpReg;
242 if ((high_reg & 1) == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 // High reg is even - fail.
244 return res;
245 }
246 // Is the low reg of the pair free?
buzbee02031b12012-11-23 09:41:35 -0800247 RegisterInfo* p = cg->GetRegInfo(cu, high_reg-1);
buzbeefa57c472012-11-21 12:06:18 -0800248 if (p->in_use || p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 // Already allocated or not preserved - fail.
250 return res;
251 }
252 // OK - good to go.
253 res = p->reg;
buzbeefa57c472012-11-21 12:06:18 -0800254 p->in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 DCHECK_EQ((res & 1), 0);
buzbee02031b12012-11-23 09:41:35 -0800256 cg->MarkPreservedSingle(cu, v_reg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 } else {
buzbeefa57c472012-11-21 12:06:18 -0800258 RegisterInfo* FPRegs = cu->reg_pool->FPRegs;
259 for (int i = 0; i < cu->reg_pool->num_fp_regs; i++) {
260 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 ((FPRegs[i].reg & 0x1) == 0x0) &&
buzbeefa57c472012-11-21 12:06:18 -0800262 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
264 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
265 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800266 FPRegs[i].in_use = true;
buzbee02031b12012-11-23 09:41:35 -0800267 cg->MarkPreservedSingle(cu, v_reg, res);
buzbeefa57c472012-11-21 12:06:18 -0800268 FPRegs[i+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
buzbee02031b12012-11-23 09:41:35 -0800270 cg->MarkPreservedSingle(cu, v_reg+1, res+1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 break;
272 }
buzbee67bf8852011-08-17 17:51:35 -0700273 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 }
275 if (res != -1) {
buzbeefa57c472012-11-21 12:06:18 -0800276 cu->promotion_map[p_map_idx].fp_location = kLocPhysReg;
277 cu->promotion_map[p_map_idx].FpReg = res;
278 cu->promotion_map[p_map_idx+1].fp_location = kLocPhysReg;
279 cu->promotion_map[p_map_idx+1].FpReg = res + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 }
281 return res;
buzbee67bf8852011-08-17 17:51:35 -0700282}
283
284
285/*
286 * Reserve a callee-save fp register. If this register can be used
287 * as the first of a double, attempt to allocate an even pair of fp
288 * single regs (but if can't still attempt to allocate a single, preferring
289 * first to allocate an odd register.
290 */
buzbeefa57c472012-11-21 12:06:18 -0800291static int AllocPreservedFPReg(CompilationUnit* cu, int s_reg, bool double_start)
buzbee67bf8852011-08-17 17:51:35 -0700292{
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800294 if (double_start) {
295 res = AllocPreservedDouble(cu, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 }
297 if (res == -1) {
buzbeefa57c472012-11-21 12:06:18 -0800298 res = AllocPreservedSingle(cu, s_reg, false /* try odd # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700299 }
300 if (res == -1)
buzbeefa57c472012-11-21 12:06:18 -0800301 res = AllocPreservedSingle(cu, s_reg, true /* try even # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 return res;
buzbee67bf8852011-08-17 17:51:35 -0700303}
304
buzbeefa57c472012-11-21 12:06:18 -0800305static int AllocTempBody(CompilationUnit* cu, RegisterInfo* p, int num_regs, int* next_temp,
buzbeeaad94382012-11-21 07:40:50 -0800306 bool required)
buzbee67bf8852011-08-17 17:51:35 -0700307{
Bill Buzbeea114add2012-05-03 15:00:40 -0700308 int i;
buzbeefa57c472012-11-21 12:06:18 -0800309 int next = *next_temp;
310 for (i=0; i< num_regs; i++) {
311 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800313 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
314 Clobber(cu, p[next].reg);
315 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700316 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800317 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700319 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 next++;
321 }
buzbeefa57c472012-11-21 12:06:18 -0800322 next = *next_temp;
323 for (i=0; i< num_regs; i++) {
324 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800326 if (p[next].is_temp && !p[next].in_use) {
327 Clobber(cu, p[next].reg);
328 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800330 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700332 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 next++;
334 }
335 if (required) {
buzbeefa57c472012-11-21 12:06:18 -0800336 CodegenDump(cu);
337 DumpRegPool(cu->reg_pool->core_regs,
338 cu->reg_pool->num_core_regs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 LOG(FATAL) << "No free temp registers";
340 }
341 return -1; // No register available
buzbee67bf8852011-08-17 17:51:35 -0700342}
343
344//REDO: too many assumptions.
buzbeefa57c472012-11-21 12:06:18 -0800345int AllocTempDouble(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700346{
buzbeefa57c472012-11-21 12:06:18 -0800347 RegisterInfo* p = cu->reg_pool->FPRegs;
348 int num_regs = cu->reg_pool->num_fp_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 /* Start looking at an even reg */
buzbeefa57c472012-11-21 12:06:18 -0800350 int next = cu->reg_pool->next_fp_reg & ~0x1;
buzbee67bf8852011-08-17 17:51:35 -0700351
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 // First try to avoid allocating live registers
buzbeefa57c472012-11-21 12:06:18 -0800353 for (int i=0; i < num_regs; i+=2) {
354 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800356 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
357 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
358 Clobber(cu, p[next].reg);
359 Clobber(cu, p[next+1].reg);
360 p[next].in_use = true;
361 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
363 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800364 cu->reg_pool->next_fp_reg = next + 2;
365 if (cu->reg_pool->next_fp_reg >= num_regs) {
366 cu->reg_pool->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 }
368 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700369 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700370 next += 2;
371 }
buzbeefa57c472012-11-21 12:06:18 -0800372 next = cu->reg_pool->next_fp_reg & ~0x1;
buzbeea50638b2011-11-02 15:15:06 -0700373
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 // No choice - find a pair and kill it.
buzbeefa57c472012-11-21 12:06:18 -0800375 for (int i=0; i < num_regs; i+=2) {
376 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800378 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
379 !p[next+1].in_use) {
380 Clobber(cu, p[next].reg);
381 Clobber(cu, p[next+1].reg);
382 p[next].in_use = true;
383 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
385 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800386 cu->reg_pool->next_fp_reg = next + 2;
387 if (cu->reg_pool->next_fp_reg >= num_regs) {
388 cu->reg_pool->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 }
390 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700391 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 next += 2;
393 }
394 LOG(FATAL) << "No free temp registers (pair)";
395 return -1;
buzbee67bf8852011-08-17 17:51:35 -0700396}
397
398/* Return a temp if one is available, -1 otherwise */
buzbeefa57c472012-11-21 12:06:18 -0800399int AllocFreeTemp(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700400{
buzbeefa57c472012-11-21 12:06:18 -0800401 return AllocTempBody(cu, cu->reg_pool->core_regs,
402 cu->reg_pool->num_core_regs,
403 &cu->reg_pool->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700404}
405
buzbeefa57c472012-11-21 12:06:18 -0800406int AllocTemp(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700407{
buzbeefa57c472012-11-21 12:06:18 -0800408 return AllocTempBody(cu, cu->reg_pool->core_regs,
409 cu->reg_pool->num_core_regs,
410 &cu->reg_pool->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700411}
412
buzbeefa57c472012-11-21 12:06:18 -0800413int AllocTempFloat(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700414{
buzbeefa57c472012-11-21 12:06:18 -0800415 return AllocTempBody(cu, cu->reg_pool->FPRegs,
416 cu->reg_pool->num_fp_regs,
417 &cu->reg_pool->next_fp_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700418}
419
buzbeefa57c472012-11-21 12:06:18 -0800420static RegisterInfo* AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700421{
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 int i;
buzbeefa57c472012-11-21 12:06:18 -0800423 if (s_reg == -1)
buzbee67bf8852011-08-17 17:51:35 -0700424 return NULL;
buzbeefa57c472012-11-21 12:06:18 -0800425 for (i=0; i < num_regs; i++) {
426 if (p[i].live && (p[i].s_reg == s_reg)) {
427 if (p[i].is_temp)
428 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 return &p[i];
430 }
431 }
432 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700433}
434
buzbeefa57c472012-11-21 12:06:18 -0800435RegisterInfo* AllocLive(CompilationUnit* cu, int s_reg, int reg_class)
buzbee67bf8852011-08-17 17:51:35 -0700436{
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 RegisterInfo* res = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800438 switch (reg_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 case kAnyReg:
buzbeefa57c472012-11-21 12:06:18 -0800440 res = AllocLiveBody(cu->reg_pool->FPRegs,
441 cu->reg_pool->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 if (res)
443 break;
444 /* Intentional fallthrough */
445 case kCoreReg:
buzbeefa57c472012-11-21 12:06:18 -0800446 res = AllocLiveBody(cu->reg_pool->core_regs,
447 cu->reg_pool->num_core_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 break;
449 case kFPReg:
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 break;
453 default:
454 LOG(FATAL) << "Invalid register type";
455 }
456 return res;
buzbee67bf8852011-08-17 17:51:35 -0700457}
458
buzbeefa57c472012-11-21 12:06:18 -0800459void FreeTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700460{
buzbeefa57c472012-11-21 12:06:18 -0800461 RegisterInfo* p = cu->reg_pool->core_regs;
462 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 int i;
buzbeefa57c472012-11-21 12:06:18 -0800464 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700465 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800466 if (p[i].is_temp) {
467 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 }
469 p[i].pair = false;
470 return;
buzbee67bf8852011-08-17 17:51:35 -0700471 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 }
buzbeefa57c472012-11-21 12:06:18 -0800473 p = cu->reg_pool->FPRegs;
474 num_regs = cu->reg_pool->num_fp_regs;
475 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800477 if (p[i].is_temp) {
478 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 }
480 p[i].pair = false;
481 return;
buzbee67bf8852011-08-17 17:51:35 -0700482 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 }
484 LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700485}
486
buzbeefa57c472012-11-21 12:06:18 -0800487RegisterInfo* IsLive(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700488{
buzbeefa57c472012-11-21 12:06:18 -0800489 RegisterInfo* p = cu->reg_pool->core_regs;
490 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 int i;
buzbeefa57c472012-11-21 12:06:18 -0800492 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 if (p[i].reg == reg) {
494 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700495 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 }
buzbeefa57c472012-11-21 12:06:18 -0800497 p = cu->reg_pool->FPRegs;
498 num_regs = cu->reg_pool->num_fp_regs;
499 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 if (p[i].reg == reg) {
501 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700502 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 }
504 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700505}
506
buzbeefa57c472012-11-21 12:06:18 -0800507RegisterInfo* IsTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700508{
buzbee02031b12012-11-23 09:41:35 -0800509 Codegen* cg = cu->cg.get();
510 RegisterInfo* p = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800511 return (p->is_temp) ? p : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700512}
513
buzbeefa57c472012-11-21 12:06:18 -0800514RegisterInfo* IsPromoted(CompilationUnit* cu, int reg)
buzbeeb29e4d12011-09-26 15:05:48 -0700515{
buzbee02031b12012-11-23 09:41:35 -0800516 Codegen* cg = cu->cg.get();
517 RegisterInfo* p = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800518 return (p->is_temp) ? NULL : p;
buzbeeb29e4d12011-09-26 15:05:48 -0700519}
520
buzbeefa57c472012-11-21 12:06:18 -0800521bool IsDirty(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700522{
buzbee02031b12012-11-23 09:41:35 -0800523 Codegen* cg = cu->cg.get();
524 RegisterInfo* p = cg->GetRegInfo(cu, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 return p->dirty;
buzbee67bf8852011-08-17 17:51:35 -0700526}
527
528/*
buzbee52a77fc2012-11-20 19:50:46 -0800529 * Similar to AllocTemp(), but forces the allocation of a specific
buzbee67bf8852011-08-17 17:51:35 -0700530 * register. No check is made to see if the register was previously
531 * allocated. Use with caution.
532 */
buzbeefa57c472012-11-21 12:06:18 -0800533void LockTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700534{
buzbeefa57c472012-11-21 12:06:18 -0800535 RegisterInfo* p = cu->reg_pool->core_regs;
536 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 int i;
buzbeefa57c472012-11-21 12:06:18 -0800538 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700539 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800540 DCHECK(p[i].is_temp);
541 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 p[i].live = false;
543 return;
buzbee67bf8852011-08-17 17:51:35 -0700544 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 }
buzbeefa57c472012-11-21 12:06:18 -0800546 p = cu->reg_pool->FPRegs;
547 num_regs = cu->reg_pool->num_fp_regs;
548 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 }
556 LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700557}
558
buzbeeaad94382012-11-21 07:40:50 -0800559static void ResetDefBody(RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -0700560{
buzbeefa57c472012-11-21 12:06:18 -0800561 p->def_start = NULL;
562 p->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700563}
564
buzbeefa57c472012-11-21 12:06:18 -0800565void ResetDef(CompilationUnit* cu, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800566{
buzbee02031b12012-11-23 09:41:35 -0800567 Codegen* cg = cu->cg.get();
568 ResetDefBody(cg->GetRegInfo(cu, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800569}
570
buzbeefa57c472012-11-21 12:06:18 -0800571static void NullifyRange(CompilationUnit* cu, LIR *start, LIR *finish, int s_reg1, int s_reg2)
buzbee67bf8852011-08-17 17:51:35 -0700572{
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 if (start && finish) {
574 LIR *p;
buzbeefa57c472012-11-21 12:06:18 -0800575 DCHECK_EQ(s_reg1, s_reg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 for (p = start; ;p = p->next) {
buzbee52a77fc2012-11-20 19:50:46 -0800577 NopLIR(p);
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 if (p == finish)
579 break;
buzbee67bf8852011-08-17 17:51:35 -0700580 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 }
buzbee67bf8852011-08-17 17:51:35 -0700582}
583
584/*
585 * Mark the beginning and end LIR of a def sequence. Note that
586 * on entry start points to the LIR prior to the beginning of the
587 * sequence.
588 */
buzbeefa57c472012-11-21 12:06:18 -0800589void MarkDef(CompilationUnit* cu, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700590 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700591{
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 DCHECK(!rl.wide);
593 DCHECK(start && start->next);
594 DCHECK(finish);
buzbee02031b12012-11-23 09:41:35 -0800595 Codegen* cg = cu->cg.get();
596 RegisterInfo* p = cg->GetRegInfo(cu, rl.low_reg);
buzbeefa57c472012-11-21 12:06:18 -0800597 p->def_start = start->next;
598 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700599}
600
601/*
602 * Mark the beginning and end LIR of a def sequence. Note that
603 * on entry start points to the LIR prior to the beginning of the
604 * sequence.
605 */
buzbeefa57c472012-11-21 12:06:18 -0800606void MarkDefWide(CompilationUnit* cu, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700608{
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 DCHECK(rl.wide);
610 DCHECK(start && start->next);
611 DCHECK(finish);
buzbee02031b12012-11-23 09:41:35 -0800612 Codegen* cg = cu->cg.get();
613 RegisterInfo* p = cg->GetRegInfo(cu, rl.low_reg);
buzbeefa57c472012-11-21 12:06:18 -0800614 ResetDef(cu, rl.high_reg); // Only track low of pair
615 p->def_start = start->next;
616 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700617}
618
buzbeefa57c472012-11-21 12:06:18 -0800619RegLocation WideToNarrow(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700620{
Bill Buzbeea114add2012-05-03 15:00:40 -0700621 DCHECK(rl.wide);
buzbee02031b12012-11-23 09:41:35 -0800622 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 if (rl.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800624 RegisterInfo* info_lo = cg->GetRegInfo(cu, rl.low_reg);
625 RegisterInfo* info_hi = cg->GetRegInfo(cu, rl.high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800626 if (info_lo->is_temp) {
627 info_lo->pair = false;
628 info_lo->def_start = NULL;
629 info_lo->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700630 }
buzbeefa57c472012-11-21 12:06:18 -0800631 if (info_hi->is_temp) {
632 info_hi->pair = false;
633 info_hi->def_start = NULL;
634 info_hi->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 }
636 }
637 rl.wide = false;
638 return rl;
buzbee67bf8852011-08-17 17:51:35 -0700639}
640
buzbeefa57c472012-11-21 12:06:18 -0800641void ResetDefLoc(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700642{
Bill Buzbeea114add2012-05-03 15:00:40 -0700643 DCHECK(!rl.wide);
buzbeefa57c472012-11-21 12:06:18 -0800644 RegisterInfo* p = IsTemp(cu, rl.low_reg);
645 if (p && !(cu->disable_opt & (1 << kSuppressLoads))) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 DCHECK(!p->pair);
buzbeefa57c472012-11-21 12:06:18 -0800647 NullifyRange(cu, p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 }
buzbeefa57c472012-11-21 12:06:18 -0800649 ResetDef(cu, rl.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700650}
651
buzbeefa57c472012-11-21 12:06:18 -0800652void ResetDefLocWide(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700653{
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 DCHECK(rl.wide);
buzbeefa57c472012-11-21 12:06:18 -0800655 RegisterInfo* p_low = IsTemp(cu, rl.low_reg);
656 RegisterInfo* p_high = IsTemp(cu, rl.high_reg);
657 if (p_low && !(cu->disable_opt & (1 << kSuppressLoads))) {
658 DCHECK(p_low->pair);
659 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 -0700660 }
buzbeefa57c472012-11-21 12:06:18 -0800661 if (p_high && !(cu->disable_opt & (1 << kSuppressLoads))) {
662 DCHECK(p_high->pair);
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 }
buzbeefa57c472012-11-21 12:06:18 -0800664 ResetDef(cu, rl.low_reg);
665 ResetDef(cu, rl.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700666}
667
buzbeefa57c472012-11-21 12:06:18 -0800668void ResetDefTracking(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700669{
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 int i;
buzbeefa57c472012-11-21 12:06:18 -0800671 for (i=0; i< cu->reg_pool->num_core_regs; i++) {
672 ResetDefBody(&cu->reg_pool->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700673 }
buzbeefa57c472012-11-21 12:06:18 -0800674 for (i=0; i< cu->reg_pool->num_fp_regs; i++) {
675 ResetDefBody(&cu->reg_pool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700676 }
buzbee67bf8852011-08-17 17:51:35 -0700677}
678
buzbeefa57c472012-11-21 12:06:18 -0800679void ClobberAllRegs(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700680{
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 int i;
buzbeefa57c472012-11-21 12:06:18 -0800682 for (i=0; i< cu->reg_pool->num_core_regs; i++) {
683 ClobberBody(cu, &cu->reg_pool->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700684 }
buzbeefa57c472012-11-21 12:06:18 -0800685 for (i=0; i< cu->reg_pool->num_fp_regs; i++) {
686 ClobberBody(cu, &cu->reg_pool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700687 }
buzbee67bf8852011-08-17 17:51:35 -0700688}
689
buzbee67bf8852011-08-17 17:51:35 -0700690// Make sure nothing is live and dirty
buzbeefa57c472012-11-21 12:06:18 -0800691static void FlushAllRegsBody(CompilationUnit* cu, RegisterInfo* info, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -0700692{
buzbee02031b12012-11-23 09:41:35 -0800693 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700694 int i;
buzbeefa57c472012-11-21 12:06:18 -0800695 for (i=0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700696 if (info[i].live && info[i].dirty) {
697 if (info[i].pair) {
buzbee02031b12012-11-23 09:41:35 -0800698 cg->FlushRegWide(cu, info[i].reg, info[i].partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 } else {
buzbee02031b12012-11-23 09:41:35 -0800700 cg->FlushReg(cu, info[i].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 }
buzbee67bf8852011-08-17 17:51:35 -0700702 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700703 }
buzbee67bf8852011-08-17 17:51:35 -0700704}
705
buzbeefa57c472012-11-21 12:06:18 -0800706void FlushAllRegs(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700707{
buzbeefa57c472012-11-21 12:06:18 -0800708 FlushAllRegsBody(cu, cu->reg_pool->core_regs,
709 cu->reg_pool->num_core_regs);
710 FlushAllRegsBody(cu, cu->reg_pool->FPRegs,
711 cu->reg_pool->num_fp_regs);
712 ClobberAllRegs(cu);
buzbee67bf8852011-08-17 17:51:35 -0700713}
714
715
716//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbee02031b12012-11-23 09:41:35 -0800717static bool RegClassMatches(CompilationUnit* cu, int reg_class, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700718{
buzbee02031b12012-11-23 09:41:35 -0800719 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800720 if (reg_class == kAnyReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 return true;
buzbeefa57c472012-11-21 12:06:18 -0800722 } else if (reg_class == kCoreReg) {
buzbee02031b12012-11-23 09:41:35 -0800723 return !cg->IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 } else {
buzbee02031b12012-11-23 09:41:35 -0800725 return cg->IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700726 }
buzbee67bf8852011-08-17 17:51:35 -0700727}
728
buzbeefa57c472012-11-21 12:06:18 -0800729void MarkLive(CompilationUnit* cu, int reg, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700730{
buzbee02031b12012-11-23 09:41:35 -0800731 Codegen* cg = cu->cg.get();
732 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800733 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700734 return; /* already live */
buzbeefa57c472012-11-21 12:06:18 -0800735 } else if (s_reg != INVALID_SREG) {
736 ClobberSReg(cu, s_reg);
737 if (info->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 info->live = true;
buzbee67bf8852011-08-17 17:51:35 -0700739 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700740 } else {
buzbeefa57c472012-11-21 12:06:18 -0800741 /* Can't be live if no associated s_reg */
742 DCHECK(info->is_temp);
Bill Buzbeea114add2012-05-03 15:00:40 -0700743 info->live = false;
744 }
buzbeefa57c472012-11-21 12:06:18 -0800745 info->s_reg = s_reg;
buzbee67bf8852011-08-17 17:51:35 -0700746}
747
buzbeefa57c472012-11-21 12:06:18 -0800748void MarkTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700749{
buzbee02031b12012-11-23 09:41:35 -0800750 Codegen* cg = cu->cg.get();
751 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800752 info->is_temp = true;
buzbee67bf8852011-08-17 17:51:35 -0700753}
754
buzbeefa57c472012-11-21 12:06:18 -0800755void UnmarkTemp(CompilationUnit* cu, int reg)
buzbee9e0f9b02011-08-24 15:32:46 -0700756{
buzbee02031b12012-11-23 09:41:35 -0800757 Codegen* cg = cu->cg.get();
758 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800759 info->is_temp = false;
buzbee9e0f9b02011-08-24 15:32:46 -0700760}
761
buzbeefa57c472012-11-21 12:06:18 -0800762void MarkPair(CompilationUnit* cu, int low_reg, int high_reg)
buzbee67bf8852011-08-17 17:51:35 -0700763{
buzbee02031b12012-11-23 09:41:35 -0800764 Codegen* cg = cu->cg.get();
765 RegisterInfo* info_lo = cg->GetRegInfo(cu, low_reg);
766 RegisterInfo* info_hi = cg->GetRegInfo(cu, high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800767 info_lo->pair = info_hi->pair = true;
768 info_lo->partner = high_reg;
769 info_hi->partner = low_reg;
buzbee67bf8852011-08-17 17:51:35 -0700770}
771
buzbeefa57c472012-11-21 12:06:18 -0800772void MarkClean(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700773{
buzbee02031b12012-11-23 09:41:35 -0800774 Codegen* cg = cu->cg.get();
775 RegisterInfo* info = cg->GetRegInfo(cu, loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700776 info->dirty = false;
777 if (loc.wide) {
buzbee02031b12012-11-23 09:41:35 -0800778 info = cg->GetRegInfo(cu, loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700779 info->dirty = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700780 }
buzbee67bf8852011-08-17 17:51:35 -0700781}
782
buzbeefa57c472012-11-21 12:06:18 -0800783void MarkDirty(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700784{
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 if (loc.home) {
786 // If already home, can't be dirty
787 return;
788 }
buzbee02031b12012-11-23 09:41:35 -0800789 Codegen* cg = cu->cg.get();
790 RegisterInfo* info = cg->GetRegInfo(cu, loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700791 info->dirty = true;
792 if (loc.wide) {
buzbee02031b12012-11-23 09:41:35 -0800793 info = cg->GetRegInfo(cu, loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700794 info->dirty = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700795 }
buzbee67bf8852011-08-17 17:51:35 -0700796}
797
buzbeefa57c472012-11-21 12:06:18 -0800798void MarkInUse(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700799{
buzbee02031b12012-11-23 09:41:35 -0800800 Codegen* cg = cu->cg.get();
801 RegisterInfo* info = cg->GetRegInfo(cu, reg);
buzbeefa57c472012-11-21 12:06:18 -0800802 info->in_use = true;
buzbee67bf8852011-08-17 17:51:35 -0700803}
804
buzbeefa57c472012-11-21 12:06:18 -0800805static void CopyRegInfo(CompilationUnit* cu, int new_reg, int old_reg)
buzbee67bf8852011-08-17 17:51:35 -0700806{
buzbee02031b12012-11-23 09:41:35 -0800807 Codegen* cg = cu->cg.get();
808 RegisterInfo* new_info = cg->GetRegInfo(cu, new_reg);
809 RegisterInfo* old_info = cg->GetRegInfo(cu, old_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700810 // Target temp status must not change
buzbeefa57c472012-11-21 12:06:18 -0800811 bool is_temp = new_info->is_temp;
812 *new_info = *old_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700813 // Restore target's temp status
buzbeefa57c472012-11-21 12:06:18 -0800814 new_info->is_temp = is_temp;
815 new_info->reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700816}
817
buzbeefa57c472012-11-21 12:06:18 -0800818static bool CheckCorePoolSanity(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -0700819{
buzbee02031b12012-11-23 09:41:35 -0800820 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800821 for (static int i = 0; i < cu->reg_pool->num_core_regs; i++) {
822 if (cu->reg_pool->core_regs[i].pair) {
823 static int my_reg = cu->reg_pool->core_regs[i].reg;
824 static int my_sreg = cu->reg_pool->core_regs[i].s_reg;
825 static int partner_reg = cu->reg_pool->core_regs[i].partner;
buzbee02031b12012-11-23 09:41:35 -0800826 static RegisterInfo* partner = cg->GetRegInfo(cu, partner_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 DCHECK(partner != NULL);
828 DCHECK(partner->pair);
buzbeefa57c472012-11-21 12:06:18 -0800829 DCHECK_EQ(my_reg, partner->partner);
830 static int partner_sreg = partner->s_reg;
831 if (my_sreg == INVALID_SREG) {
832 DCHECK_EQ(partner_sreg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700833 } else {
buzbeefa57c472012-11-21 12:06:18 -0800834 int diff = my_sreg - partner_sreg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 DCHECK((diff == -1) || (diff == 1));
buzbee6181f792011-09-29 11:14:04 -0700836 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 }
buzbeefa57c472012-11-21 12:06:18 -0800838 if (!cu->reg_pool->core_regs[i].live) {
839 DCHECK(cu->reg_pool->core_regs[i].def_start == NULL);
840 DCHECK(cu->reg_pool->core_regs[i].def_end == NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700841 }
buzbee6181f792011-09-29 11:14:04 -0700842 }
843 return true;
844}
845
buzbeeaad94382012-11-21 07:40:50 -0800846/*
847 * Return an updated location record with current in-register status.
848 * If the value lives in live temps, reflect that fact. No code
849 * is generated. If the live value is part of an older pair,
850 * clobber both low and high.
851 * TUNING: clobbering both is a bit heavy-handed, but the alternative
852 * is a bit complex when dealing with FP regs. Examine code to see
853 * if it's worthwhile trying to be more clever here.
854 */
855
buzbeefa57c472012-11-21 12:06:18 -0800856RegLocation UpdateLoc(CompilationUnit* cu, RegLocation loc)
buzbeeaad94382012-11-21 07:40:50 -0800857{
858 DCHECK(!loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800859 DCHECK(CheckCorePoolSanity(cu));
buzbeeaad94382012-11-21 07:40:50 -0800860 if (loc.location != kLocPhysReg) {
861 DCHECK((loc.location == kLocDalvikFrame) ||
862 (loc.location == kLocCompilerTemp));
buzbeefa57c472012-11-21 12:06:18 -0800863 RegisterInfo* info_lo = AllocLive(cu, loc.s_reg_low, kAnyReg);
864 if (info_lo) {
865 if (info_lo->pair) {
866 Clobber(cu, info_lo->reg);
867 Clobber(cu, info_lo->partner);
868 FreeTemp(cu, info_lo->reg);
buzbeeaad94382012-11-21 07:40:50 -0800869 } else {
buzbeefa57c472012-11-21 12:06:18 -0800870 loc.low_reg = info_lo->reg;
buzbeeaad94382012-11-21 07:40:50 -0800871 loc.location = kLocPhysReg;
872 }
873 }
874 }
875
876 return loc;
877}
878
buzbeefa57c472012-11-21 12:06:18 -0800879/* see comments for update_loc */
880RegLocation UpdateLocWide(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700881{
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800883 DCHECK(CheckCorePoolSanity(cu));
buzbee02031b12012-11-23 09:41:35 -0800884 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 if (loc.location != kLocPhysReg) {
886 DCHECK((loc.location == kLocDalvikFrame) ||
887 (loc.location == kLocCompilerTemp));
888 // Are the dalvik regs already live in physical registers?
buzbeefa57c472012-11-21 12:06:18 -0800889 RegisterInfo* info_lo = AllocLive(cu, loc.s_reg_low, kAnyReg);
890 RegisterInfo* info_hi = AllocLive(cu,
891 GetSRegHi(loc.s_reg_low), kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700892 bool match = true;
buzbeefa57c472012-11-21 12:06:18 -0800893 match = match && (info_lo != NULL);
894 match = match && (info_hi != NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 // Are they both core or both FP?
buzbee02031b12012-11-23 09:41:35 -0800896 match = match && (cg->IsFpReg(info_lo->reg) == cg->IsFpReg(info_hi->reg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700897 // If a pair of floating point singles, are they properly aligned?
buzbee02031b12012-11-23 09:41:35 -0800898 if (match && cg->IsFpReg(info_lo->reg)) {
buzbeefa57c472012-11-21 12:06:18 -0800899 match &= ((info_lo->reg & 0x1) == 0);
900 match &= ((info_hi->reg - info_lo->reg) == 1);
buzbee67bf8852011-08-17 17:51:35 -0700901 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 // If previously used as a pair, it is the same pair?
buzbeefa57c472012-11-21 12:06:18 -0800903 if (match && (info_lo->pair || info_hi->pair)) {
904 match = (info_lo->pair == info_hi->pair);
905 match &= ((info_lo->reg == info_hi->partner) &&
906 (info_hi->reg == info_lo->partner));
Bill Buzbeea114add2012-05-03 15:00:40 -0700907 }
908 if (match) {
909 // Can reuse - update the register usage info
buzbeefa57c472012-11-21 12:06:18 -0800910 loc.low_reg = info_lo->reg;
911 loc.high_reg = info_hi->reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800913 MarkPair(cu, loc.low_reg, loc.high_reg);
buzbee02031b12012-11-23 09:41:35 -0800914 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700915 return loc;
916 }
917 // Can't easily reuse - clobber and free any overlaps
buzbeefa57c472012-11-21 12:06:18 -0800918 if (info_lo) {
919 Clobber(cu, info_lo->reg);
920 FreeTemp(cu, info_lo->reg);
921 if (info_lo->pair)
922 Clobber(cu, info_lo->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700923 }
buzbeefa57c472012-11-21 12:06:18 -0800924 if (info_hi) {
925 Clobber(cu, info_hi->reg);
926 FreeTemp(cu, info_hi->reg);
927 if (info_hi->pair)
928 Clobber(cu, info_hi->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 }
930 }
931 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700932}
933
buzbeeed3e9302011-09-23 17:34:19 -0700934
935/* For use in cases we don't know (or care) width */
buzbeefa57c472012-11-21 12:06:18 -0800936RegLocation UpdateRawLoc(CompilationUnit* cu, RegLocation loc)
buzbeeed3e9302011-09-23 17:34:19 -0700937{
Bill Buzbeea114add2012-05-03 15:00:40 -0700938 if (loc.wide)
buzbeefa57c472012-11-21 12:06:18 -0800939 return UpdateLocWide(cu, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 else
buzbeefa57c472012-11-21 12:06:18 -0800941 return UpdateLoc(cu, loc);
buzbeeed3e9302011-09-23 17:34:19 -0700942}
943
buzbeefa57c472012-11-21 12:06:18 -0800944RegLocation EvalLocWide(CompilationUnit* cu, RegLocation loc, int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700945{
Bill Buzbeea114add2012-05-03 15:00:40 -0700946 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800947 int new_regs;
948 int low_reg;
949 int high_reg;
buzbee02031b12012-11-23 09:41:35 -0800950 Codegen* cg = cu->cg.get();
buzbee67bf8852011-08-17 17:51:35 -0700951
buzbeefa57c472012-11-21 12:06:18 -0800952 loc = UpdateLocWide(cu, loc);
buzbee67bf8852011-08-17 17:51:35 -0700953
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 /* If already in registers, we can assume proper form. Right reg class? */
955 if (loc.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800956 DCHECK_EQ(cg->IsFpReg(loc.low_reg), cg->IsFpReg(loc.high_reg));
957 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
958 if (!RegClassMatches(cu, reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700959 /* Wrong register class. Reallocate and copy */
buzbee02031b12012-11-23 09:41:35 -0800960 new_regs = cg->AllocTypedTempPair(cu, loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800961 low_reg = new_regs & 0xff;
962 high_reg = (new_regs >> 8) & 0xff;
buzbee02031b12012-11-23 09:41:35 -0800963 cg->OpRegCopyWide(cu, low_reg, high_reg, loc.low_reg, loc.high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800964 CopyRegInfo(cu, low_reg, loc.low_reg);
965 CopyRegInfo(cu, high_reg, loc.high_reg);
966 Clobber(cu, loc.low_reg);
967 Clobber(cu, loc.high_reg);
968 loc.low_reg = low_reg;
969 loc.high_reg = high_reg;
970 MarkPair(cu, loc.low_reg, loc.high_reg);
buzbee02031b12012-11-23 09:41:35 -0800971 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 }
buzbee67bf8852011-08-17 17:51:35 -0700973 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700974 }
975
buzbeefa57c472012-11-21 12:06:18 -0800976 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
977 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700978
buzbee02031b12012-11-23 09:41:35 -0800979 new_regs = cg->AllocTypedTempPair(cu, loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800980 loc.low_reg = new_regs & 0xff;
981 loc.high_reg = (new_regs >> 8) & 0xff;
Bill Buzbeea114add2012-05-03 15:00:40 -0700982
buzbeefa57c472012-11-21 12:06:18 -0800983 MarkPair(cu, loc.low_reg, loc.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700984 if (update) {
985 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800986 MarkLive(cu, loc.low_reg, loc.s_reg_low);
987 MarkLive(cu, loc.high_reg, GetSRegHi(loc.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 }
buzbee02031b12012-11-23 09:41:35 -0800989 DCHECK(!cg->IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700990 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700991}
992
buzbeefa57c472012-11-21 12:06:18 -0800993RegLocation EvalLoc(CompilationUnit* cu, RegLocation loc,
994 int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700995{
buzbeefa57c472012-11-21 12:06:18 -0800996 int new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700997
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 if (loc.wide)
buzbeefa57c472012-11-21 12:06:18 -0800999 return EvalLocWide(cu, loc, reg_class, update);
buzbee67bf8852011-08-17 17:51:35 -07001000
buzbee02031b12012-11-23 09:41:35 -08001001 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08001002 loc = UpdateLoc(cu, loc);
buzbee67bf8852011-08-17 17:51:35 -07001003
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 if (loc.location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -08001005 if (!RegClassMatches(cu, reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001006 /* Wrong register class. Realloc, copy and transfer ownership */
buzbee02031b12012-11-23 09:41:35 -08001007 new_reg = cg->AllocTypedTemp(cu, loc.fp, reg_class);
1008 cg->OpRegCopy(cu, new_reg, loc.low_reg);
buzbeefa57c472012-11-21 12:06:18 -08001009 CopyRegInfo(cu, new_reg, loc.low_reg);
1010 Clobber(cu, loc.low_reg);
1011 loc.low_reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -07001012 }
1013 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 }
1015
buzbeefa57c472012-11-21 12:06:18 -08001016 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -07001017
buzbee02031b12012-11-23 09:41:35 -08001018 new_reg = cg->AllocTypedTemp(cu, loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -08001019 loc.low_reg = new_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001020
1021 if (update) {
1022 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001023 MarkLive(cu, loc.low_reg, loc.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 }
1025 return loc;
buzbee67bf8852011-08-17 17:51:35 -07001026}
1027
buzbeefa57c472012-11-21 12:06:18 -08001028RegLocation GetRawSrc(CompilationUnit* cu, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001029{
buzbeefa57c472012-11-21 12:06:18 -08001030 DCHECK(num < mir->ssa_rep->num_uses);
1031 RegLocation res = cu->reg_location[mir->ssa_rep->uses[num]];
buzbee15bf9802012-06-12 17:49:27 -07001032 return res;
1033}
buzbeeeaf09bc2012-11-15 14:51:41 -08001034
buzbeefa57c472012-11-21 12:06:18 -08001035RegLocation GetRawDest(CompilationUnit* cu, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001036{
buzbeefa57c472012-11-21 12:06:18 -08001037 DCHECK_GT(mir->ssa_rep->num_defs, 0);
1038 RegLocation res = cu->reg_location[mir->ssa_rep->defs[0]];
buzbee15bf9802012-06-12 17:49:27 -07001039 return res;
1040}
buzbeeeaf09bc2012-11-15 14:51:41 -08001041
buzbeefa57c472012-11-21 12:06:18 -08001042RegLocation GetDest(CompilationUnit* cu, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001043{
buzbeefa57c472012-11-21 12:06:18 -08001044 RegLocation res = GetRawDest(cu, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 DCHECK(!res.wide);
1046 return res;
buzbee67bf8852011-08-17 17:51:35 -07001047}
buzbeeeaf09bc2012-11-15 14:51:41 -08001048
buzbeefa57c472012-11-21 12:06:18 -08001049RegLocation GetSrc(CompilationUnit* cu, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001050{
buzbeefa57c472012-11-21 12:06:18 -08001051 RegLocation res = GetRawSrc(cu, mir, num);
Bill Buzbeea114add2012-05-03 15:00:40 -07001052 DCHECK(!res.wide);
1053 return res;
buzbeee9a72f62011-09-04 17:59:07 -07001054}
buzbeeeaf09bc2012-11-15 14:51:41 -08001055
buzbeefa57c472012-11-21 12:06:18 -08001056RegLocation GetDestWide(CompilationUnit* cu, MIR* mir)
buzbeee9a72f62011-09-04 17:59:07 -07001057{
buzbeefa57c472012-11-21 12:06:18 -08001058 RegLocation res = GetRawDest(cu, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 DCHECK(res.wide);
1060 return res;
buzbee67bf8852011-08-17 17:51:35 -07001061}
1062
buzbeefa57c472012-11-21 12:06:18 -08001063RegLocation GetSrcWide(CompilationUnit* cu, MIR* mir,
buzbee15bf9802012-06-12 17:49:27 -07001064 int low)
buzbee67bf8852011-08-17 17:51:35 -07001065{
buzbeefa57c472012-11-21 12:06:18 -08001066 RegLocation res = GetRawSrc(cu, mir, low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001067 DCHECK(res.wide);
1068 return res;
buzbee67bf8852011-08-17 17:51:35 -07001069}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001070
buzbeefa57c472012-11-21 12:06:18 -08001071/* USE SSA names to count references of base Dalvik v_regs. */
1072static void CountRefs(CompilationUnit *cu, BasicBlock* bb, RefCounts* core_counts,
1073 RefCounts* fp_counts)
buzbeee3acd072012-02-25 17:03:10 -08001074{
buzbeefa57c472012-11-21 12:06:18 -08001075 if ((cu->disable_opt & (1 << kPromoteRegs)) ||
1076 !((bb->block_type == kEntryBlock) || (bb->block_type == kExitBlock) ||
1077 (bb->block_type == kDalvikByteCode))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001078 return;
1079 }
buzbeefa57c472012-11-21 12:06:18 -08001080 for (int i = 0; i < cu->num_ssa_regs;) {
1081 RegLocation loc = cu->reg_location[i];
1082 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1083 int p_map_idx = SRegToPMap(cu, loc.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001084 if (loc.defined) {
buzbeefa57c472012-11-21 12:06:18 -08001085 counts[p_map_idx].count += cu->use_counts.elem_list[i];
buzbee239c4e72012-03-16 08:42:29 -07001086 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001087 if (loc.wide) {
1088 if (loc.defined) {
1089 if (loc.fp) {
buzbeefa57c472012-11-21 12:06:18 -08001090 counts[p_map_idx].double_start = true;
1091 counts[p_map_idx+1].count += cu->use_counts.elem_list[i+1];
buzbee239c4e72012-03-16 08:42:29 -07001092 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001093 }
1094 i += 2;
1095 } else {
1096 i++;
buzbeee3acd072012-02-25 17:03:10 -08001097 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001098 }
buzbeee3acd072012-02-25 17:03:10 -08001099}
1100
1101/* qsort callback function, sort descending */
buzbeeaad94382012-11-21 07:40:50 -08001102static int SortCounts(const void *val1, const void *val2)
buzbeee3acd072012-02-25 17:03:10 -08001103{
buzbeecbd6d442012-11-17 14:11:25 -08001104 const RefCounts* op1 = reinterpret_cast<const RefCounts*>(val1);
1105 const RefCounts* op2 = reinterpret_cast<const RefCounts*>(val2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001106 return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
buzbeee3acd072012-02-25 17:03:10 -08001107}
1108
buzbeeaad94382012-11-21 07:40:50 -08001109static void DumpCounts(const RefCounts* arr, int size, const char* msg)
buzbeee3acd072012-02-25 17:03:10 -08001110{
Bill Buzbeea114add2012-05-03 15:00:40 -07001111 LOG(INFO) << msg;
1112 for (int i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001113 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 }
buzbeee3acd072012-02-25 17:03:10 -08001115}
1116
1117/*
1118 * Note: some portions of this code required even if the kPromoteRegs
1119 * optimization is disabled.
1120 */
buzbeefa57c472012-11-21 12:06:18 -08001121void DoPromotion(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -08001122{
buzbee02031b12012-11-23 09:41:35 -08001123 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -08001124 int reg_bias = cu->num_compiler_temps + 1;
1125 int dalvik_regs = cu->num_dalvik_registers;
1126 int num_regs = dalvik_regs + reg_bias;
1127 const int promotion_threshold = 2;
buzbeee3acd072012-02-25 17:03:10 -08001128
Bill Buzbeea114add2012-05-03 15:00:40 -07001129 // Allow target code to add any special registers
buzbee02031b12012-11-23 09:41:35 -08001130 cg->AdjustSpillMask(cu);
buzbeee3acd072012-02-25 17:03:10 -08001131
Bill Buzbeea114add2012-05-03 15:00:40 -07001132 /*
1133 * Simple register promotion. Just do a static count of the uses
1134 * of Dalvik registers. Note that we examine the SSA names, but
1135 * count based on original Dalvik register name. Count refs
1136 * separately based on type in order to give allocation
1137 * preference to fp doubles - which must be allocated sequential
1138 * physical single fp registers started with an even-numbered
1139 * reg.
1140 * TUNING: replace with linear scan once we have the ability
1141 * to describe register live ranges for GC.
1142 */
buzbeefa57c472012-11-21 12:06:18 -08001143 RefCounts *core_regs = static_cast<RefCounts*>(NewMem(cu, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001144 true, kAllocRegAlloc));
buzbeefa57c472012-11-21 12:06:18 -08001145 RefCounts *FpRegs = static_cast<RefCounts *>(NewMem(cu, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001146 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -07001147 // Set ssa names for original Dalvik registers
buzbeefa57c472012-11-21 12:06:18 -08001148 for (int i = 0; i < dalvik_regs; i++) {
1149 core_regs[i].s_reg = FpRegs[i].s_reg = i;
Bill Buzbeea114add2012-05-03 15:00:40 -07001150 }
1151 // Set ssa name for Method*
buzbeefa57c472012-11-21 12:06:18 -08001152 core_regs[dalvik_regs].s_reg = cu->method_sreg;
1153 FpRegs[dalvik_regs].s_reg = cu->method_sreg; // For consistecy
1154 // Set ssa names for compiler_temps
1155 for (int i = 1; i <= cu->num_compiler_temps; i++) {
1156 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cu->compiler_temps.elem_list[i]);
1157 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
1158 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001159 }
1160
1161 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001162 GrowableListIteratorInit(&cu->block_list, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 while (true) {
1164 BasicBlock* bb;
buzbee52a77fc2012-11-20 19:50:46 -08001165 bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -08001167 CountRefs(cu, bb, core_regs, FpRegs);
Bill Buzbeea114add2012-05-03 15:00:40 -07001168 }
1169
1170 /*
1171 * Ideally, we'd allocate doubles starting with an even-numbered
1172 * register. Bias the counts to try to allocate any vreg that's
1173 * used as the start of a pair first.
1174 */
buzbeefa57c472012-11-21 12:06:18 -08001175 for (int i = 0; i < num_regs; i++) {
1176 if (FpRegs[i].double_start) {
buzbee52a77fc2012-11-20 19:50:46 -08001177 FpRegs[i].count *= 2;
buzbeee3acd072012-02-25 17:03:10 -08001178 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 }
1180
1181 // Sort the count arrays
buzbeefa57c472012-11-21 12:06:18 -08001182 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
1183 qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts);
Bill Buzbeea114add2012-05-03 15:00:40 -07001184
buzbeefa57c472012-11-21 12:06:18 -08001185 if (cu->verbose) {
1186 DumpCounts(core_regs, num_regs, "Core regs after sort");
1187 DumpCounts(FpRegs, num_regs, "Fp regs after sort");
Bill Buzbeea114add2012-05-03 15:00:40 -07001188 }
1189
buzbeefa57c472012-11-21 12:06:18 -08001190 if (!(cu->disable_opt & (1 << kPromoteRegs))) {
buzbee52a77fc2012-11-20 19:50:46 -08001191 // Promote FpRegs
buzbeefa57c472012-11-21 12:06:18 -08001192 for (int i = 0; (i < num_regs) &&
1193 (FpRegs[i].count >= promotion_threshold ); i++) {
1194 int p_map_idx = SRegToPMap(cu, FpRegs[i].s_reg);
1195 if (cu->promotion_map[p_map_idx].fp_location != kLocPhysReg) {
1196 int reg = AllocPreservedFPReg(cu, FpRegs[i].s_reg,
1197 FpRegs[i].double_start);
Bill Buzbeea114add2012-05-03 15:00:40 -07001198 if (reg < 0) {
1199 break; // No more left
1200 }
1201 }
buzbee239c4e72012-03-16 08:42:29 -07001202 }
buzbee9c044ce2012-03-18 13:24:07 -07001203
Bill Buzbeea114add2012-05-03 15:00:40 -07001204 // Promote core regs
buzbeefa57c472012-11-21 12:06:18 -08001205 for (int i = 0; (i < num_regs) &&
1206 (core_regs[i].count > promotion_threshold); i++) {
1207 int p_map_idx = SRegToPMap(cu, core_regs[i].s_reg);
1208 if (cu->promotion_map[p_map_idx].core_location !=
Bill Buzbeea114add2012-05-03 15:00:40 -07001209 kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -08001210 int reg = AllocPreservedCoreReg(cu, core_regs[i].s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001211 if (reg < 0) {
1212 break; // No more left
1213 }
1214 }
buzbeee3acd072012-02-25 17:03:10 -08001215 }
buzbeefa57c472012-11-21 12:06:18 -08001216 } else if (cu->qd_mode) {
1217 AllocPreservedCoreReg(cu, cu->method_sreg);
1218 for (int i = 0; i < num_regs; i++) {
1219 int reg = AllocPreservedCoreReg(cu, i);
Bill Buzbeea114add2012-05-03 15:00:40 -07001220 if (reg < 0) {
1221 break; // No more left
1222 }
buzbeee3acd072012-02-25 17:03:10 -08001223 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 }
buzbeee3acd072012-02-25 17:03:10 -08001225
buzbeee3acd072012-02-25 17:03:10 -08001226
Bill Buzbeea114add2012-05-03 15:00:40 -07001227 // Now, update SSA names to new home locations
buzbeefa57c472012-11-21 12:06:18 -08001228 for (int i = 0; i < cu->num_ssa_regs; i++) {
1229 RegLocation *curr = &cu->reg_location[i];
1230 int p_map_idx = SRegToPMap(cu, curr->s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001231 if (!curr->wide) {
1232 if (curr->fp) {
buzbeefa57c472012-11-21 12:06:18 -08001233 if (cu->promotion_map[p_map_idx].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001234 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001235 curr->low_reg = cu->promotion_map[p_map_idx].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001236 curr->home = true;
1237 }
1238 } else {
buzbeefa57c472012-11-21 12:06:18 -08001239 if (cu->promotion_map[p_map_idx].core_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001240 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001241 curr->low_reg = cu->promotion_map[p_map_idx].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001242 curr->home = true;
1243 }
1244 }
buzbeefa57c472012-11-21 12:06:18 -08001245 curr->high_reg = INVALID_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -07001246 } else {
buzbeefa57c472012-11-21 12:06:18 -08001247 if (curr->high_word) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001248 continue;
1249 }
1250 if (curr->fp) {
buzbeefa57c472012-11-21 12:06:18 -08001251 if ((cu->promotion_map[p_map_idx].fp_location == kLocPhysReg) &&
1252 (cu->promotion_map[p_map_idx+1].fp_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001253 kLocPhysReg)) {
buzbeefa57c472012-11-21 12:06:18 -08001254 int low_reg = cu->promotion_map[p_map_idx].FpReg;
1255 int high_reg = cu->promotion_map[p_map_idx+1].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001256 // Doubles require pair of singles starting at even reg
buzbeefa57c472012-11-21 12:06:18 -08001257 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001258 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001259 curr->low_reg = low_reg;
1260 curr->high_reg = high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001261 curr->home = true;
1262 }
1263 }
1264 } else {
buzbeefa57c472012-11-21 12:06:18 -08001265 if ((cu->promotion_map[p_map_idx].core_location == kLocPhysReg)
1266 && (cu->promotion_map[p_map_idx+1].core_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001267 kLocPhysReg)) {
1268 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001269 curr->low_reg = cu->promotion_map[p_map_idx].core_reg;
1270 curr->high_reg = cu->promotion_map[p_map_idx+1].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001271 curr->home = true;
1272 }
1273 }
buzbeee3acd072012-02-25 17:03:10 -08001274 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001275 }
buzbeefa57c472012-11-21 12:06:18 -08001276 if (cu->verbose) {
1277 DumpPromotionMap(cu);
buzbeeca7a5e42012-08-20 11:12:18 -07001278 }
buzbeee3acd072012-02-25 17:03:10 -08001279}
1280
1281/* Returns sp-relative offset in bytes for a VReg */
buzbeefa57c472012-11-21 12:06:18 -08001282int VRegOffset(CompilationUnit* cu, int v_reg)
buzbeee3acd072012-02-25 17:03:10 -08001283{
buzbeefa57c472012-11-21 12:06:18 -08001284 return StackVisitor::GetVRegOffset(cu->code_item, cu->core_spill_mask,
1285 cu->fp_spill_mask, cu->frame_size, v_reg);
buzbeee3acd072012-02-25 17:03:10 -08001286}
1287
1288/* Returns sp-relative offset in bytes for a SReg */
buzbeefa57c472012-11-21 12:06:18 -08001289int SRegOffset(CompilationUnit* cu, int s_reg)
buzbeee3acd072012-02-25 17:03:10 -08001290{
buzbeefa57c472012-11-21 12:06:18 -08001291 return VRegOffset(cu, SRegToVReg(cu, s_reg));
buzbeee3acd072012-02-25 17:03:10 -08001292}
1293
buzbee02031b12012-11-23 09:41:35 -08001294RegLocation GetBadLoc()
1295{
1296 RegLocation res = bad_loc;
1297 return res;
1298}
1299
1300/* Mark register usage state and return long retloc */
1301RegLocation GetReturnWide(CompilationUnit* cu, bool is_double)
1302{
1303 Codegen* cg = cu->cg.get();
1304 RegLocation gpr_res = cg->LocCReturnWide();
1305 RegLocation fpr_res = cg->LocCReturnDouble();
1306 RegLocation res = is_double ? fpr_res : gpr_res;
1307 Clobber(cu, res.low_reg);
1308 Clobber(cu, res.high_reg);
1309 LockTemp(cu, res.low_reg);
1310 LockTemp(cu, res.high_reg);
1311 MarkPair(cu, res.low_reg, res.high_reg);
1312 return res;
1313}
1314
1315RegLocation GetReturn(CompilationUnit* cu, bool is_float)
1316{
1317 Codegen* cg = cu->cg.get();
1318 RegLocation gpr_res = cg->LocCReturn();
1319 RegLocation fpr_res = cg->LocCReturnFloat();
1320 RegLocation res = is_float ? fpr_res : gpr_res;
1321 Clobber(cu, res.low_reg);
1322 if (cu->instruction_set == kMips) {
1323 MarkInUse(cu, res.low_reg);
1324 } else {
1325 LockTemp(cu, res.low_reg);
1326 }
1327 return res;
1328}
1329
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001330} // namespace art