blob: e8fc919d5fe65ddfcb770261eb8219892c5daa5a [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);
255 if (v_reg >= 0) {
256 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
257 return v_reg;
258 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800259 /*
260 * It must be the case that the v_reg for temporary is less than or equal to the
261 * base reg for temps. For that reason, "position" must be zero or positive.
262 */
263 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
264
265 // The temporaries are placed after dalvik registers in the promotion map
266 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
267 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 }
269}
270
buzbee091cc402014-03-31 10:14:40 -0700271// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800272void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 int p_map_idx = SRegToPMap(s_reg);
274 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700275 int reg_num = reg.GetRegNum();
276 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800277 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800279 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 num_core_spills_++;
281 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800282 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283}
284
buzbee091cc402014-03-31 10:14:40 -0700285/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800286RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
287 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700288 /*
289 * Note: it really doesn't matter much whether we allocate from the core or core64
290 * pool for 64-bit targets - but for some targets it does matter whether allocations
291 * happens from the single or double pool. This entire section of code could stand
292 * a good refactoring.
293 */
buzbee091cc402014-03-31 10:14:40 -0700294 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
295 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
296 if (!info->IsTemp() && !info->InUse()) {
297 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 RecordCorePromotion(res, s_reg);
299 break;
300 }
301 }
302 return res;
303}
304
buzbeeb5860fb2014-06-21 15:31:01 -0700305void Mir2Lir::RecordFpPromotion(RegStorage reg, int s_reg) {
306 DCHECK_NE(cu_->instruction_set, kThumb2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 int p_map_idx = SRegToPMap(s_reg);
308 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700309 int reg_num = reg.GetRegNum();
buzbee091cc402014-03-31 10:14:40 -0700310 GetRegInfo(reg)->MarkInUse();
buzbeeb5860fb2014-06-21 15:31:01 -0700311 fp_spill_mask_ |= (1 << reg_num);
312 // Include reg for later sort
313 fp_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
314 num_fp_spills_++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbeeb5860fb2014-06-21 15:31:01 -0700316 promotion_map_[p_map_idx].fp_reg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317}
318
buzbeeb5860fb2014-06-21 15:31:01 -0700319// Reserve a callee-save floating point.
320RegStorage Mir2Lir::AllocPreservedFpReg(int s_reg) {
321 /*
322 * For targets other than Thumb2, it doesn't matter whether we allocate from
323 * the sp_regs_ or dp_regs_ pool. Some refactoring is in order here.
324 */
325 DCHECK_NE(cu_->instruction_set, kThumb2);
buzbee2700f7e2014-03-07 09:46:20 -0800326 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700327 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
328 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
329 if (!info->IsTemp() && !info->InUse()) {
330 res = info->GetReg();
buzbeeb5860fb2014-06-21 15:31:01 -0700331 RecordFpPromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 break;
333 }
334 }
335 return res;
336}
337
buzbeeb5860fb2014-06-21 15:31:01 -0700338// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
buzbee2700f7e2014-03-07 09:46:20 -0800339RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
340 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700341 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedDouble";
342 return res;
343}
344
345// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
346RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
347 RegStorage res;
348 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedSingle";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 return res;
350}
351
buzbee091cc402014-03-31 10:14:40 -0700352
353RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
354 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700356 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 if (next >= num_regs)
358 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700359 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700360 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700361 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee091cc402014-03-31 10:14:40 -0700362 Clobber(info->GetReg());
363 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700364 /*
365 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
366 * The caller will set wideness as appropriate.
367 */
Douglas Leung2db3e262014-06-25 16:02:55 -0700368 if (info->IsWide()) {
369 RegisterInfo* partner = GetRegInfo(info->Partner());
370 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
371 DCHECK(partner->IsWide());
372 info->SetIsWide(false);
373 partner->SetIsWide(false);
374 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700376 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 }
378 next++;
379 }
380 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700381 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700382 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 if (next >= num_regs)
384 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700385 RegisterInfo* info = regs.Get(next);
386 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700387 // Got one. Kill it.
388 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700389 Clobber(info->GetReg());
390 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700391 if (info->IsWide()) {
392 RegisterInfo* partner = GetRegInfo(info->Partner());
393 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
394 DCHECK(partner->IsWide());
395 info->SetIsWide(false);
396 partner->SetIsWide(false);
397 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700399 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 }
401 next++;
402 }
403 if (required) {
404 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700405 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 LOG(FATAL) << "No free temp registers";
407 }
buzbee2700f7e2014-03-07 09:46:20 -0800408 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409}
410
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800412RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700413 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414}
415
buzbee2700f7e2014-03-07 09:46:20 -0800416RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700417 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418}
419
buzbeeb01bf152014-05-13 15:59:07 -0700420RegStorage Mir2Lir::AllocTempWide() {
421 RegStorage res;
422 if (reg_pool_->core64_regs_.Size() != 0) {
423 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, true);
424 } else {
425 RegStorage low_reg = AllocTemp();
426 RegStorage high_reg = AllocTemp();
427 res = RegStorage::MakeRegPair(low_reg, high_reg);
428 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700429 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kIgnoreRef, FPCheck::kCheckNotFP);
buzbeeb01bf152014-05-13 15:59:07 -0700430 return res;
431}
432
buzbeea0cd2d72014-06-01 09:33:49 -0700433RegStorage Mir2Lir::AllocTempRef() {
434 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, true);
435 DCHECK(!res.IsPair());
Andreas Gampe4b537a82014-06-30 22:24:53 -0700436 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700437 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100438}
439
buzbee091cc402014-03-31 10:14:40 -0700440RegStorage Mir2Lir::AllocTempSingle() {
441 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
442 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700443 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700444 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445}
446
buzbee091cc402014-03-31 10:14:40 -0700447RegStorage Mir2Lir::AllocTempDouble() {
448 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
449 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700450 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700451 return res;
452}
453
buzbeeb01bf152014-05-13 15:59:07 -0700454RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
buzbeea0cd2d72014-06-01 09:33:49 -0700455 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700456 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
457 return AllocTempDouble();
458 }
459 return AllocTempWide();
460}
461
462RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
463 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
464 return AllocTempSingle();
buzbeea0cd2d72014-06-01 09:33:49 -0700465 } else if (reg_class == kRefReg) {
466 return AllocTempRef();
buzbeeb01bf152014-05-13 15:59:07 -0700467 }
468 return AllocTemp();
469}
470
buzbee091cc402014-03-31 10:14:40 -0700471RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
472 RegStorage res;
473 GrowableArray<RegisterInfo*>::Iterator it(&regs);
474 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
475 if ((info->SReg() == s_reg) && info->IsLive()) {
476 res = info->GetReg();
477 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 }
479 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 return res;
481}
482
buzbee091cc402014-03-31 10:14:40 -0700483RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
484 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700485 if (reg_class == kRefReg) {
486 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700487 CheckRegStorage(reg, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700488 }
489 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700490 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 }
buzbee091cc402014-03-31 10:14:40 -0700492 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700493 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700494 reg = FindLiveReg(wide || reg_class == kRefReg ? reg_pool_->core64_regs_ :
495 reg_pool_->core_regs_, s_reg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100496 } else {
497 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
498 }
buzbee091cc402014-03-31 10:14:40 -0700499 }
500 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700501 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700502 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700503 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
504 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700505 reg = RegStorage::MakeRegPair(reg, high_reg);
506 MarkWide(reg);
507 } else {
buzbee30adc732014-05-09 15:10:18 -0700508 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700509 reg = RegStorage::InvalidReg();
510 }
511 }
buzbee30adc732014-05-09 15:10:18 -0700512 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
513 // Width mismatch - don't try to reuse.
514 reg = RegStorage::InvalidReg();
515 }
516 }
517 if (reg.Valid()) {
518 if (reg.IsPair()) {
519 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
520 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
521 if (info_low->IsTemp()) {
522 info_low->MarkInUse();
523 }
524 if (info_high->IsTemp()) {
525 info_high->MarkInUse();
526 }
527 } else {
buzbee091cc402014-03-31 10:14:40 -0700528 RegisterInfo* info = GetRegInfo(reg);
529 if (info->IsTemp()) {
530 info->MarkInUse();
531 }
532 }
buzbee30adc732014-05-09 15:10:18 -0700533 } else {
534 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
535 ClobberSReg(s_reg);
536 if (wide) {
537 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700538 }
539 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700540 CheckRegStorage(reg, WidenessCheck::kIgnoreWide,
541 reg_class == kRefReg ? RefCheck::kCheckRef : RefCheck::kIgnoreRef,
542 FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700543 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544}
545
buzbee2700f7e2014-03-07 09:46:20 -0800546void Mir2Lir::FreeTemp(RegStorage reg) {
547 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700548 FreeTemp(reg.GetLow());
549 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800550 } else {
buzbee091cc402014-03-31 10:14:40 -0700551 RegisterInfo* p = GetRegInfo(reg);
552 if (p->IsTemp()) {
553 p->MarkFree();
554 p->SetIsWide(false);
555 p->SetPartner(reg);
556 }
buzbee2700f7e2014-03-07 09:46:20 -0800557 }
558}
559
buzbee082833c2014-05-17 23:16:26 -0700560void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
561 DCHECK(rl_keep.wide);
562 DCHECK(rl_free.wide);
563 int free_low = rl_free.reg.GetLowReg();
564 int free_high = rl_free.reg.GetHighReg();
565 int keep_low = rl_keep.reg.GetLowReg();
566 int keep_high = rl_keep.reg.GetHighReg();
567 if ((free_low != keep_low) && (free_low != keep_high) &&
568 (free_high != keep_low) && (free_high != keep_high)) {
569 // No overlap, free both
570 FreeTemp(rl_free.reg);
571 }
572}
573
buzbee262b2992014-03-27 11:22:43 -0700574bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700575 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800576 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700577 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
578 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700579 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700580 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800581 } else {
buzbee091cc402014-03-31 10:14:40 -0700582 RegisterInfo* p = GetRegInfo(reg);
583 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800584 }
buzbee091cc402014-03-31 10:14:40 -0700585 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586}
587
buzbee262b2992014-03-27 11:22:43 -0700588bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700589 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800590 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700591 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
592 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
593 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800594 } else {
buzbee091cc402014-03-31 10:14:40 -0700595 RegisterInfo* p = GetRegInfo(reg);
596 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800597 }
buzbee091cc402014-03-31 10:14:40 -0700598 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599}
600
buzbee262b2992014-03-27 11:22:43 -0700601bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700602 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800603 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700604 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
605 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
606 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800607 } else {
buzbee091cc402014-03-31 10:14:40 -0700608 RegisterInfo* p = GetRegInfo(reg);
609 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800610 }
buzbee091cc402014-03-31 10:14:40 -0700611 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612}
613
buzbee2700f7e2014-03-07 09:46:20 -0800614bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700615 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800616 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700617 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
618 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
619 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800620 } else {
buzbee091cc402014-03-31 10:14:40 -0700621 RegisterInfo* p = GetRegInfo(reg);
622 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800623 }
buzbee091cc402014-03-31 10:14:40 -0700624 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800625}
626
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627/*
628 * Similar to AllocTemp(), but forces the allocation of a specific
629 * register. No check is made to see if the register was previously
630 * allocated. Use with caution.
631 */
buzbee2700f7e2014-03-07 09:46:20 -0800632void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700633 DCHECK(IsTemp(reg));
634 if (reg.IsPair()) {
635 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
636 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
637 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700638 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700639 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700640 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700641 } else {
642 RegisterInfo* p = GetRegInfo(reg);
643 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700644 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700645 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646}
647
buzbee2700f7e2014-03-07 09:46:20 -0800648void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700649 if (reg.IsPair()) {
650 GetRegInfo(reg.GetLow())->ResetDefBody();
651 GetRegInfo(reg.GetHigh())->ResetDefBody();
652 } else {
653 GetRegInfo(reg)->ResetDefBody();
654 }
buzbee2700f7e2014-03-07 09:46:20 -0800655}
656
buzbee091cc402014-03-31 10:14:40 -0700657void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
658 RegisterInfo* info = nullptr;
659 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
660 if (IsTemp(rs)) {
661 info = GetRegInfo(reg);
662 }
663 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
664 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
665 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700667 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 break;
buzbee091cc402014-03-31 10:14:40 -0700669 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 }
671 }
672}
673
674/*
675 * Mark the beginning and end LIR of a def sequence. Note that
676 * on entry start points to the LIR prior to the beginning of the
677 * sequence.
678 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700679void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 DCHECK(!rl.wide);
681 DCHECK(start && start->next);
682 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700683 RegisterInfo* p = GetRegInfo(rl.reg);
684 p->SetDefStart(start->next);
685 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686}
687
688/*
689 * Mark the beginning and end LIR of a def sequence. Note that
690 * on entry start points to the LIR prior to the beginning of the
691 * sequence.
692 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700693void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 DCHECK(rl.wide);
695 DCHECK(start && start->next);
696 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700697 RegisterInfo* p;
698 if (rl.reg.IsPair()) {
699 p = GetRegInfo(rl.reg.GetLow());
700 ResetDef(rl.reg.GetHigh()); // Only track low of pair
701 } else {
702 p = GetRegInfo(rl.reg);
703 }
704 p->SetDefStart(start->next);
705 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706}
707
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700708void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700710 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
711 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 }
buzbee091cc402014-03-31 10:14:40 -0700713 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714}
715
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700716void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700718 // If pair, only track low reg of pair.
719 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
720 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
721 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 }
buzbee091cc402014-03-31 10:14:40 -0700723 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724}
725
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700726void Mir2Lir::ResetDefTracking() {
buzbeea0cd2d72014-06-01 09:33:49 -0700727 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
728 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee091cc402014-03-31 10:14:40 -0700729 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 }
731}
732
buzbeeba574512014-05-12 15:13:16 -0700733void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700734 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
735 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700736 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700737 }
738}
739
740void Mir2Lir::FlushRegWide(RegStorage reg) {
741 if (reg.IsPair()) {
742 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
743 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
744 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
buzbeeb5860fb2014-06-21 15:31:01 -0700745 (info1->Partner().ExactlyEquals(info2->GetReg())) &&
746 (info2->Partner().ExactlyEquals(info1->GetReg())));
buzbee091cc402014-03-31 10:14:40 -0700747 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
748 if (!(info1->IsTemp() && info2->IsTemp())) {
749 /* Should not happen. If it does, there's a problem in eval_loc */
750 LOG(FATAL) << "Long half-temp, half-promoted";
751 }
752
753 info1->SetIsDirty(false);
754 info2->SetIsDirty(false);
755 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
756 info1 = info2;
757 }
758 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100759 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700760 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700761 }
762 } else {
763 RegisterInfo* info = GetRegInfo(reg);
764 if (info->IsLive() && info->IsDirty()) {
765 info->SetIsDirty(false);
766 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100767 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700768 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700769 }
770 }
771}
772
773void Mir2Lir::FlushReg(RegStorage reg) {
774 DCHECK(!reg.IsPair());
775 RegisterInfo* info = GetRegInfo(reg);
776 if (info->IsLive() && info->IsDirty()) {
777 info->SetIsDirty(false);
778 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100779 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700780 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781 }
782}
783
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800784void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700785 if (info->IsWide()) {
786 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800787 } else {
buzbee091cc402014-03-31 10:14:40 -0700788 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 }
790}
791
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700792void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700793 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
794 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700795 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700796 FlushSpecificReg(info);
797 }
buzbee30adc732014-05-09 15:10:18 -0700798 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700799 info->SetIsWide(false);
800 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801}
802
803
buzbee2700f7e2014-03-07 09:46:20 -0800804bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 if (reg_class == kAnyReg) {
806 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700807 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
808 /*
809 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
810 * with width here - that should be checked at a higher level (if needed).
811 */
buzbee091cc402014-03-31 10:14:40 -0700812 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813 } else {
buzbee091cc402014-03-31 10:14:40 -0700814 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 }
816}
817
buzbee091cc402014-03-31 10:14:40 -0700818void Mir2Lir::MarkLive(RegLocation loc) {
819 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700820 if (!IsTemp(reg)) {
821 return;
822 }
buzbee091cc402014-03-31 10:14:40 -0700823 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700824 if (s_reg == INVALID_SREG) {
825 // Can't be live if no associated sreg.
826 if (reg.IsPair()) {
827 GetRegInfo(reg.GetLow())->MarkDead();
828 GetRegInfo(reg.GetHigh())->MarkDead();
829 } else {
830 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700831 }
buzbee082833c2014-05-17 23:16:26 -0700832 } else {
833 if (reg.IsPair()) {
834 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
835 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
836 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
837 (info_hi->SReg() == s_reg)) {
838 return; // Already live.
839 }
840 ClobberSReg(s_reg);
841 ClobberSReg(s_reg + 1);
842 info_lo->MarkLive(s_reg);
843 info_hi->MarkLive(s_reg + 1);
844 } else {
845 RegisterInfo* info = GetRegInfo(reg);
846 if (info->IsLive() && (info->SReg() == s_reg)) {
847 return; // Already live.
848 }
849 ClobberSReg(s_reg);
850 if (loc.wide) {
851 ClobberSReg(s_reg + 1);
852 }
853 info->MarkLive(s_reg);
854 }
855 if (loc.wide) {
856 MarkWide(reg);
857 } else {
858 MarkNarrow(reg);
859 }
buzbee091cc402014-03-31 10:14:40 -0700860 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861}
862
buzbee2700f7e2014-03-07 09:46:20 -0800863void Mir2Lir::MarkTemp(RegStorage reg) {
864 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700865 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700866 tempreg_info_.Insert(info);
867 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868}
869
buzbee2700f7e2014-03-07 09:46:20 -0800870void Mir2Lir::UnmarkTemp(RegStorage reg) {
871 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700872 RegisterInfo* info = GetRegInfo(reg);
873 tempreg_info_.Delete(info);
874 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800875}
876
buzbee091cc402014-03-31 10:14:40 -0700877void Mir2Lir::MarkWide(RegStorage reg) {
878 if (reg.IsPair()) {
879 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
880 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700881 // Unpair any old partners.
buzbeeb5860fb2014-06-21 15:31:01 -0700882 if (info_lo->IsWide() && info_lo->Partner().NotExactlyEquals(info_hi->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700883 GetRegInfo(info_lo->Partner())->SetIsWide(false);
884 }
buzbeeb5860fb2014-06-21 15:31:01 -0700885 if (info_hi->IsWide() && info_hi->Partner().NotExactlyEquals(info_lo->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700886 GetRegInfo(info_hi->Partner())->SetIsWide(false);
887 }
buzbee091cc402014-03-31 10:14:40 -0700888 info_lo->SetIsWide(true);
889 info_hi->SetIsWide(true);
890 info_lo->SetPartner(reg.GetHigh());
891 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800892 } else {
buzbee091cc402014-03-31 10:14:40 -0700893 RegisterInfo* info = GetRegInfo(reg);
894 info->SetIsWide(true);
895 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 }
897}
898
buzbee082833c2014-05-17 23:16:26 -0700899void Mir2Lir::MarkNarrow(RegStorage reg) {
900 DCHECK(!reg.IsPair());
901 RegisterInfo* info = GetRegInfo(reg);
902 info->SetIsWide(false);
903 info->SetPartner(reg);
904}
905
buzbee091cc402014-03-31 10:14:40 -0700906void Mir2Lir::MarkClean(RegLocation loc) {
907 if (loc.reg.IsPair()) {
908 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
909 info->SetIsDirty(false);
910 info = GetRegInfo(loc.reg.GetHigh());
911 info->SetIsDirty(false);
912 } else {
913 RegisterInfo* info = GetRegInfo(loc.reg);
914 info->SetIsDirty(false);
915 }
916}
917
918// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700919void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 if (loc.home) {
921 // If already home, can't be dirty
922 return;
923 }
buzbee091cc402014-03-31 10:14:40 -0700924 if (loc.reg.IsPair()) {
925 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
926 info->SetIsDirty(true);
927 info = GetRegInfo(loc.reg.GetHigh());
928 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800929 } else {
buzbee091cc402014-03-31 10:14:40 -0700930 RegisterInfo* info = GetRegInfo(loc.reg);
931 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700932 }
933}
934
buzbee2700f7e2014-03-07 09:46:20 -0800935void Mir2Lir::MarkInUse(RegStorage reg) {
936 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700937 GetRegInfo(reg.GetLow())->MarkInUse();
938 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800939 } else {
buzbee091cc402014-03-31 10:14:40 -0700940 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800941 }
942}
943
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700944bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700945 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700946 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700947 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
948 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700949 int my_sreg = info->SReg();
950 RegStorage partner_reg = info->Partner();
951 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700952 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700953 DCHECK(partner->IsWide());
954 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700955 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700956 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700957 if (my_sreg == INVALID_SREG) {
958 DCHECK_EQ(partner_sreg, INVALID_SREG);
959 } else {
960 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700961 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700962 }
963 }
buzbee082833c2014-05-17 23:16:26 -0700964 if (info->Master() != info) {
965 // Aliased.
966 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
967 // If I'm live, master should not be live, but should show liveness in alias set.
968 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
969 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700970 }
buzbee642fe342014-05-23 16:04:08 -0700971// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700972 }
973 if (info->IsAliased()) {
974 // Has child aliases.
975 DCHECK_EQ(info->Master(), info);
976 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
977 // Master live, no child should be dead - all should show liveness in set.
978 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
979 DCHECK(!p->IsDead());
980 DCHECK_EQ(p->SReg(), INVALID_SREG);
981 }
982 } else if (!info->IsDead()) {
983 // Master not live, one or more aliases must be.
984 bool live_alias = false;
985 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
986 live_alias |= p->IsLive();
987 }
988 DCHECK(live_alias);
989 }
990 }
991 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
992 // If not fully live, should have INVALID_SREG and def's should be null.
993 DCHECK(info->DefStart() == nullptr);
994 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700995 }
996 }
997 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998}
999
1000/*
1001 * Return an updated location record with current in-register status.
1002 * If the value lives in live temps, reflect that fact. No code
1003 * is generated. If the live value is part of an older pair,
1004 * clobber both low and high.
1005 * TUNING: clobbering both is a bit heavy-handed, but the alternative
1006 * is a bit complex when dealing with FP regs. Examine code to see
1007 * if it's worthwhile trying to be more clever here.
1008 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001009RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 DCHECK(!loc.wide);
1011 DCHECK(CheckCorePoolSanity());
1012 if (loc.location != kLocPhysReg) {
1013 DCHECK((loc.location == kLocDalvikFrame) ||
1014 (loc.location == kLocCompilerTemp));
Andreas Gampe4b537a82014-06-30 22:24:53 -07001015 RegStorage reg = AllocLiveReg(loc.s_reg_low, loc.ref ? kRefReg : kAnyReg, false);
buzbee091cc402014-03-31 10:14:40 -07001016 if (reg.Valid()) {
1017 bool match = true;
1018 RegisterInfo* info = GetRegInfo(reg);
1019 match &= !reg.IsPair();
1020 match &= !info->IsWide();
1021 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001023 loc.reg = reg;
1024 } else {
1025 Clobber(reg);
1026 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 }
1028 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001029 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 return loc;
1032}
1033
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001034RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 DCHECK(loc.wide);
1036 DCHECK(CheckCorePoolSanity());
1037 if (loc.location != kLocPhysReg) {
1038 DCHECK((loc.location == kLocDalvikFrame) ||
1039 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001040 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1041 if (reg.Valid()) {
1042 bool match = true;
1043 if (reg.IsPair()) {
1044 // If we've got a register pair, make sure that it was last used as the same pair.
1045 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1046 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1047 match &= info_lo->IsWide();
1048 match &= info_hi->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001049 match &= (info_lo->Partner().ExactlyEquals(info_hi->GetReg()));
1050 match &= (info_hi->Partner().ExactlyEquals(info_lo->GetReg()));
buzbee091cc402014-03-31 10:14:40 -07001051 } else {
1052 RegisterInfo* info = GetRegInfo(reg);
1053 match &= info->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001054 match &= (info->GetReg().ExactlyEquals(info->Partner()));
buzbee091cc402014-03-31 10:14:40 -07001055 }
1056 if (match) {
1057 loc.location = kLocPhysReg;
1058 loc.reg = reg;
1059 } else {
1060 Clobber(reg);
1061 FreeTemp(reg);
1062 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001064 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 }
1066 return loc;
1067}
1068
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001070RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 if (loc.wide)
1072 return UpdateLocWide(loc);
1073 else
1074 return UpdateLoc(loc);
1075}
1076
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001077RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079
1080 loc = UpdateLocWide(loc);
1081
1082 /* If already in registers, we can assume proper form. Right reg class? */
1083 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001084 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001085 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001086 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001087 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001088 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001089 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001090 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001091 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -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
1098 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1099 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1100
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001101 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001102 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 if (update) {
1105 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001106 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001108 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109 return loc;
1110}
1111
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001112RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001113 // Narrow reg_class if the loc is a ref.
1114 if (loc.ref && reg_class == kAnyReg) {
1115 reg_class = kRefReg;
1116 }
1117
buzbee091cc402014-03-31 10:14:40 -07001118 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001120 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121
1122 loc = UpdateLoc(loc);
1123
1124 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001125 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001126 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001127 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001128 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001129 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001130 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001131 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001132 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001134 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 return loc;
1136 }
1137
1138 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1139
buzbee2700f7e2014-03-07 09:46:20 -08001140 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001141 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001142
1143 if (update) {
1144 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001145 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001147 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 return loc;
1149}
1150
1151/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001152void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1154 RegLocation loc = mir_graph_->reg_location_[i];
1155 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1156 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001157 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001158 if (loc.fp) {
1159 if (loc.wide) {
1160 // Treat doubles as a unit, using upper half of fp_counts array.
buzbeeb5860fb2014-06-21 15:31:01 -07001161 counts[p_map_idx + num_regs].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001162 i++;
1163 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001164 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001165 }
1166 } else if (!IsInexpensiveConstant(loc)) {
buzbeeb5860fb2014-06-21 15:31:01 -07001167 if (loc.wide && cu_->target64) {
1168 // Treat long as a unit, using upper half of core_counts array.
1169 counts[p_map_idx + num_regs].count += use_count;
1170 i++;
1171 } else {
1172 counts[p_map_idx].count += use_count;
1173 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 }
1176}
1177
1178/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001179static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1181 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001182 // Note that we fall back to sorting on reg so we get stable output
1183 // on differing qsort implementations (such as on host and target or
1184 // between local host and build servers).
1185 return (op1->count == op2->count)
1186 ? (op1->s_reg - op2->s_reg)
1187 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188}
1189
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001190void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001191 LOG(INFO) << msg;
1192 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001193 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1194 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001195 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001196 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001197 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 }
1199}
1200
1201/*
1202 * Note: some portions of this code required even if the kPromoteRegs
1203 * optimization is disabled.
1204 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001205void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001207 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001209 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1210 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001211 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212
1213 // Allow target code to add any special registers
1214 AdjustSpillMask();
1215
1216 /*
1217 * Simple register promotion. Just do a static count of the uses
1218 * of Dalvik registers. Note that we examine the SSA names, but
1219 * count based on original Dalvik register name. Count refs
1220 * separately based on type in order to give allocation
1221 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001222 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223 * reg.
1224 * TUNING: replace with linear scan once we have the ability
1225 * to describe register live ranges for GC.
1226 */
buzbeeb5860fb2014-06-21 15:31:01 -07001227 size_t core_reg_count_size = cu_->target64 ? num_regs * 2 : num_regs;
1228 size_t fp_reg_count_size = num_regs * 2;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 RefCounts *core_regs =
buzbeeb5860fb2014-06-21 15:31:01 -07001230 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * core_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001231 kArenaAllocRegAlloc));
buzbeeb5860fb2014-06-21 15:31:01 -07001232 RefCounts *fp_regs =
1233 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * fp_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001234 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 // Set ssa names for original Dalvik registers
1236 for (int i = 0; i < dalvik_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001237 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001239
1240 // Set ssa names for compiler temporaries
1241 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1242 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1243 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeeb5860fb2014-06-21 15:31:01 -07001244 fp_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001245 }
1246
buzbeeb5860fb2014-06-21 15:31:01 -07001247 // Duplicate in upper half to represent possible wide starting sregs.
1248 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1249 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1250 }
1251 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1252 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 }
1254
1255 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001256 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001257
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258
1259 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001260 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1261 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262
1263 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001264 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1265 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 }
1267
1268 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001269 // Promote fp regs
1270 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1271 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1272 size_t p_map_idx = SRegToPMap(low_sreg);
1273 RegStorage reg = RegStorage::InvalidReg();
1274 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1275 // TODO: break out the Thumb2-specific code.
1276 if (cu_->instruction_set == kThumb2) {
1277 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1278 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001279 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001280 // Ignore result - if can't alloc double may still be able to alloc singles.
1281 AllocPreservedDouble(low_sreg);
1282 }
1283 // Continue regardless of success - might still be able to grab a single.
1284 continue;
1285 } else {
1286 reg = AllocPreservedSingle(low_sreg);
1287 }
1288 } else {
1289 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001290 }
buzbee2700f7e2014-03-07 09:46:20 -08001291 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001292 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 }
1294 }
1295 }
1296
1297 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001298 for (size_t i = 0; (i < core_reg_count_size) &&
1299 (core_regs[i].count >= promotion_threshold); i++) {
1300 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1301 size_t p_map_idx = SRegToPMap(low_sreg);
1302 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1303 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001304 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 break; // No more left
1306 }
1307 }
1308 }
1309 }
1310
1311 // Now, update SSA names to new home locations
1312 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1313 RegLocation *curr = &mir_graph_->reg_location_[i];
1314 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001315 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 -07001316 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001317 RegStorage reg = RegStorage::InvalidReg();
1318 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1319 if (wide && cu_->instruction_set == kThumb2) {
1320 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1321 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001322 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001323 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1324 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 }
1326 }
1327 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001328 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329 }
buzbeeb5860fb2014-06-21 15:31:01 -07001330 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1331 if (wide && !cu_->target64) {
1332 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1333 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1334 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1335 }
1336 } else {
1337 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1338 }
1339 }
1340 if (reg.Valid()) {
1341 curr->reg = reg;
1342 curr->location = kLocPhysReg;
1343 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 }
1345 }
1346 if (cu_->verbose) {
1347 DumpPromotionMap();
1348 }
1349}
1350
1351/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001352int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001354 fp_spill_mask_, frame_size_, v_reg,
1355 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356}
1357
1358/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001359int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1361}
1362
1363/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001364RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1365 RegLocation res;
1366 switch (reg_class) {
1367 case kRefReg: LOG(FATAL); break;
1368 case kFPReg: res = LocCReturnDouble(); break;
1369 default: res = LocCReturnWide(); break;
1370 }
buzbee082833c2014-05-17 23:16:26 -07001371 Clobber(res.reg);
1372 LockTemp(res.reg);
1373 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001374 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001375 return res;
1376}
1377
buzbeea0cd2d72014-06-01 09:33:49 -07001378RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1379 RegLocation res;
1380 switch (reg_class) {
1381 case kRefReg: res = LocCReturnRef(); break;
1382 case kFPReg: res = LocCReturnFloat(); break;
1383 default: res = LocCReturn(); break;
1384 }
buzbee091cc402014-03-31 10:14:40 -07001385 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001387 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001388 } else {
buzbee091cc402014-03-31 10:14:40 -07001389 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001391 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 return res;
1393}
1394
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001395void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 DoPromotion();
1397
1398 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1399 LOG(INFO) << "After Promotion";
1400 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1401 }
1402
1403 /* Set the frame size */
1404 frame_size_ = ComputeFrameSize();
1405}
1406
1407/*
1408 * Get the "real" sreg number associated with an s_reg slot. In general,
1409 * s_reg values passed through codegen are the SSA names created by
1410 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1411 * array. However, renaming is accomplished by simply replacing RegLocation
1412 * entries in the reglocation[] array. Therefore, when location
1413 * records for operands are first created, we need to ask the locRecord
1414 * identified by the dataflow pass what it's new name is.
1415 */
1416int Mir2Lir::GetSRegHi(int lowSreg) {
1417 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1418}
1419
buzbee091cc402014-03-31 10:14:40 -07001420bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001421 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001422 return true;
1423}
1424
Brian Carlstrom7940e442013-07-12 13:46:57 -07001425} // namespace art