blob: db110aabd507a071f1e00b5d0da7a2bf39768389 [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() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070031 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070032 if (reg_pool_->core_regs[i].is_temp)
33 reg_pool_->core_regs[i].in_use = false;
34 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -070035 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 if (reg_pool_->FPRegs[i].is_temp)
37 reg_pool_->FPRegs[i].in_use = false;
38 }
39 // Reset temp tracking sanity check.
40 if (kIsDebugBuild) {
41 live_sreg_ = INVALID_SREG;
42 }
43}
44
45 /*
46 * Set up temp & preserved register pools specialized by target.
47 * Note: num_regs may be zero.
48 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070049void Mir2Lir::CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070050 for (int i = 0; i < num; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070051 regs[i].reg = reg_nums[i];
52 regs[i].in_use = false;
53 regs[i].is_temp = false;
54 regs[i].pair = false;
55 regs[i].live = false;
56 regs[i].dirty = false;
57 regs[i].s_reg = INVALID_SREG;
58 }
59}
60
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061void Mir2Lir::DumpRegPool(RegisterInfo* p, int num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 LOG(INFO) << "================================================";
63 for (int i = 0; i < num_regs; i++) {
64 LOG(INFO) << StringPrintf(
65 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x",
66 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
67 p[i].live, p[i].dirty, p[i].s_reg, reinterpret_cast<uintptr_t>(p[i].def_start),
68 reinterpret_cast<uintptr_t>(p[i].def_end));
69 }
70 LOG(INFO) << "================================================";
71}
72
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070073void Mir2Lir::DumpCoreRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 DumpRegPool(reg_pool_->core_regs, reg_pool_->num_core_regs);
75}
76
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070077void Mir2Lir::DumpFpRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 DumpRegPool(reg_pool_->FPRegs, reg_pool_->num_fp_regs);
79}
80
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070081void Mir2Lir::ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070082 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 if (p[i].s_reg == s_reg) {
84 if (p[i].is_temp) {
85 p[i].live = false;
86 }
87 p[i].def_start = NULL;
88 p[i].def_end = NULL;
89 }
90 }
91}
92
93/*
94 * Break the association between a Dalvik vreg and a physical temp register of either register
95 * class.
96 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
97 * in the register utilities, is is also used by code gen routines to work around a deficiency in
98 * local register allocation, which fails to distinguish between the "in" and "out" identities
99 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
100 * is used both as the source and destination register of an operation in which the type
101 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
102 * addressed.
103 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700104void Mir2Lir::ClobberSReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 /* Reset live temp tracking sanity checker */
106 if (kIsDebugBuild) {
107 if (s_reg == live_sreg_) {
108 live_sreg_ = INVALID_SREG;
109 }
110 }
111 ClobberSRegBody(reg_pool_->core_regs, reg_pool_->num_core_regs, s_reg);
112 ClobberSRegBody(reg_pool_->FPRegs, reg_pool_->num_fp_regs, s_reg);
113}
114
115/*
116 * SSA names associated with the initial definitions of Dalvik
117 * registers are the same as the Dalvik register number (and
118 * thus take the same position in the promotion_map. However,
119 * the special Method* and compiler temp resisters use negative
120 * v_reg numbers to distinguish them and can have an arbitrary
121 * ssa name (above the last original Dalvik register). This function
122 * maps SSA names to positions in the promotion_map array.
123 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700124int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
126 DCHECK_GE(s_reg, 0);
127 int v_reg = mir_graph_->SRegToVReg(s_reg);
128 if (v_reg >= 0) {
129 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
130 return v_reg;
131 } else {
132 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
133 DCHECK_LE(pos, cu_->num_compiler_temps);
134 return cu_->num_dalvik_registers + pos;
135 }
136}
137
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700138void Mir2Lir::RecordCorePromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 int p_map_idx = SRegToPMap(s_reg);
140 int v_reg = mir_graph_->SRegToVReg(s_reg);
141 GetRegInfo(reg)->in_use = true;
142 core_spill_mask_ |= (1 << reg);
143 // Include reg for later sort
144 core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
145 num_core_spills_++;
146 promotion_map_[p_map_idx].core_location = kLocPhysReg;
147 promotion_map_[p_map_idx].core_reg = reg;
148}
149
150/* Reserve a callee-save register. Return -1 if none available */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700151int Mir2Lir::AllocPreservedCoreReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 int res = -1;
153 RegisterInfo* core_regs = reg_pool_->core_regs;
154 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
155 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
156 res = core_regs[i].reg;
157 RecordCorePromotion(res, s_reg);
158 break;
159 }
160 }
161 return res;
162}
163
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700164void Mir2Lir::RecordFpPromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 int p_map_idx = SRegToPMap(s_reg);
166 int v_reg = mir_graph_->SRegToVReg(s_reg);
167 GetRegInfo(reg)->in_use = true;
168 MarkPreservedSingle(v_reg, reg);
169 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
170 promotion_map_[p_map_idx].FpReg = reg;
171}
172
173/*
174 * Reserve a callee-save fp single register. Try to fullfill request for
175 * even/odd allocation, but go ahead and allocate anything if not
176 * available. If nothing's available, return -1.
177 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700178int Mir2Lir::AllocPreservedSingle(int s_reg, bool even) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 int res = -1;
180 RegisterInfo* FPRegs = reg_pool_->FPRegs;
181 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
182 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
183 ((FPRegs[i].reg & 0x1) == 0) == even) {
184 res = FPRegs[i].reg;
185 RecordFpPromotion(res, s_reg);
186 break;
187 }
188 }
189 return res;
190}
191
192/*
193 * Somewhat messy code here. We want to allocate a pair of contiguous
194 * physical single-precision floating point registers starting with
195 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
196 * has already been allocated - try to fit if possible. Fail to
197 * allocate if we can't meet the requirements for the pair of
198 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
199 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700200int Mir2Lir::AllocPreservedDouble(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700201 int res = -1; // Assume failure
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 int v_reg = mir_graph_->SRegToVReg(s_reg);
203 int p_map_idx = SRegToPMap(s_reg);
204 if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) {
205 // Upper reg is already allocated. Can we fit?
206 int high_reg = promotion_map_[p_map_idx+1].FpReg;
207 if ((high_reg & 1) == 0) {
208 // High reg is even - fail.
209 return res;
210 }
211 // Is the low reg of the pair free?
212 RegisterInfo* p = GetRegInfo(high_reg-1);
213 if (p->in_use || p->is_temp) {
214 // Already allocated or not preserved - fail.
215 return res;
216 }
217 // OK - good to go.
218 res = p->reg;
219 p->in_use = true;
220 DCHECK_EQ((res & 1), 0);
221 MarkPreservedSingle(v_reg, res);
222 } else {
223 RegisterInfo* FPRegs = reg_pool_->FPRegs;
224 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
225 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
226 ((FPRegs[i].reg & 0x1) == 0x0) &&
227 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
228 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
229 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
230 res = FPRegs[i].reg;
231 FPRegs[i].in_use = true;
232 MarkPreservedSingle(v_reg, res);
233 FPRegs[i+1].in_use = true;
234 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
235 MarkPreservedSingle(v_reg+1, res+1);
236 break;
237 }
238 }
239 }
240 if (res != -1) {
241 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
242 promotion_map_[p_map_idx].FpReg = res;
243 promotion_map_[p_map_idx+1].fp_location = kLocPhysReg;
244 promotion_map_[p_map_idx+1].FpReg = res + 1;
245 }
246 return res;
247}
248
249
250/*
251 * Reserve a callee-save fp register. If this register can be used
252 * as the first of a double, attempt to allocate an even pair of fp
253 * single regs (but if can't still attempt to allocate a single, preferring
254 * first to allocate an odd register.
255 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700256int Mir2Lir::AllocPreservedFPReg(int s_reg, bool double_start) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 int res = -1;
258 if (double_start) {
259 res = AllocPreservedDouble(s_reg);
260 }
261 if (res == -1) {
262 res = AllocPreservedSingle(s_reg, false /* try odd # */);
263 }
264 if (res == -1)
265 res = AllocPreservedSingle(s_reg, true /* try even # */);
266 return res;
267}
268
269int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700270 bool required) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700272 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 if (next >= num_regs)
274 next = 0;
275 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
276 Clobber(p[next].reg);
277 p[next].in_use = true;
278 p[next].pair = false;
279 *next_temp = next + 1;
280 return p[next].reg;
281 }
282 next++;
283 }
284 next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700285 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 if (next >= num_regs)
287 next = 0;
288 if (p[next].is_temp && !p[next].in_use) {
289 Clobber(p[next].reg);
290 p[next].in_use = true;
291 p[next].pair = false;
292 *next_temp = next + 1;
293 return p[next].reg;
294 }
295 next++;
296 }
297 if (required) {
298 CodegenDump();
299 DumpRegPool(reg_pool_->core_regs,
300 reg_pool_->num_core_regs);
301 LOG(FATAL) << "No free temp registers";
302 }
303 return -1; // No register available
304}
305
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700306// REDO: too many assumptions.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700307int Mir2Lir::AllocTempDouble() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 RegisterInfo* p = reg_pool_->FPRegs;
309 int num_regs = reg_pool_->num_fp_regs;
310 /* Start looking at an even reg */
311 int next = reg_pool_->next_fp_reg & ~0x1;
312
313 // First try to avoid allocating live registers
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].live) &&
318 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
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 next = reg_pool_->next_fp_reg & ~0x1;
334
335 // No choice - find a pair and kill it.
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700336 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 if (next >= num_regs)
338 next = 0;
339 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
340 !p[next+1].in_use) {
341 Clobber(p[next].reg);
342 Clobber(p[next+1].reg);
343 p[next].in_use = true;
344 p[next+1].in_use = true;
345 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
346 DCHECK_EQ((p[next].reg & 0x1), 0);
347 reg_pool_->next_fp_reg = next + 2;
348 if (reg_pool_->next_fp_reg >= num_regs) {
349 reg_pool_->next_fp_reg = 0;
350 }
351 return p[next].reg;
352 }
353 next += 2;
354 }
355 LOG(FATAL) << "No free temp registers (pair)";
356 return -1;
357}
358
359/* Return a temp if one is available, -1 otherwise */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700360int Mir2Lir::AllocFreeTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 return AllocTempBody(reg_pool_->core_regs,
362 reg_pool_->num_core_regs,
363 &reg_pool_->next_core_reg, true);
364}
365
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700366int Mir2Lir::AllocTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 return AllocTempBody(reg_pool_->core_regs,
368 reg_pool_->num_core_regs,
369 &reg_pool_->next_core_reg, true);
370}
371
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700372int Mir2Lir::AllocTempFloat() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 return AllocTempBody(reg_pool_->FPRegs,
374 reg_pool_->num_fp_regs,
375 &reg_pool_->next_fp_reg, true);
376}
377
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700378Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 if (s_reg == -1)
380 return NULL;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700381 for (int i = 0; i < num_regs; i++) {
buzbee56c71782013-09-05 17:13:19 -0700382 if ((p[i].s_reg == s_reg) && p[i].live) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 if (p[i].is_temp)
384 p[i].in_use = true;
385 return &p[i];
386 }
387 }
388 return NULL;
389}
390
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700391Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 RegisterInfo* res = NULL;
393 switch (reg_class) {
394 case kAnyReg:
395 res = AllocLiveBody(reg_pool_->FPRegs,
396 reg_pool_->num_fp_regs, s_reg);
397 if (res)
398 break;
399 /* Intentional fallthrough */
400 case kCoreReg:
401 res = AllocLiveBody(reg_pool_->core_regs,
402 reg_pool_->num_core_regs, s_reg);
403 break;
404 case kFPReg:
405 res = AllocLiveBody(reg_pool_->FPRegs,
406 reg_pool_->num_fp_regs, s_reg);
407 break;
408 default:
409 LOG(FATAL) << "Invalid register type";
410 }
411 return res;
412}
413
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700414void Mir2Lir::FreeTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700415 RegisterInfo* p = GetRegInfo(reg);
416 if (p->is_temp) {
417 p->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418 }
buzbee56c71782013-09-05 17:13:19 -0700419 p->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420}
421
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700422Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700423 RegisterInfo* p = GetRegInfo(reg);
424 return p->live ? p : NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425}
426
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700427Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 RegisterInfo* p = GetRegInfo(reg);
429 return (p->is_temp) ? p : NULL;
430}
431
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700432Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 RegisterInfo* p = GetRegInfo(reg);
434 return (p->is_temp) ? NULL : p;
435}
436
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700437bool Mir2Lir::IsDirty(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 RegisterInfo* p = GetRegInfo(reg);
439 return p->dirty;
440}
441
442/*
443 * Similar to AllocTemp(), but forces the allocation of a specific
444 * register. No check is made to see if the register was previously
445 * allocated. Use with caution.
446 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700447void Mir2Lir::LockTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700448 RegisterInfo* p = GetRegInfo(reg);
449 DCHECK(p->is_temp);
450 p->in_use = true;
451 p->live = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452}
453
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700454void Mir2Lir::ResetDef(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 ResetDefBody(GetRegInfo(reg));
456}
457
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700458void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 if (start && finish) {
460 LIR *p;
461 DCHECK_EQ(s_reg1, s_reg2);
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700462 for (p = start; ; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 NopLIR(p);
464 if (p == finish)
465 break;
466 }
467 }
468}
469
470/*
471 * Mark the beginning and end LIR of a def sequence. Note that
472 * on entry start points to the LIR prior to the beginning of the
473 * sequence.
474 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700475void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 DCHECK(!rl.wide);
477 DCHECK(start && start->next);
478 DCHECK(finish);
479 RegisterInfo* p = GetRegInfo(rl.low_reg);
480 p->def_start = start->next;
481 p->def_end = finish;
482}
483
484/*
485 * Mark the beginning and end LIR of a def sequence. Note that
486 * on entry start points to the LIR prior to the beginning of the
487 * sequence.
488 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700489void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 DCHECK(rl.wide);
491 DCHECK(start && start->next);
492 DCHECK(finish);
493 RegisterInfo* p = GetRegInfo(rl.low_reg);
494 ResetDef(rl.high_reg); // Only track low of pair
495 p->def_start = start->next;
496 p->def_end = finish;
497}
498
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700499RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 DCHECK(rl.wide);
501 if (rl.location == kLocPhysReg) {
502 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
503 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
504 if (info_lo->is_temp) {
505 info_lo->pair = false;
506 info_lo->def_start = NULL;
507 info_lo->def_end = NULL;
508 }
509 if (info_hi->is_temp) {
510 info_hi->pair = false;
511 info_hi->def_start = NULL;
512 info_hi->def_end = NULL;
513 }
514 }
515 rl.wide = false;
516 return rl;
517}
518
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700519void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 DCHECK(!rl.wide);
521 RegisterInfo* p = IsTemp(rl.low_reg);
522 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
523 DCHECK(!p->pair);
524 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
525 }
526 ResetDef(rl.low_reg);
527}
528
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700529void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 DCHECK(rl.wide);
531 RegisterInfo* p_low = IsTemp(rl.low_reg);
532 RegisterInfo* p_high = IsTemp(rl.high_reg);
533 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
534 DCHECK(p_low->pair);
535 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
536 }
537 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
538 DCHECK(p_high->pair);
539 }
540 ResetDef(rl.low_reg);
541 ResetDef(rl.high_reg);
542}
543
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700544void Mir2Lir::ResetDefTracking() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700545 for (int i = 0; i< reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 ResetDefBody(&reg_pool_->core_regs[i]);
547 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700548 for (int i = 0; i< reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 ResetDefBody(&reg_pool_->FPRegs[i]);
550 }
551}
552
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700553void Mir2Lir::ClobberAllRegs() {
buzbee56c71782013-09-05 17:13:19 -0700554 RegisterInfo* p;
555 for (p = reg_pool_->core_regs; p < reg_pool_->core_regs + reg_pool_->num_core_regs; p++) {
556 if (p->is_temp) {
557 p->live = false;
558 p->s_reg = INVALID_SREG;
559 p->def_start = NULL;
560 p->def_end = NULL;
561 p->pair = false;
562 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 }
buzbee56c71782013-09-05 17:13:19 -0700564 for (p = reg_pool_->FPRegs; p < reg_pool_->FPRegs + reg_pool_->num_fp_regs; p++) {
565 if (p->is_temp) {
566 p->live = false;
567 p->s_reg = INVALID_SREG;
568 p->def_start = NULL;
569 p->def_end = NULL;
570 p->pair = false;
571 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 }
573}
574
575// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700576void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700577 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 if (info[i].live && info[i].dirty) {
579 if (info[i].pair) {
580 FlushRegWide(info[i].reg, info[i].partner);
581 } else {
582 FlushReg(info[i].reg);
583 }
584 }
585 }
586}
587
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700588void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589 FlushAllRegsBody(reg_pool_->core_regs,
590 reg_pool_->num_core_regs);
591 FlushAllRegsBody(reg_pool_->FPRegs,
592 reg_pool_->num_fp_regs);
593 ClobberAllRegs();
594}
595
596
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700597// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700598bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 if (reg_class == kAnyReg) {
600 return true;
601 } else if (reg_class == kCoreReg) {
602 return !IsFpReg(reg);
603 } else {
604 return IsFpReg(reg);
605 }
606}
607
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700608void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 RegisterInfo* info = GetRegInfo(reg);
610 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
611 return; /* already live */
612 } else if (s_reg != INVALID_SREG) {
613 ClobberSReg(s_reg);
614 if (info->is_temp) {
615 info->live = true;
616 }
617 } else {
618 /* Can't be live if no associated s_reg */
619 DCHECK(info->is_temp);
620 info->live = false;
621 }
622 info->s_reg = s_reg;
623}
624
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700625void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 RegisterInfo* info = GetRegInfo(reg);
627 info->is_temp = true;
628}
629
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700630void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 RegisterInfo* info = GetRegInfo(reg);
632 info->is_temp = false;
633}
634
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700635void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 RegisterInfo* info_lo = GetRegInfo(low_reg);
637 RegisterInfo* info_hi = GetRegInfo(high_reg);
638 info_lo->pair = info_hi->pair = true;
639 info_lo->partner = high_reg;
640 info_hi->partner = low_reg;
641}
642
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700643void Mir2Lir::MarkClean(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 RegisterInfo* info = GetRegInfo(loc.low_reg);
645 info->dirty = false;
646 if (loc.wide) {
647 info = GetRegInfo(loc.high_reg);
648 info->dirty = false;
649 }
650}
651
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700652void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 if (loc.home) {
654 // If already home, can't be dirty
655 return;
656 }
657 RegisterInfo* info = GetRegInfo(loc.low_reg);
658 info->dirty = true;
659 if (loc.wide) {
660 info = GetRegInfo(loc.high_reg);
661 info->dirty = true;
662 }
663}
664
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665void Mir2Lir::MarkInUse(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 RegisterInfo* info = GetRegInfo(reg);
667 info->in_use = true;
668}
669
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700670void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 RegisterInfo* new_info = GetRegInfo(new_reg);
672 RegisterInfo* old_info = GetRegInfo(old_reg);
673 // Target temp status must not change
674 bool is_temp = new_info->is_temp;
675 *new_info = *old_info;
676 // Restore target's temp status
677 new_info->is_temp = is_temp;
678 new_info->reg = new_reg;
679}
680
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700681bool Mir2Lir::CheckCorePoolSanity() {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700682 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
683 if (reg_pool_->core_regs[i].pair) {
684 static int my_reg = reg_pool_->core_regs[i].reg;
685 static int my_sreg = reg_pool_->core_regs[i].s_reg;
686 static int partner_reg = reg_pool_->core_regs[i].partner;
687 static RegisterInfo* partner = GetRegInfo(partner_reg);
688 DCHECK(partner != NULL);
689 DCHECK(partner->pair);
690 DCHECK_EQ(my_reg, partner->partner);
691 static int partner_sreg = partner->s_reg;
692 if (my_sreg == INVALID_SREG) {
693 DCHECK_EQ(partner_sreg, INVALID_SREG);
694 } else {
695 int diff = my_sreg - partner_sreg;
696 DCHECK((diff == -1) || (diff == 1));
697 }
698 }
699 if (!reg_pool_->core_regs[i].live) {
700 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
701 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
702 }
703 }
704 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705}
706
707/*
708 * Return an updated location record with current in-register status.
709 * If the value lives in live temps, reflect that fact. No code
710 * is generated. If the live value is part of an older pair,
711 * clobber both low and high.
712 * TUNING: clobbering both is a bit heavy-handed, but the alternative
713 * is a bit complex when dealing with FP regs. Examine code to see
714 * if it's worthwhile trying to be more clever here.
715 */
716
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700717RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 DCHECK(!loc.wide);
719 DCHECK(CheckCorePoolSanity());
720 if (loc.location != kLocPhysReg) {
721 DCHECK((loc.location == kLocDalvikFrame) ||
722 (loc.location == kLocCompilerTemp));
723 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
724 if (info_lo) {
725 if (info_lo->pair) {
726 Clobber(info_lo->reg);
727 Clobber(info_lo->partner);
728 FreeTemp(info_lo->reg);
729 } else {
730 loc.low_reg = info_lo->reg;
731 loc.location = kLocPhysReg;
732 }
733 }
734 }
735
736 return loc;
737}
738
739/* see comments for update_loc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700740RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700741 DCHECK(loc.wide);
742 DCHECK(CheckCorePoolSanity());
743 if (loc.location != kLocPhysReg) {
744 DCHECK((loc.location == kLocDalvikFrame) ||
745 (loc.location == kLocCompilerTemp));
746 // Are the dalvik regs already live in physical registers?
747 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
748 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
749 bool match = true;
750 match = match && (info_lo != NULL);
751 match = match && (info_hi != NULL);
752 // Are they both core or both FP?
753 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
754 // If a pair of floating point singles, are they properly aligned?
755 if (match && IsFpReg(info_lo->reg)) {
756 match &= ((info_lo->reg & 0x1) == 0);
757 match &= ((info_hi->reg - info_lo->reg) == 1);
758 }
759 // If previously used as a pair, it is the same pair?
760 if (match && (info_lo->pair || info_hi->pair)) {
761 match = (info_lo->pair == info_hi->pair);
762 match &= ((info_lo->reg == info_hi->partner) &&
763 (info_hi->reg == info_lo->partner));
764 }
765 if (match) {
766 // Can reuse - update the register usage info
767 loc.low_reg = info_lo->reg;
768 loc.high_reg = info_hi->reg;
769 loc.location = kLocPhysReg;
770 MarkPair(loc.low_reg, loc.high_reg);
771 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
772 return loc;
773 }
774 // Can't easily reuse - clobber and free any overlaps
775 if (info_lo) {
776 Clobber(info_lo->reg);
777 FreeTemp(info_lo->reg);
778 if (info_lo->pair)
779 Clobber(info_lo->partner);
780 }
781 if (info_hi) {
782 Clobber(info_hi->reg);
783 FreeTemp(info_hi->reg);
784 if (info_hi->pair)
785 Clobber(info_hi->partner);
786 }
787 }
788 return loc;
789}
790
791
792/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700793RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700794 if (loc.wide)
795 return UpdateLocWide(loc);
796 else
797 return UpdateLoc(loc);
798}
799
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700800RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 DCHECK(loc.wide);
802 int new_regs;
803 int low_reg;
804 int high_reg;
805
806 loc = UpdateLocWide(loc);
807
808 /* If already in registers, we can assume proper form. Right reg class? */
809 if (loc.location == kLocPhysReg) {
810 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
811 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
812 if (!RegClassMatches(reg_class, loc.low_reg)) {
813 /* Wrong register class. Reallocate and copy */
814 new_regs = AllocTypedTempPair(loc.fp, reg_class);
815 low_reg = new_regs & 0xff;
816 high_reg = (new_regs >> 8) & 0xff;
817 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
818 CopyRegInfo(low_reg, loc.low_reg);
819 CopyRegInfo(high_reg, loc.high_reg);
820 Clobber(loc.low_reg);
821 Clobber(loc.high_reg);
822 loc.low_reg = low_reg;
823 loc.high_reg = high_reg;
824 MarkPair(loc.low_reg, loc.high_reg);
825 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
826 }
827 return loc;
828 }
829
830 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
831 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
832
833 new_regs = AllocTypedTempPair(loc.fp, reg_class);
834 loc.low_reg = new_regs & 0xff;
835 loc.high_reg = (new_regs >> 8) & 0xff;
836
837 MarkPair(loc.low_reg, loc.high_reg);
838 if (update) {
839 loc.location = kLocPhysReg;
840 MarkLive(loc.low_reg, loc.s_reg_low);
841 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
842 }
843 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
844 return loc;
845}
846
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700847RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 int new_reg;
849
850 if (loc.wide)
851 return EvalLocWide(loc, reg_class, update);
852
853 loc = UpdateLoc(loc);
854
855 if (loc.location == kLocPhysReg) {
856 if (!RegClassMatches(reg_class, loc.low_reg)) {
857 /* Wrong register class. Realloc, copy and transfer ownership */
858 new_reg = AllocTypedTemp(loc.fp, reg_class);
859 OpRegCopy(new_reg, loc.low_reg);
860 CopyRegInfo(new_reg, loc.low_reg);
861 Clobber(loc.low_reg);
862 loc.low_reg = new_reg;
863 }
864 return loc;
865 }
866
867 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
868
869 new_reg = AllocTypedTemp(loc.fp, reg_class);
870 loc.low_reg = new_reg;
871
872 if (update) {
873 loc.location = kLocPhysReg;
874 MarkLive(loc.low_reg, loc.s_reg_low);
875 }
876 return loc;
877}
878
879/* USE SSA names to count references of base Dalvik v_regs. */
880void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts) {
881 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
882 RegLocation loc = mir_graph_->reg_location_[i];
883 RefCounts* counts = loc.fp ? fp_counts : core_counts;
884 int p_map_idx = SRegToPMap(loc.s_reg_low);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700885 // Don't count easily regenerated immediates
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 if (loc.fp || !IsInexpensiveConstant(loc)) {
887 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
888 }
889 if (loc.wide && loc.fp && !loc.high_word) {
890 counts[p_map_idx].double_start = true;
891 }
892 }
893}
894
895/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700896static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
898 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700899 // Note that we fall back to sorting on reg so we get stable output
900 // on differing qsort implementations (such as on host and target or
901 // between local host and build servers).
902 return (op1->count == op2->count)
903 ? (op1->s_reg - op2->s_reg)
904 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905}
906
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700907void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 LOG(INFO) << msg;
909 for (int i = 0; i < size; i++) {
910 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
911 }
912}
913
914/*
915 * Note: some portions of this code required even if the kPromoteRegs
916 * optimization is disabled.
917 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700918void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 int reg_bias = cu_->num_compiler_temps + 1;
920 int dalvik_regs = cu_->num_dalvik_registers;
921 int num_regs = dalvik_regs + reg_bias;
922 const int promotion_threshold = 1;
923
924 // Allow target code to add any special registers
925 AdjustSpillMask();
926
927 /*
928 * Simple register promotion. Just do a static count of the uses
929 * of Dalvik registers. Note that we examine the SSA names, but
930 * count based on original Dalvik register name. Count refs
931 * separately based on type in order to give allocation
932 * preference to fp doubles - which must be allocated sequential
933 * physical single fp registers started with an even-numbered
934 * reg.
935 * TUNING: replace with linear scan once we have the ability
936 * to describe register live ranges for GC.
937 */
938 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700939 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
940 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700941 RefCounts *FpRegs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700942 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs,
943 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 // Set ssa names for original Dalvik registers
945 for (int i = 0; i < dalvik_regs; i++) {
946 core_regs[i].s_reg = FpRegs[i].s_reg = i;
947 }
948 // Set ssa name for Method*
949 core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg();
950 FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy
951 // Set ssa names for compiler_temps
952 for (int i = 1; i <= cu_->num_compiler_temps; i++) {
953 CompilerTemp* ct = mir_graph_->compiler_temps_.Get(i);
954 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
955 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
956 }
957
958 // Sum use counts of SSA regs by original Dalvik vreg.
959 CountRefs(core_regs, FpRegs);
960
961 /*
962 * Ideally, we'd allocate doubles starting with an even-numbered
963 * register. Bias the counts to try to allocate any vreg that's
964 * used as the start of a pair first.
965 */
966 for (int i = 0; i < num_regs; i++) {
967 if (FpRegs[i].double_start) {
968 FpRegs[i].count *= 2;
969 }
970 }
971
972 // Sort the count arrays
973 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
974 qsort(FpRegs, num_regs, sizeof(RefCounts), SortCounts);
975
976 if (cu_->verbose) {
977 DumpCounts(core_regs, num_regs, "Core regs after sort");
978 DumpCounts(FpRegs, num_regs, "Fp regs after sort");
979 }
980
981 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
982 // Promote FpRegs
Brian Carlstromdf629502013-07-17 22:39:56 -0700983 for (int i = 0; (i < num_regs) && (FpRegs[i].count >= promotion_threshold); i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 int p_map_idx = SRegToPMap(FpRegs[i].s_reg);
985 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
986 int reg = AllocPreservedFPReg(FpRegs[i].s_reg,
987 FpRegs[i].double_start);
988 if (reg < 0) {
989 break; // No more left
990 }
991 }
992 }
993
994 // Promote core regs
995 for (int i = 0; (i < num_regs) &&
996 (core_regs[i].count >= promotion_threshold); i++) {
997 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
998 if (promotion_map_[p_map_idx].core_location !=
999 kLocPhysReg) {
1000 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1001 if (reg < 0) {
1002 break; // No more left
1003 }
1004 }
1005 }
1006 }
1007
1008 // Now, update SSA names to new home locations
1009 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1010 RegLocation *curr = &mir_graph_->reg_location_[i];
1011 int p_map_idx = SRegToPMap(curr->s_reg_low);
1012 if (!curr->wide) {
1013 if (curr->fp) {
1014 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1015 curr->location = kLocPhysReg;
1016 curr->low_reg = promotion_map_[p_map_idx].FpReg;
1017 curr->home = true;
1018 }
1019 } else {
1020 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1021 curr->location = kLocPhysReg;
1022 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1023 curr->home = true;
1024 }
1025 }
1026 curr->high_reg = INVALID_REG;
1027 } else {
1028 if (curr->high_word) {
1029 continue;
1030 }
1031 if (curr->fp) {
1032 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1033 (promotion_map_[p_map_idx+1].fp_location ==
1034 kLocPhysReg)) {
1035 int low_reg = promotion_map_[p_map_idx].FpReg;
1036 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1037 // Doubles require pair of singles starting at even reg
1038 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1039 curr->location = kLocPhysReg;
1040 curr->low_reg = low_reg;
1041 curr->high_reg = high_reg;
1042 curr->home = true;
1043 }
1044 }
1045 } else {
1046 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1047 && (promotion_map_[p_map_idx+1].core_location ==
1048 kLocPhysReg)) {
1049 curr->location = kLocPhysReg;
1050 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1051 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1052 curr->home = true;
1053 }
1054 }
1055 }
1056 }
1057 if (cu_->verbose) {
1058 DumpPromotionMap();
1059 }
1060}
1061
1062/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001063int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1065 fp_spill_mask_, frame_size_, v_reg);
1066}
1067
1068/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001069int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1071}
1072
1073/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001074RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 RegLocation gpr_res = LocCReturnWide();
1076 RegLocation fpr_res = LocCReturnDouble();
1077 RegLocation res = is_double ? fpr_res : gpr_res;
1078 Clobber(res.low_reg);
1079 Clobber(res.high_reg);
1080 LockTemp(res.low_reg);
1081 LockTemp(res.high_reg);
1082 MarkPair(res.low_reg, res.high_reg);
1083 return res;
1084}
1085
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001086RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 RegLocation gpr_res = LocCReturn();
1088 RegLocation fpr_res = LocCReturnFloat();
1089 RegLocation res = is_float ? fpr_res : gpr_res;
1090 Clobber(res.low_reg);
1091 if (cu_->instruction_set == kMips) {
1092 MarkInUse(res.low_reg);
1093 } else {
1094 LockTemp(res.low_reg);
1095 }
1096 return res;
1097}
1098
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001099void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 DoPromotion();
1101
1102 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1103 LOG(INFO) << "After Promotion";
1104 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1105 }
1106
1107 /* Set the frame size */
1108 frame_size_ = ComputeFrameSize();
1109}
1110
1111/*
1112 * Get the "real" sreg number associated with an s_reg slot. In general,
1113 * s_reg values passed through codegen are the SSA names created by
1114 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1115 * array. However, renaming is accomplished by simply replacing RegLocation
1116 * entries in the reglocation[] array. Therefore, when location
1117 * records for operands are first created, we need to ask the locRecord
1118 * identified by the dataflow pass what it's new name is.
1119 */
1120int Mir2Lir::GetSRegHi(int lowSreg) {
1121 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1122}
1123
1124bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001125 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 return true;
1127}
1128
1129int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1130 DCHECK_GT(mir->ssa_rep->num_uses, num);
1131 return mir->ssa_rep->uses[num];
1132}
1133
1134} // namespace art