blob: 8ec86fa56c67df783226f67b0fc7dc4103da92f1 [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.
buzbee2700f7e2014-03-07 09:46:20 -0800323RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700324 UNUSED(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700325 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedDouble";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700326 UNREACHABLE();
buzbeeb5860fb2014-06-21 15:31:01 -0700327}
328
329// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
330RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700331 UNUSED(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700332 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedSingle";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700333 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334}
335
buzbee091cc402014-03-31 10:14:40 -0700336
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100337RegStorage Mir2Lir::AllocTempBody(ArenaVector<RegisterInfo*>& regs, int* next_temp, bool required) {
338 int num_regs = regs.size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700340 for (int i = 0; i< num_regs; i++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100341 if (next >= num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 next = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100343 }
344 RegisterInfo* info = regs[next];
buzbee30adc732014-05-09 15:10:18 -0700345 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700346 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee88a6b412014-08-25 09:34:03 -0700347 // If it's wide, split it up.
348 if (info->IsWide()) {
349 // If the pair was associated with a wide value, unmark the partner as well.
350 if (info->SReg() != INVALID_SREG) {
351 RegisterInfo* partner = GetRegInfo(info->Partner());
352 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
353 DCHECK(partner->IsWide());
354 partner->SetIsWide(false);
355 }
356 info->SetIsWide(false);
357 }
buzbee091cc402014-03-31 10:14:40 -0700358 Clobber(info->GetReg());
359 info->MarkInUse();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700361 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 }
363 next++;
364 }
365 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700366 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700367 for (int i = 0; i< num_regs; i++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100368 if (next >= num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 next = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100370 }
371 RegisterInfo* info = regs[next];
buzbee091cc402014-03-31 10:14:40 -0700372 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700373 // Got one. Kill it.
374 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700375 Clobber(info->GetReg());
376 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700377 if (info->IsWide()) {
378 RegisterInfo* partner = GetRegInfo(info->Partner());
379 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
380 DCHECK(partner->IsWide());
381 info->SetIsWide(false);
382 partner->SetIsWide(false);
383 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700385 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 }
387 next++;
388 }
389 if (required) {
390 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700391 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 LOG(FATAL) << "No free temp registers";
393 }
buzbee2700f7e2014-03-07 09:46:20 -0800394 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395}
396
Serguei Katkov9ee45192014-07-17 14:39:03 +0700397RegStorage Mir2Lir::AllocTemp(bool required) {
398 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, required);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399}
400
Serguei Katkov9ee45192014-07-17 14:39:03 +0700401RegStorage Mir2Lir::AllocTempWide(bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700402 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100403 if (reg_pool_->core64_regs_.size() != 0) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700404 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, required);
buzbeeb01bf152014-05-13 15:59:07 -0700405 } else {
406 RegStorage low_reg = AllocTemp();
407 RegStorage high_reg = AllocTemp();
408 res = RegStorage::MakeRegPair(low_reg, high_reg);
409 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700410 if (required) {
411 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kIgnoreRef, FPCheck::kCheckNotFP);
412 }
buzbeeb01bf152014-05-13 15:59:07 -0700413 return res;
414}
415
Serguei Katkov9ee45192014-07-17 14:39:03 +0700416RegStorage Mir2Lir::AllocTempRef(bool required) {
417 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, required);
418 if (required) {
419 DCHECK(!res.IsPair());
420 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
421 }
buzbeea0cd2d72014-06-01 09:33:49 -0700422 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100423}
424
Serguei Katkov9ee45192014-07-17 14:39:03 +0700425RegStorage Mir2Lir::AllocTempSingle(bool required) {
426 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, required);
427 if (required) {
428 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
429 CheckRegStorage(res, WidenessCheck::kCheckNotWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
430 }
buzbee091cc402014-03-31 10:14:40 -0700431 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432}
433
Serguei Katkov9ee45192014-07-17 14:39:03 +0700434RegStorage Mir2Lir::AllocTempDouble(bool required) {
435 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, required);
436 if (required) {
437 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
438 CheckRegStorage(res, WidenessCheck::kCheckWide, RefCheck::kCheckNotRef, FPCheck::kIgnoreFP);
439 }
buzbee091cc402014-03-31 10:14:40 -0700440 return res;
441}
442
Serguei Katkov9ee45192014-07-17 14:39:03 +0700443RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class, bool required) {
buzbeea0cd2d72014-06-01 09:33:49 -0700444 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700445 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700446 return AllocTempDouble(required);
buzbeeb01bf152014-05-13 15:59:07 -0700447 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700448 return AllocTempWide(required);
buzbeeb01bf152014-05-13 15:59:07 -0700449}
450
Serguei Katkov9ee45192014-07-17 14:39:03 +0700451RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class, bool required) {
buzbeeb01bf152014-05-13 15:59:07 -0700452 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700453 return AllocTempSingle(required);
buzbeea0cd2d72014-06-01 09:33:49 -0700454 } else if (reg_class == kRefReg) {
Serguei Katkov9ee45192014-07-17 14:39:03 +0700455 return AllocTempRef(required);
buzbeeb01bf152014-05-13 15:59:07 -0700456 }
Serguei Katkov9ee45192014-07-17 14:39:03 +0700457 return AllocTemp(required);
buzbeeb01bf152014-05-13 15:59:07 -0700458}
459
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100460RegStorage Mir2Lir::FindLiveReg(ArenaVector<RegisterInfo*>& regs, int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700461 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100462 for (RegisterInfo* info : regs) {
buzbee091cc402014-03-31 10:14:40 -0700463 if ((info->SReg() == s_reg) && info->IsLive()) {
464 res = info->GetReg();
465 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 }
467 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 return res;
469}
470
buzbee091cc402014-03-31 10:14:40 -0700471RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
472 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700473 if (reg_class == kRefReg) {
474 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -0700475 CheckRegStorage(reg, WidenessCheck::kCheckNotWide, RefCheck::kCheckRef, FPCheck::kCheckNotFP);
buzbeea0cd2d72014-06-01 09:33:49 -0700476 }
477 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700478 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 }
buzbee091cc402014-03-31 10:14:40 -0700480 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700481 if (cu_->target64) {
Andreas Gampe4b537a82014-06-30 22:24:53 -0700482 reg = FindLiveReg(wide || reg_class == kRefReg ? reg_pool_->core64_regs_ :
483 reg_pool_->core_regs_, s_reg);
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100484 } else {
485 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
486 }
buzbee091cc402014-03-31 10:14:40 -0700487 }
488 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700489 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700490 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700491 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
492 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700493 reg = RegStorage::MakeRegPair(reg, high_reg);
494 MarkWide(reg);
495 } else {
buzbee30adc732014-05-09 15:10:18 -0700496 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700497 reg = RegStorage::InvalidReg();
498 }
499 }
buzbee30adc732014-05-09 15:10:18 -0700500 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
501 // Width mismatch - don't try to reuse.
502 reg = RegStorage::InvalidReg();
503 }
504 }
505 if (reg.Valid()) {
506 if (reg.IsPair()) {
507 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
508 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
509 if (info_low->IsTemp()) {
510 info_low->MarkInUse();
511 }
512 if (info_high->IsTemp()) {
513 info_high->MarkInUse();
514 }
515 } else {
buzbee091cc402014-03-31 10:14:40 -0700516 RegisterInfo* info = GetRegInfo(reg);
517 if (info->IsTemp()) {
518 info->MarkInUse();
519 }
520 }
buzbee30adc732014-05-09 15:10:18 -0700521 } else {
522 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
523 ClobberSReg(s_reg);
524 if (wide) {
525 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700526 }
527 }
Andreas Gampe4b537a82014-06-30 22:24:53 -0700528 CheckRegStorage(reg, WidenessCheck::kIgnoreWide,
529 reg_class == kRefReg ? RefCheck::kCheckRef : RefCheck::kIgnoreRef,
530 FPCheck::kIgnoreFP);
buzbee091cc402014-03-31 10:14:40 -0700531 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532}
533
buzbee2700f7e2014-03-07 09:46:20 -0800534void Mir2Lir::FreeTemp(RegStorage reg) {
535 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700536 FreeTemp(reg.GetLow());
537 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800538 } else {
buzbee091cc402014-03-31 10:14:40 -0700539 RegisterInfo* p = GetRegInfo(reg);
540 if (p->IsTemp()) {
541 p->MarkFree();
542 p->SetIsWide(false);
543 p->SetPartner(reg);
544 }
buzbee2700f7e2014-03-07 09:46:20 -0800545 }
546}
547
buzbee082833c2014-05-17 23:16:26 -0700548void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
549 DCHECK(rl_keep.wide);
550 DCHECK(rl_free.wide);
551 int free_low = rl_free.reg.GetLowReg();
552 int free_high = rl_free.reg.GetHighReg();
553 int keep_low = rl_keep.reg.GetLowReg();
554 int keep_high = rl_keep.reg.GetHighReg();
555 if ((free_low != keep_low) && (free_low != keep_high) &&
556 (free_high != keep_low) && (free_high != keep_high)) {
557 // No overlap, free both
558 FreeTemp(rl_free.reg);
559 }
560}
561
buzbee262b2992014-03-27 11:22:43 -0700562bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700563 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800564 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700565 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
566 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700567 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700568 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800569 } else {
buzbee091cc402014-03-31 10:14:40 -0700570 RegisterInfo* p = GetRegInfo(reg);
571 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800572 }
buzbee091cc402014-03-31 10:14:40 -0700573 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574}
575
buzbee262b2992014-03-27 11:22:43 -0700576bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700577 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800578 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700579 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
580 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
581 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800582 } else {
buzbee091cc402014-03-31 10:14:40 -0700583 RegisterInfo* p = GetRegInfo(reg);
584 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800585 }
buzbee091cc402014-03-31 10:14:40 -0700586 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587}
588
buzbee262b2992014-03-27 11:22:43 -0700589bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700590 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800591 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700592 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
593 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
594 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800595 } else {
buzbee091cc402014-03-31 10:14:40 -0700596 RegisterInfo* p = GetRegInfo(reg);
597 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800598 }
buzbee091cc402014-03-31 10:14:40 -0700599 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600}
601
buzbee2700f7e2014-03-07 09:46:20 -0800602bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700603 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800604 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700605 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
606 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
607 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800608 } else {
buzbee091cc402014-03-31 10:14:40 -0700609 RegisterInfo* p = GetRegInfo(reg);
610 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800611 }
buzbee091cc402014-03-31 10:14:40 -0700612 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800613}
614
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615/*
616 * Similar to AllocTemp(), but forces the allocation of a specific
617 * register. No check is made to see if the register was previously
618 * allocated. Use with caution.
619 */
buzbee2700f7e2014-03-07 09:46:20 -0800620void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700621 DCHECK(IsTemp(reg));
622 if (reg.IsPair()) {
623 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
624 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
625 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700626 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700627 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700628 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700629 } else {
630 RegisterInfo* p = GetRegInfo(reg);
631 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700632 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700633 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634}
635
buzbee2700f7e2014-03-07 09:46:20 -0800636void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700637 if (reg.IsPair()) {
638 GetRegInfo(reg.GetLow())->ResetDefBody();
639 GetRegInfo(reg.GetHigh())->ResetDefBody();
640 } else {
641 GetRegInfo(reg)->ResetDefBody();
642 }
buzbee2700f7e2014-03-07 09:46:20 -0800643}
644
buzbee091cc402014-03-31 10:14:40 -0700645void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
646 RegisterInfo* info = nullptr;
647 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
648 if (IsTemp(rs)) {
649 info = GetRegInfo(reg);
650 }
651 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
652 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
653 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700655 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 break;
buzbee091cc402014-03-31 10:14:40 -0700657 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 }
659 }
660}
661
662/*
663 * Mark the beginning and end LIR of a def sequence. Note that
664 * on entry start points to the LIR prior to the beginning of the
665 * sequence.
666 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700667void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700668 DCHECK(!rl.wide);
669 DCHECK(start && start->next);
670 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700671 RegisterInfo* p = GetRegInfo(rl.reg);
672 p->SetDefStart(start->next);
673 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674}
675
676/*
677 * Mark the beginning and end LIR of a def sequence. Note that
678 * on entry start points to the LIR prior to the beginning of the
679 * sequence.
680 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700681void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 DCHECK(rl.wide);
683 DCHECK(start && start->next);
684 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700685 RegisterInfo* p;
686 if (rl.reg.IsPair()) {
687 p = GetRegInfo(rl.reg.GetLow());
688 ResetDef(rl.reg.GetHigh()); // Only track low of pair
689 } else {
690 p = GetRegInfo(rl.reg);
691 }
692 p->SetDefStart(start->next);
693 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694}
695
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700696void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700698 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
699 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 }
buzbee091cc402014-03-31 10:14:40 -0700701 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702}
703
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700704void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700706 // If pair, only track low reg of pair.
707 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
708 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
709 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 }
buzbee091cc402014-03-31 10:14:40 -0700711 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712}
713
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700714void Mir2Lir::ResetDefTracking() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100715 for (RegisterInfo* info : tempreg_info_) {
buzbee091cc402014-03-31 10:14:40 -0700716 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717 }
718}
719
buzbeeba574512014-05-12 15:13:16 -0700720void Mir2Lir::ClobberAllTemps() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100721 for (RegisterInfo* info : tempreg_info_) {
buzbee30adc732014-05-09 15:10:18 -0700722 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700723 }
724}
725
726void Mir2Lir::FlushRegWide(RegStorage reg) {
727 if (reg.IsPair()) {
728 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
729 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
730 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
buzbeeb5860fb2014-06-21 15:31:01 -0700731 (info1->Partner().ExactlyEquals(info2->GetReg())) &&
732 (info2->Partner().ExactlyEquals(info1->GetReg())));
buzbee091cc402014-03-31 10:14:40 -0700733 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
734 if (!(info1->IsTemp() && info2->IsTemp())) {
735 /* Should not happen. If it does, there's a problem in eval_loc */
736 LOG(FATAL) << "Long half-temp, half-promoted";
737 }
738
739 info1->SetIsDirty(false);
740 info2->SetIsDirty(false);
741 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
742 info1 = info2;
743 }
744 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100745 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700746 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700747 }
748 } else {
749 RegisterInfo* info = GetRegInfo(reg);
750 if (info->IsLive() && info->IsDirty()) {
751 info->SetIsDirty(false);
752 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100753 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700754 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700755 }
756 }
757}
758
759void Mir2Lir::FlushReg(RegStorage reg) {
760 DCHECK(!reg.IsPair());
761 RegisterInfo* info = GetRegInfo(reg);
762 if (info->IsLive() && info->IsDirty()) {
763 info->SetIsDirty(false);
764 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100765 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Chao-ying Fua77ee512014-07-01 17:43:41 -0700766 StoreBaseDisp(TargetPtrReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 }
768}
769
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800770void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700771 if (info->IsWide()) {
772 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800773 } else {
buzbee091cc402014-03-31 10:14:40 -0700774 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 }
776}
777
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700778void Mir2Lir::FlushAllRegs() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100779 for (RegisterInfo* info : tempreg_info_) {
buzbeeba574512014-05-12 15:13:16 -0700780 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700781 FlushSpecificReg(info);
782 }
buzbee30adc732014-05-09 15:10:18 -0700783 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700784 info->SetIsWide(false);
785 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786}
787
788
buzbee2700f7e2014-03-07 09:46:20 -0800789bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790 if (reg_class == kAnyReg) {
791 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700792 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
793 /*
794 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
795 * with width here - that should be checked at a higher level (if needed).
796 */
buzbee091cc402014-03-31 10:14:40 -0700797 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700798 } else {
buzbee091cc402014-03-31 10:14:40 -0700799 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700800 }
801}
802
buzbee091cc402014-03-31 10:14:40 -0700803void Mir2Lir::MarkLive(RegLocation loc) {
804 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700805 if (!IsTemp(reg)) {
806 return;
807 }
buzbee091cc402014-03-31 10:14:40 -0700808 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700809 if (s_reg == INVALID_SREG) {
810 // Can't be live if no associated sreg.
811 if (reg.IsPair()) {
812 GetRegInfo(reg.GetLow())->MarkDead();
813 GetRegInfo(reg.GetHigh())->MarkDead();
814 } else {
815 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700816 }
buzbee082833c2014-05-17 23:16:26 -0700817 } else {
818 if (reg.IsPair()) {
819 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
820 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
821 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
822 (info_hi->SReg() == s_reg)) {
823 return; // Already live.
824 }
825 ClobberSReg(s_reg);
826 ClobberSReg(s_reg + 1);
827 info_lo->MarkLive(s_reg);
828 info_hi->MarkLive(s_reg + 1);
829 } else {
830 RegisterInfo* info = GetRegInfo(reg);
831 if (info->IsLive() && (info->SReg() == s_reg)) {
832 return; // Already live.
833 }
834 ClobberSReg(s_reg);
835 if (loc.wide) {
836 ClobberSReg(s_reg + 1);
837 }
838 info->MarkLive(s_reg);
839 }
840 if (loc.wide) {
841 MarkWide(reg);
842 } else {
843 MarkNarrow(reg);
844 }
buzbee091cc402014-03-31 10:14:40 -0700845 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846}
847
buzbee2700f7e2014-03-07 09:46:20 -0800848void Mir2Lir::MarkTemp(RegStorage reg) {
849 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 RegisterInfo* info = GetRegInfo(reg);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100851 tempreg_info_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -0700852 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853}
854
buzbee2700f7e2014-03-07 09:46:20 -0800855void Mir2Lir::UnmarkTemp(RegStorage reg) {
856 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700857 RegisterInfo* info = GetRegInfo(reg);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100858 auto pos = std::find(tempreg_info_.begin(), tempreg_info_.end(), info);
859 DCHECK(pos != tempreg_info_.end());
860 tempreg_info_.erase(pos);
buzbee091cc402014-03-31 10:14:40 -0700861 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800862}
863
buzbee091cc402014-03-31 10:14:40 -0700864void Mir2Lir::MarkWide(RegStorage reg) {
865 if (reg.IsPair()) {
866 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
867 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700868 // Unpair any old partners.
buzbeeb5860fb2014-06-21 15:31:01 -0700869 if (info_lo->IsWide() && info_lo->Partner().NotExactlyEquals(info_hi->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700870 GetRegInfo(info_lo->Partner())->SetIsWide(false);
871 }
buzbeeb5860fb2014-06-21 15:31:01 -0700872 if (info_hi->IsWide() && info_hi->Partner().NotExactlyEquals(info_lo->GetReg())) {
buzbee082833c2014-05-17 23:16:26 -0700873 GetRegInfo(info_hi->Partner())->SetIsWide(false);
874 }
buzbee091cc402014-03-31 10:14:40 -0700875 info_lo->SetIsWide(true);
876 info_hi->SetIsWide(true);
877 info_lo->SetPartner(reg.GetHigh());
878 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800879 } else {
buzbee091cc402014-03-31 10:14:40 -0700880 RegisterInfo* info = GetRegInfo(reg);
881 info->SetIsWide(true);
882 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 }
884}
885
buzbee082833c2014-05-17 23:16:26 -0700886void Mir2Lir::MarkNarrow(RegStorage reg) {
887 DCHECK(!reg.IsPair());
888 RegisterInfo* info = GetRegInfo(reg);
889 info->SetIsWide(false);
890 info->SetPartner(reg);
891}
892
buzbee091cc402014-03-31 10:14:40 -0700893void Mir2Lir::MarkClean(RegLocation loc) {
894 if (loc.reg.IsPair()) {
895 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
896 info->SetIsDirty(false);
897 info = GetRegInfo(loc.reg.GetHigh());
898 info->SetIsDirty(false);
899 } else {
900 RegisterInfo* info = GetRegInfo(loc.reg);
901 info->SetIsDirty(false);
902 }
903}
904
905// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700906void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700907 if (loc.home) {
908 // If already home, can't be dirty
909 return;
910 }
buzbee091cc402014-03-31 10:14:40 -0700911 if (loc.reg.IsPair()) {
912 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
913 info->SetIsDirty(true);
914 info = GetRegInfo(loc.reg.GetHigh());
915 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800916 } else {
buzbee091cc402014-03-31 10:14:40 -0700917 RegisterInfo* info = GetRegInfo(loc.reg);
918 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 }
920}
921
buzbee2700f7e2014-03-07 09:46:20 -0800922void Mir2Lir::MarkInUse(RegStorage reg) {
923 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700924 GetRegInfo(reg.GetLow())->MarkInUse();
925 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800926 } else {
buzbee091cc402014-03-31 10:14:40 -0700927 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800928 }
929}
930
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700931bool Mir2Lir::CheckCorePoolSanity() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100932 for (RegisterInfo* info : tempreg_info_) {
buzbee3a658072014-08-28 13:48:56 -0700933 int my_sreg = info->SReg();
934 if (info->IsTemp() && info->IsLive() && info->IsWide() && my_sreg != INVALID_SREG) {
buzbee082833c2014-05-17 23:16:26 -0700935 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700936 RegStorage partner_reg = info->Partner();
937 RegisterInfo* partner = GetRegInfo(partner_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700938 DCHECK(partner != nullptr);
buzbee091cc402014-03-31 10:14:40 -0700939 DCHECK(partner->IsWide());
940 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700941 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700942 int partner_sreg = partner->SReg();
buzbee3a658072014-08-28 13:48:56 -0700943 int diff = my_sreg - partner_sreg;
944 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700945 }
buzbee082833c2014-05-17 23:16:26 -0700946 if (info->Master() != info) {
947 // Aliased.
948 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
949 // If I'm live, master should not be live, but should show liveness in alias set.
950 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
951 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700952 }
buzbee642fe342014-05-23 16:04:08 -0700953// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700954 }
955 if (info->IsAliased()) {
956 // Has child aliases.
957 DCHECK_EQ(info->Master(), info);
958 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
959 // Master live, no child should be dead - all should show liveness in set.
960 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
961 DCHECK(!p->IsDead());
962 DCHECK_EQ(p->SReg(), INVALID_SREG);
963 }
964 } else if (!info->IsDead()) {
965 // Master not live, one or more aliases must be.
966 bool live_alias = false;
967 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
968 live_alias |= p->IsLive();
969 }
970 DCHECK(live_alias);
971 }
972 }
973 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
974 // If not fully live, should have INVALID_SREG and def's should be null.
975 DCHECK(info->DefStart() == nullptr);
976 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700977 }
978 }
979 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980}
981
982/*
983 * Return an updated location record with current in-register status.
984 * If the value lives in live temps, reflect that fact. No code
985 * is generated. If the live value is part of an older pair,
986 * clobber both low and high.
987 * TUNING: clobbering both is a bit heavy-handed, but the alternative
988 * is a bit complex when dealing with FP regs. Examine code to see
989 * if it's worthwhile trying to be more clever here.
990 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700991RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 DCHECK(!loc.wide);
993 DCHECK(CheckCorePoolSanity());
994 if (loc.location != kLocPhysReg) {
995 DCHECK((loc.location == kLocDalvikFrame) ||
996 (loc.location == kLocCompilerTemp));
Andreas Gampe4b537a82014-06-30 22:24:53 -0700997 RegStorage reg = AllocLiveReg(loc.s_reg_low, loc.ref ? kRefReg : kAnyReg, false);
buzbee091cc402014-03-31 10:14:40 -0700998 if (reg.Valid()) {
999 bool match = true;
1000 RegisterInfo* info = GetRegInfo(reg);
1001 match &= !reg.IsPair();
1002 match &= !info->IsWide();
1003 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001005 loc.reg = reg;
1006 } else {
1007 Clobber(reg);
1008 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 }
1010 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001011 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 return loc;
1014}
1015
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001016RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 DCHECK(loc.wide);
1018 DCHECK(CheckCorePoolSanity());
1019 if (loc.location != kLocPhysReg) {
1020 DCHECK((loc.location == kLocDalvikFrame) ||
1021 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001022 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1023 if (reg.Valid()) {
1024 bool match = true;
1025 if (reg.IsPair()) {
1026 // If we've got a register pair, make sure that it was last used as the same pair.
1027 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1028 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1029 match &= info_lo->IsWide();
1030 match &= info_hi->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001031 match &= (info_lo->Partner().ExactlyEquals(info_hi->GetReg()));
1032 match &= (info_hi->Partner().ExactlyEquals(info_lo->GetReg()));
buzbee091cc402014-03-31 10:14:40 -07001033 } else {
1034 RegisterInfo* info = GetRegInfo(reg);
1035 match &= info->IsWide();
buzbeeb5860fb2014-06-21 15:31:01 -07001036 match &= (info->GetReg().ExactlyEquals(info->Partner()));
buzbee091cc402014-03-31 10:14:40 -07001037 }
1038 if (match) {
1039 loc.location = kLocPhysReg;
1040 loc.reg = reg;
1041 } else {
1042 Clobber(reg);
1043 FreeTemp(reg);
1044 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001046 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 }
1048 return loc;
1049}
1050
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001052RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 if (loc.wide)
1054 return UpdateLocWide(loc);
1055 else
1056 return UpdateLoc(loc);
1057}
1058
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001059RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061
1062 loc = UpdateLocWide(loc);
1063
1064 /* If already in registers, we can assume proper form. Right reg class? */
1065 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001066 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001067 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001068 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001069 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001070 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001071 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001072 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001073 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001074 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001076 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001077 return loc;
1078 }
1079
1080 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1081 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1082
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001083 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001084 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 if (update) {
1087 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001088 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001090 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 return loc;
1092}
1093
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001094RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
Andreas Gampe4b537a82014-06-30 22:24:53 -07001095 // Narrow reg_class if the loc is a ref.
1096 if (loc.ref && reg_class == kAnyReg) {
1097 reg_class = kRefReg;
1098 }
1099
buzbee091cc402014-03-31 10:14:40 -07001100 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001102 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001103
1104 loc = UpdateLoc(loc);
1105
1106 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001107 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001108 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001109 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001110 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001111 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001112 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001113 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001114 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001116 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 return loc;
1118 }
1119
1120 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1121
buzbee2700f7e2014-03-07 09:46:20 -08001122 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001123 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124
1125 if (update) {
1126 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001127 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001128 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001129 CheckRegLocation(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 return loc;
1131}
1132
Vladimir Markocc234812015-04-07 09:36:09 +01001133void Mir2Lir::AnalyzeMIR(RefCounts* core_counts, MIR* mir, uint32_t weight) {
1134 // NOTE: This should be in sync with functions that actually generate code for
1135 // the opcodes below. However, if we get this wrong, the generated code will
1136 // still be correct even if it may be sub-optimal.
1137 int opcode = mir->dalvikInsn.opcode;
1138 bool uses_method = false;
1139 bool uses_pc_rel_load = false;
1140 uint32_t dex_cache_array_offset = std::numeric_limits<uint32_t>::max();
1141 switch (opcode) {
1142 case Instruction::CHECK_CAST:
1143 case Instruction::INSTANCE_OF: {
1144 if ((opcode == Instruction::CHECK_CAST) &&
1145 (mir->optimization_flags & MIR_IGNORE_CHECK_CAST) != 0) {
1146 break; // No code generated.
1147 }
1148 uint32_t type_idx =
1149 (opcode == Instruction::CHECK_CAST) ? mir->dalvikInsn.vB : mir->dalvikInsn.vC;
1150 bool type_known_final, type_known_abstract, use_declaring_class;
1151 bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(
1152 cu_->method_idx, *cu_->dex_file, type_idx,
1153 &type_known_final, &type_known_abstract, &use_declaring_class);
1154 if (opcode == Instruction::CHECK_CAST && !needs_access_check &&
1155 cu_->compiler_driver->IsSafeCast(
1156 mir_graph_->GetCurrentDexCompilationUnit(), mir->offset)) {
1157 break; // No code generated.
1158 }
Vladimir Marko87b7c522015-04-08 10:01:01 +01001159 if (!needs_access_check && !use_declaring_class && CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001160 uses_pc_rel_load = true; // And ignore method use in slow path.
1161 dex_cache_array_offset = dex_cache_arrays_layout_.TypeOffset(type_idx);
1162 } else {
1163 uses_method = true;
1164 }
1165 break;
1166 }
1167
1168 case Instruction::CONST_CLASS:
Vladimir Marko87b7c522015-04-08 10:01:01 +01001169 if (CanUseOpPcRelDexCacheArrayLoad() &&
Vladimir Markocc234812015-04-07 09:36:09 +01001170 cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
1171 mir->dalvikInsn.vB)) {
1172 uses_pc_rel_load = true; // And ignore method use in slow path.
1173 dex_cache_array_offset = dex_cache_arrays_layout_.TypeOffset(mir->dalvikInsn.vB);
1174 } else {
1175 uses_method = true;
1176 }
1177 break;
1178
1179 case Instruction::CONST_STRING:
1180 case Instruction::CONST_STRING_JUMBO:
Vladimir Marko87b7c522015-04-08 10:01:01 +01001181 if (CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001182 uses_pc_rel_load = true; // And ignore method use in slow path.
1183 dex_cache_array_offset = dex_cache_arrays_layout_.StringOffset(mir->dalvikInsn.vB);
1184 } else {
1185 uses_method = true;
1186 }
1187 break;
1188
1189 case Instruction::INVOKE_VIRTUAL:
1190 case Instruction::INVOKE_SUPER:
1191 case Instruction::INVOKE_DIRECT:
1192 case Instruction::INVOKE_STATIC:
1193 case Instruction::INVOKE_INTERFACE:
1194 case Instruction::INVOKE_VIRTUAL_RANGE:
1195 case Instruction::INVOKE_SUPER_RANGE:
1196 case Instruction::INVOKE_DIRECT_RANGE:
1197 case Instruction::INVOKE_STATIC_RANGE:
1198 case Instruction::INVOKE_INTERFACE_RANGE:
1199 case Instruction::INVOKE_VIRTUAL_QUICK:
1200 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1201 const MirMethodLoweringInfo& info = mir_graph_->GetMethodLoweringInfo(mir);
1202 InvokeType sharp_type = info.GetSharpType();
Vladimir Marko87b7c522015-04-08 10:01:01 +01001203 if (info.IsIntrinsic()) {
1204 // Nothing to do, if an intrinsic uses ArtMethod* it's in the slow-path - don't count it.
1205 } else if (!info.FastPath() || (sharp_type != kStatic && sharp_type != kDirect)) {
Vladimir Markocc234812015-04-07 09:36:09 +01001206 // Nothing to do, the generated code or entrypoint uses method from the stack.
1207 } else if (info.DirectCode() != 0 && info.DirectMethod() != 0) {
1208 // Nothing to do, the generated code uses method from the stack.
Vladimir Marko87b7c522015-04-08 10:01:01 +01001209 } else if (CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001210 uses_pc_rel_load = true;
1211 dex_cache_array_offset = dex_cache_arrays_layout_.MethodOffset(mir->dalvikInsn.vB);
1212 } else {
1213 uses_method = true;
1214 }
1215 break;
1216 }
1217
1218 case Instruction::NEW_INSTANCE:
1219 case Instruction::NEW_ARRAY:
1220 case Instruction::FILLED_NEW_ARRAY:
1221 case Instruction::FILLED_NEW_ARRAY_RANGE:
1222 uses_method = true;
1223 break;
1224 case Instruction::FILL_ARRAY_DATA:
1225 // Nothing to do, the entrypoint uses method from the stack.
1226 break;
1227 case Instruction::THROW:
1228 // Nothing to do, the entrypoint uses method from the stack.
1229 break;
1230
1231 case Instruction::SGET:
1232 case Instruction::SGET_WIDE:
1233 case Instruction::SGET_OBJECT:
1234 case Instruction::SGET_BOOLEAN:
1235 case Instruction::SGET_BYTE:
1236 case Instruction::SGET_CHAR:
1237 case Instruction::SGET_SHORT:
1238 case Instruction::SPUT:
1239 case Instruction::SPUT_WIDE:
1240 case Instruction::SPUT_OBJECT:
1241 case Instruction::SPUT_BOOLEAN:
1242 case Instruction::SPUT_BYTE:
1243 case Instruction::SPUT_CHAR:
1244 case Instruction::SPUT_SHORT: {
1245 const MirSFieldLoweringInfo& field_info = mir_graph_->GetSFieldLoweringInfo(mir);
1246 bool fast = IsInstructionSGet(static_cast<Instruction::Code>(opcode))
1247 ? field_info.FastGet()
1248 : field_info.FastPut();
1249 if (fast && (cu_->enable_debug & (1 << kDebugSlowFieldPath)) == 0) {
Vladimir Marko87b7c522015-04-08 10:01:01 +01001250 if (!field_info.IsReferrersClass() && CanUseOpPcRelDexCacheArrayLoad()) {
Vladimir Markocc234812015-04-07 09:36:09 +01001251 uses_pc_rel_load = true; // And ignore method use in slow path.
1252 dex_cache_array_offset = dex_cache_arrays_layout_.TypeOffset(field_info.StorageIndex());
1253 } else {
1254 uses_method = true;
1255 }
1256 } else {
1257 // Nothing to do, the entrypoint uses method from the stack.
1258 }
1259 break;
1260 }
1261
1262 default:
1263 break;
1264 }
1265 if (uses_method) {
1266 core_counts[SRegToPMap(mir_graph_->GetMethodLoc().s_reg_low)].count += weight;
1267 }
1268 if (uses_pc_rel_load) {
Vladimir Marko87b7c522015-04-08 10:01:01 +01001269 if (pc_rel_temp_ != nullptr) {
1270 core_counts[SRegToPMap(pc_rel_temp_->s_reg_low)].count += weight;
1271 DCHECK_NE(dex_cache_array_offset, std::numeric_limits<uint32_t>::max());
1272 dex_cache_arrays_min_offset_ = std::min(dex_cache_arrays_min_offset_, dex_cache_array_offset);
1273 } else {
1274 // Nothing to do, using PC-relative addressing without promoting base PC to register.
1275 }
Vladimir Markocc234812015-04-07 09:36:09 +01001276 }
1277}
1278
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001280void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1282 RegLocation loc = mir_graph_->reg_location_[i];
1283 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1284 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001285 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001286 if (loc.fp) {
1287 if (loc.wide) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001288 if (WideFPRsAreAliases()) {
1289 // Floats and doubles can be counted together.
1290 counts[p_map_idx].count += use_count;
1291 } else {
1292 // Treat doubles as a unit, using upper half of fp_counts array.
1293 counts[p_map_idx + num_regs].count += use_count;
1294 }
buzbeec729a6b2013-09-14 16:04:31 -07001295 i++;
1296 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001297 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001298 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001299 } else {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001300 if (loc.wide && WideGPRsAreAliases()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001301 i++;
buzbeeb5860fb2014-06-21 15:31:01 -07001302 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001303 if (!IsInexpensiveConstant(loc)) {
1304 counts[p_map_idx].count += use_count;
1305 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001307 }
Vladimir Markocc234812015-04-07 09:36:09 +01001308
1309 // Now analyze the ArtMethod* and pc_rel_temp_ uses.
1310 DCHECK_EQ(core_counts[SRegToPMap(mir_graph_->GetMethodLoc().s_reg_low)].count, 0);
1311 if (pc_rel_temp_ != nullptr) {
1312 DCHECK_EQ(core_counts[SRegToPMap(pc_rel_temp_->s_reg_low)].count, 0);
1313 }
1314 PreOrderDfsIterator iter(mir_graph_);
1315 for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) {
1316 if (bb->block_type == kDead) {
1317 continue;
1318 }
1319 uint32_t weight = mir_graph_->GetUseCountWeight(bb);
1320 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
1321 AnalyzeMIR(core_counts, mir, weight);
1322 }
1323 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001324}
1325
1326/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001327static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1329 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Matteo Franchinc763e352014-07-04 12:53:27 +01001330 // Note that we fall back to sorting on reg so we get stable output on differing qsort
1331 // implementations (such as on host and target or between local host and build servers).
1332 // Note also that if a wide val1 and a non-wide val2 have the same count, then val1 always
1333 // ``loses'' (as STARTING_WIDE_SREG is or-ed in val1->s_reg).
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001334 return (op1->count == op2->count)
1335 ? (op1->s_reg - op2->s_reg)
1336 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337}
1338
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001339void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001340 LOG(INFO) << msg;
1341 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001342 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1343 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001344 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001345 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001346 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 }
1348}
1349
1350/*
1351 * Note: some portions of this code required even if the kPromoteRegs
1352 * optimization is disabled.
1353 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001354void Mir2Lir::DoPromotion() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001355 int num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001357 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +00001358 promotion_map_ = arena_->AllocArray<PromotionMap>(num_regs, kArenaAllocRegAlloc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001359
1360 // Allow target code to add any special registers
1361 AdjustSpillMask();
1362
1363 /*
1364 * Simple register promotion. Just do a static count of the uses
1365 * of Dalvik registers. Note that we examine the SSA names, but
1366 * count based on original Dalvik register name. Count refs
1367 * separately based on type in order to give allocation
1368 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001369 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001370 * reg.
1371 * TUNING: replace with linear scan once we have the ability
1372 * to describe register live ranges for GC.
1373 */
Matteo Franchinc763e352014-07-04 12:53:27 +01001374 size_t core_reg_count_size = WideGPRsAreAliases() ? num_regs : num_regs * 2;
1375 size_t fp_reg_count_size = WideFPRsAreAliases() ? num_regs : num_regs * 2;
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +00001376 RefCounts *core_regs = arena_->AllocArray<RefCounts>(core_reg_count_size, kArenaAllocRegAlloc);
1377 RefCounts *fp_regs = arena_->AllocArray<RefCounts>(fp_reg_count_size, kArenaAllocRegAlloc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001378 // Set ssa names for original Dalvik registers
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001379 for (int i = 0; i < num_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001380 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001382
buzbeeb5860fb2014-06-21 15:31:01 -07001383 // Duplicate in upper half to represent possible wide starting sregs.
1384 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1385 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1386 }
1387 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1388 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389 }
1390
1391 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001392 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001393
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001395 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1396 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001397
1398 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001399 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1400 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001401 }
1402
1403 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001404 // Promote fp regs
1405 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1406 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1407 size_t p_map_idx = SRegToPMap(low_sreg);
1408 RegStorage reg = RegStorage::InvalidReg();
1409 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1410 // TODO: break out the Thumb2-specific code.
1411 if (cu_->instruction_set == kThumb2) {
1412 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1413 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001414 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001415 // Ignore result - if can't alloc double may still be able to alloc singles.
1416 AllocPreservedDouble(low_sreg);
1417 }
1418 // Continue regardless of success - might still be able to grab a single.
1419 continue;
1420 } else {
1421 reg = AllocPreservedSingle(low_sreg);
1422 }
1423 } else {
1424 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001425 }
buzbee2700f7e2014-03-07 09:46:20 -08001426 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001427 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001428 }
1429 }
1430 }
1431
1432 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001433 for (size_t i = 0; (i < core_reg_count_size) &&
1434 (core_regs[i].count >= promotion_threshold); i++) {
1435 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1436 size_t p_map_idx = SRegToPMap(low_sreg);
1437 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1438 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001439 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 break; // No more left
1441 }
1442 }
1443 }
1444 }
1445
1446 // Now, update SSA names to new home locations
1447 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1448 RegLocation *curr = &mir_graph_->reg_location_[i];
1449 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001450 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 -07001451 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001452 RegStorage reg = RegStorage::InvalidReg();
1453 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1454 if (wide && cu_->instruction_set == kThumb2) {
1455 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1456 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001457 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001458 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1459 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001460 }
1461 }
1462 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001463 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001464 }
buzbeeb5860fb2014-06-21 15:31:01 -07001465 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1466 if (wide && !cu_->target64) {
1467 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1468 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1469 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1470 }
1471 } else {
1472 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1473 }
1474 }
1475 if (reg.Valid()) {
1476 curr->reg = reg;
1477 curr->location = kLocPhysReg;
1478 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 }
1480 }
1481 if (cu_->verbose) {
1482 DumpPromotionMap();
1483 }
1484}
1485
1486/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001487int Mir2Lir::VRegOffset(int v_reg) {
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001488 const DexFile::CodeItem* code_item = mir_graph_->GetCurrentDexCompilationUnit()->GetCodeItem();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001489 return StackVisitor::GetVRegOffsetFromQuickCode(code_item, core_spill_mask_,
1490 fp_spill_mask_, frame_size_, v_reg,
1491 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001492}
1493
1494/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001495int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001496 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1497}
1498
1499/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001500RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1501 RegLocation res;
1502 switch (reg_class) {
1503 case kRefReg: LOG(FATAL); break;
1504 case kFPReg: res = LocCReturnDouble(); break;
1505 default: res = LocCReturnWide(); break;
1506 }
buzbee082833c2014-05-17 23:16:26 -07001507 Clobber(res.reg);
1508 LockTemp(res.reg);
1509 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001510 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001511 return res;
1512}
1513
buzbeea0cd2d72014-06-01 09:33:49 -07001514RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1515 RegLocation res;
1516 switch (reg_class) {
1517 case kRefReg: res = LocCReturnRef(); break;
1518 case kFPReg: res = LocCReturnFloat(); break;
1519 default: res = LocCReturn(); break;
1520 }
buzbee091cc402014-03-31 10:14:40 -07001521 Clobber(res.reg);
Maja Gagic6ea651f2015-02-24 16:55:04 +01001522 if (cu_->instruction_set == kMips || cu_->instruction_set == kMips64) {
buzbee091cc402014-03-31 10:14:40 -07001523 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001524 } else {
buzbee091cc402014-03-31 10:14:40 -07001525 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001526 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001527 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001528 return res;
1529}
1530
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001531void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001532 DoPromotion();
1533
1534 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1535 LOG(INFO) << "After Promotion";
1536 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1537 }
1538
1539 /* Set the frame size */
1540 frame_size_ = ComputeFrameSize();
1541}
1542
1543/*
1544 * Get the "real" sreg number associated with an s_reg slot. In general,
1545 * s_reg values passed through codegen are the SSA names created by
1546 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1547 * array. However, renaming is accomplished by simply replacing RegLocation
1548 * entries in the reglocation[] array. Therefore, when location
1549 * records for operands are first created, we need to ask the locRecord
1550 * identified by the dataflow pass what it's new name is.
1551 */
1552int Mir2Lir::GetSRegHi(int lowSreg) {
1553 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1554}
1555
buzbee091cc402014-03-31 10:14:40 -07001556bool Mir2Lir::LiveOut(int s_reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001557 UNUSED(s_reg);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001558 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001559 return true;
1560}
1561
Brian Carlstrom7940e442013-07-12 13:46:57 -07001562} // namespace art