blob: 32c22f2c04a96d33f9dca316e06b80ec69afe1c3 [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 {
135 int pos = std::abs(v_reg) - std::abs(SSA_METHOD_BASEREG);
136 DCHECK_LE(pos, cu_->num_compiler_temps);
137 return cu_->num_dalvik_registers + pos;
138 }
139}
140
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700141void Mir2Lir::RecordCorePromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 int p_map_idx = SRegToPMap(s_reg);
143 int v_reg = mir_graph_->SRegToVReg(s_reg);
144 GetRegInfo(reg)->in_use = true;
145 core_spill_mask_ |= (1 << reg);
146 // Include reg for later sort
147 core_vmap_table_.push_back(reg << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
148 num_core_spills_++;
149 promotion_map_[p_map_idx].core_location = kLocPhysReg;
150 promotion_map_[p_map_idx].core_reg = reg;
151}
152
153/* Reserve a callee-save register. Return -1 if none available */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700154int Mir2Lir::AllocPreservedCoreReg(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 int res = -1;
156 RegisterInfo* core_regs = reg_pool_->core_regs;
157 for (int i = 0; i < reg_pool_->num_core_regs; i++) {
158 if (!core_regs[i].is_temp && !core_regs[i].in_use) {
159 res = core_regs[i].reg;
160 RecordCorePromotion(res, s_reg);
161 break;
162 }
163 }
164 return res;
165}
166
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700167void Mir2Lir::RecordFpPromotion(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 int p_map_idx = SRegToPMap(s_reg);
169 int v_reg = mir_graph_->SRegToVReg(s_reg);
170 GetRegInfo(reg)->in_use = true;
171 MarkPreservedSingle(v_reg, reg);
172 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
173 promotion_map_[p_map_idx].FpReg = reg;
174}
175
buzbeec729a6b2013-09-14 16:04:31 -0700176// Reserve a callee-save fp single register.
177int Mir2Lir::AllocPreservedSingle(int s_reg) {
178 int res = -1; // Return code if none available.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 RegisterInfo* FPRegs = reg_pool_->FPRegs;
180 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700181 if (!FPRegs[i].is_temp && !FPRegs[i].in_use) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 res = FPRegs[i].reg;
183 RecordFpPromotion(res, s_reg);
184 break;
185 }
186 }
187 return res;
188}
189
190/*
191 * Somewhat messy code here. We want to allocate a pair of contiguous
192 * physical single-precision floating point registers starting with
193 * an even numbered reg. It is possible that the paired s_reg (s_reg+1)
194 * has already been allocated - try to fit if possible. Fail to
195 * allocate if we can't meet the requirements for the pair of
196 * s_reg<=sX[even] & (s_reg+1)<= sX+1.
197 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700198int Mir2Lir::AllocPreservedDouble(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700199 int res = -1; // Assume failure
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 int v_reg = mir_graph_->SRegToVReg(s_reg);
201 int p_map_idx = SRegToPMap(s_reg);
202 if (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg) {
203 // Upper reg is already allocated. Can we fit?
204 int high_reg = promotion_map_[p_map_idx+1].FpReg;
205 if ((high_reg & 1) == 0) {
206 // High reg is even - fail.
207 return res;
208 }
209 // Is the low reg of the pair free?
210 RegisterInfo* p = GetRegInfo(high_reg-1);
211 if (p->in_use || p->is_temp) {
212 // Already allocated or not preserved - fail.
213 return res;
214 }
215 // OK - good to go.
216 res = p->reg;
217 p->in_use = true;
218 DCHECK_EQ((res & 1), 0);
219 MarkPreservedSingle(v_reg, res);
220 } else {
221 RegisterInfo* FPRegs = reg_pool_->FPRegs;
222 for (int i = 0; i < reg_pool_->num_fp_regs; i++) {
223 if (!FPRegs[i].is_temp && !FPRegs[i].in_use &&
224 ((FPRegs[i].reg & 0x1) == 0x0) &&
225 !FPRegs[i+1].is_temp && !FPRegs[i+1].in_use &&
226 ((FPRegs[i+1].reg & 0x1) == 0x1) &&
227 (FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
228 res = FPRegs[i].reg;
229 FPRegs[i].in_use = true;
230 MarkPreservedSingle(v_reg, res);
231 FPRegs[i+1].in_use = true;
232 DCHECK_EQ(res + 1, FPRegs[i+1].reg);
233 MarkPreservedSingle(v_reg+1, res+1);
234 break;
235 }
236 }
237 }
238 if (res != -1) {
239 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
240 promotion_map_[p_map_idx].FpReg = res;
241 promotion_map_[p_map_idx+1].fp_location = kLocPhysReg;
242 promotion_map_[p_map_idx+1].FpReg = res + 1;
243 }
244 return res;
245}
246
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247int Mir2Lir::AllocTempBody(RegisterInfo* p, int num_regs, int* next_temp,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700248 bool required) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700250 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 if (next >= num_regs)
252 next = 0;
253 if (p[next].is_temp && !p[next].in_use && !p[next].live) {
254 Clobber(p[next].reg);
255 p[next].in_use = true;
256 p[next].pair = false;
257 *next_temp = next + 1;
258 return p[next].reg;
259 }
260 next++;
261 }
262 next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700263 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 if (next >= num_regs)
265 next = 0;
266 if (p[next].is_temp && !p[next].in_use) {
267 Clobber(p[next].reg);
268 p[next].in_use = true;
269 p[next].pair = false;
270 *next_temp = next + 1;
271 return p[next].reg;
272 }
273 next++;
274 }
275 if (required) {
276 CodegenDump();
277 DumpRegPool(reg_pool_->core_regs,
278 reg_pool_->num_core_regs);
279 LOG(FATAL) << "No free temp registers";
280 }
281 return -1; // No register available
282}
283
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700284// REDO: too many assumptions.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700285int Mir2Lir::AllocTempDouble() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 RegisterInfo* p = reg_pool_->FPRegs;
287 int num_regs = reg_pool_->num_fp_regs;
288 /* Start looking at an even reg */
289 int next = reg_pool_->next_fp_reg & ~0x1;
290
291 // First try to avoid allocating live registers
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700292 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 if (next >= num_regs)
294 next = 0;
295 if ((p[next].is_temp && !p[next].in_use && !p[next].live) &&
296 (p[next+1].is_temp && !p[next+1].in_use && !p[next+1].live)) {
297 Clobber(p[next].reg);
298 Clobber(p[next+1].reg);
299 p[next].in_use = true;
300 p[next+1].in_use = true;
301 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
302 DCHECK_EQ((p[next].reg & 0x1), 0);
303 reg_pool_->next_fp_reg = next + 2;
304 if (reg_pool_->next_fp_reg >= num_regs) {
305 reg_pool_->next_fp_reg = 0;
306 }
307 return p[next].reg;
308 }
309 next += 2;
310 }
311 next = reg_pool_->next_fp_reg & ~0x1;
312
313 // No choice - find a pair and kill it.
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700314 for (int i = 0; i < num_regs; i+=2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 if (next >= num_regs)
316 next = 0;
317 if (p[next].is_temp && !p[next].in_use && p[next+1].is_temp &&
318 !p[next+1].in_use) {
319 Clobber(p[next].reg);
320 Clobber(p[next+1].reg);
321 p[next].in_use = true;
322 p[next+1].in_use = true;
323 DCHECK_EQ((p[next].reg+1), p[next+1].reg);
324 DCHECK_EQ((p[next].reg & 0x1), 0);
325 reg_pool_->next_fp_reg = next + 2;
326 if (reg_pool_->next_fp_reg >= num_regs) {
327 reg_pool_->next_fp_reg = 0;
328 }
329 return p[next].reg;
330 }
331 next += 2;
332 }
333 LOG(FATAL) << "No free temp registers (pair)";
334 return -1;
335}
336
337/* Return a temp if one is available, -1 otherwise */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700338int Mir2Lir::AllocFreeTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 return AllocTempBody(reg_pool_->core_regs,
340 reg_pool_->num_core_regs,
Vladimir Marko73e08b32013-11-21 10:58:36 +0000341 &reg_pool_->next_core_reg, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342}
343
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700344int Mir2Lir::AllocTemp() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 return AllocTempBody(reg_pool_->core_regs,
346 reg_pool_->num_core_regs,
347 &reg_pool_->next_core_reg, true);
348}
349
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700350int Mir2Lir::AllocTempFloat() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 return AllocTempBody(reg_pool_->FPRegs,
352 reg_pool_->num_fp_regs,
353 &reg_pool_->next_fp_reg, true);
354}
355
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700356Mir2Lir::RegisterInfo* Mir2Lir::AllocLiveBody(RegisterInfo* p, int num_regs, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 if (s_reg == -1)
358 return NULL;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700359 for (int i = 0; i < num_regs; i++) {
buzbee56c71782013-09-05 17:13:19 -0700360 if ((p[i].s_reg == s_reg) && p[i].live) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 if (p[i].is_temp)
362 p[i].in_use = true;
363 return &p[i];
364 }
365 }
366 return NULL;
367}
368
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369Mir2Lir::RegisterInfo* Mir2Lir::AllocLive(int s_reg, int reg_class) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 RegisterInfo* res = NULL;
371 switch (reg_class) {
372 case kAnyReg:
373 res = AllocLiveBody(reg_pool_->FPRegs,
374 reg_pool_->num_fp_regs, s_reg);
375 if (res)
376 break;
377 /* Intentional fallthrough */
378 case kCoreReg:
379 res = AllocLiveBody(reg_pool_->core_regs,
380 reg_pool_->num_core_regs, s_reg);
381 break;
382 case kFPReg:
383 res = AllocLiveBody(reg_pool_->FPRegs,
384 reg_pool_->num_fp_regs, s_reg);
385 break;
386 default:
387 LOG(FATAL) << "Invalid register type";
388 }
389 return res;
390}
391
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700392void Mir2Lir::FreeTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700393 RegisterInfo* p = GetRegInfo(reg);
394 if (p->is_temp) {
395 p->in_use = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
buzbee56c71782013-09-05 17:13:19 -0700397 p->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398}
399
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700400Mir2Lir::RegisterInfo* Mir2Lir::IsLive(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700401 RegisterInfo* p = GetRegInfo(reg);
402 return p->live ? p : NULL;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403}
404
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700405Mir2Lir::RegisterInfo* Mir2Lir::IsTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 RegisterInfo* p = GetRegInfo(reg);
407 return (p->is_temp) ? p : NULL;
408}
409
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700410Mir2Lir::RegisterInfo* Mir2Lir::IsPromoted(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 RegisterInfo* p = GetRegInfo(reg);
412 return (p->is_temp) ? NULL : p;
413}
414
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700415bool Mir2Lir::IsDirty(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 RegisterInfo* p = GetRegInfo(reg);
417 return p->dirty;
418}
419
420/*
421 * Similar to AllocTemp(), but forces the allocation of a specific
422 * register. No check is made to see if the register was previously
423 * allocated. Use with caution.
424 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700425void Mir2Lir::LockTemp(int reg) {
buzbee56c71782013-09-05 17:13:19 -0700426 RegisterInfo* p = GetRegInfo(reg);
427 DCHECK(p->is_temp);
428 p->in_use = true;
429 p->live = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430}
431
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700432void Mir2Lir::ResetDef(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 ResetDefBody(GetRegInfo(reg));
434}
435
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700436void Mir2Lir::NullifyRange(LIR *start, LIR *finish, int s_reg1, int s_reg2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 if (start && finish) {
438 LIR *p;
439 DCHECK_EQ(s_reg1, s_reg2);
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700440 for (p = start; ; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 NopLIR(p);
442 if (p == finish)
443 break;
444 }
445 }
446}
447
448/*
449 * Mark the beginning and end LIR of a def sequence. Note that
450 * on entry start points to the LIR prior to the beginning of the
451 * sequence.
452 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700453void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 DCHECK(!rl.wide);
455 DCHECK(start && start->next);
456 DCHECK(finish);
457 RegisterInfo* p = GetRegInfo(rl.low_reg);
458 p->def_start = start->next;
459 p->def_end = finish;
460}
461
462/*
463 * Mark the beginning and end LIR of a def sequence. Note that
464 * on entry start points to the LIR prior to the beginning of the
465 * sequence.
466 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700467void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 DCHECK(rl.wide);
469 DCHECK(start && start->next);
470 DCHECK(finish);
471 RegisterInfo* p = GetRegInfo(rl.low_reg);
472 ResetDef(rl.high_reg); // Only track low of pair
473 p->def_start = start->next;
474 p->def_end = finish;
475}
476
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700477RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 DCHECK(rl.wide);
479 if (rl.location == kLocPhysReg) {
480 RegisterInfo* info_lo = GetRegInfo(rl.low_reg);
481 RegisterInfo* info_hi = GetRegInfo(rl.high_reg);
482 if (info_lo->is_temp) {
483 info_lo->pair = false;
484 info_lo->def_start = NULL;
485 info_lo->def_end = NULL;
486 }
487 if (info_hi->is_temp) {
488 info_hi->pair = false;
489 info_hi->def_start = NULL;
490 info_hi->def_end = NULL;
491 }
492 }
493 rl.wide = false;
494 return rl;
495}
496
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 DCHECK(!rl.wide);
499 RegisterInfo* p = IsTemp(rl.low_reg);
500 if (p && !(cu_->disable_opt & (1 << kSuppressLoads))) {
501 DCHECK(!p->pair);
502 NullifyRange(p->def_start, p->def_end, p->s_reg, rl.s_reg_low);
503 }
504 ResetDef(rl.low_reg);
505}
506
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700507void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 DCHECK(rl.wide);
509 RegisterInfo* p_low = IsTemp(rl.low_reg);
510 RegisterInfo* p_high = IsTemp(rl.high_reg);
511 if (p_low && !(cu_->disable_opt & (1 << kSuppressLoads))) {
512 DCHECK(p_low->pair);
513 NullifyRange(p_low->def_start, p_low->def_end, p_low->s_reg, rl.s_reg_low);
514 }
515 if (p_high && !(cu_->disable_opt & (1 << kSuppressLoads))) {
516 DCHECK(p_high->pair);
517 }
518 ResetDef(rl.low_reg);
519 ResetDef(rl.high_reg);
520}
521
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700522void Mir2Lir::ResetDefTracking() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700523 for (int i = 0; i< reg_pool_->num_core_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 ResetDefBody(&reg_pool_->core_regs[i]);
525 }
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700526 for (int i = 0; i< reg_pool_->num_fp_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 ResetDefBody(&reg_pool_->FPRegs[i]);
528 }
529}
530
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700531void Mir2Lir::ClobberAllRegs() {
buzbeebd663de2013-09-10 15:41:31 -0700532 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
533 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
534 info->live = false;
535 info->s_reg = INVALID_SREG;
536 info->def_start = NULL;
537 info->def_end = NULL;
538 info->pair = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 }
540}
541
542// Make sure nothing is live and dirty
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700543void Mir2Lir::FlushAllRegsBody(RegisterInfo* info, int num_regs) {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700544 for (int i = 0; i < num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 if (info[i].live && info[i].dirty) {
546 if (info[i].pair) {
547 FlushRegWide(info[i].reg, info[i].partner);
548 } else {
549 FlushReg(info[i].reg);
550 }
551 }
552 }
553}
554
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700555void Mir2Lir::FlushAllRegs() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 FlushAllRegsBody(reg_pool_->core_regs,
557 reg_pool_->num_core_regs);
558 FlushAllRegsBody(reg_pool_->FPRegs,
559 reg_pool_->num_fp_regs);
560 ClobberAllRegs();
561}
562
563
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700564// TUNING: rewrite all of this reg stuff. Probably use an attribute table
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700565bool Mir2Lir::RegClassMatches(int reg_class, int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 if (reg_class == kAnyReg) {
567 return true;
568 } else if (reg_class == kCoreReg) {
569 return !IsFpReg(reg);
570 } else {
571 return IsFpReg(reg);
572 }
573}
574
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700575void Mir2Lir::MarkLive(int reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 RegisterInfo* info = GetRegInfo(reg);
577 if ((info->reg == reg) && (info->s_reg == s_reg) && info->live) {
578 return; /* already live */
579 } else if (s_reg != INVALID_SREG) {
580 ClobberSReg(s_reg);
581 if (info->is_temp) {
582 info->live = true;
583 }
584 } else {
585 /* Can't be live if no associated s_reg */
586 DCHECK(info->is_temp);
587 info->live = false;
588 }
589 info->s_reg = s_reg;
590}
591
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700592void Mir2Lir::MarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700594 tempreg_info_.Insert(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 info->is_temp = true;
596}
597
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700598void Mir2Lir::UnmarkTemp(int reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 RegisterInfo* info = GetRegInfo(reg);
buzbeebd663de2013-09-10 15:41:31 -0700600 tempreg_info_.Delete(info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 info->is_temp = false;
602}
603
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700604void Mir2Lir::MarkPair(int low_reg, int high_reg) {
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000605 DCHECK_NE(low_reg, 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);
buzbee0d829482013-10-11 15:24:55 -0700772 int32_t new_regs;
773 int32_t low_reg;
774 int32_t high_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775
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);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000811 // Does this wide value live in two registers or one vector register?
812 if (loc.low_reg != loc.high_reg) {
813 MarkLive(loc.high_reg, GetSRegHi(loc.s_reg_low));
814 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 }
816 DCHECK(!IsFpReg(loc.low_reg) || ((loc.low_reg & 0x1) == 0));
817 return loc;
818}
819
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700820RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821 int new_reg;
822
823 if (loc.wide)
824 return EvalLocWide(loc, reg_class, update);
825
826 loc = UpdateLoc(loc);
827
828 if (loc.location == kLocPhysReg) {
829 if (!RegClassMatches(reg_class, loc.low_reg)) {
830 /* Wrong register class. Realloc, copy and transfer ownership */
831 new_reg = AllocTypedTemp(loc.fp, reg_class);
832 OpRegCopy(new_reg, loc.low_reg);
833 CopyRegInfo(new_reg, loc.low_reg);
834 Clobber(loc.low_reg);
835 loc.low_reg = new_reg;
836 }
837 return loc;
838 }
839
840 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
841
842 new_reg = AllocTypedTemp(loc.fp, reg_class);
843 loc.low_reg = new_reg;
844
845 if (update) {
846 loc.location = kLocPhysReg;
847 MarkLive(loc.low_reg, loc.s_reg_low);
848 }
849 return loc;
850}
851
852/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -0700853void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
855 RegLocation loc = mir_graph_->reg_location_[i];
856 RefCounts* counts = loc.fp ? fp_counts : core_counts;
857 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -0700858 if (loc.fp) {
859 if (loc.wide) {
860 // Treat doubles as a unit, using upper half of fp_counts array.
861 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
862 i++;
863 } else {
864 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
865 }
866 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
868 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700869 }
870}
871
872/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700873static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
875 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700876 // Note that we fall back to sorting on reg so we get stable output
877 // on differing qsort implementations (such as on host and target or
878 // between local host and build servers).
879 return (op1->count == op2->count)
880 ? (op1->s_reg - op2->s_reg)
881 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882}
883
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700884void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 LOG(INFO) << msg;
886 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -0700887 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
888 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
889 } else {
890 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
891 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892 }
893}
894
895/*
896 * Note: some portions of this code required even if the kPromoteRegs
897 * optimization is disabled.
898 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700899void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900 int reg_bias = cu_->num_compiler_temps + 1;
901 int dalvik_regs = cu_->num_dalvik_registers;
902 int num_regs = dalvik_regs + reg_bias;
903 const int promotion_threshold = 1;
904
905 // Allow target code to add any special registers
906 AdjustSpillMask();
907
908 /*
909 * Simple register promotion. Just do a static count of the uses
910 * of Dalvik registers. Note that we examine the SSA names, but
911 * count based on original Dalvik register name. Count refs
912 * separately based on type in order to give allocation
913 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -0700914 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915 * reg.
916 * TUNING: replace with linear scan once we have the ability
917 * to describe register live ranges for GC.
918 */
919 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700920 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
921 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -0700923 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700924 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700925 // Set ssa names for original Dalvik registers
926 for (int i = 0; i < dalvik_regs; i++) {
927 core_regs[i].s_reg = FpRegs[i].s_reg = i;
928 }
929 // Set ssa name for Method*
930 core_regs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg();
buzbeec729a6b2013-09-14 16:04:31 -0700931 FpRegs[dalvik_regs].s_reg = mir_graph_->GetMethodSReg(); // For consistecy.
932 FpRegs[dalvik_regs + num_regs].s_reg = mir_graph_->GetMethodSReg(); // for consistency.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 // Set ssa names for compiler_temps
934 for (int i = 1; i <= cu_->num_compiler_temps; i++) {
935 CompilerTemp* ct = mir_graph_->compiler_temps_.Get(i);
936 core_regs[dalvik_regs + i].s_reg = ct->s_reg;
937 FpRegs[dalvik_regs + i].s_reg = ct->s_reg;
buzbeec729a6b2013-09-14 16:04:31 -0700938 FpRegs[num_regs + dalvik_regs + i].s_reg = ct->s_reg;
939 }
940
941 // Duplicate in upper half to represent possible fp double starting sregs.
942 for (int i = 0; i < num_regs; i++) {
943 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 }
945
946 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -0700947 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949
950 // Sort the count arrays
951 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -0700952 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953
954 if (cu_->verbose) {
955 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -0700956 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 }
958
959 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
960 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -0700961 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
962 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
963 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
964 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
965 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
966 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
967 // Ignore result - if can't alloc double may still be able to alloc singles.
968 AllocPreservedDouble(low_sreg);
969 }
970 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
971 int reg = AllocPreservedSingle(FpRegs[i].s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 if (reg < 0) {
buzbeec729a6b2013-09-14 16:04:31 -0700973 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 }
975 }
976 }
977
978 // Promote core regs
979 for (int i = 0; (i < num_regs) &&
980 (core_regs[i].count >= promotion_threshold); i++) {
981 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
982 if (promotion_map_[p_map_idx].core_location !=
983 kLocPhysReg) {
984 int reg = AllocPreservedCoreReg(core_regs[i].s_reg);
985 if (reg < 0) {
986 break; // No more left
987 }
988 }
989 }
990 }
991
992 // Now, update SSA names to new home locations
993 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
994 RegLocation *curr = &mir_graph_->reg_location_[i];
995 int p_map_idx = SRegToPMap(curr->s_reg_low);
996 if (!curr->wide) {
997 if (curr->fp) {
998 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
999 curr->location = kLocPhysReg;
1000 curr->low_reg = promotion_map_[p_map_idx].FpReg;
1001 curr->home = true;
1002 }
1003 } else {
1004 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1005 curr->location = kLocPhysReg;
1006 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1007 curr->home = true;
1008 }
1009 }
1010 curr->high_reg = INVALID_REG;
1011 } else {
1012 if (curr->high_word) {
1013 continue;
1014 }
1015 if (curr->fp) {
1016 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
1017 (promotion_map_[p_map_idx+1].fp_location ==
1018 kLocPhysReg)) {
1019 int low_reg = promotion_map_[p_map_idx].FpReg;
1020 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1021 // Doubles require pair of singles starting at even reg
1022 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1023 curr->location = kLocPhysReg;
1024 curr->low_reg = low_reg;
1025 curr->high_reg = high_reg;
1026 curr->home = true;
1027 }
1028 }
1029 } else {
1030 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1031 && (promotion_map_[p_map_idx+1].core_location ==
1032 kLocPhysReg)) {
1033 curr->location = kLocPhysReg;
1034 curr->low_reg = promotion_map_[p_map_idx].core_reg;
1035 curr->high_reg = promotion_map_[p_map_idx+1].core_reg;
1036 curr->home = true;
1037 }
1038 }
1039 }
1040 }
1041 if (cu_->verbose) {
1042 DumpPromotionMap();
1043 }
1044}
1045
1046/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001047int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
1049 fp_spill_mask_, frame_size_, v_reg);
1050}
1051
1052/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001053int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1055}
1056
1057/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001058RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 RegLocation gpr_res = LocCReturnWide();
1060 RegLocation fpr_res = LocCReturnDouble();
1061 RegLocation res = is_double ? fpr_res : gpr_res;
1062 Clobber(res.low_reg);
1063 Clobber(res.high_reg);
1064 LockTemp(res.low_reg);
1065 LockTemp(res.high_reg);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001066 // Does this wide value live in two registers or one vector register?
1067 if (res.low_reg != res.high_reg) {
1068 MarkPair(res.low_reg, res.high_reg);
1069 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 return res;
1071}
1072
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001073RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 RegLocation gpr_res = LocCReturn();
1075 RegLocation fpr_res = LocCReturnFloat();
1076 RegLocation res = is_float ? fpr_res : gpr_res;
1077 Clobber(res.low_reg);
1078 if (cu_->instruction_set == kMips) {
1079 MarkInUse(res.low_reg);
1080 } else {
1081 LockTemp(res.low_reg);
1082 }
1083 return res;
1084}
1085
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001086void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 DoPromotion();
1088
1089 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1090 LOG(INFO) << "After Promotion";
1091 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1092 }
1093
1094 /* Set the frame size */
1095 frame_size_ = ComputeFrameSize();
1096}
1097
1098/*
1099 * Get the "real" sreg number associated with an s_reg slot. In general,
1100 * s_reg values passed through codegen are the SSA names created by
1101 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1102 * array. However, renaming is accomplished by simply replacing RegLocation
1103 * entries in the reglocation[] array. Therefore, when location
1104 * records for operands are first created, we need to ask the locRecord
1105 * identified by the dataflow pass what it's new name is.
1106 */
1107int Mir2Lir::GetSRegHi(int lowSreg) {
1108 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1109}
1110
1111bool Mir2Lir::oat_live_out(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001112 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 return true;
1114}
1115
1116int Mir2Lir::oatSSASrc(MIR* mir, int num) {
1117 DCHECK_GT(mir->ssa_rep->num_uses, num);
1118 return mir->ssa_rep->uses[num];
1119}
1120
1121} // namespace art