blob: 7927ff9864fa17543162ed9235b9b8a2d34bb2c6 [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
buzbeec729a6b2013-09-14 16:04:31 -0700177// Reserve a callee-save fp single register.
178int Mir2Lir::AllocPreservedSingle(int s_reg) {
179 int res = -1; // Return code if none available.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 RegisterInfo* FPRegs = reg_pool_->FPRegs;
181 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700182 if (!FPRegs[i].is_temp && !FPRegs[i].in_use) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 res = FPRegs[i].reg;
184 RecordFpPromotion(res, s_reg);
185 break;
186 }
187 }
188 return res;
189}
190
191/*
192 * Somewhat messy code here. We want to allocate a pair of contiguous
193 * physical single-precision floating point registers starting with
194 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
195 * has already been allocated - try to fit if possible. Fail to
196 * allocate if we can't meet the requirements for the pair of
197 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
198 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700199int Mir2Lir::AllocPreservedDouble(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700200 int res = -1; // Assume failure
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 int v_reg = mir_graph_->SRegToVReg(s_reg);
202 int p_map_idx = SRegToPMap(s_reg);
203 if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) {
204 // Upper reg is already allocated. Can we fit?
205 int high_reg = promotion_map_[p_map_idx+1].FpReg;
206 if ((high_reg & 1) == 0) {
207 // High reg is even - fail.
208 return res;
209 }
210 // Is the low reg of the pair free?
211 RegisterInfo* p = GetRegInfo(high_reg-1);
212 if (p->in_use || p->is_temp) {
213 // Already allocated or not preserved - fail.
214 return res;
215 }
216 // OK - good to go.
217 res = p->reg;
218 p->in_use = true;
219 DCHECK_EQ((res & 1), 0);
220 MarkPreservedSingle(v_reg, res);
221 } else {
222 RegisterInfo* FPRegs = reg_pool_->FPRegs;
223 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
224 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
225 ((FPRegs[i].reg & 0x1) == 0x0) &&
226 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
227 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
228 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
229 res = FPRegs[i].reg;
230 FPRegs[i].in_use = true;
231 MarkPreservedSingle(v_reg, res);
232 FPRegs[i+1].in_use = true;
233 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
234 MarkPreservedSingle(v_reg+1, res+1);
235 break;
236 }
237 }
238 }
239 if (res != -1) {
240 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
241 promotion_map_[p_map_idx].FpReg = res;
242 promotion_map_[p_map_idx+1].fp_location = kLocPhysReg;
243 promotion_map_[p_map_idx+1].FpReg = res + 1;
244 }
245 return res;
246}
247
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700249 bool required) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700251 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 if (next >= num_regs)
253 next = 0;
254 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
255 Clobber(p[next].reg);
256 p[next].in_use = true;
257 p[next].pair = false;
258 *next_temp = next + 1;
259 return p[next].reg;
260 }
261 next++;
262 }
263 next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700264 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 if (next >= num_regs)
266 next = 0;
267 if (p[next].is_temp && !p[next].in_use) {
268 Clobber(p[next].reg);
269 p[next].in_use = true;
270 p[next].pair = false;
271 *next_temp = next + 1;
272 return p[next].reg;
273 }
274 next++;
275 }
276 if (required) {
277 CodegenDump();
278 DumpRegPool(reg_pool_->core_regs,
279 reg_pool_->num_core_regs);
280 LOG(FATAL) << "No free temp registers";
281 }
282 return -1; // No register available
283}
284
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700285// REDO: too many assumptions.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700286int Mir2Lir::AllocTempDouble() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 RegisterInfo* p = reg_pool_->FPRegs;
288 int num_regs = reg_pool_->num_fp_regs;
289 /* Start looking at an even reg */
290 int next = reg_pool_->next_fp_reg & ~0x1;
291
292 // First try to avoid allocating live registers
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700293 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 if (next >= num_regs)
295 next = 0;
296 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
297 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
298 Clobber(p[next].reg);
299 Clobber(p[next+1].reg);
300 p[next].in_use = true;
301 p[next+1].in_use = true;
302 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
303 DCHECK_EQ((p[next].reg & 0x1), 0);
304 reg_pool_->next_fp_reg = next + 2;
305 if (reg_pool_->next_fp_reg >= num_regs) {
306 reg_pool_->next_fp_reg = 0;
307 }
308 return p[next].reg;
309 }
310 next += 2;
311 }
312 next = reg_pool_->next_fp_reg & ~0x1;
313
314 // No choice - find a pair and kill it.
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700315 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 if (next >= num_regs)
317 next = 0;
318 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
319 !p[next+1].in_use) {
320 Clobber(p[next].reg);
321 Clobber(p[next+1].reg);
322 p[next].in_use = true;
323 p[next+1].in_use = true;
324 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
325 DCHECK_EQ((p[next].reg & 0x1), 0);
326 reg_pool_->next_fp_reg = next + 2;
327 if (reg_pool_->next_fp_reg >= num_regs) {
328 reg_pool_->next_fp_reg = 0;
329 }
330 return p[next].reg;
331 }
332 next += 2;
333 }
334 LOG(FATAL) << "No free temp registers (pair)";
335 return -1;
336}
337
338/* Return a temp if one is available, -1 otherwise */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700339int Mir2Lir::AllocFreeTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 return AllocTempBody(reg_pool_->core_regs,
341 reg_pool_->num_core_regs,
342 &reg_pool_->next_core_reg, true);
343}
344
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700345int Mir2Lir::AllocTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 return AllocTempBody(reg_pool_->core_regs,
347 reg_pool_->num_core_regs,
348 &reg_pool_->next_core_reg, true);
349}
350
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700351int Mir2Lir::AllocTempFloat() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 return AllocTempBody(reg_pool_->FPRegs,
353 reg_pool_->num_fp_regs,
354 &reg_pool_->next_fp_reg, true);
355}
356
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700357Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 if (s_reg == -1)
359 return NULL;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700360 for (int i = 0; i < num_regs; i++) {
buzbee56c71782013-09-05 17:13:19 -0700361 if ((p[i].s_reg == s_reg) && p[i].live) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 if (p[i].is_temp)
363 p[i].in_use = true;
364 return &p[i];
365 }
366 }
367 return NULL;
368}
369
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700370Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 RegisterInfo* res = NULL;
372 switch (reg_class) {
373 case kAnyReg:
374 res = AllocLiveBody(reg_pool_->FPRegs,
375 reg_pool_->num_fp_regs, s_reg);
376 if (res)
377 break;
378 /* Intentional fallthrough */
379 case kCoreReg:
380 res = AllocLiveBody(reg_pool_->core_regs,
381 reg_pool_->num_core_regs, s_reg);
382 break;
383 case kFPReg:
384 res = AllocLiveBody(reg_pool_->FPRegs,
385 reg_pool_->num_fp_regs, s_reg);
386 break;
387 default:
388 LOG(FATAL) << "Invalid register type";
389 }
390 return res;
391}
392
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700393void Mir2Lir::FreeTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700394 RegisterInfo* p = GetRegInfo(reg);
395 if (p->is_temp) {
396 p->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 }
buzbee56c71782013-09-05 17:13:19 -0700398 p->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399}
400
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700401Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700402 RegisterInfo* p = GetRegInfo(reg);
403 return p->live ? p : NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404}
405
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700406Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 RegisterInfo* p = GetRegInfo(reg);
408 return (p->is_temp) ? p : NULL;
409}
410
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700411Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 RegisterInfo* p = GetRegInfo(reg);
413 return (p->is_temp) ? NULL : p;
414}
415
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700416bool Mir2Lir::IsDirty(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 RegisterInfo* p = GetRegInfo(reg);
418 return p->dirty;
419}
420
421/*
422 * Similar to AllocTemp(), but forces the allocation of a specific
423 * register. No check is made to see if the register was previously
424 * allocated. Use with caution.
425 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426void Mir2Lir::LockTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700427 RegisterInfo* p = GetRegInfo(reg);
428 DCHECK(p->is_temp);
429 p->in_use = true;
430 p->live = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431}
432
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700433void Mir2Lir::ResetDef(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 ResetDefBody(GetRegInfo(reg));
435}
436
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700437void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 if (start && finish) {
439 LIR *p;
440 DCHECK_EQ(s_reg1, s_reg2);
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700441 for (p = start; ; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 NopLIR(p);
443 if (p == finish)
444 break;
445 }
446 }
447}
448
449/*
450 * Mark the beginning and end LIR of a def sequence. Note that
451 * on entry start points to the LIR prior to the beginning of the
452 * sequence.
453 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700454void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 DCHECK(!rl.wide);
456 DCHECK(start && start->next);
457 DCHECK(finish);
458 RegisterInfo* p = GetRegInfo(rl.low_reg);
459 p->def_start = start->next;
460 p->def_end = finish;
461}
462
463/*
464 * Mark the beginning and end LIR of a def sequence. Note that
465 * on entry start points to the LIR prior to the beginning of the
466 * sequence.
467 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700468void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 DCHECK(rl.wide);
470 DCHECK(start && start->next);
471 DCHECK(finish);
472 RegisterInfo* p = GetRegInfo(rl.low_reg);
473 ResetDef(rl.high_reg); // Only track low of pair
474 p->def_start = start->next;
475 p->def_end = finish;
476}
477
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700478RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 DCHECK(rl.wide);
480 if (rl.location == kLocPhysReg) {
481 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
482 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
483 if (info_lo->is_temp) {
484 info_lo->pair = false;
485 info_lo->def_start = NULL;
486 info_lo->def_end = NULL;
487 }
488 if (info_hi->is_temp) {
489 info_hi->pair = false;
490 info_hi->def_start = NULL;
491 info_hi->def_end = NULL;
492 }
493 }
494 rl.wide = false;
495 return rl;
496}
497
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700498void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 DCHECK(!rl.wide);
500 RegisterInfo* p = IsTemp(rl.low_reg);
501 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
502 DCHECK(!p->pair);
503 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
504 }
505 ResetDef(rl.low_reg);
506}
507
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700508void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 DCHECK(rl.wide);
510 RegisterInfo* p_low = IsTemp(rl.low_reg);
511 RegisterInfo* p_high = IsTemp(rl.high_reg);
512 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
513 DCHECK(p_low->pair);
514 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
515 }
516 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
517 DCHECK(p_high->pair);
518 }
519 ResetDef(rl.low_reg);
520 ResetDef(rl.high_reg);
521}
522
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700523void Mir2Lir::ResetDefTracking() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700524 for (int i = 0; i< reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 ResetDefBody(&reg_pool_->core_regs[i]);
526 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700527 for (int i = 0; i< reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 ResetDefBody(&reg_pool_->FPRegs[i]);
529 }
530}
531
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700532void Mir2Lir::ClobberAllRegs() {
buzbeebd663de2013-09-10 15:41:31 -0700533 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
534 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
535 info->live = false;
536 info->s_reg = INVALID_SREG;
537 info->def_start = NULL;
538 info->def_end = NULL;
539 info->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 }
541}
542
543// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700544void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700545 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 if (info[i].live && info[i].dirty) {
547 if (info[i].pair) {
548 FlushRegWide(info[i].reg, info[i].partner);
549 } else {
550 FlushReg(info[i].reg);
551 }
552 }
553 }
554}
555
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700556void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 FlushAllRegsBody(reg_pool_->core_regs,
558 reg_pool_->num_core_regs);
559 FlushAllRegsBody(reg_pool_->FPRegs,
560 reg_pool_->num_fp_regs);
561 ClobberAllRegs();
562}
563
564
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700565// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700566bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 if (reg_class == kAnyReg) {
568 return true;
569 } else if (reg_class == kCoreReg) {
570 return !IsFpReg(reg);
571 } else {
572 return IsFpReg(reg);
573 }
574}
575
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700576void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 RegisterInfo* info = GetRegInfo(reg);
578 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
579 return; /* already live */
580 } else if (s_reg != INVALID_SREG) {
581 ClobberSReg(s_reg);
582 if (info->is_temp) {
583 info->live = true;
584 }
585 } else {
586 /* Can't be live if no associated s_reg */
587 DCHECK(info->is_temp);
588 info->live = false;
589 }
590 info->s_reg = s_reg;
591}
592
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700593void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700595 tempreg_info_.Insert(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 info->is_temp = true;
597}
598
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700599void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700601 tempreg_info_.Delete(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 info->is_temp = false;
603}
604
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700605void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 RegisterInfo* info_lo = GetRegInfo(low_reg);
607 RegisterInfo* info_hi = GetRegInfo(high_reg);
608 info_lo->pair = info_hi->pair = true;
609 info_lo->partner = high_reg;
610 info_hi->partner = low_reg;
611}
612
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700613void Mir2Lir::MarkClean(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 RegisterInfo* info = GetRegInfo(loc.low_reg);
615 info->dirty = false;
616 if (loc.wide) {
617 info = GetRegInfo(loc.high_reg);
618 info->dirty = false;
619 }
620}
621
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700622void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 if (loc.home) {
624 // If already home, can't be dirty
625 return;
626 }
627 RegisterInfo* info = GetRegInfo(loc.low_reg);
628 info->dirty = true;
629 if (loc.wide) {
630 info = GetRegInfo(loc.high_reg);
631 info->dirty = true;
632 }
633}
634
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700635void Mir2Lir::MarkInUse(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 RegisterInfo* info = GetRegInfo(reg);
637 info->in_use = true;
638}
639
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700640void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 RegisterInfo* new_info = GetRegInfo(new_reg);
642 RegisterInfo* old_info = GetRegInfo(old_reg);
643 // Target temp status must not change
644 bool is_temp = new_info->is_temp;
645 *new_info = *old_info;
646 // Restore target's temp status
647 new_info->is_temp = is_temp;
648 new_info->reg = new_reg;
649}
650
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700651bool Mir2Lir::CheckCorePoolSanity() {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700652 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
653 if (reg_pool_->core_regs[i].pair) {
654 static int my_reg = reg_pool_->core_regs[i].reg;
655 static int my_sreg = reg_pool_->core_regs[i].s_reg;
656 static int partner_reg = reg_pool_->core_regs[i].partner;
657 static RegisterInfo* partner = GetRegInfo(partner_reg);
658 DCHECK(partner != NULL);
659 DCHECK(partner->pair);
660 DCHECK_EQ(my_reg, partner->partner);
661 static int partner_sreg = partner->s_reg;
662 if (my_sreg == INVALID_SREG) {
663 DCHECK_EQ(partner_sreg, INVALID_SREG);
664 } else {
665 int diff = my_sreg - partner_sreg;
666 DCHECK((diff == -1) || (diff == 1));
667 }
668 }
669 if (!reg_pool_->core_regs[i].live) {
670 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
671 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
672 }
673 }
674 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675}
676
677/*
678 * Return an updated location record with current in-register status.
679 * If the value lives in live temps, reflect that fact. No code
680 * is generated. If the live value is part of an older pair,
681 * clobber both low and high.
682 * TUNING: clobbering both is a bit heavy-handed, but the alternative
683 * is a bit complex when dealing with FP regs. Examine code to see
684 * if it's worthwhile trying to be more clever here.
685 */
686
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700687RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 DCHECK(!loc.wide);
689 DCHECK(CheckCorePoolSanity());
690 if (loc.location != kLocPhysReg) {
691 DCHECK((loc.location == kLocDalvikFrame) ||
692 (loc.location == kLocCompilerTemp));
693 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
694 if (info_lo) {
695 if (info_lo->pair) {
696 Clobber(info_lo->reg);
697 Clobber(info_lo->partner);
698 FreeTemp(info_lo->reg);
699 } else {
700 loc.low_reg = info_lo->reg;
701 loc.location = kLocPhysReg;
702 }
703 }
704 }
705
706 return loc;
707}
708
709/* see comments for update_loc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700710RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 DCHECK(loc.wide);
712 DCHECK(CheckCorePoolSanity());
713 if (loc.location != kLocPhysReg) {
714 DCHECK((loc.location == kLocDalvikFrame) ||
715 (loc.location == kLocCompilerTemp));
716 // Are the dalvik regs already live in physical registers?
717 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
718 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
719 bool match = true;
720 match = match && (info_lo != NULL);
721 match = match && (info_hi != NULL);
722 // Are they both core or both FP?
723 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
724 // If a pair of floating point singles, are they properly aligned?
725 if (match && IsFpReg(info_lo->reg)) {
726 match &= ((info_lo->reg & 0x1) == 0);
727 match &= ((info_hi->reg - info_lo->reg) == 1);
728 }
729 // If previously used as a pair, it is the same pair?
730 if (match && (info_lo->pair || info_hi->pair)) {
731 match = (info_lo->pair == info_hi->pair);
732 match &= ((info_lo->reg == info_hi->partner) &&
733 (info_hi->reg == info_lo->partner));
734 }
735 if (match) {
736 // Can reuse - update the register usage info
737 loc.low_reg = info_lo->reg;
738 loc.high_reg = info_hi->reg;
739 loc.location = kLocPhysReg;
740 MarkPair(loc.low_reg, loc.high_reg);
741 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
742 return loc;
743 }
744 // Can't easily reuse - clobber and free any overlaps
745 if (info_lo) {
746 Clobber(info_lo->reg);
747 FreeTemp(info_lo->reg);
748 if (info_lo->pair)
749 Clobber(info_lo->partner);
750 }
751 if (info_hi) {
752 Clobber(info_hi->reg);
753 FreeTemp(info_hi->reg);
754 if (info_hi->pair)
755 Clobber(info_hi->partner);
756 }
757 }
758 return loc;
759}
760
761
762/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700763RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 if (loc.wide)
765 return UpdateLocWide(loc);
766 else
767 return UpdateLoc(loc);
768}
769
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700770RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 DCHECK(loc.wide);
772 int new_regs;
773 int low_reg;
774 int high_reg;
775
776 loc = UpdateLocWide(loc);
777
778 /* If already in registers, we can assume proper form. Right reg class? */
779 if (loc.location == kLocPhysReg) {
780 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
781 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
782 if (!RegClassMatches(reg_class, loc.low_reg)) {
783 /* Wrong register class. Reallocate and copy */
784 new_regs = AllocTypedTempPair(loc.fp, reg_class);
785 low_reg = new_regs & 0xff;
786 high_reg = (new_regs >> 8) & 0xff;
787 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
788 CopyRegInfo(low_reg, loc.low_reg);
789 CopyRegInfo(high_reg, loc.high_reg);
790 Clobber(loc.low_reg);
791 Clobber(loc.high_reg);
792 loc.low_reg = low_reg;
793 loc.high_reg = high_reg;
794 MarkPair(loc.low_reg, loc.high_reg);
795 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
796 }
797 return loc;
798 }
799
800 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
801 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
802
803 new_regs = AllocTypedTempPair(loc.fp, reg_class);
804 loc.low_reg = new_regs & 0xff;
805 loc.high_reg = (new_regs >> 8) & 0xff;
806
807 MarkPair(loc.low_reg, loc.high_reg);
808 if (update) {
809 loc.location = kLocPhysReg;
810 MarkLive(loc.low_reg, loc.s_reg_low);
811 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
812 }
813 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
814 return loc;
815}
816
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700817RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 int new_reg;
819
820 if (loc.wide)
821 return EvalLocWide(loc, reg_class, update);
822
823 loc = UpdateLoc(loc);
824
825 if (loc.location == kLocPhysReg) {
826 if (!RegClassMatches(reg_class, loc.low_reg)) {
827 /* Wrong register class. Realloc, copy and transfer ownership */
828 new_reg = AllocTypedTemp(loc.fp, reg_class);
829 OpRegCopy(new_reg, loc.low_reg);
830 CopyRegInfo(new_reg, loc.low_reg);
831 Clobber(loc.low_reg);
832 loc.low_reg = new_reg;
833 }
834 return loc;
835 }
836
837 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
838
839 new_reg = AllocTypedTemp(loc.fp, reg_class);
840 loc.low_reg = new_reg;
841
842 if (update) {
843 loc.location = kLocPhysReg;
844 MarkLive(loc.low_reg, loc.s_reg_low);
845 }
846 return loc;
847}
848
849/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -0700850void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
852 RegLocation loc = mir_graph_->reg_location_[i];
853 RefCounts* counts = loc.fp ? fp_counts : core_counts;
854 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -0700855 if (loc.fp) {
856 if (loc.wide) {
857 // Treat doubles as a unit, using upper half of fp_counts array.
858 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
859 i++;
860 } else {
861 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
862 }
863 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
865 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 }
867}
868
869/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700870static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
872 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700873 // Note that we fall back to sorting on reg so we get stable output
874 // on differing qsort implementations (such as on host and target or
875 // between local host and build servers).
876 return (op1->count == op2->count)
877 ? (op1->s_reg - op2->s_reg)
878 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879}
880
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700881void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 LOG(INFO) << msg;
883 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700884 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
885 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
886 } else {
887 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
888 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700889 }
890}
891
892/*
893 * Note: some portions of this code required even if the kPromoteRegs
894 * optimization is disabled.
895 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700896void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897 int reg_bias = cu_->num_compiler_temps + 1;
898 int dalvik_regs = cu_->num_dalvik_registers;
899 int num_regs = dalvik_regs + reg_bias;
900 const int promotion_threshold = 1;
901
902 // Allow target code to add any special registers
903 AdjustSpillMask();
904
905 /*
906 * Simple register promotion. Just do a static count of the uses
907 * of Dalvik registers. Note that we examine the SSA names, but
908 * count based on original Dalvik register name. Count refs
909 * separately based on type in order to give allocation
910 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -0700911 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 * reg.
913 * TUNING: replace with linear scan once we have the ability
914 * to describe register live ranges for GC.
915 */
916 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700917 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
918 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -0700920 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700921 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 // Set ssa names for original Dalvik registers
923 for (int i = 0; i < dalvik_regs; i++) {
924 core_regs[i].s_reg = FpRegs[i].s_reg = i;
925 }
926 // Set ssa name for Method*
927 core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg();
buzbeec729a6b2013-09-14 16:04:31 -0700928 FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy.
929 FpRegs[dalvik_regs + num_regs].s_reg = mir_graph_->GetMethodSReg(); // for consistency.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 // Set ssa names for compiler_temps
931 for (int i = 1; i <= cu_->num_compiler_temps; i++) {
932 CompilerTemp* ct = mir_graph_->compiler_temps_.Get(i);
933 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
934 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
buzbeec729a6b2013-09-14 16:04:31 -0700935 FpRegs[num_regs + dalvik_regs + i].s_reg = ct->s_reg;
936 }
937
938 // Duplicate in upper half to represent possible fp double starting sregs.
939 for (int i = 0; i < num_regs; i++) {
940 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700941 }
942
943 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -0700944 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700945
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946
947 // Sort the count arrays
948 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -0700949 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950
951 if (cu_->verbose) {
952 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -0700953 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 }
955
956 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
957 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -0700958 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
959 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
960 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
961 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
962 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
963 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
964 // Ignore result - if can't alloc double may still be able to alloc singles.
965 AllocPreservedDouble(low_sreg);
966 }
967 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
968 int reg = AllocPreservedSingle(FpRegs[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969 if (reg < 0) {
buzbeec729a6b2013-09-14 16:04:31 -0700970 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 }
972 }
973 }
974
975 // Promote core regs
976 for (int i = 0; (i < num_regs) &&
977 (core_regs[i].count >= promotion_threshold); i++) {
978 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
979 if (promotion_map_[p_map_idx].core_location !=
980 kLocPhysReg) {
981 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
982 if (reg < 0) {
983 break; // No more left
984 }
985 }
986 }
987 }
988
989 // Now, update SSA names to new home locations
990 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
991 RegLocation *curr = &mir_graph_->reg_location_[i];
992 int p_map_idx = SRegToPMap(curr->s_reg_low);
993 if (!curr->wide) {
994 if (curr->fp) {
995 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
996 curr->location = kLocPhysReg;
997 curr->low_reg = promotion_map_[p_map_idx].FpReg;
998 curr->home = true;
999 }
1000 } else {
1001 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1002 curr->location = kLocPhysReg;
1003 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1004 curr->home = true;
1005 }
1006 }
1007 curr->high_reg = INVALID_REG;
1008 } else {
1009 if (curr->high_word) {
1010 continue;
1011 }
1012 if (curr->fp) {
1013 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1014 (promotion_map_[p_map_idx+1].fp_location ==
1015 kLocPhysReg)) {
1016 int low_reg = promotion_map_[p_map_idx].FpReg;
1017 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1018 // Doubles require pair of singles starting at even reg
1019 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1020 curr->location = kLocPhysReg;
1021 curr->low_reg = low_reg;
1022 curr->high_reg = high_reg;
1023 curr->home = true;
1024 }
1025 }
1026 } else {
1027 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1028 && (promotion_map_[p_map_idx+1].core_location ==
1029 kLocPhysReg)) {
1030 curr->location = kLocPhysReg;
1031 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1032 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1033 curr->home = true;
1034 }
1035 }
1036 }
1037 }
1038 if (cu_->verbose) {
1039 DumpPromotionMap();
1040 }
1041}
1042
1043/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001044int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1046 fp_spill_mask_, frame_size_, v_reg);
1047}
1048
1049/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001050int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1052}
1053
1054/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001055RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 RegLocation gpr_res = LocCReturnWide();
1057 RegLocation fpr_res = LocCReturnDouble();
1058 RegLocation res = is_double ? fpr_res : gpr_res;
1059 Clobber(res.low_reg);
1060 Clobber(res.high_reg);
1061 LockTemp(res.low_reg);
1062 LockTemp(res.high_reg);
1063 MarkPair(res.low_reg, res.high_reg);
1064 return res;
1065}
1066
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001067RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 RegLocation gpr_res = LocCReturn();
1069 RegLocation fpr_res = LocCReturnFloat();
1070 RegLocation res = is_float ? fpr_res : gpr_res;
1071 Clobber(res.low_reg);
1072 if (cu_->instruction_set == kMips) {
1073 MarkInUse(res.low_reg);
1074 } else {
1075 LockTemp(res.low_reg);
1076 }
1077 return res;
1078}
1079
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001080void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 DoPromotion();
1082
1083 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1084 LOG(INFO) << "After Promotion";
1085 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1086 }
1087
1088 /* Set the frame size */
1089 frame_size_ = ComputeFrameSize();
1090}
1091
1092/*
1093 * Get the "real" sreg number associated with an s_reg slot. In general,
1094 * s_reg values passed through codegen are the SSA names created by
1095 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1096 * array. However, renaming is accomplished by simply replacing RegLocation
1097 * entries in the reglocation[] array. Therefore, when location
1098 * records for operands are first created, we need to ask the locRecord
1099 * identified by the dataflow pass what it's new name is.
1100 */
1101int Mir2Lir::GetSRegHi(int lowSreg) {
1102 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1103}
1104
1105bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001106 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 return true;
1108}
1109
1110int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1111 DCHECK_GT(mir->ssa_rep->num_uses, num);
1112 return mir->ssa_rep->uses[num];
1113}
1114
1115} // namespace art