blob: 0a651713abc190c127d71a074fd9df89018ec99c [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
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800548void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
549 if (info->pair) {
550 FlushRegWide(info->reg, info->partner);
551 } else {
552 FlushReg(info->reg);
553 }
554}
555
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700557void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700558 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 if (info[i].live && info[i].dirty) {
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800560 FlushSpecificReg(&info[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 }
562 }
563}
564
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700565void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 FlushAllRegsBody(reg_pool_->core_regs,
567 reg_pool_->num_core_regs);
568 FlushAllRegsBody(reg_pool_->FPRegs,
569 reg_pool_->num_fp_regs);
570 ClobberAllRegs();
571}
572
573
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700574// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700575bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 if (reg_class == kAnyReg) {
577 return true;
578 } else if (reg_class == kCoreReg) {
579 return !IsFpReg(reg);
580 } else {
581 return IsFpReg(reg);
582 }
583}
584
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700585void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 RegisterInfo* info = GetRegInfo(reg);
587 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
588 return; /* already live */
589 } else if (s_reg != INVALID_SREG) {
590 ClobberSReg(s_reg);
591 if (info->is_temp) {
592 info->live = true;
593 }
594 } else {
595 /* Can't be live if no associated s_reg */
596 DCHECK(info->is_temp);
597 info->live = false;
598 }
599 info->s_reg = s_reg;
600}
601
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700602void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700604 tempreg_info_.Insert(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 info->is_temp = true;
606}
607
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700608void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700610 tempreg_info_.Delete(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 info->is_temp = false;
612}
613
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700614void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000615 DCHECK_NE(low_reg, high_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 RegisterInfo* info_lo = GetRegInfo(low_reg);
617 RegisterInfo* info_hi = GetRegInfo(high_reg);
618 info_lo->pair = info_hi->pair = true;
619 info_lo->partner = high_reg;
620 info_hi->partner = low_reg;
621}
622
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700623void Mir2Lir::MarkClean(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 RegisterInfo* info = GetRegInfo(loc.low_reg);
625 info->dirty = false;
626 if (loc.wide) {
627 info = GetRegInfo(loc.high_reg);
628 info->dirty = false;
629 }
630}
631
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700632void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 if (loc.home) {
634 // If already home, can't be dirty
635 return;
636 }
637 RegisterInfo* info = GetRegInfo(loc.low_reg);
638 info->dirty = true;
639 if (loc.wide) {
640 info = GetRegInfo(loc.high_reg);
641 info->dirty = true;
642 }
643}
644
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700645void Mir2Lir::MarkInUse(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 RegisterInfo* info = GetRegInfo(reg);
647 info->in_use = true;
648}
649
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700650void Mir2Lir::CopyRegInfo(int new_reg, int old_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 RegisterInfo* new_info = GetRegInfo(new_reg);
652 RegisterInfo* old_info = GetRegInfo(old_reg);
653 // Target temp status must not change
654 bool is_temp = new_info->is_temp;
655 *new_info = *old_info;
656 // Restore target's temp status
657 new_info->is_temp = is_temp;
658 new_info->reg = new_reg;
659}
660
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700661bool Mir2Lir::CheckCorePoolSanity() {
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700662 for (static int i = 0; i < reg_pool_->num_core_regs; i++) {
663 if (reg_pool_->core_regs[i].pair) {
664 static int my_reg = reg_pool_->core_regs[i].reg;
665 static int my_sreg = reg_pool_->core_regs[i].s_reg;
666 static int partner_reg = reg_pool_->core_regs[i].partner;
667 static RegisterInfo* partner = GetRegInfo(partner_reg);
668 DCHECK(partner != NULL);
669 DCHECK(partner->pair);
670 DCHECK_EQ(my_reg, partner->partner);
671 static int partner_sreg = partner->s_reg;
672 if (my_sreg == INVALID_SREG) {
673 DCHECK_EQ(partner_sreg, INVALID_SREG);
674 } else {
675 int diff = my_sreg - partner_sreg;
676 DCHECK((diff == -1) || (diff == 1));
677 }
678 }
679 if (!reg_pool_->core_regs[i].live) {
680 DCHECK(reg_pool_->core_regs[i].def_start == NULL);
681 DCHECK(reg_pool_->core_regs[i].def_end == NULL);
682 }
683 }
684 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685}
686
687/*
688 * Return an updated location record with current in-register status.
689 * If the value lives in live temps, reflect that fact. No code
690 * is generated. If the live value is part of an older pair,
691 * clobber both low and high.
692 * TUNING: clobbering both is a bit heavy-handed, but the alternative
693 * is a bit complex when dealing with FP regs. Examine code to see
694 * if it's worthwhile trying to be more clever here.
695 */
696
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700697RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 DCHECK(!loc.wide);
699 DCHECK(CheckCorePoolSanity());
700 if (loc.location != kLocPhysReg) {
701 DCHECK((loc.location == kLocDalvikFrame) ||
702 (loc.location == kLocCompilerTemp));
703 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
704 if (info_lo) {
705 if (info_lo->pair) {
706 Clobber(info_lo->reg);
707 Clobber(info_lo->partner);
708 FreeTemp(info_lo->reg);
709 } else {
710 loc.low_reg = info_lo->reg;
711 loc.location = kLocPhysReg;
712 }
713 }
714 }
715
716 return loc;
717}
718
719/* see comments for update_loc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700720RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 DCHECK(loc.wide);
722 DCHECK(CheckCorePoolSanity());
723 if (loc.location != kLocPhysReg) {
724 DCHECK((loc.location == kLocDalvikFrame) ||
725 (loc.location == kLocCompilerTemp));
726 // Are the dalvik regs already live in physical registers?
727 RegisterInfo* info_lo = AllocLive(loc.s_reg_low, kAnyReg);
728 RegisterInfo* info_hi = AllocLive(GetSRegHi(loc.s_reg_low), kAnyReg);
729 bool match = true;
730 match = match && (info_lo != NULL);
731 match = match && (info_hi != NULL);
732 // Are they both core or both FP?
733 match = match && (IsFpReg(info_lo->reg) == IsFpReg(info_hi->reg));
734 // If a pair of floating point singles, are they properly aligned?
735 if (match && IsFpReg(info_lo->reg)) {
736 match &= ((info_lo->reg & 0x1) == 0);
737 match &= ((info_hi->reg - info_lo->reg) == 1);
738 }
739 // If previously used as a pair, it is the same pair?
740 if (match && (info_lo->pair || info_hi->pair)) {
741 match = (info_lo->pair == info_hi->pair);
742 match &= ((info_lo->reg == info_hi->partner) &&
743 (info_hi->reg == info_lo->partner));
744 }
745 if (match) {
746 // Can reuse - update the register usage info
747 loc.low_reg = info_lo->reg;
748 loc.high_reg = info_hi->reg;
749 loc.location = kLocPhysReg;
750 MarkPair(loc.low_reg, loc.high_reg);
751 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
752 return loc;
753 }
754 // Can't easily reuse - clobber and free any overlaps
755 if (info_lo) {
756 Clobber(info_lo->reg);
757 FreeTemp(info_lo->reg);
758 if (info_lo->pair)
759 Clobber(info_lo->partner);
760 }
761 if (info_hi) {
762 Clobber(info_hi->reg);
763 FreeTemp(info_hi->reg);
764 if (info_hi->pair)
765 Clobber(info_hi->partner);
766 }
767 }
768 return loc;
769}
770
771
772/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700773RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 if (loc.wide)
775 return UpdateLocWide(loc);
776 else
777 return UpdateLoc(loc);
778}
779
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700780RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 DCHECK(loc.wide);
buzbee0d829482013-10-11 15:24:55 -0700782 int32_t new_regs;
783 int32_t low_reg;
784 int32_t high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785
786 loc = UpdateLocWide(loc);
787
788 /* If already in registers, we can assume proper form. Right reg class? */
789 if (loc.location == kLocPhysReg) {
790 DCHECK_EQ(IsFpReg(loc.low_reg), IsFpReg(loc.high_reg));
791 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
792 if (!RegClassMatches(reg_class, loc.low_reg)) {
793 /* Wrong register class. Reallocate and copy */
794 new_regs = AllocTypedTempPair(loc.fp, reg_class);
795 low_reg = new_regs & 0xff;
796 high_reg = (new_regs >> 8) & 0xff;
797 OpRegCopyWide(low_reg, high_reg, loc.low_reg, loc.high_reg);
798 CopyRegInfo(low_reg, loc.low_reg);
799 CopyRegInfo(high_reg, loc.high_reg);
800 Clobber(loc.low_reg);
801 Clobber(loc.high_reg);
802 loc.low_reg = low_reg;
803 loc.high_reg = high_reg;
804 MarkPair(loc.low_reg, loc.high_reg);
805 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
806 }
807 return loc;
808 }
809
810 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
811 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
812
813 new_regs = AllocTypedTempPair(loc.fp, reg_class);
814 loc.low_reg = new_regs & 0xff;
815 loc.high_reg = (new_regs >> 8) & 0xff;
816
817 MarkPair(loc.low_reg, loc.high_reg);
818 if (update) {
819 loc.location = kLocPhysReg;
820 MarkLive(loc.low_reg, loc.s_reg_low);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000821 // Does this wide value live in two registers or one vector register?
822 if (loc.low_reg != loc.high_reg) {
823 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
824 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 }
826 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
827 return loc;
828}
829
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700830RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 int new_reg;
832
833 if (loc.wide)
834 return EvalLocWide(loc, reg_class, update);
835
836 loc = UpdateLoc(loc);
837
838 if (loc.location == kLocPhysReg) {
839 if (!RegClassMatches(reg_class, loc.low_reg)) {
840 /* Wrong register class. Realloc, copy and transfer ownership */
841 new_reg = AllocTypedTemp(loc.fp, reg_class);
842 OpRegCopy(new_reg, loc.low_reg);
843 CopyRegInfo(new_reg, loc.low_reg);
844 Clobber(loc.low_reg);
845 loc.low_reg = new_reg;
846 }
847 return loc;
848 }
849
850 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
851
852 new_reg = AllocTypedTemp(loc.fp, reg_class);
853 loc.low_reg = new_reg;
854
855 if (update) {
856 loc.location = kLocPhysReg;
857 MarkLive(loc.low_reg, loc.s_reg_low);
858 }
859 return loc;
860}
861
862/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -0700863void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
865 RegLocation loc = mir_graph_->reg_location_[i];
866 RefCounts* counts = loc.fp ? fp_counts : core_counts;
867 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -0700868 if (loc.fp) {
869 if (loc.wide) {
870 // Treat doubles as a unit, using upper half of fp_counts array.
871 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
872 i++;
873 } else {
874 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
875 }
876 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
878 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 }
880}
881
882/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700883static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
885 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700886 // Note that we fall back to sorting on reg so we get stable output
887 // on differing qsort implementations (such as on host and target or
888 // between local host and build servers).
889 return (op1->count == op2->count)
890 ? (op1->s_reg - op2->s_reg)
891 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892}
893
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700894void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 LOG(INFO) << msg;
896 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700897 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
898 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
899 } else {
900 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
901 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 }
903}
904
905/*
906 * Note: some portions of this code required even if the kPromoteRegs
907 * optimization is disabled.
908 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700909void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800911 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -0800913 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
914 promotion_map_ = static_cast<PromotionMap*>
915 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916
917 // Allow target code to add any special registers
918 AdjustSpillMask();
919
920 /*
921 * Simple register promotion. Just do a static count of the uses
922 * of Dalvik registers. Note that we examine the SSA names, but
923 * count based on original Dalvik register name. Count refs
924 * separately based on type in order to give allocation
925 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -0700926 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -0700927 * reg.
928 * TUNING: replace with linear scan once we have the ability
929 * to describe register live ranges for GC.
930 */
931 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700932 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
933 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -0700935 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700936 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700937 // Set ssa names for original Dalvik registers
938 for (int i = 0; i < dalvik_regs; i++) {
939 core_regs[i].s_reg = FpRegs[i].s_reg = i;
940 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800941
942 // Set ssa names for compiler temporaries
943 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
944 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
945 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
946 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
947 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -0700948 }
949
950 // Duplicate in upper half to represent possible fp double starting sregs.
951 for (int i = 0; i < num_regs; i++) {
952 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 }
954
955 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -0700956 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958
959 // Sort the count arrays
960 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -0700961 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962
963 if (cu_->verbose) {
964 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -0700965 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 }
967
968 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
969 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -0700970 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
971 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
972 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
973 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
974 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
975 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
976 // Ignore result - if can't alloc double may still be able to alloc singles.
977 AllocPreservedDouble(low_sreg);
978 }
979 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
980 int reg = AllocPreservedSingle(FpRegs[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 if (reg < 0) {
buzbeec729a6b2013-09-14 16:04:31 -0700982 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 }
984 }
985 }
986
987 // Promote core regs
988 for (int i = 0; (i < num_regs) &&
989 (core_regs[i].count >= promotion_threshold); i++) {
990 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
991 if (promotion_map_[p_map_idx].core_location !=
992 kLocPhysReg) {
993 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
994 if (reg < 0) {
995 break; // No more left
996 }
997 }
998 }
999 }
1000
1001 // Now, update SSA names to new home locations
1002 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1003 RegLocation *curr = &mir_graph_->reg_location_[i];
1004 int p_map_idx = SRegToPMap(curr->s_reg_low);
1005 if (!curr->wide) {
1006 if (curr->fp) {
1007 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1008 curr->location = kLocPhysReg;
1009 curr->low_reg = promotion_map_[p_map_idx].FpReg;
1010 curr->home = true;
1011 }
1012 } else {
1013 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1014 curr->location = kLocPhysReg;
1015 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1016 curr->home = true;
1017 }
1018 }
1019 curr->high_reg = INVALID_REG;
1020 } else {
1021 if (curr->high_word) {
1022 continue;
1023 }
1024 if (curr->fp) {
1025 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1026 (promotion_map_[p_map_idx+1].fp_location ==
1027 kLocPhysReg)) {
1028 int low_reg = promotion_map_[p_map_idx].FpReg;
1029 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1030 // Doubles require pair of singles starting at even reg
1031 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1032 curr->location = kLocPhysReg;
1033 curr->low_reg = low_reg;
1034 curr->high_reg = high_reg;
1035 curr->home = true;
1036 }
1037 }
1038 } else {
1039 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1040 && (promotion_map_[p_map_idx+1].core_location ==
1041 kLocPhysReg)) {
1042 curr->location = kLocPhysReg;
1043 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1044 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1045 curr->home = true;
1046 }
1047 }
1048 }
1049 }
1050 if (cu_->verbose) {
1051 DumpPromotionMap();
1052 }
1053}
1054
1055/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001056int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1058 fp_spill_mask_, frame_size_, v_reg);
1059}
1060
1061/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001062int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1064}
1065
1066/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001067RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 RegLocation gpr_res = LocCReturnWide();
1069 RegLocation fpr_res = LocCReturnDouble();
1070 RegLocation res = is_double ? fpr_res : gpr_res;
1071 Clobber(res.low_reg);
1072 Clobber(res.high_reg);
1073 LockTemp(res.low_reg);
1074 LockTemp(res.high_reg);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001075 // Does this wide value live in two registers or one vector register?
1076 if (res.low_reg != res.high_reg) {
1077 MarkPair(res.low_reg, res.high_reg);
1078 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 return res;
1080}
1081
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001082RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 RegLocation gpr_res = LocCReturn();
1084 RegLocation fpr_res = LocCReturnFloat();
1085 RegLocation res = is_float ? fpr_res : gpr_res;
1086 Clobber(res.low_reg);
1087 if (cu_->instruction_set == kMips) {
1088 MarkInUse(res.low_reg);
1089 } else {
1090 LockTemp(res.low_reg);
1091 }
1092 return res;
1093}
1094
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001095void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 DoPromotion();
1097
1098 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1099 LOG(INFO) << "After Promotion";
1100 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1101 }
1102
1103 /* Set the frame size */
1104 frame_size_ = ComputeFrameSize();
1105}
1106
1107/*
1108 * Get the "real" sreg number associated with an s_reg slot. In general,
1109 * s_reg values passed through codegen are the SSA names created by
1110 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1111 * array. However, renaming is accomplished by simply replacing RegLocation
1112 * entries in the reglocation[] array. Therefore, when location
1113 * records for operands are first created, we need to ask the locRecord
1114 * identified by the dataflow pass what it's new name is.
1115 */
1116int Mir2Lir::GetSRegHi(int lowSreg) {
1117 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1118}
1119
1120bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001121 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 return true;
1123}
1124
1125int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1126 DCHECK_GT(mir->ssa_rep->num_uses, num);
1127 return mir->ssa_rep->uses[num];
1128}
1129
1130} // namespace art