blob: a89cfa837bd0e5893bea85e3c3e9c21e1353cbd2 [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
buzbee395116c2013-02-27 14:30:25 -080019#include "compiler/dex/compiler_ir.h"
buzbee311ca162013-02-28 15:56:43 -080020#include "compiler/dex/compiler_internals.h"
buzbee395116c2013-02-27 14:30:25 -080021#include "compiler/dex/compiler_utility.h"
buzbee67bf8852011-08-17 17:51:35 -070022
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080023namespace art {
24
buzbee67bf8852011-08-17 17:51:35 -070025/*
26 * Free all allocated temps in the temp pools. Note that this does
27 * not affect the "liveness" of a temp register, which will stay
28 * live until it is either explicitly killed or reallocated.
29 */
buzbee1fd33462013-03-25 13:40:45 -070030void Mir2Lir::ResetRegPool()
buzbee67bf8852011-08-17 17:51:35 -070031{
Bill Buzbeea114add2012-05-03 15:00:40 -070032 int i;
buzbee1fd33462013-03-25 13:40:45 -070033 for (i=0; i < reg_pool_->num_core_regs; i++) {
34 if (reg_pool_->core_regs[i].is_temp)
35 reg_pool_->core_regs[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070036 }
buzbee1fd33462013-03-25 13:40:45 -070037 for (i=0; i < reg_pool_->num_fp_regs; i++) {
38 if (reg_pool_->FPRegs[i].is_temp)
39 reg_pool_->FPRegs[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070040 }
buzbee67bf8852011-08-17 17:51:35 -070041}
42
buzbeee3acd072012-02-25 17:03:10 -080043 /*
44 * Set up temp & preserved register pools specialized by target.
buzbeefa57c472012-11-21 12:06:18 -080045 * Note: num_regs may be zero.
buzbeee3acd072012-02-25 17:03:10 -080046 */
buzbee1fd33462013-03-25 13:40:45 -070047void Mir2Lir::CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num)
buzbee67bf8852011-08-17 17:51:35 -070048{
Bill Buzbeea114add2012-05-03 15:00:40 -070049 int i;
50 for (i=0; i < num; i++) {
buzbeefa57c472012-11-21 12:06:18 -080051 regs[i].reg = reg_nums[i];
52 regs[i].in_use = false;
53 regs[i].is_temp = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070054 regs[i].pair = false;
55 regs[i].live = false;
56 regs[i].dirty = false;
buzbeefa57c472012-11-21 12:06:18 -080057 regs[i].s_reg = INVALID_SREG;
Bill Buzbeea114add2012-05-03 15:00:40 -070058 }
buzbee67bf8852011-08-17 17:51:35 -070059}
60
buzbee1fd33462013-03-25 13:40:45 -070061void Mir2Lir::DumpRegPool(RegisterInfo* p, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -070062{
Bill Buzbeea114add2012-05-03 15:00:40 -070063 LOG(INFO) << "================================================";
buzbeefa57c472012-11-21 12:06:18 -080064 for (int i = 0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -070065 LOG(INFO) << StringPrintf(
66 "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 -080067 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
68 p[i].live, p[i].dirty, p[i].s_reg, reinterpret_cast<uintptr_t>(p[i].def_start),
69 reinterpret_cast<uintptr_t>(p[i].def_end));
Bill Buzbeea114add2012-05-03 15:00:40 -070070 }
71 LOG(INFO) << "================================================";
buzbee67bf8852011-08-17 17:51:35 -070072}
73
buzbee1fd33462013-03-25 13:40:45 -070074void Mir2Lir::DumpCoreRegPool()
buzbee6181f792011-09-29 11:14:04 -070075{
buzbee1fd33462013-03-25 13:40:45 -070076 DumpRegPool(reg_pool_->core_regs, reg_pool_->num_core_regs);
buzbee6181f792011-09-29 11:14:04 -070077}
78
buzbee1fd33462013-03-25 13:40:45 -070079void Mir2Lir::DumpFpRegPool()
buzbee6181f792011-09-29 11:14:04 -070080{
buzbee1fd33462013-03-25 13:40:45 -070081 DumpRegPool(reg_pool_->FPRegs, reg_pool_->num_fp_regs);
buzbee6181f792011-09-29 11:14:04 -070082}
83
buzbee67bf8852011-08-17 17:51:35 -070084/* Mark a temp register as dead. Does not affect allocation state. */
buzbee1fd33462013-03-25 13:40:45 -070085void Mir2Lir::ClobberBody(RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -070086{
buzbeefa57c472012-11-21 12:06:18 -080087 if (p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -070088 DCHECK(!(p->live && p->dirty)) << "Live & dirty temp in clobber";
89 p->live = false;
buzbeefa57c472012-11-21 12:06:18 -080090 p->s_reg = INVALID_SREG;
91 p->def_start = NULL;
92 p->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -070093 if (p->pair) {
94 p->pair = false;
buzbee1fd33462013-03-25 13:40:45 -070095 Clobber(p->partner);
buzbee67bf8852011-08-17 17:51:35 -070096 }
Bill Buzbeea114add2012-05-03 15:00:40 -070097 }
buzbee67bf8852011-08-17 17:51:35 -070098}
99
buzbee5abfa3e2012-01-31 17:01:43 -0800100/* Mark a temp register as dead. Does not affect allocation state. */
buzbee1fd33462013-03-25 13:40:45 -0700101void Mir2Lir::Clobber(int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800102{
buzbee1fd33462013-03-25 13:40:45 -0700103 ClobberBody(GetRegInfo(reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800104}
105
buzbee1fd33462013-03-25 13:40:45 -0700106void Mir2Lir::ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700107{
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 int i;
buzbeefa57c472012-11-21 12:06:18 -0800109 for (i=0; i< num_regs; i++) {
110 if (p[i].s_reg == s_reg) {
111 if (p[i].is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 p[i].live = false;
113 }
buzbeefa57c472012-11-21 12:06:18 -0800114 p[i].def_start = NULL;
115 p[i].def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700116 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700117 }
buzbee67bf8852011-08-17 17:51:35 -0700118}
119
buzbee078fa452012-12-03 15:51:33 -0800120/*
121 * Break the association between a Dalvik vreg and a physical temp register of either register
122 * class.
123 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
124 * in the register utilities, is is also used by code gen routines to work around a deficiency in
125 * local register allocation, which fails to distinguish between the "in" and "out" identities
126 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
127 * is used both as the source and destination register of an operation in which the type
128 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
129 * addressed.
130 */
buzbee1fd33462013-03-25 13:40:45 -0700131void Mir2Lir::ClobberSReg(int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700132{
Bill Buzbeea114add2012-05-03 15:00:40 -0700133 /* Reset live temp tracking sanity checker */
buzbee311ca162013-02-28 15:56:43 -0800134 if (kIsDebugBuild) {
buzbee1fd33462013-03-25 13:40:45 -0700135 if (s_reg == live_sreg_) {
136 live_sreg_ = INVALID_SREG;
buzbee311ca162013-02-28 15:56:43 -0800137 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700138 }
buzbee1fd33462013-03-25 13:40:45 -0700139 ClobberSRegBody(reg_pool_->core_regs, reg_pool_->num_core_regs, s_reg);
140 ClobberSRegBody(reg_pool_->FPRegs, reg_pool_->num_fp_regs, s_reg);
buzbee67bf8852011-08-17 17:51:35 -0700141}
142
buzbee9c044ce2012-03-18 13:24:07 -0700143/*
144 * SSA names associated with the initial definitions of Dalvik
145 * registers are the same as the Dalvik register number (and
buzbeefa57c472012-11-21 12:06:18 -0800146 * thus take the same position in the promotion_map. However,
buzbee9c044ce2012-03-18 13:24:07 -0700147 * the special Method* and compiler temp resisters use negative
buzbeefa57c472012-11-21 12:06:18 -0800148 * v_reg numbers to distinguish them and can have an arbitrary
buzbee9c044ce2012-03-18 13:24:07 -0700149 * ssa name (above the last original Dalvik register). This function
buzbeefa57c472012-11-21 12:06:18 -0800150 * maps SSA names to positions in the promotion_map array.
buzbee9c044ce2012-03-18 13:24:07 -0700151 */
buzbee1fd33462013-03-25 13:40:45 -0700152int Mir2Lir::SRegToPMap(int s_reg)
buzbeee1965672012-03-11 18:39:19 -0700153{
buzbee1fd33462013-03-25 13:40:45 -0700154 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
buzbeefa57c472012-11-21 12:06:18 -0800155 DCHECK_GE(s_reg, 0);
buzbee1fd33462013-03-25 13:40:45 -0700156 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbeefa57c472012-11-21 12:06:18 -0800157 if (v_reg >= 0) {
buzbee1fd33462013-03-25 13:40:45 -0700158 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
buzbeefa57c472012-11-21 12:06:18 -0800159 return v_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 } else {
buzbeefa57c472012-11-21 12:06:18 -0800161 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
buzbee1fd33462013-03-25 13:40:45 -0700162 DCHECK_LE(pos, cu_->num_compiler_temps);
163 return cu_->num_dalvik_registers + pos;
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 }
buzbeee1965672012-03-11 18:39:19 -0700165}
166
buzbee1fd33462013-03-25 13:40:45 -0700167void Mir2Lir::RecordCorePromotion(int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700168{
buzbee1fd33462013-03-25 13:40:45 -0700169 int p_map_idx = SRegToPMap(s_reg);
170 int v_reg = mir_graph_->SRegToVReg(s_reg);
171 GetRegInfo(reg)->in_use = true;
172 core_spill_mask_ |= (1 << reg);
buzbeeca7a5e42012-08-20 11:12:18 -0700173 // Include reg for later sort
buzbee1fd33462013-03-25 13:40:45 -0700174 core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
175 num_core_spills_++;
176 promotion_map_[p_map_idx].core_location = kLocPhysReg;
177 promotion_map_[p_map_idx].core_reg = reg;
buzbeeca7a5e42012-08-20 11:12:18 -0700178}
179
buzbee67bf8852011-08-17 17:51:35 -0700180/* Reserve a callee-save register. Return -1 if none available */
buzbee1fd33462013-03-25 13:40:45 -0700181int Mir2Lir::AllocPreservedCoreReg(int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700182{
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 int res = -1;
buzbee1fd33462013-03-25 13:40:45 -0700184 RegisterInfo* core_regs = reg_pool_->core_regs;
185 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800186 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
187 res = core_regs[i].reg;
buzbee1fd33462013-03-25 13:40:45 -0700188 RecordCorePromotion(res, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 break;
buzbee67bf8852011-08-17 17:51:35 -0700190 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 }
192 return res;
buzbee67bf8852011-08-17 17:51:35 -0700193}
194
buzbee1fd33462013-03-25 13:40:45 -0700195void Mir2Lir::RecordFpPromotion(int reg, int s_reg)
buzbeeca7a5e42012-08-20 11:12:18 -0700196{
buzbee1fd33462013-03-25 13:40:45 -0700197 int p_map_idx = SRegToPMap(s_reg);
198 int v_reg = mir_graph_->SRegToVReg(s_reg);
199 GetRegInfo(reg)->in_use = true;
200 MarkPreservedSingle(v_reg, reg);
201 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
202 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 */
buzbee1fd33462013-03-25 13:40:45 -0700210int Mir2Lir::AllocPreservedSingle(int s_reg, bool even)
buzbee67bf8852011-08-17 17:51:35 -0700211{
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 int res = -1;
buzbee1fd33462013-03-25 13:40:45 -0700213 RegisterInfo* FPRegs = reg_pool_->FPRegs;
214 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800215 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;
buzbee1fd33462013-03-25 13:40:45 -0700218 RecordFpPromotion(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 */
buzbee1fd33462013-03-25 13:40:45 -0700233int Mir2Lir::AllocPreservedDouble(int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700234{
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 int res = -1; // Assume failure
buzbee1fd33462013-03-25 13:40:45 -0700236 int v_reg = mir_graph_->SRegToVReg(s_reg);
237 int p_map_idx = SRegToPMap(s_reg);
238 if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 // Upper reg is already allocated. Can we fit?
buzbee1fd33462013-03-25 13:40:45 -0700240 int high_reg = promotion_map_[p_map_idx+1].FpReg;
buzbeefa57c472012-11-21 12:06:18 -0800241 if ((high_reg & 1) == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 // High reg is even - fail.
243 return res;
244 }
245 // Is the low reg of the pair free?
buzbee1fd33462013-03-25 13:40:45 -0700246 RegisterInfo* p = GetRegInfo(high_reg-1);
buzbeefa57c472012-11-21 12:06:18 -0800247 if (p->in_use || p->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 // Already allocated or not preserved - fail.
249 return res;
250 }
251 // OK - good to go.
252 res = p->reg;
buzbeefa57c472012-11-21 12:06:18 -0800253 p->in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 DCHECK_EQ((res & 1), 0);
buzbee1fd33462013-03-25 13:40:45 -0700255 MarkPreservedSingle(v_reg, res);
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 } else {
buzbee1fd33462013-03-25 13:40:45 -0700257 RegisterInfo* FPRegs = reg_pool_->FPRegs;
258 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800259 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 ((FPRegs[i].reg & 0x1) == 0x0) &&
buzbeefa57c472012-11-21 12:06:18 -0800261 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
263 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
264 res = FPRegs[i].reg;
buzbeefa57c472012-11-21 12:06:18 -0800265 FPRegs[i].in_use = true;
buzbee1fd33462013-03-25 13:40:45 -0700266 MarkPreservedSingle(v_reg, res);
buzbeefa57c472012-11-21 12:06:18 -0800267 FPRegs[i+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
buzbee1fd33462013-03-25 13:40:45 -0700269 MarkPreservedSingle(v_reg+1, res+1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 break;
271 }
buzbee67bf8852011-08-17 17:51:35 -0700272 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 }
274 if (res != -1) {
buzbee1fd33462013-03-25 13:40:45 -0700275 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
276 promotion_map_[p_map_idx].FpReg = res;
277 promotion_map_[p_map_idx+1].fp_location = kLocPhysReg;
278 promotion_map_[p_map_idx+1].FpReg = res + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 }
280 return res;
buzbee67bf8852011-08-17 17:51:35 -0700281}
282
283
284/*
285 * Reserve a callee-save fp register. If this register can be used
286 * as the first of a double, attempt to allocate an even pair of fp
287 * single regs (but if can't still attempt to allocate a single, preferring
288 * first to allocate an odd register.
289 */
buzbee1fd33462013-03-25 13:40:45 -0700290int Mir2Lir::AllocPreservedFPReg(int s_reg, bool double_start)
buzbee67bf8852011-08-17 17:51:35 -0700291{
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 int res = -1;
buzbeefa57c472012-11-21 12:06:18 -0800293 if (double_start) {
buzbee1fd33462013-03-25 13:40:45 -0700294 res = AllocPreservedDouble(s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700295 }
296 if (res == -1) {
buzbee1fd33462013-03-25 13:40:45 -0700297 res = AllocPreservedSingle(s_reg, false /* try odd # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 }
299 if (res == -1)
buzbee1fd33462013-03-25 13:40:45 -0700300 res = AllocPreservedSingle(s_reg, true /* try even # */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 return res;
buzbee67bf8852011-08-17 17:51:35 -0700302}
303
buzbee1fd33462013-03-25 13:40:45 -0700304int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
305 bool required)
buzbee67bf8852011-08-17 17:51:35 -0700306{
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 int i;
buzbeefa57c472012-11-21 12:06:18 -0800308 int next = *next_temp;
309 for (i=0; i< num_regs; i++) {
310 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800312 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
buzbee1fd33462013-03-25 13:40:45 -0700313 Clobber(p[next].reg);
buzbeefa57c472012-11-21 12:06:18 -0800314 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800316 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700318 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 next++;
320 }
buzbeefa57c472012-11-21 12:06:18 -0800321 next = *next_temp;
322 for (i=0; i< num_regs; i++) {
323 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800325 if (p[next].is_temp && !p[next].in_use) {
buzbee1fd33462013-03-25 13:40:45 -0700326 Clobber(p[next].reg);
buzbeefa57c472012-11-21 12:06:18 -0800327 p[next].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700328 p[next].pair = false;
buzbeefa57c472012-11-21 12:06:18 -0800329 *next_temp = next + 1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700331 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 next++;
333 }
334 if (required) {
buzbee1fd33462013-03-25 13:40:45 -0700335 CodegenDump();
336 DumpRegPool(reg_pool_->core_regs,
337 reg_pool_->num_core_regs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 LOG(FATAL) << "No free temp registers";
339 }
340 return -1; // No register available
buzbee67bf8852011-08-17 17:51:35 -0700341}
342
343//REDO: too many assumptions.
buzbee1fd33462013-03-25 13:40:45 -0700344int Mir2Lir::AllocTempDouble()
buzbee67bf8852011-08-17 17:51:35 -0700345{
buzbee1fd33462013-03-25 13:40:45 -0700346 RegisterInfo* p = reg_pool_->FPRegs;
347 int num_regs = reg_pool_->num_fp_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 /* Start looking at an even reg */
buzbee1fd33462013-03-25 13:40:45 -0700349 int next = reg_pool_->next_fp_reg & ~0x1;
buzbee67bf8852011-08-17 17:51:35 -0700350
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 // First try to avoid allocating live registers
buzbeefa57c472012-11-21 12:06:18 -0800352 for (int i=0; i < num_regs; i+=2) {
353 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800355 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
356 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
buzbee1fd33462013-03-25 13:40:45 -0700357 Clobber(p[next].reg);
358 Clobber(p[next+1].reg);
buzbeefa57c472012-11-21 12:06:18 -0800359 p[next].in_use = true;
360 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
362 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbee1fd33462013-03-25 13:40:45 -0700363 reg_pool_->next_fp_reg = next + 2;
364 if (reg_pool_->next_fp_reg >= num_regs) {
365 reg_pool_->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 }
367 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700368 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 next += 2;
370 }
buzbee1fd33462013-03-25 13:40:45 -0700371 next = reg_pool_->next_fp_reg & ~0x1;
buzbeea50638b2011-11-02 15:15:06 -0700372
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 // No choice - find a pair and kill it.
buzbeefa57c472012-11-21 12:06:18 -0800374 for (int i=0; i < num_regs; i+=2) {
375 if (next >= num_regs)
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 next = 0;
buzbeefa57c472012-11-21 12:06:18 -0800377 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
378 !p[next+1].in_use) {
buzbee1fd33462013-03-25 13:40:45 -0700379 Clobber(p[next].reg);
380 Clobber(p[next+1].reg);
buzbeefa57c472012-11-21 12:06:18 -0800381 p[next].in_use = true;
382 p[next+1].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
384 DCHECK_EQ((p[next].reg & 0x1), 0);
buzbee1fd33462013-03-25 13:40:45 -0700385 reg_pool_->next_fp_reg = next + 2;
386 if (reg_pool_->next_fp_reg >= num_regs) {
387 reg_pool_->next_fp_reg = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 }
389 return p[next].reg;
buzbee67bf8852011-08-17 17:51:35 -0700390 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700391 next += 2;
392 }
393 LOG(FATAL) << "No free temp registers (pair)";
394 return -1;
buzbee67bf8852011-08-17 17:51:35 -0700395}
396
397/* Return a temp if one is available, -1 otherwise */
buzbee1fd33462013-03-25 13:40:45 -0700398int Mir2Lir::AllocFreeTemp()
buzbee67bf8852011-08-17 17:51:35 -0700399{
buzbee1fd33462013-03-25 13:40:45 -0700400 return AllocTempBody(reg_pool_->core_regs,
401 reg_pool_->num_core_regs,
402 &reg_pool_->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700403}
404
buzbee1fd33462013-03-25 13:40:45 -0700405int Mir2Lir::AllocTemp()
buzbee67bf8852011-08-17 17:51:35 -0700406{
buzbee1fd33462013-03-25 13:40:45 -0700407 return AllocTempBody(reg_pool_->core_regs,
408 reg_pool_->num_core_regs,
409 &reg_pool_->next_core_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700410}
411
buzbee1fd33462013-03-25 13:40:45 -0700412int Mir2Lir::AllocTempFloat()
buzbee67bf8852011-08-17 17:51:35 -0700413{
buzbee1fd33462013-03-25 13:40:45 -0700414 return AllocTempBody(reg_pool_->FPRegs,
415 reg_pool_->num_fp_regs,
416 &reg_pool_->next_fp_reg, true);
buzbee67bf8852011-08-17 17:51:35 -0700417}
418
buzbee1fd33462013-03-25 13:40:45 -0700419Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700420{
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 int i;
buzbeefa57c472012-11-21 12:06:18 -0800422 if (s_reg == -1)
buzbee67bf8852011-08-17 17:51:35 -0700423 return NULL;
buzbeefa57c472012-11-21 12:06:18 -0800424 for (i=0; i < num_regs; i++) {
425 if (p[i].live && (p[i].s_reg == s_reg)) {
426 if (p[i].is_temp)
427 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 return &p[i];
429 }
430 }
431 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700432}
433
buzbee1fd33462013-03-25 13:40:45 -0700434Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class)
buzbee67bf8852011-08-17 17:51:35 -0700435{
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 RegisterInfo* res = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800437 switch (reg_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 case kAnyReg:
buzbee1fd33462013-03-25 13:40:45 -0700439 res = AllocLiveBody(reg_pool_->FPRegs,
440 reg_pool_->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 if (res)
442 break;
443 /* Intentional fallthrough */
444 case kCoreReg:
buzbee1fd33462013-03-25 13:40:45 -0700445 res = AllocLiveBody(reg_pool_->core_regs,
446 reg_pool_->num_core_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 break;
448 case kFPReg:
buzbee1fd33462013-03-25 13:40:45 -0700449 res = AllocLiveBody(reg_pool_->FPRegs,
450 reg_pool_->num_fp_regs, s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700451 break;
452 default:
453 LOG(FATAL) << "Invalid register type";
454 }
455 return res;
buzbee67bf8852011-08-17 17:51:35 -0700456}
457
buzbee1fd33462013-03-25 13:40:45 -0700458void Mir2Lir::FreeTemp(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700459{
buzbee1fd33462013-03-25 13:40:45 -0700460 RegisterInfo* p = reg_pool_->core_regs;
461 int num_regs = reg_pool_->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 int i;
buzbeefa57c472012-11-21 12:06:18 -0800463 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700464 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800465 if (p[i].is_temp) {
466 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700467 }
468 p[i].pair = false;
469 return;
buzbee67bf8852011-08-17 17:51:35 -0700470 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 }
buzbee1fd33462013-03-25 13:40:45 -0700472 p = reg_pool_->FPRegs;
473 num_regs = reg_pool_->num_fp_regs;
buzbeefa57c472012-11-21 12:06:18 -0800474 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800476 if (p[i].is_temp) {
477 p[i].in_use = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 }
479 p[i].pair = false;
480 return;
buzbee67bf8852011-08-17 17:51:35 -0700481 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 }
483 LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700484}
485
buzbee1fd33462013-03-25 13:40:45 -0700486Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700487{
buzbee1fd33462013-03-25 13:40:45 -0700488 RegisterInfo* p = reg_pool_->core_regs;
489 int num_regs = reg_pool_->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 int i;
buzbeefa57c472012-11-21 12:06:18 -0800491 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 }
buzbee1fd33462013-03-25 13:40:45 -0700496 p = reg_pool_->FPRegs;
497 num_regs = reg_pool_->num_fp_regs;
buzbeefa57c472012-11-21 12:06:18 -0800498 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 if (p[i].reg == reg) {
500 return p[i].live ? &p[i] : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700501 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700502 }
503 return NULL;
buzbee67bf8852011-08-17 17:51:35 -0700504}
505
buzbee1fd33462013-03-25 13:40:45 -0700506Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700507{
buzbee1fd33462013-03-25 13:40:45 -0700508 RegisterInfo* p = GetRegInfo(reg);
buzbeefa57c472012-11-21 12:06:18 -0800509 return (p->is_temp) ? p : NULL;
buzbee67bf8852011-08-17 17:51:35 -0700510}
511
buzbee1fd33462013-03-25 13:40:45 -0700512Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg)
buzbeeb29e4d12011-09-26 15:05:48 -0700513{
buzbee1fd33462013-03-25 13:40:45 -0700514 RegisterInfo* p = GetRegInfo(reg);
buzbeefa57c472012-11-21 12:06:18 -0800515 return (p->is_temp) ? NULL : p;
buzbeeb29e4d12011-09-26 15:05:48 -0700516}
517
buzbee1fd33462013-03-25 13:40:45 -0700518bool Mir2Lir::IsDirty(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700519{
buzbee1fd33462013-03-25 13:40:45 -0700520 RegisterInfo* p = GetRegInfo(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 return p->dirty;
buzbee67bf8852011-08-17 17:51:35 -0700522}
523
524/*
buzbee52a77fc2012-11-20 19:50:46 -0800525 * Similar to AllocTemp(), but forces the allocation of a specific
buzbee67bf8852011-08-17 17:51:35 -0700526 * register. No check is made to see if the register was previously
527 * allocated. Use with caution.
528 */
buzbee1fd33462013-03-25 13:40:45 -0700529void Mir2Lir::LockTemp(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700530{
buzbee1fd33462013-03-25 13:40:45 -0700531 RegisterInfo* p = reg_pool_->core_regs;
532 int num_regs = reg_pool_->num_core_regs;
Bill Buzbeea114add2012-05-03 15:00:40 -0700533 int i;
buzbeefa57c472012-11-21 12:06:18 -0800534 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800536 DCHECK(p[i].is_temp);
537 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 p[i].live = false;
539 return;
buzbee67bf8852011-08-17 17:51:35 -0700540 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 }
buzbee1fd33462013-03-25 13:40:45 -0700542 p = reg_pool_->FPRegs;
543 num_regs = reg_pool_->num_fp_regs;
buzbeefa57c472012-11-21 12:06:18 -0800544 for (i=0; i< num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 if (p[i].reg == reg) {
buzbeefa57c472012-11-21 12:06:18 -0800546 DCHECK(p[i].is_temp);
547 p[i].in_use = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 p[i].live = false;
549 return;
buzbee67bf8852011-08-17 17:51:35 -0700550 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 }
552 LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
buzbee67bf8852011-08-17 17:51:35 -0700553}
554
buzbee1fd33462013-03-25 13:40:45 -0700555void Mir2Lir::ResetDefBody(RegisterInfo* p)
buzbee67bf8852011-08-17 17:51:35 -0700556{
buzbeefa57c472012-11-21 12:06:18 -0800557 p->def_start = NULL;
558 p->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700559}
560
buzbee1fd33462013-03-25 13:40:45 -0700561void Mir2Lir::ResetDef(int reg)
buzbee5abfa3e2012-01-31 17:01:43 -0800562{
buzbee1fd33462013-03-25 13:40:45 -0700563 ResetDefBody(GetRegInfo(reg));
buzbee5abfa3e2012-01-31 17:01:43 -0800564}
565
buzbee1fd33462013-03-25 13:40:45 -0700566void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2)
buzbee67bf8852011-08-17 17:51:35 -0700567{
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 if (start && finish) {
569 LIR *p;
buzbeefa57c472012-11-21 12:06:18 -0800570 DCHECK_EQ(s_reg1, s_reg2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 for (p = start; ;p = p->next) {
buzbee52a77fc2012-11-20 19:50:46 -0800572 NopLIR(p);
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 if (p == finish)
574 break;
buzbee67bf8852011-08-17 17:51:35 -0700575 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 }
buzbee67bf8852011-08-17 17:51:35 -0700577}
578
579/*
580 * Mark the beginning and end LIR of a def sequence. Note that
581 * on entry start points to the LIR prior to the beginning of the
582 * sequence.
583 */
buzbee1fd33462013-03-25 13:40:45 -0700584void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700585{
Bill Buzbeea114add2012-05-03 15:00:40 -0700586 DCHECK(!rl.wide);
587 DCHECK(start && start->next);
588 DCHECK(finish);
buzbee1fd33462013-03-25 13:40:45 -0700589 RegisterInfo* p = GetRegInfo(rl.low_reg);
buzbeefa57c472012-11-21 12:06:18 -0800590 p->def_start = start->next;
591 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700592}
593
594/*
595 * Mark the beginning and end LIR of a def sequence. Note that
596 * on entry start points to the LIR prior to the beginning of the
597 * sequence.
598 */
buzbee1fd33462013-03-25 13:40:45 -0700599void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish)
buzbee67bf8852011-08-17 17:51:35 -0700600{
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 DCHECK(rl.wide);
602 DCHECK(start && start->next);
603 DCHECK(finish);
buzbee1fd33462013-03-25 13:40:45 -0700604 RegisterInfo* p = GetRegInfo(rl.low_reg);
605 ResetDef(rl.high_reg); // Only track low of pair
buzbeefa57c472012-11-21 12:06:18 -0800606 p->def_start = start->next;
607 p->def_end = finish;
buzbee67bf8852011-08-17 17:51:35 -0700608}
609
buzbee1fd33462013-03-25 13:40:45 -0700610RegLocation Mir2Lir::WideToNarrow(RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700611{
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 DCHECK(rl.wide);
613 if (rl.location == kLocPhysReg) {
buzbee1fd33462013-03-25 13:40:45 -0700614 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
615 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800616 if (info_lo->is_temp) {
617 info_lo->pair = false;
618 info_lo->def_start = NULL;
619 info_lo->def_end = NULL;
buzbee67bf8852011-08-17 17:51:35 -0700620 }
buzbeefa57c472012-11-21 12:06:18 -0800621 if (info_hi->is_temp) {
622 info_hi->pair = false;
623 info_hi->def_start = NULL;
624 info_hi->def_end = NULL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700625 }
626 }
627 rl.wide = false;
628 return rl;
buzbee67bf8852011-08-17 17:51:35 -0700629}
630
buzbee1fd33462013-03-25 13:40:45 -0700631void Mir2Lir::ResetDefLoc(RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700632{
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 DCHECK(!rl.wide);
buzbee1fd33462013-03-25 13:40:45 -0700634 RegisterInfo* p = IsTemp(rl.low_reg);
635 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 DCHECK(!p->pair);
buzbee1fd33462013-03-25 13:40:45 -0700637 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700638 }
buzbee1fd33462013-03-25 13:40:45 -0700639 ResetDef(rl.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700640}
641
buzbee1fd33462013-03-25 13:40:45 -0700642void Mir2Lir::ResetDefLocWide(RegLocation rl)
buzbee67bf8852011-08-17 17:51:35 -0700643{
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 DCHECK(rl.wide);
buzbee1fd33462013-03-25 13:40:45 -0700645 RegisterInfo* p_low = IsTemp(rl.low_reg);
646 RegisterInfo* p_high = IsTemp(rl.high_reg);
647 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
buzbeefa57c472012-11-21 12:06:18 -0800648 DCHECK(p_low->pair);
buzbee1fd33462013-03-25 13:40:45 -0700649 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700650 }
buzbee1fd33462013-03-25 13:40:45 -0700651 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
buzbeefa57c472012-11-21 12:06:18 -0800652 DCHECK(p_high->pair);
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 }
buzbee1fd33462013-03-25 13:40:45 -0700654 ResetDef(rl.low_reg);
655 ResetDef(rl.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700656}
657
buzbee1fd33462013-03-25 13:40:45 -0700658void Mir2Lir::ResetDefTracking()
buzbee67bf8852011-08-17 17:51:35 -0700659{
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 int i;
buzbee1fd33462013-03-25 13:40:45 -0700661 for (i=0; i< reg_pool_->num_core_regs; i++) {
662 ResetDefBody(&reg_pool_->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 }
buzbee1fd33462013-03-25 13:40:45 -0700664 for (i=0; i< reg_pool_->num_fp_regs; i++) {
665 ResetDefBody(&reg_pool_->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 }
buzbee67bf8852011-08-17 17:51:35 -0700667}
668
buzbee1fd33462013-03-25 13:40:45 -0700669void Mir2Lir::ClobberAllRegs()
buzbee67bf8852011-08-17 17:51:35 -0700670{
Bill Buzbeea114add2012-05-03 15:00:40 -0700671 int i;
buzbee1fd33462013-03-25 13:40:45 -0700672 for (i=0; i< reg_pool_->num_core_regs; i++) {
673 ClobberBody(&reg_pool_->core_regs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 }
buzbee1fd33462013-03-25 13:40:45 -0700675 for (i=0; i< reg_pool_->num_fp_regs; i++) {
676 ClobberBody(&reg_pool_->FPRegs[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 }
buzbee67bf8852011-08-17 17:51:35 -0700678}
679
buzbee67bf8852011-08-17 17:51:35 -0700680// Make sure nothing is live and dirty
buzbee1fd33462013-03-25 13:40:45 -0700681void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs)
buzbee67bf8852011-08-17 17:51:35 -0700682{
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 int i;
buzbeefa57c472012-11-21 12:06:18 -0800684 for (i=0; i < num_regs; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700685 if (info[i].live && info[i].dirty) {
686 if (info[i].pair) {
buzbee1fd33462013-03-25 13:40:45 -0700687 FlushRegWide(info[i].reg, info[i].partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700688 } else {
buzbee1fd33462013-03-25 13:40:45 -0700689 FlushReg(info[i].reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700690 }
buzbee67bf8852011-08-17 17:51:35 -0700691 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700692 }
buzbee67bf8852011-08-17 17:51:35 -0700693}
694
buzbee1fd33462013-03-25 13:40:45 -0700695void Mir2Lir::FlushAllRegs()
buzbee67bf8852011-08-17 17:51:35 -0700696{
buzbee1fd33462013-03-25 13:40:45 -0700697 FlushAllRegsBody(reg_pool_->core_regs,
698 reg_pool_->num_core_regs);
699 FlushAllRegsBody(reg_pool_->FPRegs,
700 reg_pool_->num_fp_regs);
701 ClobberAllRegs();
buzbee67bf8852011-08-17 17:51:35 -0700702}
703
704
705//TUNING: rewrite all of this reg stuff. Probably use an attribute table
buzbee1fd33462013-03-25 13:40:45 -0700706bool Mir2Lir::RegClassMatches(int reg_class, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700707{
buzbeefa57c472012-11-21 12:06:18 -0800708 if (reg_class == kAnyReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 return true;
buzbeefa57c472012-11-21 12:06:18 -0800710 } else if (reg_class == kCoreReg) {
buzbee1fd33462013-03-25 13:40:45 -0700711 return !IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700712 } else {
buzbee1fd33462013-03-25 13:40:45 -0700713 return IsFpReg(reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 }
buzbee67bf8852011-08-17 17:51:35 -0700715}
716
buzbee1fd33462013-03-25 13:40:45 -0700717void Mir2Lir::MarkLive(int reg, int s_reg)
buzbee67bf8852011-08-17 17:51:35 -0700718{
buzbee1fd33462013-03-25 13:40:45 -0700719 RegisterInfo* info = GetRegInfo(reg);
buzbeefa57c472012-11-21 12:06:18 -0800720 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 return; /* already live */
buzbeefa57c472012-11-21 12:06:18 -0800722 } else if (s_reg != INVALID_SREG) {
buzbee1fd33462013-03-25 13:40:45 -0700723 ClobberSReg(s_reg);
buzbeefa57c472012-11-21 12:06:18 -0800724 if (info->is_temp) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 info->live = true;
buzbee67bf8852011-08-17 17:51:35 -0700726 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700727 } else {
buzbeefa57c472012-11-21 12:06:18 -0800728 /* Can't be live if no associated s_reg */
729 DCHECK(info->is_temp);
Bill Buzbeea114add2012-05-03 15:00:40 -0700730 info->live = false;
731 }
buzbeefa57c472012-11-21 12:06:18 -0800732 info->s_reg = s_reg;
buzbee67bf8852011-08-17 17:51:35 -0700733}
734
buzbee1fd33462013-03-25 13:40:45 -0700735void Mir2Lir::MarkTemp(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700736{
buzbee1fd33462013-03-25 13:40:45 -0700737 RegisterInfo* info = GetRegInfo(reg);
buzbeefa57c472012-11-21 12:06:18 -0800738 info->is_temp = true;
buzbee67bf8852011-08-17 17:51:35 -0700739}
740
buzbee1fd33462013-03-25 13:40:45 -0700741void Mir2Lir::UnmarkTemp(int reg)
buzbee9e0f9b02011-08-24 15:32:46 -0700742{
buzbee1fd33462013-03-25 13:40:45 -0700743 RegisterInfo* info = GetRegInfo(reg);
buzbeefa57c472012-11-21 12:06:18 -0800744 info->is_temp = false;
buzbee9e0f9b02011-08-24 15:32:46 -0700745}
746
buzbee1fd33462013-03-25 13:40:45 -0700747void Mir2Lir::MarkPair(int low_reg, int high_reg)
buzbee67bf8852011-08-17 17:51:35 -0700748{
buzbee1fd33462013-03-25 13:40:45 -0700749 RegisterInfo* info_lo = GetRegInfo(low_reg);
750 RegisterInfo* info_hi = GetRegInfo(high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800751 info_lo->pair = info_hi->pair = true;
752 info_lo->partner = high_reg;
753 info_hi->partner = low_reg;
buzbee67bf8852011-08-17 17:51:35 -0700754}
755
buzbee1fd33462013-03-25 13:40:45 -0700756void Mir2Lir::MarkClean(RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700757{
buzbee1fd33462013-03-25 13:40:45 -0700758 RegisterInfo* info = GetRegInfo(loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700759 info->dirty = false;
760 if (loc.wide) {
buzbee1fd33462013-03-25 13:40:45 -0700761 info = GetRegInfo(loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700762 info->dirty = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700763 }
buzbee67bf8852011-08-17 17:51:35 -0700764}
765
buzbee1fd33462013-03-25 13:40:45 -0700766void Mir2Lir::MarkDirty(RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700767{
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 if (loc.home) {
769 // If already home, can't be dirty
770 return;
771 }
buzbee1fd33462013-03-25 13:40:45 -0700772 RegisterInfo* info = GetRegInfo(loc.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 info->dirty = true;
774 if (loc.wide) {
buzbee1fd33462013-03-25 13:40:45 -0700775 info = GetRegInfo(loc.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700776 info->dirty = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700777 }
buzbee67bf8852011-08-17 17:51:35 -0700778}
779
buzbee1fd33462013-03-25 13:40:45 -0700780void Mir2Lir::MarkInUse(int reg)
buzbee67bf8852011-08-17 17:51:35 -0700781{
buzbee1fd33462013-03-25 13:40:45 -0700782 RegisterInfo* info = GetRegInfo(reg);
buzbeefa57c472012-11-21 12:06:18 -0800783 info->in_use = true;
buzbee67bf8852011-08-17 17:51:35 -0700784}
785
buzbee1fd33462013-03-25 13:40:45 -0700786void Mir2Lir::CopyRegInfo(int new_reg, int old_reg)
buzbee67bf8852011-08-17 17:51:35 -0700787{
buzbee1fd33462013-03-25 13:40:45 -0700788 RegisterInfo* new_info = GetRegInfo(new_reg);
789 RegisterInfo* old_info = GetRegInfo(old_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 // Target temp status must not change
buzbeefa57c472012-11-21 12:06:18 -0800791 bool is_temp = new_info->is_temp;
792 *new_info = *old_info;
Bill Buzbeea114add2012-05-03 15:00:40 -0700793 // Restore target's temp status
buzbeefa57c472012-11-21 12:06:18 -0800794 new_info->is_temp = is_temp;
795 new_info->reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700796}
797
buzbee1fd33462013-03-25 13:40:45 -0700798bool Mir2Lir::CheckCorePoolSanity()
buzbee6181f792011-09-29 11:14:04 -0700799{
buzbee1fd33462013-03-25 13:40:45 -0700800 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
801 if (reg_pool_->core_regs[i].pair) {
802 static int my_reg = reg_pool_->core_regs[i].reg;
803 static int my_sreg = reg_pool_->core_regs[i].s_reg;
804 static int partner_reg = reg_pool_->core_regs[i].partner;
805 static RegisterInfo* partner = GetRegInfo(partner_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700806 DCHECK(partner != NULL);
807 DCHECK(partner->pair);
buzbeefa57c472012-11-21 12:06:18 -0800808 DCHECK_EQ(my_reg, partner->partner);
809 static int partner_sreg = partner->s_reg;
810 if (my_sreg == INVALID_SREG) {
811 DCHECK_EQ(partner_sreg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700812 } else {
buzbeefa57c472012-11-21 12:06:18 -0800813 int diff = my_sreg - partner_sreg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700814 DCHECK((diff == -1) || (diff == 1));
buzbee6181f792011-09-29 11:14:04 -0700815 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 }
buzbee1fd33462013-03-25 13:40:45 -0700817 if (!reg_pool_->core_regs[i].live) {
818 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
819 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700820 }
buzbee6181f792011-09-29 11:14:04 -0700821 }
822 return true;
823}
824
buzbeeaad94382012-11-21 07:40:50 -0800825/*
826 * Return an updated location record with current in-register status.
827 * If the value lives in live temps, reflect that fact. No code
828 * is generated. If the live value is part of an older pair,
829 * clobber both low and high.
830 * TUNING: clobbering both is a bit heavy-handed, but the alternative
831 * is a bit complex when dealing with FP regs. Examine code to see
832 * if it's worthwhile trying to be more clever here.
833 */
834
buzbee1fd33462013-03-25 13:40:45 -0700835RegLocation Mir2Lir::UpdateLoc(RegLocation loc)
buzbeeaad94382012-11-21 07:40:50 -0800836{
837 DCHECK(!loc.wide);
buzbee1fd33462013-03-25 13:40:45 -0700838 DCHECK(CheckCorePoolSanity());
buzbeeaad94382012-11-21 07:40:50 -0800839 if (loc.location != kLocPhysReg) {
840 DCHECK((loc.location == kLocDalvikFrame) ||
841 (loc.location == kLocCompilerTemp));
buzbee1fd33462013-03-25 13:40:45 -0700842 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
buzbeefa57c472012-11-21 12:06:18 -0800843 if (info_lo) {
844 if (info_lo->pair) {
buzbee1fd33462013-03-25 13:40:45 -0700845 Clobber(info_lo->reg);
846 Clobber(info_lo->partner);
847 FreeTemp(info_lo->reg);
buzbeeaad94382012-11-21 07:40:50 -0800848 } else {
buzbeefa57c472012-11-21 12:06:18 -0800849 loc.low_reg = info_lo->reg;
buzbeeaad94382012-11-21 07:40:50 -0800850 loc.location = kLocPhysReg;
851 }
852 }
853 }
854
855 return loc;
856}
857
buzbeefa57c472012-11-21 12:06:18 -0800858/* see comments for update_loc */
buzbee1fd33462013-03-25 13:40:45 -0700859RegLocation Mir2Lir::UpdateLocWide(RegLocation loc)
buzbee67bf8852011-08-17 17:51:35 -0700860{
Bill Buzbeea114add2012-05-03 15:00:40 -0700861 DCHECK(loc.wide);
buzbee1fd33462013-03-25 13:40:45 -0700862 DCHECK(CheckCorePoolSanity());
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 if (loc.location != kLocPhysReg) {
864 DCHECK((loc.location == kLocDalvikFrame) ||
865 (loc.location == kLocCompilerTemp));
866 // Are the dalvik regs already live in physical registers?
buzbee1fd33462013-03-25 13:40:45 -0700867 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
868 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700869 bool match = true;
buzbeefa57c472012-11-21 12:06:18 -0800870 match = match && (info_lo != NULL);
871 match = match && (info_hi != NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700872 // Are they both core or both FP?
buzbee1fd33462013-03-25 13:40:45 -0700873 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700874 // If a pair of floating point singles, are they properly aligned?
buzbee1fd33462013-03-25 13:40:45 -0700875 if (match && IsFpReg(info_lo->reg)) {
buzbeefa57c472012-11-21 12:06:18 -0800876 match &= ((info_lo->reg & 0x1) == 0);
877 match &= ((info_hi->reg - info_lo->reg) == 1);
buzbee67bf8852011-08-17 17:51:35 -0700878 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700879 // If previously used as a pair, it is the same pair?
buzbeefa57c472012-11-21 12:06:18 -0800880 if (match && (info_lo->pair || info_hi->pair)) {
881 match = (info_lo->pair == info_hi->pair);
882 match &= ((info_lo->reg == info_hi->partner) &&
883 (info_hi->reg == info_lo->partner));
Bill Buzbeea114add2012-05-03 15:00:40 -0700884 }
885 if (match) {
886 // Can reuse - update the register usage info
buzbeefa57c472012-11-21 12:06:18 -0800887 loc.low_reg = info_lo->reg;
888 loc.high_reg = info_hi->reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700889 loc.location = kLocPhysReg;
buzbee1fd33462013-03-25 13:40:45 -0700890 MarkPair(loc.low_reg, loc.high_reg);
891 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700892 return loc;
893 }
894 // Can't easily reuse - clobber and free any overlaps
buzbeefa57c472012-11-21 12:06:18 -0800895 if (info_lo) {
buzbee1fd33462013-03-25 13:40:45 -0700896 Clobber(info_lo->reg);
897 FreeTemp(info_lo->reg);
buzbeefa57c472012-11-21 12:06:18 -0800898 if (info_lo->pair)
buzbee1fd33462013-03-25 13:40:45 -0700899 Clobber(info_lo->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 }
buzbeefa57c472012-11-21 12:06:18 -0800901 if (info_hi) {
buzbee1fd33462013-03-25 13:40:45 -0700902 Clobber(info_hi->reg);
903 FreeTemp(info_hi->reg);
buzbeefa57c472012-11-21 12:06:18 -0800904 if (info_hi->pair)
buzbee1fd33462013-03-25 13:40:45 -0700905 Clobber(info_hi->partner);
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 }
907 }
908 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700909}
910
buzbeeed3e9302011-09-23 17:34:19 -0700911
912/* For use in cases we don't know (or care) width */
buzbee1fd33462013-03-25 13:40:45 -0700913RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc)
buzbeeed3e9302011-09-23 17:34:19 -0700914{
Bill Buzbeea114add2012-05-03 15:00:40 -0700915 if (loc.wide)
buzbee1fd33462013-03-25 13:40:45 -0700916 return UpdateLocWide(loc);
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 else
buzbee1fd33462013-03-25 13:40:45 -0700918 return UpdateLoc(loc);
buzbeeed3e9302011-09-23 17:34:19 -0700919}
920
buzbee1fd33462013-03-25 13:40:45 -0700921RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700922{
Bill Buzbeea114add2012-05-03 15:00:40 -0700923 DCHECK(loc.wide);
buzbeefa57c472012-11-21 12:06:18 -0800924 int new_regs;
925 int low_reg;
926 int high_reg;
buzbee67bf8852011-08-17 17:51:35 -0700927
buzbee1fd33462013-03-25 13:40:45 -0700928 loc = UpdateLocWide(loc);
buzbee67bf8852011-08-17 17:51:35 -0700929
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 /* If already in registers, we can assume proper form. Right reg class? */
931 if (loc.location == kLocPhysReg) {
buzbee1fd33462013-03-25 13:40:45 -0700932 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
933 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
934 if (!RegClassMatches(reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700935 /* Wrong register class. Reallocate and copy */
buzbee1fd33462013-03-25 13:40:45 -0700936 new_regs = AllocTypedTempPair(loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800937 low_reg = new_regs & 0xff;
938 high_reg = (new_regs >> 8) & 0xff;
buzbee1fd33462013-03-25 13:40:45 -0700939 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
940 CopyRegInfo(low_reg, loc.low_reg);
941 CopyRegInfo(high_reg, loc.high_reg);
942 Clobber(loc.low_reg);
943 Clobber(loc.high_reg);
buzbeefa57c472012-11-21 12:06:18 -0800944 loc.low_reg = low_reg;
945 loc.high_reg = high_reg;
buzbee1fd33462013-03-25 13:40:45 -0700946 MarkPair(loc.low_reg, loc.high_reg);
947 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 }
buzbee67bf8852011-08-17 17:51:35 -0700949 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 }
951
buzbeefa57c472012-11-21 12:06:18 -0800952 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
953 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700954
buzbee1fd33462013-03-25 13:40:45 -0700955 new_regs = AllocTypedTempPair(loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800956 loc.low_reg = new_regs & 0xff;
957 loc.high_reg = (new_regs >> 8) & 0xff;
Bill Buzbeea114add2012-05-03 15:00:40 -0700958
buzbee1fd33462013-03-25 13:40:45 -0700959 MarkPair(loc.low_reg, loc.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 if (update) {
961 loc.location = kLocPhysReg;
buzbee1fd33462013-03-25 13:40:45 -0700962 MarkLive(loc.low_reg, loc.s_reg_low);
963 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 }
buzbee1fd33462013-03-25 13:40:45 -0700965 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 return loc;
buzbee67bf8852011-08-17 17:51:35 -0700967}
968
buzbee1fd33462013-03-25 13:40:45 -0700969RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update)
buzbee67bf8852011-08-17 17:51:35 -0700970{
buzbeefa57c472012-11-21 12:06:18 -0800971 int new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700972
Bill Buzbeea114add2012-05-03 15:00:40 -0700973 if (loc.wide)
buzbee1fd33462013-03-25 13:40:45 -0700974 return EvalLocWide(loc, reg_class, update);
buzbee67bf8852011-08-17 17:51:35 -0700975
buzbee1fd33462013-03-25 13:40:45 -0700976 loc = UpdateLoc(loc);
buzbee67bf8852011-08-17 17:51:35 -0700977
Bill Buzbeea114add2012-05-03 15:00:40 -0700978 if (loc.location == kLocPhysReg) {
buzbee1fd33462013-03-25 13:40:45 -0700979 if (!RegClassMatches(reg_class, loc.low_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 /* Wrong register class. Realloc, copy and transfer ownership */
buzbee1fd33462013-03-25 13:40:45 -0700981 new_reg = AllocTypedTemp(loc.fp, reg_class);
982 OpRegCopy(new_reg, loc.low_reg);
983 CopyRegInfo(new_reg, loc.low_reg);
984 Clobber(loc.low_reg);
buzbeefa57c472012-11-21 12:06:18 -0800985 loc.low_reg = new_reg;
buzbee67bf8852011-08-17 17:51:35 -0700986 }
987 return loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 }
989
buzbeefa57c472012-11-21 12:06:18 -0800990 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700991
buzbee1fd33462013-03-25 13:40:45 -0700992 new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbeefa57c472012-11-21 12:06:18 -0800993 loc.low_reg = new_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -0700994
995 if (update) {
996 loc.location = kLocPhysReg;
buzbee1fd33462013-03-25 13:40:45 -0700997 MarkLive(loc.low_reg, loc.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 }
999 return loc;
buzbee67bf8852011-08-17 17:51:35 -07001000}
1001
buzbeefa57c472012-11-21 12:06:18 -08001002/* USE SSA names to count references of base Dalvik v_regs. */
buzbee1fd33462013-03-25 13:40:45 -07001003void Mir2Lir::CountRefs(BasicBlock* bb, RefCounts* core_counts,
1004 RefCounts* fp_counts)
buzbeee3acd072012-02-25 17:03:10 -08001005{
buzbeec7d1f912013-02-07 15:22:39 -08001006 // TUNING: this routine could use some tweaking.
buzbee1fd33462013-03-25 13:40:45 -07001007 if ((cu_->disable_opt & (1 << kPromoteRegs)) ||
buzbeefa57c472012-11-21 12:06:18 -08001008 !((bb->block_type == kEntryBlock) || (bb->block_type == kExitBlock) ||
1009 (bb->block_type == kDalvikByteCode))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 return;
1011 }
buzbee1fd33462013-03-25 13:40:45 -07001012 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1013 RegLocation loc = mir_graph_->reg_location_[i];
buzbeefa57c472012-11-21 12:06:18 -08001014 RefCounts* counts = loc.fp ? fp_counts : core_counts;
buzbee1fd33462013-03-25 13:40:45 -07001015 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbee4ef3e452012-12-14 13:35:28 -08001016 //Don't count easily regenerated immediates
buzbee1fd33462013-03-25 13:40:45 -07001017 if (loc.fp || !IsInexpensiveConstant(loc)) {
1018 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
buzbee239c4e72012-03-16 08:42:29 -07001019 }
buzbeec7d1f912013-02-07 15:22:39 -08001020 if (loc.wide && loc.fp && !loc.high_word) {
1021 counts[p_map_idx].double_start = true;
buzbeee3acd072012-02-25 17:03:10 -08001022 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001023 }
buzbeee3acd072012-02-25 17:03:10 -08001024}
1025
1026/* qsort callback function, sort descending */
buzbeeaad94382012-11-21 07:40:50 -08001027static int SortCounts(const void *val1, const void *val2)
buzbeee3acd072012-02-25 17:03:10 -08001028{
buzbee1fd33462013-03-25 13:40:45 -07001029 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1030 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001031 return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
buzbeee3acd072012-02-25 17:03:10 -08001032}
1033
buzbee1fd33462013-03-25 13:40:45 -07001034void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg)
buzbeee3acd072012-02-25 17:03:10 -08001035{
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 LOG(INFO) << msg;
1037 for (int i = 0; i < size; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001038 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 }
buzbeee3acd072012-02-25 17:03:10 -08001040}
1041
1042/*
1043 * Note: some portions of this code required even if the kPromoteRegs
1044 * optimization is disabled.
1045 */
buzbee1fd33462013-03-25 13:40:45 -07001046void Mir2Lir::DoPromotion()
buzbeee3acd072012-02-25 17:03:10 -08001047{
buzbee1fd33462013-03-25 13:40:45 -07001048 int reg_bias = cu_->num_compiler_temps + 1;
1049 int dalvik_regs = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -08001050 int num_regs = dalvik_regs + reg_bias;
1051 const int promotion_threshold = 2;
buzbeee3acd072012-02-25 17:03:10 -08001052
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 // Allow target code to add any special registers
buzbee1fd33462013-03-25 13:40:45 -07001054 AdjustSpillMask();
buzbeee3acd072012-02-25 17:03:10 -08001055
Bill Buzbeea114add2012-05-03 15:00:40 -07001056 /*
1057 * Simple register promotion. Just do a static count of the uses
1058 * of Dalvik registers. Note that we examine the SSA names, but
1059 * count based on original Dalvik register name. Count refs
1060 * separately based on type in order to give allocation
1061 * preference to fp doubles - which must be allocated sequential
1062 * physical single fp registers started with an even-numbered
1063 * reg.
1064 * TUNING: replace with linear scan once we have the ability
1065 * to describe register live ranges for GC.
1066 */
buzbee1fd33462013-03-25 13:40:45 -07001067 RefCounts *core_regs = static_cast<RefCounts*>(NewMem(cu_, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001068 true, kAllocRegAlloc));
buzbee1fd33462013-03-25 13:40:45 -07001069 RefCounts *FpRegs = static_cast<RefCounts *>(NewMem(cu_, sizeof(RefCounts) * num_regs,
buzbeecbd6d442012-11-17 14:11:25 -08001070 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -07001071 // Set ssa names for original Dalvik registers
buzbeefa57c472012-11-21 12:06:18 -08001072 for (int i = 0; i < dalvik_regs; i++) {
1073 core_regs[i].s_reg = FpRegs[i].s_reg = i;
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 }
1075 // Set ssa name for Method*
buzbee1fd33462013-03-25 13:40:45 -07001076 core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg();
1077 FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy
buzbeefa57c472012-11-21 12:06:18 -08001078 // Set ssa names for compiler_temps
buzbee1fd33462013-03-25 13:40:45 -07001079 for (int i = 1; i <= cu_->num_compiler_temps; i++) {
1080 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(mir_graph_->compiler_temps_.elem_list[i]);
buzbeefa57c472012-11-21 12:06:18 -08001081 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
1082 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001083 }
1084
buzbee1fd33462013-03-25 13:40:45 -07001085 GrowableListIterator iterator = mir_graph_->GetBasicBlockIterator();
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 while (true) {
1087 BasicBlock* bb;
buzbee52a77fc2012-11-20 19:50:46 -08001088 bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -07001089 if (bb == NULL) break;
buzbee1fd33462013-03-25 13:40:45 -07001090 CountRefs(bb, core_regs, FpRegs);
Bill Buzbeea114add2012-05-03 15:00:40 -07001091 }
1092
1093 /*
1094 * Ideally, we'd allocate doubles starting with an even-numbered
1095 * register. Bias the counts to try to allocate any vreg that's
1096 * used as the start of a pair first.
1097 */
buzbeefa57c472012-11-21 12:06:18 -08001098 for (int i = 0; i < num_regs; i++) {
1099 if (FpRegs[i].double_start) {
buzbee52a77fc2012-11-20 19:50:46 -08001100 FpRegs[i].count *= 2;
buzbeee3acd072012-02-25 17:03:10 -08001101 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001102 }
1103
1104 // Sort the count arrays
buzbeefa57c472012-11-21 12:06:18 -08001105 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
1106 qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts);
Bill Buzbeea114add2012-05-03 15:00:40 -07001107
buzbee1fd33462013-03-25 13:40:45 -07001108 if (cu_->verbose) {
buzbeefa57c472012-11-21 12:06:18 -08001109 DumpCounts(core_regs, num_regs, "Core regs after sort");
1110 DumpCounts(FpRegs, num_regs, "Fp regs after sort");
Bill Buzbeea114add2012-05-03 15:00:40 -07001111 }
1112
buzbee1fd33462013-03-25 13:40:45 -07001113 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbee52a77fc2012-11-20 19:50:46 -08001114 // Promote FpRegs
buzbeefa57c472012-11-21 12:06:18 -08001115 for (int i = 0; (i < num_regs) &&
1116 (FpRegs[i].count >= promotion_threshold ); i++) {
buzbee1fd33462013-03-25 13:40:45 -07001117 int p_map_idx = SRegToPMap(FpRegs[i].s_reg);
1118 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1119 int reg = AllocPreservedFPReg(FpRegs[i].s_reg,
buzbeefa57c472012-11-21 12:06:18 -08001120 FpRegs[i].double_start);
Bill Buzbeea114add2012-05-03 15:00:40 -07001121 if (reg < 0) {
1122 break; // No more left
1123 }
1124 }
buzbee239c4e72012-03-16 08:42:29 -07001125 }
buzbee9c044ce2012-03-18 13:24:07 -07001126
Bill Buzbeea114add2012-05-03 15:00:40 -07001127 // Promote core regs
buzbeefa57c472012-11-21 12:06:18 -08001128 for (int i = 0; (i < num_regs) &&
1129 (core_regs[i].count > promotion_threshold); i++) {
buzbee1fd33462013-03-25 13:40:45 -07001130 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1131 if (promotion_map_[p_map_idx].core_location !=
Bill Buzbeea114add2012-05-03 15:00:40 -07001132 kLocPhysReg) {
buzbee1fd33462013-03-25 13:40:45 -07001133 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001134 if (reg < 0) {
1135 break; // No more left
1136 }
1137 }
buzbeee3acd072012-02-25 17:03:10 -08001138 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 }
buzbeee3acd072012-02-25 17:03:10 -08001140
Bill Buzbeea114add2012-05-03 15:00:40 -07001141 // Now, update SSA names to new home locations
buzbee1fd33462013-03-25 13:40:45 -07001142 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1143 RegLocation *curr = &mir_graph_->reg_location_[i];
1144 int p_map_idx = SRegToPMap(curr->s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -07001145 if (!curr->wide) {
1146 if (curr->fp) {
buzbee1fd33462013-03-25 13:40:45 -07001147 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001148 curr->location = kLocPhysReg;
buzbee1fd33462013-03-25 13:40:45 -07001149 curr->low_reg = promotion_map_[p_map_idx].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001150 curr->home = true;
1151 }
1152 } else {
buzbee1fd33462013-03-25 13:40:45 -07001153 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001154 curr->location = kLocPhysReg;
buzbee1fd33462013-03-25 13:40:45 -07001155 curr->low_reg = promotion_map_[p_map_idx].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001156 curr->home = true;
1157 }
1158 }
buzbeefa57c472012-11-21 12:06:18 -08001159 curr->high_reg = INVALID_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -07001160 } else {
buzbeefa57c472012-11-21 12:06:18 -08001161 if (curr->high_word) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001162 continue;
1163 }
1164 if (curr->fp) {
buzbee1fd33462013-03-25 13:40:45 -07001165 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1166 (promotion_map_[p_map_idx+1].fp_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001167 kLocPhysReg)) {
buzbee1fd33462013-03-25 13:40:45 -07001168 int low_reg = promotion_map_[p_map_idx].FpReg;
1169 int high_reg = promotion_map_[p_map_idx+1].FpReg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001170 // Doubles require pair of singles starting at even reg
buzbeefa57c472012-11-21 12:06:18 -08001171 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001172 curr->location = kLocPhysReg;
buzbeefa57c472012-11-21 12:06:18 -08001173 curr->low_reg = low_reg;
1174 curr->high_reg = high_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001175 curr->home = true;
1176 }
1177 }
1178 } else {
buzbee1fd33462013-03-25 13:40:45 -07001179 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1180 && (promotion_map_[p_map_idx+1].core_location ==
Bill Buzbeea114add2012-05-03 15:00:40 -07001181 kLocPhysReg)) {
1182 curr->location = kLocPhysReg;
buzbee1fd33462013-03-25 13:40:45 -07001183 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1184 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001185 curr->home = true;
1186 }
1187 }
buzbeee3acd072012-02-25 17:03:10 -08001188 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001189 }
buzbee1fd33462013-03-25 13:40:45 -07001190 if (cu_->verbose) {
1191 DumpPromotionMap();
buzbeeca7a5e42012-08-20 11:12:18 -07001192 }
buzbeee3acd072012-02-25 17:03:10 -08001193}
1194
1195/* Returns sp-relative offset in bytes for a VReg */
buzbee1fd33462013-03-25 13:40:45 -07001196int Mir2Lir::VRegOffset(int v_reg)
buzbeee3acd072012-02-25 17:03:10 -08001197{
buzbee1fd33462013-03-25 13:40:45 -07001198 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1199 fp_spill_mask_, frame_size_, v_reg);
buzbeee3acd072012-02-25 17:03:10 -08001200}
1201
1202/* Returns sp-relative offset in bytes for a SReg */
buzbee1fd33462013-03-25 13:40:45 -07001203int Mir2Lir::SRegOffset(int s_reg)
buzbeee3acd072012-02-25 17:03:10 -08001204{
buzbee1fd33462013-03-25 13:40:45 -07001205 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
buzbee02031b12012-11-23 09:41:35 -08001206}
1207
1208/* Mark register usage state and return long retloc */
buzbee1fd33462013-03-25 13:40:45 -07001209RegLocation Mir2Lir::GetReturnWide(bool is_double)
buzbee02031b12012-11-23 09:41:35 -08001210{
buzbee1fd33462013-03-25 13:40:45 -07001211 RegLocation gpr_res = LocCReturnWide();
1212 RegLocation fpr_res = LocCReturnDouble();
buzbee02031b12012-11-23 09:41:35 -08001213 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee1fd33462013-03-25 13:40:45 -07001214 Clobber(res.low_reg);
1215 Clobber(res.high_reg);
1216 LockTemp(res.low_reg);
1217 LockTemp(res.high_reg);
1218 MarkPair(res.low_reg, res.high_reg);
buzbee02031b12012-11-23 09:41:35 -08001219 return res;
1220}
1221
buzbee1fd33462013-03-25 13:40:45 -07001222RegLocation Mir2Lir::GetReturn(bool is_float)
buzbee02031b12012-11-23 09:41:35 -08001223{
buzbee1fd33462013-03-25 13:40:45 -07001224 RegLocation gpr_res = LocCReturn();
1225 RegLocation fpr_res = LocCReturnFloat();
buzbee02031b12012-11-23 09:41:35 -08001226 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee1fd33462013-03-25 13:40:45 -07001227 Clobber(res.low_reg);
1228 if (cu_->instruction_set == kMips) {
1229 MarkInUse(res.low_reg);
buzbee02031b12012-11-23 09:41:35 -08001230 } else {
buzbee1fd33462013-03-25 13:40:45 -07001231 LockTemp(res.low_reg);
buzbee02031b12012-11-23 09:41:35 -08001232 }
1233 return res;
1234}
1235
buzbee1fd33462013-03-25 13:40:45 -07001236void Mir2Lir::SimpleRegAlloc()
buzbee311ca162013-02-28 15:56:43 -08001237{
buzbee1fd33462013-03-25 13:40:45 -07001238 DoPromotion();
buzbee311ca162013-02-28 15:56:43 -08001239
buzbee1fd33462013-03-25 13:40:45 -07001240 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbee311ca162013-02-28 15:56:43 -08001241 LOG(INFO) << "After Promotion";
buzbee1fd33462013-03-25 13:40:45 -07001242 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
buzbee311ca162013-02-28 15:56:43 -08001243 }
1244
1245 /* Set the frame size */
buzbee1fd33462013-03-25 13:40:45 -07001246 frame_size_ = ComputeFrameSize();
1247}
1248
1249/*
1250 * Get the "real" sreg number associated with an s_reg slot. In general,
1251 * s_reg values passed through codegen are the SSA names created by
1252 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1253 * array. However, renaming is accomplished by simply replacing RegLocation
1254 * entries in the reglocation[] array. Therefore, when location
1255 * records for operands are first created, we need to ask the locRecord
1256 * identified by the dataflow pass what it's new name is.
1257 */
1258int Mir2Lir::GetSRegHi(int lowSreg) {
1259 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1260}
1261
1262bool Mir2Lir::oat_live_out(int s_reg) {
1263 //For now.
1264 return true;
1265}
1266
1267int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1268 DCHECK_GT(mir->ssa_rep->num_uses, num);
1269 return mir->ssa_rep->uses[num];
buzbee311ca162013-02-28 15:56:43 -08001270}
1271
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001272} // namespace art