blob: 195da0dad24b12afa6b4294c0571b7f529e697d7 [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
Vladimir Marko8dea81c2014-06-06 14:50:36 +010041Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, const ResourceMask& 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,
Vladimir Marko089142c2014-06-05 10:57:05 +010059 const ArrayRef<const RegStorage>& core_regs,
60 const ArrayRef<const RegStorage>& core64_regs,
61 const ArrayRef<const RegStorage>& sp_regs,
62 const ArrayRef<const RegStorage>& dp_regs,
63 const ArrayRef<const RegStorage>& reserved_regs,
64 const ArrayRef<const RegStorage>& reserved64_regs,
65 const ArrayRef<const RegStorage>& core_temps,
66 const ArrayRef<const RegStorage>& core64_temps,
67 const ArrayRef<const RegStorage>& sp_temps,
68 const ArrayRef<const 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.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010085 for (const RegStorage& reg : core_regs) {
buzbee091cc402014-03-31 10:14:40 -070086 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
87 m2l_->reginfo_map_.Put(reg.GetReg(), info);
88 core_regs_.Insert(info);
89 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +010090 for (const RegStorage& reg : core64_regs) {
buzbeeb01bf152014-05-13 15:59:07 -070091 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
92 m2l_->reginfo_map_.Put(reg.GetReg(), info);
93 core64_regs_.Insert(info);
94 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +010095 for (const RegStorage& reg : sp_regs) {
buzbee091cc402014-03-31 10:14:40 -070096 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
97 m2l_->reginfo_map_.Put(reg.GetReg(), info);
98 sp_regs_.Insert(info);
99 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100100 for (const RegStorage& reg : dp_regs) {
buzbee091cc402014-03-31 10:14:40 -0700101 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.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100129 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), kEncodeNone);
buzbee091cc402014-03-31 10:14:40 -0700130 m2l_->reginfo_map_.Put(RegStorage::InvalidReg().GetReg(), invalid_reg);
buzbeea0cd2d72014-06-01 09:33:49 -0700131
132 // Existence of core64 registers implies wide references.
133 if (core64_regs_.Size() != 0) {
134 ref_regs_ = &core64_regs_;
135 next_ref_reg_ = &next_core64_reg_;
136 } else {
137 ref_regs_ = &core_regs_;
138 next_ref_reg_ = &next_core_reg_;
139 }
buzbee091cc402014-03-31 10:14:40 -0700140}
141
142void Mir2Lir::DumpRegPool(GrowableArray<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 LOG(INFO) << "================================================";
buzbee091cc402014-03-31 10:14:40 -0700144 GrowableArray<RegisterInfo*>::Iterator it(regs);
145 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700147 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
148 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
149 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
150 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
152 LOG(INFO) << "================================================";
153}
154
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700155void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700156 DumpRegPool(&reg_pool_->core_regs_);
buzbeea0cd2d72014-06-01 09:33:49 -0700157 DumpRegPool(&reg_pool_->core64_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158}
159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700161 DumpRegPool(&reg_pool_->sp_regs_);
162 DumpRegPool(&reg_pool_->dp_regs_);
163}
164
165void Mir2Lir::DumpRegPools() {
166 LOG(INFO) << "Core registers";
167 DumpCoreRegPool();
168 LOG(INFO) << "FP registers";
169 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170}
171
buzbee2700f7e2014-03-07 09:46:20 -0800172void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700173 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700174 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700175 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700176 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700177 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800178 } else {
buzbee30adc732014-05-09 15:10:18 -0700179 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700180 if (info->IsTemp() && !info->IsDead()) {
buzbeeb5860fb2014-06-21 15:31:01 -0700181 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700182 ClobberBody(GetRegInfo(info->Partner()));
183 }
buzbeeba574512014-05-12 15:13:16 -0700184 ClobberBody(info);
185 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700186 ClobberAliases(info, info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700187 } else {
188 RegisterInfo* master = info->Master();
189 if (info != master) {
190 ClobberBody(info->Master());
buzbee642fe342014-05-23 16:04:08 -0700191 ClobberAliases(info->Master(), info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700192 }
193 }
buzbee30adc732014-05-09 15:10:18 -0700194 }
buzbee2700f7e2014-03-07 09:46:20 -0800195 }
196}
197
buzbee642fe342014-05-23 16:04:08 -0700198void Mir2Lir::ClobberAliases(RegisterInfo* info, uint32_t clobber_mask) {
buzbeeba574512014-05-12 15:13:16 -0700199 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
200 alias = alias->GetAliasChain()) {
201 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee642fe342014-05-23 16:04:08 -0700202 // Only clobber if we have overlap.
203 if ((alias->StorageMask() & clobber_mask) != 0) {
204 ClobberBody(alias);
205 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 }
207}
208
209/*
210 * Break the association between a Dalvik vreg and a physical temp register of either register
211 * class.
212 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
213 * in the register utilities, is is also used by code gen routines to work around a deficiency in
214 * local register allocation, which fails to distinguish between the "in" and "out" identities
215 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
216 * is used both as the source and destination register of an operation in which the type
217 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
218 * addressed.
219 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700220void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700221 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700222 if (kIsDebugBuild && s_reg == live_sreg_) {
223 live_sreg_ = INVALID_SREG;
224 }
225 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
226 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
227 if (info->SReg() == s_reg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700228 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700229 // Dealing with a pair - clobber the other half.
230 DCHECK(!info->IsAliased());
231 ClobberBody(GetRegInfo(info->Partner()));
232 }
buzbeeba574512014-05-12 15:13:16 -0700233 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700234 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700235 ClobberAliases(info, info->StorageMask());
buzbee30adc732014-05-09 15:10:18 -0700236 }
buzbee091cc402014-03-31 10:14:40 -0700237 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 }
239 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240}
241
242/*
243 * SSA names associated with the initial definitions of Dalvik
244 * registers are the same as the Dalvik register number (and
245 * thus take the same position in the promotion_map. However,
246 * the special Method* and compiler temp resisters use negative
247 * v_reg numbers to distinguish them and can have an arbitrary
248 * ssa name (above the last original Dalvik register). This function
249 * maps SSA names to positions in the promotion_map array.
250 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700251int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
253 DCHECK_GE(s_reg, 0);
254 int v_reg = mir_graph_->SRegToVReg(s_reg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700255 return v_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256}
257
buzbee091cc402014-03-31 10:14:40 -0700258// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800259void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 int p_map_idx = SRegToPMap(s_reg);
261 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700262 int reg_num = reg.GetRegNum();
263 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800264 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800266 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 num_core_spills_++;
268 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800269 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270}
271
buzbee091cc402014-03-31 10:14:40 -0700272/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800273RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
274 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700275 /*
276 * Note: it really doesn't matter much whether we allocate from the core or core64
277 * pool for 64-bit targets - but for some targets it does matter whether allocations
278 * happens from the single or double pool. This entire section of code could stand
279 * a good refactoring.
280 */
buzbee091cc402014-03-31 10:14:40 -0700281 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
282 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
283 if (!info->IsTemp() && !info->InUse()) {
284 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 RecordCorePromotion(res, s_reg);
286 break;
287 }
288 }
289 return res;
290}
291
buzbeeb5860fb2014-06-21 15:31:01 -0700292void Mir2Lir::RecordFpPromotion(RegStorage reg, int s_reg) {
293 DCHECK_NE(cu_->instruction_set, kThumb2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 int p_map_idx = SRegToPMap(s_reg);
295 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700296 int reg_num = reg.GetRegNum();
buzbee091cc402014-03-31 10:14:40 -0700297 GetRegInfo(reg)->MarkInUse();
buzbeeb5860fb2014-06-21 15:31:01 -0700298 fp_spill_mask_ |= (1 << reg_num);
299 // Include reg for later sort
300 fp_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
301 num_fp_spills_++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbeeb5860fb2014-06-21 15:31:01 -0700303 promotion_map_[p_map_idx].fp_reg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304}
305
buzbeeb5860fb2014-06-21 15:31:01 -0700306// Reserve a callee-save floating point.
307RegStorage Mir2Lir::AllocPreservedFpReg(int s_reg) {
308 /*
309 * For targets other than Thumb2, it doesn't matter whether we allocate from
310 * the sp_regs_ or dp_regs_ pool. Some refactoring is in order here.
311 */
312 DCHECK_NE(cu_->instruction_set, kThumb2);
buzbee2700f7e2014-03-07 09:46:20 -0800313 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700314 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
315 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
316 if (!info->IsTemp() && !info->InUse()) {
317 res = info->GetReg();
buzbeeb5860fb2014-06-21 15:31:01 -0700318 RecordFpPromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 break;
320 }
321 }
322 return res;
323}
324
buzbeeb5860fb2014-06-21 15:31:01 -0700325// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
buzbee2700f7e2014-03-07 09:46:20 -0800326RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
327 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700328 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedDouble";
329 return res;
330}
331
332// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
333RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
334 RegStorage res;
335 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedSingle";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 return res;
337}
338
buzbee091cc402014-03-31 10:14:40 -0700339
340RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
341 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700343 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 if (next >= num_regs)
345 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700346 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700347 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700348 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee88a6b412014-08-25 09:34:03 -0700349 // If it's wide, split it up.
350 if (info->IsWide()) {
351 // If the pair was associated with a wide value, unmark the partner as well.
352 if (info->SReg() != INVALID_SREG) {
353 RegisterInfo* partner = GetRegInfo(info->Partner());
354 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
355 DCHECK(partner->IsWide());
356 partner->SetIsWide(false);
357 }
358 info->SetIsWide(false);
359 }
buzbee091cc402014-03-31 10:14:40 -0700360 Clobber(info->GetReg());
361 info->MarkInUse();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700363 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 }
365 next++;
366 }
367 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700368 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700369 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 if (next >= num_regs)
371 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700372 RegisterInfo* info = regs.Get(next);
373 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700374 // Got one. Kill it.
375 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700376 Clobber(info->GetReg());
377 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700378 if (info->IsWide()) {
379 RegisterInfo* partner = GetRegInfo(info->Partner());
380 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
381 DCHECK(partner->IsWide());
382 info->SetIsWide(false);
383 partner->SetIsWide(false);
384 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700386 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 }
388 next++;
389 }
390 if (required) {
391 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700392 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 LOG(FATAL) << "No free temp registers";
394 }
buzbee2700f7e2014-03-07 09:46:20 -0800395 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396}
397
Serguei Katkov9ee45192014-07-17 14:39:03 +0700398RegStorage Mir2Lir::AllocTemp(bool required) {
399 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, required);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400}
401
Serguei Katkov9ee45192014-07-17 14:39:03 +0700402RegStorage Mir2Lir::AllocTempWide(bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700403 RegStorage res;
404 if (reg_pool_->core64_regs_.Size() != 0) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700405 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, required);
buzbeeb01bf152014-05-13 15:59:07 -0700406 } else {
407 RegStorage low_reg = AllocTemp();
408 RegStorage high_reg = AllocTemp();
409 res = RegStorage::MakeRegPair(low_reg, high_reg);
410 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700411 if (required) {
412 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kIgnoreRef, FPCheck::kCheckNotFP);
413 }
buzbeeb01bf152014-05-13 15:59:07 -0700414 return res;
415}
416
Serguei Katkov9ee45192014-07-17 14:39:03 +0700417RegStorage Mir2Lir::AllocTempRef(bool required) {
418 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, required);
419 if (required) {
420 DCHECK(!res.IsPair());
421 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
422 }
buzbeea0cd2d72014-06-01 09:33:49 -0700423 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100424}
425
Serguei Katkov9ee45192014-07-17 14:39:03 +0700426RegStorage Mir2Lir::AllocTempSingle(bool required) {
427 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, required);
428 if (required) {
429 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
430 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
431 }
buzbee091cc402014-03-31 10:14:40 -0700432 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433}
434
Serguei Katkov9ee45192014-07-17 14:39:03 +0700435RegStorage Mir2Lir::AllocTempDouble(bool required) {
436 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, required);
437 if (required) {
438 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
439 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
440 }
buzbee091cc402014-03-31 10:14:40 -0700441 return res;
442}
443
Serguei Katkov9ee45192014-07-17 14:39:03 +0700444RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class, bool required) {
buzbeea0cd2d72014-06-01 09:33:49 -0700445 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700446 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700447 return AllocTempDouble(required);
buzbeeb01bf152014-05-13 15:59:07 -0700448 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700449 return AllocTempWide(required);
buzbeeb01bf152014-05-13 15:59:07 -0700450}
451
Serguei Katkov9ee45192014-07-17 14:39:03 +0700452RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class, bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700453 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700454 return AllocTempSingle(required);
buzbeea0cd2d72014-06-01 09:33:49 -0700455 } else if (reg_class == kRefReg) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700456 return AllocTempRef(required);
buzbeeb01bf152014-05-13 15:59:07 -0700457 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700458 return AllocTemp(required);
buzbeeb01bf152014-05-13 15:59:07 -0700459}
460
buzbee091cc402014-03-31 10:14:40 -0700461RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
462 RegStorage res;
463 GrowableArray<RegisterInfo*>::Iterator it(&regs);
464 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
465 if ((info->SReg() == s_reg) && info->IsLive()) {
466 res = info->GetReg();
467 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 }
469 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 return res;
471}
472
buzbee091cc402014-03-31 10:14:40 -0700473RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
474 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700475 if (reg_class == kRefReg) {
476 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700477 CheckRegStorage(reg, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700478 }
479 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700480 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 }
buzbee091cc402014-03-31 10:14:40 -0700482 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700483 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700484 reg = FindLiveReg(wide || reg_class == kRefReg ? reg_pool_->core64_regs_ :
485 reg_pool_->core_regs_, s_reg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100486 } else {
487 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
488 }
buzbee091cc402014-03-31 10:14:40 -0700489 }
490 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700491 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700492 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700493 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
494 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700495 reg = RegStorage::MakeRegPair(reg, high_reg);
496 MarkWide(reg);
497 } else {
buzbee30adc732014-05-09 15:10:18 -0700498 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700499 reg = RegStorage::InvalidReg();
500 }
501 }
buzbee30adc732014-05-09 15:10:18 -0700502 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
503 // Width mismatch - don't try to reuse.
504 reg = RegStorage::InvalidReg();
505 }
506 }
507 if (reg.Valid()) {
508 if (reg.IsPair()) {
509 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
510 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
511 if (info_low->IsTemp()) {
512 info_low->MarkInUse();
513 }
514 if (info_high->IsTemp()) {
515 info_high->MarkInUse();
516 }
517 } else {
buzbee091cc402014-03-31 10:14:40 -0700518 RegisterInfo* info = GetRegInfo(reg);
519 if (info->IsTemp()) {
520 info->MarkInUse();
521 }
522 }
buzbee30adc732014-05-09 15:10:18 -0700523 } else {
524 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
525 ClobberSReg(s_reg);
526 if (wide) {
527 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700528 }
529 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700530 CheckRegStorage(reg, WidenessCheck::kIgnoreWide,
531 reg_class == kRefReg ? RefCheck::kCheckRef : RefCheck::kIgnoreRef,
532 FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700533 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534}
535
buzbee2700f7e2014-03-07 09:46:20 -0800536void Mir2Lir::FreeTemp(RegStorage reg) {
537 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700538 FreeTemp(reg.GetLow());
539 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800540 } else {
buzbee091cc402014-03-31 10:14:40 -0700541 RegisterInfo* p = GetRegInfo(reg);
542 if (p->IsTemp()) {
543 p->MarkFree();
544 p->SetIsWide(false);
545 p->SetPartner(reg);
546 }
buzbee2700f7e2014-03-07 09:46:20 -0800547 }
548}
549
buzbee082833c2014-05-17 23:16:26 -0700550void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
551 DCHECK(rl_keep.wide);
552 DCHECK(rl_free.wide);
553 int free_low = rl_free.reg.GetLowReg();
554 int free_high = rl_free.reg.GetHighReg();
555 int keep_low = rl_keep.reg.GetLowReg();
556 int keep_high = rl_keep.reg.GetHighReg();
557 if ((free_low != keep_low) && (free_low != keep_high) &&
558 (free_high != keep_low) && (free_high != keep_high)) {
559 // No overlap, free both
560 FreeTemp(rl_free.reg);
561 }
562}
563
buzbee262b2992014-03-27 11:22:43 -0700564bool Mir2Lir::IsLive(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());
buzbee30adc732014-05-09 15:10:18 -0700569 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700570 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800571 } else {
buzbee091cc402014-03-31 10:14:40 -0700572 RegisterInfo* p = GetRegInfo(reg);
573 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800574 }
buzbee091cc402014-03-31 10:14:40 -0700575 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576}
577
buzbee262b2992014-03-27 11:22:43 -0700578bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700579 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800580 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700581 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
582 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
583 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800584 } else {
buzbee091cc402014-03-31 10:14:40 -0700585 RegisterInfo* p = GetRegInfo(reg);
586 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800587 }
buzbee091cc402014-03-31 10:14:40 -0700588 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700589}
590
buzbee262b2992014-03-27 11:22:43 -0700591bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700592 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800593 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700594 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
595 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
596 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800597 } else {
buzbee091cc402014-03-31 10:14:40 -0700598 RegisterInfo* p = GetRegInfo(reg);
599 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800600 }
buzbee091cc402014-03-31 10:14:40 -0700601 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602}
603
buzbee2700f7e2014-03-07 09:46:20 -0800604bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700605 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800606 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700607 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
608 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
609 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800610 } else {
buzbee091cc402014-03-31 10:14:40 -0700611 RegisterInfo* p = GetRegInfo(reg);
612 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800613 }
buzbee091cc402014-03-31 10:14:40 -0700614 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800615}
616
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617/*
618 * Similar to AllocTemp(), but forces the allocation of a specific
619 * register. No check is made to see if the register was previously
620 * allocated. Use with caution.
621 */
buzbee2700f7e2014-03-07 09:46:20 -0800622void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700623 DCHECK(IsTemp(reg));
624 if (reg.IsPair()) {
625 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
626 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
627 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700628 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700629 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700630 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700631 } else {
632 RegisterInfo* p = GetRegInfo(reg);
633 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700634 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700635 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636}
637
buzbee2700f7e2014-03-07 09:46:20 -0800638void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700639 if (reg.IsPair()) {
640 GetRegInfo(reg.GetLow())->ResetDefBody();
641 GetRegInfo(reg.GetHigh())->ResetDefBody();
642 } else {
643 GetRegInfo(reg)->ResetDefBody();
644 }
buzbee2700f7e2014-03-07 09:46:20 -0800645}
646
buzbee091cc402014-03-31 10:14:40 -0700647void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
648 RegisterInfo* info = nullptr;
649 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
650 if (IsTemp(rs)) {
651 info = GetRegInfo(reg);
652 }
653 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
654 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
655 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700657 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 break;
buzbee091cc402014-03-31 10:14:40 -0700659 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 }
661 }
662}
663
664/*
665 * Mark the beginning and end LIR of a def sequence. Note that
666 * on entry start points to the LIR prior to the beginning of the
667 * sequence.
668 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700669void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 DCHECK(!rl.wide);
671 DCHECK(start && start->next);
672 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700673 RegisterInfo* p = GetRegInfo(rl.reg);
674 p->SetDefStart(start->next);
675 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676}
677
678/*
679 * Mark the beginning and end LIR of a def sequence. Note that
680 * on entry start points to the LIR prior to the beginning of the
681 * sequence.
682 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700683void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 DCHECK(rl.wide);
685 DCHECK(start && start->next);
686 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700687 RegisterInfo* p;
688 if (rl.reg.IsPair()) {
689 p = GetRegInfo(rl.reg.GetLow());
690 ResetDef(rl.reg.GetHigh()); // Only track low of pair
691 } else {
692 p = GetRegInfo(rl.reg);
693 }
694 p->SetDefStart(start->next);
695 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696}
697
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700698void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700700 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
701 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 }
buzbee091cc402014-03-31 10:14:40 -0700703 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704}
705
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700708 // If pair, only track low reg of pair.
709 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
710 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
711 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 }
buzbee091cc402014-03-31 10:14:40 -0700713 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714}
715
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700716void Mir2Lir::ResetDefTracking() {
buzbeea0cd2d72014-06-01 09:33:49 -0700717 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
718 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee091cc402014-03-31 10:14:40 -0700719 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 }
721}
722
buzbeeba574512014-05-12 15:13:16 -0700723void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700724 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
725 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700726 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700727 }
728}
729
730void Mir2Lir::FlushRegWide(RegStorage reg) {
731 if (reg.IsPair()) {
732 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
733 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
734 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
buzbeeb5860fb2014-06-21 15:31:01 -0700735 (info1->Partner().ExactlyEquals(info2->GetReg())) &&
736 (info2->Partner().ExactlyEquals(info1->GetReg())));
buzbee091cc402014-03-31 10:14:40 -0700737 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 Marko8dea81c2014-06-06 14:50:36 +0100749 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700750 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700751 }
752 } else {
753 RegisterInfo* info = GetRegInfo(reg);
754 if (info->IsLive() && info->IsDirty()) {
755 info->SetIsDirty(false);
756 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100757 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700758 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700759 }
760 }
761}
762
763void Mir2Lir::FlushReg(RegStorage reg) {
764 DCHECK(!reg.IsPair());
765 RegisterInfo* info = GetRegInfo(reg);
766 if (info->IsLive() && info->IsDirty()) {
767 info->SetIsDirty(false);
768 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100769 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700770 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 }
772}
773
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800774void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700775 if (info->IsWide()) {
776 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800777 } else {
buzbee091cc402014-03-31 10:14:40 -0700778 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 }
780}
781
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700782void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700783 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
784 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700785 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700786 FlushSpecificReg(info);
787 }
buzbee30adc732014-05-09 15:10:18 -0700788 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700789 info->SetIsWide(false);
790 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791}
792
793
buzbee2700f7e2014-03-07 09:46:20 -0800794bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 if (reg_class == kAnyReg) {
796 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700797 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
798 /*
799 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
800 * with width here - that should be checked at a higher level (if needed).
801 */
buzbee091cc402014-03-31 10:14:40 -0700802 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 } else {
buzbee091cc402014-03-31 10:14:40 -0700804 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 }
806}
807
buzbee091cc402014-03-31 10:14:40 -0700808void Mir2Lir::MarkLive(RegLocation loc) {
809 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700810 if (!IsTemp(reg)) {
811 return;
812 }
buzbee091cc402014-03-31 10:14:40 -0700813 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700814 if (s_reg == INVALID_SREG) {
815 // Can't be live if no associated sreg.
816 if (reg.IsPair()) {
817 GetRegInfo(reg.GetLow())->MarkDead();
818 GetRegInfo(reg.GetHigh())->MarkDead();
819 } else {
820 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700821 }
buzbee082833c2014-05-17 23:16:26 -0700822 } else {
823 if (reg.IsPair()) {
824 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
825 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
826 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
827 (info_hi->SReg() == s_reg)) {
828 return; // Already live.
829 }
830 ClobberSReg(s_reg);
831 ClobberSReg(s_reg + 1);
832 info_lo->MarkLive(s_reg);
833 info_hi->MarkLive(s_reg + 1);
834 } else {
835 RegisterInfo* info = GetRegInfo(reg);
836 if (info->IsLive() && (info->SReg() == s_reg)) {
837 return; // Already live.
838 }
839 ClobberSReg(s_reg);
840 if (loc.wide) {
841 ClobberSReg(s_reg + 1);
842 }
843 info->MarkLive(s_reg);
844 }
845 if (loc.wide) {
846 MarkWide(reg);
847 } else {
848 MarkNarrow(reg);
849 }
buzbee091cc402014-03-31 10:14:40 -0700850 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851}
852
buzbee2700f7e2014-03-07 09:46:20 -0800853void Mir2Lir::MarkTemp(RegStorage reg) {
854 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700856 tempreg_info_.Insert(info);
857 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858}
859
buzbee2700f7e2014-03-07 09:46:20 -0800860void Mir2Lir::UnmarkTemp(RegStorage reg) {
861 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700862 RegisterInfo* info = GetRegInfo(reg);
863 tempreg_info_.Delete(info);
864 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800865}
866
buzbee091cc402014-03-31 10:14:40 -0700867void Mir2Lir::MarkWide(RegStorage reg) {
868 if (reg.IsPair()) {
869 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
870 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700871 // Unpair any old partners.
buzbeeb5860fb2014-06-21 15:31:01 -0700872 if (info_lo->IsWide() && info_lo->Partner().NotExactlyEquals(info_hi->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700873 GetRegInfo(info_lo->Partner())->SetIsWide(false);
874 }
buzbeeb5860fb2014-06-21 15:31:01 -0700875 if (info_hi->IsWide() && info_hi->Partner().NotExactlyEquals(info_lo->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700876 GetRegInfo(info_hi->Partner())->SetIsWide(false);
877 }
buzbee091cc402014-03-31 10:14:40 -0700878 info_lo->SetIsWide(true);
879 info_hi->SetIsWide(true);
880 info_lo->SetPartner(reg.GetHigh());
881 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800882 } else {
buzbee091cc402014-03-31 10:14:40 -0700883 RegisterInfo* info = GetRegInfo(reg);
884 info->SetIsWide(true);
885 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 }
887}
888
buzbee082833c2014-05-17 23:16:26 -0700889void Mir2Lir::MarkNarrow(RegStorage reg) {
890 DCHECK(!reg.IsPair());
891 RegisterInfo* info = GetRegInfo(reg);
892 info->SetIsWide(false);
893 info->SetPartner(reg);
894}
895
buzbee091cc402014-03-31 10:14:40 -0700896void Mir2Lir::MarkClean(RegLocation loc) {
897 if (loc.reg.IsPair()) {
898 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
899 info->SetIsDirty(false);
900 info = GetRegInfo(loc.reg.GetHigh());
901 info->SetIsDirty(false);
902 } else {
903 RegisterInfo* info = GetRegInfo(loc.reg);
904 info->SetIsDirty(false);
905 }
906}
907
908// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700909void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 if (loc.home) {
911 // If already home, can't be dirty
912 return;
913 }
buzbee091cc402014-03-31 10:14:40 -0700914 if (loc.reg.IsPair()) {
915 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
916 info->SetIsDirty(true);
917 info = GetRegInfo(loc.reg.GetHigh());
918 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800919 } else {
buzbee091cc402014-03-31 10:14:40 -0700920 RegisterInfo* info = GetRegInfo(loc.reg);
921 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 }
923}
924
buzbee2700f7e2014-03-07 09:46:20 -0800925void Mir2Lir::MarkInUse(RegStorage reg) {
926 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700927 GetRegInfo(reg.GetLow())->MarkInUse();
928 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800929 } else {
buzbee091cc402014-03-31 10:14:40 -0700930 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800931 }
932}
933
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700934bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700935 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700936 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee3a658072014-08-28 13:48:56 -0700937 int my_sreg = info->SReg();
938 if (info->IsTemp() && info->IsLive() && info->IsWide() && my_sreg != INVALID_SREG) {
buzbee082833c2014-05-17 23:16:26 -0700939 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700940 RegStorage partner_reg = info->Partner();
941 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700942 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700943 DCHECK(partner->IsWide());
944 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700945 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700946 int partner_sreg = partner->SReg();
buzbee3a658072014-08-28 13:48:56 -0700947 int diff = my_sreg - partner_sreg;
948 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700949 }
buzbee082833c2014-05-17 23:16:26 -0700950 if (info->Master() != info) {
951 // Aliased.
952 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
953 // If I'm live, master should not be live, but should show liveness in alias set.
954 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
955 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700956 }
buzbee642fe342014-05-23 16:04:08 -0700957// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700958 }
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));
Andreas Gampe4b537a82014-06-30 22:24:53 -07001001 RegStorage reg = AllocLiveReg(loc.s_reg_low, loc.ref ? kRefReg : kAnyReg, false);
buzbee091cc402014-03-31 10:14:40 -07001002 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 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001015 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 return loc;
1018}
1019
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001020RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 DCHECK(loc.wide);
1022 DCHECK(CheckCorePoolSanity());
1023 if (loc.location != kLocPhysReg) {
1024 DCHECK((loc.location == kLocDalvikFrame) ||
1025 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001026 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1027 if (reg.Valid()) {
1028 bool match = true;
1029 if (reg.IsPair()) {
1030 // If we've got a register pair, make sure that it was last used as the same pair.
1031 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1032 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1033 match &= info_lo->IsWide();
1034 match &= info_hi->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001035 match &= (info_lo->Partner().ExactlyEquals(info_hi->GetReg()));
1036 match &= (info_hi->Partner().ExactlyEquals(info_lo->GetReg()));
buzbee091cc402014-03-31 10:14:40 -07001037 } else {
1038 RegisterInfo* info = GetRegInfo(reg);
1039 match &= info->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001040 match &= (info->GetReg().ExactlyEquals(info->Partner()));
buzbee091cc402014-03-31 10:14:40 -07001041 }
1042 if (match) {
1043 loc.location = kLocPhysReg;
1044 loc.reg = reg;
1045 } else {
1046 Clobber(reg);
1047 FreeTemp(reg);
1048 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001050 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 }
1052 return loc;
1053}
1054
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001056RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057 if (loc.wide)
1058 return UpdateLocWide(loc);
1059 else
1060 return UpdateLoc(loc);
1061}
1062
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001063RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001064 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065
1066 loc = UpdateLocWide(loc);
1067
1068 /* If already in registers, we can assume proper form. Right reg class? */
1069 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001070 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001071 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001072 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001073 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001074 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001075 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001076 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001077 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001078 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001080 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 return loc;
1082 }
1083
1084 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1085 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1086
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001087 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001088 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 if (update) {
1091 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001092 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001094 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 return loc;
1096}
1097
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001098RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001099 // Narrow reg_class if the loc is a ref.
1100 if (loc.ref && reg_class == kAnyReg) {
1101 reg_class = kRefReg;
1102 }
1103
buzbee091cc402014-03-31 10:14:40 -07001104 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001106 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107
1108 loc = UpdateLoc(loc);
1109
1110 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001111 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001112 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001113 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001114 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001115 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001116 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001117 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001118 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001120 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121 return loc;
1122 }
1123
1124 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1125
buzbee2700f7e2014-03-07 09:46:20 -08001126 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001127 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128
1129 if (update) {
1130 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001131 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001133 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 return loc;
1135}
1136
1137/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001138void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1140 RegLocation loc = mir_graph_->reg_location_[i];
1141 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1142 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001143 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001144 if (loc.fp) {
1145 if (loc.wide) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001146 if (WideFPRsAreAliases()) {
1147 // Floats and doubles can be counted together.
1148 counts[p_map_idx].count += use_count;
1149 } else {
1150 // Treat doubles as a unit, using upper half of fp_counts array.
1151 counts[p_map_idx + num_regs].count += use_count;
1152 }
buzbeec729a6b2013-09-14 16:04:31 -07001153 i++;
1154 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001155 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001156 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001157 } else {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001158 if (loc.wide && WideGPRsAreAliases()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001159 i++;
buzbeeb5860fb2014-06-21 15:31:01 -07001160 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001161 if (!IsInexpensiveConstant(loc)) {
1162 counts[p_map_idx].count += use_count;
1163 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 }
1166}
1167
1168/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001169static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1171 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Matteo Franchinc763e352014-07-04 12:53:27 +01001172 // Note that we fall back to sorting on reg so we get stable output on differing qsort
1173 // implementations (such as on host and target or between local host and build servers).
1174 // Note also that if a wide val1 and a non-wide val2 have the same count, then val1 always
1175 // ``loses'' (as STARTING_WIDE_SREG is or-ed in val1->s_reg).
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001176 return (op1->count == op2->count)
1177 ? (op1->s_reg - op2->s_reg)
1178 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179}
1180
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001181void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 LOG(INFO) << msg;
1183 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001184 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1185 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001186 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001187 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001188 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 }
1190}
1191
1192/*
1193 * Note: some portions of this code required even if the kPromoteRegs
1194 * optimization is disabled.
1195 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001196void Mir2Lir::DoPromotion() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001197 int num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001199 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1200 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001201 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001202
1203 // Allow target code to add any special registers
1204 AdjustSpillMask();
1205
1206 /*
1207 * Simple register promotion. Just do a static count of the uses
1208 * of Dalvik registers. Note that we examine the SSA names, but
1209 * count based on original Dalvik register name. Count refs
1210 * separately based on type in order to give allocation
1211 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001212 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213 * reg.
1214 * TUNING: replace with linear scan once we have the ability
1215 * to describe register live ranges for GC.
1216 */
Matteo Franchinc763e352014-07-04 12:53:27 +01001217 size_t core_reg_count_size = WideGPRsAreAliases() ? num_regs : num_regs * 2;
1218 size_t fp_reg_count_size = WideFPRsAreAliases() ? num_regs : num_regs * 2;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 RefCounts *core_regs =
buzbeeb5860fb2014-06-21 15:31:01 -07001220 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * core_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001221 kArenaAllocRegAlloc));
buzbeeb5860fb2014-06-21 15:31:01 -07001222 RefCounts *fp_regs =
1223 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * fp_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001224 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 // Set ssa names for original Dalvik registers
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001226 for (int i = 0; i < num_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001227 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001229
buzbeeb5860fb2014-06-21 15:31:01 -07001230 // Duplicate in upper half to represent possible wide starting sregs.
1231 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1232 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1233 }
1234 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1235 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 }
1237
1238 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001239 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001242 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1243 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244
1245 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001246 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1247 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 }
1249
1250 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001251 // Promote fp regs
1252 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1253 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1254 size_t p_map_idx = SRegToPMap(low_sreg);
1255 RegStorage reg = RegStorage::InvalidReg();
1256 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1257 // TODO: break out the Thumb2-specific code.
1258 if (cu_->instruction_set == kThumb2) {
1259 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1260 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001261 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001262 // Ignore result - if can't alloc double may still be able to alloc singles.
1263 AllocPreservedDouble(low_sreg);
1264 }
1265 // Continue regardless of success - might still be able to grab a single.
1266 continue;
1267 } else {
1268 reg = AllocPreservedSingle(low_sreg);
1269 }
1270 } else {
1271 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001272 }
buzbee2700f7e2014-03-07 09:46:20 -08001273 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001274 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001275 }
1276 }
1277 }
1278
1279 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001280 for (size_t i = 0; (i < core_reg_count_size) &&
1281 (core_regs[i].count >= promotion_threshold); i++) {
1282 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1283 size_t p_map_idx = SRegToPMap(low_sreg);
1284 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1285 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001286 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 break; // No more left
1288 }
1289 }
1290 }
1291 }
1292
1293 // Now, update SSA names to new home locations
1294 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1295 RegLocation *curr = &mir_graph_->reg_location_[i];
1296 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001297 int reg_num = curr->fp ? promotion_map_[p_map_idx].fp_reg : promotion_map_[p_map_idx].core_reg;
Chao-ying Fua77ee512014-07-01 17:43:41 -07001298 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001299 RegStorage reg = RegStorage::InvalidReg();
1300 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1301 if (wide && cu_->instruction_set == kThumb2) {
1302 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1303 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001304 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001305 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1306 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 }
1308 }
1309 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001310 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 }
buzbeeb5860fb2014-06-21 15:31:01 -07001312 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1313 if (wide && !cu_->target64) {
1314 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1315 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1316 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1317 }
1318 } else {
1319 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1320 }
1321 }
1322 if (reg.Valid()) {
1323 curr->reg = reg;
1324 curr->location = kLocPhysReg;
1325 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 }
1327 }
1328 if (cu_->verbose) {
1329 DumpPromotionMap();
1330 }
1331}
1332
1333/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001334int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001336 fp_spill_mask_, frame_size_, v_reg,
1337 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338}
1339
1340/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001341int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1343}
1344
1345/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001346RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1347 RegLocation res;
1348 switch (reg_class) {
1349 case kRefReg: LOG(FATAL); break;
1350 case kFPReg: res = LocCReturnDouble(); break;
1351 default: res = LocCReturnWide(); break;
1352 }
buzbee082833c2014-05-17 23:16:26 -07001353 Clobber(res.reg);
1354 LockTemp(res.reg);
1355 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001356 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357 return res;
1358}
1359
buzbeea0cd2d72014-06-01 09:33:49 -07001360RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1361 RegLocation res;
1362 switch (reg_class) {
1363 case kRefReg: res = LocCReturnRef(); break;
1364 case kFPReg: res = LocCReturnFloat(); break;
1365 default: res = LocCReturn(); break;
1366 }
buzbee091cc402014-03-31 10:14:40 -07001367 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001369 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 } else {
buzbee091cc402014-03-31 10:14:40 -07001371 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001372 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001373 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001374 return res;
1375}
1376
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001377void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 DoPromotion();
1379
1380 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1381 LOG(INFO) << "After Promotion";
1382 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1383 }
1384
1385 /* Set the frame size */
1386 frame_size_ = ComputeFrameSize();
1387}
1388
1389/*
1390 * Get the "real" sreg number associated with an s_reg slot. In general,
1391 * s_reg values passed through codegen are the SSA names created by
1392 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1393 * array. However, renaming is accomplished by simply replacing RegLocation
1394 * entries in the reglocation[] array. Therefore, when location
1395 * records for operands are first created, we need to ask the locRecord
1396 * identified by the dataflow pass what it's new name is.
1397 */
1398int Mir2Lir::GetSRegHi(int lowSreg) {
1399 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1400}
1401
buzbee091cc402014-03-31 10:14:40 -07001402bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001403 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001404 return true;
1405}
1406
Brian Carlstrom7940e442013-07-12 13:46:57 -07001407} // namespace art