blob: 67fb8040f7bf24ca5820e4c60868d3dbc1554b80 [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"
22#include "dex/mir_graph.h"
23#include "driver/compiler_driver.h"
24#include "driver/dex_compilation_unit.h"
25
Brian Carlstrom7940e442013-07-12 13:46:57 -070026namespace art {
27
28/*
29 * Free all allocated temps in the temp pools. Note that this does
30 * not affect the "liveness" of a temp register, which will stay
31 * live until it is either explicitly killed or reallocated.
32 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070033void Mir2Lir::ResetRegPool() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010034 for (RegisterInfo* info : tempreg_info_) {
buzbee091cc402014-03-31 10:14:40 -070035 info->MarkFree();
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 }
37 // Reset temp tracking sanity check.
38 if (kIsDebugBuild) {
39 live_sreg_ = INVALID_SREG;
40 }
41}
42
Vladimir Marko8dea81c2014-06-06 14:50:36 +010043Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, const ResourceMask& mask)
buzbee30adc732014-05-09 15:10:18 -070044 : reg_(r), is_temp_(false), wide_value_(false), dirty_(false), aliased_(false), partner_(r),
buzbeeba574512014-05-12 15:13:16 -070045 s_reg_(INVALID_SREG), def_use_mask_(mask), master_(this), def_start_(nullptr),
46 def_end_(nullptr), alias_chain_(nullptr) {
buzbee091cc402014-03-31 10:14:40 -070047 switch (r.StorageSize()) {
48 case 0: storage_mask_ = 0xffffffff; break;
49 case 4: storage_mask_ = 0x00000001; break;
50 case 8: storage_mask_ = 0x00000003; break;
51 case 16: storage_mask_ = 0x0000000f; break;
52 case 32: storage_mask_ = 0x000000ff; break;
53 case 64: storage_mask_ = 0x0000ffff; break;
54 case 128: storage_mask_ = 0xffffffff; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 }
buzbee091cc402014-03-31 10:14:40 -070056 used_storage_ = r.Valid() ? ~storage_mask_ : storage_mask_;
buzbee30adc732014-05-09 15:10:18 -070057 liveness_ = used_storage_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070058}
59
buzbee091cc402014-03-31 10:14:40 -070060Mir2Lir::RegisterPool::RegisterPool(Mir2Lir* m2l, ArenaAllocator* arena,
Vladimir Marko089142c2014-06-05 10:57:05 +010061 const ArrayRef<const RegStorage>& core_regs,
62 const ArrayRef<const RegStorage>& core64_regs,
63 const ArrayRef<const RegStorage>& sp_regs,
64 const ArrayRef<const RegStorage>& dp_regs,
65 const ArrayRef<const RegStorage>& reserved_regs,
66 const ArrayRef<const RegStorage>& reserved64_regs,
67 const ArrayRef<const RegStorage>& core_temps,
68 const ArrayRef<const RegStorage>& core64_temps,
69 const ArrayRef<const RegStorage>& sp_temps,
70 const ArrayRef<const RegStorage>& dp_temps) :
Vladimir Markoe39c54e2014-09-22 14:50:02 +010071 core_regs_(arena->Adapter()), next_core_reg_(0),
72 core64_regs_(arena->Adapter()), next_core64_reg_(0),
73 sp_regs_(arena->Adapter()), next_sp_reg_(0),
74 dp_regs_(arena->Adapter()), next_dp_reg_(0), m2l_(m2l) {
buzbee091cc402014-03-31 10:14:40 -070075 // Initialize the fast lookup map.
Vladimir Markoe39c54e2014-09-22 14:50:02 +010076 m2l_->reginfo_map_.clear();
77 m2l_->reginfo_map_.resize(RegStorage::kMaxRegs, nullptr);
buzbee091cc402014-03-31 10:14:40 -070078
79 // Construct the register pool.
Vladimir Markoe39c54e2014-09-22 14:50:02 +010080 core_regs_.reserve(core_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010081 for (const RegStorage& reg : core_regs) {
buzbee091cc402014-03-31 10:14:40 -070082 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010083 m2l_->reginfo_map_[reg.GetReg()] = info;
84 core_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -070085 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010086 core64_regs_.reserve(core64_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010087 for (const RegStorage& reg : core64_regs) {
buzbeeb01bf152014-05-13 15:59:07 -070088 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010089 m2l_->reginfo_map_[reg.GetReg()] = info;
90 core64_regs_.push_back(info);
buzbeeb01bf152014-05-13 15:59:07 -070091 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010092 sp_regs_.reserve(sp_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010093 for (const RegStorage& reg : sp_regs) {
buzbee091cc402014-03-31 10:14:40 -070094 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010095 m2l_->reginfo_map_[reg.GetReg()] = info;
96 sp_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -070097 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010098 dp_regs_.reserve(dp_regs.size());
Vladimir Marko8dea81c2014-06-06 14:50:36 +010099 for (const RegStorage& reg : dp_regs) {
buzbee091cc402014-03-31 10:14:40 -0700100 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100101 m2l_->reginfo_map_[reg.GetReg()] = info;
102 dp_regs_.push_back(info);
buzbee091cc402014-03-31 10:14:40 -0700103 }
104
105 // Keep special registers from being allocated.
106 for (RegStorage reg : reserved_regs) {
107 m2l_->MarkInUse(reg);
108 }
buzbeeb01bf152014-05-13 15:59:07 -0700109 for (RegStorage reg : reserved64_regs) {
110 m2l_->MarkInUse(reg);
111 }
buzbee091cc402014-03-31 10:14:40 -0700112
113 // Mark temp regs - all others not in use can be used for promotion
114 for (RegStorage reg : core_temps) {
115 m2l_->MarkTemp(reg);
116 }
buzbeeb01bf152014-05-13 15:59:07 -0700117 for (RegStorage reg : core64_temps) {
118 m2l_->MarkTemp(reg);
119 }
buzbee091cc402014-03-31 10:14:40 -0700120 for (RegStorage reg : sp_temps) {
121 m2l_->MarkTemp(reg);
122 }
123 for (RegStorage reg : dp_temps) {
124 m2l_->MarkTemp(reg);
125 }
126
127 // Add an entry for InvalidReg with zero'd mask.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100128 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), kEncodeNone);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100129 m2l_->reginfo_map_[RegStorage::InvalidReg().GetReg()] = invalid_reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700130
131 // Existence of core64 registers implies wide references.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100132 if (core64_regs_.size() != 0) {
buzbeea0cd2d72014-06-01 09:33:49 -0700133 ref_regs_ = &core64_regs_;
134 next_ref_reg_ = &next_core64_reg_;
135 } else {
136 ref_regs_ = &core_regs_;
137 next_ref_reg_ = &next_core_reg_;
138 }
buzbee091cc402014-03-31 10:14:40 -0700139}
140
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100141void Mir2Lir::DumpRegPool(ArenaVector<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 LOG(INFO) << "================================================";
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100143 for (RegisterInfo* info : *regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700145 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
146 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
147 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
148 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 }
150 LOG(INFO) << "================================================";
151}
152
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700153void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700154 DumpRegPool(&reg_pool_->core_regs_);
buzbeea0cd2d72014-06-01 09:33:49 -0700155 DumpRegPool(&reg_pool_->core64_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156}
157
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700158void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700159 DumpRegPool(&reg_pool_->sp_regs_);
160 DumpRegPool(&reg_pool_->dp_regs_);
161}
162
163void Mir2Lir::DumpRegPools() {
164 LOG(INFO) << "Core registers";
165 DumpCoreRegPool();
166 LOG(INFO) << "FP registers";
167 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168}
169
buzbee2700f7e2014-03-07 09:46:20 -0800170void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700171 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700172 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700173 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700174 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700175 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800176 } else {
buzbee30adc732014-05-09 15:10:18 -0700177 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700178 if (info->IsTemp() && !info->IsDead()) {
buzbeeb5860fb2014-06-21 15:31:01 -0700179 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700180 ClobberBody(GetRegInfo(info->Partner()));
181 }
buzbeeba574512014-05-12 15:13:16 -0700182 ClobberBody(info);
183 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700184 ClobberAliases(info, info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700185 } else {
186 RegisterInfo* master = info->Master();
187 if (info != master) {
188 ClobberBody(info->Master());
buzbee642fe342014-05-23 16:04:08 -0700189 ClobberAliases(info->Master(), info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700190 }
191 }
buzbee30adc732014-05-09 15:10:18 -0700192 }
buzbee2700f7e2014-03-07 09:46:20 -0800193 }
194}
195
buzbee642fe342014-05-23 16:04:08 -0700196void Mir2Lir::ClobberAliases(RegisterInfo* info, uint32_t clobber_mask) {
buzbeeba574512014-05-12 15:13:16 -0700197 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
198 alias = alias->GetAliasChain()) {
199 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee642fe342014-05-23 16:04:08 -0700200 // Only clobber if we have overlap.
201 if ((alias->StorageMask() & clobber_mask) != 0) {
202 ClobberBody(alias);
203 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 }
205}
206
207/*
208 * Break the association between a Dalvik vreg and a physical temp register of either register
209 * class.
210 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
211 * in the register utilities, is is also used by code gen routines to work around a deficiency in
212 * local register allocation, which fails to distinguish between the "in" and "out" identities
213 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
214 * is used both as the source and destination register of an operation in which the type
215 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
216 * addressed.
217 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700218void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700219 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700220 if (kIsDebugBuild && s_reg == live_sreg_) {
221 live_sreg_ = INVALID_SREG;
222 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100223 for (RegisterInfo* info : tempreg_info_) {
buzbee30adc732014-05-09 15:10:18 -0700224 if (info->SReg() == s_reg) {
buzbeeb5860fb2014-06-21 15:31:01 -0700225 if (info->GetReg().NotExactlyEquals(info->Partner())) {
buzbee082833c2014-05-17 23:16:26 -0700226 // Dealing with a pair - clobber the other half.
227 DCHECK(!info->IsAliased());
228 ClobberBody(GetRegInfo(info->Partner()));
229 }
buzbeeba574512014-05-12 15:13:16 -0700230 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700231 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700232 ClobberAliases(info, info->StorageMask());
buzbee30adc732014-05-09 15:10:18 -0700233 }
buzbee091cc402014-03-31 10:14:40 -0700234 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 }
236 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237}
238
239/*
240 * SSA names associated with the initial definitions of Dalvik
241 * registers are the same as the Dalvik register number (and
242 * thus take the same position in the promotion_map. However,
243 * the special Method* and compiler temp resisters use negative
244 * v_reg numbers to distinguish them and can have an arbitrary
245 * ssa name (above the last original Dalvik register). This function
246 * maps SSA names to positions in the promotion_map array.
247 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700248int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
250 DCHECK_GE(s_reg, 0);
251 int v_reg = mir_graph_->SRegToVReg(s_reg);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700252 return v_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253}
254
buzbee091cc402014-03-31 10:14:40 -0700255// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800256void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 int p_map_idx = SRegToPMap(s_reg);
258 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700259 int reg_num = reg.GetRegNum();
260 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800261 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800263 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 num_core_spills_++;
265 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800266 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267}
268
buzbee091cc402014-03-31 10:14:40 -0700269/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800270RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
271 RegStorage res;
buzbeeb5860fb2014-06-21 15:31:01 -0700272 /*
273 * Note: it really doesn't matter much whether we allocate from the core or core64
274 * pool for 64-bit targets - but for some targets it does matter whether allocations
275 * happens from the single or double pool. This entire section of code could stand
276 * a good refactoring.
277 */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100278 for (RegisterInfo* info : reg_pool_->core_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700279 if (!info->IsTemp() && !info->InUse()) {
280 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 RecordCorePromotion(res, s_reg);
282 break;
283 }
284 }
285 return res;
286}
287
buzbeeb5860fb2014-06-21 15:31:01 -0700288void Mir2Lir::RecordFpPromotion(RegStorage reg, int s_reg) {
289 DCHECK_NE(cu_->instruction_set, kThumb2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 int p_map_idx = SRegToPMap(s_reg);
291 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700292 int reg_num = reg.GetRegNum();
buzbee091cc402014-03-31 10:14:40 -0700293 GetRegInfo(reg)->MarkInUse();
buzbeeb5860fb2014-06-21 15:31:01 -0700294 fp_spill_mask_ |= (1 << reg_num);
295 // Include reg for later sort
296 fp_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
297 num_fp_spills_++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbeeb5860fb2014-06-21 15:31:01 -0700299 promotion_map_[p_map_idx].fp_reg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300}
301
buzbeeb5860fb2014-06-21 15:31:01 -0700302// Reserve a callee-save floating point.
303RegStorage Mir2Lir::AllocPreservedFpReg(int s_reg) {
304 /*
305 * For targets other than Thumb2, it doesn't matter whether we allocate from
306 * the sp_regs_ or dp_regs_ pool. Some refactoring is in order here.
307 */
308 DCHECK_NE(cu_->instruction_set, kThumb2);
buzbee2700f7e2014-03-07 09:46:20 -0800309 RegStorage res;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100310 for (RegisterInfo* info : reg_pool_->sp_regs_) {
buzbee091cc402014-03-31 10:14:40 -0700311 if (!info->IsTemp() && !info->InUse()) {
312 res = info->GetReg();
buzbeeb5860fb2014-06-21 15:31:01 -0700313 RecordFpPromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 break;
315 }
316 }
317 return res;
318}
319
buzbeeb5860fb2014-06-21 15:31:01 -0700320// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
buzbee2700f7e2014-03-07 09:46:20 -0800321RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700322 UNUSED(s_reg);
buzbeeb5860fb2014-06-21 15:31:01 -0700323 UNIMPLEMENTED(FATAL) << "Unexpected use of AllocPreservedDouble";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700324 UNREACHABLE();
buzbeeb5860fb2014-06-21 15:31:01 -0700325}
326
327// TODO: this is Thumb2 only. Remove when DoPromotion refactored.
328RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700329 UNUSED(s_reg);
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);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700936 DCHECK(partner != NULL);
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
1131/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001132void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1134 RegLocation loc = mir_graph_->reg_location_[i];
1135 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1136 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001137 int use_count = mir_graph_->GetUseCount(i);
buzbeec729a6b2013-09-14 16:04:31 -07001138 if (loc.fp) {
1139 if (loc.wide) {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001140 if (WideFPRsAreAliases()) {
1141 // Floats and doubles can be counted together.
1142 counts[p_map_idx].count += use_count;
1143 } else {
1144 // Treat doubles as a unit, using upper half of fp_counts array.
1145 counts[p_map_idx + num_regs].count += use_count;
1146 }
buzbeec729a6b2013-09-14 16:04:31 -07001147 i++;
1148 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001149 counts[p_map_idx].count += use_count;
buzbeec729a6b2013-09-14 16:04:31 -07001150 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001151 } else {
Serguei Katkov59a42af2014-07-05 00:55:46 +07001152 if (loc.wide && WideGPRsAreAliases()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001153 i++;
buzbeeb5860fb2014-06-21 15:31:01 -07001154 }
Matteo Franchinc763e352014-07-04 12:53:27 +01001155 if (!IsInexpensiveConstant(loc)) {
1156 counts[p_map_idx].count += use_count;
1157 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 }
1160}
1161
1162/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001163static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1165 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Matteo Franchinc763e352014-07-04 12:53:27 +01001166 // Note that we fall back to sorting on reg so we get stable output on differing qsort
1167 // implementations (such as on host and target or between local host and build servers).
1168 // Note also that if a wide val1 and a non-wide val2 have the same count, then val1 always
1169 // ``loses'' (as STARTING_WIDE_SREG is or-ed in val1->s_reg).
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001170 return (op1->count == op2->count)
1171 ? (op1->s_reg - op2->s_reg)
1172 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173}
1174
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001175void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 LOG(INFO) << msg;
1177 for (int i = 0; i < size; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001178 if ((arr[i].s_reg & STARTING_WIDE_SREG) != 0) {
1179 LOG(INFO) << "s_reg[64_" << (arr[i].s_reg & ~STARTING_WIDE_SREG) << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001180 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001181 LOG(INFO) << "s_reg[32_" << arr[i].s_reg << "]: " << arr[i].count;
buzbeec729a6b2013-09-14 16:04:31 -07001182 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 }
1184}
1185
1186/*
1187 * Note: some portions of this code required even if the kPromoteRegs
1188 * optimization is disabled.
1189 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001190void Mir2Lir::DoPromotion() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001191 int num_regs = mir_graph_->GetNumOfCodeAndTempVRs();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001193 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +00001194 promotion_map_ = arena_->AllocArray<PromotionMap>(num_regs, kArenaAllocRegAlloc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195
1196 // Allow target code to add any special registers
1197 AdjustSpillMask();
1198
1199 /*
1200 * Simple register promotion. Just do a static count of the uses
1201 * of Dalvik registers. Note that we examine the SSA names, but
1202 * count based on original Dalvik register name. Count refs
1203 * separately based on type in order to give allocation
1204 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001205 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001206 * reg.
1207 * TUNING: replace with linear scan once we have the ability
1208 * to describe register live ranges for GC.
1209 */
Matteo Franchinc763e352014-07-04 12:53:27 +01001210 size_t core_reg_count_size = WideGPRsAreAliases() ? num_regs : num_regs * 2;
1211 size_t fp_reg_count_size = WideFPRsAreAliases() ? num_regs : num_regs * 2;
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +00001212 RefCounts *core_regs = arena_->AllocArray<RefCounts>(core_reg_count_size, kArenaAllocRegAlloc);
1213 RefCounts *fp_regs = arena_->AllocArray<RefCounts>(fp_reg_count_size, kArenaAllocRegAlloc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 // Set ssa names for original Dalvik registers
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001215 for (int i = 0; i < num_regs; i++) {
buzbeeb5860fb2014-06-21 15:31:01 -07001216 core_regs[i].s_reg = fp_regs[i].s_reg = i;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001218
buzbeeb5860fb2014-06-21 15:31:01 -07001219 // Duplicate in upper half to represent possible wide starting sregs.
1220 for (size_t i = num_regs; i < fp_reg_count_size; i++) {
1221 fp_regs[i].s_reg = fp_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
1222 }
1223 for (size_t i = num_regs; i < core_reg_count_size; i++) {
1224 core_regs[i].s_reg = core_regs[i - num_regs].s_reg | STARTING_WIDE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 }
1226
1227 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeeb5860fb2014-06-21 15:31:01 -07001228 CountRefs(core_regs, fp_regs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229
Brian Carlstrom7940e442013-07-12 13:46:57 -07001230 // Sort the count arrays
buzbeeb5860fb2014-06-21 15:31:01 -07001231 qsort(core_regs, core_reg_count_size, sizeof(RefCounts), SortCounts);
1232 qsort(fp_regs, fp_reg_count_size, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233
1234 if (cu_->verbose) {
buzbeeb5860fb2014-06-21 15:31:01 -07001235 DumpCounts(core_regs, core_reg_count_size, "Core regs after sort");
1236 DumpCounts(fp_regs, fp_reg_count_size, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 }
1238
1239 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
buzbeeb5860fb2014-06-21 15:31:01 -07001240 // Promote fp regs
1241 for (size_t i = 0; (i < fp_reg_count_size) && (fp_regs[i].count >= promotion_threshold); i++) {
1242 int low_sreg = fp_regs[i].s_reg & ~STARTING_WIDE_SREG;
1243 size_t p_map_idx = SRegToPMap(low_sreg);
1244 RegStorage reg = RegStorage::InvalidReg();
1245 if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
1246 // TODO: break out the Thumb2-specific code.
1247 if (cu_->instruction_set == kThumb2) {
1248 bool wide = fp_regs[i].s_reg & STARTING_WIDE_SREG;
1249 if (wide) {
Andreas Gampe01758d52014-07-08 21:10:55 -07001250 if (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg) {
buzbeeb5860fb2014-06-21 15:31:01 -07001251 // Ignore result - if can't alloc double may still be able to alloc singles.
1252 AllocPreservedDouble(low_sreg);
1253 }
1254 // Continue regardless of success - might still be able to grab a single.
1255 continue;
1256 } else {
1257 reg = AllocPreservedSingle(low_sreg);
1258 }
1259 } else {
1260 reg = AllocPreservedFpReg(low_sreg);
buzbeec729a6b2013-09-14 16:04:31 -07001261 }
buzbee2700f7e2014-03-07 09:46:20 -08001262 if (!reg.Valid()) {
buzbeeb5860fb2014-06-21 15:31:01 -07001263 break; // No more left
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 }
1265 }
1266 }
1267
1268 // Promote core regs
buzbeeb5860fb2014-06-21 15:31:01 -07001269 for (size_t i = 0; (i < core_reg_count_size) &&
1270 (core_regs[i].count >= promotion_threshold); i++) {
1271 int low_sreg = core_regs[i].s_reg & ~STARTING_WIDE_SREG;
1272 size_t p_map_idx = SRegToPMap(low_sreg);
1273 if (promotion_map_[p_map_idx].core_location != kLocPhysReg) {
1274 RegStorage reg = AllocPreservedCoreReg(low_sreg);
buzbee2700f7e2014-03-07 09:46:20 -08001275 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 break; // No more left
1277 }
1278 }
1279 }
1280 }
1281
1282 // Now, update SSA names to new home locations
1283 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1284 RegLocation *curr = &mir_graph_->reg_location_[i];
1285 int p_map_idx = SRegToPMap(curr->s_reg_low);
buzbeeb5860fb2014-06-21 15:31:01 -07001286 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 -07001287 bool wide = curr->wide || (cu_->target64 && curr->ref);
buzbeeb5860fb2014-06-21 15:31:01 -07001288 RegStorage reg = RegStorage::InvalidReg();
1289 if (curr->fp && promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1290 if (wide && cu_->instruction_set == kThumb2) {
1291 if (promotion_map_[p_map_idx + 1].fp_location == kLocPhysReg) {
1292 int high_reg = promotion_map_[p_map_idx+1].fp_reg;
buzbee091cc402014-03-31 10:14:40 -07001293 // TODO: move target-specific restrictions out of here.
buzbeeb5860fb2014-06-21 15:31:01 -07001294 if (((reg_num & 0x1) == 0) && ((reg_num + 1) == high_reg)) {
1295 reg = RegStorage::FloatSolo64(RegStorage::RegNum(reg_num) >> 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 }
1297 }
1298 } else {
buzbeeb5860fb2014-06-21 15:31:01 -07001299 reg = wide ? RegStorage::FloatSolo64(reg_num) : RegStorage::FloatSolo32(reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001300 }
buzbeeb5860fb2014-06-21 15:31:01 -07001301 } else if (!curr->fp && promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1302 if (wide && !cu_->target64) {
1303 if (promotion_map_[p_map_idx + 1].core_location == kLocPhysReg) {
1304 int high_reg = promotion_map_[p_map_idx+1].core_reg;
1305 reg = RegStorage(RegStorage::k64BitPair, reg_num, high_reg);
1306 }
1307 } else {
1308 reg = wide ? RegStorage::Solo64(reg_num) : RegStorage::Solo32(reg_num);
1309 }
1310 }
1311 if (reg.Valid()) {
1312 curr->reg = reg;
1313 curr->location = kLocPhysReg;
1314 curr->home = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001315 }
1316 }
1317 if (cu_->verbose) {
1318 DumpPromotionMap();
1319 }
1320}
1321
1322/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001323int Mir2Lir::VRegOffset(int v_reg) {
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001324 const DexFile::CodeItem* code_item = mir_graph_->GetCurrentDexCompilationUnit()->GetCodeItem();
1325 return StackVisitor::GetVRegOffset(code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001326 fp_spill_mask_, frame_size_, v_reg,
1327 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328}
1329
1330/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001331int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1333}
1334
1335/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001336RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1337 RegLocation res;
1338 switch (reg_class) {
1339 case kRefReg: LOG(FATAL); break;
1340 case kFPReg: res = LocCReturnDouble(); break;
1341 default: res = LocCReturnWide(); break;
1342 }
buzbee082833c2014-05-17 23:16:26 -07001343 Clobber(res.reg);
1344 LockTemp(res.reg);
1345 MarkWide(res.reg);
Andreas Gampe4b537a82014-06-30 22:24:53 -07001346 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 return res;
1348}
1349
buzbeea0cd2d72014-06-01 09:33:49 -07001350RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1351 RegLocation res;
1352 switch (reg_class) {
1353 case kRefReg: res = LocCReturnRef(); break;
1354 case kFPReg: res = LocCReturnFloat(); break;
1355 default: res = LocCReturn(); break;
1356 }
buzbee091cc402014-03-31 10:14:40 -07001357 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001359 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 } else {
buzbee091cc402014-03-31 10:14:40 -07001361 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001362 }
Andreas Gampe4b537a82014-06-30 22:24:53 -07001363 CheckRegLocation(res);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001364 return res;
1365}
1366
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001367void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001368 DoPromotion();
1369
1370 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1371 LOG(INFO) << "After Promotion";
1372 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1373 }
1374
1375 /* Set the frame size */
1376 frame_size_ = ComputeFrameSize();
1377}
1378
1379/*
1380 * Get the "real" sreg number associated with an s_reg slot. In general,
1381 * s_reg values passed through codegen are the SSA names created by
1382 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1383 * array. However, renaming is accomplished by simply replacing RegLocation
1384 * entries in the reglocation[] array. Therefore, when location
1385 * records for operands are first created, we need to ask the locRecord
1386 * identified by the dataflow pass what it's new name is.
1387 */
1388int Mir2Lir::GetSRegHi(int lowSreg) {
1389 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1390}
1391
buzbee091cc402014-03-31 10:14:40 -07001392bool Mir2Lir::LiveOut(int s_reg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001393 UNUSED(s_reg);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001394 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001395 return true;
1396}
1397
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398} // namespace art