blob: d9d0434e8a965537d7ca7ab45bcbc7ec8e6a737e [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
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "mir_to_lir-inl.h"
20
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021#include "dex/compiler_ir.h"
Vladimir Markocc234812015-04-07 09:36:09 +010022#include "dex/dataflow_iterator-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "dex/mir_graph.h"
24#include "driver/compiler_driver.h"
25#include "driver/dex_compilation_unit.h"
Vladimir Markocc234812015-04-07 09:36:09 +010026#include "utils/dex_cache_arrays_layout-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080027
Brian Carlstrom7940e442013-07-12 13:46:57 -070028namespace art {
29
30/*
31 * Free all allocated temps in the temp pools. Note that this does
32 * not affect the "liveness" of a temp register, which will stay
33 * live until it is either explicitly killed or reallocated.
34 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070035void Mir2Lir::ResetRegPool() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010036 for (RegisterInfo* info : tempreg_info_) {
buzbee091cc402014-03-31 10:14:40 -070037 info->MarkFree();
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 }
39 // Reset temp tracking sanity check.
40 if (kIsDebugBuild) {
41 live_sreg_ = INVALID_SREG;
42 }
43}
44
Vladimir Marko8dea81c2014-06-06 14:50:36 +010045Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, const ResourceMask& mask)
buzbee30adc732014-05-09 15:10:18 -070046 : reg_(r), is_temp_(false), wide_value_(false), dirty_(false), aliased_(false), partner_(r),
buzbeeba574512014-05-12 15:13:16 -070047 s_reg_(INVALID_SREG), def_use_mask_(mask), master_(this), def_start_(nullptr),
48 def_end_(nullptr), alias_chain_(nullptr) {
buzbee091cc402014-03-31 10:14:40 -070049 switch (r.StorageSize()) {
50 case 0: storage_mask_ = 0xffffffff; break;
51 case 4: storage_mask_ = 0x00000001; break;
52 case 8: storage_mask_ = 0x00000003; break;
53 case 16: storage_mask_ = 0x0000000f; break;
54 case 32: storage_mask_ = 0x000000ff; break;
55 case 64: storage_mask_ = 0x0000ffff; break;
56 case 128: storage_mask_ = 0xffffffff; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 }
buzbee091cc402014-03-31 10:14:40 -070058 used_storage_ = r.Valid() ? ~storage_mask_ : storage_mask_;
buzbee30adc732014-05-09 15:10:18 -070059 liveness_ = used_storage_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070060}
61
buzbee091cc402014-03-31 10:14:40 -070062Mir2Lir::RegisterPool::RegisterPool(Mir2Lir* m2l, ArenaAllocator* arena,
Vladimir Marko089142c2014-06-05 10:57:05 +010063 const ArrayRef<const RegStorage>& core_regs,
64 const ArrayRef<const RegStorage>& core64_regs,
65 const ArrayRef<const RegStorage>& sp_regs,
66 const ArrayRef<const RegStorage>& dp_regs,
67 const ArrayRef<const RegStorage>& reserved_regs,
68 const ArrayRef<const RegStorage>& reserved64_regs,
69 const ArrayRef<const RegStorage>& core_temps,
70 const ArrayRef<const RegStorage>& core64_temps,
71 const ArrayRef<const RegStorage>& sp_temps,
72 const ArrayRef<const RegStorage>& dp_temps) :
Vladimir Markoe39c54e2014-09-22 14:50:02 +010073 core_regs_(arena->Adapter()), next_core_reg_(0),
74 core64_regs_(arena->Adapter()), next_core64_reg_(0),
75 sp_regs_(arena->Adapter()), next_sp_reg_(0),
76 dp_regs_(arena->Adapter()), next_dp_reg_(0), m2l_(m2l) {
buzbee091cc402014-03-31 10:14:40 -070077 // Initialize the fast lookup map.
Vladimir Markoe39c54e2014-09-22 14:50:02 +010078 m2l_->reginfo_map_.clear();
79 m2l_->reginfo_map_.resize(RegStorage::kMaxRegs, nullptr);
buzbee091cc402014-03-31 10:14:40 -070080
81 // Construct the register pool.
Vladimir Markoe39c54e2014-09-22 14:50:02 +010082 core_regs_.reserve(core_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010083 for (const RegStorage& reg : core_regs) {
buzbee091cc402014-03-31 10:14:40 -070084 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010085 m2l_->reginfo_map_[reg.GetReg()] = info;
86 core_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -070087 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010088 core64_regs_.reserve(core64_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010089 for (const RegStorage& reg : core64_regs) {
buzbeeb01bf152014-05-13 15:59:07 -070090 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010091 m2l_->reginfo_map_[reg.GetReg()] = info;
92 core64_regs_.push_back(info);
buzbeeb01bf152014-05-13 15:59:07 -070093 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010094 sp_regs_.reserve(sp_regs.size());
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));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010097 m2l_->reginfo_map_[reg.GetReg()] = info;
98 sp_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -070099 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100100 dp_regs_.reserve(dp_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100101 for (const RegStorage& reg : dp_regs) {
buzbee091cc402014-03-31 10:14:40 -0700102 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100103 m2l_->reginfo_map_[reg.GetReg()] = info;
104 dp_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -0700105 }
106
107 // Keep special registers from being allocated.
108 for (RegStorage reg : reserved_regs) {
109 m2l_->MarkInUse(reg);
110 }
buzbeeb01bf152014-05-13 15:59:07 -0700111 for (RegStorage reg : reserved64_regs) {
112 m2l_->MarkInUse(reg);
113 }
buzbee091cc402014-03-31 10:14:40 -0700114
115 // Mark temp regs - all others not in use can be used for promotion
116 for (RegStorage reg : core_temps) {
117 m2l_->MarkTemp(reg);
118 }
buzbeeb01bf152014-05-13 15:59:07 -0700119 for (RegStorage reg : core64_temps) {
120 m2l_->MarkTemp(reg);
121 }
buzbee091cc402014-03-31 10:14:40 -0700122 for (RegStorage reg : sp_temps) {
123 m2l_->MarkTemp(reg);
124 }
125 for (RegStorage reg : dp_temps) {
126 m2l_->MarkTemp(reg);
127 }
128
129 // Add an entry for InvalidReg with zero'd mask.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100130 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), kEncodeNone);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100131 m2l_->reginfo_map_[RegStorage::InvalidReg().GetReg()] = invalid_reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700132
133 // Existence of core64 registers implies wide references.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100134 if (core64_regs_.size() != 0) {
buzbeea0cd2d72014-06-01 09:33:49 -0700135 ref_regs_ = &core64_regs_;
136 next_ref_reg_ = &next_core64_reg_;
137 } else {
138 ref_regs_ = &core_regs_;
139 next_ref_reg_ = &next_core_reg_;
140 }
buzbee091cc402014-03-31 10:14:40 -0700141}
142
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100143void Mir2Lir::DumpRegPool(ArenaVector<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 LOG(INFO) << "================================================";
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100145 for (RegisterInfo* info : *regs) {
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 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100225 for (RegisterInfo* info : tempreg_info_) {
buzbee30adc732014-05-09 15:10:18 -0700226 if (info->SReg() == s_reg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700227 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700228 // Dealing with a pair - clobber the other half.
229 DCHECK(!info->IsAliased());
230 ClobberBody(GetRegInfo(info->Partner()));
231 }
buzbeeba574512014-05-12 15:13:16 -0700232 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700233 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700234 ClobberAliases(info, info->StorageMask());
buzbee30adc732014-05-09 15:10:18 -0700235 }
buzbee091cc402014-03-31 10:14:40 -0700236 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 }
238 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239}
240
241/*
242 * SSA names associated with the initial definitions of Dalvik
243 * registers are the same as the Dalvik register number (and
244 * thus take the same position in the promotion_map. However,
245 * the special Method* and compiler temp resisters use negative
246 * v_reg numbers to distinguish them and can have an arbitrary
247 * ssa name (above the last original Dalvik register). This function
248 * maps SSA names to positions in the promotion_map array.
249 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700250int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
252 DCHECK_GE(s_reg, 0);
253 int v_reg = mir_graph_->SRegToVReg(s_reg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700254 return v_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255}
256
buzbee091cc402014-03-31 10:14:40 -0700257// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800258void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 int p_map_idx = SRegToPMap(s_reg);
260 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700261 int reg_num = reg.GetRegNum();
262 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800263 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800265 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 num_core_spills_++;
267 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800268 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269}
270
buzbee091cc402014-03-31 10:14:40 -0700271/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800272RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
273 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700274 /*
275 * Note: it really doesn't matter much whether we allocate from the core or core64
276 * pool for 64-bit targets - but for some targets it does matter whether allocations
277 * happens from the single or double pool. This entire section of code could stand
278 * a good refactoring.
279 */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100280 for (RegisterInfo* info : reg_pool_->core_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700281 if (!info->IsTemp() && !info->InUse()) {
282 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 RecordCorePromotion(res, s_reg);
284 break;
285 }
286 }
287 return res;
288}
289
buzbeeb5860fb2014-06-21 15:31:01 -0700290void Mir2Lir::RecordFpPromotion(RegStorage reg, int s_reg) {
291 DCHECK_NE(cu_->instruction_set, kThumb2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 int p_map_idx = SRegToPMap(s_reg);
293 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700294 int reg_num = reg.GetRegNum();
buzbee091cc402014-03-31 10:14:40 -0700295 GetRegInfo(reg)->MarkInUse();
buzbeeb5860fb2014-06-21 15:31:01 -0700296 fp_spill_mask_ |= (1 << reg_num);
297 // Include reg for later sort
298 fp_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
299 num_fp_spills_++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbeeb5860fb2014-06-21 15:31:01 -0700301 promotion_map_[p_map_idx].fp_reg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302}
303
buzbeeb5860fb2014-06-21 15:31:01 -0700304// Reserve a callee-save floating point.
305RegStorage Mir2Lir::AllocPreservedFpReg(int s_reg) {
306 /*
307 * For targets other than Thumb2, it doesn't matter whether we allocate from
308 * the sp_regs_ or dp_regs_ pool. Some refactoring is in order here.
309 */
310 DCHECK_NE(cu_->instruction_set, kThumb2);
buzbee2700f7e2014-03-07 09:46:20 -0800311 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100312 for (RegisterInfo* info : reg_pool_->sp_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700313 if (!info->IsTemp() && !info->InUse()) {
314 res = info->GetReg();
buzbeeb5860fb2014-06-21 15:31:01 -0700315 RecordFpPromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 break;
317 }
318 }
319 return res;
320}
321
buzbeeb5860fb2014-06-21 15:31:01 -0700322// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100323RegStorage Mir2Lir::AllocPreservedDouble(int s_reg ATTRIBUTE_UNUSED) {
buzbeeb5860fb2014-06-21 15:31:01 -0700324 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedDouble";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 UNREACHABLE();
buzbeeb5860fb2014-06-21 15:31:01 -0700326}
327
328// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100329RegStorage Mir2Lir::AllocPreservedSingle(int s_reg ATTRIBUTE_UNUSED) {
buzbeeb5860fb2014-06-21 15:31:01 -0700330 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedSingle";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700331 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332}
333
buzbee091cc402014-03-31 10:14:40 -0700334
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100335RegStorage Mir2Lir::AllocTempBody(ArenaVector<RegisterInfo*>& regs, int* next_temp, bool required) {
336 int num_regs = regs.size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700338 for (int i = 0; i< num_regs; i++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100339 if (next >= num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 next = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100341 }
342 RegisterInfo* info = regs[next];
buzbee30adc732014-05-09 15:10:18 -0700343 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700344 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee88a6b412014-08-25 09:34:03 -0700345 // If it's wide, split it up.
346 if (info->IsWide()) {
347 // If the pair was associated with a wide value, unmark the partner as well.
348 if (info->SReg() != INVALID_SREG) {
349 RegisterInfo* partner = GetRegInfo(info->Partner());
350 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
351 DCHECK(partner->IsWide());
352 partner->SetIsWide(false);
353 }
354 info->SetIsWide(false);
355 }
buzbee091cc402014-03-31 10:14:40 -0700356 Clobber(info->GetReg());
357 info->MarkInUse();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700359 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 }
361 next++;
362 }
363 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700364 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700365 for (int i = 0; i< num_regs; i++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100366 if (next >= num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 next = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100368 }
369 RegisterInfo* info = regs[next];
buzbee091cc402014-03-31 10:14:40 -0700370 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700371 // Got one. Kill it.
372 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700373 Clobber(info->GetReg());
374 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700375 if (info->IsWide()) {
376 RegisterInfo* partner = GetRegInfo(info->Partner());
377 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
378 DCHECK(partner->IsWide());
379 info->SetIsWide(false);
380 partner->SetIsWide(false);
381 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700383 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
385 next++;
386 }
387 if (required) {
388 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700389 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 LOG(FATAL) << "No free temp registers";
391 }
buzbee2700f7e2014-03-07 09:46:20 -0800392 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393}
394
Serguei Katkov9ee45192014-07-17 14:39:03 +0700395RegStorage Mir2Lir::AllocTemp(bool required) {
396 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, required);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397}
398
Serguei Katkov9ee45192014-07-17 14:39:03 +0700399RegStorage Mir2Lir::AllocTempWide(bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700400 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100401 if (reg_pool_->core64_regs_.size() != 0) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700402 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, required);
buzbeeb01bf152014-05-13 15:59:07 -0700403 } else {
404 RegStorage low_reg = AllocTemp();
405 RegStorage high_reg = AllocTemp();
406 res = RegStorage::MakeRegPair(low_reg, high_reg);
407 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700408 if (required) {
409 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kIgnoreRef, FPCheck::kCheckNotFP);
410 }
buzbeeb01bf152014-05-13 15:59:07 -0700411 return res;
412}
413
Serguei Katkov9ee45192014-07-17 14:39:03 +0700414RegStorage Mir2Lir::AllocTempRef(bool required) {
415 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, required);
416 if (required) {
417 DCHECK(!res.IsPair());
418 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
419 }
buzbeea0cd2d72014-06-01 09:33:49 -0700420 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100421}
422
Serguei Katkov9ee45192014-07-17 14:39:03 +0700423RegStorage Mir2Lir::AllocTempSingle(bool required) {
424 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, required);
425 if (required) {
426 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
427 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
428 }
buzbee091cc402014-03-31 10:14:40 -0700429 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430}
431
Serguei Katkov9ee45192014-07-17 14:39:03 +0700432RegStorage Mir2Lir::AllocTempDouble(bool required) {
433 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, required);
434 if (required) {
435 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
436 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
437 }
buzbee091cc402014-03-31 10:14:40 -0700438 return res;
439}
440
Serguei Katkov9ee45192014-07-17 14:39:03 +0700441RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class, bool required) {
buzbeea0cd2d72014-06-01 09:33:49 -0700442 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700443 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700444 return AllocTempDouble(required);
buzbeeb01bf152014-05-13 15:59:07 -0700445 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700446 return AllocTempWide(required);
buzbeeb01bf152014-05-13 15:59:07 -0700447}
448
Serguei Katkov9ee45192014-07-17 14:39:03 +0700449RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class, bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700450 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700451 return AllocTempSingle(required);
buzbeea0cd2d72014-06-01 09:33:49 -0700452 } else if (reg_class == kRefReg) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700453 return AllocTempRef(required);
buzbeeb01bf152014-05-13 15:59:07 -0700454 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700455 return AllocTemp(required);
buzbeeb01bf152014-05-13 15:59:07 -0700456}
457
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100458RegStorage Mir2Lir::FindLiveReg(ArenaVector<RegisterInfo*>& regs, int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700459 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100460 for (RegisterInfo* info : regs) {
buzbee091cc402014-03-31 10:14:40 -0700461 if ((info->SReg() == s_reg) && info->IsLive()) {
462 res = info->GetReg();
463 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 }
465 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 return res;
467}
468
buzbee091cc402014-03-31 10:14:40 -0700469RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
470 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700471 if (reg_class == kRefReg) {
472 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700473 CheckRegStorage(reg, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700474 }
475 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700476 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 }
buzbee091cc402014-03-31 10:14:40 -0700478 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700479 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700480 reg = FindLiveReg(wide || reg_class == kRefReg ? reg_pool_->core64_regs_ :
481 reg_pool_->core_regs_, s_reg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100482 } else {
483 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
484 }
buzbee091cc402014-03-31 10:14:40 -0700485 }
486 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700487 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700488 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700489 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
490 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700491 reg = RegStorage::MakeRegPair(reg, high_reg);
492 MarkWide(reg);
493 } else {
buzbee30adc732014-05-09 15:10:18 -0700494 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700495 reg = RegStorage::InvalidReg();
496 }
497 }
buzbee30adc732014-05-09 15:10:18 -0700498 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
499 // Width mismatch - don't try to reuse.
500 reg = RegStorage::InvalidReg();
501 }
502 }
503 if (reg.Valid()) {
504 if (reg.IsPair()) {
505 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
506 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
507 if (info_low->IsTemp()) {
508 info_low->MarkInUse();
509 }
510 if (info_high->IsTemp()) {
511 info_high->MarkInUse();
512 }
513 } else {
buzbee091cc402014-03-31 10:14:40 -0700514 RegisterInfo* info = GetRegInfo(reg);
515 if (info->IsTemp()) {
516 info->MarkInUse();
517 }
518 }
buzbee30adc732014-05-09 15:10:18 -0700519 } else {
520 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
521 ClobberSReg(s_reg);
522 if (wide) {
523 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700524 }
525 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700526 CheckRegStorage(reg, WidenessCheck::kIgnoreWide,
527 reg_class == kRefReg ? RefCheck::kCheckRef : RefCheck::kIgnoreRef,
528 FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700529 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530}
531
buzbee2700f7e2014-03-07 09:46:20 -0800532void Mir2Lir::FreeTemp(RegStorage reg) {
533 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700534 FreeTemp(reg.GetLow());
535 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800536 } else {
buzbee091cc402014-03-31 10:14:40 -0700537 RegisterInfo* p = GetRegInfo(reg);
538 if (p->IsTemp()) {
539 p->MarkFree();
540 p->SetIsWide(false);
541 p->SetPartner(reg);
542 }
buzbee2700f7e2014-03-07 09:46:20 -0800543 }
544}
545
buzbee082833c2014-05-17 23:16:26 -0700546void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
547 DCHECK(rl_keep.wide);
548 DCHECK(rl_free.wide);
549 int free_low = rl_free.reg.GetLowReg();
550 int free_high = rl_free.reg.GetHighReg();
551 int keep_low = rl_keep.reg.GetLowReg();
552 int keep_high = rl_keep.reg.GetHighReg();
553 if ((free_low != keep_low) && (free_low != keep_high) &&
554 (free_high != keep_low) && (free_high != keep_high)) {
555 // No overlap, free both
556 FreeTemp(rl_free.reg);
557 }
558}
559
buzbee262b2992014-03-27 11:22:43 -0700560bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700561 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800562 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700563 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
564 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700565 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700566 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800567 } else {
buzbee091cc402014-03-31 10:14:40 -0700568 RegisterInfo* p = GetRegInfo(reg);
569 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800570 }
buzbee091cc402014-03-31 10:14:40 -0700571 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572}
573
buzbee262b2992014-03-27 11:22:43 -0700574bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700575 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800576 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700577 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
578 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
579 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800580 } else {
buzbee091cc402014-03-31 10:14:40 -0700581 RegisterInfo* p = GetRegInfo(reg);
582 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800583 }
buzbee091cc402014-03-31 10:14:40 -0700584 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585}
586
buzbee262b2992014-03-27 11:22:43 -0700587bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700588 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800589 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700590 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
591 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
592 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800593 } else {
buzbee091cc402014-03-31 10:14:40 -0700594 RegisterInfo* p = GetRegInfo(reg);
595 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800596 }
buzbee091cc402014-03-31 10:14:40 -0700597 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598}
599
buzbee2700f7e2014-03-07 09:46:20 -0800600bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700601 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800602 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700603 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
604 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
605 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800606 } else {
buzbee091cc402014-03-31 10:14:40 -0700607 RegisterInfo* p = GetRegInfo(reg);
608 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800609 }
buzbee091cc402014-03-31 10:14:40 -0700610 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800611}
612
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613/*
614 * Similar to AllocTemp(), but forces the allocation of a specific
615 * register. No check is made to see if the register was previously
616 * allocated. Use with caution.
617 */
buzbee2700f7e2014-03-07 09:46:20 -0800618void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700619 DCHECK(IsTemp(reg));
620 if (reg.IsPair()) {
621 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
622 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
623 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700624 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700625 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700626 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700627 } else {
628 RegisterInfo* p = GetRegInfo(reg);
629 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700630 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700631 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632}
633
buzbee2700f7e2014-03-07 09:46:20 -0800634void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700635 if (reg.IsPair()) {
636 GetRegInfo(reg.GetLow())->ResetDefBody();
637 GetRegInfo(reg.GetHigh())->ResetDefBody();
638 } else {
639 GetRegInfo(reg)->ResetDefBody();
640 }
buzbee2700f7e2014-03-07 09:46:20 -0800641}
642
buzbee091cc402014-03-31 10:14:40 -0700643void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
644 RegisterInfo* info = nullptr;
645 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
646 if (IsTemp(rs)) {
647 info = GetRegInfo(reg);
648 }
649 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
650 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
651 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700653 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 break;
buzbee091cc402014-03-31 10:14:40 -0700655 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 }
657 }
658}
659
660/*
661 * Mark the beginning and end LIR of a def sequence. Note that
662 * on entry start points to the LIR prior to the beginning of the
663 * sequence.
664 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 DCHECK(!rl.wide);
667 DCHECK(start && start->next);
668 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700669 RegisterInfo* p = GetRegInfo(rl.reg);
670 p->SetDefStart(start->next);
671 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672}
673
674/*
675 * Mark the beginning and end LIR of a def sequence. Note that
676 * on entry start points to the LIR prior to the beginning of the
677 * sequence.
678 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700679void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 DCHECK(rl.wide);
681 DCHECK(start && start->next);
682 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700683 RegisterInfo* p;
684 if (rl.reg.IsPair()) {
685 p = GetRegInfo(rl.reg.GetLow());
686 ResetDef(rl.reg.GetHigh()); // Only track low of pair
687 } else {
688 p = GetRegInfo(rl.reg);
689 }
690 p->SetDefStart(start->next);
691 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692}
693
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700694void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700696 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
697 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 }
buzbee091cc402014-03-31 10:14:40 -0700699 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700}
701
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700702void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700704 // If pair, only track low reg of pair.
705 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
706 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
707 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 }
buzbee091cc402014-03-31 10:14:40 -0700709 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710}
711
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700712void Mir2Lir::ResetDefTracking() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100713 for (RegisterInfo* info : tempreg_info_) {
buzbee091cc402014-03-31 10:14:40 -0700714 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 }
716}
717
buzbeeba574512014-05-12 15:13:16 -0700718void Mir2Lir::ClobberAllTemps() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100719 for (RegisterInfo* info : tempreg_info_) {
buzbee30adc732014-05-09 15:10:18 -0700720 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700721 }
722}
723
724void Mir2Lir::FlushRegWide(RegStorage reg) {
725 if (reg.IsPair()) {
726 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
727 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
728 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
buzbeeb5860fb2014-06-21 15:31:01 -0700729 (info1->Partner().ExactlyEquals(info2->GetReg())) &&
730 (info2->Partner().ExactlyEquals(info1->GetReg())));
buzbee091cc402014-03-31 10:14:40 -0700731 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
732 if (!(info1->IsTemp() && info2->IsTemp())) {
733 /* Should not happen. If it does, there's a problem in eval_loc */
734 LOG(FATAL) << "Long half-temp, half-promoted";
735 }
736
737 info1->SetIsDirty(false);
738 info2->SetIsDirty(false);
739 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
740 info1 = info2;
741 }
742 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100743 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700744 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700745 }
746 } else {
747 RegisterInfo* info = GetRegInfo(reg);
748 if (info->IsLive() && info->IsDirty()) {
749 info->SetIsDirty(false);
750 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100751 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700752 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700753 }
754 }
755}
756
757void Mir2Lir::FlushReg(RegStorage reg) {
758 DCHECK(!reg.IsPair());
759 RegisterInfo* info = GetRegInfo(reg);
760 if (info->IsLive() && info->IsDirty()) {
761 info->SetIsDirty(false);
762 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100763 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700764 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 }
766}
767
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800768void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700769 if (info->IsWide()) {
770 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800771 } else {
buzbee091cc402014-03-31 10:14:40 -0700772 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 }
774}
775
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700776void Mir2Lir::FlushAllRegs() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100777 for (RegisterInfo* info : tempreg_info_) {
buzbeeba574512014-05-12 15:13:16 -0700778 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700779 FlushSpecificReg(info);
780 }
buzbee30adc732014-05-09 15:10:18 -0700781 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700782 info->SetIsWide(false);
783 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700784}
785
786
buzbee2700f7e2014-03-07 09:46:20 -0800787bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700788 if (reg_class == kAnyReg) {
789 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700790 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
791 /*
792 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
793 * with width here - that should be checked at a higher level (if needed).
794 */
buzbee091cc402014-03-31 10:14:40 -0700795 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 } else {
buzbee091cc402014-03-31 10:14:40 -0700797 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 }
799}
800
buzbee091cc402014-03-31 10:14:40 -0700801void Mir2Lir::MarkLive(RegLocation loc) {
802 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700803 if (!IsTemp(reg)) {
804 return;
805 }
buzbee091cc402014-03-31 10:14:40 -0700806 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700807 if (s_reg == INVALID_SREG) {
808 // Can't be live if no associated sreg.
809 if (reg.IsPair()) {
810 GetRegInfo(reg.GetLow())->MarkDead();
811 GetRegInfo(reg.GetHigh())->MarkDead();
812 } else {
813 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700814 }
buzbee082833c2014-05-17 23:16:26 -0700815 } else {
816 if (reg.IsPair()) {
817 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
818 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
819 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
820 (info_hi->SReg() == s_reg)) {
821 return; // Already live.
822 }
823 ClobberSReg(s_reg);
824 ClobberSReg(s_reg + 1);
825 info_lo->MarkLive(s_reg);
826 info_hi->MarkLive(s_reg + 1);
827 } else {
828 RegisterInfo* info = GetRegInfo(reg);
829 if (info->IsLive() && (info->SReg() == s_reg)) {
830 return; // Already live.
831 }
832 ClobberSReg(s_reg);
833 if (loc.wide) {
834 ClobberSReg(s_reg + 1);
835 }
836 info->MarkLive(s_reg);
837 }
838 if (loc.wide) {
839 MarkWide(reg);
840 } else {
841 MarkNarrow(reg);
842 }
buzbee091cc402014-03-31 10:14:40 -0700843 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844}
845
buzbee2700f7e2014-03-07 09:46:20 -0800846void Mir2Lir::MarkTemp(RegStorage reg) {
847 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700848 RegisterInfo* info = GetRegInfo(reg);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100849 tempreg_info_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -0700850 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851}
852
buzbee2700f7e2014-03-07 09:46:20 -0800853void Mir2Lir::UnmarkTemp(RegStorage reg) {
854 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700855 RegisterInfo* info = GetRegInfo(reg);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100856 auto pos = std::find(tempreg_info_.begin(), tempreg_info_.end(), info);
857 DCHECK(pos != tempreg_info_.end());
858 tempreg_info_.erase(pos);
buzbee091cc402014-03-31 10:14:40 -0700859 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800860}
861
buzbee091cc402014-03-31 10:14:40 -0700862void Mir2Lir::MarkWide(RegStorage reg) {
863 if (reg.IsPair()) {
864 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
865 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700866 // Unpair any old partners.
buzbeeb5860fb2014-06-21 15:31:01 -0700867 if (info_lo->IsWide() && info_lo->Partner().NotExactlyEquals(info_hi->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700868 GetRegInfo(info_lo->Partner())->SetIsWide(false);
869 }
buzbeeb5860fb2014-06-21 15:31:01 -0700870 if (info_hi->IsWide() && info_hi->Partner().NotExactlyEquals(info_lo->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700871 GetRegInfo(info_hi->Partner())->SetIsWide(false);
872 }
buzbee091cc402014-03-31 10:14:40 -0700873 info_lo->SetIsWide(true);
874 info_hi->SetIsWide(true);
875 info_lo->SetPartner(reg.GetHigh());
876 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800877 } else {
buzbee091cc402014-03-31 10:14:40 -0700878 RegisterInfo* info = GetRegInfo(reg);
879 info->SetIsWide(true);
880 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700881 }
882}
883
buzbee082833c2014-05-17 23:16:26 -0700884void Mir2Lir::MarkNarrow(RegStorage reg) {
885 DCHECK(!reg.IsPair());
886 RegisterInfo* info = GetRegInfo(reg);
887 info->SetIsWide(false);
888 info->SetPartner(reg);
889}
890
buzbee091cc402014-03-31 10:14:40 -0700891void Mir2Lir::MarkClean(RegLocation loc) {
892 if (loc.reg.IsPair()) {
893 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
894 info->SetIsDirty(false);
895 info = GetRegInfo(loc.reg.GetHigh());
896 info->SetIsDirty(false);
897 } else {
898 RegisterInfo* info = GetRegInfo(loc.reg);
899 info->SetIsDirty(false);
900 }
901}
902
903// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700904void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 if (loc.home) {
906 // If already home, can't be dirty
907 return;
908 }
buzbee091cc402014-03-31 10:14:40 -0700909 if (loc.reg.IsPair()) {
910 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
911 info->SetIsDirty(true);
912 info = GetRegInfo(loc.reg.GetHigh());
913 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800914 } else {
buzbee091cc402014-03-31 10:14:40 -0700915 RegisterInfo* info = GetRegInfo(loc.reg);
916 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700917 }
918}
919
buzbee2700f7e2014-03-07 09:46:20 -0800920void Mir2Lir::MarkInUse(RegStorage reg) {
921 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700922 GetRegInfo(reg.GetLow())->MarkInUse();
923 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800924 } else {
buzbee091cc402014-03-31 10:14:40 -0700925 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800926 }
927}
928
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700929bool Mir2Lir::CheckCorePoolSanity() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100930 for (RegisterInfo* info : tempreg_info_) {
buzbee3a658072014-08-28 13:48:56 -0700931 int my_sreg = info->SReg();
932 if (info->IsTemp() && info->IsLive() && info->IsWide() && my_sreg != INVALID_SREG) {
buzbee082833c2014-05-17 23:16:26 -0700933 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700934 RegStorage partner_reg = info->Partner();
935 RegisterInfo* partner = GetRegInfo(partner_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700936 DCHECK(partner != nullptr);
buzbee091cc402014-03-31 10:14:40 -0700937 DCHECK(partner->IsWide());
938 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700939 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700940 int partner_sreg = partner->SReg();
buzbee3a658072014-08-28 13:48:56 -0700941 int diff = my_sreg - partner_sreg;
942 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700943 }
buzbee082833c2014-05-17 23:16:26 -0700944 if (info->Master() != info) {
945 // Aliased.
946 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
947 // If I'm live, master should not be live, but should show liveness in alias set.
948 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
949 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700950 }
buzbee642fe342014-05-23 16:04:08 -0700951// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700952 }
953 if (info->IsAliased()) {
954 // Has child aliases.
955 DCHECK_EQ(info->Master(), info);
956 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
957 // Master live, no child should be dead - all should show liveness in set.
958 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
959 DCHECK(!p->IsDead());
960 DCHECK_EQ(p->SReg(), INVALID_SREG);
961 }
962 } else if (!info->IsDead()) {
963 // Master not live, one or more aliases must be.
964 bool live_alias = false;
965 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
966 live_alias |= p->IsLive();
967 }
968 DCHECK(live_alias);
969 }
970 }
971 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
972 // If not fully live, should have INVALID_SREG and def's should be null.
973 DCHECK(info->DefStart() == nullptr);
974 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700975 }
976 }
977 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978}
979
980/*
981 * Return an updated location record with current in-register status.
982 * If the value lives in live temps, reflect that fact. No code
983 * is generated. If the live value is part of an older pair,
984 * clobber both low and high.
985 * TUNING: clobbering both is a bit heavy-handed, but the alternative
986 * is a bit complex when dealing with FP regs. Examine code to see
987 * if it's worthwhile trying to be more clever here.
988 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700989RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 DCHECK(!loc.wide);
991 DCHECK(CheckCorePoolSanity());
992 if (loc.location != kLocPhysReg) {
993 DCHECK((loc.location == kLocDalvikFrame) ||
994 (loc.location == kLocCompilerTemp));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700995 RegStorage reg = AllocLiveReg(loc.s_reg_low, loc.ref ? kRefReg : kAnyReg, false);
buzbee091cc402014-03-31 10:14:40 -0700996 if (reg.Valid()) {
997 bool match = true;
998 RegisterInfo* info = GetRegInfo(reg);
999 match &= !reg.IsPair();
1000 match &= !info->IsWide();
1001 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001003 loc.reg = reg;
1004 } else {
1005 Clobber(reg);
1006 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 }
1008 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001009 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001010 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 return loc;
1012}
1013
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001014RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 DCHECK(loc.wide);
1016 DCHECK(CheckCorePoolSanity());
1017 if (loc.location != kLocPhysReg) {
1018 DCHECK((loc.location == kLocDalvikFrame) ||
1019 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001020 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1021 if (reg.Valid()) {
1022 bool match = true;
1023 if (reg.IsPair()) {
1024 // If we've got a register pair, make sure that it was last used as the same pair.
1025 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1026 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1027 match &= info_lo->IsWide();
1028 match &= info_hi->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001029 match &= (info_lo->Partner().ExactlyEquals(info_hi->GetReg()));
1030 match &= (info_hi->Partner().ExactlyEquals(info_lo->GetReg()));
buzbee091cc402014-03-31 10:14:40 -07001031 } else {
1032 RegisterInfo* info = GetRegInfo(reg);
1033 match &= info->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001034 match &= (info->GetReg().ExactlyEquals(info->Partner()));
buzbee091cc402014-03-31 10:14:40 -07001035 }
1036 if (match) {
1037 loc.location = kLocPhysReg;
1038 loc.reg = reg;
1039 } else {
1040 Clobber(reg);
1041 FreeTemp(reg);
1042 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001043 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001044 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 }
1046 return loc;
1047}
1048
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001050RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 if (loc.wide)
1052 return UpdateLocWide(loc);
1053 else
1054 return UpdateLoc(loc);
1055}
1056
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001057RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059
1060 loc = UpdateLocWide(loc);
1061
1062 /* If already in registers, we can assume proper form. Right reg class? */
1063 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001064 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001065 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001066 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001067 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001068 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001069 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001070 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001071 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001072 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001073 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001074 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 return loc;
1076 }
1077
1078 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1079 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1080
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001081 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001082 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 if (update) {
1085 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001086 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001088 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 return loc;
1090}
1091
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001092RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001093 // Narrow reg_class if the loc is a ref.
1094 if (loc.ref && reg_class == kAnyReg) {
1095 reg_class = kRefReg;
1096 }
1097
buzbee091cc402014-03-31 10:14:40 -07001098 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001100 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101
1102 loc = UpdateLoc(loc);
1103
1104 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001105 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001106 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001107 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001108 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001109 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001110 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001111 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001112 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001114 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 return loc;
1116 }
1117
1118 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1119
buzbee2700f7e2014-03-07 09:46:20 -08001120 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001121 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001122
1123 if (update) {
1124 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001125 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001126 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001127 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 return loc;
1129}
1130
Vladimir Markocc234812015-04-07 09:36:09 +01001131void Mir2Lir::AnalyzeMIR(RefCounts* core_counts, MIR* mir, uint32_t weight) {
1132 // NOTE: This should be in sync with functions that actually generate code for
1133 // the opcodes below. However, if we get this wrong, the generated code will
1134 // still be correct even if it may be sub-optimal.
1135 int opcode = mir->dalvikInsn.opcode;
1136 bool uses_method = false;
1137 bool uses_pc_rel_load = false;
1138 uint32_t dex_cache_array_offset = std::numeric_limits<uint32_t>::max();
1139 switch (opcode) {
1140 case Instruction::CHECK_CAST:
1141 case Instruction::INSTANCE_OF: {
1142 if ((opcode == Instruction::CHECK_CAST) &&
1143 (mir->optimization_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1144 break; // No code generated.
1145 }
1146 uint32_t type_idx =
1147 (opcode == Instruction::CHECK_CAST) ? mir->dalvikInsn.vB : mir->dalvikInsn.vC;
1148 bool type_known_final, type_known_abstract, use_declaring_class;
1149 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(
1150 cu_->method_idx, *cu_->dex_file, type_idx,
1151 &type_known_final, &type_known_abstract, &use_declaring_class);
1152 if (opcode == Instruction::CHECK_CAST && !needs_access_check &&
1153 cu_->compiler_driver->IsSafeCast(
1154 mir_graph_->GetCurrentDexCompilationUnit(), mir->offset)) {
1155 break; // No code generated.
1156 }
Vladimir Marko87b7c522015-04-08 10:01:01 +01001157 if (!needs_access_check && !use_declaring_class && CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001158 uses_pc_rel_load = true; // And ignore method use in slow path.
1159 dex_cache_array_offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1160 } else {
1161 uses_method = true;
1162 }
1163 break;
1164 }
1165
1166 case Instruction::CONST_CLASS:
Vladimir Marko87b7c522015-04-08 10:01:01 +01001167 if (CanUseOpPcRelDexCacheArrayLoad() &&
Vladimir Markocc234812015-04-07 09:36:09 +01001168 cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
1169 mir->dalvikInsn.vB)) {
1170 uses_pc_rel_load = true; // And ignore method use in slow path.
1171 dex_cache_array_offset = dex_cache_arrays_layout_.TypeOffset(mir->dalvikInsn.vB);
1172 } else {
1173 uses_method = true;
1174 }
1175 break;
1176
1177 case Instruction::CONST_STRING:
1178 case Instruction::CONST_STRING_JUMBO:
Vladimir Marko87b7c522015-04-08 10:01:01 +01001179 if (CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001180 uses_pc_rel_load = true; // And ignore method use in slow path.
1181 dex_cache_array_offset = dex_cache_arrays_layout_.StringOffset(mir->dalvikInsn.vB);
1182 } else {
1183 uses_method = true;
1184 }
1185 break;
1186
1187 case Instruction::INVOKE_VIRTUAL:
1188 case Instruction::INVOKE_SUPER:
1189 case Instruction::INVOKE_DIRECT:
1190 case Instruction::INVOKE_STATIC:
1191 case Instruction::INVOKE_INTERFACE:
1192 case Instruction::INVOKE_VIRTUAL_RANGE:
1193 case Instruction::INVOKE_SUPER_RANGE:
1194 case Instruction::INVOKE_DIRECT_RANGE:
1195 case Instruction::INVOKE_STATIC_RANGE:
1196 case Instruction::INVOKE_INTERFACE_RANGE:
1197 case Instruction::INVOKE_VIRTUAL_QUICK:
1198 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1199 const MirMethodLoweringInfo& info = mir_graph_->GetMethodLoweringInfo(mir);
1200 InvokeType sharp_type = info.GetSharpType();
Vladimir Marko87b7c522015-04-08 10:01:01 +01001201 if (info.IsIntrinsic()) {
1202 // Nothing to do, if an intrinsic uses ArtMethod* it's in the slow-path - don't count it.
1203 } else if (!info.FastPath() || (sharp_type != kStatic && sharp_type != kDirect)) {
Vladimir Markocc234812015-04-07 09:36:09 +01001204 // Nothing to do, the generated code or entrypoint uses method from the stack.
1205 } else if (info.DirectCode() != 0 && info.DirectMethod() != 0) {
1206 // Nothing to do, the generated code uses method from the stack.
Vladimir Marko87b7c522015-04-08 10:01:01 +01001207 } else if (CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001208 uses_pc_rel_load = true;
1209 dex_cache_array_offset = dex_cache_arrays_layout_.MethodOffset(mir->dalvikInsn.vB);
1210 } else {
1211 uses_method = true;
1212 }
1213 break;
1214 }
1215
1216 case Instruction::NEW_INSTANCE:
1217 case Instruction::NEW_ARRAY:
1218 case Instruction::FILLED_NEW_ARRAY:
1219 case Instruction::FILLED_NEW_ARRAY_RANGE:
1220 uses_method = true;
1221 break;
1222 case Instruction::FILL_ARRAY_DATA:
1223 // Nothing to do, the entrypoint uses method from the stack.
1224 break;
1225 case Instruction::THROW:
1226 // Nothing to do, the entrypoint uses method from the stack.
1227 break;
1228
1229 case Instruction::SGET:
1230 case Instruction::SGET_WIDE:
1231 case Instruction::SGET_OBJECT:
1232 case Instruction::SGET_BOOLEAN:
1233 case Instruction::SGET_BYTE:
1234 case Instruction::SGET_CHAR:
1235 case Instruction::SGET_SHORT:
1236 case Instruction::SPUT:
1237 case Instruction::SPUT_WIDE:
1238 case Instruction::SPUT_OBJECT:
1239 case Instruction::SPUT_BOOLEAN:
1240 case Instruction::SPUT_BYTE:
1241 case Instruction::SPUT_CHAR:
1242 case Instruction::SPUT_SHORT: {
1243 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
1244 bool fast = IsInstructionSGet(static_cast<Instruction::Code>(opcode))
1245 ? field_info.FastGet()
1246 : field_info.FastPut();
1247 if (fast && (cu_->enable_debug & (1 << kDebugSlowFieldPath)) == 0) {
Vladimir Marko87b7c522015-04-08 10:01:01 +01001248 if (!field_info.IsReferrersClass() && CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001249 uses_pc_rel_load = true; // And ignore method use in slow path.
1250 dex_cache_array_offset = dex_cache_arrays_layout_.TypeOffset(field_info.StorageIndex());
1251 } else {
1252 uses_method = true;
1253 }
1254 } else {
1255 // Nothing to do, the entrypoint uses method from the stack.
1256 }
1257 break;
1258 }
1259
1260 default:
1261 break;
1262 }
1263 if (uses_method) {
1264 core_counts[SRegToPMap(mir_graph_->GetMethodLoc().s_reg_low)].count += weight;
1265 }
1266 if (uses_pc_rel_load) {
Vladimir Marko87b7c522015-04-08 10:01:01 +01001267 if (pc_rel_temp_ != nullptr) {
1268 core_counts[SRegToPMap(pc_rel_temp_->s_reg_low)].count += weight;
1269 DCHECK_NE(dex_cache_array_offset, std::numeric_limits<uint32_t>::max());
1270 dex_cache_arrays_min_offset_ = std::min(dex_cache_arrays_min_offset_, dex_cache_array_offset);
1271 } else {
1272 // Nothing to do, using PC-relative addressing without promoting base PC to register.
1273 }
Vladimir Markocc234812015-04-07 09:36:09 +01001274 }
1275}
1276
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001278void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1280 RegLocation loc = mir_graph_->reg_location_[i];
1281 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1282 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001283 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001284 if (loc.fp) {
1285 if (loc.wide) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001286 if (WideFPRsAreAliases()) {
1287 // Floats and doubles can be counted together.
1288 counts[p_map_idx].count += use_count;
1289 } else {
1290 // Treat doubles as a unit, using upper half of fp_counts array.
1291 counts[p_map_idx + num_regs].count += use_count;
1292 }
buzbeec729a6b2013-09-14 16:04:31 -07001293 i++;
1294 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001295 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001296 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001297 } else {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001298 if (loc.wide && WideGPRsAreAliases()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001299 i++;
buzbeeb5860fb2014-06-21 15:31:01 -07001300 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001301 if (!IsInexpensiveConstant(loc)) {
1302 counts[p_map_idx].count += use_count;
1303 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001305 }
Vladimir Markocc234812015-04-07 09:36:09 +01001306
1307 // Now analyze the ArtMethod* and pc_rel_temp_ uses.
1308 DCHECK_EQ(core_counts[SRegToPMap(mir_graph_->GetMethodLoc().s_reg_low)].count, 0);
1309 if (pc_rel_temp_ != nullptr) {
1310 DCHECK_EQ(core_counts[SRegToPMap(pc_rel_temp_->s_reg_low)].count, 0);
1311 }
1312 PreOrderDfsIterator iter(mir_graph_);
1313 for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) {
1314 if (bb->block_type == kDead) {
1315 continue;
1316 }
1317 uint32_t weight = mir_graph_->GetUseCountWeight(bb);
1318 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
1319 AnalyzeMIR(core_counts, mir, weight);
1320 }
1321 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001322}
1323
1324/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001325static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1327 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Matteo Franchinc763e352014-07-04 12:53:27 +01001328 // Note that we fall back to sorting on reg so we get stable output on differing qsort
1329 // implementations (such as on host and target or between local host and build servers).
1330 // Note also that if a wide val1 and a non-wide val2 have the same count, then val1 always
1331 // ``loses'' (as STARTING_WIDE_SREG is or-ed in val1->s_reg).
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001332 return (op1->count == op2->count)
1333 ? (op1->s_reg - op2->s_reg)
1334 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335}
1336
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001337void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 LOG(INFO) << msg;
1339 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001340 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1341 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001342 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001343 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001344 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001345 }
1346}
1347
1348/*
1349 * Note: some portions of this code required even if the kPromoteRegs
1350 * optimization is disabled.
1351 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001352void Mir2Lir::DoPromotion() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001353 int num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001355 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +00001356 promotion_map_ = arena_->AllocArray<PromotionMap>(num_regs, kArenaAllocRegAlloc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001357
1358 // Allow target code to add any special registers
1359 AdjustSpillMask();
1360
1361 /*
1362 * Simple register promotion. Just do a static count of the uses
1363 * of Dalvik registers. Note that we examine the SSA names, but
1364 * count based on original Dalvik register name. Count refs
1365 * separately based on type in order to give allocation
1366 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001367 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 * reg.
1369 * TUNING: replace with linear scan once we have the ability
1370 * to describe register live ranges for GC.
1371 */
Matteo Franchinc763e352014-07-04 12:53:27 +01001372 size_t core_reg_count_size = WideGPRsAreAliases() ? num_regs : num_regs * 2;
1373 size_t fp_reg_count_size = WideFPRsAreAliases() ? num_regs : num_regs * 2;
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +00001374 RefCounts *core_regs = arena_->AllocArray<RefCounts>(core_reg_count_size, kArenaAllocRegAlloc);
1375 RefCounts *fp_regs = arena_->AllocArray<RefCounts>(fp_reg_count_size, kArenaAllocRegAlloc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001376 // Set ssa names for original Dalvik registers
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001377 for (int i = 0; i < num_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001378 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001379 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001380
buzbeeb5860fb2014-06-21 15:31:01 -07001381 // Duplicate in upper half to represent possible wide starting sregs.
1382 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1383 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1384 }
1385 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1386 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 }
1388
1389 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001390 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391
Brian Carlstrom7940e442013-07-12 13:46:57 -07001392 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001393 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1394 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395
1396 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001397 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1398 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001399 }
1400
1401 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001402 // Promote fp regs
1403 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1404 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1405 size_t p_map_idx = SRegToPMap(low_sreg);
1406 RegStorage reg = RegStorage::InvalidReg();
1407 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1408 // TODO: break out the Thumb2-specific code.
1409 if (cu_->instruction_set == kThumb2) {
1410 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1411 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001412 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001413 // Ignore result - if can't alloc double may still be able to alloc singles.
1414 AllocPreservedDouble(low_sreg);
1415 }
1416 // Continue regardless of success - might still be able to grab a single.
1417 continue;
1418 } else {
1419 reg = AllocPreservedSingle(low_sreg);
1420 }
1421 } else {
1422 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001423 }
buzbee2700f7e2014-03-07 09:46:20 -08001424 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001425 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 }
1427 }
1428 }
1429
1430 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001431 for (size_t i = 0; (i < core_reg_count_size) &&
1432 (core_regs[i].count >= promotion_threshold); i++) {
1433 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1434 size_t p_map_idx = SRegToPMap(low_sreg);
1435 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1436 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001437 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001438 break; // No more left
1439 }
1440 }
1441 }
1442 }
1443
1444 // Now, update SSA names to new home locations
1445 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1446 RegLocation *curr = &mir_graph_->reg_location_[i];
1447 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001448 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 -07001449 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001450 RegStorage reg = RegStorage::InvalidReg();
1451 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1452 if (wide && cu_->instruction_set == kThumb2) {
1453 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1454 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001455 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001456 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1457 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 }
1459 }
1460 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001461 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001462 }
buzbeeb5860fb2014-06-21 15:31:01 -07001463 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1464 if (wide && !cu_->target64) {
1465 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1466 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1467 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1468 }
1469 } else {
1470 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1471 }
1472 }
1473 if (reg.Valid()) {
1474 curr->reg = reg;
1475 curr->location = kLocPhysReg;
1476 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001477 }
1478 }
1479 if (cu_->verbose) {
1480 DumpPromotionMap();
1481 }
1482}
1483
1484/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001485int Mir2Lir::VRegOffset(int v_reg) {
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001486 const DexFile::CodeItem* code_item = mir_graph_->GetCurrentDexCompilationUnit()->GetCodeItem();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001487 return StackVisitor::GetVRegOffsetFromQuickCode(code_item, core_spill_mask_,
1488 fp_spill_mask_, frame_size_, v_reg,
1489 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001490}
1491
1492/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001493int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001494 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1495}
1496
1497/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001498RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1499 RegLocation res;
1500 switch (reg_class) {
1501 case kRefReg: LOG(FATAL); break;
1502 case kFPReg: res = LocCReturnDouble(); break;
1503 default: res = LocCReturnWide(); break;
1504 }
buzbee082833c2014-05-17 23:16:26 -07001505 Clobber(res.reg);
1506 LockTemp(res.reg);
1507 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001508 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001509 return res;
1510}
1511
buzbeea0cd2d72014-06-01 09:33:49 -07001512RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1513 RegLocation res;
1514 switch (reg_class) {
1515 case kRefReg: res = LocCReturnRef(); break;
1516 case kFPReg: res = LocCReturnFloat(); break;
1517 default: res = LocCReturn(); break;
1518 }
buzbee091cc402014-03-31 10:14:40 -07001519 Clobber(res.reg);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001520 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
buzbee091cc402014-03-31 10:14:40 -07001521 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001522 } else {
buzbee091cc402014-03-31 10:14:40 -07001523 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001525 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 return res;
1527}
1528
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001529void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 DoPromotion();
1531
1532 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1533 LOG(INFO) << "After Promotion";
1534 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1535 }
1536
1537 /* Set the frame size */
1538 frame_size_ = ComputeFrameSize();
1539}
1540
1541/*
1542 * Get the "real" sreg number associated with an s_reg slot. In general,
1543 * s_reg values passed through codegen are the SSA names created by
1544 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1545 * array. However, renaming is accomplished by simply replacing RegLocation
1546 * entries in the reglocation[] array. Therefore, when location
1547 * records for operands are first created, we need to ask the locRecord
1548 * identified by the dataflow pass what it's new name is.
1549 */
1550int Mir2Lir::GetSRegHi(int lowSreg) {
1551 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1552}
1553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001554bool Mir2Lir::LiveOut(int s_reg ATTRIBUTE_UNUSED) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001555 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001556 return true;
1557}
1558
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559} // namespace art