blob: 45244e1a7a3c59371e13cb9fc77ba35dcb93adac [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
Serguei Katkov9ee45192014-07-17 14:39:03 +0700411RegStorage Mir2Lir::AllocTemp(bool required) {
412 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, required);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413}
414
Serguei Katkov9ee45192014-07-17 14:39:03 +0700415RegStorage Mir2Lir::AllocTempWide(bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700416 RegStorage res;
417 if (reg_pool_->core64_regs_.Size() != 0) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700418 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, required);
buzbeeb01bf152014-05-13 15:59:07 -0700419 } else {
420 RegStorage low_reg = AllocTemp();
421 RegStorage high_reg = AllocTemp();
422 res = RegStorage::MakeRegPair(low_reg, high_reg);
423 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700424 if (required) {
425 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kIgnoreRef, FPCheck::kCheckNotFP);
426 }
buzbeeb01bf152014-05-13 15:59:07 -0700427 return res;
428}
429
Serguei Katkov9ee45192014-07-17 14:39:03 +0700430RegStorage Mir2Lir::AllocTempRef(bool required) {
431 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, required);
432 if (required) {
433 DCHECK(!res.IsPair());
434 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
435 }
buzbeea0cd2d72014-06-01 09:33:49 -0700436 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100437}
438
Serguei Katkov9ee45192014-07-17 14:39:03 +0700439RegStorage Mir2Lir::AllocTempSingle(bool required) {
440 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, required);
441 if (required) {
442 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
443 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
444 }
buzbee091cc402014-03-31 10:14:40 -0700445 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446}
447
Serguei Katkov9ee45192014-07-17 14:39:03 +0700448RegStorage Mir2Lir::AllocTempDouble(bool required) {
449 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, required);
450 if (required) {
451 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
452 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
453 }
buzbee091cc402014-03-31 10:14:40 -0700454 return res;
455}
456
Serguei Katkov9ee45192014-07-17 14:39:03 +0700457RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class, bool required) {
buzbeea0cd2d72014-06-01 09:33:49 -0700458 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700459 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700460 return AllocTempDouble(required);
buzbeeb01bf152014-05-13 15:59:07 -0700461 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700462 return AllocTempWide(required);
buzbeeb01bf152014-05-13 15:59:07 -0700463}
464
Serguei Katkov9ee45192014-07-17 14:39:03 +0700465RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class, bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700466 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700467 return AllocTempSingle(required);
buzbeea0cd2d72014-06-01 09:33:49 -0700468 } else if (reg_class == kRefReg) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700469 return AllocTempRef(required);
buzbeeb01bf152014-05-13 15:59:07 -0700470 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700471 return AllocTemp(required);
buzbeeb01bf152014-05-13 15:59:07 -0700472}
473
buzbee091cc402014-03-31 10:14:40 -0700474RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
475 RegStorage res;
476 GrowableArray<RegisterInfo*>::Iterator it(&regs);
477 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
478 if ((info->SReg() == s_reg) && info->IsLive()) {
479 res = info->GetReg();
480 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 }
482 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 return res;
484}
485
buzbee091cc402014-03-31 10:14:40 -0700486RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
487 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700488 if (reg_class == kRefReg) {
489 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700490 CheckRegStorage(reg, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700491 }
492 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700493 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 }
buzbee091cc402014-03-31 10:14:40 -0700495 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700496 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700497 reg = FindLiveReg(wide || reg_class == kRefReg ? reg_pool_->core64_regs_ :
498 reg_pool_->core_regs_, s_reg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100499 } else {
500 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
501 }
buzbee091cc402014-03-31 10:14:40 -0700502 }
503 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700504 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700505 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700506 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
507 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700508 reg = RegStorage::MakeRegPair(reg, high_reg);
509 MarkWide(reg);
510 } else {
buzbee30adc732014-05-09 15:10:18 -0700511 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700512 reg = RegStorage::InvalidReg();
513 }
514 }
buzbee30adc732014-05-09 15:10:18 -0700515 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
516 // Width mismatch - don't try to reuse.
517 reg = RegStorage::InvalidReg();
518 }
519 }
520 if (reg.Valid()) {
521 if (reg.IsPair()) {
522 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
523 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
524 if (info_low->IsTemp()) {
525 info_low->MarkInUse();
526 }
527 if (info_high->IsTemp()) {
528 info_high->MarkInUse();
529 }
530 } else {
buzbee091cc402014-03-31 10:14:40 -0700531 RegisterInfo* info = GetRegInfo(reg);
532 if (info->IsTemp()) {
533 info->MarkInUse();
534 }
535 }
buzbee30adc732014-05-09 15:10:18 -0700536 } else {
537 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
538 ClobberSReg(s_reg);
539 if (wide) {
540 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700541 }
542 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700543 CheckRegStorage(reg, WidenessCheck::kIgnoreWide,
544 reg_class == kRefReg ? RefCheck::kCheckRef : RefCheck::kIgnoreRef,
545 FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700546 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547}
548
buzbee2700f7e2014-03-07 09:46:20 -0800549void Mir2Lir::FreeTemp(RegStorage reg) {
550 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700551 FreeTemp(reg.GetLow());
552 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800553 } else {
buzbee091cc402014-03-31 10:14:40 -0700554 RegisterInfo* p = GetRegInfo(reg);
555 if (p->IsTemp()) {
556 p->MarkFree();
557 p->SetIsWide(false);
558 p->SetPartner(reg);
559 }
buzbee2700f7e2014-03-07 09:46:20 -0800560 }
561}
562
buzbee082833c2014-05-17 23:16:26 -0700563void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
564 DCHECK(rl_keep.wide);
565 DCHECK(rl_free.wide);
566 int free_low = rl_free.reg.GetLowReg();
567 int free_high = rl_free.reg.GetHighReg();
568 int keep_low = rl_keep.reg.GetLowReg();
569 int keep_high = rl_keep.reg.GetHighReg();
570 if ((free_low != keep_low) && (free_low != keep_high) &&
571 (free_high != keep_low) && (free_high != keep_high)) {
572 // No overlap, free both
573 FreeTemp(rl_free.reg);
574 }
575}
576
buzbee262b2992014-03-27 11:22:43 -0700577bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700578 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800579 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700580 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
581 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700582 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700583 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800584 } else {
buzbee091cc402014-03-31 10:14:40 -0700585 RegisterInfo* p = GetRegInfo(reg);
586 res = p->IsLive();
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::IsTemp(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
buzbee262b2992014-03-27 11:22:43 -0700604bool Mir2Lir::IsPromoted(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->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800610 } else {
buzbee091cc402014-03-31 10:14:40 -0700611 RegisterInfo* p = GetRegInfo(reg);
612 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800613 }
buzbee091cc402014-03-31 10:14:40 -0700614 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615}
616
buzbee2700f7e2014-03-07 09:46:20 -0800617bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700618 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800619 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700620 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
621 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
622 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800623 } else {
buzbee091cc402014-03-31 10:14:40 -0700624 RegisterInfo* p = GetRegInfo(reg);
625 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800626 }
buzbee091cc402014-03-31 10:14:40 -0700627 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800628}
629
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630/*
631 * Similar to AllocTemp(), but forces the allocation of a specific
632 * register. No check is made to see if the register was previously
633 * allocated. Use with caution.
634 */
buzbee2700f7e2014-03-07 09:46:20 -0800635void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700636 DCHECK(IsTemp(reg));
637 if (reg.IsPair()) {
638 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
639 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
640 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700641 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700642 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700643 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700644 } else {
645 RegisterInfo* p = GetRegInfo(reg);
646 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700647 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700648 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649}
650
buzbee2700f7e2014-03-07 09:46:20 -0800651void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700652 if (reg.IsPair()) {
653 GetRegInfo(reg.GetLow())->ResetDefBody();
654 GetRegInfo(reg.GetHigh())->ResetDefBody();
655 } else {
656 GetRegInfo(reg)->ResetDefBody();
657 }
buzbee2700f7e2014-03-07 09:46:20 -0800658}
659
buzbee091cc402014-03-31 10:14:40 -0700660void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
661 RegisterInfo* info = nullptr;
662 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
663 if (IsTemp(rs)) {
664 info = GetRegInfo(reg);
665 }
666 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
667 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
668 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700670 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 break;
buzbee091cc402014-03-31 10:14:40 -0700672 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 }
674 }
675}
676
677/*
678 * Mark the beginning and end LIR of a def sequence. Note that
679 * on entry start points to the LIR prior to the beginning of the
680 * sequence.
681 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700682void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 DCHECK(!rl.wide);
684 DCHECK(start && start->next);
685 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700686 RegisterInfo* p = GetRegInfo(rl.reg);
687 p->SetDefStart(start->next);
688 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689}
690
691/*
692 * Mark the beginning and end LIR of a def sequence. Note that
693 * on entry start points to the LIR prior to the beginning of the
694 * sequence.
695 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700696void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 DCHECK(rl.wide);
698 DCHECK(start && start->next);
699 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700700 RegisterInfo* p;
701 if (rl.reg.IsPair()) {
702 p = GetRegInfo(rl.reg.GetLow());
703 ResetDef(rl.reg.GetHigh()); // Only track low of pair
704 } else {
705 p = GetRegInfo(rl.reg);
706 }
707 p->SetDefStart(start->next);
708 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709}
710
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700711void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700713 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
714 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 }
buzbee091cc402014-03-31 10:14:40 -0700716 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717}
718
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700719void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700721 // If pair, only track low reg of pair.
722 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
723 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
724 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 }
buzbee091cc402014-03-31 10:14:40 -0700726 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727}
728
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700729void Mir2Lir::ResetDefTracking() {
buzbeea0cd2d72014-06-01 09:33:49 -0700730 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
731 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee091cc402014-03-31 10:14:40 -0700732 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700733 }
734}
735
buzbeeba574512014-05-12 15:13:16 -0700736void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700737 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
738 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700739 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700740 }
741}
742
743void Mir2Lir::FlushRegWide(RegStorage reg) {
744 if (reg.IsPair()) {
745 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
746 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
747 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
buzbeeb5860fb2014-06-21 15:31:01 -0700748 (info1->Partner().ExactlyEquals(info2->GetReg())) &&
749 (info2->Partner().ExactlyEquals(info1->GetReg())));
buzbee091cc402014-03-31 10:14:40 -0700750 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
751 if (!(info1->IsTemp() && info2->IsTemp())) {
752 /* Should not happen. If it does, there's a problem in eval_loc */
753 LOG(FATAL) << "Long half-temp, half-promoted";
754 }
755
756 info1->SetIsDirty(false);
757 info2->SetIsDirty(false);
758 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
759 info1 = info2;
760 }
761 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100762 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700763 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700764 }
765 } else {
766 RegisterInfo* info = GetRegInfo(reg);
767 if (info->IsLive() && info->IsDirty()) {
768 info->SetIsDirty(false);
769 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100770 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700771 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700772 }
773 }
774}
775
776void Mir2Lir::FlushReg(RegStorage reg) {
777 DCHECK(!reg.IsPair());
778 RegisterInfo* info = GetRegInfo(reg);
779 if (info->IsLive() && info->IsDirty()) {
780 info->SetIsDirty(false);
781 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100782 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700783 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784 }
785}
786
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800787void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700788 if (info->IsWide()) {
789 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800790 } else {
buzbee091cc402014-03-31 10:14:40 -0700791 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 }
793}
794
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700795void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700796 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
797 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700798 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700799 FlushSpecificReg(info);
800 }
buzbee30adc732014-05-09 15:10:18 -0700801 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700802 info->SetIsWide(false);
803 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700804}
805
806
buzbee2700f7e2014-03-07 09:46:20 -0800807bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 if (reg_class == kAnyReg) {
809 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700810 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
811 /*
812 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
813 * with width here - that should be checked at a higher level (if needed).
814 */
buzbee091cc402014-03-31 10:14:40 -0700815 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 } else {
buzbee091cc402014-03-31 10:14:40 -0700817 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 }
819}
820
buzbee091cc402014-03-31 10:14:40 -0700821void Mir2Lir::MarkLive(RegLocation loc) {
822 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700823 if (!IsTemp(reg)) {
824 return;
825 }
buzbee091cc402014-03-31 10:14:40 -0700826 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700827 if (s_reg == INVALID_SREG) {
828 // Can't be live if no associated sreg.
829 if (reg.IsPair()) {
830 GetRegInfo(reg.GetLow())->MarkDead();
831 GetRegInfo(reg.GetHigh())->MarkDead();
832 } else {
833 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700834 }
buzbee082833c2014-05-17 23:16:26 -0700835 } else {
836 if (reg.IsPair()) {
837 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
838 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
839 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
840 (info_hi->SReg() == s_reg)) {
841 return; // Already live.
842 }
843 ClobberSReg(s_reg);
844 ClobberSReg(s_reg + 1);
845 info_lo->MarkLive(s_reg);
846 info_hi->MarkLive(s_reg + 1);
847 } else {
848 RegisterInfo* info = GetRegInfo(reg);
849 if (info->IsLive() && (info->SReg() == s_reg)) {
850 return; // Already live.
851 }
852 ClobberSReg(s_reg);
853 if (loc.wide) {
854 ClobberSReg(s_reg + 1);
855 }
856 info->MarkLive(s_reg);
857 }
858 if (loc.wide) {
859 MarkWide(reg);
860 } else {
861 MarkNarrow(reg);
862 }
buzbee091cc402014-03-31 10:14:40 -0700863 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700864}
865
buzbee2700f7e2014-03-07 09:46:20 -0800866void Mir2Lir::MarkTemp(RegStorage reg) {
867 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700868 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700869 tempreg_info_.Insert(info);
870 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700871}
872
buzbee2700f7e2014-03-07 09:46:20 -0800873void Mir2Lir::UnmarkTemp(RegStorage reg) {
874 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700875 RegisterInfo* info = GetRegInfo(reg);
876 tempreg_info_.Delete(info);
877 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800878}
879
buzbee091cc402014-03-31 10:14:40 -0700880void Mir2Lir::MarkWide(RegStorage reg) {
881 if (reg.IsPair()) {
882 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
883 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700884 // Unpair any old partners.
buzbeeb5860fb2014-06-21 15:31:01 -0700885 if (info_lo->IsWide() && info_lo->Partner().NotExactlyEquals(info_hi->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700886 GetRegInfo(info_lo->Partner())->SetIsWide(false);
887 }
buzbeeb5860fb2014-06-21 15:31:01 -0700888 if (info_hi->IsWide() && info_hi->Partner().NotExactlyEquals(info_lo->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700889 GetRegInfo(info_hi->Partner())->SetIsWide(false);
890 }
buzbee091cc402014-03-31 10:14:40 -0700891 info_lo->SetIsWide(true);
892 info_hi->SetIsWide(true);
893 info_lo->SetPartner(reg.GetHigh());
894 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800895 } else {
buzbee091cc402014-03-31 10:14:40 -0700896 RegisterInfo* info = GetRegInfo(reg);
897 info->SetIsWide(true);
898 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899 }
900}
901
buzbee082833c2014-05-17 23:16:26 -0700902void Mir2Lir::MarkNarrow(RegStorage reg) {
903 DCHECK(!reg.IsPair());
904 RegisterInfo* info = GetRegInfo(reg);
905 info->SetIsWide(false);
906 info->SetPartner(reg);
907}
908
buzbee091cc402014-03-31 10:14:40 -0700909void Mir2Lir::MarkClean(RegLocation loc) {
910 if (loc.reg.IsPair()) {
911 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
912 info->SetIsDirty(false);
913 info = GetRegInfo(loc.reg.GetHigh());
914 info->SetIsDirty(false);
915 } else {
916 RegisterInfo* info = GetRegInfo(loc.reg);
917 info->SetIsDirty(false);
918 }
919}
920
921// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700922void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 if (loc.home) {
924 // If already home, can't be dirty
925 return;
926 }
buzbee091cc402014-03-31 10:14:40 -0700927 if (loc.reg.IsPair()) {
928 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
929 info->SetIsDirty(true);
930 info = GetRegInfo(loc.reg.GetHigh());
931 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800932 } else {
buzbee091cc402014-03-31 10:14:40 -0700933 RegisterInfo* info = GetRegInfo(loc.reg);
934 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700935 }
936}
937
buzbee2700f7e2014-03-07 09:46:20 -0800938void Mir2Lir::MarkInUse(RegStorage reg) {
939 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700940 GetRegInfo(reg.GetLow())->MarkInUse();
941 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800942 } else {
buzbee091cc402014-03-31 10:14:40 -0700943 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800944 }
945}
946
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700947bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700948 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700949 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700950 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
951 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700952 int my_sreg = info->SReg();
953 RegStorage partner_reg = info->Partner();
954 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700955 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700956 DCHECK(partner->IsWide());
957 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700958 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700959 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700960 if (my_sreg == INVALID_SREG) {
961 DCHECK_EQ(partner_sreg, INVALID_SREG);
962 } else {
963 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700964 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700965 }
966 }
buzbee082833c2014-05-17 23:16:26 -0700967 if (info->Master() != info) {
968 // Aliased.
969 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
970 // If I'm live, master should not be live, but should show liveness in alias set.
971 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
972 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700973 }
buzbee642fe342014-05-23 16:04:08 -0700974// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700975 }
976 if (info->IsAliased()) {
977 // Has child aliases.
978 DCHECK_EQ(info->Master(), info);
979 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
980 // Master live, no child should be dead - all should show liveness in set.
981 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
982 DCHECK(!p->IsDead());
983 DCHECK_EQ(p->SReg(), INVALID_SREG);
984 }
985 } else if (!info->IsDead()) {
986 // Master not live, one or more aliases must be.
987 bool live_alias = false;
988 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
989 live_alias |= p->IsLive();
990 }
991 DCHECK(live_alias);
992 }
993 }
994 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
995 // If not fully live, should have INVALID_SREG and def's should be null.
996 DCHECK(info->DefStart() == nullptr);
997 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700998 }
999 }
1000 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001}
1002
1003/*
1004 * Return an updated location record with current in-register status.
1005 * If the value lives in live temps, reflect that fact. No code
1006 * is generated. If the live value is part of an older pair,
1007 * clobber both low and high.
1008 * TUNING: clobbering both is a bit heavy-handed, but the alternative
1009 * is a bit complex when dealing with FP regs. Examine code to see
1010 * if it's worthwhile trying to be more clever here.
1011 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001012RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 DCHECK(!loc.wide);
1014 DCHECK(CheckCorePoolSanity());
1015 if (loc.location != kLocPhysReg) {
1016 DCHECK((loc.location == kLocDalvikFrame) ||
1017 (loc.location == kLocCompilerTemp));
Andreas Gampe4b537a82014-06-30 22:24:53 -07001018 RegStorage reg = AllocLiveReg(loc.s_reg_low, loc.ref ? kRefReg : kAnyReg, false);
buzbee091cc402014-03-31 10:14:40 -07001019 if (reg.Valid()) {
1020 bool match = true;
1021 RegisterInfo* info = GetRegInfo(reg);
1022 match &= !reg.IsPair();
1023 match &= !info->IsWide();
1024 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001025 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001026 loc.reg = reg;
1027 } else {
1028 Clobber(reg);
1029 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001030 }
1031 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001032 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 return loc;
1035}
1036
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001037RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001038 DCHECK(loc.wide);
1039 DCHECK(CheckCorePoolSanity());
1040 if (loc.location != kLocPhysReg) {
1041 DCHECK((loc.location == kLocDalvikFrame) ||
1042 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001043 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1044 if (reg.Valid()) {
1045 bool match = true;
1046 if (reg.IsPair()) {
1047 // If we've got a register pair, make sure that it was last used as the same pair.
1048 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1049 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1050 match &= info_lo->IsWide();
1051 match &= info_hi->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001052 match &= (info_lo->Partner().ExactlyEquals(info_hi->GetReg()));
1053 match &= (info_hi->Partner().ExactlyEquals(info_lo->GetReg()));
buzbee091cc402014-03-31 10:14:40 -07001054 } else {
1055 RegisterInfo* info = GetRegInfo(reg);
1056 match &= info->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001057 match &= (info->GetReg().ExactlyEquals(info->Partner()));
buzbee091cc402014-03-31 10:14:40 -07001058 }
1059 if (match) {
1060 loc.location = kLocPhysReg;
1061 loc.reg = reg;
1062 } else {
1063 Clobber(reg);
1064 FreeTemp(reg);
1065 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001067 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 }
1069 return loc;
1070}
1071
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001073RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 if (loc.wide)
1075 return UpdateLocWide(loc);
1076 else
1077 return UpdateLoc(loc);
1078}
1079
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001080RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082
1083 loc = UpdateLocWide(loc);
1084
1085 /* If already in registers, we can assume proper form. Right reg class? */
1086 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001087 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001088 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001089 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001090 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001091 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001092 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001093 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001094 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001095 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001097 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 return loc;
1099 }
1100
1101 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1102 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1103
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001104 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001105 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107 if (update) {
1108 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001109 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001111 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 return loc;
1113}
1114
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001115RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001116 // Narrow reg_class if the loc is a ref.
1117 if (loc.ref && reg_class == kAnyReg) {
1118 reg_class = kRefReg;
1119 }
1120
buzbee091cc402014-03-31 10:14:40 -07001121 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001123 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124
1125 loc = UpdateLoc(loc);
1126
1127 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001128 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001129 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001130 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001131 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001132 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001133 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001134 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001135 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001136 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001137 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 return loc;
1139 }
1140
1141 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1142
buzbee2700f7e2014-03-07 09:46:20 -08001143 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001144 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145
1146 if (update) {
1147 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001148 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001150 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 return loc;
1152}
1153
1154/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001155void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1157 RegLocation loc = mir_graph_->reg_location_[i];
1158 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1159 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001160 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001161 if (loc.fp) {
1162 if (loc.wide) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001163 if (WideFPRsAreAliases()) {
1164 // Floats and doubles can be counted together.
1165 counts[p_map_idx].count += use_count;
1166 } else {
1167 // Treat doubles as a unit, using upper half of fp_counts array.
1168 counts[p_map_idx + num_regs].count += use_count;
1169 }
buzbeec729a6b2013-09-14 16:04:31 -07001170 i++;
1171 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001172 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001173 }
1174 } else if (!IsInexpensiveConstant(loc)) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001175 if (loc.wide && WideGPRsAreAliases()) {
1176 // Longs and doubles can be counted together.
buzbeeb5860fb2014-06-21 15:31:01 -07001177 i++;
buzbeeb5860fb2014-06-21 15:31:01 -07001178 }
Serguei Katkov59a42af2014-07-05 00:55:46 +07001179 counts[p_map_idx].count += use_count;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 }
1182}
1183
1184/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001185static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1187 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001188 // Note that we fall back to sorting on reg so we get stable output
1189 // on differing qsort implementations (such as on host and target or
1190 // between local host and build servers).
1191 return (op1->count == op2->count)
1192 ? (op1->s_reg - op2->s_reg)
1193 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194}
1195
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001196void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001197 LOG(INFO) << msg;
1198 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001199 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1200 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001201 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001202 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001203 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 }
1205}
1206
1207/*
1208 * Note: some portions of this code required even if the kPromoteRegs
1209 * optimization is disabled.
1210 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001211void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001213 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001215 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1216 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001217 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218
1219 // Allow target code to add any special registers
1220 AdjustSpillMask();
1221
1222 /*
1223 * Simple register promotion. Just do a static count of the uses
1224 * of Dalvik registers. Note that we examine the SSA names, but
1225 * count based on original Dalvik register name. Count refs
1226 * separately based on type in order to give allocation
1227 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001228 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 * reg.
1230 * TUNING: replace with linear scan once we have the ability
1231 * to describe register live ranges for GC.
1232 */
buzbeeb5860fb2014-06-21 15:31:01 -07001233 size_t core_reg_count_size = cu_->target64 ? num_regs * 2 : num_regs;
1234 size_t fp_reg_count_size = num_regs * 2;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235 RefCounts *core_regs =
buzbeeb5860fb2014-06-21 15:31:01 -07001236 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * core_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001237 kArenaAllocRegAlloc));
buzbeeb5860fb2014-06-21 15:31:01 -07001238 RefCounts *fp_regs =
1239 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * fp_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001240 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001241 // Set ssa names for original Dalvik registers
1242 for (int i = 0; i < dalvik_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001243 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001245
1246 // Set ssa names for compiler temporaries
1247 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1248 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1249 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeeb5860fb2014-06-21 15:31:01 -07001250 fp_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001251 }
1252
buzbeeb5860fb2014-06-21 15:31:01 -07001253 // Duplicate in upper half to represent possible wide starting sregs.
1254 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1255 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1256 }
1257 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1258 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001259 }
1260
1261 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001262 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001263
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264
1265 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001266 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1267 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268
1269 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001270 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1271 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001272 }
1273
1274 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001275 // Promote fp regs
1276 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1277 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1278 size_t p_map_idx = SRegToPMap(low_sreg);
1279 RegStorage reg = RegStorage::InvalidReg();
1280 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1281 // TODO: break out the Thumb2-specific code.
1282 if (cu_->instruction_set == kThumb2) {
1283 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1284 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001285 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001286 // Ignore result - if can't alloc double may still be able to alloc singles.
1287 AllocPreservedDouble(low_sreg);
1288 }
1289 // Continue regardless of success - might still be able to grab a single.
1290 continue;
1291 } else {
1292 reg = AllocPreservedSingle(low_sreg);
1293 }
1294 } else {
1295 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001296 }
buzbee2700f7e2014-03-07 09:46:20 -08001297 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001298 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001299 }
1300 }
1301 }
1302
1303 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001304 for (size_t i = 0; (i < core_reg_count_size) &&
1305 (core_regs[i].count >= promotion_threshold); i++) {
1306 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1307 size_t p_map_idx = SRegToPMap(low_sreg);
1308 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1309 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001310 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001311 break; // No more left
1312 }
1313 }
1314 }
1315 }
1316
1317 // Now, update SSA names to new home locations
1318 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1319 RegLocation *curr = &mir_graph_->reg_location_[i];
1320 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001321 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 -07001322 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001323 RegStorage reg = RegStorage::InvalidReg();
1324 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1325 if (wide && cu_->instruction_set == kThumb2) {
1326 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1327 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001328 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001329 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1330 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001331 }
1332 }
1333 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001334 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 }
buzbeeb5860fb2014-06-21 15:31:01 -07001336 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1337 if (wide && !cu_->target64) {
1338 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1339 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1340 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1341 }
1342 } else {
1343 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1344 }
1345 }
1346 if (reg.Valid()) {
1347 curr->reg = reg;
1348 curr->location = kLocPhysReg;
1349 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 }
1351 }
1352 if (cu_->verbose) {
1353 DumpPromotionMap();
1354 }
1355}
1356
1357/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001358int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001360 fp_spill_mask_, frame_size_, v_reg,
1361 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362}
1363
1364/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001365int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1367}
1368
1369/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001370RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1371 RegLocation res;
1372 switch (reg_class) {
1373 case kRefReg: LOG(FATAL); break;
1374 case kFPReg: res = LocCReturnDouble(); break;
1375 default: res = LocCReturnWide(); break;
1376 }
buzbee082833c2014-05-17 23:16:26 -07001377 Clobber(res.reg);
1378 LockTemp(res.reg);
1379 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001380 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 return res;
1382}
1383
buzbeea0cd2d72014-06-01 09:33:49 -07001384RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1385 RegLocation res;
1386 switch (reg_class) {
1387 case kRefReg: res = LocCReturnRef(); break;
1388 case kFPReg: res = LocCReturnFloat(); break;
1389 default: res = LocCReturn(); break;
1390 }
buzbee091cc402014-03-31 10:14:40 -07001391 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001393 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 } else {
buzbee091cc402014-03-31 10:14:40 -07001395 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001397 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398 return res;
1399}
1400
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001401void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001402 DoPromotion();
1403
1404 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1405 LOG(INFO) << "After Promotion";
1406 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1407 }
1408
1409 /* Set the frame size */
1410 frame_size_ = ComputeFrameSize();
1411}
1412
1413/*
1414 * Get the "real" sreg number associated with an s_reg slot. In general,
1415 * s_reg values passed through codegen are the SSA names created by
1416 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1417 * array. However, renaming is accomplished by simply replacing RegLocation
1418 * entries in the reglocation[] array. Therefore, when location
1419 * records for operands are first created, we need to ask the locRecord
1420 * identified by the dataflow pass what it's new name is.
1421 */
1422int Mir2Lir::GetSRegHi(int lowSreg) {
1423 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1424}
1425
buzbee091cc402014-03-31 10:14:40 -07001426bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001427 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 return true;
1429}
1430
Brian Carlstrom7940e442013-07-12 13:46:57 -07001431} // namespace art