blob: 6d6951206b51d3d27cfb4d0a12a6b5b1ac946eaa [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains register alloction support. */
18
19#include "dex/compiler_ir.h"
20#include "dex/compiler_internals.h"
21#include "mir_to_lir-inl.h"
22
23namespace art {
24
25/*
26 * Free all allocated temps in the temp pools. Note that this does
27 * not affect the "liveness" of a temp register, which will stay
28 * live until it is either explicitly killed or reallocated.
29 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030void Mir2Lir::ResetRegPool() {
buzbeebd663de2013-09-10 15:41:31 -070031 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
32 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
33 info->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -070034 }
35 // Reset temp tracking sanity check.
36 if (kIsDebugBuild) {
37 live_sreg_ = INVALID_SREG;
38 }
39}
40
41 /*
42 * Set up temp & preserved register pools specialized by target.
43 * Note: num_regs may be zero.
44 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070045void Mir2Lir::CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070046 for (int i = 0; i < num; i++) {
buzbeebd663de2013-09-10 15:41:31 -070047 uint32_t reg_number = reg_nums[i];
48 regs[i].reg = reg_number;
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 regs[i].in_use = false;
50 regs[i].is_temp = false;
51 regs[i].pair = false;
52 regs[i].live = false;
53 regs[i].dirty = false;
54 regs[i].s_reg = INVALID_SREG;
buzbeebd663de2013-09-10 15:41:31 -070055 size_t map_size = reginfo_map_.Size();
56 if (reg_number >= map_size) {
57 for (uint32_t i = 0; i < ((reg_number - map_size) + 1); i++) {
58 reginfo_map_.Insert(NULL);
59 }
60 }
61 reginfo_map_.Put(reg_number, &regs[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 }
63}
64
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070065void Mir2Lir::DumpRegPool(RegisterInfo* p, int num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 LOG(INFO) << "================================================";
67 for (int i = 0; i < num_regs; i++) {
68 LOG(INFO) << StringPrintf(
buzbee0d829482013-10-11 15:24:55 -070069 "R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d",
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 p[i].reg, p[i].is_temp, p[i].in_use, p[i].pair, p[i].partner,
buzbee0d829482013-10-11 15:24:55 -070071 p[i].live, p[i].dirty, p[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 }
73 LOG(INFO) << "================================================";
74}
75
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070076void Mir2Lir::DumpCoreRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 DumpRegPool(reg_pool_->core_regs, reg_pool_->num_core_regs);
78}
79
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070080void Mir2Lir::DumpFpRegPool() {
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 DumpRegPool(reg_pool_->FPRegs, reg_pool_->num_fp_regs);
82}
83
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070084void Mir2Lir::ClobberSRegBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -070085 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 if (p[i].s_reg == s_reg) {
87 if (p[i].is_temp) {
88 p[i].live = false;
89 }
90 p[i].def_start = NULL;
91 p[i].def_end = NULL;
92 }
93 }
94}
95
96/*
97 * Break the association between a Dalvik vreg and a physical temp register of either register
98 * class.
99 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
100 * in the register utilities, is is also used by code gen routines to work around a deficiency in
101 * local register allocation, which fails to distinguish between the "in" and "out" identities
102 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
103 * is used both as the source and destination register of an operation in which the type
104 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
105 * addressed.
106 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700107void Mir2Lir::ClobberSReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 /* Reset live temp tracking sanity checker */
109 if (kIsDebugBuild) {
110 if (s_reg == live_sreg_) {
111 live_sreg_ = INVALID_SREG;
112 }
113 }
114 ClobberSRegBody(reg_pool_->core_regs, reg_pool_->num_core_regs, s_reg);
115 ClobberSRegBody(reg_pool_->FPRegs, reg_pool_->num_fp_regs, s_reg);
116}
117
118/*
119 * SSA names associated with the initial definitions of Dalvik
120 * registers are the same as the Dalvik register number (and
121 * thus take the same position in the promotion_map. However,
122 * the special Method* and compiler temp resisters use negative
123 * v_reg numbers to distinguish them and can have an arbitrary
124 * ssa name (above the last original Dalvik register). This function
125 * maps SSA names to positions in the promotion_map array.
126 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700127int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
129 DCHECK_GE(s_reg, 0);
130 int v_reg = mir_graph_->SRegToVReg(s_reg);
131 if (v_reg >= 0) {
132 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
133 return v_reg;
134 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800135 /*
136 * It must be the case that the v_reg for temporary is less than or equal to the
137 * base reg for temps. For that reason, "position" must be zero or positive.
138 */
139 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
140
141 // The temporaries are placed after dalvik registers in the promotion map
142 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
143 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 }
145}
146
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700147void Mir2Lir::RecordCorePromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 int p_map_idx = SRegToPMap(s_reg);
149 int v_reg = mir_graph_->SRegToVReg(s_reg);
150 GetRegInfo(reg)->in_use = true;
151 core_spill_mask_ |= (1 << reg);
152 // Include reg for later sort
153 core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
154 num_core_spills_++;
155 promotion_map_[p_map_idx].core_location = kLocPhysReg;
156 promotion_map_[p_map_idx].core_reg = reg;
157}
158
159/* Reserve a callee-save register. Return -1 if none available */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160int Mir2Lir::AllocPreservedCoreReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 int res = -1;
162 RegisterInfo* core_regs = reg_pool_->core_regs;
163 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
164 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
165 res = core_regs[i].reg;
166 RecordCorePromotion(res, s_reg);
167 break;
168 }
169 }
170 return res;
171}
172
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173void Mir2Lir::RecordFpPromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 int p_map_idx = SRegToPMap(s_reg);
175 int v_reg = mir_graph_->SRegToVReg(s_reg);
176 GetRegInfo(reg)->in_use = true;
177 MarkPreservedSingle(v_reg, reg);
178 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
179 promotion_map_[p_map_idx].FpReg = reg;
180}
181
buzbeec729a6b2013-09-14 16:04:31 -0700182// Reserve a callee-save fp single register.
183int Mir2Lir::AllocPreservedSingle(int s_reg) {
184 int res = -1; // Return code if none available.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 RegisterInfo* FPRegs = reg_pool_->FPRegs;
186 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700187 if (!FPRegs[i].is_temp && !FPRegs[i].in_use) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 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
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700254 bool required) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700256 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 if (next >= num_regs)
258 next = 0;
259 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
260 Clobber(p[next].reg);
261 p[next].in_use = true;
262 p[next].pair = false;
263 *next_temp = next + 1;
264 return p[next].reg;
265 }
266 next++;
267 }
268 next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700269 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 if (next >= num_regs)
271 next = 0;
272 if (p[next].is_temp && !p[next].in_use) {
273 Clobber(p[next].reg);
274 p[next].in_use = true;
275 p[next].pair = false;
276 *next_temp = next + 1;
277 return p[next].reg;
278 }
279 next++;
280 }
281 if (required) {
282 CodegenDump();
283 DumpRegPool(reg_pool_->core_regs,
284 reg_pool_->num_core_regs);
285 LOG(FATAL) << "No free temp registers";
286 }
287 return -1; // No register available
288}
289
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700290// REDO: too many assumptions.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700291int Mir2Lir::AllocTempDouble() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 RegisterInfo* p = reg_pool_->FPRegs;
293 int num_regs = reg_pool_->num_fp_regs;
294 /* Start looking at an even reg */
295 int next = reg_pool_->next_fp_reg & ~0x1;
296
297 // First try to avoid allocating live registers
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700298 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 if (next >= num_regs)
300 next = 0;
301 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
302 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
303 Clobber(p[next].reg);
304 Clobber(p[next+1].reg);
305 p[next].in_use = true;
306 p[next+1].in_use = true;
307 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
308 DCHECK_EQ((p[next].reg & 0x1), 0);
309 reg_pool_->next_fp_reg = next + 2;
310 if (reg_pool_->next_fp_reg >= num_regs) {
311 reg_pool_->next_fp_reg = 0;
312 }
313 return p[next].reg;
314 }
315 next += 2;
316 }
317 next = reg_pool_->next_fp_reg & ~0x1;
318
319 // No choice - find a pair and kill it.
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700320 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 if (next >= num_regs)
322 next = 0;
323 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
324 !p[next+1].in_use) {
325 Clobber(p[next].reg);
326 Clobber(p[next+1].reg);
327 p[next].in_use = true;
328 p[next+1].in_use = true;
329 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
330 DCHECK_EQ((p[next].reg & 0x1), 0);
331 reg_pool_->next_fp_reg = next + 2;
332 if (reg_pool_->next_fp_reg >= num_regs) {
333 reg_pool_->next_fp_reg = 0;
334 }
335 return p[next].reg;
336 }
337 next += 2;
338 }
339 LOG(FATAL) << "No free temp registers (pair)";
340 return -1;
341}
342
343/* Return a temp if one is available, -1 otherwise */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700344int Mir2Lir::AllocFreeTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 return AllocTempBody(reg_pool_->core_regs,
346 reg_pool_->num_core_regs,
Vladimir Marko73e08b32013-11-21 10:58:36 +0000347 &reg_pool_->next_core_reg, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348}
349
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700350int Mir2Lir::AllocTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 return AllocTempBody(reg_pool_->core_regs,
352 reg_pool_->num_core_regs,
353 &reg_pool_->next_core_reg, true);
354}
355
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700356int Mir2Lir::AllocTempFloat() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 return AllocTempBody(reg_pool_->FPRegs,
358 reg_pool_->num_fp_regs,
359 &reg_pool_->next_fp_reg, true);
360}
361
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700362Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 if (s_reg == -1)
364 return NULL;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700365 for (int i = 0; i < num_regs; i++) {
buzbee56c71782013-09-05 17:13:19 -0700366 if ((p[i].s_reg == s_reg) && p[i].live) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 if (p[i].is_temp)
368 p[i].in_use = true;
369 return &p[i];
370 }
371 }
372 return NULL;
373}
374
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700375Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 RegisterInfo* res = NULL;
377 switch (reg_class) {
378 case kAnyReg:
379 res = AllocLiveBody(reg_pool_->FPRegs,
380 reg_pool_->num_fp_regs, s_reg);
381 if (res)
382 break;
383 /* Intentional fallthrough */
384 case kCoreReg:
385 res = AllocLiveBody(reg_pool_->core_regs,
386 reg_pool_->num_core_regs, s_reg);
387 break;
388 case kFPReg:
389 res = AllocLiveBody(reg_pool_->FPRegs,
390 reg_pool_->num_fp_regs, s_reg);
391 break;
392 default:
393 LOG(FATAL) << "Invalid register type";
394 }
395 return res;
396}
397
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700398void Mir2Lir::FreeTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700399 RegisterInfo* p = GetRegInfo(reg);
400 if (p->is_temp) {
401 p->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 }
buzbee56c71782013-09-05 17:13:19 -0700403 p->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404}
405
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700406Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700407 RegisterInfo* p = GetRegInfo(reg);
408 return p->live ? p : NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409}
410
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700411Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 RegisterInfo* p = GetRegInfo(reg);
413 return (p->is_temp) ? p : NULL;
414}
415
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700416Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 RegisterInfo* p = GetRegInfo(reg);
418 return (p->is_temp) ? NULL : p;
419}
420
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700421bool Mir2Lir::IsDirty(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 RegisterInfo* p = GetRegInfo(reg);
423 return p->dirty;
424}
425
426/*
427 * Similar to AllocTemp(), but forces the allocation of a specific
428 * register. No check is made to see if the register was previously
429 * allocated. Use with caution.
430 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700431void Mir2Lir::LockTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700432 RegisterInfo* p = GetRegInfo(reg);
433 DCHECK(p->is_temp);
434 p->in_use = true;
435 p->live = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436}
437
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700438void Mir2Lir::ResetDef(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 ResetDefBody(GetRegInfo(reg));
440}
441
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700442void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 if (start && finish) {
444 LIR *p;
445 DCHECK_EQ(s_reg1, s_reg2);
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700446 for (p = start; ; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 NopLIR(p);
448 if (p == finish)
449 break;
450 }
451 }
452}
453
454/*
455 * Mark the beginning and end LIR of a def sequence. Note that
456 * on entry start points to the LIR prior to the beginning of the
457 * sequence.
458 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700459void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 DCHECK(!rl.wide);
461 DCHECK(start && start->next);
462 DCHECK(finish);
463 RegisterInfo* p = GetRegInfo(rl.low_reg);
464 p->def_start = start->next;
465 p->def_end = finish;
466}
467
468/*
469 * Mark the beginning and end LIR of a def sequence. Note that
470 * on entry start points to the LIR prior to the beginning of the
471 * sequence.
472 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700473void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 DCHECK(rl.wide);
475 DCHECK(start && start->next);
476 DCHECK(finish);
477 RegisterInfo* p = GetRegInfo(rl.low_reg);
478 ResetDef(rl.high_reg); // Only track low of pair
479 p->def_start = start->next;
480 p->def_end = finish;
481}
482
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700483RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700484 DCHECK(rl.wide);
485 if (rl.location == kLocPhysReg) {
486 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
487 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
488 if (info_lo->is_temp) {
489 info_lo->pair = false;
490 info_lo->def_start = NULL;
491 info_lo->def_end = NULL;
492 }
493 if (info_hi->is_temp) {
494 info_hi->pair = false;
495 info_hi->def_start = NULL;
496 info_hi->def_end = NULL;
497 }
498 }
499 rl.wide = false;
500 return rl;
501}
502
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700503void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 DCHECK(!rl.wide);
505 RegisterInfo* p = IsTemp(rl.low_reg);
506 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
507 DCHECK(!p->pair);
508 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
509 }
510 ResetDef(rl.low_reg);
511}
512
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700513void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 DCHECK(rl.wide);
515 RegisterInfo* p_low = IsTemp(rl.low_reg);
516 RegisterInfo* p_high = IsTemp(rl.high_reg);
517 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
518 DCHECK(p_low->pair);
519 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
520 }
521 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
522 DCHECK(p_high->pair);
523 }
524 ResetDef(rl.low_reg);
525 ResetDef(rl.high_reg);
526}
527
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700528void Mir2Lir::ResetDefTracking() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700529 for (int i = 0; i< reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 ResetDefBody(&reg_pool_->core_regs[i]);
531 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700532 for (int i = 0; i< reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700533 ResetDefBody(&reg_pool_->FPRegs[i]);
534 }
535}
536
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700537void Mir2Lir::ClobberAllRegs() {
buzbeebd663de2013-09-10 15:41:31 -0700538 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
539 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
540 info->live = false;
541 info->s_reg = INVALID_SREG;
542 info->def_start = NULL;
543 info->def_end = NULL;
544 info->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 }
546}
547
548// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700549void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700550 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 if (info[i].live && info[i].dirty) {
552 if (info[i].pair) {
553 FlushRegWide(info[i].reg, info[i].partner);
554 } else {
555 FlushReg(info[i].reg);
556 }
557 }
558 }
559}
560
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700561void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 FlushAllRegsBody(reg_pool_->core_regs,
563 reg_pool_->num_core_regs);
564 FlushAllRegsBody(reg_pool_->FPRegs,
565 reg_pool_->num_fp_regs);
566 ClobberAllRegs();
567}
568
569
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700570// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700571bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 if (reg_class == kAnyReg) {
573 return true;
574 } else if (reg_class == kCoreReg) {
575 return !IsFpReg(reg);
576 } else {
577 return IsFpReg(reg);
578 }
579}
580
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700581void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 RegisterInfo* info = GetRegInfo(reg);
583 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
584 return; /* already live */
585 } else if (s_reg != INVALID_SREG) {
586 ClobberSReg(s_reg);
587 if (info->is_temp) {
588 info->live = true;
589 }
590 } else {
591 /* Can't be live if no associated s_reg */
592 DCHECK(info->is_temp);
593 info->live = false;
594 }
595 info->s_reg = s_reg;
596}
597
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700598void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700600 tempreg_info_.Insert(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 info->is_temp = true;
602}
603
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700604void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700606 tempreg_info_.Delete(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 info->is_temp = false;
608}
609
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700610void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000611 DCHECK_NE(low_reg, high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 RegisterInfo* info_lo = GetRegInfo(low_reg);
613 RegisterInfo* info_hi = GetRegInfo(high_reg);
614 info_lo->pair = info_hi->pair = true;
615 info_lo->partner = high_reg;
616 info_hi->partner = low_reg;
617}
618
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700619void Mir2Lir::MarkClean(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 RegisterInfo* info = GetRegInfo(loc.low_reg);
621 info->dirty = false;
622 if (loc.wide) {
623 info = GetRegInfo(loc.high_reg);
624 info->dirty = false;
625 }
626}
627
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700628void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 if (loc.home) {
630 // If already home, can't be dirty
631 return;
632 }
633 RegisterInfo* info = GetRegInfo(loc.low_reg);
634 info->dirty = true;
635 if (loc.wide) {
636 info = GetRegInfo(loc.high_reg);
637 info->dirty = true;
638 }
639}
640
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700641void Mir2Lir::MarkInUse(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 RegisterInfo* info = GetRegInfo(reg);
643 info->in_use = true;
644}
645
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700646void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 RegisterInfo* new_info = GetRegInfo(new_reg);
648 RegisterInfo* old_info = GetRegInfo(old_reg);
649 // Target temp status must not change
650 bool is_temp = new_info->is_temp;
651 *new_info = *old_info;
652 // Restore target's temp status
653 new_info->is_temp = is_temp;
654 new_info->reg = new_reg;
655}
656
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700657bool Mir2Lir::CheckCorePoolSanity() {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700658 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
659 if (reg_pool_->core_regs[i].pair) {
660 static int my_reg = reg_pool_->core_regs[i].reg;
661 static int my_sreg = reg_pool_->core_regs[i].s_reg;
662 static int partner_reg = reg_pool_->core_regs[i].partner;
663 static RegisterInfo* partner = GetRegInfo(partner_reg);
664 DCHECK(partner != NULL);
665 DCHECK(partner->pair);
666 DCHECK_EQ(my_reg, partner->partner);
667 static int partner_sreg = partner->s_reg;
668 if (my_sreg == INVALID_SREG) {
669 DCHECK_EQ(partner_sreg, INVALID_SREG);
670 } else {
671 int diff = my_sreg - partner_sreg;
672 DCHECK((diff == -1) || (diff == 1));
673 }
674 }
675 if (!reg_pool_->core_regs[i].live) {
676 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
677 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
678 }
679 }
680 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681}
682
683/*
684 * Return an updated location record with current in-register status.
685 * If the value lives in live temps, reflect that fact. No code
686 * is generated. If the live value is part of an older pair,
687 * clobber both low and high.
688 * TUNING: clobbering both is a bit heavy-handed, but the alternative
689 * is a bit complex when dealing with FP regs. Examine code to see
690 * if it's worthwhile trying to be more clever here.
691 */
692
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700693RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 DCHECK(!loc.wide);
695 DCHECK(CheckCorePoolSanity());
696 if (loc.location != kLocPhysReg) {
697 DCHECK((loc.location == kLocDalvikFrame) ||
698 (loc.location == kLocCompilerTemp));
699 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
700 if (info_lo) {
701 if (info_lo->pair) {
702 Clobber(info_lo->reg);
703 Clobber(info_lo->partner);
704 FreeTemp(info_lo->reg);
705 } else {
706 loc.low_reg = info_lo->reg;
707 loc.location = kLocPhysReg;
708 }
709 }
710 }
711
712 return loc;
713}
714
715/* see comments for update_loc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700716RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 DCHECK(loc.wide);
718 DCHECK(CheckCorePoolSanity());
719 if (loc.location != kLocPhysReg) {
720 DCHECK((loc.location == kLocDalvikFrame) ||
721 (loc.location == kLocCompilerTemp));
722 // Are the dalvik regs already live in physical registers?
723 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
724 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
725 bool match = true;
726 match = match && (info_lo != NULL);
727 match = match && (info_hi != NULL);
728 // Are they both core or both FP?
729 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
730 // If a pair of floating point singles, are they properly aligned?
731 if (match && IsFpReg(info_lo->reg)) {
732 match &= ((info_lo->reg & 0x1) == 0);
733 match &= ((info_hi->reg - info_lo->reg) == 1);
734 }
735 // If previously used as a pair, it is the same pair?
736 if (match && (info_lo->pair || info_hi->pair)) {
737 match = (info_lo->pair == info_hi->pair);
738 match &= ((info_lo->reg == info_hi->partner) &&
739 (info_hi->reg == info_lo->partner));
740 }
741 if (match) {
742 // Can reuse - update the register usage info
743 loc.low_reg = info_lo->reg;
744 loc.high_reg = info_hi->reg;
745 loc.location = kLocPhysReg;
746 MarkPair(loc.low_reg, loc.high_reg);
747 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
748 return loc;
749 }
750 // Can't easily reuse - clobber and free any overlaps
751 if (info_lo) {
752 Clobber(info_lo->reg);
753 FreeTemp(info_lo->reg);
754 if (info_lo->pair)
755 Clobber(info_lo->partner);
756 }
757 if (info_hi) {
758 Clobber(info_hi->reg);
759 FreeTemp(info_hi->reg);
760 if (info_hi->pair)
761 Clobber(info_hi->partner);
762 }
763 }
764 return loc;
765}
766
767
768/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700769RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 if (loc.wide)
771 return UpdateLocWide(loc);
772 else
773 return UpdateLoc(loc);
774}
775
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700776RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700777 DCHECK(loc.wide);
buzbee0d829482013-10-11 15:24:55 -0700778 int32_t new_regs;
779 int32_t low_reg;
780 int32_t high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781
782 loc = UpdateLocWide(loc);
783
784 /* If already in registers, we can assume proper form. Right reg class? */
785 if (loc.location == kLocPhysReg) {
786 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
787 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
788 if (!RegClassMatches(reg_class, loc.low_reg)) {
789 /* Wrong register class. Reallocate and copy */
790 new_regs = AllocTypedTempPair(loc.fp, reg_class);
791 low_reg = new_regs & 0xff;
792 high_reg = (new_regs >> 8) & 0xff;
793 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
794 CopyRegInfo(low_reg, loc.low_reg);
795 CopyRegInfo(high_reg, loc.high_reg);
796 Clobber(loc.low_reg);
797 Clobber(loc.high_reg);
798 loc.low_reg = low_reg;
799 loc.high_reg = high_reg;
800 MarkPair(loc.low_reg, loc.high_reg);
801 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
802 }
803 return loc;
804 }
805
806 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
807 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
808
809 new_regs = AllocTypedTempPair(loc.fp, reg_class);
810 loc.low_reg = new_regs & 0xff;
811 loc.high_reg = (new_regs >> 8) & 0xff;
812
813 MarkPair(loc.low_reg, loc.high_reg);
814 if (update) {
815 loc.location = kLocPhysReg;
816 MarkLive(loc.low_reg, loc.s_reg_low);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000817 // Does this wide value live in two registers or one vector register?
818 if (loc.low_reg != loc.high_reg) {
819 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
820 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 }
822 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
823 return loc;
824}
825
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700826RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 int new_reg;
828
829 if (loc.wide)
830 return EvalLocWide(loc, reg_class, update);
831
832 loc = UpdateLoc(loc);
833
834 if (loc.location == kLocPhysReg) {
835 if (!RegClassMatches(reg_class, loc.low_reg)) {
836 /* Wrong register class. Realloc, copy and transfer ownership */
837 new_reg = AllocTypedTemp(loc.fp, reg_class);
838 OpRegCopy(new_reg, loc.low_reg);
839 CopyRegInfo(new_reg, loc.low_reg);
840 Clobber(loc.low_reg);
841 loc.low_reg = new_reg;
842 }
843 return loc;
844 }
845
846 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
847
848 new_reg = AllocTypedTemp(loc.fp, reg_class);
849 loc.low_reg = new_reg;
850
851 if (update) {
852 loc.location = kLocPhysReg;
853 MarkLive(loc.low_reg, loc.s_reg_low);
854 }
855 return loc;
856}
857
858/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -0700859void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
861 RegLocation loc = mir_graph_->reg_location_[i];
862 RefCounts* counts = loc.fp ? fp_counts : core_counts;
863 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -0700864 if (loc.fp) {
865 if (loc.wide) {
866 // Treat doubles as a unit, using upper half of fp_counts array.
867 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
868 i++;
869 } else {
870 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
871 }
872 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
874 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 }
876}
877
878/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700879static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
881 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700882 // Note that we fall back to sorting on reg so we get stable output
883 // on differing qsort implementations (such as on host and target or
884 // between local host and build servers).
885 return (op1->count == op2->count)
886 ? (op1->s_reg - op2->s_reg)
887 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700888}
889
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700890void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 LOG(INFO) << msg;
892 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700893 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
894 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
895 } else {
896 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
897 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 }
899}
900
901/*
902 * Note: some portions of this code required even if the kPromoteRegs
903 * optimization is disabled.
904 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700905void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800907 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 const int promotion_threshold = 1;
909
910 // Allow target code to add any special registers
911 AdjustSpillMask();
912
913 /*
914 * Simple register promotion. Just do a static count of the uses
915 * of Dalvik registers. Note that we examine the SSA names, but
916 * count based on original Dalvik register name. Count refs
917 * separately based on type in order to give allocation
918 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -0700919 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 * reg.
921 * TUNING: replace with linear scan once we have the ability
922 * to describe register live ranges for GC.
923 */
924 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700925 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
926 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -0700928 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700929 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700930 // Set ssa names for original Dalvik registers
931 for (int i = 0; i < dalvik_regs; i++) {
932 core_regs[i].s_reg = FpRegs[i].s_reg = i;
933 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800934
935 // Set ssa names for compiler temporaries
936 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
937 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
938 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
939 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
940 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -0700941 }
942
943 // Duplicate in upper half to represent possible fp double starting sregs.
944 for (int i = 0; i < num_regs; i++) {
945 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 }
947
948 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -0700949 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950
Brian Carlstrom7940e442013-07-12 13:46:57 -0700951
952 // Sort the count arrays
953 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -0700954 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955
956 if (cu_->verbose) {
957 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -0700958 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700959 }
960
961 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
962 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -0700963 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
964 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
965 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
966 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
967 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
968 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
969 // Ignore result - if can't alloc double may still be able to alloc singles.
970 AllocPreservedDouble(low_sreg);
971 }
972 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
973 int reg = AllocPreservedSingle(FpRegs[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 if (reg < 0) {
buzbeec729a6b2013-09-14 16:04:31 -0700975 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976 }
977 }
978 }
979
980 // Promote core regs
981 for (int i = 0; (i < num_regs) &&
982 (core_regs[i].count >= promotion_threshold); i++) {
983 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
984 if (promotion_map_[p_map_idx].core_location !=
985 kLocPhysReg) {
986 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
987 if (reg < 0) {
988 break; // No more left
989 }
990 }
991 }
992 }
993
994 // Now, update SSA names to new home locations
995 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
996 RegLocation *curr = &mir_graph_->reg_location_[i];
997 int p_map_idx = SRegToPMap(curr->s_reg_low);
998 if (!curr->wide) {
999 if (curr->fp) {
1000 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1001 curr->location = kLocPhysReg;
1002 curr->low_reg = promotion_map_[p_map_idx].FpReg;
1003 curr->home = true;
1004 }
1005 } else {
1006 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1007 curr->location = kLocPhysReg;
1008 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1009 curr->home = true;
1010 }
1011 }
1012 curr->high_reg = INVALID_REG;
1013 } else {
1014 if (curr->high_word) {
1015 continue;
1016 }
1017 if (curr->fp) {
1018 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1019 (promotion_map_[p_map_idx+1].fp_location ==
1020 kLocPhysReg)) {
1021 int low_reg = promotion_map_[p_map_idx].FpReg;
1022 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1023 // Doubles require pair of singles starting at even reg
1024 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1025 curr->location = kLocPhysReg;
1026 curr->low_reg = low_reg;
1027 curr->high_reg = high_reg;
1028 curr->home = true;
1029 }
1030 }
1031 } else {
1032 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1033 && (promotion_map_[p_map_idx+1].core_location ==
1034 kLocPhysReg)) {
1035 curr->location = kLocPhysReg;
1036 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1037 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1038 curr->home = true;
1039 }
1040 }
1041 }
1042 }
1043 if (cu_->verbose) {
1044 DumpPromotionMap();
1045 }
1046}
1047
1048/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001049int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1051 fp_spill_mask_, frame_size_, v_reg);
1052}
1053
1054/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001055int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1057}
1058
1059/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001060RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 RegLocation gpr_res = LocCReturnWide();
1062 RegLocation fpr_res = LocCReturnDouble();
1063 RegLocation res = is_double ? fpr_res : gpr_res;
1064 Clobber(res.low_reg);
1065 Clobber(res.high_reg);
1066 LockTemp(res.low_reg);
1067 LockTemp(res.high_reg);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001068 // Does this wide value live in two registers or one vector register?
1069 if (res.low_reg != res.high_reg) {
1070 MarkPair(res.low_reg, res.high_reg);
1071 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 return res;
1073}
1074
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001075RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 RegLocation gpr_res = LocCReturn();
1077 RegLocation fpr_res = LocCReturnFloat();
1078 RegLocation res = is_float ? fpr_res : gpr_res;
1079 Clobber(res.low_reg);
1080 if (cu_->instruction_set == kMips) {
1081 MarkInUse(res.low_reg);
1082 } else {
1083 LockTemp(res.low_reg);
1084 }
1085 return res;
1086}
1087
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001088void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 DoPromotion();
1090
1091 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1092 LOG(INFO) << "After Promotion";
1093 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1094 }
1095
1096 /* Set the frame size */
1097 frame_size_ = ComputeFrameSize();
1098}
1099
1100/*
1101 * Get the "real" sreg number associated with an s_reg slot. In general,
1102 * s_reg values passed through codegen are the SSA names created by
1103 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1104 * array. However, renaming is accomplished by simply replacing RegLocation
1105 * entries in the reglocation[] array. Therefore, when location
1106 * records for operands are first created, we need to ask the locRecord
1107 * identified by the dataflow pass what it's new name is.
1108 */
1109int Mir2Lir::GetSRegHi(int lowSreg) {
1110 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1111}
1112
1113bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001114 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 return true;
1116}
1117
1118int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1119 DCHECK_GT(mir->ssa_rep->num_uses, num);
1120 return mir->ssa_rep->uses[num];
1121}
1122
1123} // namespace art