blob: a0f22fc576a34fa662c24abc41c1eb2c6d02c347 [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(
69 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x",
70 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
71 p[i].live, p[i].dirty, p[i].s_reg, reinterpret_cast<uintptr_t>(p[i].def_start),
72 reinterpret_cast<uintptr_t>(p[i].def_end));
73 }
74 LOG(INFO) << "================================================";
75}
76
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070077void Mir2Lir::DumpCoreRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 DumpRegPool(reg_pool_->core_regs, reg_pool_->num_core_regs);
79}
80
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070081void Mir2Lir::DumpFpRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 DumpRegPool(reg_pool_->FPRegs, reg_pool_->num_fp_regs);
83}
84
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070085void Mir2Lir::ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070086 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 if (p[i].s_reg == s_reg) {
88 if (p[i].is_temp) {
89 p[i].live = false;
90 }
91 p[i].def_start = NULL;
92 p[i].def_end = NULL;
93 }
94 }
95}
96
97/*
98 * Break the association between a Dalvik vreg and a physical temp register of either register
99 * class.
100 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
101 * in the register utilities, is is also used by code gen routines to work around a deficiency in
102 * local register allocation, which fails to distinguish between the "in" and "out" identities
103 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
104 * is used both as the source and destination register of an operation in which the type
105 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
106 * addressed.
107 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700108void Mir2Lir::ClobberSReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 /* Reset live temp tracking sanity checker */
110 if (kIsDebugBuild) {
111 if (s_reg == live_sreg_) {
112 live_sreg_ = INVALID_SREG;
113 }
114 }
115 ClobberSRegBody(reg_pool_->core_regs, reg_pool_->num_core_regs, s_reg);
116 ClobberSRegBody(reg_pool_->FPRegs, reg_pool_->num_fp_regs, s_reg);
117}
118
119/*
120 * SSA names associated with the initial definitions of Dalvik
121 * registers are the same as the Dalvik register number (and
122 * thus take the same position in the promotion_map. However,
123 * the special Method* and compiler temp resisters use negative
124 * v_reg numbers to distinguish them and can have an arbitrary
125 * ssa name (above the last original Dalvik register). This function
126 * maps SSA names to positions in the promotion_map array.
127 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700128int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
130 DCHECK_GE(s_reg, 0);
131 int v_reg = mir_graph_->SRegToVReg(s_reg);
132 if (v_reg >= 0) {
133 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
134 return v_reg;
135 } else {
136 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
137 DCHECK_LE(pos, cu_->num_compiler_temps);
138 return cu_->num_dalvik_registers + pos;
139 }
140}
141
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700142void Mir2Lir::RecordCorePromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 int p_map_idx = SRegToPMap(s_reg);
144 int v_reg = mir_graph_->SRegToVReg(s_reg);
145 GetRegInfo(reg)->in_use = true;
146 core_spill_mask_ |= (1 << reg);
147 // Include reg for later sort
148 core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
149 num_core_spills_++;
150 promotion_map_[p_map_idx].core_location = kLocPhysReg;
151 promotion_map_[p_map_idx].core_reg = reg;
152}
153
154/* Reserve a callee-save register. Return -1 if none available */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700155int Mir2Lir::AllocPreservedCoreReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 int res = -1;
157 RegisterInfo* core_regs = reg_pool_->core_regs;
158 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
159 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
160 res = core_regs[i].reg;
161 RecordCorePromotion(res, s_reg);
162 break;
163 }
164 }
165 return res;
166}
167
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700168void Mir2Lir::RecordFpPromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 int p_map_idx = SRegToPMap(s_reg);
170 int v_reg = mir_graph_->SRegToVReg(s_reg);
171 GetRegInfo(reg)->in_use = true;
172 MarkPreservedSingle(v_reg, reg);
173 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
174 promotion_map_[p_map_idx].FpReg = reg;
175}
176
177/*
178 * Reserve a callee-save fp single register. Try to fullfill request for
179 * even/odd allocation, but go ahead and allocate anything if not
180 * available. If nothing's available, return -1.
181 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182int Mir2Lir::AllocPreservedSingle(int s_reg, bool even) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 int res = -1;
184 RegisterInfo* FPRegs = reg_pool_->FPRegs;
185 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
186 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
187 ((FPRegs[i].reg & 0x1) == 0) == even) {
188 res = FPRegs[i].reg;
189 RecordFpPromotion(res, s_reg);
190 break;
191 }
192 }
193 return res;
194}
195
196/*
197 * Somewhat messy code here. We want to allocate a pair of contiguous
198 * physical single-precision floating point registers starting with
199 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
200 * has already been allocated - try to fit if possible. Fail to
201 * allocate if we can't meet the requirements for the pair of
202 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
203 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700204int Mir2Lir::AllocPreservedDouble(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700205 int res = -1; // Assume failure
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 int v_reg = mir_graph_->SRegToVReg(s_reg);
207 int p_map_idx = SRegToPMap(s_reg);
208 if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) {
209 // Upper reg is already allocated. Can we fit?
210 int high_reg = promotion_map_[p_map_idx+1].FpReg;
211 if ((high_reg & 1) == 0) {
212 // High reg is even - fail.
213 return res;
214 }
215 // Is the low reg of the pair free?
216 RegisterInfo* p = GetRegInfo(high_reg-1);
217 if (p->in_use || p->is_temp) {
218 // Already allocated or not preserved - fail.
219 return res;
220 }
221 // OK - good to go.
222 res = p->reg;
223 p->in_use = true;
224 DCHECK_EQ((res & 1), 0);
225 MarkPreservedSingle(v_reg, res);
226 } else {
227 RegisterInfo* FPRegs = reg_pool_->FPRegs;
228 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
229 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
230 ((FPRegs[i].reg & 0x1) == 0x0) &&
231 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
232 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
233 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
234 res = FPRegs[i].reg;
235 FPRegs[i].in_use = true;
236 MarkPreservedSingle(v_reg, res);
237 FPRegs[i+1].in_use = true;
238 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
239 MarkPreservedSingle(v_reg+1, res+1);
240 break;
241 }
242 }
243 }
244 if (res != -1) {
245 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
246 promotion_map_[p_map_idx].FpReg = res;
247 promotion_map_[p_map_idx+1].fp_location = kLocPhysReg;
248 promotion_map_[p_map_idx+1].FpReg = res + 1;
249 }
250 return res;
251}
252
253
254/*
255 * Reserve a callee-save fp register. If this register can be used
256 * as the first of a double, attempt to allocate an even pair of fp
257 * single regs (but if can't still attempt to allocate a single, preferring
258 * first to allocate an odd register.
259 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700260int Mir2Lir::AllocPreservedFPReg(int s_reg, bool double_start) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 int res = -1;
262 if (double_start) {
263 res = AllocPreservedDouble(s_reg);
264 }
265 if (res == -1) {
266 res = AllocPreservedSingle(s_reg, false /* try odd # */);
267 }
268 if (res == -1)
269 res = AllocPreservedSingle(s_reg, true /* try even # */);
270 return res;
271}
272
273int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700274 bool required) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700276 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 if (next >= num_regs)
278 next = 0;
279 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
280 Clobber(p[next].reg);
281 p[next].in_use = true;
282 p[next].pair = false;
283 *next_temp = next + 1;
284 return p[next].reg;
285 }
286 next++;
287 }
288 next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700289 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 if (next >= num_regs)
291 next = 0;
292 if (p[next].is_temp && !p[next].in_use) {
293 Clobber(p[next].reg);
294 p[next].in_use = true;
295 p[next].pair = false;
296 *next_temp = next + 1;
297 return p[next].reg;
298 }
299 next++;
300 }
301 if (required) {
302 CodegenDump();
303 DumpRegPool(reg_pool_->core_regs,
304 reg_pool_->num_core_regs);
305 LOG(FATAL) << "No free temp registers";
306 }
307 return -1; // No register available
308}
309
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700310// REDO: too many assumptions.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700311int Mir2Lir::AllocTempDouble() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 RegisterInfo* p = reg_pool_->FPRegs;
313 int num_regs = reg_pool_->num_fp_regs;
314 /* Start looking at an even reg */
315 int next = reg_pool_->next_fp_reg & ~0x1;
316
317 // First try to avoid allocating live registers
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700318 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 if (next >= num_regs)
320 next = 0;
321 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
322 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
323 Clobber(p[next].reg);
324 Clobber(p[next+1].reg);
325 p[next].in_use = true;
326 p[next+1].in_use = true;
327 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
328 DCHECK_EQ((p[next].reg & 0x1), 0);
329 reg_pool_->next_fp_reg = next + 2;
330 if (reg_pool_->next_fp_reg >= num_regs) {
331 reg_pool_->next_fp_reg = 0;
332 }
333 return p[next].reg;
334 }
335 next += 2;
336 }
337 next = reg_pool_->next_fp_reg & ~0x1;
338
339 // No choice - find a pair and kill it.
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700340 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 if (next >= num_regs)
342 next = 0;
343 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
344 !p[next+1].in_use) {
345 Clobber(p[next].reg);
346 Clobber(p[next+1].reg);
347 p[next].in_use = true;
348 p[next+1].in_use = true;
349 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
350 DCHECK_EQ((p[next].reg & 0x1), 0);
351 reg_pool_->next_fp_reg = next + 2;
352 if (reg_pool_->next_fp_reg >= num_regs) {
353 reg_pool_->next_fp_reg = 0;
354 }
355 return p[next].reg;
356 }
357 next += 2;
358 }
359 LOG(FATAL) << "No free temp registers (pair)";
360 return -1;
361}
362
363/* Return a temp if one is available, -1 otherwise */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700364int Mir2Lir::AllocFreeTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 return AllocTempBody(reg_pool_->core_regs,
366 reg_pool_->num_core_regs,
367 &reg_pool_->next_core_reg, true);
368}
369
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700370int Mir2Lir::AllocTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 return AllocTempBody(reg_pool_->core_regs,
372 reg_pool_->num_core_regs,
373 &reg_pool_->next_core_reg, true);
374}
375
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700376int Mir2Lir::AllocTempFloat() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 return AllocTempBody(reg_pool_->FPRegs,
378 reg_pool_->num_fp_regs,
379 &reg_pool_->next_fp_reg, true);
380}
381
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700382Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 if (s_reg == -1)
384 return NULL;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700385 for (int i = 0; i < num_regs; i++) {
buzbee56c71782013-09-05 17:13:19 -0700386 if ((p[i].s_reg == s_reg) && p[i].live) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 if (p[i].is_temp)
388 p[i].in_use = true;
389 return &p[i];
390 }
391 }
392 return NULL;
393}
394
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700395Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 RegisterInfo* res = NULL;
397 switch (reg_class) {
398 case kAnyReg:
399 res = AllocLiveBody(reg_pool_->FPRegs,
400 reg_pool_->num_fp_regs, s_reg);
401 if (res)
402 break;
403 /* Intentional fallthrough */
404 case kCoreReg:
405 res = AllocLiveBody(reg_pool_->core_regs,
406 reg_pool_->num_core_regs, s_reg);
407 break;
408 case kFPReg:
409 res = AllocLiveBody(reg_pool_->FPRegs,
410 reg_pool_->num_fp_regs, s_reg);
411 break;
412 default:
413 LOG(FATAL) << "Invalid register type";
414 }
415 return res;
416}
417
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700418void Mir2Lir::FreeTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700419 RegisterInfo* p = GetRegInfo(reg);
420 if (p->is_temp) {
421 p->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 }
buzbee56c71782013-09-05 17:13:19 -0700423 p->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424}
425
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700427 RegisterInfo* p = GetRegInfo(reg);
428 return p->live ? p : NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429}
430
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700431Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 RegisterInfo* p = GetRegInfo(reg);
433 return (p->is_temp) ? p : NULL;
434}
435
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700436Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 RegisterInfo* p = GetRegInfo(reg);
438 return (p->is_temp) ? NULL : p;
439}
440
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700441bool Mir2Lir::IsDirty(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 RegisterInfo* p = GetRegInfo(reg);
443 return p->dirty;
444}
445
446/*
447 * Similar to AllocTemp(), but forces the allocation of a specific
448 * register. No check is made to see if the register was previously
449 * allocated. Use with caution.
450 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700451void Mir2Lir::LockTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700452 RegisterInfo* p = GetRegInfo(reg);
453 DCHECK(p->is_temp);
454 p->in_use = true;
455 p->live = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456}
457
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700458void Mir2Lir::ResetDef(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 ResetDefBody(GetRegInfo(reg));
460}
461
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700462void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 if (start && finish) {
464 LIR *p;
465 DCHECK_EQ(s_reg1, s_reg2);
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700466 for (p = start; ; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 NopLIR(p);
468 if (p == finish)
469 break;
470 }
471 }
472}
473
474/*
475 * Mark the beginning and end LIR of a def sequence. Note that
476 * on entry start points to the LIR prior to the beginning of the
477 * sequence.
478 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700479void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 DCHECK(!rl.wide);
481 DCHECK(start && start->next);
482 DCHECK(finish);
483 RegisterInfo* p = GetRegInfo(rl.low_reg);
484 p->def_start = start->next;
485 p->def_end = finish;
486}
487
488/*
489 * Mark the beginning and end LIR of a def sequence. Note that
490 * on entry start points to the LIR prior to the beginning of the
491 * sequence.
492 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700493void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 DCHECK(rl.wide);
495 DCHECK(start && start->next);
496 DCHECK(finish);
497 RegisterInfo* p = GetRegInfo(rl.low_reg);
498 ResetDef(rl.high_reg); // Only track low of pair
499 p->def_start = start->next;
500 p->def_end = finish;
501}
502
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700503RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 DCHECK(rl.wide);
505 if (rl.location == kLocPhysReg) {
506 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
507 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
508 if (info_lo->is_temp) {
509 info_lo->pair = false;
510 info_lo->def_start = NULL;
511 info_lo->def_end = NULL;
512 }
513 if (info_hi->is_temp) {
514 info_hi->pair = false;
515 info_hi->def_start = NULL;
516 info_hi->def_end = NULL;
517 }
518 }
519 rl.wide = false;
520 return rl;
521}
522
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700523void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 DCHECK(!rl.wide);
525 RegisterInfo* p = IsTemp(rl.low_reg);
526 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
527 DCHECK(!p->pair);
528 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
529 }
530 ResetDef(rl.low_reg);
531}
532
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700533void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 DCHECK(rl.wide);
535 RegisterInfo* p_low = IsTemp(rl.low_reg);
536 RegisterInfo* p_high = IsTemp(rl.high_reg);
537 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
538 DCHECK(p_low->pair);
539 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
540 }
541 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
542 DCHECK(p_high->pair);
543 }
544 ResetDef(rl.low_reg);
545 ResetDef(rl.high_reg);
546}
547
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700548void Mir2Lir::ResetDefTracking() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700549 for (int i = 0; i< reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700550 ResetDefBody(&reg_pool_->core_regs[i]);
551 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700552 for (int i = 0; i< reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 ResetDefBody(&reg_pool_->FPRegs[i]);
554 }
555}
556
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700557void Mir2Lir::ClobberAllRegs() {
buzbeebd663de2013-09-10 15:41:31 -0700558 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
559 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
560 info->live = false;
561 info->s_reg = INVALID_SREG;
562 info->def_start = NULL;
563 info->def_end = NULL;
564 info->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 }
566}
567
568// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700569void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700570 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 if (info[i].live && info[i].dirty) {
572 if (info[i].pair) {
573 FlushRegWide(info[i].reg, info[i].partner);
574 } else {
575 FlushReg(info[i].reg);
576 }
577 }
578 }
579}
580
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700581void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 FlushAllRegsBody(reg_pool_->core_regs,
583 reg_pool_->num_core_regs);
584 FlushAllRegsBody(reg_pool_->FPRegs,
585 reg_pool_->num_fp_regs);
586 ClobberAllRegs();
587}
588
589
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700590// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700591bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 if (reg_class == kAnyReg) {
593 return true;
594 } else if (reg_class == kCoreReg) {
595 return !IsFpReg(reg);
596 } else {
597 return IsFpReg(reg);
598 }
599}
600
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700601void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 RegisterInfo* info = GetRegInfo(reg);
603 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
604 return; /* already live */
605 } else if (s_reg != INVALID_SREG) {
606 ClobberSReg(s_reg);
607 if (info->is_temp) {
608 info->live = true;
609 }
610 } else {
611 /* Can't be live if no associated s_reg */
612 DCHECK(info->is_temp);
613 info->live = false;
614 }
615 info->s_reg = s_reg;
616}
617
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700618void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700620 tempreg_info_.Insert(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 info->is_temp = true;
622}
623
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700624void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700626 tempreg_info_.Delete(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 info->is_temp = false;
628}
629
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700630void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 RegisterInfo* info_lo = GetRegInfo(low_reg);
632 RegisterInfo* info_hi = GetRegInfo(high_reg);
633 info_lo->pair = info_hi->pair = true;
634 info_lo->partner = high_reg;
635 info_hi->partner = low_reg;
636}
637
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700638void Mir2Lir::MarkClean(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 RegisterInfo* info = GetRegInfo(loc.low_reg);
640 info->dirty = false;
641 if (loc.wide) {
642 info = GetRegInfo(loc.high_reg);
643 info->dirty = false;
644 }
645}
646
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700647void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 if (loc.home) {
649 // If already home, can't be dirty
650 return;
651 }
652 RegisterInfo* info = GetRegInfo(loc.low_reg);
653 info->dirty = true;
654 if (loc.wide) {
655 info = GetRegInfo(loc.high_reg);
656 info->dirty = true;
657 }
658}
659
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700660void Mir2Lir::MarkInUse(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 RegisterInfo* info = GetRegInfo(reg);
662 info->in_use = true;
663}
664
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 RegisterInfo* new_info = GetRegInfo(new_reg);
667 RegisterInfo* old_info = GetRegInfo(old_reg);
668 // Target temp status must not change
669 bool is_temp = new_info->is_temp;
670 *new_info = *old_info;
671 // Restore target's temp status
672 new_info->is_temp = is_temp;
673 new_info->reg = new_reg;
674}
675
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700676bool Mir2Lir::CheckCorePoolSanity() {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700677 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
678 if (reg_pool_->core_regs[i].pair) {
679 static int my_reg = reg_pool_->core_regs[i].reg;
680 static int my_sreg = reg_pool_->core_regs[i].s_reg;
681 static int partner_reg = reg_pool_->core_regs[i].partner;
682 static RegisterInfo* partner = GetRegInfo(partner_reg);
683 DCHECK(partner != NULL);
684 DCHECK(partner->pair);
685 DCHECK_EQ(my_reg, partner->partner);
686 static int partner_sreg = partner->s_reg;
687 if (my_sreg == INVALID_SREG) {
688 DCHECK_EQ(partner_sreg, INVALID_SREG);
689 } else {
690 int diff = my_sreg - partner_sreg;
691 DCHECK((diff == -1) || (diff == 1));
692 }
693 }
694 if (!reg_pool_->core_regs[i].live) {
695 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
696 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
697 }
698 }
699 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700}
701
702/*
703 * Return an updated location record with current in-register status.
704 * If the value lives in live temps, reflect that fact. No code
705 * is generated. If the live value is part of an older pair,
706 * clobber both low and high.
707 * TUNING: clobbering both is a bit heavy-handed, but the alternative
708 * is a bit complex when dealing with FP regs. Examine code to see
709 * if it's worthwhile trying to be more clever here.
710 */
711
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700712RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 DCHECK(!loc.wide);
714 DCHECK(CheckCorePoolSanity());
715 if (loc.location != kLocPhysReg) {
716 DCHECK((loc.location == kLocDalvikFrame) ||
717 (loc.location == kLocCompilerTemp));
718 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
719 if (info_lo) {
720 if (info_lo->pair) {
721 Clobber(info_lo->reg);
722 Clobber(info_lo->partner);
723 FreeTemp(info_lo->reg);
724 } else {
725 loc.low_reg = info_lo->reg;
726 loc.location = kLocPhysReg;
727 }
728 }
729 }
730
731 return loc;
732}
733
734/* see comments for update_loc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700735RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 DCHECK(loc.wide);
737 DCHECK(CheckCorePoolSanity());
738 if (loc.location != kLocPhysReg) {
739 DCHECK((loc.location == kLocDalvikFrame) ||
740 (loc.location == kLocCompilerTemp));
741 // Are the dalvik regs already live in physical registers?
742 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
743 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
744 bool match = true;
745 match = match && (info_lo != NULL);
746 match = match && (info_hi != NULL);
747 // Are they both core or both FP?
748 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
749 // If a pair of floating point singles, are they properly aligned?
750 if (match && IsFpReg(info_lo->reg)) {
751 match &= ((info_lo->reg & 0x1) == 0);
752 match &= ((info_hi->reg - info_lo->reg) == 1);
753 }
754 // If previously used as a pair, it is the same pair?
755 if (match && (info_lo->pair || info_hi->pair)) {
756 match = (info_lo->pair == info_hi->pair);
757 match &= ((info_lo->reg == info_hi->partner) &&
758 (info_hi->reg == info_lo->partner));
759 }
760 if (match) {
761 // Can reuse - update the register usage info
762 loc.low_reg = info_lo->reg;
763 loc.high_reg = info_hi->reg;
764 loc.location = kLocPhysReg;
765 MarkPair(loc.low_reg, loc.high_reg);
766 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
767 return loc;
768 }
769 // Can't easily reuse - clobber and free any overlaps
770 if (info_lo) {
771 Clobber(info_lo->reg);
772 FreeTemp(info_lo->reg);
773 if (info_lo->pair)
774 Clobber(info_lo->partner);
775 }
776 if (info_hi) {
777 Clobber(info_hi->reg);
778 FreeTemp(info_hi->reg);
779 if (info_hi->pair)
780 Clobber(info_hi->partner);
781 }
782 }
783 return loc;
784}
785
786
787/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700788RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 if (loc.wide)
790 return UpdateLocWide(loc);
791 else
792 return UpdateLoc(loc);
793}
794
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700795RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 DCHECK(loc.wide);
797 int new_regs;
798 int low_reg;
799 int high_reg;
800
801 loc = UpdateLocWide(loc);
802
803 /* If already in registers, we can assume proper form. Right reg class? */
804 if (loc.location == kLocPhysReg) {
805 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
806 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
807 if (!RegClassMatches(reg_class, loc.low_reg)) {
808 /* Wrong register class. Reallocate and copy */
809 new_regs = AllocTypedTempPair(loc.fp, reg_class);
810 low_reg = new_regs & 0xff;
811 high_reg = (new_regs >> 8) & 0xff;
812 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
813 CopyRegInfo(low_reg, loc.low_reg);
814 CopyRegInfo(high_reg, loc.high_reg);
815 Clobber(loc.low_reg);
816 Clobber(loc.high_reg);
817 loc.low_reg = low_reg;
818 loc.high_reg = high_reg;
819 MarkPair(loc.low_reg, loc.high_reg);
820 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
821 }
822 return loc;
823 }
824
825 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
826 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
827
828 new_regs = AllocTypedTempPair(loc.fp, reg_class);
829 loc.low_reg = new_regs & 0xff;
830 loc.high_reg = (new_regs >> 8) & 0xff;
831
832 MarkPair(loc.low_reg, loc.high_reg);
833 if (update) {
834 loc.location = kLocPhysReg;
835 MarkLive(loc.low_reg, loc.s_reg_low);
836 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
837 }
838 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
839 return loc;
840}
841
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700842RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 int new_reg;
844
845 if (loc.wide)
846 return EvalLocWide(loc, reg_class, update);
847
848 loc = UpdateLoc(loc);
849
850 if (loc.location == kLocPhysReg) {
851 if (!RegClassMatches(reg_class, loc.low_reg)) {
852 /* Wrong register class. Realloc, copy and transfer ownership */
853 new_reg = AllocTypedTemp(loc.fp, reg_class);
854 OpRegCopy(new_reg, loc.low_reg);
855 CopyRegInfo(new_reg, loc.low_reg);
856 Clobber(loc.low_reg);
857 loc.low_reg = new_reg;
858 }
859 return loc;
860 }
861
862 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
863
864 new_reg = AllocTypedTemp(loc.fp, reg_class);
865 loc.low_reg = new_reg;
866
867 if (update) {
868 loc.location = kLocPhysReg;
869 MarkLive(loc.low_reg, loc.s_reg_low);
870 }
871 return loc;
872}
873
874/* USE SSA names to count references of base Dalvik v_regs. */
875void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts) {
876 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
877 RegLocation loc = mir_graph_->reg_location_[i];
878 RefCounts* counts = loc.fp ? fp_counts : core_counts;
879 int p_map_idx = SRegToPMap(loc.s_reg_low);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700880 // Don't count easily regenerated immediates
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 if (loc.fp || !IsInexpensiveConstant(loc)) {
882 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
883 }
884 if (loc.wide && loc.fp && !loc.high_word) {
885 counts[p_map_idx].double_start = true;
886 }
887 }
888}
889
890/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700891static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
893 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700894 // Note that we fall back to sorting on reg so we get stable output
895 // on differing qsort implementations (such as on host and target or
896 // between local host and build servers).
897 return (op1->count == op2->count)
898 ? (op1->s_reg - op2->s_reg)
899 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900}
901
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 LOG(INFO) << msg;
904 for (int i = 0; i < size; i++) {
905 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
906 }
907}
908
909/*
910 * Note: some portions of this code required even if the kPromoteRegs
911 * optimization is disabled.
912 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700913void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 int reg_bias = cu_->num_compiler_temps + 1;
915 int dalvik_regs = cu_->num_dalvik_registers;
916 int num_regs = dalvik_regs + reg_bias;
917 const int promotion_threshold = 1;
918
919 // Allow target code to add any special registers
920 AdjustSpillMask();
921
922 /*
923 * Simple register promotion. Just do a static count of the uses
924 * of Dalvik registers. Note that we examine the SSA names, but
925 * count based on original Dalvik register name. Count refs
926 * separately based on type in order to give allocation
927 * preference to fp doubles - which must be allocated sequential
928 * physical single fp registers started with an even-numbered
929 * reg.
930 * TUNING: replace with linear scan once we have the ability
931 * to describe register live ranges for GC.
932 */
933 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700934 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
935 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 RefCounts *FpRegs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700937 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs,
938 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 // Set ssa names for original Dalvik registers
940 for (int i = 0; i < dalvik_regs; i++) {
941 core_regs[i].s_reg = FpRegs[i].s_reg = i;
942 }
943 // Set ssa name for Method*
944 core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg();
945 FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy
946 // Set ssa names for compiler_temps
947 for (int i = 1; i <= cu_->num_compiler_temps; i++) {
948 CompilerTemp* ct = mir_graph_->compiler_temps_.Get(i);
949 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
950 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
951 }
952
953 // Sum use counts of SSA regs by original Dalvik vreg.
954 CountRefs(core_regs, FpRegs);
955
956 /*
957 * Ideally, we'd allocate doubles starting with an even-numbered
958 * register. Bias the counts to try to allocate any vreg that's
959 * used as the start of a pair first.
960 */
961 for (int i = 0; i < num_regs; i++) {
962 if (FpRegs[i].double_start) {
963 FpRegs[i].count *= 2;
964 }
965 }
966
967 // Sort the count arrays
968 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
969 qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts);
970
971 if (cu_->verbose) {
972 DumpCounts(core_regs, num_regs, "Core regs after sort");
973 DumpCounts(FpRegs, num_regs, "Fp regs after sort");
974 }
975
976 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
977 // Promote FpRegs
Brian Carlstromdf629502013-07-17 22:39:56 -0700978 for (int i = 0; (i < num_regs) && (FpRegs[i].count >= promotion_threshold); i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 int p_map_idx = SRegToPMap(FpRegs[i].s_reg);
980 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
981 int reg = AllocPreservedFPReg(FpRegs[i].s_reg,
982 FpRegs[i].double_start);
983 if (reg < 0) {
984 break; // No more left
985 }
986 }
987 }
988
989 // Promote core regs
990 for (int i = 0; (i < num_regs) &&
991 (core_regs[i].count >= promotion_threshold); i++) {
992 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
993 if (promotion_map_[p_map_idx].core_location !=
994 kLocPhysReg) {
995 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
996 if (reg < 0) {
997 break; // No more left
998 }
999 }
1000 }
1001 }
1002
1003 // Now, update SSA names to new home locations
1004 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1005 RegLocation *curr = &mir_graph_->reg_location_[i];
1006 int p_map_idx = SRegToPMap(curr->s_reg_low);
1007 if (!curr->wide) {
1008 if (curr->fp) {
1009 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1010 curr->location = kLocPhysReg;
1011 curr->low_reg = promotion_map_[p_map_idx].FpReg;
1012 curr->home = true;
1013 }
1014 } else {
1015 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1016 curr->location = kLocPhysReg;
1017 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1018 curr->home = true;
1019 }
1020 }
1021 curr->high_reg = INVALID_REG;
1022 } else {
1023 if (curr->high_word) {
1024 continue;
1025 }
1026 if (curr->fp) {
1027 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1028 (promotion_map_[p_map_idx+1].fp_location ==
1029 kLocPhysReg)) {
1030 int low_reg = promotion_map_[p_map_idx].FpReg;
1031 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1032 // Doubles require pair of singles starting at even reg
1033 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1034 curr->location = kLocPhysReg;
1035 curr->low_reg = low_reg;
1036 curr->high_reg = high_reg;
1037 curr->home = true;
1038 }
1039 }
1040 } else {
1041 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1042 && (promotion_map_[p_map_idx+1].core_location ==
1043 kLocPhysReg)) {
1044 curr->location = kLocPhysReg;
1045 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1046 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1047 curr->home = true;
1048 }
1049 }
1050 }
1051 }
1052 if (cu_->verbose) {
1053 DumpPromotionMap();
1054 }
1055}
1056
1057/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001058int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1060 fp_spill_mask_, frame_size_, v_reg);
1061}
1062
1063/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001064int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1066}
1067
1068/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001069RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 RegLocation gpr_res = LocCReturnWide();
1071 RegLocation fpr_res = LocCReturnDouble();
1072 RegLocation res = is_double ? fpr_res : gpr_res;
1073 Clobber(res.low_reg);
1074 Clobber(res.high_reg);
1075 LockTemp(res.low_reg);
1076 LockTemp(res.high_reg);
1077 MarkPair(res.low_reg, res.high_reg);
1078 return res;
1079}
1080
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001081RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 RegLocation gpr_res = LocCReturn();
1083 RegLocation fpr_res = LocCReturnFloat();
1084 RegLocation res = is_float ? fpr_res : gpr_res;
1085 Clobber(res.low_reg);
1086 if (cu_->instruction_set == kMips) {
1087 MarkInUse(res.low_reg);
1088 } else {
1089 LockTemp(res.low_reg);
1090 }
1091 return res;
1092}
1093
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001094void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 DoPromotion();
1096
1097 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1098 LOG(INFO) << "After Promotion";
1099 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1100 }
1101
1102 /* Set the frame size */
1103 frame_size_ = ComputeFrameSize();
1104}
1105
1106/*
1107 * Get the "real" sreg number associated with an s_reg slot. In general,
1108 * s_reg values passed through codegen are the SSA names created by
1109 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1110 * array. However, renaming is accomplished by simply replacing RegLocation
1111 * entries in the reglocation[] array. Therefore, when location
1112 * records for operands are first created, we need to ask the locRecord
1113 * identified by the dataflow pass what it's new name is.
1114 */
1115int Mir2Lir::GetSRegHi(int lowSreg) {
1116 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1117}
1118
1119bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001120 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 return true;
1122}
1123
1124int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1125 DCHECK_GT(mir->ssa_rep->num_uses, num);
1126 return mir->ssa_rep->uses[num];
1127}
1128
1129} // namespace art