blob: 2c51c1f2fda5abff20e2ce5f1751a4b4fb6c6f82 [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()) {
buzbee091cc402014-03-31 10:14:40 -070033 info->MarkFree();
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
buzbee091cc402014-03-31 10:14:40 -070041Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, uint64_t mask)
buzbee30adc732014-05-09 15:10:18 -070042 : reg_(r), is_temp_(false), wide_value_(false), dirty_(false), aliased_(false), partner_(r),
buzbeeba574512014-05-12 15:13:16 -070043 s_reg_(INVALID_SREG), def_use_mask_(mask), master_(this), def_start_(nullptr),
44 def_end_(nullptr), alias_chain_(nullptr) {
buzbee091cc402014-03-31 10:14:40 -070045 switch (r.StorageSize()) {
46 case 0: storage_mask_ = 0xffffffff; break;
47 case 4: storage_mask_ = 0x00000001; break;
48 case 8: storage_mask_ = 0x00000003; break;
49 case 16: storage_mask_ = 0x0000000f; break;
50 case 32: storage_mask_ = 0x000000ff; break;
51 case 64: storage_mask_ = 0x0000ffff; break;
52 case 128: storage_mask_ = 0xffffffff; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 }
buzbee091cc402014-03-31 10:14:40 -070054 used_storage_ = r.Valid() ? ~storage_mask_ : storage_mask_;
buzbee30adc732014-05-09 15:10:18 -070055 liveness_ = used_storage_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070056}
57
buzbee091cc402014-03-31 10:14:40 -070058Mir2Lir::RegisterPool::RegisterPool(Mir2Lir* m2l, ArenaAllocator* arena,
59 const std::vector<RegStorage>& core_regs,
buzbeeb01bf152014-05-13 15:59:07 -070060 const std::vector<RegStorage>& core64_regs,
buzbee091cc402014-03-31 10:14:40 -070061 const std::vector<RegStorage>& sp_regs,
62 const std::vector<RegStorage>& dp_regs,
63 const std::vector<RegStorage>& reserved_regs,
buzbeeb01bf152014-05-13 15:59:07 -070064 const std::vector<RegStorage>& reserved64_regs,
buzbee091cc402014-03-31 10:14:40 -070065 const std::vector<RegStorage>& core_temps,
buzbeeb01bf152014-05-13 15:59:07 -070066 const std::vector<RegStorage>& core64_temps,
buzbee091cc402014-03-31 10:14:40 -070067 const std::vector<RegStorage>& sp_temps,
68 const std::vector<RegStorage>& dp_temps) :
buzbeeb01bf152014-05-13 15:59:07 -070069 core_regs_(arena, core_regs.size()), next_core_reg_(0),
70 core64_regs_(arena, core64_regs.size()), next_core64_reg_(0),
71 sp_regs_(arena, sp_regs.size()), next_sp_reg_(0),
72 dp_regs_(arena, dp_regs.size()), next_dp_reg_(0), m2l_(m2l) {
buzbee091cc402014-03-31 10:14:40 -070073 // Initialize the fast lookup map.
74 m2l_->reginfo_map_.Reset();
buzbeeba574512014-05-12 15:13:16 -070075 if (kIsDebugBuild) {
76 m2l_->reginfo_map_.Resize(RegStorage::kMaxRegs);
77 for (unsigned i = 0; i < RegStorage::kMaxRegs; i++) {
78 m2l_->reginfo_map_.Insert(nullptr);
79 }
80 } else {
81 m2l_->reginfo_map_.SetSize(RegStorage::kMaxRegs);
buzbee091cc402014-03-31 10:14:40 -070082 }
83
84 // Construct the register pool.
85 for (RegStorage reg : core_regs) {
86 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
87 m2l_->reginfo_map_.Put(reg.GetReg(), info);
88 core_regs_.Insert(info);
89 }
buzbeeb01bf152014-05-13 15:59:07 -070090 for (RegStorage reg : core64_regs) {
91 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
92 m2l_->reginfo_map_.Put(reg.GetReg(), info);
93 core64_regs_.Insert(info);
94 }
buzbee091cc402014-03-31 10:14:40 -070095 for (RegStorage reg : sp_regs) {
96 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
97 m2l_->reginfo_map_.Put(reg.GetReg(), info);
98 sp_regs_.Insert(info);
99 }
100 for (RegStorage reg : dp_regs) {
101 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
102 m2l_->reginfo_map_.Put(reg.GetReg(), info);
103 dp_regs_.Insert(info);
104 }
105
106 // Keep special registers from being allocated.
107 for (RegStorage reg : reserved_regs) {
108 m2l_->MarkInUse(reg);
109 }
buzbeeb01bf152014-05-13 15:59:07 -0700110 for (RegStorage reg : reserved64_regs) {
111 m2l_->MarkInUse(reg);
112 }
buzbee091cc402014-03-31 10:14:40 -0700113
114 // Mark temp regs - all others not in use can be used for promotion
115 for (RegStorage reg : core_temps) {
116 m2l_->MarkTemp(reg);
117 }
buzbeeb01bf152014-05-13 15:59:07 -0700118 for (RegStorage reg : core64_temps) {
119 m2l_->MarkTemp(reg);
120 }
buzbee091cc402014-03-31 10:14:40 -0700121 for (RegStorage reg : sp_temps) {
122 m2l_->MarkTemp(reg);
123 }
124 for (RegStorage reg : dp_temps) {
125 m2l_->MarkTemp(reg);
126 }
127
128 // Add an entry for InvalidReg with zero'd mask.
129 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), 0);
130 m2l_->reginfo_map_.Put(RegStorage::InvalidReg().GetReg(), invalid_reg);
131}
132
133void Mir2Lir::DumpRegPool(GrowableArray<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 LOG(INFO) << "================================================";
buzbee091cc402014-03-31 10:14:40 -0700135 GrowableArray<RegisterInfo*>::Iterator it(regs);
136 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700138 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
139 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
140 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
141 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 }
143 LOG(INFO) << "================================================";
144}
145
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700147 DumpRegPool(&reg_pool_->core_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148}
149
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700150void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700151 DumpRegPool(&reg_pool_->sp_regs_);
152 DumpRegPool(&reg_pool_->dp_regs_);
153}
154
155void Mir2Lir::DumpRegPools() {
156 LOG(INFO) << "Core registers";
157 DumpCoreRegPool();
158 LOG(INFO) << "FP registers";
159 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160}
161
buzbee2700f7e2014-03-07 09:46:20 -0800162void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700163 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700164 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700165 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700166 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700167 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800168 } else {
buzbee30adc732014-05-09 15:10:18 -0700169 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700170 if (info->IsTemp() && !info->IsDead()) {
buzbee082833c2014-05-17 23:16:26 -0700171 if (info->GetReg() != info->Partner()) {
172 ClobberBody(GetRegInfo(info->Partner()));
173 }
buzbeeba574512014-05-12 15:13:16 -0700174 ClobberBody(info);
175 if (info->IsAliased()) {
176 ClobberAliases(info);
177 } else {
178 RegisterInfo* master = info->Master();
179 if (info != master) {
180 ClobberBody(info->Master());
181 }
182 }
buzbee30adc732014-05-09 15:10:18 -0700183 }
buzbee2700f7e2014-03-07 09:46:20 -0800184 }
185}
186
buzbee30adc732014-05-09 15:10:18 -0700187void Mir2Lir::ClobberAliases(RegisterInfo* info) {
buzbeeba574512014-05-12 15:13:16 -0700188 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
189 alias = alias->GetAliasChain()) {
190 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee082833c2014-05-17 23:16:26 -0700191 ClobberBody(alias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 }
193}
194
195/*
196 * Break the association between a Dalvik vreg and a physical temp register of either register
197 * class.
198 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
199 * in the register utilities, is is also used by code gen routines to work around a deficiency in
200 * local register allocation, which fails to distinguish between the "in" and "out" identities
201 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
202 * is used both as the source and destination register of an operation in which the type
203 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
204 * addressed.
205 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700206void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700207 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700208 if (kIsDebugBuild && s_reg == live_sreg_) {
209 live_sreg_ = INVALID_SREG;
210 }
211 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
212 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
213 if (info->SReg() == s_reg) {
buzbee082833c2014-05-17 23:16:26 -0700214 if (info->GetReg() != info->Partner()) {
215 // Dealing with a pair - clobber the other half.
216 DCHECK(!info->IsAliased());
217 ClobberBody(GetRegInfo(info->Partner()));
218 }
buzbeeba574512014-05-12 15:13:16 -0700219 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700220 if (info->IsAliased()) {
buzbee30adc732014-05-09 15:10:18 -0700221 ClobberAliases(info);
222 }
buzbee091cc402014-03-31 10:14:40 -0700223 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 }
225 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226}
227
228/*
229 * SSA names associated with the initial definitions of Dalvik
230 * registers are the same as the Dalvik register number (and
231 * thus take the same position in the promotion_map. However,
232 * the special Method* and compiler temp resisters use negative
233 * v_reg numbers to distinguish them and can have an arbitrary
234 * ssa name (above the last original Dalvik register). This function
235 * maps SSA names to positions in the promotion_map array.
236 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700237int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
239 DCHECK_GE(s_reg, 0);
240 int v_reg = mir_graph_->SRegToVReg(s_reg);
241 if (v_reg >= 0) {
242 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
243 return v_reg;
244 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800245 /*
246 * It must be the case that the v_reg for temporary is less than or equal to the
247 * base reg for temps. For that reason, "position" must be zero or positive.
248 */
249 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
250
251 // The temporaries are placed after dalvik registers in the promotion map
252 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
253 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 }
255}
256
buzbee091cc402014-03-31 10:14:40 -0700257// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800258void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 int p_map_idx = SRegToPMap(s_reg);
260 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700261 int reg_num = reg.GetRegNum();
262 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800263 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800265 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 num_core_spills_++;
267 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800268 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269}
270
buzbee091cc402014-03-31 10:14:40 -0700271/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800272RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
273 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700274 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
275 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
276 if (!info->IsTemp() && !info->InUse()) {
277 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 RecordCorePromotion(res, s_reg);
279 break;
280 }
281 }
282 return res;
283}
284
buzbee091cc402014-03-31 10:14:40 -0700285void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 int p_map_idx = SRegToPMap(s_reg);
287 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700288 GetRegInfo(reg)->MarkInUse();
289 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700291 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292}
293
buzbee091cc402014-03-31 10:14:40 -0700294// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800295RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
296 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700297 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
298 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
299 if (!info->IsTemp() && !info->InUse()) {
300 res = info->GetReg();
301 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 break;
303 }
304 }
305 return res;
306}
307
buzbee091cc402014-03-31 10:14:40 -0700308void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
309 int p_map_idx = SRegToPMap(s_reg);
310 int v_reg = mir_graph_->SRegToVReg(s_reg);
311 GetRegInfo(reg)->MarkInUse();
312 MarkPreservedDouble(v_reg, reg);
313 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
314 promotion_map_[p_map_idx].FpReg = reg.GetReg();
315}
316
317// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800318RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
319 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700320 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
321 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
322 if (!info->IsTemp() && !info->InUse()) {
323 res = info->GetReg();
324 RecordDoublePromotion(res, s_reg);
325 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 }
328 return res;
329}
330
buzbee091cc402014-03-31 10:14:40 -0700331
332RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
333 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700335 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 if (next >= num_regs)
337 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700338 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700339 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700340 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee091cc402014-03-31 10:14:40 -0700341 Clobber(info->GetReg());
342 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700343 /*
344 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
345 * The caller will set wideness as appropriate.
346 */
buzbee091cc402014-03-31 10:14:40 -0700347 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700349 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 }
351 next++;
352 }
353 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700354 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700355 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 if (next >= num_regs)
357 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700358 RegisterInfo* info = regs.Get(next);
359 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700360 // Got one. Kill it.
361 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700362 Clobber(info->GetReg());
363 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700364 if (info->IsWide()) {
365 RegisterInfo* partner = GetRegInfo(info->Partner());
366 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
367 DCHECK(partner->IsWide());
368 info->SetIsWide(false);
369 partner->SetIsWide(false);
370 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700372 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 }
374 next++;
375 }
376 if (required) {
377 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700378 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 LOG(FATAL) << "No free temp registers";
380 }
buzbee2700f7e2014-03-07 09:46:20 -0800381 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382}
383
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800385RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700386 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387}
388
buzbee2700f7e2014-03-07 09:46:20 -0800389RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700390 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391}
392
buzbeeb01bf152014-05-13 15:59:07 -0700393RegStorage Mir2Lir::AllocTempWide() {
394 RegStorage res;
395 if (reg_pool_->core64_regs_.Size() != 0) {
396 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, true);
397 } else {
398 RegStorage low_reg = AllocTemp();
399 RegStorage high_reg = AllocTemp();
400 res = RegStorage::MakeRegPair(low_reg, high_reg);
401 }
402 return res;
403}
404
buzbee091cc402014-03-31 10:14:40 -0700405RegStorage Mir2Lir::AllocTempSingle() {
406 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
407 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
408 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409}
410
buzbee091cc402014-03-31 10:14:40 -0700411RegStorage Mir2Lir::AllocTempDouble() {
412 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
413 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
414 return res;
415}
416
buzbeeb01bf152014-05-13 15:59:07 -0700417RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
418 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
419 return AllocTempDouble();
420 }
421 return AllocTempWide();
422}
423
424RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
425 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
426 return AllocTempSingle();
427 }
428 return AllocTemp();
429}
430
buzbee091cc402014-03-31 10:14:40 -0700431RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
432 RegStorage res;
433 GrowableArray<RegisterInfo*>::Iterator it(&regs);
434 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
435 if ((info->SReg() == s_reg) && info->IsLive()) {
436 res = info->GetReg();
437 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 return res;
441}
442
buzbee091cc402014-03-31 10:14:40 -0700443RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
444 RegStorage reg;
445 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
446 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
447 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 }
buzbee091cc402014-03-31 10:14:40 -0700449 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee30adc732014-05-09 15:10:18 -0700450 // TODO: add 64-bit core pool similar to above.
buzbee091cc402014-03-31 10:14:40 -0700451 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
452 }
453 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700454 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
455 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700456 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
457 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700458 reg = RegStorage::MakeRegPair(reg, high_reg);
459 MarkWide(reg);
460 } else {
buzbee30adc732014-05-09 15:10:18 -0700461 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700462 reg = RegStorage::InvalidReg();
463 }
464 }
buzbee30adc732014-05-09 15:10:18 -0700465 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
466 // Width mismatch - don't try to reuse.
467 reg = RegStorage::InvalidReg();
468 }
469 }
470 if (reg.Valid()) {
471 if (reg.IsPair()) {
472 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
473 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
474 if (info_low->IsTemp()) {
475 info_low->MarkInUse();
476 }
477 if (info_high->IsTemp()) {
478 info_high->MarkInUse();
479 }
480 } else {
buzbee091cc402014-03-31 10:14:40 -0700481 RegisterInfo* info = GetRegInfo(reg);
482 if (info->IsTemp()) {
483 info->MarkInUse();
484 }
485 }
buzbee30adc732014-05-09 15:10:18 -0700486 } else {
487 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
488 ClobberSReg(s_reg);
489 if (wide) {
490 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700491 }
492 }
493 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494}
495
buzbee2700f7e2014-03-07 09:46:20 -0800496void Mir2Lir::FreeTemp(RegStorage reg) {
497 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700498 FreeTemp(reg.GetLow());
499 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800500 } else {
buzbee091cc402014-03-31 10:14:40 -0700501 RegisterInfo* p = GetRegInfo(reg);
502 if (p->IsTemp()) {
503 p->MarkFree();
504 p->SetIsWide(false);
505 p->SetPartner(reg);
506 }
buzbee2700f7e2014-03-07 09:46:20 -0800507 }
508}
509
buzbee082833c2014-05-17 23:16:26 -0700510void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
511 DCHECK(rl_keep.wide);
512 DCHECK(rl_free.wide);
513 int free_low = rl_free.reg.GetLowReg();
514 int free_high = rl_free.reg.GetHighReg();
515 int keep_low = rl_keep.reg.GetLowReg();
516 int keep_high = rl_keep.reg.GetHighReg();
517 if ((free_low != keep_low) && (free_low != keep_high) &&
518 (free_high != keep_low) && (free_high != keep_high)) {
519 // No overlap, free both
520 FreeTemp(rl_free.reg);
521 }
522}
523
buzbee262b2992014-03-27 11:22:43 -0700524bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700525 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800526 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700527 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
528 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700529 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700530 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800531 } else {
buzbee091cc402014-03-31 10:14:40 -0700532 RegisterInfo* p = GetRegInfo(reg);
533 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800534 }
buzbee091cc402014-03-31 10:14:40 -0700535 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536}
537
buzbee262b2992014-03-27 11:22:43 -0700538bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700539 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800540 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700541 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
542 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
543 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800544 } else {
buzbee091cc402014-03-31 10:14:40 -0700545 RegisterInfo* p = GetRegInfo(reg);
546 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800547 }
buzbee091cc402014-03-31 10:14:40 -0700548 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549}
550
buzbee262b2992014-03-27 11:22:43 -0700551bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700552 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800553 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700554 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
555 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
556 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800557 } else {
buzbee091cc402014-03-31 10:14:40 -0700558 RegisterInfo* p = GetRegInfo(reg);
559 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800560 }
buzbee091cc402014-03-31 10:14:40 -0700561 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562}
563
buzbee2700f7e2014-03-07 09:46:20 -0800564bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700565 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800566 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700567 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
568 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
569 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800570 } else {
buzbee091cc402014-03-31 10:14:40 -0700571 RegisterInfo* p = GetRegInfo(reg);
572 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800573 }
buzbee091cc402014-03-31 10:14:40 -0700574 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800575}
576
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577/*
578 * Similar to AllocTemp(), but forces the allocation of a specific
579 * register. No check is made to see if the register was previously
580 * allocated. Use with caution.
581 */
buzbee2700f7e2014-03-07 09:46:20 -0800582void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700583 DCHECK(IsTemp(reg));
584 if (reg.IsPair()) {
585 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
586 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
587 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700588 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700589 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700590 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700591 } else {
592 RegisterInfo* p = GetRegInfo(reg);
593 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700594 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700595 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596}
597
buzbee2700f7e2014-03-07 09:46:20 -0800598void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700599 if (reg.IsPair()) {
600 GetRegInfo(reg.GetLow())->ResetDefBody();
601 GetRegInfo(reg.GetHigh())->ResetDefBody();
602 } else {
603 GetRegInfo(reg)->ResetDefBody();
604 }
buzbee2700f7e2014-03-07 09:46:20 -0800605}
606
buzbee091cc402014-03-31 10:14:40 -0700607void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
608 RegisterInfo* info = nullptr;
609 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
610 if (IsTemp(rs)) {
611 info = GetRegInfo(reg);
612 }
613 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
614 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
615 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700617 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 break;
buzbee091cc402014-03-31 10:14:40 -0700619 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 }
621 }
622}
623
624/*
625 * Mark the beginning and end LIR of a def sequence. Note that
626 * on entry start points to the LIR prior to the beginning of the
627 * sequence.
628 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700629void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 DCHECK(!rl.wide);
631 DCHECK(start && start->next);
632 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700633 RegisterInfo* p = GetRegInfo(rl.reg);
634 p->SetDefStart(start->next);
635 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636}
637
638/*
639 * Mark the beginning and end LIR of a def sequence. Note that
640 * on entry start points to the LIR prior to the beginning of the
641 * sequence.
642 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700643void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 DCHECK(rl.wide);
645 DCHECK(start && start->next);
646 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700647 RegisterInfo* p;
648 if (rl.reg.IsPair()) {
649 p = GetRegInfo(rl.reg.GetLow());
650 ResetDef(rl.reg.GetHigh()); // Only track low of pair
651 } else {
652 p = GetRegInfo(rl.reg);
653 }
654 p->SetDefStart(start->next);
655 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656}
657
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700658RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 DCHECK(rl.wide);
660 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700661 if (rl.reg.IsPair()) {
662 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
663 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
664 if (info_lo->IsTemp()) {
665 info_lo->SetIsWide(false);
666 info_lo->ResetDefBody();
667 }
668 if (info_hi->IsTemp()) {
669 info_hi->SetIsWide(false);
670 info_hi->ResetDefBody();
671 }
672 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700673 } else {
674 /*
675 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
676 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
677 * this code. Will probably want to make this a virtual function.
678 */
679 // Can't narrow 64-bit register. Clobber.
680 if (GetRegInfo(rl.reg)->IsTemp()) {
681 Clobber(rl.reg);
682 FreeTemp(rl.reg);
683 }
684 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 }
687 rl.wide = false;
688 return rl;
689}
690
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700691void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700693 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
694 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 }
buzbee091cc402014-03-31 10:14:40 -0700696 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697}
698
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700699void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700701 // If pair, only track low reg of pair.
702 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
703 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
704 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 }
buzbee091cc402014-03-31 10:14:40 -0700706 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707}
708
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700709void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700710 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
711 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
712 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 }
buzbee091cc402014-03-31 10:14:40 -0700714 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
715 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
716 info->ResetDefBody();
717 }
718 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
719 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
720 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700721 }
722}
723
buzbeeba574512014-05-12 15:13:16 -0700724void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700725 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
726 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700727 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700728 }
729}
730
731void Mir2Lir::FlushRegWide(RegStorage reg) {
732 if (reg.IsPair()) {
733 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
734 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
735 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
736 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
737 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
738 if (!(info1->IsTemp() && info2->IsTemp())) {
739 /* Should not happen. If it does, there's a problem in eval_loc */
740 LOG(FATAL) << "Long half-temp, half-promoted";
741 }
742
743 info1->SetIsDirty(false);
744 info2->SetIsDirty(false);
745 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
746 info1 = info2;
747 }
748 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100749 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700750 }
751 } else {
752 RegisterInfo* info = GetRegInfo(reg);
753 if (info->IsLive() && info->IsDirty()) {
754 info->SetIsDirty(false);
755 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100756 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700757 }
758 }
759}
760
761void Mir2Lir::FlushReg(RegStorage reg) {
762 DCHECK(!reg.IsPair());
763 RegisterInfo* info = GetRegInfo(reg);
764 if (info->IsLive() && info->IsDirty()) {
765 info->SetIsDirty(false);
766 int v_reg = mir_graph_->SRegToVReg(info->SReg());
767 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 }
769}
770
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800771void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700772 if (info->IsWide()) {
773 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800774 } else {
buzbee091cc402014-03-31 10:14:40 -0700775 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700776 }
777}
778
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700779void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700780 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
781 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700782 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700783 FlushSpecificReg(info);
784 }
buzbee30adc732014-05-09 15:10:18 -0700785 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700786 info->SetIsWide(false);
787 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788}
789
790
buzbee2700f7e2014-03-07 09:46:20 -0800791bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 if (reg_class == kAnyReg) {
793 return true;
794 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700795 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 } else {
buzbee091cc402014-03-31 10:14:40 -0700797 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 }
799}
800
buzbee091cc402014-03-31 10:14:40 -0700801void Mir2Lir::MarkLive(RegLocation loc) {
802 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700803 if (!IsTemp(reg)) {
804 return;
805 }
buzbee091cc402014-03-31 10:14:40 -0700806 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700807 if (s_reg == INVALID_SREG) {
808 // Can't be live if no associated sreg.
809 if (reg.IsPair()) {
810 GetRegInfo(reg.GetLow())->MarkDead();
811 GetRegInfo(reg.GetHigh())->MarkDead();
812 } else {
813 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700814 }
buzbee082833c2014-05-17 23:16:26 -0700815 } else {
816 if (reg.IsPair()) {
817 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
818 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
819 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
820 (info_hi->SReg() == s_reg)) {
821 return; // Already live.
822 }
823 ClobberSReg(s_reg);
824 ClobberSReg(s_reg + 1);
825 info_lo->MarkLive(s_reg);
826 info_hi->MarkLive(s_reg + 1);
827 } else {
828 RegisterInfo* info = GetRegInfo(reg);
829 if (info->IsLive() && (info->SReg() == s_reg)) {
830 return; // Already live.
831 }
832 ClobberSReg(s_reg);
833 if (loc.wide) {
834 ClobberSReg(s_reg + 1);
835 }
836 info->MarkLive(s_reg);
837 }
838 if (loc.wide) {
839 MarkWide(reg);
840 } else {
841 MarkNarrow(reg);
842 }
buzbee091cc402014-03-31 10:14:40 -0700843 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844}
845
buzbee2700f7e2014-03-07 09:46:20 -0800846void Mir2Lir::MarkTemp(RegStorage reg) {
847 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700849 tempreg_info_.Insert(info);
850 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851}
852
buzbee2700f7e2014-03-07 09:46:20 -0800853void Mir2Lir::UnmarkTemp(RegStorage reg) {
854 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700855 RegisterInfo* info = GetRegInfo(reg);
856 tempreg_info_.Delete(info);
857 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800858}
859
buzbee091cc402014-03-31 10:14:40 -0700860void Mir2Lir::MarkWide(RegStorage reg) {
861 if (reg.IsPair()) {
862 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
863 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700864 // Unpair any old partners.
865 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
866 GetRegInfo(info_lo->Partner())->SetIsWide(false);
867 }
868 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
869 GetRegInfo(info_hi->Partner())->SetIsWide(false);
870 }
buzbee091cc402014-03-31 10:14:40 -0700871 info_lo->SetIsWide(true);
872 info_hi->SetIsWide(true);
873 info_lo->SetPartner(reg.GetHigh());
874 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800875 } else {
buzbee091cc402014-03-31 10:14:40 -0700876 RegisterInfo* info = GetRegInfo(reg);
877 info->SetIsWide(true);
878 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 }
880}
881
buzbee082833c2014-05-17 23:16:26 -0700882void Mir2Lir::MarkNarrow(RegStorage reg) {
883 DCHECK(!reg.IsPair());
884 RegisterInfo* info = GetRegInfo(reg);
885 info->SetIsWide(false);
886 info->SetPartner(reg);
887}
888
buzbee091cc402014-03-31 10:14:40 -0700889void Mir2Lir::MarkClean(RegLocation loc) {
890 if (loc.reg.IsPair()) {
891 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
892 info->SetIsDirty(false);
893 info = GetRegInfo(loc.reg.GetHigh());
894 info->SetIsDirty(false);
895 } else {
896 RegisterInfo* info = GetRegInfo(loc.reg);
897 info->SetIsDirty(false);
898 }
899}
900
901// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700902void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700903 if (loc.home) {
904 // If already home, can't be dirty
905 return;
906 }
buzbee091cc402014-03-31 10:14:40 -0700907 if (loc.reg.IsPair()) {
908 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
909 info->SetIsDirty(true);
910 info = GetRegInfo(loc.reg.GetHigh());
911 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800912 } else {
buzbee091cc402014-03-31 10:14:40 -0700913 RegisterInfo* info = GetRegInfo(loc.reg);
914 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915 }
916}
917
buzbee2700f7e2014-03-07 09:46:20 -0800918void Mir2Lir::MarkInUse(RegStorage reg) {
919 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700920 GetRegInfo(reg.GetLow())->MarkInUse();
921 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800922 } else {
buzbee091cc402014-03-31 10:14:40 -0700923 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800924 }
925}
926
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700927bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700928 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700929 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700930 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
931 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700932 int my_sreg = info->SReg();
933 RegStorage partner_reg = info->Partner();
934 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700935 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700936 DCHECK(partner->IsWide());
937 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700938 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700939 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700940 if (my_sreg == INVALID_SREG) {
941 DCHECK_EQ(partner_sreg, INVALID_SREG);
942 } else {
943 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700944 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700945 }
946 }
buzbee082833c2014-05-17 23:16:26 -0700947 if (info->Master() != info) {
948 // Aliased.
949 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
950 // If I'm live, master should not be live, but should show liveness in alias set.
951 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
952 DCHECK(!info->Master()->IsDead());
953 } else if (!info->IsDead()) {
954 // If I'm not live, but there is liveness in the set master must be live.
955 DCHECK_EQ(info->SReg(), INVALID_SREG);
956 DCHECK(info->Master()->IsLive());
957 }
958 }
959 if (info->IsAliased()) {
960 // Has child aliases.
961 DCHECK_EQ(info->Master(), info);
962 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
963 // Master live, no child should be dead - all should show liveness in set.
964 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
965 DCHECK(!p->IsDead());
966 DCHECK_EQ(p->SReg(), INVALID_SREG);
967 }
968 } else if (!info->IsDead()) {
969 // Master not live, one or more aliases must be.
970 bool live_alias = false;
971 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
972 live_alias |= p->IsLive();
973 }
974 DCHECK(live_alias);
975 }
976 }
977 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
978 // If not fully live, should have INVALID_SREG and def's should be null.
979 DCHECK(info->DefStart() == nullptr);
980 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700981 }
982 }
983 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984}
985
986/*
987 * Return an updated location record with current in-register status.
988 * If the value lives in live temps, reflect that fact. No code
989 * is generated. If the live value is part of an older pair,
990 * clobber both low and high.
991 * TUNING: clobbering both is a bit heavy-handed, but the alternative
992 * is a bit complex when dealing with FP regs. Examine code to see
993 * if it's worthwhile trying to be more clever here.
994 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700995RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 DCHECK(!loc.wide);
997 DCHECK(CheckCorePoolSanity());
998 if (loc.location != kLocPhysReg) {
999 DCHECK((loc.location == kLocDalvikFrame) ||
1000 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001001 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
1002 if (reg.Valid()) {
1003 bool match = true;
1004 RegisterInfo* info = GetRegInfo(reg);
1005 match &= !reg.IsPair();
1006 match &= !info->IsWide();
1007 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001009 loc.reg = reg;
1010 } else {
1011 Clobber(reg);
1012 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
1014 }
1015 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 return loc;
1017}
1018
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001019RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 DCHECK(loc.wide);
1021 DCHECK(CheckCorePoolSanity());
1022 if (loc.location != kLocPhysReg) {
1023 DCHECK((loc.location == kLocDalvikFrame) ||
1024 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001025 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1026 if (reg.Valid()) {
1027 bool match = true;
1028 if (reg.IsPair()) {
1029 // If we've got a register pair, make sure that it was last used as the same pair.
1030 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1031 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1032 match &= info_lo->IsWide();
1033 match &= info_hi->IsWide();
1034 match &= (info_lo->Partner() == info_hi->GetReg());
1035 match &= (info_hi->Partner() == info_lo->GetReg());
1036 } else {
1037 RegisterInfo* info = GetRegInfo(reg);
1038 match &= info->IsWide();
1039 match &= (info->GetReg() == info->Partner());
1040 }
1041 if (match) {
1042 loc.location = kLocPhysReg;
1043 loc.reg = reg;
1044 } else {
1045 Clobber(reg);
1046 FreeTemp(reg);
1047 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 }
1049 }
1050 return loc;
1051}
1052
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001054RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 if (loc.wide)
1056 return UpdateLocWide(loc);
1057 else
1058 return UpdateLoc(loc);
1059}
1060
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001061RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001062 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063
1064 loc = UpdateLocWide(loc);
1065
1066 /* If already in registers, we can assume proper form. Right reg class? */
1067 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001068 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001069 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001070 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001071 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001072 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001073 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001074 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001075 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001076 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 }
1078 return loc;
1079 }
1080
1081 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1082 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1083
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001084 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001085 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 if (update) {
1088 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001089 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 return loc;
1092}
1093
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001094RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001095 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001097 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098
1099 loc = UpdateLoc(loc);
1100
1101 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001102 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001103 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001104 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001105 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001106 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001107 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001108 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001109 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 }
1111 return loc;
1112 }
1113
1114 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1115
buzbee2700f7e2014-03-07 09:46:20 -08001116 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117
1118 if (update) {
1119 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001120 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 }
1122 return loc;
1123}
1124
1125/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001126void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001127 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1128 RegLocation loc = mir_graph_->reg_location_[i];
1129 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1130 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001131 if (loc.fp) {
1132 if (loc.wide) {
1133 // Treat doubles as a unit, using upper half of fp_counts array.
1134 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1135 i++;
1136 } else {
1137 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1138 }
1139 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1141 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142 }
1143}
1144
1145/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001146static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1148 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001149 // Note that we fall back to sorting on reg so we get stable output
1150 // on differing qsort implementations (such as on host and target or
1151 // between local host and build servers).
1152 return (op1->count == op2->count)
1153 ? (op1->s_reg - op2->s_reg)
1154 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155}
1156
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001157void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 LOG(INFO) << msg;
1159 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001160 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1161 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1162 } else {
1163 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1164 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 }
1166}
1167
1168/*
1169 * Note: some portions of this code required even if the kPromoteRegs
1170 * optimization is disabled.
1171 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001172void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001174 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001176 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1177 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001178 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179
1180 // Allow target code to add any special registers
1181 AdjustSpillMask();
1182
1183 /*
1184 * Simple register promotion. Just do a static count of the uses
1185 * of Dalvik registers. Note that we examine the SSA names, but
1186 * count based on original Dalvik register name. Count refs
1187 * separately based on type in order to give allocation
1188 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001189 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001190 * reg.
1191 * TUNING: replace with linear scan once we have the ability
1192 * to describe register live ranges for GC.
1193 */
1194 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001195 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001196 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001198 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001199 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 // Set ssa names for original Dalvik registers
1201 for (int i = 0; i < dalvik_regs; i++) {
1202 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1203 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001204
1205 // Set ssa names for compiler temporaries
1206 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1207 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1208 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1209 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1210 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001211 }
1212
1213 // Duplicate in upper half to represent possible fp double starting sregs.
1214 for (int i = 0; i < num_regs; i++) {
1215 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 }
1217
1218 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001219 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221
1222 // Sort the count arrays
1223 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001224 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225
1226 if (cu_->verbose) {
1227 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001228 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 }
1230
1231 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1232 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001233 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1234 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1235 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1236 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1237 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1238 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1239 // Ignore result - if can't alloc double may still be able to alloc singles.
1240 AllocPreservedDouble(low_sreg);
1241 }
1242 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001243 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1244 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001245 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 }
1247 }
1248 }
1249
1250 // Promote core regs
1251 for (int i = 0; (i < num_regs) &&
1252 (core_regs[i].count >= promotion_threshold); i++) {
1253 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1254 if (promotion_map_[p_map_idx].core_location !=
1255 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001256 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1257 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 break; // No more left
1259 }
1260 }
1261 }
1262 }
1263
1264 // Now, update SSA names to new home locations
1265 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1266 RegLocation *curr = &mir_graph_->reg_location_[i];
1267 int p_map_idx = SRegToPMap(curr->s_reg_low);
1268 if (!curr->wide) {
1269 if (curr->fp) {
1270 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1271 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001272 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001273 curr->home = true;
1274 }
1275 } else {
1276 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1277 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001278 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 curr->home = true;
1280 }
1281 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 } else {
1283 if (curr->high_word) {
1284 continue;
1285 }
1286 if (curr->fp) {
1287 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001288 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001289 int low_reg = promotion_map_[p_map_idx].FpReg;
1290 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1291 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001292 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1294 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001295 if (cu_->instruction_set == kThumb2) {
1296 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1297 } else {
1298 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1299 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 curr->home = true;
1301 }
1302 }
1303 } else {
1304 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1305 && (promotion_map_[p_map_idx+1].core_location ==
1306 kLocPhysReg)) {
1307 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001308 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1309 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 curr->home = true;
1311 }
1312 }
1313 }
1314 }
1315 if (cu_->verbose) {
1316 DumpPromotionMap();
1317 }
1318}
1319
1320/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001321int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001323 fp_spill_mask_, frame_size_, v_reg,
1324 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325}
1326
1327/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001328int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1330}
1331
1332/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001333RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 RegLocation gpr_res = LocCReturnWide();
1335 RegLocation fpr_res = LocCReturnDouble();
1336 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee082833c2014-05-17 23:16:26 -07001337 Clobber(res.reg);
1338 LockTemp(res.reg);
1339 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 return res;
1341}
1342
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001343RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 RegLocation gpr_res = LocCReturn();
1345 RegLocation fpr_res = LocCReturnFloat();
1346 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001347 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001349 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 } else {
buzbee091cc402014-03-31 10:14:40 -07001351 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 }
1353 return res;
1354}
1355
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001356void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 DoPromotion();
1358
1359 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1360 LOG(INFO) << "After Promotion";
1361 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1362 }
1363
1364 /* Set the frame size */
1365 frame_size_ = ComputeFrameSize();
1366}
1367
1368/*
1369 * Get the "real" sreg number associated with an s_reg slot. In general,
1370 * s_reg values passed through codegen are the SSA names created by
1371 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1372 * array. However, renaming is accomplished by simply replacing RegLocation
1373 * entries in the reglocation[] array. Therefore, when location
1374 * records for operands are first created, we need to ask the locRecord
1375 * identified by the dataflow pass what it's new name is.
1376 */
1377int Mir2Lir::GetSRegHi(int lowSreg) {
1378 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1379}
1380
buzbee091cc402014-03-31 10:14:40 -07001381bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001382 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001383 return true;
1384}
1385
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386} // namespace art