blob: 41a57afca19a2a95fd9a7784a807368bafd8d68f [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains register alloction support. */
18
19#include "dex/compiler_ir.h"
20#include "dex/compiler_internals.h"
21#include "mir_to_lir-inl.h"
22
23namespace art {
24
25/*
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 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030void Mir2Lir::ResetRegPool() {
buzbeebd663de2013-09-10 15:41:31 -070031 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
32 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
33 info->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -070034 }
35 // Reset temp tracking sanity check.
36 if (kIsDebugBuild) {
37 live_sreg_ = INVALID_SREG;
38 }
39}
40
41 /*
42 * Set up temp & preserved register pools specialized by target.
43 * Note: num_regs may be zero.
44 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070045void Mir2Lir::CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070046 for (int i = 0; i < num; i++) {
buzbeebd663de2013-09-10 15:41:31 -070047 uint32_t reg_number = reg_nums[i];
48 regs[i].reg = reg_number;
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 regs[i].in_use = false;
50 regs[i].is_temp = false;
51 regs[i].pair = false;
52 regs[i].live = false;
53 regs[i].dirty = false;
54 regs[i].s_reg = INVALID_SREG;
buzbeebd663de2013-09-10 15:41:31 -070055 size_t map_size = reginfo_map_.Size();
56 if (reg_number >= map_size) {
57 for (uint32_t i = 0; i < ((reg_number - map_size) + 1); i++) {
58 reginfo_map_.Insert(NULL);
59 }
60 }
61 reginfo_map_.Put(reg_number, &regs[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 }
63}
64
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070065void Mir2Lir::DumpRegPool(RegisterInfo* p, int num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 LOG(INFO) << "================================================";
67 for (int i = 0; i < num_regs; i++) {
68 LOG(INFO) << StringPrintf(
buzbee0d829482013-10-11 15:24:55 -070069 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d",
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
buzbee0d829482013-10-11 15:24:55 -070071 p[i].live, p[i].dirty, p[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 }
73 LOG(INFO) << "================================================";
74}
75
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070076void Mir2Lir::DumpCoreRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 DumpRegPool(reg_pool_->core_regs, reg_pool_->num_core_regs);
78}
79
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070080void Mir2Lir::DumpFpRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 DumpRegPool(reg_pool_->FPRegs, reg_pool_->num_fp_regs);
82}
83
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070084void Mir2Lir::ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070085 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 if (p[i].s_reg == s_reg) {
87 if (p[i].is_temp) {
88 p[i].live = false;
89 }
90 p[i].def_start = NULL;
91 p[i].def_end = NULL;
92 }
93 }
94}
95
96/*
97 * Break the association between a Dalvik vreg and a physical temp register of either register
98 * class.
99 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
100 * in the register utilities, is is also used by code gen routines to work around a deficiency in
101 * local register allocation, which fails to distinguish between the "in" and "out" identities
102 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
103 * is used both as the source and destination register of an operation in which the type
104 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
105 * addressed.
106 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700107void Mir2Lir::ClobberSReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 /* Reset live temp tracking sanity checker */
109 if (kIsDebugBuild) {
110 if (s_reg == live_sreg_) {
111 live_sreg_ = INVALID_SREG;
112 }
113 }
114 ClobberSRegBody(reg_pool_->core_regs, reg_pool_->num_core_regs, s_reg);
115 ClobberSRegBody(reg_pool_->FPRegs, reg_pool_->num_fp_regs, s_reg);
116}
117
118/*
119 * SSA names associated with the initial definitions of Dalvik
120 * registers are the same as the Dalvik register number (and
121 * thus take the same position in the promotion_map. However,
122 * the special Method* and compiler temp resisters use negative
123 * v_reg numbers to distinguish them and can have an arbitrary
124 * ssa name (above the last original Dalvik register). This function
125 * maps SSA names to positions in the promotion_map array.
126 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700127int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
129 DCHECK_GE(s_reg, 0);
130 int v_reg = mir_graph_->SRegToVReg(s_reg);
131 if (v_reg >= 0) {
132 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
133 return v_reg;
134 } else {
135 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
136 DCHECK_LE(pos, cu_->num_compiler_temps);
137 return cu_->num_dalvik_registers + pos;
138 }
139}
140
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700141void Mir2Lir::RecordCorePromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 int p_map_idx = SRegToPMap(s_reg);
143 int v_reg = mir_graph_->SRegToVReg(s_reg);
144 GetRegInfo(reg)->in_use = true;
145 core_spill_mask_ |= (1 << reg);
146 // Include reg for later sort
147 core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
148 num_core_spills_++;
149 promotion_map_[p_map_idx].core_location = kLocPhysReg;
150 promotion_map_[p_map_idx].core_reg = reg;
151}
152
153/* Reserve a callee-save register. Return -1 if none available */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700154int Mir2Lir::AllocPreservedCoreReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 int res = -1;
156 RegisterInfo* core_regs = reg_pool_->core_regs;
157 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
158 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
159 res = core_regs[i].reg;
160 RecordCorePromotion(res, s_reg);
161 break;
162 }
163 }
164 return res;
165}
166
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700167void Mir2Lir::RecordFpPromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 int p_map_idx = SRegToPMap(s_reg);
169 int v_reg = mir_graph_->SRegToVReg(s_reg);
170 GetRegInfo(reg)->in_use = true;
171 MarkPreservedSingle(v_reg, reg);
172 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
173 promotion_map_[p_map_idx].FpReg = reg;
174}
175
buzbeec729a6b2013-09-14 16:04:31 -0700176// Reserve a callee-save fp single register.
177int Mir2Lir::AllocPreservedSingle(int s_reg) {
178 int res = -1; // Return code if none available.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegisterInfo* FPRegs = reg_pool_->FPRegs;
180 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700181 if (!FPRegs[i].is_temp && !FPRegs[i].in_use) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 res = FPRegs[i].reg;
183 RecordFpPromotion(res, s_reg);
184 break;
185 }
186 }
187 return res;
188}
189
190/*
191 * Somewhat messy code here. We want to allocate a pair of contiguous
192 * physical single-precision floating point registers starting with
193 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
194 * has already been allocated - try to fit if possible. Fail to
195 * allocate if we can't meet the requirements for the pair of
196 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
197 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700198int Mir2Lir::AllocPreservedDouble(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700199 int res = -1; // Assume failure
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 int v_reg = mir_graph_->SRegToVReg(s_reg);
201 int p_map_idx = SRegToPMap(s_reg);
202 if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) {
203 // Upper reg is already allocated. Can we fit?
204 int high_reg = promotion_map_[p_map_idx+1].FpReg;
205 if ((high_reg & 1) == 0) {
206 // High reg is even - fail.
207 return res;
208 }
209 // Is the low reg of the pair free?
210 RegisterInfo* p = GetRegInfo(high_reg-1);
211 if (p->in_use || p->is_temp) {
212 // Already allocated or not preserved - fail.
213 return res;
214 }
215 // OK - good to go.
216 res = p->reg;
217 p->in_use = true;
218 DCHECK_EQ((res & 1), 0);
219 MarkPreservedSingle(v_reg, res);
220 } else {
221 RegisterInfo* FPRegs = reg_pool_->FPRegs;
222 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
223 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
224 ((FPRegs[i].reg & 0x1) == 0x0) &&
225 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
226 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
227 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
228 res = FPRegs[i].reg;
229 FPRegs[i].in_use = true;
230 MarkPreservedSingle(v_reg, res);
231 FPRegs[i+1].in_use = true;
232 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
233 MarkPreservedSingle(v_reg+1, res+1);
234 break;
235 }
236 }
237 }
238 if (res != -1) {
239 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
240 promotion_map_[p_map_idx].FpReg = res;
241 promotion_map_[p_map_idx+1].fp_location = kLocPhysReg;
242 promotion_map_[p_map_idx+1].FpReg = res + 1;
243 }
244 return res;
245}
246
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700248 bool required) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700250 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 if (next >= num_regs)
252 next = 0;
253 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
254 Clobber(p[next].reg);
255 p[next].in_use = true;
256 p[next].pair = false;
257 *next_temp = next + 1;
258 return p[next].reg;
259 }
260 next++;
261 }
262 next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700263 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 if (next >= num_regs)
265 next = 0;
266 if (p[next].is_temp && !p[next].in_use) {
267 Clobber(p[next].reg);
268 p[next].in_use = true;
269 p[next].pair = false;
270 *next_temp = next + 1;
271 return p[next].reg;
272 }
273 next++;
274 }
275 if (required) {
276 CodegenDump();
277 DumpRegPool(reg_pool_->core_regs,
278 reg_pool_->num_core_regs);
279 LOG(FATAL) << "No free temp registers";
280 }
281 return -1; // No register available
282}
283
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700284// REDO: too many assumptions.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700285int Mir2Lir::AllocTempDouble() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 RegisterInfo* p = reg_pool_->FPRegs;
287 int num_regs = reg_pool_->num_fp_regs;
288 /* Start looking at an even reg */
289 int next = reg_pool_->next_fp_reg & ~0x1;
290
291 // First try to avoid allocating live registers
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700292 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 if (next >= num_regs)
294 next = 0;
295 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
296 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
297 Clobber(p[next].reg);
298 Clobber(p[next+1].reg);
299 p[next].in_use = true;
300 p[next+1].in_use = true;
301 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
302 DCHECK_EQ((p[next].reg & 0x1), 0);
303 reg_pool_->next_fp_reg = next + 2;
304 if (reg_pool_->next_fp_reg >= num_regs) {
305 reg_pool_->next_fp_reg = 0;
306 }
307 return p[next].reg;
308 }
309 next += 2;
310 }
311 next = reg_pool_->next_fp_reg & ~0x1;
312
313 // No choice - find a pair and kill it.
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700314 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 if (next >= num_regs)
316 next = 0;
317 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
318 !p[next+1].in_use) {
319 Clobber(p[next].reg);
320 Clobber(p[next+1].reg);
321 p[next].in_use = true;
322 p[next+1].in_use = true;
323 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
324 DCHECK_EQ((p[next].reg & 0x1), 0);
325 reg_pool_->next_fp_reg = next + 2;
326 if (reg_pool_->next_fp_reg >= num_regs) {
327 reg_pool_->next_fp_reg = 0;
328 }
329 return p[next].reg;
330 }
331 next += 2;
332 }
333 LOG(FATAL) << "No free temp registers (pair)";
334 return -1;
335}
336
337/* Return a temp if one is available, -1 otherwise */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700338int Mir2Lir::AllocFreeTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 return AllocTempBody(reg_pool_->core_regs,
340 reg_pool_->num_core_regs,
341 &reg_pool_->next_core_reg, true);
342}
343
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700344int Mir2Lir::AllocTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 return AllocTempBody(reg_pool_->core_regs,
346 reg_pool_->num_core_regs,
347 &reg_pool_->next_core_reg, true);
348}
349
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700350int Mir2Lir::AllocTempFloat() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 return AllocTempBody(reg_pool_->FPRegs,
352 reg_pool_->num_fp_regs,
353 &reg_pool_->next_fp_reg, true);
354}
355
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700356Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 if (s_reg == -1)
358 return NULL;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700359 for (int i = 0; i < num_regs; i++) {
buzbee56c71782013-09-05 17:13:19 -0700360 if ((p[i].s_reg == s_reg) && p[i].live) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 if (p[i].is_temp)
362 p[i].in_use = true;
363 return &p[i];
364 }
365 }
366 return NULL;
367}
368
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 RegisterInfo* res = NULL;
371 switch (reg_class) {
372 case kAnyReg:
373 res = AllocLiveBody(reg_pool_->FPRegs,
374 reg_pool_->num_fp_regs, s_reg);
375 if (res)
376 break;
377 /* Intentional fallthrough */
378 case kCoreReg:
379 res = AllocLiveBody(reg_pool_->core_regs,
380 reg_pool_->num_core_regs, s_reg);
381 break;
382 case kFPReg:
383 res = AllocLiveBody(reg_pool_->FPRegs,
384 reg_pool_->num_fp_regs, s_reg);
385 break;
386 default:
387 LOG(FATAL) << "Invalid register type";
388 }
389 return res;
390}
391
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700392void Mir2Lir::FreeTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700393 RegisterInfo* p = GetRegInfo(reg);
394 if (p->is_temp) {
395 p->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
buzbee56c71782013-09-05 17:13:19 -0700397 p->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398}
399
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700400Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700401 RegisterInfo* p = GetRegInfo(reg);
402 return p->live ? p : NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403}
404
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700405Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 RegisterInfo* p = GetRegInfo(reg);
407 return (p->is_temp) ? p : NULL;
408}
409
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700410Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 RegisterInfo* p = GetRegInfo(reg);
412 return (p->is_temp) ? NULL : p;
413}
414
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700415bool Mir2Lir::IsDirty(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 RegisterInfo* p = GetRegInfo(reg);
417 return p->dirty;
418}
419
420/*
421 * Similar to AllocTemp(), but forces the allocation of a specific
422 * register. No check is made to see if the register was previously
423 * allocated. Use with caution.
424 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700425void Mir2Lir::LockTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700426 RegisterInfo* p = GetRegInfo(reg);
427 DCHECK(p->is_temp);
428 p->in_use = true;
429 p->live = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430}
431
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700432void Mir2Lir::ResetDef(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 ResetDefBody(GetRegInfo(reg));
434}
435
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700436void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 if (start && finish) {
438 LIR *p;
439 DCHECK_EQ(s_reg1, s_reg2);
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700440 for (p = start; ; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 NopLIR(p);
442 if (p == finish)
443 break;
444 }
445 }
446}
447
448/*
449 * Mark the beginning and end LIR of a def sequence. Note that
450 * on entry start points to the LIR prior to the beginning of the
451 * sequence.
452 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700453void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 DCHECK(!rl.wide);
455 DCHECK(start && start->next);
456 DCHECK(finish);
457 RegisterInfo* p = GetRegInfo(rl.low_reg);
458 p->def_start = start->next;
459 p->def_end = finish;
460}
461
462/*
463 * Mark the beginning and end LIR of a def sequence. Note that
464 * on entry start points to the LIR prior to the beginning of the
465 * sequence.
466 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700467void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 DCHECK(rl.wide);
469 DCHECK(start && start->next);
470 DCHECK(finish);
471 RegisterInfo* p = GetRegInfo(rl.low_reg);
472 ResetDef(rl.high_reg); // Only track low of pair
473 p->def_start = start->next;
474 p->def_end = finish;
475}
476
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700477RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 DCHECK(rl.wide);
479 if (rl.location == kLocPhysReg) {
480 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
481 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
482 if (info_lo->is_temp) {
483 info_lo->pair = false;
484 info_lo->def_start = NULL;
485 info_lo->def_end = NULL;
486 }
487 if (info_hi->is_temp) {
488 info_hi->pair = false;
489 info_hi->def_start = NULL;
490 info_hi->def_end = NULL;
491 }
492 }
493 rl.wide = false;
494 return rl;
495}
496
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 DCHECK(!rl.wide);
499 RegisterInfo* p = IsTemp(rl.low_reg);
500 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
501 DCHECK(!p->pair);
502 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
503 }
504 ResetDef(rl.low_reg);
505}
506
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700507void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 DCHECK(rl.wide);
509 RegisterInfo* p_low = IsTemp(rl.low_reg);
510 RegisterInfo* p_high = IsTemp(rl.high_reg);
511 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
512 DCHECK(p_low->pair);
513 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
514 }
515 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
516 DCHECK(p_high->pair);
517 }
518 ResetDef(rl.low_reg);
519 ResetDef(rl.high_reg);
520}
521
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700522void Mir2Lir::ResetDefTracking() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700523 for (int i = 0; i< reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 ResetDefBody(&reg_pool_->core_regs[i]);
525 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700526 for (int i = 0; i< reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 ResetDefBody(&reg_pool_->FPRegs[i]);
528 }
529}
530
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700531void Mir2Lir::ClobberAllRegs() {
buzbeebd663de2013-09-10 15:41:31 -0700532 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
533 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
534 info->live = false;
535 info->s_reg = INVALID_SREG;
536 info->def_start = NULL;
537 info->def_end = NULL;
538 info->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 }
540}
541
542// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700543void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700544 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 if (info[i].live && info[i].dirty) {
546 if (info[i].pair) {
547 FlushRegWide(info[i].reg, info[i].partner);
548 } else {
549 FlushReg(info[i].reg);
550 }
551 }
552 }
553}
554
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700555void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 FlushAllRegsBody(reg_pool_->core_regs,
557 reg_pool_->num_core_regs);
558 FlushAllRegsBody(reg_pool_->FPRegs,
559 reg_pool_->num_fp_regs);
560 ClobberAllRegs();
561}
562
563
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700564// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700565bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 if (reg_class == kAnyReg) {
567 return true;
568 } else if (reg_class == kCoreReg) {
569 return !IsFpReg(reg);
570 } else {
571 return IsFpReg(reg);
572 }
573}
574
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700575void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 RegisterInfo* info = GetRegInfo(reg);
577 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
578 return; /* already live */
579 } else if (s_reg != INVALID_SREG) {
580 ClobberSReg(s_reg);
581 if (info->is_temp) {
582 info->live = true;
583 }
584 } else {
585 /* Can't be live if no associated s_reg */
586 DCHECK(info->is_temp);
587 info->live = false;
588 }
589 info->s_reg = s_reg;
590}
591
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700592void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700594 tempreg_info_.Insert(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 info->is_temp = true;
596}
597
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700598void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700600 tempreg_info_.Delete(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 info->is_temp = false;
602}
603
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700604void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 RegisterInfo* info_lo = GetRegInfo(low_reg);
606 RegisterInfo* info_hi = GetRegInfo(high_reg);
607 info_lo->pair = info_hi->pair = true;
608 info_lo->partner = high_reg;
609 info_hi->partner = low_reg;
610}
611
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700612void Mir2Lir::MarkClean(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 RegisterInfo* info = GetRegInfo(loc.low_reg);
614 info->dirty = false;
615 if (loc.wide) {
616 info = GetRegInfo(loc.high_reg);
617 info->dirty = false;
618 }
619}
620
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700621void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 if (loc.home) {
623 // If already home, can't be dirty
624 return;
625 }
626 RegisterInfo* info = GetRegInfo(loc.low_reg);
627 info->dirty = true;
628 if (loc.wide) {
629 info = GetRegInfo(loc.high_reg);
630 info->dirty = true;
631 }
632}
633
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700634void Mir2Lir::MarkInUse(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 RegisterInfo* info = GetRegInfo(reg);
636 info->in_use = true;
637}
638
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700639void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 RegisterInfo* new_info = GetRegInfo(new_reg);
641 RegisterInfo* old_info = GetRegInfo(old_reg);
642 // Target temp status must not change
643 bool is_temp = new_info->is_temp;
644 *new_info = *old_info;
645 // Restore target's temp status
646 new_info->is_temp = is_temp;
647 new_info->reg = new_reg;
648}
649
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700650bool Mir2Lir::CheckCorePoolSanity() {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700651 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
652 if (reg_pool_->core_regs[i].pair) {
653 static int my_reg = reg_pool_->core_regs[i].reg;
654 static int my_sreg = reg_pool_->core_regs[i].s_reg;
655 static int partner_reg = reg_pool_->core_regs[i].partner;
656 static RegisterInfo* partner = GetRegInfo(partner_reg);
657 DCHECK(partner != NULL);
658 DCHECK(partner->pair);
659 DCHECK_EQ(my_reg, partner->partner);
660 static int partner_sreg = partner->s_reg;
661 if (my_sreg == INVALID_SREG) {
662 DCHECK_EQ(partner_sreg, INVALID_SREG);
663 } else {
664 int diff = my_sreg - partner_sreg;
665 DCHECK((diff == -1) || (diff == 1));
666 }
667 }
668 if (!reg_pool_->core_regs[i].live) {
669 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
670 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
671 }
672 }
673 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674}
675
676/*
677 * Return an updated location record with current in-register status.
678 * If the value lives in live temps, reflect that fact. No code
679 * is generated. If the live value is part of an older pair,
680 * clobber both low and high.
681 * TUNING: clobbering both is a bit heavy-handed, but the alternative
682 * is a bit complex when dealing with FP regs. Examine code to see
683 * if it's worthwhile trying to be more clever here.
684 */
685
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700686RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700687 DCHECK(!loc.wide);
688 DCHECK(CheckCorePoolSanity());
689 if (loc.location != kLocPhysReg) {
690 DCHECK((loc.location == kLocDalvikFrame) ||
691 (loc.location == kLocCompilerTemp));
692 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
693 if (info_lo) {
694 if (info_lo->pair) {
695 Clobber(info_lo->reg);
696 Clobber(info_lo->partner);
697 FreeTemp(info_lo->reg);
698 } else {
699 loc.low_reg = info_lo->reg;
700 loc.location = kLocPhysReg;
701 }
702 }
703 }
704
705 return loc;
706}
707
708/* see comments for update_loc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700709RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 DCHECK(loc.wide);
711 DCHECK(CheckCorePoolSanity());
712 if (loc.location != kLocPhysReg) {
713 DCHECK((loc.location == kLocDalvikFrame) ||
714 (loc.location == kLocCompilerTemp));
715 // Are the dalvik regs already live in physical registers?
716 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
717 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
718 bool match = true;
719 match = match && (info_lo != NULL);
720 match = match && (info_hi != NULL);
721 // Are they both core or both FP?
722 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
723 // If a pair of floating point singles, are they properly aligned?
724 if (match && IsFpReg(info_lo->reg)) {
725 match &= ((info_lo->reg & 0x1) == 0);
726 match &= ((info_hi->reg - info_lo->reg) == 1);
727 }
728 // If previously used as a pair, it is the same pair?
729 if (match && (info_lo->pair || info_hi->pair)) {
730 match = (info_lo->pair == info_hi->pair);
731 match &= ((info_lo->reg == info_hi->partner) &&
732 (info_hi->reg == info_lo->partner));
733 }
734 if (match) {
735 // Can reuse - update the register usage info
736 loc.low_reg = info_lo->reg;
737 loc.high_reg = info_hi->reg;
738 loc.location = kLocPhysReg;
739 MarkPair(loc.low_reg, loc.high_reg);
740 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
741 return loc;
742 }
743 // Can't easily reuse - clobber and free any overlaps
744 if (info_lo) {
745 Clobber(info_lo->reg);
746 FreeTemp(info_lo->reg);
747 if (info_lo->pair)
748 Clobber(info_lo->partner);
749 }
750 if (info_hi) {
751 Clobber(info_hi->reg);
752 FreeTemp(info_hi->reg);
753 if (info_hi->pair)
754 Clobber(info_hi->partner);
755 }
756 }
757 return loc;
758}
759
760
761/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700762RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700763 if (loc.wide)
764 return UpdateLocWide(loc);
765 else
766 return UpdateLoc(loc);
767}
768
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700769RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 DCHECK(loc.wide);
buzbee0d829482013-10-11 15:24:55 -0700771 int32_t new_regs;
772 int32_t low_reg;
773 int32_t high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774
775 loc = UpdateLocWide(loc);
776
777 /* If already in registers, we can assume proper form. Right reg class? */
778 if (loc.location == kLocPhysReg) {
779 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
780 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
781 if (!RegClassMatches(reg_class, loc.low_reg)) {
782 /* Wrong register class. Reallocate and copy */
783 new_regs = AllocTypedTempPair(loc.fp, reg_class);
784 low_reg = new_regs & 0xff;
785 high_reg = (new_regs >> 8) & 0xff;
786 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
787 CopyRegInfo(low_reg, loc.low_reg);
788 CopyRegInfo(high_reg, loc.high_reg);
789 Clobber(loc.low_reg);
790 Clobber(loc.high_reg);
791 loc.low_reg = low_reg;
792 loc.high_reg = high_reg;
793 MarkPair(loc.low_reg, loc.high_reg);
794 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
795 }
796 return loc;
797 }
798
799 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
800 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
801
802 new_regs = AllocTypedTempPair(loc.fp, reg_class);
803 loc.low_reg = new_regs & 0xff;
804 loc.high_reg = (new_regs >> 8) & 0xff;
805
806 MarkPair(loc.low_reg, loc.high_reg);
807 if (update) {
808 loc.location = kLocPhysReg;
809 MarkLive(loc.low_reg, loc.s_reg_low);
810 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
811 }
812 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
813 return loc;
814}
815
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700816RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700817 int new_reg;
818
819 if (loc.wide)
820 return EvalLocWide(loc, reg_class, update);
821
822 loc = UpdateLoc(loc);
823
824 if (loc.location == kLocPhysReg) {
825 if (!RegClassMatches(reg_class, loc.low_reg)) {
826 /* Wrong register class. Realloc, copy and transfer ownership */
827 new_reg = AllocTypedTemp(loc.fp, reg_class);
828 OpRegCopy(new_reg, loc.low_reg);
829 CopyRegInfo(new_reg, loc.low_reg);
830 Clobber(loc.low_reg);
831 loc.low_reg = new_reg;
832 }
833 return loc;
834 }
835
836 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
837
838 new_reg = AllocTypedTemp(loc.fp, reg_class);
839 loc.low_reg = new_reg;
840
841 if (update) {
842 loc.location = kLocPhysReg;
843 MarkLive(loc.low_reg, loc.s_reg_low);
844 }
845 return loc;
846}
847
848/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -0700849void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
851 RegLocation loc = mir_graph_->reg_location_[i];
852 RefCounts* counts = loc.fp ? fp_counts : core_counts;
853 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -0700854 if (loc.fp) {
855 if (loc.wide) {
856 // Treat doubles as a unit, using upper half of fp_counts array.
857 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
858 i++;
859 } else {
860 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
861 }
862 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
864 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 }
866}
867
868/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700869static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700870 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
871 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700872 // Note that we fall back to sorting on reg so we get stable output
873 // on differing qsort implementations (such as on host and target or
874 // between local host and build servers).
875 return (op1->count == op2->count)
876 ? (op1->s_reg - op2->s_reg)
877 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878}
879
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700880void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 LOG(INFO) << msg;
882 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700883 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
884 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
885 } else {
886 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
887 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888 }
889}
890
891/*
892 * Note: some portions of this code required even if the kPromoteRegs
893 * optimization is disabled.
894 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700895void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 int reg_bias = cu_->num_compiler_temps + 1;
897 int dalvik_regs = cu_->num_dalvik_registers;
898 int num_regs = dalvik_regs + reg_bias;
899 const int promotion_threshold = 1;
900
901 // Allow target code to add any special registers
902 AdjustSpillMask();
903
904 /*
905 * Simple register promotion. Just do a static count of the uses
906 * of Dalvik registers. Note that we examine the SSA names, but
907 * count based on original Dalvik register name. Count refs
908 * separately based on type in order to give allocation
909 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -0700910 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 * reg.
912 * TUNING: replace with linear scan once we have the ability
913 * to describe register live ranges for GC.
914 */
915 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700916 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
917 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -0700919 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700920 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 // Set ssa names for original Dalvik registers
922 for (int i = 0; i < dalvik_regs; i++) {
923 core_regs[i].s_reg = FpRegs[i].s_reg = i;
924 }
925 // Set ssa name for Method*
926 core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg();
buzbeec729a6b2013-09-14 16:04:31 -0700927 FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy.
928 FpRegs[dalvik_regs + num_regs].s_reg = mir_graph_->GetMethodSReg(); // for consistency.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700929 // Set ssa names for compiler_temps
930 for (int i = 1; i <= cu_->num_compiler_temps; i++) {
931 CompilerTemp* ct = mir_graph_->compiler_temps_.Get(i);
932 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
933 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
buzbeec729a6b2013-09-14 16:04:31 -0700934 FpRegs[num_regs + dalvik_regs + i].s_reg = ct->s_reg;
935 }
936
937 // Duplicate in upper half to represent possible fp double starting sregs.
938 for (int i = 0; i < num_regs; i++) {
939 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 }
941
942 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -0700943 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944
Brian Carlstrom7940e442013-07-12 13:46:57 -0700945
946 // Sort the count arrays
947 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -0700948 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949
950 if (cu_->verbose) {
951 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -0700952 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 }
954
955 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
956 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -0700957 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
958 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
959 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
960 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
961 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
962 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
963 // Ignore result - if can't alloc double may still be able to alloc singles.
964 AllocPreservedDouble(low_sreg);
965 }
966 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
967 int reg = AllocPreservedSingle(FpRegs[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 if (reg < 0) {
buzbeec729a6b2013-09-14 16:04:31 -0700969 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 }
971 }
972 }
973
974 // Promote core regs
975 for (int i = 0; (i < num_regs) &&
976 (core_regs[i].count >= promotion_threshold); i++) {
977 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
978 if (promotion_map_[p_map_idx].core_location !=
979 kLocPhysReg) {
980 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
981 if (reg < 0) {
982 break; // No more left
983 }
984 }
985 }
986 }
987
988 // Now, update SSA names to new home locations
989 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
990 RegLocation *curr = &mir_graph_->reg_location_[i];
991 int p_map_idx = SRegToPMap(curr->s_reg_low);
992 if (!curr->wide) {
993 if (curr->fp) {
994 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
995 curr->location = kLocPhysReg;
996 curr->low_reg = promotion_map_[p_map_idx].FpReg;
997 curr->home = true;
998 }
999 } else {
1000 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1001 curr->location = kLocPhysReg;
1002 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1003 curr->home = true;
1004 }
1005 }
1006 curr->high_reg = INVALID_REG;
1007 } else {
1008 if (curr->high_word) {
1009 continue;
1010 }
1011 if (curr->fp) {
1012 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1013 (promotion_map_[p_map_idx+1].fp_location ==
1014 kLocPhysReg)) {
1015 int low_reg = promotion_map_[p_map_idx].FpReg;
1016 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1017 // Doubles require pair of singles starting at even reg
1018 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1019 curr->location = kLocPhysReg;
1020 curr->low_reg = low_reg;
1021 curr->high_reg = high_reg;
1022 curr->home = true;
1023 }
1024 }
1025 } else {
1026 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1027 && (promotion_map_[p_map_idx+1].core_location ==
1028 kLocPhysReg)) {
1029 curr->location = kLocPhysReg;
1030 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1031 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1032 curr->home = true;
1033 }
1034 }
1035 }
1036 }
1037 if (cu_->verbose) {
1038 DumpPromotionMap();
1039 }
1040}
1041
1042/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001043int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1045 fp_spill_mask_, frame_size_, v_reg);
1046}
1047
1048/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001049int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1051}
1052
1053/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001054RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 RegLocation gpr_res = LocCReturnWide();
1056 RegLocation fpr_res = LocCReturnDouble();
1057 RegLocation res = is_double ? fpr_res : gpr_res;
1058 Clobber(res.low_reg);
1059 Clobber(res.high_reg);
1060 LockTemp(res.low_reg);
1061 LockTemp(res.high_reg);
1062 MarkPair(res.low_reg, res.high_reg);
1063 return res;
1064}
1065
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001066RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 RegLocation gpr_res = LocCReturn();
1068 RegLocation fpr_res = LocCReturnFloat();
1069 RegLocation res = is_float ? fpr_res : gpr_res;
1070 Clobber(res.low_reg);
1071 if (cu_->instruction_set == kMips) {
1072 MarkInUse(res.low_reg);
1073 } else {
1074 LockTemp(res.low_reg);
1075 }
1076 return res;
1077}
1078
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001079void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 DoPromotion();
1081
1082 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1083 LOG(INFO) << "After Promotion";
1084 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1085 }
1086
1087 /* Set the frame size */
1088 frame_size_ = ComputeFrameSize();
1089}
1090
1091/*
1092 * Get the "real" sreg number associated with an s_reg slot. In general,
1093 * s_reg values passed through codegen are the SSA names created by
1094 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1095 * array. However, renaming is accomplished by simply replacing RegLocation
1096 * entries in the reglocation[] array. Therefore, when location
1097 * records for operands are first created, we need to ask the locRecord
1098 * identified by the dataflow pass what it's new name is.
1099 */
1100int Mir2Lir::GetSRegHi(int lowSreg) {
1101 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1102}
1103
1104bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001105 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106 return true;
1107}
1108
1109int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1110 DCHECK_GT(mir->ssa_rep->num_uses, num);
1111 return mir->ssa_rep->uses[num];
1112}
1113
1114} // namespace art