blob: 6305b22dedab0c3d2271ecf7dfb38bc821f15ae7 [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() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010031 for (RegisterInfo* info : tempreg_info_) {
buzbee091cc402014-03-31 10:14:40 -070032 info->MarkFree();
Brian Carlstrom7940e442013-07-12 13:46:57 -070033 }
34 // Reset temp tracking sanity check.
35 if (kIsDebugBuild) {
36 live_sreg_ = INVALID_SREG;
37 }
38}
39
Vladimir Marko8dea81c2014-06-06 14:50:36 +010040Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, const ResourceMask& mask)
buzbee30adc732014-05-09 15:10:18 -070041 : reg_(r), is_temp_(false), wide_value_(false), dirty_(false), aliased_(false), partner_(r),
buzbeeba574512014-05-12 15:13:16 -070042 s_reg_(INVALID_SREG), def_use_mask_(mask), master_(this), def_start_(nullptr),
43 def_end_(nullptr), alias_chain_(nullptr) {
buzbee091cc402014-03-31 10:14:40 -070044 switch (r.StorageSize()) {
45 case 0: storage_mask_ = 0xffffffff; break;
46 case 4: storage_mask_ = 0x00000001; break;
47 case 8: storage_mask_ = 0x00000003; break;
48 case 16: storage_mask_ = 0x0000000f; break;
49 case 32: storage_mask_ = 0x000000ff; break;
50 case 64: storage_mask_ = 0x0000ffff; break;
51 case 128: storage_mask_ = 0xffffffff; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 }
buzbee091cc402014-03-31 10:14:40 -070053 used_storage_ = r.Valid() ? ~storage_mask_ : storage_mask_;
buzbee30adc732014-05-09 15:10:18 -070054 liveness_ = used_storage_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070055}
56
buzbee091cc402014-03-31 10:14:40 -070057Mir2Lir::RegisterPool::RegisterPool(Mir2Lir* m2l, ArenaAllocator* arena,
Vladimir Marko089142c2014-06-05 10:57:05 +010058 const ArrayRef<const RegStorage>& core_regs,
59 const ArrayRef<const RegStorage>& core64_regs,
60 const ArrayRef<const RegStorage>& sp_regs,
61 const ArrayRef<const RegStorage>& dp_regs,
62 const ArrayRef<const RegStorage>& reserved_regs,
63 const ArrayRef<const RegStorage>& reserved64_regs,
64 const ArrayRef<const RegStorage>& core_temps,
65 const ArrayRef<const RegStorage>& core64_temps,
66 const ArrayRef<const RegStorage>& sp_temps,
67 const ArrayRef<const RegStorage>& dp_temps) :
Vladimir Markoe39c54e2014-09-22 14:50:02 +010068 core_regs_(arena->Adapter()), next_core_reg_(0),
69 core64_regs_(arena->Adapter()), next_core64_reg_(0),
70 sp_regs_(arena->Adapter()), next_sp_reg_(0),
71 dp_regs_(arena->Adapter()), next_dp_reg_(0), m2l_(m2l) {
buzbee091cc402014-03-31 10:14:40 -070072 // Initialize the fast lookup map.
Vladimir Markoe39c54e2014-09-22 14:50:02 +010073 m2l_->reginfo_map_.clear();
74 m2l_->reginfo_map_.resize(RegStorage::kMaxRegs, nullptr);
buzbee091cc402014-03-31 10:14:40 -070075
76 // Construct the register pool.
Vladimir Markoe39c54e2014-09-22 14:50:02 +010077 core_regs_.reserve(core_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010078 for (const RegStorage& reg : core_regs) {
buzbee091cc402014-03-31 10:14:40 -070079 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010080 m2l_->reginfo_map_[reg.GetReg()] = info;
81 core_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -070082 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010083 core64_regs_.reserve(core64_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010084 for (const RegStorage& reg : core64_regs) {
buzbeeb01bf152014-05-13 15:59:07 -070085 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010086 m2l_->reginfo_map_[reg.GetReg()] = info;
87 core64_regs_.push_back(info);
buzbeeb01bf152014-05-13 15:59:07 -070088 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010089 sp_regs_.reserve(sp_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010090 for (const RegStorage& reg : sp_regs) {
buzbee091cc402014-03-31 10:14:40 -070091 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010092 m2l_->reginfo_map_[reg.GetReg()] = info;
93 sp_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -070094 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010095 dp_regs_.reserve(dp_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010096 for (const RegStorage& reg : dp_regs) {
buzbee091cc402014-03-31 10:14:40 -070097 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010098 m2l_->reginfo_map_[reg.GetReg()] = info;
99 dp_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -0700100 }
101
102 // Keep special registers from being allocated.
103 for (RegStorage reg : reserved_regs) {
104 m2l_->MarkInUse(reg);
105 }
buzbeeb01bf152014-05-13 15:59:07 -0700106 for (RegStorage reg : reserved64_regs) {
107 m2l_->MarkInUse(reg);
108 }
buzbee091cc402014-03-31 10:14:40 -0700109
110 // Mark temp regs - all others not in use can be used for promotion
111 for (RegStorage reg : core_temps) {
112 m2l_->MarkTemp(reg);
113 }
buzbeeb01bf152014-05-13 15:59:07 -0700114 for (RegStorage reg : core64_temps) {
115 m2l_->MarkTemp(reg);
116 }
buzbee091cc402014-03-31 10:14:40 -0700117 for (RegStorage reg : sp_temps) {
118 m2l_->MarkTemp(reg);
119 }
120 for (RegStorage reg : dp_temps) {
121 m2l_->MarkTemp(reg);
122 }
123
124 // Add an entry for InvalidReg with zero'd mask.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100125 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), kEncodeNone);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100126 m2l_->reginfo_map_[RegStorage::InvalidReg().GetReg()] = invalid_reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700127
128 // Existence of core64 registers implies wide references.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100129 if (core64_regs_.size() != 0) {
buzbeea0cd2d72014-06-01 09:33:49 -0700130 ref_regs_ = &core64_regs_;
131 next_ref_reg_ = &next_core64_reg_;
132 } else {
133 ref_regs_ = &core_regs_;
134 next_ref_reg_ = &next_core_reg_;
135 }
buzbee091cc402014-03-31 10:14:40 -0700136}
137
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100138void Mir2Lir::DumpRegPool(ArenaVector<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 LOG(INFO) << "================================================";
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100140 for (RegisterInfo* info : *regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700142 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
143 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
144 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
145 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 }
147 LOG(INFO) << "================================================";
148}
149
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700150void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700151 DumpRegPool(&reg_pool_->core_regs_);
buzbeea0cd2d72014-06-01 09:33:49 -0700152 DumpRegPool(&reg_pool_->core64_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153}
154
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700155void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700156 DumpRegPool(&reg_pool_->sp_regs_);
157 DumpRegPool(&reg_pool_->dp_regs_);
158}
159
160void Mir2Lir::DumpRegPools() {
161 LOG(INFO) << "Core registers";
162 DumpCoreRegPool();
163 LOG(INFO) << "FP registers";
164 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165}
166
buzbee2700f7e2014-03-07 09:46:20 -0800167void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700168 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700169 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700170 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700171 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700172 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800173 } else {
buzbee30adc732014-05-09 15:10:18 -0700174 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700175 if (info->IsTemp() && !info->IsDead()) {
buzbeeb5860fb2014-06-21 15:31:01 -0700176 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700177 ClobberBody(GetRegInfo(info->Partner()));
178 }
buzbeeba574512014-05-12 15:13:16 -0700179 ClobberBody(info);
180 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700181 ClobberAliases(info, info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700182 } else {
183 RegisterInfo* master = info->Master();
184 if (info != master) {
185 ClobberBody(info->Master());
buzbee642fe342014-05-23 16:04:08 -0700186 ClobberAliases(info->Master(), info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700187 }
188 }
buzbee30adc732014-05-09 15:10:18 -0700189 }
buzbee2700f7e2014-03-07 09:46:20 -0800190 }
191}
192
buzbee642fe342014-05-23 16:04:08 -0700193void Mir2Lir::ClobberAliases(RegisterInfo* info, uint32_t clobber_mask) {
buzbeeba574512014-05-12 15:13:16 -0700194 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
195 alias = alias->GetAliasChain()) {
196 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee642fe342014-05-23 16:04:08 -0700197 // Only clobber if we have overlap.
198 if ((alias->StorageMask() & clobber_mask) != 0) {
199 ClobberBody(alias);
200 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 }
202}
203
204/*
205 * Break the association between a Dalvik vreg and a physical temp register of either register
206 * class.
207 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
208 * in the register utilities, is is also used by code gen routines to work around a deficiency in
209 * local register allocation, which fails to distinguish between the "in" and "out" identities
210 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
211 * is used both as the source and destination register of an operation in which the type
212 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
213 * addressed.
214 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700215void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700216 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700217 if (kIsDebugBuild && s_reg == live_sreg_) {
218 live_sreg_ = INVALID_SREG;
219 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100220 for (RegisterInfo* info : tempreg_info_) {
buzbee30adc732014-05-09 15:10:18 -0700221 if (info->SReg() == s_reg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700222 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700223 // Dealing with a pair - clobber the other half.
224 DCHECK(!info->IsAliased());
225 ClobberBody(GetRegInfo(info->Partner()));
226 }
buzbeeba574512014-05-12 15:13:16 -0700227 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700228 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700229 ClobberAliases(info, info->StorageMask());
buzbee30adc732014-05-09 15:10:18 -0700230 }
buzbee091cc402014-03-31 10:14:40 -0700231 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 }
233 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234}
235
236/*
237 * SSA names associated with the initial definitions of Dalvik
238 * registers are the same as the Dalvik register number (and
239 * thus take the same position in the promotion_map. However,
240 * the special Method* and compiler temp resisters use negative
241 * v_reg numbers to distinguish them and can have an arbitrary
242 * ssa name (above the last original Dalvik register). This function
243 * maps SSA names to positions in the promotion_map array.
244 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700245int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
247 DCHECK_GE(s_reg, 0);
248 int v_reg = mir_graph_->SRegToVReg(s_reg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700249 return v_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250}
251
buzbee091cc402014-03-31 10:14:40 -0700252// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800253void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 int p_map_idx = SRegToPMap(s_reg);
255 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700256 int reg_num = reg.GetRegNum();
257 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800258 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800260 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 num_core_spills_++;
262 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800263 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264}
265
buzbee091cc402014-03-31 10:14:40 -0700266/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800267RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
268 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700269 /*
270 * Note: it really doesn't matter much whether we allocate from the core or core64
271 * pool for 64-bit targets - but for some targets it does matter whether allocations
272 * happens from the single or double pool. This entire section of code could stand
273 * a good refactoring.
274 */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100275 for (RegisterInfo* info : reg_pool_->core_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700276 if (!info->IsTemp() && !info->InUse()) {
277 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 RecordCorePromotion(res, s_reg);
279 break;
280 }
281 }
282 return res;
283}
284
buzbeeb5860fb2014-06-21 15:31:01 -0700285void Mir2Lir::RecordFpPromotion(RegStorage reg, int s_reg) {
286 DCHECK_NE(cu_->instruction_set, kThumb2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 int p_map_idx = SRegToPMap(s_reg);
288 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700289 int reg_num = reg.GetRegNum();
buzbee091cc402014-03-31 10:14:40 -0700290 GetRegInfo(reg)->MarkInUse();
buzbeeb5860fb2014-06-21 15:31:01 -0700291 fp_spill_mask_ |= (1 << reg_num);
292 // Include reg for later sort
293 fp_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
294 num_fp_spills_++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbeeb5860fb2014-06-21 15:31:01 -0700296 promotion_map_[p_map_idx].fp_reg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297}
298
buzbeeb5860fb2014-06-21 15:31:01 -0700299// Reserve a callee-save floating point.
300RegStorage Mir2Lir::AllocPreservedFpReg(int s_reg) {
301 /*
302 * For targets other than Thumb2, it doesn't matter whether we allocate from
303 * the sp_regs_ or dp_regs_ pool. Some refactoring is in order here.
304 */
305 DCHECK_NE(cu_->instruction_set, kThumb2);
buzbee2700f7e2014-03-07 09:46:20 -0800306 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100307 for (RegisterInfo* info : reg_pool_->sp_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700308 if (!info->IsTemp() && !info->InUse()) {
309 res = info->GetReg();
buzbeeb5860fb2014-06-21 15:31:01 -0700310 RecordFpPromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 break;
312 }
313 }
314 return res;
315}
316
buzbeeb5860fb2014-06-21 15:31:01 -0700317// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
buzbee2700f7e2014-03-07 09:46:20 -0800318RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
319 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700320 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedDouble";
321 return res;
322}
323
324// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
325RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
326 RegStorage res;
327 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedSingle";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 return res;
329}
330
buzbee091cc402014-03-31 10:14:40 -0700331
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100332RegStorage Mir2Lir::AllocTempBody(ArenaVector<RegisterInfo*>& regs, int* next_temp, bool required) {
333 int num_regs = regs.size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700335 for (int i = 0; i< num_regs; i++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100336 if (next >= num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 next = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100338 }
339 RegisterInfo* info = regs[next];
buzbee30adc732014-05-09 15:10:18 -0700340 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700341 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee88a6b412014-08-25 09:34:03 -0700342 // If it's wide, split it up.
343 if (info->IsWide()) {
344 // If the pair was associated with a wide value, unmark the partner as well.
345 if (info->SReg() != INVALID_SREG) {
346 RegisterInfo* partner = GetRegInfo(info->Partner());
347 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
348 DCHECK(partner->IsWide());
349 partner->SetIsWide(false);
350 }
351 info->SetIsWide(false);
352 }
buzbee091cc402014-03-31 10:14:40 -0700353 Clobber(info->GetReg());
354 info->MarkInUse();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700356 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 }
358 next++;
359 }
360 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700361 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700362 for (int i = 0; i< num_regs; i++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100363 if (next >= num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 next = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100365 }
366 RegisterInfo* info = regs[next];
buzbee091cc402014-03-31 10:14:40 -0700367 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700368 // Got one. Kill it.
369 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700370 Clobber(info->GetReg());
371 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700372 if (info->IsWide()) {
373 RegisterInfo* partner = GetRegInfo(info->Partner());
374 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
375 DCHECK(partner->IsWide());
376 info->SetIsWide(false);
377 partner->SetIsWide(false);
378 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700380 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
382 next++;
383 }
384 if (required) {
385 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700386 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 LOG(FATAL) << "No free temp registers";
388 }
buzbee2700f7e2014-03-07 09:46:20 -0800389 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390}
391
Serguei Katkov9ee45192014-07-17 14:39:03 +0700392RegStorage Mir2Lir::AllocTemp(bool required) {
393 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, required);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394}
395
Serguei Katkov9ee45192014-07-17 14:39:03 +0700396RegStorage Mir2Lir::AllocTempWide(bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700397 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100398 if (reg_pool_->core64_regs_.size() != 0) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700399 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, required);
buzbeeb01bf152014-05-13 15:59:07 -0700400 } else {
401 RegStorage low_reg = AllocTemp();
402 RegStorage high_reg = AllocTemp();
403 res = RegStorage::MakeRegPair(low_reg, high_reg);
404 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700405 if (required) {
406 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kIgnoreRef, FPCheck::kCheckNotFP);
407 }
buzbeeb01bf152014-05-13 15:59:07 -0700408 return res;
409}
410
Serguei Katkov9ee45192014-07-17 14:39:03 +0700411RegStorage Mir2Lir::AllocTempRef(bool required) {
412 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, required);
413 if (required) {
414 DCHECK(!res.IsPair());
415 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
416 }
buzbeea0cd2d72014-06-01 09:33:49 -0700417 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100418}
419
Serguei Katkov9ee45192014-07-17 14:39:03 +0700420RegStorage Mir2Lir::AllocTempSingle(bool required) {
421 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, required);
422 if (required) {
423 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
424 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
425 }
buzbee091cc402014-03-31 10:14:40 -0700426 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427}
428
Serguei Katkov9ee45192014-07-17 14:39:03 +0700429RegStorage Mir2Lir::AllocTempDouble(bool required) {
430 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, required);
431 if (required) {
432 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
433 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
434 }
buzbee091cc402014-03-31 10:14:40 -0700435 return res;
436}
437
Serguei Katkov9ee45192014-07-17 14:39:03 +0700438RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class, bool required) {
buzbeea0cd2d72014-06-01 09:33:49 -0700439 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700440 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700441 return AllocTempDouble(required);
buzbeeb01bf152014-05-13 15:59:07 -0700442 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700443 return AllocTempWide(required);
buzbeeb01bf152014-05-13 15:59:07 -0700444}
445
Serguei Katkov9ee45192014-07-17 14:39:03 +0700446RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class, bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700447 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700448 return AllocTempSingle(required);
buzbeea0cd2d72014-06-01 09:33:49 -0700449 } else if (reg_class == kRefReg) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700450 return AllocTempRef(required);
buzbeeb01bf152014-05-13 15:59:07 -0700451 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700452 return AllocTemp(required);
buzbeeb01bf152014-05-13 15:59:07 -0700453}
454
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100455RegStorage Mir2Lir::FindLiveReg(ArenaVector<RegisterInfo*>& regs, int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700456 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100457 for (RegisterInfo* info : regs) {
buzbee091cc402014-03-31 10:14:40 -0700458 if ((info->SReg() == s_reg) && info->IsLive()) {
459 res = info->GetReg();
460 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 }
462 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 return res;
464}
465
buzbee091cc402014-03-31 10:14:40 -0700466RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
467 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700468 if (reg_class == kRefReg) {
469 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700470 CheckRegStorage(reg, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700471 }
472 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700473 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 }
buzbee091cc402014-03-31 10:14:40 -0700475 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700476 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700477 reg = FindLiveReg(wide || reg_class == kRefReg ? reg_pool_->core64_regs_ :
478 reg_pool_->core_regs_, s_reg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100479 } else {
480 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
481 }
buzbee091cc402014-03-31 10:14:40 -0700482 }
483 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700484 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700485 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700486 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
487 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700488 reg = RegStorage::MakeRegPair(reg, high_reg);
489 MarkWide(reg);
490 } else {
buzbee30adc732014-05-09 15:10:18 -0700491 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700492 reg = RegStorage::InvalidReg();
493 }
494 }
buzbee30adc732014-05-09 15:10:18 -0700495 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
496 // Width mismatch - don't try to reuse.
497 reg = RegStorage::InvalidReg();
498 }
499 }
500 if (reg.Valid()) {
501 if (reg.IsPair()) {
502 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
503 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
504 if (info_low->IsTemp()) {
505 info_low->MarkInUse();
506 }
507 if (info_high->IsTemp()) {
508 info_high->MarkInUse();
509 }
510 } else {
buzbee091cc402014-03-31 10:14:40 -0700511 RegisterInfo* info = GetRegInfo(reg);
512 if (info->IsTemp()) {
513 info->MarkInUse();
514 }
515 }
buzbee30adc732014-05-09 15:10:18 -0700516 } else {
517 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
518 ClobberSReg(s_reg);
519 if (wide) {
520 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700521 }
522 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700523 CheckRegStorage(reg, WidenessCheck::kIgnoreWide,
524 reg_class == kRefReg ? RefCheck::kCheckRef : RefCheck::kIgnoreRef,
525 FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700526 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527}
528
buzbee2700f7e2014-03-07 09:46:20 -0800529void Mir2Lir::FreeTemp(RegStorage reg) {
530 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700531 FreeTemp(reg.GetLow());
532 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800533 } else {
buzbee091cc402014-03-31 10:14:40 -0700534 RegisterInfo* p = GetRegInfo(reg);
535 if (p->IsTemp()) {
536 p->MarkFree();
537 p->SetIsWide(false);
538 p->SetPartner(reg);
539 }
buzbee2700f7e2014-03-07 09:46:20 -0800540 }
541}
542
buzbee082833c2014-05-17 23:16:26 -0700543void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
544 DCHECK(rl_keep.wide);
545 DCHECK(rl_free.wide);
546 int free_low = rl_free.reg.GetLowReg();
547 int free_high = rl_free.reg.GetHighReg();
548 int keep_low = rl_keep.reg.GetLowReg();
549 int keep_high = rl_keep.reg.GetHighReg();
550 if ((free_low != keep_low) && (free_low != keep_high) &&
551 (free_high != keep_low) && (free_high != keep_high)) {
552 // No overlap, free both
553 FreeTemp(rl_free.reg);
554 }
555}
556
buzbee262b2992014-03-27 11:22:43 -0700557bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700558 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800559 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700560 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
561 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700562 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700563 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800564 } else {
buzbee091cc402014-03-31 10:14:40 -0700565 RegisterInfo* p = GetRegInfo(reg);
566 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800567 }
buzbee091cc402014-03-31 10:14:40 -0700568 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569}
570
buzbee262b2992014-03-27 11:22:43 -0700571bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700572 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800573 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700574 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
575 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
576 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800577 } else {
buzbee091cc402014-03-31 10:14:40 -0700578 RegisterInfo* p = GetRegInfo(reg);
579 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800580 }
buzbee091cc402014-03-31 10:14:40 -0700581 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582}
583
buzbee262b2992014-03-27 11:22:43 -0700584bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700585 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800586 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700587 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
588 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
589 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800590 } else {
buzbee091cc402014-03-31 10:14:40 -0700591 RegisterInfo* p = GetRegInfo(reg);
592 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800593 }
buzbee091cc402014-03-31 10:14:40 -0700594 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595}
596
buzbee2700f7e2014-03-07 09:46:20 -0800597bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700598 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800599 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700600 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
601 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
602 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800603 } else {
buzbee091cc402014-03-31 10:14:40 -0700604 RegisterInfo* p = GetRegInfo(reg);
605 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800606 }
buzbee091cc402014-03-31 10:14:40 -0700607 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800608}
609
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610/*
611 * Similar to AllocTemp(), but forces the allocation of a specific
612 * register. No check is made to see if the register was previously
613 * allocated. Use with caution.
614 */
buzbee2700f7e2014-03-07 09:46:20 -0800615void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700616 DCHECK(IsTemp(reg));
617 if (reg.IsPair()) {
618 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
619 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
620 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700621 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700622 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700623 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700624 } else {
625 RegisterInfo* p = GetRegInfo(reg);
626 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700627 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700628 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629}
630
buzbee2700f7e2014-03-07 09:46:20 -0800631void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700632 if (reg.IsPair()) {
633 GetRegInfo(reg.GetLow())->ResetDefBody();
634 GetRegInfo(reg.GetHigh())->ResetDefBody();
635 } else {
636 GetRegInfo(reg)->ResetDefBody();
637 }
buzbee2700f7e2014-03-07 09:46:20 -0800638}
639
buzbee091cc402014-03-31 10:14:40 -0700640void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
641 RegisterInfo* info = nullptr;
642 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
643 if (IsTemp(rs)) {
644 info = GetRegInfo(reg);
645 }
646 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
647 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
648 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700650 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 break;
buzbee091cc402014-03-31 10:14:40 -0700652 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 }
654 }
655}
656
657/*
658 * Mark the beginning and end LIR of a def sequence. Note that
659 * on entry start points to the LIR prior to the beginning of the
660 * sequence.
661 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700662void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 DCHECK(!rl.wide);
664 DCHECK(start && start->next);
665 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700666 RegisterInfo* p = GetRegInfo(rl.reg);
667 p->SetDefStart(start->next);
668 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669}
670
671/*
672 * Mark the beginning and end LIR of a def sequence. Note that
673 * on entry start points to the LIR prior to the beginning of the
674 * sequence.
675 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700676void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 DCHECK(rl.wide);
678 DCHECK(start && start->next);
679 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700680 RegisterInfo* p;
681 if (rl.reg.IsPair()) {
682 p = GetRegInfo(rl.reg.GetLow());
683 ResetDef(rl.reg.GetHigh()); // Only track low of pair
684 } else {
685 p = GetRegInfo(rl.reg);
686 }
687 p->SetDefStart(start->next);
688 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689}
690
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700691void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700693 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
694 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 }
buzbee091cc402014-03-31 10:14:40 -0700696 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697}
698
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700699void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700701 // If pair, only track low reg of pair.
702 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
703 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
704 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 }
buzbee091cc402014-03-31 10:14:40 -0700706 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707}
708
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700709void Mir2Lir::ResetDefTracking() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100710 for (RegisterInfo* info : tempreg_info_) {
buzbee091cc402014-03-31 10:14:40 -0700711 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 }
713}
714
buzbeeba574512014-05-12 15:13:16 -0700715void Mir2Lir::ClobberAllTemps() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100716 for (RegisterInfo* info : tempreg_info_) {
buzbee30adc732014-05-09 15:10:18 -0700717 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700718 }
719}
720
721void Mir2Lir::FlushRegWide(RegStorage reg) {
722 if (reg.IsPair()) {
723 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
724 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
725 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
buzbeeb5860fb2014-06-21 15:31:01 -0700726 (info1->Partner().ExactlyEquals(info2->GetReg())) &&
727 (info2->Partner().ExactlyEquals(info1->GetReg())));
buzbee091cc402014-03-31 10:14:40 -0700728 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
729 if (!(info1->IsTemp() && info2->IsTemp())) {
730 /* Should not happen. If it does, there's a problem in eval_loc */
731 LOG(FATAL) << "Long half-temp, half-promoted";
732 }
733
734 info1->SetIsDirty(false);
735 info2->SetIsDirty(false);
736 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
737 info1 = info2;
738 }
739 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100740 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700741 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700742 }
743 } else {
744 RegisterInfo* info = GetRegInfo(reg);
745 if (info->IsLive() && info->IsDirty()) {
746 info->SetIsDirty(false);
747 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100748 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700749 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700750 }
751 }
752}
753
754void Mir2Lir::FlushReg(RegStorage reg) {
755 DCHECK(!reg.IsPair());
756 RegisterInfo* info = GetRegInfo(reg);
757 if (info->IsLive() && info->IsDirty()) {
758 info->SetIsDirty(false);
759 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100760 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700761 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 }
763}
764
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800765void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700766 if (info->IsWide()) {
767 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800768 } else {
buzbee091cc402014-03-31 10:14:40 -0700769 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700770 }
771}
772
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700773void Mir2Lir::FlushAllRegs() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100774 for (RegisterInfo* info : tempreg_info_) {
buzbeeba574512014-05-12 15:13:16 -0700775 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700776 FlushSpecificReg(info);
777 }
buzbee30adc732014-05-09 15:10:18 -0700778 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700779 info->SetIsWide(false);
780 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700781}
782
783
buzbee2700f7e2014-03-07 09:46:20 -0800784bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785 if (reg_class == kAnyReg) {
786 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700787 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
788 /*
789 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
790 * with width here - that should be checked at a higher level (if needed).
791 */
buzbee091cc402014-03-31 10:14:40 -0700792 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 } else {
buzbee091cc402014-03-31 10:14:40 -0700794 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 }
796}
797
buzbee091cc402014-03-31 10:14:40 -0700798void Mir2Lir::MarkLive(RegLocation loc) {
799 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700800 if (!IsTemp(reg)) {
801 return;
802 }
buzbee091cc402014-03-31 10:14:40 -0700803 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700804 if (s_reg == INVALID_SREG) {
805 // Can't be live if no associated sreg.
806 if (reg.IsPair()) {
807 GetRegInfo(reg.GetLow())->MarkDead();
808 GetRegInfo(reg.GetHigh())->MarkDead();
809 } else {
810 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700811 }
buzbee082833c2014-05-17 23:16:26 -0700812 } else {
813 if (reg.IsPair()) {
814 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
815 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
816 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
817 (info_hi->SReg() == s_reg)) {
818 return; // Already live.
819 }
820 ClobberSReg(s_reg);
821 ClobberSReg(s_reg + 1);
822 info_lo->MarkLive(s_reg);
823 info_hi->MarkLive(s_reg + 1);
824 } else {
825 RegisterInfo* info = GetRegInfo(reg);
826 if (info->IsLive() && (info->SReg() == s_reg)) {
827 return; // Already live.
828 }
829 ClobberSReg(s_reg);
830 if (loc.wide) {
831 ClobberSReg(s_reg + 1);
832 }
833 info->MarkLive(s_reg);
834 }
835 if (loc.wide) {
836 MarkWide(reg);
837 } else {
838 MarkNarrow(reg);
839 }
buzbee091cc402014-03-31 10:14:40 -0700840 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700841}
842
buzbee2700f7e2014-03-07 09:46:20 -0800843void Mir2Lir::MarkTemp(RegStorage reg) {
844 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845 RegisterInfo* info = GetRegInfo(reg);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100846 tempreg_info_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -0700847 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848}
849
buzbee2700f7e2014-03-07 09:46:20 -0800850void Mir2Lir::UnmarkTemp(RegStorage reg) {
851 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700852 RegisterInfo* info = GetRegInfo(reg);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100853 auto pos = std::find(tempreg_info_.begin(), tempreg_info_.end(), info);
854 DCHECK(pos != tempreg_info_.end());
855 tempreg_info_.erase(pos);
buzbee091cc402014-03-31 10:14:40 -0700856 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800857}
858
buzbee091cc402014-03-31 10:14:40 -0700859void Mir2Lir::MarkWide(RegStorage reg) {
860 if (reg.IsPair()) {
861 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
862 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700863 // Unpair any old partners.
buzbeeb5860fb2014-06-21 15:31:01 -0700864 if (info_lo->IsWide() && info_lo->Partner().NotExactlyEquals(info_hi->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700865 GetRegInfo(info_lo->Partner())->SetIsWide(false);
866 }
buzbeeb5860fb2014-06-21 15:31:01 -0700867 if (info_hi->IsWide() && info_hi->Partner().NotExactlyEquals(info_lo->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700868 GetRegInfo(info_hi->Partner())->SetIsWide(false);
869 }
buzbee091cc402014-03-31 10:14:40 -0700870 info_lo->SetIsWide(true);
871 info_hi->SetIsWide(true);
872 info_lo->SetPartner(reg.GetHigh());
873 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800874 } else {
buzbee091cc402014-03-31 10:14:40 -0700875 RegisterInfo* info = GetRegInfo(reg);
876 info->SetIsWide(true);
877 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 }
879}
880
buzbee082833c2014-05-17 23:16:26 -0700881void Mir2Lir::MarkNarrow(RegStorage reg) {
882 DCHECK(!reg.IsPair());
883 RegisterInfo* info = GetRegInfo(reg);
884 info->SetIsWide(false);
885 info->SetPartner(reg);
886}
887
buzbee091cc402014-03-31 10:14:40 -0700888void Mir2Lir::MarkClean(RegLocation loc) {
889 if (loc.reg.IsPair()) {
890 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
891 info->SetIsDirty(false);
892 info = GetRegInfo(loc.reg.GetHigh());
893 info->SetIsDirty(false);
894 } else {
895 RegisterInfo* info = GetRegInfo(loc.reg);
896 info->SetIsDirty(false);
897 }
898}
899
900// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700901void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 if (loc.home) {
903 // If already home, can't be dirty
904 return;
905 }
buzbee091cc402014-03-31 10:14:40 -0700906 if (loc.reg.IsPair()) {
907 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
908 info->SetIsDirty(true);
909 info = GetRegInfo(loc.reg.GetHigh());
910 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800911 } else {
buzbee091cc402014-03-31 10:14:40 -0700912 RegisterInfo* info = GetRegInfo(loc.reg);
913 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 }
915}
916
buzbee2700f7e2014-03-07 09:46:20 -0800917void Mir2Lir::MarkInUse(RegStorage reg) {
918 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700919 GetRegInfo(reg.GetLow())->MarkInUse();
920 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800921 } else {
buzbee091cc402014-03-31 10:14:40 -0700922 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800923 }
924}
925
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700926bool Mir2Lir::CheckCorePoolSanity() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100927 for (RegisterInfo* info : tempreg_info_) {
buzbee3a658072014-08-28 13:48:56 -0700928 int my_sreg = info->SReg();
929 if (info->IsTemp() && info->IsLive() && info->IsWide() && my_sreg != INVALID_SREG) {
buzbee082833c2014-05-17 23:16:26 -0700930 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700931 RegStorage partner_reg = info->Partner();
932 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700933 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700934 DCHECK(partner->IsWide());
935 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700936 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700937 int partner_sreg = partner->SReg();
buzbee3a658072014-08-28 13:48:56 -0700938 int diff = my_sreg - partner_sreg;
939 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700940 }
buzbee082833c2014-05-17 23:16:26 -0700941 if (info->Master() != info) {
942 // Aliased.
943 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
944 // If I'm live, master should not be live, but should show liveness in alias set.
945 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
946 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700947 }
buzbee642fe342014-05-23 16:04:08 -0700948// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700949 }
950 if (info->IsAliased()) {
951 // Has child aliases.
952 DCHECK_EQ(info->Master(), info);
953 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
954 // Master live, no child should be dead - all should show liveness in set.
955 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
956 DCHECK(!p->IsDead());
957 DCHECK_EQ(p->SReg(), INVALID_SREG);
958 }
959 } else if (!info->IsDead()) {
960 // Master not live, one or more aliases must be.
961 bool live_alias = false;
962 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
963 live_alias |= p->IsLive();
964 }
965 DCHECK(live_alias);
966 }
967 }
968 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
969 // If not fully live, should have INVALID_SREG and def's should be null.
970 DCHECK(info->DefStart() == nullptr);
971 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700972 }
973 }
974 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975}
976
977/*
978 * Return an updated location record with current in-register status.
979 * If the value lives in live temps, reflect that fact. No code
980 * is generated. If the live value is part of an older pair,
981 * clobber both low and high.
982 * TUNING: clobbering both is a bit heavy-handed, but the alternative
983 * is a bit complex when dealing with FP regs. Examine code to see
984 * if it's worthwhile trying to be more clever here.
985 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700986RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987 DCHECK(!loc.wide);
988 DCHECK(CheckCorePoolSanity());
989 if (loc.location != kLocPhysReg) {
990 DCHECK((loc.location == kLocDalvikFrame) ||
991 (loc.location == kLocCompilerTemp));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700992 RegStorage reg = AllocLiveReg(loc.s_reg_low, loc.ref ? kRefReg : kAnyReg, false);
buzbee091cc402014-03-31 10:14:40 -0700993 if (reg.Valid()) {
994 bool match = true;
995 RegisterInfo* info = GetRegInfo(reg);
996 match &= !reg.IsPair();
997 match &= !info->IsWide();
998 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001000 loc.reg = reg;
1001 } else {
1002 Clobber(reg);
1003 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 }
1005 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001006 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008 return loc;
1009}
1010
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001011RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 DCHECK(loc.wide);
1013 DCHECK(CheckCorePoolSanity());
1014 if (loc.location != kLocPhysReg) {
1015 DCHECK((loc.location == kLocDalvikFrame) ||
1016 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001017 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1018 if (reg.Valid()) {
1019 bool match = true;
1020 if (reg.IsPair()) {
1021 // If we've got a register pair, make sure that it was last used as the same pair.
1022 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1023 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1024 match &= info_lo->IsWide();
1025 match &= info_hi->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001026 match &= (info_lo->Partner().ExactlyEquals(info_hi->GetReg()));
1027 match &= (info_hi->Partner().ExactlyEquals(info_lo->GetReg()));
buzbee091cc402014-03-31 10:14:40 -07001028 } else {
1029 RegisterInfo* info = GetRegInfo(reg);
1030 match &= info->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001031 match &= (info->GetReg().ExactlyEquals(info->Partner()));
buzbee091cc402014-03-31 10:14:40 -07001032 }
1033 if (match) {
1034 loc.location = kLocPhysReg;
1035 loc.reg = reg;
1036 } else {
1037 Clobber(reg);
1038 FreeTemp(reg);
1039 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001041 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001042 }
1043 return loc;
1044}
1045
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001047RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 if (loc.wide)
1049 return UpdateLocWide(loc);
1050 else
1051 return UpdateLoc(loc);
1052}
1053
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001054RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056
1057 loc = UpdateLocWide(loc);
1058
1059 /* If already in registers, we can assume proper form. Right reg class? */
1060 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001061 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001062 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001063 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001064 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001065 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001066 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001067 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001068 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001069 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001071 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 return loc;
1073 }
1074
1075 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1076 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1077
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001078 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001079 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 if (update) {
1082 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001083 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001085 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 return loc;
1087}
1088
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001089RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001090 // Narrow reg_class if the loc is a ref.
1091 if (loc.ref && reg_class == kAnyReg) {
1092 reg_class = kRefReg;
1093 }
1094
buzbee091cc402014-03-31 10:14:40 -07001095 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001097 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098
1099 loc = UpdateLoc(loc);
1100
1101 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001102 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001103 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001104 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001105 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001106 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001107 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001108 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001109 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001110 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001111 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 return loc;
1113 }
1114
1115 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1116
buzbee2700f7e2014-03-07 09:46:20 -08001117 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001118 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119
1120 if (update) {
1121 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001122 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001124 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 return loc;
1126}
1127
1128/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001129void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1131 RegLocation loc = mir_graph_->reg_location_[i];
1132 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1133 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001134 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001135 if (loc.fp) {
1136 if (loc.wide) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001137 if (WideFPRsAreAliases()) {
1138 // Floats and doubles can be counted together.
1139 counts[p_map_idx].count += use_count;
1140 } else {
1141 // Treat doubles as a unit, using upper half of fp_counts array.
1142 counts[p_map_idx + num_regs].count += use_count;
1143 }
buzbeec729a6b2013-09-14 16:04:31 -07001144 i++;
1145 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001146 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001147 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001148 } else {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001149 if (loc.wide && WideGPRsAreAliases()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001150 i++;
buzbeeb5860fb2014-06-21 15:31:01 -07001151 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001152 if (!IsInexpensiveConstant(loc)) {
1153 counts[p_map_idx].count += use_count;
1154 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 }
1157}
1158
1159/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001160static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1162 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Matteo Franchinc763e352014-07-04 12:53:27 +01001163 // Note that we fall back to sorting on reg so we get stable output on differing qsort
1164 // implementations (such as on host and target or between local host and build servers).
1165 // Note also that if a wide val1 and a non-wide val2 have the same count, then val1 always
1166 // ``loses'' (as STARTING_WIDE_SREG is or-ed in val1->s_reg).
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001167 return (op1->count == op2->count)
1168 ? (op1->s_reg - op2->s_reg)
1169 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001170}
1171
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001172void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 LOG(INFO) << msg;
1174 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001175 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1176 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001177 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001178 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001179 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 }
1181}
1182
1183/*
1184 * Note: some portions of this code required even if the kPromoteRegs
1185 * optimization is disabled.
1186 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001187void Mir2Lir::DoPromotion() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001188 int num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001190 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1191 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001192 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193
1194 // Allow target code to add any special registers
1195 AdjustSpillMask();
1196
1197 /*
1198 * Simple register promotion. Just do a static count of the uses
1199 * of Dalvik registers. Note that we examine the SSA names, but
1200 * count based on original Dalvik register name. Count refs
1201 * separately based on type in order to give allocation
1202 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001203 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 * reg.
1205 * TUNING: replace with linear scan once we have the ability
1206 * to describe register live ranges for GC.
1207 */
Matteo Franchinc763e352014-07-04 12:53:27 +01001208 size_t core_reg_count_size = WideGPRsAreAliases() ? num_regs : num_regs * 2;
1209 size_t fp_reg_count_size = WideFPRsAreAliases() ? num_regs : num_regs * 2;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001210 RefCounts *core_regs =
buzbeeb5860fb2014-06-21 15:31:01 -07001211 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * core_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001212 kArenaAllocRegAlloc));
buzbeeb5860fb2014-06-21 15:31:01 -07001213 RefCounts *fp_regs =
1214 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * fp_reg_count_size,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001215 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 // Set ssa names for original Dalvik registers
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001217 for (int i = 0; i < num_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001218 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001220
buzbeeb5860fb2014-06-21 15:31:01 -07001221 // Duplicate in upper half to represent possible wide starting sregs.
1222 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1223 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1224 }
1225 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1226 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 }
1228
1229 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001230 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001233 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1234 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001235
1236 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001237 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1238 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001239 }
1240
1241 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001242 // Promote fp regs
1243 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1244 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1245 size_t p_map_idx = SRegToPMap(low_sreg);
1246 RegStorage reg = RegStorage::InvalidReg();
1247 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1248 // TODO: break out the Thumb2-specific code.
1249 if (cu_->instruction_set == kThumb2) {
1250 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1251 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001252 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001253 // Ignore result - if can't alloc double may still be able to alloc singles.
1254 AllocPreservedDouble(low_sreg);
1255 }
1256 // Continue regardless of success - might still be able to grab a single.
1257 continue;
1258 } else {
1259 reg = AllocPreservedSingle(low_sreg);
1260 }
1261 } else {
1262 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001263 }
buzbee2700f7e2014-03-07 09:46:20 -08001264 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001265 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 }
1267 }
1268 }
1269
1270 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001271 for (size_t i = 0; (i < core_reg_count_size) &&
1272 (core_regs[i].count >= promotion_threshold); i++) {
1273 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1274 size_t p_map_idx = SRegToPMap(low_sreg);
1275 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1276 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001277 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001278 break; // No more left
1279 }
1280 }
1281 }
1282 }
1283
1284 // Now, update SSA names to new home locations
1285 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1286 RegLocation *curr = &mir_graph_->reg_location_[i];
1287 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001288 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 -07001289 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001290 RegStorage reg = RegStorage::InvalidReg();
1291 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1292 if (wide && cu_->instruction_set == kThumb2) {
1293 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1294 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001295 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001296 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1297 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 }
1299 }
1300 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001301 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 }
buzbeeb5860fb2014-06-21 15:31:01 -07001303 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1304 if (wide && !cu_->target64) {
1305 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1306 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1307 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1308 }
1309 } else {
1310 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1311 }
1312 }
1313 if (reg.Valid()) {
1314 curr->reg = reg;
1315 curr->location = kLocPhysReg;
1316 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317 }
1318 }
1319 if (cu_->verbose) {
1320 DumpPromotionMap();
1321 }
1322}
1323
1324/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001325int Mir2Lir::VRegOffset(int v_reg) {
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001326 const DexFile::CodeItem* code_item = mir_graph_->GetCurrentDexCompilationUnit()->GetCodeItem();
1327 return StackVisitor::GetVRegOffset(code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001328 fp_spill_mask_, frame_size_, v_reg,
1329 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330}
1331
1332/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001333int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001334 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1335}
1336
1337/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001338RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1339 RegLocation res;
1340 switch (reg_class) {
1341 case kRefReg: LOG(FATAL); break;
1342 case kFPReg: res = LocCReturnDouble(); break;
1343 default: res = LocCReturnWide(); break;
1344 }
buzbee082833c2014-05-17 23:16:26 -07001345 Clobber(res.reg);
1346 LockTemp(res.reg);
1347 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001348 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001349 return res;
1350}
1351
buzbeea0cd2d72014-06-01 09:33:49 -07001352RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1353 RegLocation res;
1354 switch (reg_class) {
1355 case kRefReg: res = LocCReturnRef(); break;
1356 case kFPReg: res = LocCReturnFloat(); break;
1357 default: res = LocCReturn(); break;
1358 }
buzbee091cc402014-03-31 10:14:40 -07001359 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001361 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 } else {
buzbee091cc402014-03-31 10:14:40 -07001363 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001365 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001366 return res;
1367}
1368
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001369void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 DoPromotion();
1371
1372 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1373 LOG(INFO) << "After Promotion";
1374 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1375 }
1376
1377 /* Set the frame size */
1378 frame_size_ = ComputeFrameSize();
1379}
1380
1381/*
1382 * Get the "real" sreg number associated with an s_reg slot. In general,
1383 * s_reg values passed through codegen are the SSA names created by
1384 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1385 * array. However, renaming is accomplished by simply replacing RegLocation
1386 * entries in the reglocation[] array. Therefore, when location
1387 * records for operands are first created, we need to ask the locRecord
1388 * identified by the dataflow pass what it's new name is.
1389 */
1390int Mir2Lir::GetSRegHi(int lowSreg) {
1391 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1392}
1393
buzbee091cc402014-03-31 10:14:40 -07001394bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001395 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001396 return true;
1397}
1398
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399} // namespace art