blob: a26e0cd42022b6900d82749169c2b7ebf0398c45 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbeeb046e162012-10-30 15:48:42 -070017/* This file contains register alloction support. */
buzbee67bf8852011-08-17 17:51:35 -070018
buzbeeefc63692012-11-14 16:31:52 -080019#include "../compiler_utility.h"
20#include "../compiler_ir.h"
21#include "../dataflow.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080022#include "ralloc_util.h"
23#include "codegen_util.h"
buzbee67bf8852011-08-17 17:51:35 -070024
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
buzbee67bf8852011-08-17 17:51:35 -070027/*
28 * Free all allocated temps in the temp pools. Note that this does
29 * not affect the "liveness" of a temp register, which will stay
30 * live until it is either explicitly killed or reallocated.
31 */
buzbeefa57c472012-11-21 12:06:18 -080032void ResetRegPool(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -070033{
Bill Buzbeea114add2012-05-03 15:00:40 -070034 int i;
buzbeefa57c472012-11-21 12:06:18 -080035 for (i=0; i < cu->reg_pool->num_core_regs; i++) {
36 if (cu->reg_pool->core_regs[i].is_temp)
37 cu->reg_pool->core_regs[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070038 }
buzbeefa57c472012-11-21 12:06:18 -080039 for (i=0; i < cu->reg_pool->num_fp_regs; i++) {
40 if (cu->reg_pool->FPRegs[i].is_temp)
41 cu->reg_pool->FPRegs[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070042 }
buzbee67bf8852011-08-17 17:51:35 -070043}
44
buzbeee3acd072012-02-25 17:03:10 -080045 /*
46 * Set up temp & preserved register pools specialized by target.
buzbeefa57c472012-11-21 12:06:18 -080047 * Note: num_regs may be zero.
buzbeee3acd072012-02-25 17:03:10 -080048 */
buzbeefa57c472012-11-21 12:06:18 -080049void CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num)
buzbee67bf8852011-08-17 17:51:35 -070050{
Bill Buzbeea114add2012-05-03 15:00:40 -070051 int i;
52 for (i=0; i < num; i++) {
buzbeefa57c472012-11-21 12:06:18 -080053 regs[i].reg = reg_nums[i];
54 regs[i].in_use = false;
55 regs[i].is_temp = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070056 regs[i].pair = false;
57 regs[i].live = false;
58 regs[i].dirty = false;
buzbeefa57c472012-11-21 12:06:18 -080059 regs[i].s_reg = INVALID_SREG;
Bill Buzbeea114add2012-05-03 15:00:40 -070060 }
buzbee67bf8852011-08-17 17:51:35 -070061}
62
buzbeefa57c472012-11-21 12:06:18 -080063static void DumpRegPool(RegisterInfo* p, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -070064{
Bill Buzbeea114add2012-05-03 15:00:40 -070065 LOG(INFO) << "================================================";
buzbeefa57c472012-11-21 12:06:18 -080066 for (int i = 0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -070067 LOG(INFO) << StringPrintf(
68 "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 -080069 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
70 p[i].live, p[i].dirty, p[i].s_reg, reinterpret_cast<uintptr_t>(p[i].def_start),
71 reinterpret_cast<uintptr_t>(p[i].def_end));
Bill Buzbeea114add2012-05-03 15:00:40 -070072 }
73 LOG(INFO) << "================================================";
buzbee67bf8852011-08-17 17:51:35 -070074}
75
buzbeefa57c472012-11-21 12:06:18 -080076void DumpCoreRegPool(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -070077{
buzbeefa57c472012-11-21 12:06:18 -080078 DumpRegPool(cu->reg_pool->core_regs, cu->reg_pool->num_core_regs);
buzbee6181f792011-09-29 11:14:04 -070079}
80
buzbeefa57c472012-11-21 12:06:18 -080081void DumpFpRegPool(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -070082{
buzbeefa57c472012-11-21 12:06:18 -080083 DumpRegPool(cu->reg_pool->FPRegs, cu->reg_pool->num_fp_regs);
buzbee6181f792011-09-29 11:14:04 -070084}
85
buzbee67bf8852011-08-17 17:51:35 -070086/* Mark a temp register as dead. Does not affect allocation state. */
buzbeefa57c472012-11-21 12:06:18 -080087static void ClobberBody(CompilationUnit *cu, RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -070088{
buzbeefa57c472012-11-21 12:06:18 -080089 if (p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -070090 DCHECK(!(p->live && p->dirty)) << "Live & dirty temp in clobber";
91 p->live = false;
buzbeefa57c472012-11-21 12:06:18 -080092 p->s_reg = INVALID_SREG;
93 p->def_start = NULL;
94 p->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -070095 if (p->pair) {
96 p->pair = false;
buzbeefa57c472012-11-21 12:06:18 -080097 Clobber(cu, p->partner);
buzbee67bf8852011-08-17 17:51:35 -070098 }
Bill Buzbeea114add2012-05-03 15:00:40 -070099 }
buzbee67bf8852011-08-17 17:51:35 -0700100}
101
buzbee5abfa3e2012-01-31 17:01:43 -0800102/* Mark a temp register as dead. Does not affect allocation state. */
buzbeefa57c472012-11-21 12:06:18 -0800103void Clobber(CompilationUnit* cu, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800104{
buzbeefa57c472012-11-21 12:06:18 -0800105 ClobberBody(cu, GetRegInfo(cu, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800106}
107
buzbeefa57c472012-11-21 12:06:18 -0800108static void ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700109{
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 int i;
buzbeefa57c472012-11-21 12:06:18 -0800111 for (i=0; i< num_regs; i++) {
112 if (p[i].s_reg == s_reg) {
113 if (p[i].is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 p[i].live = false;
115 }
buzbeefa57c472012-11-21 12:06:18 -0800116 p[i].def_start = NULL;
117 p[i].def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700118 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 }
buzbee67bf8852011-08-17 17:51:35 -0700120}
121
buzbeefa57c472012-11-21 12:06:18 -0800122/* Clobber any temp associated with an s_reg. Could be in either class */
123void ClobberSReg(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700124{
buzbee3d661942012-03-14 17:37:27 -0700125#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 /* Reset live temp tracking sanity checker */
buzbeefa57c472012-11-21 12:06:18 -0800127 if (s_reg == cu->live_sreg) {
128 cu->live_sreg = INVALID_SREG;
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 }
buzbee3d661942012-03-14 17:37:27 -0700130#endif
buzbeefa57c472012-11-21 12:06:18 -0800131 ClobberSRegBody(cu->reg_pool->core_regs, cu->reg_pool->num_core_regs, s_reg);
132 ClobberSRegBody(cu->reg_pool->FPRegs, cu->reg_pool->num_fp_regs, s_reg);
buzbee67bf8852011-08-17 17:51:35 -0700133}
134
buzbee9c044ce2012-03-18 13:24:07 -0700135/*
136 * SSA names associated with the initial definitions of Dalvik
137 * registers are the same as the Dalvik register number (and
buzbeefa57c472012-11-21 12:06:18 -0800138 * thus take the same position in the promotion_map. However,
buzbee9c044ce2012-03-18 13:24:07 -0700139 * the special Method* and compiler temp resisters use negative
buzbeefa57c472012-11-21 12:06:18 -0800140 * v_reg numbers to distinguish them and can have an arbitrary
buzbee9c044ce2012-03-18 13:24:07 -0700141 * ssa name (above the last original Dalvik register). This function
buzbeefa57c472012-11-21 12:06:18 -0800142 * maps SSA names to positions in the promotion_map array.
buzbee9c044ce2012-03-18 13:24:07 -0700143 */
buzbeefa57c472012-11-21 12:06:18 -0800144static int SRegToPMap(CompilationUnit* cu, int s_reg)
buzbeee1965672012-03-11 18:39:19 -0700145{
buzbeefa57c472012-11-21 12:06:18 -0800146 DCHECK_LT(s_reg, cu->num_ssa_regs);
147 DCHECK_GE(s_reg, 0);
148 int v_reg = SRegToVReg(cu, s_reg);
149 if (v_reg >= 0) {
150 DCHECK_LT(v_reg, cu->num_dalvik_registers);
151 return v_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 } else {
buzbeefa57c472012-11-21 12:06:18 -0800153 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
154 DCHECK_LE(pos, cu->num_compiler_temps);
155 return cu->num_dalvik_registers + pos;
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 }
buzbeee1965672012-03-11 18:39:19 -0700157}
158
buzbeefa57c472012-11-21 12:06:18 -0800159void RecordCorePromotion(CompilationUnit* cu, int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700160{
buzbeefa57c472012-11-21 12:06:18 -0800161 int p_map_idx = SRegToPMap(cu, s_reg);
162 int v_reg = SRegToVReg(cu, s_reg);
163 GetRegInfo(cu, reg)->in_use = true;
164 cu->core_spill_mask |= (1 << reg);
buzbeeca7a5e42012-08-20 11:12:18 -0700165 // Include reg for later sort
buzbeefa57c472012-11-21 12:06:18 -0800166 cu->core_vmap_table.push_back(reg << VREG_NUM_WIDTH |
167 (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
168 cu->num_core_spills++;
169 cu->promotion_map[p_map_idx].core_location = kLocPhysReg;
170 cu->promotion_map[p_map_idx].core_reg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700171}
172
buzbee67bf8852011-08-17 17:51:35 -0700173/* Reserve a callee-save register. Return -1 if none available */
buzbeefa57c472012-11-21 12:06:18 -0800174static int AllocPreservedCoreReg(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700175{
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800177 RegisterInfo* core_regs = cu->reg_pool->core_regs;
178 for (int i = 0; i < cu->reg_pool->num_core_regs; i++) {
179 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
180 res = core_regs[i].reg;
181 RecordCorePromotion(cu, res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 break;
buzbee67bf8852011-08-17 17:51:35 -0700183 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 }
185 return res;
buzbee67bf8852011-08-17 17:51:35 -0700186}
187
buzbeefa57c472012-11-21 12:06:18 -0800188void RecordFpPromotion(CompilationUnit* cu, int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700189{
buzbeefa57c472012-11-21 12:06:18 -0800190 int p_map_idx = SRegToPMap(cu, s_reg);
191 int v_reg = SRegToVReg(cu, s_reg);
192 GetRegInfo(cu, reg)->in_use = true;
193 MarkPreservedSingle(cu, v_reg, reg);
194 cu->promotion_map[p_map_idx].fp_location = kLocPhysReg;
195 cu->promotion_map[p_map_idx].FpReg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700196}
197
buzbee67bf8852011-08-17 17:51:35 -0700198/*
199 * Reserve a callee-save fp single register. Try to fullfill request for
200 * even/odd allocation, but go ahead and allocate anything if not
201 * available. If nothing's available, return -1.
202 */
buzbeefa57c472012-11-21 12:06:18 -0800203static int AllocPreservedSingle(CompilationUnit* cu, int s_reg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700204{
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800206 RegisterInfo* FPRegs = cu->reg_pool->FPRegs;
207 for (int i = 0; i < cu->reg_pool->num_fp_regs; i++) {
208 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 ((FPRegs[i].reg & 0x1) == 0) == even) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800211 RecordFpPromotion(cu, res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 break;
buzbee67bf8852011-08-17 17:51:35 -0700213 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 }
215 return res;
buzbee67bf8852011-08-17 17:51:35 -0700216}
217
218/*
219 * Somewhat messy code here. We want to allocate a pair of contiguous
220 * physical single-precision floating point registers starting with
buzbeefa57c472012-11-21 12:06:18 -0800221 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
buzbee67bf8852011-08-17 17:51:35 -0700222 * has already been allocated - try to fit if possible. Fail to
223 * allocate if we can't meet the requirements for the pair of
buzbeefa57c472012-11-21 12:06:18 -0800224 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
buzbee67bf8852011-08-17 17:51:35 -0700225 */
buzbeefa57c472012-11-21 12:06:18 -0800226static int AllocPreservedDouble(CompilationUnit* cu, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700227{
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 int res = -1; // Assume failure
buzbeefa57c472012-11-21 12:06:18 -0800229 int v_reg = SRegToVReg(cu, s_reg);
230 int p_map_idx = SRegToPMap(cu, s_reg);
231 if (cu->promotion_map[p_map_idx+1].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 // Upper reg is already allocated. Can we fit?
buzbeefa57c472012-11-21 12:06:18 -0800233 int high_reg = cu->promotion_map[p_map_idx+1].FpReg;
234 if ((high_reg & 1) == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 // High reg is even - fail.
236 return res;
237 }
238 // Is the low reg of the pair free?
buzbeefa57c472012-11-21 12:06:18 -0800239 RegisterInfo* p = GetRegInfo(cu, high_reg-1);
240 if (p->in_use || p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 // Already allocated or not preserved - fail.
242 return res;
243 }
244 // OK - good to go.
245 res = p->reg;
buzbeefa57c472012-11-21 12:06:18 -0800246 p->in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 DCHECK_EQ((res & 1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800248 MarkPreservedSingle(cu, v_reg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 } else {
buzbeefa57c472012-11-21 12:06:18 -0800250 RegisterInfo* FPRegs = cu->reg_pool->FPRegs;
251 for (int i = 0; i < cu->reg_pool->num_fp_regs; i++) {
252 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 ((FPRegs[i].reg & 0x1) == 0x0) &&
buzbeefa57c472012-11-21 12:06:18 -0800254 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
256 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
257 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800258 FPRegs[i].in_use = true;
259 MarkPreservedSingle(cu, v_reg, res);
260 FPRegs[i+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
buzbeefa57c472012-11-21 12:06:18 -0800262 MarkPreservedSingle(cu, v_reg+1, res+1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 break;
264 }
buzbee67bf8852011-08-17 17:51:35 -0700265 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 }
267 if (res != -1) {
buzbeefa57c472012-11-21 12:06:18 -0800268 cu->promotion_map[p_map_idx].fp_location = kLocPhysReg;
269 cu->promotion_map[p_map_idx].FpReg = res;
270 cu->promotion_map[p_map_idx+1].fp_location = kLocPhysReg;
271 cu->promotion_map[p_map_idx+1].FpReg = res + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 }
273 return res;
buzbee67bf8852011-08-17 17:51:35 -0700274}
275
276
277/*
278 * Reserve a callee-save fp register. If this register can be used
279 * as the first of a double, attempt to allocate an even pair of fp
280 * single regs (but if can't still attempt to allocate a single, preferring
281 * first to allocate an odd register.
282 */
buzbeefa57c472012-11-21 12:06:18 -0800283static int AllocPreservedFPReg(CompilationUnit* cu, int s_reg, bool double_start)
buzbee67bf8852011-08-17 17:51:35 -0700284{
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800286 if (double_start) {
287 res = AllocPreservedDouble(cu, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 }
289 if (res == -1) {
buzbeefa57c472012-11-21 12:06:18 -0800290 res = AllocPreservedSingle(cu, s_reg, false /* try odd # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 }
292 if (res == -1)
buzbeefa57c472012-11-21 12:06:18 -0800293 res = AllocPreservedSingle(cu, s_reg, true /* try even # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 return res;
buzbee67bf8852011-08-17 17:51:35 -0700295}
296
buzbeefa57c472012-11-21 12:06:18 -0800297static int AllocTempBody(CompilationUnit* cu, RegisterInfo* p, int num_regs, int* next_temp,
buzbeeaad94382012-11-21 07:40:50 -0800298 bool required)
buzbee67bf8852011-08-17 17:51:35 -0700299{
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 int i;
buzbeefa57c472012-11-21 12:06:18 -0800301 int next = *next_temp;
302 for (i=0; i< num_regs; i++) {
303 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700304 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800305 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
306 Clobber(cu, p[next].reg);
307 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700308 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800309 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700311 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 next++;
313 }
buzbeefa57c472012-11-21 12:06:18 -0800314 next = *next_temp;
315 for (i=0; i< num_regs; i++) {
316 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800318 if (p[next].is_temp && !p[next].in_use) {
319 Clobber(cu, p[next].reg);
320 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800322 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700323 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700324 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 next++;
326 }
327 if (required) {
buzbeefa57c472012-11-21 12:06:18 -0800328 CodegenDump(cu);
329 DumpRegPool(cu->reg_pool->core_regs,
330 cu->reg_pool->num_core_regs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 LOG(FATAL) << "No free temp registers";
332 }
333 return -1; // No register available
buzbee67bf8852011-08-17 17:51:35 -0700334}
335
336//REDO: too many assumptions.
buzbeefa57c472012-11-21 12:06:18 -0800337int AllocTempDouble(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700338{
buzbeefa57c472012-11-21 12:06:18 -0800339 RegisterInfo* p = cu->reg_pool->FPRegs;
340 int num_regs = cu->reg_pool->num_fp_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 /* Start looking at an even reg */
buzbeefa57c472012-11-21 12:06:18 -0800342 int next = cu->reg_pool->next_fp_reg & ~0x1;
buzbee67bf8852011-08-17 17:51:35 -0700343
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 // First try to avoid allocating live registers
buzbeefa57c472012-11-21 12:06:18 -0800345 for (int i=0; i < num_regs; i+=2) {
346 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700347 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800348 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
349 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
350 Clobber(cu, p[next].reg);
351 Clobber(cu, p[next+1].reg);
352 p[next].in_use = true;
353 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
355 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800356 cu->reg_pool->next_fp_reg = next + 2;
357 if (cu->reg_pool->next_fp_reg >= num_regs) {
358 cu->reg_pool->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700359 }
360 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700361 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 next += 2;
363 }
buzbeefa57c472012-11-21 12:06:18 -0800364 next = cu->reg_pool->next_fp_reg & ~0x1;
buzbeea50638b2011-11-02 15:15:06 -0700365
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 // No choice - find a pair and kill it.
buzbeefa57c472012-11-21 12:06:18 -0800367 for (int i=0; i < num_regs; i+=2) {
368 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800370 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
371 !p[next+1].in_use) {
372 Clobber(cu, p[next].reg);
373 Clobber(cu, p[next+1].reg);
374 p[next].in_use = true;
375 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
377 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbeefa57c472012-11-21 12:06:18 -0800378 cu->reg_pool->next_fp_reg = next + 2;
379 if (cu->reg_pool->next_fp_reg >= num_regs) {
380 cu->reg_pool->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 }
382 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700383 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 next += 2;
385 }
386 LOG(FATAL) << "No free temp registers (pair)";
387 return -1;
buzbee67bf8852011-08-17 17:51:35 -0700388}
389
390/* Return a temp if one is available, -1 otherwise */
buzbeefa57c472012-11-21 12:06:18 -0800391int AllocFreeTemp(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700392{
buzbeefa57c472012-11-21 12:06:18 -0800393 return AllocTempBody(cu, cu->reg_pool->core_regs,
394 cu->reg_pool->num_core_regs,
395 &cu->reg_pool->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700396}
397
buzbeefa57c472012-11-21 12:06:18 -0800398int AllocTemp(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700399{
buzbeefa57c472012-11-21 12:06:18 -0800400 return AllocTempBody(cu, cu->reg_pool->core_regs,
401 cu->reg_pool->num_core_regs,
402 &cu->reg_pool->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700403}
404
buzbeefa57c472012-11-21 12:06:18 -0800405int AllocTempFloat(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700406{
buzbeefa57c472012-11-21 12:06:18 -0800407 return AllocTempBody(cu, cu->reg_pool->FPRegs,
408 cu->reg_pool->num_fp_regs,
409 &cu->reg_pool->next_fp_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700410}
411
buzbeefa57c472012-11-21 12:06:18 -0800412static RegisterInfo* AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700413{
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 int i;
buzbeefa57c472012-11-21 12:06:18 -0800415 if (s_reg == -1)
buzbee67bf8852011-08-17 17:51:35 -0700416 return NULL;
buzbeefa57c472012-11-21 12:06:18 -0800417 for (i=0; i < num_regs; i++) {
418 if (p[i].live && (p[i].s_reg == s_reg)) {
419 if (p[i].is_temp)
420 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 return &p[i];
422 }
423 }
424 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700425}
426
buzbeefa57c472012-11-21 12:06:18 -0800427RegisterInfo* AllocLive(CompilationUnit* cu, int s_reg, int reg_class)
buzbee67bf8852011-08-17 17:51:35 -0700428{
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 RegisterInfo* res = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800430 switch (reg_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 case kAnyReg:
buzbeefa57c472012-11-21 12:06:18 -0800432 res = AllocLiveBody(cu->reg_pool->FPRegs,
433 cu->reg_pool->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 if (res)
435 break;
436 /* Intentional fallthrough */
437 case kCoreReg:
buzbeefa57c472012-11-21 12:06:18 -0800438 res = AllocLiveBody(cu->reg_pool->core_regs,
439 cu->reg_pool->num_core_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 break;
441 case kFPReg:
buzbeefa57c472012-11-21 12:06:18 -0800442 res = AllocLiveBody(cu->reg_pool->FPRegs,
443 cu->reg_pool->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 break;
445 default:
446 LOG(FATAL) << "Invalid register type";
447 }
448 return res;
buzbee67bf8852011-08-17 17:51:35 -0700449}
450
buzbeefa57c472012-11-21 12:06:18 -0800451void FreeTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700452{
buzbeefa57c472012-11-21 12:06:18 -0800453 RegisterInfo* p = cu->reg_pool->core_regs;
454 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 int i;
buzbeefa57c472012-11-21 12:06:18 -0800456 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800458 if (p[i].is_temp) {
459 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700460 }
461 p[i].pair = false;
462 return;
buzbee67bf8852011-08-17 17:51:35 -0700463 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700464 }
buzbeefa57c472012-11-21 12:06:18 -0800465 p = cu->reg_pool->FPRegs;
466 num_regs = cu->reg_pool->num_fp_regs;
467 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800469 if (p[i].is_temp) {
470 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 }
472 p[i].pair = false;
473 return;
buzbee67bf8852011-08-17 17:51:35 -0700474 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 }
476 LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700477}
478
buzbeefa57c472012-11-21 12:06:18 -0800479RegisterInfo* IsLive(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700480{
buzbeefa57c472012-11-21 12:06:18 -0800481 RegisterInfo* p = cu->reg_pool->core_regs;
482 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 int i;
buzbeefa57c472012-11-21 12:06:18 -0800484 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 if (p[i].reg == reg) {
486 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700487 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 }
buzbeefa57c472012-11-21 12:06:18 -0800489 p = cu->reg_pool->FPRegs;
490 num_regs = cu->reg_pool->num_fp_regs;
491 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700492 if (p[i].reg == reg) {
493 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700494 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 }
496 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700497}
498
buzbeefa57c472012-11-21 12:06:18 -0800499RegisterInfo* IsTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700500{
buzbeefa57c472012-11-21 12:06:18 -0800501 RegisterInfo* p = GetRegInfo(cu, reg);
502 return (p->is_temp) ? p : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700503}
504
buzbeefa57c472012-11-21 12:06:18 -0800505RegisterInfo* IsPromoted(CompilationUnit* cu, int reg)
buzbeeb29e4d12011-09-26 15:05:48 -0700506{
buzbeefa57c472012-11-21 12:06:18 -0800507 RegisterInfo* p = GetRegInfo(cu, reg);
508 return (p->is_temp) ? NULL : p;
buzbeeb29e4d12011-09-26 15:05:48 -0700509}
510
buzbeefa57c472012-11-21 12:06:18 -0800511bool IsDirty(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700512{
buzbeefa57c472012-11-21 12:06:18 -0800513 RegisterInfo* p = GetRegInfo(cu, reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 return p->dirty;
buzbee67bf8852011-08-17 17:51:35 -0700515}
516
517/*
buzbee52a77fc2012-11-20 19:50:46 -0800518 * Similar to AllocTemp(), but forces the allocation of a specific
buzbee67bf8852011-08-17 17:51:35 -0700519 * register. No check is made to see if the register was previously
520 * allocated. Use with caution.
521 */
buzbeefa57c472012-11-21 12:06:18 -0800522void LockTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700523{
buzbeefa57c472012-11-21 12:06:18 -0800524 RegisterInfo* p = cu->reg_pool->core_regs;
525 int num_regs = cu->reg_pool->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 int i;
buzbeefa57c472012-11-21 12:06:18 -0800527 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800529 DCHECK(p[i].is_temp);
530 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700531 p[i].live = false;
532 return;
buzbee67bf8852011-08-17 17:51:35 -0700533 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 }
buzbeefa57c472012-11-21 12:06:18 -0800535 p = cu->reg_pool->FPRegs;
536 num_regs = cu->reg_pool->num_fp_regs;
537 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800539 DCHECK(p[i].is_temp);
540 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 p[i].live = false;
542 return;
buzbee67bf8852011-08-17 17:51:35 -0700543 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 }
545 LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700546}
547
buzbeeaad94382012-11-21 07:40:50 -0800548static void ResetDefBody(RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -0700549{
buzbeefa57c472012-11-21 12:06:18 -0800550 p->def_start = NULL;
551 p->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700552}
553
buzbeefa57c472012-11-21 12:06:18 -0800554void ResetDef(CompilationUnit* cu, int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800555{
buzbeefa57c472012-11-21 12:06:18 -0800556 ResetDefBody(GetRegInfo(cu, reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800557}
558
buzbeefa57c472012-11-21 12:06:18 -0800559static void NullifyRange(CompilationUnit* cu, LIR *start, LIR *finish, int s_reg1, int s_reg2)
buzbee67bf8852011-08-17 17:51:35 -0700560{
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 if (start && finish) {
562 LIR *p;
buzbeefa57c472012-11-21 12:06:18 -0800563 DCHECK_EQ(s_reg1, s_reg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700564 for (p = start; ;p = p->next) {
buzbee52a77fc2012-11-20 19:50:46 -0800565 NopLIR(p);
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 if (p == finish)
567 break;
buzbee67bf8852011-08-17 17:51:35 -0700568 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700569 }
buzbee67bf8852011-08-17 17:51:35 -0700570}
571
572/*
573 * Mark the beginning and end LIR of a def sequence. Note that
574 * on entry start points to the LIR prior to the beginning of the
575 * sequence.
576 */
buzbeefa57c472012-11-21 12:06:18 -0800577void MarkDef(CompilationUnit* cu, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700579{
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 DCHECK(!rl.wide);
581 DCHECK(start && start->next);
582 DCHECK(finish);
buzbeefa57c472012-11-21 12:06:18 -0800583 RegisterInfo* p = GetRegInfo(cu, rl.low_reg);
584 p->def_start = start->next;
585 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700586}
587
588/*
589 * Mark the beginning and end LIR of a def sequence. Note that
590 * on entry start points to the LIR prior to the beginning of the
591 * sequence.
592 */
buzbeefa57c472012-11-21 12:06:18 -0800593void MarkDefWide(CompilationUnit* cu, RegLocation rl,
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700595{
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 DCHECK(rl.wide);
597 DCHECK(start && start->next);
598 DCHECK(finish);
buzbeefa57c472012-11-21 12:06:18 -0800599 RegisterInfo* p = GetRegInfo(cu, rl.low_reg);
600 ResetDef(cu, rl.high_reg); // Only track low of pair
601 p->def_start = start->next;
602 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700603}
604
buzbeefa57c472012-11-21 12:06:18 -0800605RegLocation WideToNarrow(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700606{
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 DCHECK(rl.wide);
608 if (rl.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800609 RegisterInfo* info_lo = GetRegInfo(cu, rl.low_reg);
610 RegisterInfo* info_hi = GetRegInfo(cu, rl.high_reg);
611 if (info_lo->is_temp) {
612 info_lo->pair = false;
613 info_lo->def_start = NULL;
614 info_lo->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700615 }
buzbeefa57c472012-11-21 12:06:18 -0800616 if (info_hi->is_temp) {
617 info_hi->pair = false;
618 info_hi->def_start = NULL;
619 info_hi->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700620 }
621 }
622 rl.wide = false;
623 return rl;
buzbee67bf8852011-08-17 17:51:35 -0700624}
625
buzbeefa57c472012-11-21 12:06:18 -0800626void ResetDefLoc(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700627{
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 DCHECK(!rl.wide);
buzbeefa57c472012-11-21 12:06:18 -0800629 RegisterInfo* p = IsTemp(cu, rl.low_reg);
630 if (p && !(cu->disable_opt & (1 << kSuppressLoads))) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 DCHECK(!p->pair);
buzbeefa57c472012-11-21 12:06:18 -0800632 NullifyRange(cu, p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 }
buzbeefa57c472012-11-21 12:06:18 -0800634 ResetDef(cu, rl.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700635}
636
buzbeefa57c472012-11-21 12:06:18 -0800637void ResetDefLocWide(CompilationUnit* cu, RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700638{
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 DCHECK(rl.wide);
buzbeefa57c472012-11-21 12:06:18 -0800640 RegisterInfo* p_low = IsTemp(cu, rl.low_reg);
641 RegisterInfo* p_high = IsTemp(cu, rl.high_reg);
642 if (p_low && !(cu->disable_opt & (1 << kSuppressLoads))) {
643 DCHECK(p_low->pair);
644 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 -0700645 }
buzbeefa57c472012-11-21 12:06:18 -0800646 if (p_high && !(cu->disable_opt & (1 << kSuppressLoads))) {
647 DCHECK(p_high->pair);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 }
buzbeefa57c472012-11-21 12:06:18 -0800649 ResetDef(cu, rl.low_reg);
650 ResetDef(cu, rl.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700651}
652
buzbeefa57c472012-11-21 12:06:18 -0800653void ResetDefTracking(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700654{
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 int i;
buzbeefa57c472012-11-21 12:06:18 -0800656 for (i=0; i< cu->reg_pool->num_core_regs; i++) {
657 ResetDefBody(&cu->reg_pool->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 }
buzbeefa57c472012-11-21 12:06:18 -0800659 for (i=0; i< cu->reg_pool->num_fp_regs; i++) {
660 ResetDefBody(&cu->reg_pool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700661 }
buzbee67bf8852011-08-17 17:51:35 -0700662}
663
buzbeefa57c472012-11-21 12:06:18 -0800664void ClobberAllRegs(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700665{
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 int i;
buzbeefa57c472012-11-21 12:06:18 -0800667 for (i=0; i< cu->reg_pool->num_core_regs; i++) {
668 ClobberBody(cu, &cu->reg_pool->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700669 }
buzbeefa57c472012-11-21 12:06:18 -0800670 for (i=0; i< cu->reg_pool->num_fp_regs; i++) {
671 ClobberBody(cu, &cu->reg_pool->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 }
buzbee67bf8852011-08-17 17:51:35 -0700673}
674
buzbee67bf8852011-08-17 17:51:35 -0700675// Make sure nothing is live and dirty
buzbeefa57c472012-11-21 12:06:18 -0800676static void FlushAllRegsBody(CompilationUnit* cu, RegisterInfo* info, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -0700677{
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 int i;
buzbeefa57c472012-11-21 12:06:18 -0800679 for (i=0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 if (info[i].live && info[i].dirty) {
681 if (info[i].pair) {
buzbeefa57c472012-11-21 12:06:18 -0800682 FlushRegWide(cu, info[i].reg, info[i].partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 } else {
buzbeefa57c472012-11-21 12:06:18 -0800684 FlushReg(cu, info[i].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 }
buzbee67bf8852011-08-17 17:51:35 -0700686 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700687 }
buzbee67bf8852011-08-17 17:51:35 -0700688}
689
buzbeefa57c472012-11-21 12:06:18 -0800690void FlushAllRegs(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700691{
buzbeefa57c472012-11-21 12:06:18 -0800692 FlushAllRegsBody(cu, cu->reg_pool->core_regs,
693 cu->reg_pool->num_core_regs);
694 FlushAllRegsBody(cu, cu->reg_pool->FPRegs,
695 cu->reg_pool->num_fp_regs);
696 ClobberAllRegs(cu);
buzbee67bf8852011-08-17 17:51:35 -0700697}
698
699
700//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbeefa57c472012-11-21 12:06:18 -0800701static bool RegClassMatches(int reg_class, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700702{
buzbeefa57c472012-11-21 12:06:18 -0800703 if (reg_class == kAnyReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700704 return true;
buzbeefa57c472012-11-21 12:06:18 -0800705 } else if (reg_class == kCoreReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800706 return !IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800708 return IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 }
buzbee67bf8852011-08-17 17:51:35 -0700710}
711
buzbeefa57c472012-11-21 12:06:18 -0800712void MarkLive(CompilationUnit* cu, int reg, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700713{
buzbeefa57c472012-11-21 12:06:18 -0800714 RegisterInfo* info = GetRegInfo(cu, reg);
715 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700716 return; /* already live */
buzbeefa57c472012-11-21 12:06:18 -0800717 } else if (s_reg != INVALID_SREG) {
718 ClobberSReg(cu, s_reg);
719 if (info->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700720 info->live = true;
buzbee67bf8852011-08-17 17:51:35 -0700721 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700722 } else {
buzbeefa57c472012-11-21 12:06:18 -0800723 /* Can't be live if no associated s_reg */
724 DCHECK(info->is_temp);
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 info->live = false;
726 }
buzbeefa57c472012-11-21 12:06:18 -0800727 info->s_reg = s_reg;
buzbee67bf8852011-08-17 17:51:35 -0700728}
729
buzbeefa57c472012-11-21 12:06:18 -0800730void MarkTemp(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700731{
buzbeefa57c472012-11-21 12:06:18 -0800732 RegisterInfo* info = GetRegInfo(cu, reg);
733 info->is_temp = true;
buzbee67bf8852011-08-17 17:51:35 -0700734}
735
buzbeefa57c472012-11-21 12:06:18 -0800736void UnmarkTemp(CompilationUnit* cu, int reg)
buzbee9e0f9b02011-08-24 15:32:46 -0700737{
buzbeefa57c472012-11-21 12:06:18 -0800738 RegisterInfo* info = GetRegInfo(cu, reg);
739 info->is_temp = false;
buzbee9e0f9b02011-08-24 15:32:46 -0700740}
741
buzbeefa57c472012-11-21 12:06:18 -0800742void MarkPair(CompilationUnit* cu, int low_reg, int high_reg)
buzbee67bf8852011-08-17 17:51:35 -0700743{
buzbeefa57c472012-11-21 12:06:18 -0800744 RegisterInfo* info_lo = GetRegInfo(cu, low_reg);
745 RegisterInfo* info_hi = GetRegInfo(cu, high_reg);
746 info_lo->pair = info_hi->pair = true;
747 info_lo->partner = high_reg;
748 info_hi->partner = low_reg;
buzbee67bf8852011-08-17 17:51:35 -0700749}
750
buzbeefa57c472012-11-21 12:06:18 -0800751void MarkClean(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700752{
buzbeefa57c472012-11-21 12:06:18 -0800753 RegisterInfo* info = GetRegInfo(cu, loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700754 info->dirty = false;
755 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -0800756 info = GetRegInfo(cu, loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700757 info->dirty = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700758 }
buzbee67bf8852011-08-17 17:51:35 -0700759}
760
buzbeefa57c472012-11-21 12:06:18 -0800761void MarkDirty(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700762{
Bill Buzbeea114add2012-05-03 15:00:40 -0700763 if (loc.home) {
764 // If already home, can't be dirty
765 return;
766 }
buzbeefa57c472012-11-21 12:06:18 -0800767 RegisterInfo* info = GetRegInfo(cu, loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 info->dirty = true;
769 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -0800770 info = GetRegInfo(cu, loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700771 info->dirty = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 }
buzbee67bf8852011-08-17 17:51:35 -0700773}
774
buzbeefa57c472012-11-21 12:06:18 -0800775void MarkInUse(CompilationUnit* cu, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700776{
buzbeefa57c472012-11-21 12:06:18 -0800777 RegisterInfo* info = GetRegInfo(cu, reg);
778 info->in_use = true;
buzbee67bf8852011-08-17 17:51:35 -0700779}
780
buzbeefa57c472012-11-21 12:06:18 -0800781static void CopyRegInfo(CompilationUnit* cu, int new_reg, int old_reg)
buzbee67bf8852011-08-17 17:51:35 -0700782{
buzbeefa57c472012-11-21 12:06:18 -0800783 RegisterInfo* new_info = GetRegInfo(cu, new_reg);
784 RegisterInfo* old_info = GetRegInfo(cu, old_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 // Target temp status must not change
buzbeefa57c472012-11-21 12:06:18 -0800786 bool is_temp = new_info->is_temp;
787 *new_info = *old_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700788 // Restore target's temp status
buzbeefa57c472012-11-21 12:06:18 -0800789 new_info->is_temp = is_temp;
790 new_info->reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700791}
792
buzbeefa57c472012-11-21 12:06:18 -0800793static bool CheckCorePoolSanity(CompilationUnit* cu)
buzbee6181f792011-09-29 11:14:04 -0700794{
buzbeefa57c472012-11-21 12:06:18 -0800795 for (static int i = 0; i < cu->reg_pool->num_core_regs; i++) {
796 if (cu->reg_pool->core_regs[i].pair) {
797 static int my_reg = cu->reg_pool->core_regs[i].reg;
798 static int my_sreg = cu->reg_pool->core_regs[i].s_reg;
799 static int partner_reg = cu->reg_pool->core_regs[i].partner;
800 static RegisterInfo* partner = GetRegInfo(cu, partner_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 DCHECK(partner != NULL);
802 DCHECK(partner->pair);
buzbeefa57c472012-11-21 12:06:18 -0800803 DCHECK_EQ(my_reg, partner->partner);
804 static int partner_sreg = partner->s_reg;
805 if (my_sreg == INVALID_SREG) {
806 DCHECK_EQ(partner_sreg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 } else {
buzbeefa57c472012-11-21 12:06:18 -0800808 int diff = my_sreg - partner_sreg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 DCHECK((diff == -1) || (diff == 1));
buzbee6181f792011-09-29 11:14:04 -0700810 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700811 }
buzbeefa57c472012-11-21 12:06:18 -0800812 if (!cu->reg_pool->core_regs[i].live) {
813 DCHECK(cu->reg_pool->core_regs[i].def_start == NULL);
814 DCHECK(cu->reg_pool->core_regs[i].def_end == NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700815 }
buzbee6181f792011-09-29 11:14:04 -0700816 }
817 return true;
818}
819
buzbeeaad94382012-11-21 07:40:50 -0800820/*
821 * Return an updated location record with current in-register status.
822 * If the value lives in live temps, reflect that fact. No code
823 * is generated. If the live value is part of an older pair,
824 * clobber both low and high.
825 * TUNING: clobbering both is a bit heavy-handed, but the alternative
826 * is a bit complex when dealing with FP regs. Examine code to see
827 * if it's worthwhile trying to be more clever here.
828 */
829
buzbeefa57c472012-11-21 12:06:18 -0800830RegLocation UpdateLoc(CompilationUnit* cu, RegLocation loc)
buzbeeaad94382012-11-21 07:40:50 -0800831{
832 DCHECK(!loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800833 DCHECK(CheckCorePoolSanity(cu));
buzbeeaad94382012-11-21 07:40:50 -0800834 if (loc.location != kLocPhysReg) {
835 DCHECK((loc.location == kLocDalvikFrame) ||
836 (loc.location == kLocCompilerTemp));
buzbeefa57c472012-11-21 12:06:18 -0800837 RegisterInfo* info_lo = AllocLive(cu, loc.s_reg_low, kAnyReg);
838 if (info_lo) {
839 if (info_lo->pair) {
840 Clobber(cu, info_lo->reg);
841 Clobber(cu, info_lo->partner);
842 FreeTemp(cu, info_lo->reg);
buzbeeaad94382012-11-21 07:40:50 -0800843 } else {
buzbeefa57c472012-11-21 12:06:18 -0800844 loc.low_reg = info_lo->reg;
buzbeeaad94382012-11-21 07:40:50 -0800845 loc.location = kLocPhysReg;
846 }
847 }
848 }
849
850 return loc;
851}
852
buzbeefa57c472012-11-21 12:06:18 -0800853/* see comments for update_loc */
854RegLocation UpdateLocWide(CompilationUnit* cu, RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700855{
Bill Buzbeea114add2012-05-03 15:00:40 -0700856 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800857 DCHECK(CheckCorePoolSanity(cu));
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 if (loc.location != kLocPhysReg) {
859 DCHECK((loc.location == kLocDalvikFrame) ||
860 (loc.location == kLocCompilerTemp));
861 // Are the dalvik regs already live in physical registers?
buzbeefa57c472012-11-21 12:06:18 -0800862 RegisterInfo* info_lo = AllocLive(cu, loc.s_reg_low, kAnyReg);
863 RegisterInfo* info_hi = AllocLive(cu,
864 GetSRegHi(loc.s_reg_low), kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 bool match = true;
buzbeefa57c472012-11-21 12:06:18 -0800866 match = match && (info_lo != NULL);
867 match = match && (info_hi != NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700868 // Are they both core or both FP?
buzbeefa57c472012-11-21 12:06:18 -0800869 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 // If a pair of floating point singles, are they properly aligned?
buzbeefa57c472012-11-21 12:06:18 -0800871 if (match && IsFpReg(info_lo->reg)) {
872 match &= ((info_lo->reg & 0x1) == 0);
873 match &= ((info_hi->reg - info_lo->reg) == 1);
buzbee67bf8852011-08-17 17:51:35 -0700874 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700875 // If previously used as a pair, it is the same pair?
buzbeefa57c472012-11-21 12:06:18 -0800876 if (match && (info_lo->pair || info_hi->pair)) {
877 match = (info_lo->pair == info_hi->pair);
878 match &= ((info_lo->reg == info_hi->partner) &&
879 (info_hi->reg == info_lo->partner));
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 }
881 if (match) {
882 // Can reuse - update the register usage info
buzbeefa57c472012-11-21 12:06:18 -0800883 loc.low_reg = info_lo->reg;
884 loc.high_reg = info_hi->reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800886 MarkPair(cu, loc.low_reg, loc.high_reg);
887 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700888 return loc;
889 }
890 // Can't easily reuse - clobber and free any overlaps
buzbeefa57c472012-11-21 12:06:18 -0800891 if (info_lo) {
892 Clobber(cu, info_lo->reg);
893 FreeTemp(cu, info_lo->reg);
894 if (info_lo->pair)
895 Clobber(cu, info_lo->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 }
buzbeefa57c472012-11-21 12:06:18 -0800897 if (info_hi) {
898 Clobber(cu, info_hi->reg);
899 FreeTemp(cu, info_hi->reg);
900 if (info_hi->pair)
901 Clobber(cu, info_hi->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 }
903 }
904 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700905}
906
buzbeeed3e9302011-09-23 17:34:19 -0700907
908/* For use in cases we don't know (or care) width */
buzbeefa57c472012-11-21 12:06:18 -0800909RegLocation UpdateRawLoc(CompilationUnit* cu, RegLocation loc)
buzbeeed3e9302011-09-23 17:34:19 -0700910{
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 if (loc.wide)
buzbeefa57c472012-11-21 12:06:18 -0800912 return UpdateLocWide(cu, loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 else
buzbeefa57c472012-11-21 12:06:18 -0800914 return UpdateLoc(cu, loc);
buzbeeed3e9302011-09-23 17:34:19 -0700915}
916
buzbeefa57c472012-11-21 12:06:18 -0800917RegLocation EvalLocWide(CompilationUnit* cu, RegLocation loc, int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700918{
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800920 int new_regs;
921 int low_reg;
922 int high_reg;
buzbee67bf8852011-08-17 17:51:35 -0700923
buzbeefa57c472012-11-21 12:06:18 -0800924 loc = UpdateLocWide(cu, loc);
buzbee67bf8852011-08-17 17:51:35 -0700925
Bill Buzbeea114add2012-05-03 15:00:40 -0700926 /* If already in registers, we can assume proper form. Right reg class? */
927 if (loc.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800928 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
929 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
930 if (!RegClassMatches(reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 /* Wrong register class. Reallocate and copy */
buzbeefa57c472012-11-21 12:06:18 -0800932 new_regs = AllocTypedTempPair(cu, loc.fp, reg_class);
933 low_reg = new_regs & 0xff;
934 high_reg = (new_regs >> 8) & 0xff;
935 OpRegCopyWide(cu, low_reg, high_reg, loc.low_reg,
936 loc.high_reg);
937 CopyRegInfo(cu, low_reg, loc.low_reg);
938 CopyRegInfo(cu, high_reg, loc.high_reg);
939 Clobber(cu, loc.low_reg);
940 Clobber(cu, loc.high_reg);
941 loc.low_reg = low_reg;
942 loc.high_reg = high_reg;
943 MarkPair(cu, loc.low_reg, loc.high_reg);
944 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 }
buzbee67bf8852011-08-17 17:51:35 -0700946 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 }
948
buzbeefa57c472012-11-21 12:06:18 -0800949 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
950 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700951
buzbeefa57c472012-11-21 12:06:18 -0800952 new_regs = AllocTypedTempPair(cu, loc.fp, reg_class);
953 loc.low_reg = new_regs & 0xff;
954 loc.high_reg = (new_regs >> 8) & 0xff;
Bill Buzbeea114add2012-05-03 15:00:40 -0700955
buzbeefa57c472012-11-21 12:06:18 -0800956 MarkPair(cu, loc.low_reg, loc.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 if (update) {
958 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800959 MarkLive(cu, loc.low_reg, loc.s_reg_low);
960 MarkLive(cu, loc.high_reg, GetSRegHi(loc.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700961 }
buzbeefa57c472012-11-21 12:06:18 -0800962 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700963 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700964}
965
buzbeefa57c472012-11-21 12:06:18 -0800966RegLocation EvalLoc(CompilationUnit* cu, RegLocation loc,
967 int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700968{
buzbeefa57c472012-11-21 12:06:18 -0800969 int new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700970
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 if (loc.wide)
buzbeefa57c472012-11-21 12:06:18 -0800972 return EvalLocWide(cu, loc, reg_class, update);
buzbee67bf8852011-08-17 17:51:35 -0700973
buzbeefa57c472012-11-21 12:06:18 -0800974 loc = UpdateLoc(cu, loc);
buzbee67bf8852011-08-17 17:51:35 -0700975
Bill Buzbeea114add2012-05-03 15:00:40 -0700976 if (loc.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800977 if (!RegClassMatches(reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700978 /* Wrong register class. Realloc, copy and transfer ownership */
buzbeefa57c472012-11-21 12:06:18 -0800979 new_reg = AllocTypedTemp(cu, loc.fp, reg_class);
980 OpRegCopy(cu, new_reg, loc.low_reg);
981 CopyRegInfo(cu, new_reg, loc.low_reg);
982 Clobber(cu, loc.low_reg);
983 loc.low_reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700984 }
985 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 }
987
buzbeefa57c472012-11-21 12:06:18 -0800988 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700989
buzbeefa57c472012-11-21 12:06:18 -0800990 new_reg = AllocTypedTemp(cu, loc.fp, reg_class);
991 loc.low_reg = new_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700992
993 if (update) {
994 loc.location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -0800995 MarkLive(cu, loc.low_reg, loc.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 }
997 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700998}
999
buzbeefa57c472012-11-21 12:06:18 -08001000RegLocation GetRawSrc(CompilationUnit* cu, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001001{
buzbeefa57c472012-11-21 12:06:18 -08001002 DCHECK(num < mir->ssa_rep->num_uses);
1003 RegLocation res = cu->reg_location[mir->ssa_rep->uses[num]];
buzbee15bf9802012-06-12 17:49:27 -07001004 return res;
1005}
buzbeeeaf09bc2012-11-15 14:51:41 -08001006
buzbeefa57c472012-11-21 12:06:18 -08001007RegLocation GetRawDest(CompilationUnit* cu, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001008{
buzbeefa57c472012-11-21 12:06:18 -08001009 DCHECK_GT(mir->ssa_rep->num_defs, 0);
1010 RegLocation res = cu->reg_location[mir->ssa_rep->defs[0]];
buzbee15bf9802012-06-12 17:49:27 -07001011 return res;
1012}
buzbeeeaf09bc2012-11-15 14:51:41 -08001013
buzbeefa57c472012-11-21 12:06:18 -08001014RegLocation GetDest(CompilationUnit* cu, MIR* mir)
buzbee15bf9802012-06-12 17:49:27 -07001015{
buzbeefa57c472012-11-21 12:06:18 -08001016 RegLocation res = GetRawDest(cu, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001017 DCHECK(!res.wide);
1018 return res;
buzbee67bf8852011-08-17 17:51:35 -07001019}
buzbeeeaf09bc2012-11-15 14:51:41 -08001020
buzbeefa57c472012-11-21 12:06:18 -08001021RegLocation GetSrc(CompilationUnit* cu, MIR* mir, int num)
buzbee67bf8852011-08-17 17:51:35 -07001022{
buzbeefa57c472012-11-21 12:06:18 -08001023 RegLocation res = GetRawSrc(cu, mir, num);
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 DCHECK(!res.wide);
1025 return res;
buzbeee9a72f62011-09-04 17:59:07 -07001026}
buzbeeeaf09bc2012-11-15 14:51:41 -08001027
buzbeefa57c472012-11-21 12:06:18 -08001028RegLocation GetDestWide(CompilationUnit* cu, MIR* mir)
buzbeee9a72f62011-09-04 17:59:07 -07001029{
buzbeefa57c472012-11-21 12:06:18 -08001030 RegLocation res = GetRawDest(cu, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -07001031 DCHECK(res.wide);
1032 return res;
buzbee67bf8852011-08-17 17:51:35 -07001033}
1034
buzbeefa57c472012-11-21 12:06:18 -08001035RegLocation GetSrcWide(CompilationUnit* cu, MIR* mir,
buzbee15bf9802012-06-12 17:49:27 -07001036 int low)
buzbee67bf8852011-08-17 17:51:35 -07001037{
buzbeefa57c472012-11-21 12:06:18 -08001038 RegLocation res = GetRawSrc(cu, mir, low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 DCHECK(res.wide);
1040 return res;
buzbee67bf8852011-08-17 17:51:35 -07001041}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001042
buzbeefa57c472012-11-21 12:06:18 -08001043/* USE SSA names to count references of base Dalvik v_regs. */
1044static void CountRefs(CompilationUnit *cu, BasicBlock* bb, RefCounts* core_counts,
1045 RefCounts* fp_counts)
buzbeee3acd072012-02-25 17:03:10 -08001046{
buzbeefa57c472012-11-21 12:06:18 -08001047 if ((cu->disable_opt & (1 << kPromoteRegs)) ||
1048 !((bb->block_type == kEntryBlock) || (bb->block_type == kExitBlock) ||
1049 (bb->block_type == kDalvikByteCode))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001050 return;
1051 }
buzbeefa57c472012-11-21 12:06:18 -08001052 for (int i = 0; i < cu->num_ssa_regs;) {
1053 RegLocation loc = cu->reg_location[i];
1054 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1055 int p_map_idx = SRegToPMap(cu, loc.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001056 if (loc.defined) {
buzbeefa57c472012-11-21 12:06:18 -08001057 counts[p_map_idx].count += cu->use_counts.elem_list[i];
buzbee239c4e72012-03-16 08:42:29 -07001058 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 if (loc.wide) {
1060 if (loc.defined) {
1061 if (loc.fp) {
buzbeefa57c472012-11-21 12:06:18 -08001062 counts[p_map_idx].double_start = true;
1063 counts[p_map_idx+1].count += cu->use_counts.elem_list[i+1];
buzbee239c4e72012-03-16 08:42:29 -07001064 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001065 }
1066 i += 2;
1067 } else {
1068 i++;
buzbeee3acd072012-02-25 17:03:10 -08001069 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001070 }
buzbeee3acd072012-02-25 17:03:10 -08001071}
1072
1073/* qsort callback function, sort descending */
buzbeeaad94382012-11-21 07:40:50 -08001074static int SortCounts(const void *val1, const void *val2)
buzbeee3acd072012-02-25 17:03:10 -08001075{
buzbeecbd6d442012-11-17 14:11:25 -08001076 const RefCounts* op1 = reinterpret_cast<const RefCounts*>(val1);
1077 const RefCounts* op2 = reinterpret_cast<const RefCounts*>(val2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001078 return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
buzbeee3acd072012-02-25 17:03:10 -08001079}
1080
buzbeeaad94382012-11-21 07:40:50 -08001081static void DumpCounts(const RefCounts* arr, int size, const char* msg)
buzbeee3acd072012-02-25 17:03:10 -08001082{
Bill Buzbeea114add2012-05-03 15:00:40 -07001083 LOG(INFO) << msg;
1084 for (int i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001085 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 }
buzbeee3acd072012-02-25 17:03:10 -08001087}
1088
1089/*
1090 * Note: some portions of this code required even if the kPromoteRegs
1091 * optimization is disabled.
1092 */
buzbeefa57c472012-11-21 12:06:18 -08001093void DoPromotion(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -08001094{
buzbeefa57c472012-11-21 12:06:18 -08001095 int reg_bias = cu->num_compiler_temps + 1;
1096 int dalvik_regs = cu->num_dalvik_registers;
1097 int num_regs = dalvik_regs + reg_bias;
1098 const int promotion_threshold = 2;
buzbeee3acd072012-02-25 17:03:10 -08001099
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 // Allow target code to add any special registers
buzbeefa57c472012-11-21 12:06:18 -08001101 AdjustSpillMask(cu);
buzbeee3acd072012-02-25 17:03:10 -08001102
Bill Buzbeea114add2012-05-03 15:00:40 -07001103 /*
1104 * Simple register promotion. Just do a static count of the uses
1105 * of Dalvik registers. Note that we examine the SSA names, but
1106 * count based on original Dalvik register name. Count refs
1107 * separately based on type in order to give allocation
1108 * preference to fp doubles - which must be allocated sequential
1109 * physical single fp registers started with an even-numbered
1110 * reg.
1111 * TUNING: replace with linear scan once we have the ability
1112 * to describe register live ranges for GC.
1113 */
buzbeefa57c472012-11-21 12:06:18 -08001114 RefCounts *core_regs = static_cast<RefCounts*>(NewMem(cu, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001115 true, kAllocRegAlloc));
buzbeefa57c472012-11-21 12:06:18 -08001116 RefCounts *FpRegs = static_cast<RefCounts *>(NewMem(cu, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001117 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -07001118 // Set ssa names for original Dalvik registers
buzbeefa57c472012-11-21 12:06:18 -08001119 for (int i = 0; i < dalvik_regs; i++) {
1120 core_regs[i].s_reg = FpRegs[i].s_reg = i;
Bill Buzbeea114add2012-05-03 15:00:40 -07001121 }
1122 // Set ssa name for Method*
buzbeefa57c472012-11-21 12:06:18 -08001123 core_regs[dalvik_regs].s_reg = cu->method_sreg;
1124 FpRegs[dalvik_regs].s_reg = cu->method_sreg; // For consistecy
1125 // Set ssa names for compiler_temps
1126 for (int i = 1; i <= cu->num_compiler_temps; i++) {
1127 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cu->compiler_temps.elem_list[i]);
1128 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
1129 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001130 }
1131
1132 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001133 GrowableListIteratorInit(&cu->block_list, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001134 while (true) {
1135 BasicBlock* bb;
buzbee52a77fc2012-11-20 19:50:46 -08001136 bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -07001137 if (bb == NULL) break;
buzbeefa57c472012-11-21 12:06:18 -08001138 CountRefs(cu, bb, core_regs, FpRegs);
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 }
1140
1141 /*
1142 * Ideally, we'd allocate doubles starting with an even-numbered
1143 * register. Bias the counts to try to allocate any vreg that's
1144 * used as the start of a pair first.
1145 */
buzbeefa57c472012-11-21 12:06:18 -08001146 for (int i = 0; i < num_regs; i++) {
1147 if (FpRegs[i].double_start) {
buzbee52a77fc2012-11-20 19:50:46 -08001148 FpRegs[i].count *= 2;
buzbeee3acd072012-02-25 17:03:10 -08001149 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001150 }
1151
1152 // Sort the count arrays
buzbeefa57c472012-11-21 12:06:18 -08001153 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
1154 qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts);
Bill Buzbeea114add2012-05-03 15:00:40 -07001155
buzbeefa57c472012-11-21 12:06:18 -08001156 if (cu->verbose) {
1157 DumpCounts(core_regs, num_regs, "Core regs after sort");
1158 DumpCounts(FpRegs, num_regs, "Fp regs after sort");
Bill Buzbeea114add2012-05-03 15:00:40 -07001159 }
1160
buzbeefa57c472012-11-21 12:06:18 -08001161 if (!(cu->disable_opt & (1 << kPromoteRegs))) {
buzbee52a77fc2012-11-20 19:50:46 -08001162 // Promote FpRegs
buzbeefa57c472012-11-21 12:06:18 -08001163 for (int i = 0; (i < num_regs) &&
1164 (FpRegs[i].count >= promotion_threshold ); i++) {
1165 int p_map_idx = SRegToPMap(cu, FpRegs[i].s_reg);
1166 if (cu->promotion_map[p_map_idx].fp_location != kLocPhysReg) {
1167 int reg = AllocPreservedFPReg(cu, FpRegs[i].s_reg,
1168 FpRegs[i].double_start);
Bill Buzbeea114add2012-05-03 15:00:40 -07001169 if (reg < 0) {
1170 break; // No more left
1171 }
1172 }
buzbee239c4e72012-03-16 08:42:29 -07001173 }
buzbee9c044ce2012-03-18 13:24:07 -07001174
Bill Buzbeea114add2012-05-03 15:00:40 -07001175 // Promote core regs
buzbeefa57c472012-11-21 12:06:18 -08001176 for (int i = 0; (i < num_regs) &&
1177 (core_regs[i].count > promotion_threshold); i++) {
1178 int p_map_idx = SRegToPMap(cu, core_regs[i].s_reg);
1179 if (cu->promotion_map[p_map_idx].core_location !=
Bill Buzbeea114add2012-05-03 15:00:40 -07001180 kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -08001181 int reg = AllocPreservedCoreReg(cu, core_regs[i].s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001182 if (reg < 0) {
1183 break; // No more left
1184 }
1185 }
buzbeee3acd072012-02-25 17:03:10 -08001186 }
buzbeefa57c472012-11-21 12:06:18 -08001187 } else if (cu->qd_mode) {
1188 AllocPreservedCoreReg(cu, cu->method_sreg);
1189 for (int i = 0; i < num_regs; i++) {
1190 int reg = AllocPreservedCoreReg(cu, i);
Bill Buzbeea114add2012-05-03 15:00:40 -07001191 if (reg < 0) {
1192 break; // No more left
1193 }
buzbeee3acd072012-02-25 17:03:10 -08001194 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001195 }
buzbeee3acd072012-02-25 17:03:10 -08001196
buzbeee3acd072012-02-25 17:03:10 -08001197
Bill Buzbeea114add2012-05-03 15:00:40 -07001198 // Now, update SSA names to new home locations
buzbeefa57c472012-11-21 12:06:18 -08001199 for (int i = 0; i < cu->num_ssa_regs; i++) {
1200 RegLocation *curr = &cu->reg_location[i];
1201 int p_map_idx = SRegToPMap(cu, curr->s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001202 if (!curr->wide) {
1203 if (curr->fp) {
buzbeefa57c472012-11-21 12:06:18 -08001204 if (cu->promotion_map[p_map_idx].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001205 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001206 curr->low_reg = cu->promotion_map[p_map_idx].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001207 curr->home = true;
1208 }
1209 } else {
buzbeefa57c472012-11-21 12:06:18 -08001210 if (cu->promotion_map[p_map_idx].core_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001211 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001212 curr->low_reg = cu->promotion_map[p_map_idx].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001213 curr->home = true;
1214 }
1215 }
buzbeefa57c472012-11-21 12:06:18 -08001216 curr->high_reg = INVALID_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -07001217 } else {
buzbeefa57c472012-11-21 12:06:18 -08001218 if (curr->high_word) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001219 continue;
1220 }
1221 if (curr->fp) {
buzbeefa57c472012-11-21 12:06:18 -08001222 if ((cu->promotion_map[p_map_idx].fp_location == kLocPhysReg) &&
1223 (cu->promotion_map[p_map_idx+1].fp_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 kLocPhysReg)) {
buzbeefa57c472012-11-21 12:06:18 -08001225 int low_reg = cu->promotion_map[p_map_idx].FpReg;
1226 int high_reg = cu->promotion_map[p_map_idx+1].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001227 // Doubles require pair of singles starting at even reg
buzbeefa57c472012-11-21 12:06:18 -08001228 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001229 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001230 curr->low_reg = low_reg;
1231 curr->high_reg = high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001232 curr->home = true;
1233 }
1234 }
1235 } else {
buzbeefa57c472012-11-21 12:06:18 -08001236 if ((cu->promotion_map[p_map_idx].core_location == kLocPhysReg)
1237 && (cu->promotion_map[p_map_idx+1].core_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001238 kLocPhysReg)) {
1239 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001240 curr->low_reg = cu->promotion_map[p_map_idx].core_reg;
1241 curr->high_reg = cu->promotion_map[p_map_idx+1].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001242 curr->home = true;
1243 }
1244 }
buzbeee3acd072012-02-25 17:03:10 -08001245 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001246 }
buzbeefa57c472012-11-21 12:06:18 -08001247 if (cu->verbose) {
1248 DumpPromotionMap(cu);
buzbeeca7a5e42012-08-20 11:12:18 -07001249 }
buzbeee3acd072012-02-25 17:03:10 -08001250}
1251
1252/* Returns sp-relative offset in bytes for a VReg */
buzbeefa57c472012-11-21 12:06:18 -08001253int VRegOffset(CompilationUnit* cu, int v_reg)
buzbeee3acd072012-02-25 17:03:10 -08001254{
buzbeefa57c472012-11-21 12:06:18 -08001255 return StackVisitor::GetVRegOffset(cu->code_item, cu->core_spill_mask,
1256 cu->fp_spill_mask, cu->frame_size, v_reg);
buzbeee3acd072012-02-25 17:03:10 -08001257}
1258
1259/* Returns sp-relative offset in bytes for a SReg */
buzbeefa57c472012-11-21 12:06:18 -08001260int SRegOffset(CompilationUnit* cu, int s_reg)
buzbeee3acd072012-02-25 17:03:10 -08001261{
buzbeefa57c472012-11-21 12:06:18 -08001262 return VRegOffset(cu, SRegToVReg(cu, s_reg));
buzbeee3acd072012-02-25 17:03:10 -08001263}
1264
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001265} // namespace art