blob: 59ae16ed36cc6dba9ff28c10ef7661659adee552 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains register alloction support. */
18
19#include "dex/compiler_ir.h"
20#include "dex/compiler_internals.h"
21#include "mir_to_lir-inl.h"
22
23namespace art {
24
25/*
26 * Free all allocated temps in the temp pools. Note that this does
27 * not affect the "liveness" of a temp register, which will stay
28 * live until it is either explicitly killed or reallocated.
29 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030void Mir2Lir::ResetRegPool() {
buzbeebd663de2013-09-10 15:41:31 -070031 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
32 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee091cc402014-03-31 10:14:40 -070033 info->MarkFree();
Brian Carlstrom7940e442013-07-12 13:46:57 -070034 }
35 // Reset temp tracking sanity check.
36 if (kIsDebugBuild) {
37 live_sreg_ = INVALID_SREG;
38 }
39}
40
buzbee091cc402014-03-31 10:14:40 -070041Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, uint64_t mask)
buzbee30adc732014-05-09 15:10:18 -070042 : reg_(r), is_temp_(false), wide_value_(false), dirty_(false), aliased_(false), partner_(r),
buzbeeba574512014-05-12 15:13:16 -070043 s_reg_(INVALID_SREG), def_use_mask_(mask), master_(this), def_start_(nullptr),
44 def_end_(nullptr), alias_chain_(nullptr) {
buzbee091cc402014-03-31 10:14:40 -070045 switch (r.StorageSize()) {
46 case 0: storage_mask_ = 0xffffffff; break;
47 case 4: storage_mask_ = 0x00000001; break;
48 case 8: storage_mask_ = 0x00000003; break;
49 case 16: storage_mask_ = 0x0000000f; break;
50 case 32: storage_mask_ = 0x000000ff; break;
51 case 64: storage_mask_ = 0x0000ffff; break;
52 case 128: storage_mask_ = 0xffffffff; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 }
buzbee091cc402014-03-31 10:14:40 -070054 used_storage_ = r.Valid() ? ~storage_mask_ : storage_mask_;
buzbee30adc732014-05-09 15:10:18 -070055 liveness_ = used_storage_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070056}
57
buzbee091cc402014-03-31 10:14:40 -070058Mir2Lir::RegisterPool::RegisterPool(Mir2Lir* m2l, ArenaAllocator* arena,
59 const std::vector<RegStorage>& core_regs,
buzbeeb01bf152014-05-13 15:59:07 -070060 const std::vector<RegStorage>& core64_regs,
buzbee091cc402014-03-31 10:14:40 -070061 const std::vector<RegStorage>& sp_regs,
62 const std::vector<RegStorage>& dp_regs,
63 const std::vector<RegStorage>& reserved_regs,
buzbeeb01bf152014-05-13 15:59:07 -070064 const std::vector<RegStorage>& reserved64_regs,
buzbee091cc402014-03-31 10:14:40 -070065 const std::vector<RegStorage>& core_temps,
buzbeeb01bf152014-05-13 15:59:07 -070066 const std::vector<RegStorage>& core64_temps,
buzbee091cc402014-03-31 10:14:40 -070067 const std::vector<RegStorage>& sp_temps,
68 const std::vector<RegStorage>& dp_temps) :
buzbeeb01bf152014-05-13 15:59:07 -070069 core_regs_(arena, core_regs.size()), next_core_reg_(0),
70 core64_regs_(arena, core64_regs.size()), next_core64_reg_(0),
71 sp_regs_(arena, sp_regs.size()), next_sp_reg_(0),
72 dp_regs_(arena, dp_regs.size()), next_dp_reg_(0), m2l_(m2l) {
buzbee091cc402014-03-31 10:14:40 -070073 // Initialize the fast lookup map.
74 m2l_->reginfo_map_.Reset();
buzbeeba574512014-05-12 15:13:16 -070075 if (kIsDebugBuild) {
76 m2l_->reginfo_map_.Resize(RegStorage::kMaxRegs);
77 for (unsigned i = 0; i < RegStorage::kMaxRegs; i++) {
78 m2l_->reginfo_map_.Insert(nullptr);
79 }
80 } else {
81 m2l_->reginfo_map_.SetSize(RegStorage::kMaxRegs);
buzbee091cc402014-03-31 10:14:40 -070082 }
83
84 // Construct the register pool.
85 for (RegStorage reg : core_regs) {
86 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
87 m2l_->reginfo_map_.Put(reg.GetReg(), info);
88 core_regs_.Insert(info);
89 }
buzbeeb01bf152014-05-13 15:59:07 -070090 for (RegStorage reg : core64_regs) {
91 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
92 m2l_->reginfo_map_.Put(reg.GetReg(), info);
93 core64_regs_.Insert(info);
94 }
buzbee091cc402014-03-31 10:14:40 -070095 for (RegStorage reg : sp_regs) {
96 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
97 m2l_->reginfo_map_.Put(reg.GetReg(), info);
98 sp_regs_.Insert(info);
99 }
100 for (RegStorage reg : dp_regs) {
101 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
102 m2l_->reginfo_map_.Put(reg.GetReg(), info);
103 dp_regs_.Insert(info);
104 }
105
106 // Keep special registers from being allocated.
107 for (RegStorage reg : reserved_regs) {
108 m2l_->MarkInUse(reg);
109 }
buzbeeb01bf152014-05-13 15:59:07 -0700110 for (RegStorage reg : reserved64_regs) {
111 m2l_->MarkInUse(reg);
112 }
buzbee091cc402014-03-31 10:14:40 -0700113
114 // Mark temp regs - all others not in use can be used for promotion
115 for (RegStorage reg : core_temps) {
116 m2l_->MarkTemp(reg);
117 }
buzbeeb01bf152014-05-13 15:59:07 -0700118 for (RegStorage reg : core64_temps) {
119 m2l_->MarkTemp(reg);
120 }
buzbee091cc402014-03-31 10:14:40 -0700121 for (RegStorage reg : sp_temps) {
122 m2l_->MarkTemp(reg);
123 }
124 for (RegStorage reg : dp_temps) {
125 m2l_->MarkTemp(reg);
126 }
127
128 // Add an entry for InvalidReg with zero'd mask.
129 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), 0);
130 m2l_->reginfo_map_.Put(RegStorage::InvalidReg().GetReg(), invalid_reg);
131}
132
133void Mir2Lir::DumpRegPool(GrowableArray<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 LOG(INFO) << "================================================";
buzbee091cc402014-03-31 10:14:40 -0700135 GrowableArray<RegisterInfo*>::Iterator it(regs);
136 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700138 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
139 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
140 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
141 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 }
143 LOG(INFO) << "================================================";
144}
145
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700147 DumpRegPool(&reg_pool_->core_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148}
149
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700150void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700151 DumpRegPool(&reg_pool_->sp_regs_);
152 DumpRegPool(&reg_pool_->dp_regs_);
153}
154
155void Mir2Lir::DumpRegPools() {
156 LOG(INFO) << "Core registers";
157 DumpCoreRegPool();
158 LOG(INFO) << "FP registers";
159 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160}
161
buzbee2700f7e2014-03-07 09:46:20 -0800162void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700163 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700164 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700165 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700166 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700167 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800168 } else {
buzbee30adc732014-05-09 15:10:18 -0700169 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700170 if (info->IsTemp() && !info->IsDead()) {
buzbee082833c2014-05-17 23:16:26 -0700171 if (info->GetReg() != info->Partner()) {
172 ClobberBody(GetRegInfo(info->Partner()));
173 }
buzbeeba574512014-05-12 15:13:16 -0700174 ClobberBody(info);
175 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700176 ClobberAliases(info, info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700177 } else {
178 RegisterInfo* master = info->Master();
179 if (info != master) {
180 ClobberBody(info->Master());
buzbee642fe342014-05-23 16:04:08 -0700181 ClobberAliases(info->Master(), info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700182 }
183 }
buzbee30adc732014-05-09 15:10:18 -0700184 }
buzbee2700f7e2014-03-07 09:46:20 -0800185 }
186}
187
buzbee642fe342014-05-23 16:04:08 -0700188void Mir2Lir::ClobberAliases(RegisterInfo* info, uint32_t clobber_mask) {
buzbeeba574512014-05-12 15:13:16 -0700189 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
190 alias = alias->GetAliasChain()) {
191 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee642fe342014-05-23 16:04:08 -0700192 // Only clobber if we have overlap.
193 if ((alias->StorageMask() & clobber_mask) != 0) {
194 ClobberBody(alias);
195 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 }
197}
198
199/*
200 * Break the association between a Dalvik vreg and a physical temp register of either register
201 * class.
202 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
203 * in the register utilities, is is also used by code gen routines to work around a deficiency in
204 * local register allocation, which fails to distinguish between the "in" and "out" identities
205 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
206 * is used both as the source and destination register of an operation in which the type
207 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
208 * addressed.
209 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700210void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700211 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700212 if (kIsDebugBuild && s_reg == live_sreg_) {
213 live_sreg_ = INVALID_SREG;
214 }
215 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
216 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
217 if (info->SReg() == s_reg) {
buzbee082833c2014-05-17 23:16:26 -0700218 if (info->GetReg() != info->Partner()) {
219 // Dealing with a pair - clobber the other half.
220 DCHECK(!info->IsAliased());
221 ClobberBody(GetRegInfo(info->Partner()));
222 }
buzbeeba574512014-05-12 15:13:16 -0700223 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700224 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700225 ClobberAliases(info, info->StorageMask());
buzbee30adc732014-05-09 15:10:18 -0700226 }
buzbee091cc402014-03-31 10:14:40 -0700227 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 }
229 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230}
231
232/*
233 * SSA names associated with the initial definitions of Dalvik
234 * registers are the same as the Dalvik register number (and
235 * thus take the same position in the promotion_map. However,
236 * the special Method* and compiler temp resisters use negative
237 * v_reg numbers to distinguish them and can have an arbitrary
238 * ssa name (above the last original Dalvik register). This function
239 * maps SSA names to positions in the promotion_map array.
240 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700241int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
243 DCHECK_GE(s_reg, 0);
244 int v_reg = mir_graph_->SRegToVReg(s_reg);
245 if (v_reg >= 0) {
246 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
247 return v_reg;
248 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800249 /*
250 * It must be the case that the v_reg for temporary is less than or equal to the
251 * base reg for temps. For that reason, "position" must be zero or positive.
252 */
253 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
254
255 // The temporaries are placed after dalvik registers in the promotion map
256 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
257 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 }
259}
260
buzbee091cc402014-03-31 10:14:40 -0700261// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800262void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 int p_map_idx = SRegToPMap(s_reg);
264 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700265 int reg_num = reg.GetRegNum();
266 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800267 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800269 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 num_core_spills_++;
271 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800272 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273}
274
buzbee091cc402014-03-31 10:14:40 -0700275/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800276RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
277 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700278 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
279 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
280 if (!info->IsTemp() && !info->InUse()) {
281 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 RecordCorePromotion(res, s_reg);
283 break;
284 }
285 }
286 return res;
287}
288
buzbee091cc402014-03-31 10:14:40 -0700289void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 int p_map_idx = SRegToPMap(s_reg);
291 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700292 GetRegInfo(reg)->MarkInUse();
293 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700295 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296}
297
buzbee091cc402014-03-31 10:14:40 -0700298// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800299RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
300 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700301 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
302 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
303 if (!info->IsTemp() && !info->InUse()) {
304 res = info->GetReg();
305 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 break;
307 }
308 }
309 return res;
310}
311
buzbee091cc402014-03-31 10:14:40 -0700312void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
313 int p_map_idx = SRegToPMap(s_reg);
314 int v_reg = mir_graph_->SRegToVReg(s_reg);
315 GetRegInfo(reg)->MarkInUse();
316 MarkPreservedDouble(v_reg, reg);
317 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
318 promotion_map_[p_map_idx].FpReg = reg.GetReg();
319}
320
321// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800322RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
323 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700324 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
325 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
326 if (!info->IsTemp() && !info->InUse()) {
327 res = info->GetReg();
328 RecordDoublePromotion(res, s_reg);
329 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 }
332 return res;
333}
334
buzbee091cc402014-03-31 10:14:40 -0700335
336RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
337 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700339 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 if (next >= num_regs)
341 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700342 RegisterInfo* info = regs.Get(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()) {
buzbee091cc402014-03-31 10:14:40 -0700345 Clobber(info->GetReg());
346 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700347 /*
348 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
349 * The caller will set wideness as appropriate.
350 */
buzbee091cc402014-03-31 10:14:40 -0700351 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700353 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 }
355 next++;
356 }
357 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700358 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700359 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 if (next >= num_regs)
361 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700362 RegisterInfo* info = regs.Get(next);
363 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700364 // Got one. Kill it.
365 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700366 Clobber(info->GetReg());
367 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700368 if (info->IsWide()) {
369 RegisterInfo* partner = GetRegInfo(info->Partner());
370 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
371 DCHECK(partner->IsWide());
372 info->SetIsWide(false);
373 partner->SetIsWide(false);
374 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700376 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 }
378 next++;
379 }
380 if (required) {
381 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700382 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 LOG(FATAL) << "No free temp registers";
384 }
buzbee2700f7e2014-03-07 09:46:20 -0800385 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386}
387
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800389RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700390 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391}
392
buzbee2700f7e2014-03-07 09:46:20 -0800393RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700394 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395}
396
buzbeeb01bf152014-05-13 15:59:07 -0700397RegStorage Mir2Lir::AllocTempWide() {
398 RegStorage res;
399 if (reg_pool_->core64_regs_.Size() != 0) {
400 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, true);
401 } else {
402 RegStorage low_reg = AllocTemp();
403 RegStorage high_reg = AllocTemp();
404 res = RegStorage::MakeRegPair(low_reg, high_reg);
405 }
406 return res;
407}
408
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100409RegStorage Mir2Lir::AllocTempWord() {
410 return (Is64BitInstructionSet(cu_->instruction_set)) ? AllocTempWide() : AllocTemp();
411}
412
buzbee091cc402014-03-31 10:14:40 -0700413RegStorage Mir2Lir::AllocTempSingle() {
414 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
415 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
416 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417}
418
buzbee091cc402014-03-31 10:14:40 -0700419RegStorage Mir2Lir::AllocTempDouble() {
420 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
421 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
422 return res;
423}
424
buzbeeb01bf152014-05-13 15:59:07 -0700425RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
426 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
427 return AllocTempDouble();
428 }
429 return AllocTempWide();
430}
431
432RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
433 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
434 return AllocTempSingle();
435 }
436 return AllocTemp();
437}
438
buzbee091cc402014-03-31 10:14:40 -0700439RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
440 RegStorage res;
441 GrowableArray<RegisterInfo*>::Iterator it(&regs);
442 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
443 if ((info->SReg() == s_reg) && info->IsLive()) {
444 res = info->GetReg();
445 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 }
447 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 return res;
449}
450
buzbee091cc402014-03-31 10:14:40 -0700451RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
452 RegStorage reg;
453 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
454 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
455 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 }
buzbee091cc402014-03-31 10:14:40 -0700457 if (!reg.Valid() && (reg_class != kFPReg)) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100458 if (Is64BitInstructionSet(cu_->instruction_set)) {
459 reg = FindLiveReg(wide ? reg_pool_->core64_regs_ : reg_pool_->core_regs_, s_reg);
460 } else {
461 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
462 }
buzbee091cc402014-03-31 10:14:40 -0700463 }
464 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700465 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
466 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700467 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
468 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700469 reg = RegStorage::MakeRegPair(reg, high_reg);
470 MarkWide(reg);
471 } else {
buzbee30adc732014-05-09 15:10:18 -0700472 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700473 reg = RegStorage::InvalidReg();
474 }
475 }
buzbee30adc732014-05-09 15:10:18 -0700476 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
477 // Width mismatch - don't try to reuse.
478 reg = RegStorage::InvalidReg();
479 }
480 }
481 if (reg.Valid()) {
482 if (reg.IsPair()) {
483 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
484 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
485 if (info_low->IsTemp()) {
486 info_low->MarkInUse();
487 }
488 if (info_high->IsTemp()) {
489 info_high->MarkInUse();
490 }
491 } else {
buzbee091cc402014-03-31 10:14:40 -0700492 RegisterInfo* info = GetRegInfo(reg);
493 if (info->IsTemp()) {
494 info->MarkInUse();
495 }
496 }
buzbee30adc732014-05-09 15:10:18 -0700497 } else {
498 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
499 ClobberSReg(s_reg);
500 if (wide) {
501 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700502 }
503 }
504 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505}
506
buzbee2700f7e2014-03-07 09:46:20 -0800507void Mir2Lir::FreeTemp(RegStorage reg) {
508 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700509 FreeTemp(reg.GetLow());
510 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800511 } else {
buzbee091cc402014-03-31 10:14:40 -0700512 RegisterInfo* p = GetRegInfo(reg);
513 if (p->IsTemp()) {
514 p->MarkFree();
515 p->SetIsWide(false);
516 p->SetPartner(reg);
517 }
buzbee2700f7e2014-03-07 09:46:20 -0800518 }
519}
520
buzbee082833c2014-05-17 23:16:26 -0700521void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
522 DCHECK(rl_keep.wide);
523 DCHECK(rl_free.wide);
524 int free_low = rl_free.reg.GetLowReg();
525 int free_high = rl_free.reg.GetHighReg();
526 int keep_low = rl_keep.reg.GetLowReg();
527 int keep_high = rl_keep.reg.GetHighReg();
528 if ((free_low != keep_low) && (free_low != keep_high) &&
529 (free_high != keep_low) && (free_high != keep_high)) {
530 // No overlap, free both
531 FreeTemp(rl_free.reg);
532 }
533}
534
buzbee262b2992014-03-27 11:22:43 -0700535bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700536 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800537 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700538 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
539 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700540 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700541 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800542 } else {
buzbee091cc402014-03-31 10:14:40 -0700543 RegisterInfo* p = GetRegInfo(reg);
544 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800545 }
buzbee091cc402014-03-31 10:14:40 -0700546 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547}
548
buzbee262b2992014-03-27 11:22:43 -0700549bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700550 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800551 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700552 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
553 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
554 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800555 } else {
buzbee091cc402014-03-31 10:14:40 -0700556 RegisterInfo* p = GetRegInfo(reg);
557 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800558 }
buzbee091cc402014-03-31 10:14:40 -0700559 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560}
561
buzbee262b2992014-03-27 11:22:43 -0700562bool Mir2Lir::IsPromoted(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());
567 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800568 } else {
buzbee091cc402014-03-31 10:14:40 -0700569 RegisterInfo* p = GetRegInfo(reg);
570 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800571 }
buzbee091cc402014-03-31 10:14:40 -0700572 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573}
574
buzbee2700f7e2014-03-07 09:46:20 -0800575bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700576 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800577 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700578 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
579 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
580 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800581 } else {
buzbee091cc402014-03-31 10:14:40 -0700582 RegisterInfo* p = GetRegInfo(reg);
583 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800584 }
buzbee091cc402014-03-31 10:14:40 -0700585 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800586}
587
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588/*
589 * Similar to AllocTemp(), but forces the allocation of a specific
590 * register. No check is made to see if the register was previously
591 * allocated. Use with caution.
592 */
buzbee2700f7e2014-03-07 09:46:20 -0800593void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700594 DCHECK(IsTemp(reg));
595 if (reg.IsPair()) {
596 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
597 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
598 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700599 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700600 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700601 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700602 } else {
603 RegisterInfo* p = GetRegInfo(reg);
604 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700605 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700606 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607}
608
buzbee2700f7e2014-03-07 09:46:20 -0800609void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700610 if (reg.IsPair()) {
611 GetRegInfo(reg.GetLow())->ResetDefBody();
612 GetRegInfo(reg.GetHigh())->ResetDefBody();
613 } else {
614 GetRegInfo(reg)->ResetDefBody();
615 }
buzbee2700f7e2014-03-07 09:46:20 -0800616}
617
buzbee091cc402014-03-31 10:14:40 -0700618void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
619 RegisterInfo* info = nullptr;
620 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
621 if (IsTemp(rs)) {
622 info = GetRegInfo(reg);
623 }
624 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
625 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
626 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700628 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 break;
buzbee091cc402014-03-31 10:14:40 -0700630 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 }
632 }
633}
634
635/*
636 * Mark the beginning and end LIR of a def sequence. Note that
637 * on entry start points to the LIR prior to the beginning of the
638 * sequence.
639 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700640void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700641 DCHECK(!rl.wide);
642 DCHECK(start && start->next);
643 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700644 RegisterInfo* p = GetRegInfo(rl.reg);
645 p->SetDefStart(start->next);
646 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647}
648
649/*
650 * Mark the beginning and end LIR of a def sequence. Note that
651 * on entry start points to the LIR prior to the beginning of the
652 * sequence.
653 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700654void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 DCHECK(rl.wide);
656 DCHECK(start && start->next);
657 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700658 RegisterInfo* p;
659 if (rl.reg.IsPair()) {
660 p = GetRegInfo(rl.reg.GetLow());
661 ResetDef(rl.reg.GetHigh()); // Only track low of pair
662 } else {
663 p = GetRegInfo(rl.reg);
664 }
665 p->SetDefStart(start->next);
666 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667}
668
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700669RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 DCHECK(rl.wide);
671 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700672 if (rl.reg.IsPair()) {
673 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
674 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
675 if (info_lo->IsTemp()) {
676 info_lo->SetIsWide(false);
677 info_lo->ResetDefBody();
678 }
679 if (info_hi->IsTemp()) {
680 info_hi->SetIsWide(false);
681 info_hi->ResetDefBody();
682 }
683 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700684 } else {
685 /*
686 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
687 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
688 * this code. Will probably want to make this a virtual function.
689 */
690 // Can't narrow 64-bit register. Clobber.
691 if (GetRegInfo(rl.reg)->IsTemp()) {
692 Clobber(rl.reg);
693 FreeTemp(rl.reg);
694 }
695 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 }
698 rl.wide = false;
699 return rl;
700}
701
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700702void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700704 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
705 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 }
buzbee091cc402014-03-31 10:14:40 -0700707 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708}
709
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700710void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700712 // If pair, only track low reg of pair.
713 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
714 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
715 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 }
buzbee091cc402014-03-31 10:14:40 -0700717 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718}
719
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700720void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700721 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
722 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
723 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
buzbee091cc402014-03-31 10:14:40 -0700725 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
726 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
727 info->ResetDefBody();
728 }
729 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
730 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
731 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 }
733}
734
buzbeeba574512014-05-12 15:13:16 -0700735void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700736 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
737 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700738 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700739 }
740}
741
742void Mir2Lir::FlushRegWide(RegStorage reg) {
743 if (reg.IsPair()) {
744 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
745 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
746 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
747 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
748 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
749 if (!(info1->IsTemp() && info2->IsTemp())) {
750 /* Should not happen. If it does, there's a problem in eval_loc */
751 LOG(FATAL) << "Long half-temp, half-promoted";
752 }
753
754 info1->SetIsDirty(false);
755 info2->SetIsDirty(false);
756 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
757 info1 = info2;
758 }
759 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100760 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700761 }
762 } else {
763 RegisterInfo* info = GetRegInfo(reg);
764 if (info->IsLive() && info->IsDirty()) {
765 info->SetIsDirty(false);
766 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100767 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700768 }
769 }
770}
771
772void Mir2Lir::FlushReg(RegStorage reg) {
773 DCHECK(!reg.IsPair());
774 RegisterInfo* info = GetRegInfo(reg);
775 if (info->IsLive() && info->IsDirty()) {
776 info->SetIsDirty(false);
777 int v_reg = mir_graph_->SRegToVReg(info->SReg());
778 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 }
780}
781
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800782void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700783 if (info->IsWide()) {
784 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800785 } else {
buzbee091cc402014-03-31 10:14:40 -0700786 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 }
788}
789
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700790void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700791 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
792 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700793 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700794 FlushSpecificReg(info);
795 }
buzbee30adc732014-05-09 15:10:18 -0700796 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700797 info->SetIsWide(false);
798 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799}
800
801
buzbee2700f7e2014-03-07 09:46:20 -0800802bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 if (reg_class == kAnyReg) {
804 return true;
805 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700806 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 } else {
buzbee091cc402014-03-31 10:14:40 -0700808 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 }
810}
811
buzbee091cc402014-03-31 10:14:40 -0700812void Mir2Lir::MarkLive(RegLocation loc) {
813 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700814 if (!IsTemp(reg)) {
815 return;
816 }
buzbee091cc402014-03-31 10:14:40 -0700817 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700818 if (s_reg == INVALID_SREG) {
819 // Can't be live if no associated sreg.
820 if (reg.IsPair()) {
821 GetRegInfo(reg.GetLow())->MarkDead();
822 GetRegInfo(reg.GetHigh())->MarkDead();
823 } else {
824 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700825 }
buzbee082833c2014-05-17 23:16:26 -0700826 } else {
827 if (reg.IsPair()) {
828 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
829 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
830 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
831 (info_hi->SReg() == s_reg)) {
832 return; // Already live.
833 }
834 ClobberSReg(s_reg);
835 ClobberSReg(s_reg + 1);
836 info_lo->MarkLive(s_reg);
837 info_hi->MarkLive(s_reg + 1);
838 } else {
839 RegisterInfo* info = GetRegInfo(reg);
840 if (info->IsLive() && (info->SReg() == s_reg)) {
841 return; // Already live.
842 }
843 ClobberSReg(s_reg);
844 if (loc.wide) {
845 ClobberSReg(s_reg + 1);
846 }
847 info->MarkLive(s_reg);
848 }
849 if (loc.wide) {
850 MarkWide(reg);
851 } else {
852 MarkNarrow(reg);
853 }
buzbee091cc402014-03-31 10:14:40 -0700854 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855}
856
buzbee2700f7e2014-03-07 09:46:20 -0800857void Mir2Lir::MarkTemp(RegStorage reg) {
858 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700860 tempreg_info_.Insert(info);
861 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862}
863
buzbee2700f7e2014-03-07 09:46:20 -0800864void Mir2Lir::UnmarkTemp(RegStorage reg) {
865 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700866 RegisterInfo* info = GetRegInfo(reg);
867 tempreg_info_.Delete(info);
868 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800869}
870
buzbee091cc402014-03-31 10:14:40 -0700871void Mir2Lir::MarkWide(RegStorage reg) {
872 if (reg.IsPair()) {
873 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
874 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700875 // Unpair any old partners.
876 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
877 GetRegInfo(info_lo->Partner())->SetIsWide(false);
878 }
879 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
880 GetRegInfo(info_hi->Partner())->SetIsWide(false);
881 }
buzbee091cc402014-03-31 10:14:40 -0700882 info_lo->SetIsWide(true);
883 info_hi->SetIsWide(true);
884 info_lo->SetPartner(reg.GetHigh());
885 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800886 } else {
buzbee091cc402014-03-31 10:14:40 -0700887 RegisterInfo* info = GetRegInfo(reg);
888 info->SetIsWide(true);
889 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700890 }
891}
892
buzbee082833c2014-05-17 23:16:26 -0700893void Mir2Lir::MarkNarrow(RegStorage reg) {
894 DCHECK(!reg.IsPair());
895 RegisterInfo* info = GetRegInfo(reg);
896 info->SetIsWide(false);
897 info->SetPartner(reg);
898}
899
buzbee091cc402014-03-31 10:14:40 -0700900void Mir2Lir::MarkClean(RegLocation loc) {
901 if (loc.reg.IsPair()) {
902 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
903 info->SetIsDirty(false);
904 info = GetRegInfo(loc.reg.GetHigh());
905 info->SetIsDirty(false);
906 } else {
907 RegisterInfo* info = GetRegInfo(loc.reg);
908 info->SetIsDirty(false);
909 }
910}
911
912// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700913void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700914 if (loc.home) {
915 // If already home, can't be dirty
916 return;
917 }
buzbee091cc402014-03-31 10:14:40 -0700918 if (loc.reg.IsPair()) {
919 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
920 info->SetIsDirty(true);
921 info = GetRegInfo(loc.reg.GetHigh());
922 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800923 } else {
buzbee091cc402014-03-31 10:14:40 -0700924 RegisterInfo* info = GetRegInfo(loc.reg);
925 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 }
927}
928
buzbee2700f7e2014-03-07 09:46:20 -0800929void Mir2Lir::MarkInUse(RegStorage reg) {
930 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700931 GetRegInfo(reg.GetLow())->MarkInUse();
932 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800933 } else {
buzbee091cc402014-03-31 10:14:40 -0700934 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800935 }
936}
937
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700938bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700939 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700940 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700941 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
942 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700943 int my_sreg = info->SReg();
944 RegStorage partner_reg = info->Partner();
945 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700946 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700947 DCHECK(partner->IsWide());
948 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700949 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700950 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700951 if (my_sreg == INVALID_SREG) {
952 DCHECK_EQ(partner_sreg, INVALID_SREG);
953 } else {
954 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700955 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700956 }
957 }
buzbee082833c2014-05-17 23:16:26 -0700958 if (info->Master() != info) {
959 // Aliased.
960 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
961 // If I'm live, master should not be live, but should show liveness in alias set.
962 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
963 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700964 }
buzbee642fe342014-05-23 16:04:08 -0700965// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700966 }
967 if (info->IsAliased()) {
968 // Has child aliases.
969 DCHECK_EQ(info->Master(), info);
970 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
971 // Master live, no child should be dead - all should show liveness in set.
972 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
973 DCHECK(!p->IsDead());
974 DCHECK_EQ(p->SReg(), INVALID_SREG);
975 }
976 } else if (!info->IsDead()) {
977 // Master not live, one or more aliases must be.
978 bool live_alias = false;
979 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
980 live_alias |= p->IsLive();
981 }
982 DCHECK(live_alias);
983 }
984 }
985 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
986 // If not fully live, should have INVALID_SREG and def's should be null.
987 DCHECK(info->DefStart() == nullptr);
988 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700989 }
990 }
991 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992}
993
994/*
995 * Return an updated location record with current in-register status.
996 * If the value lives in live temps, reflect that fact. No code
997 * is generated. If the live value is part of an older pair,
998 * clobber both low and high.
999 * TUNING: clobbering both is a bit heavy-handed, but the alternative
1000 * is a bit complex when dealing with FP regs. Examine code to see
1001 * if it's worthwhile trying to be more clever here.
1002 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001003RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 DCHECK(!loc.wide);
1005 DCHECK(CheckCorePoolSanity());
1006 if (loc.location != kLocPhysReg) {
1007 DCHECK((loc.location == kLocDalvikFrame) ||
1008 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001009 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
1010 if (reg.Valid()) {
1011 bool match = true;
1012 RegisterInfo* info = GetRegInfo(reg);
1013 match &= !reg.IsPair();
1014 match &= !info->IsWide();
1015 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001017 loc.reg = reg;
1018 } else {
1019 Clobber(reg);
1020 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 }
1022 }
1023 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 return loc;
1025}
1026
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001027RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 DCHECK(loc.wide);
1029 DCHECK(CheckCorePoolSanity());
1030 if (loc.location != kLocPhysReg) {
1031 DCHECK((loc.location == kLocDalvikFrame) ||
1032 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001033 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1034 if (reg.Valid()) {
1035 bool match = true;
1036 if (reg.IsPair()) {
1037 // If we've got a register pair, make sure that it was last used as the same pair.
1038 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1039 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1040 match &= info_lo->IsWide();
1041 match &= info_hi->IsWide();
1042 match &= (info_lo->Partner() == info_hi->GetReg());
1043 match &= (info_hi->Partner() == info_lo->GetReg());
1044 } else {
1045 RegisterInfo* info = GetRegInfo(reg);
1046 match &= info->IsWide();
1047 match &= (info->GetReg() == info->Partner());
1048 }
1049 if (match) {
1050 loc.location = kLocPhysReg;
1051 loc.reg = reg;
1052 } else {
1053 Clobber(reg);
1054 FreeTemp(reg);
1055 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 }
1057 }
1058 return loc;
1059}
1060
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001062RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001063 if (loc.wide)
1064 return UpdateLocWide(loc);
1065 else
1066 return UpdateLoc(loc);
1067}
1068
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001069RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001070 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071
1072 loc = UpdateLocWide(loc);
1073
1074 /* If already in registers, we can assume proper form. Right reg class? */
1075 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001076 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001077 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001078 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001079 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001080 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001081 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001082 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001083 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001084 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 }
1086 return loc;
1087 }
1088
1089 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1090 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1091
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001092 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001093 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 if (update) {
1096 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001097 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 return loc;
1100}
1101
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001102RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001103 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001105 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106
1107 loc = UpdateLoc(loc);
1108
1109 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001110 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001111 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001112 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001113 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001114 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001115 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001116 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001117 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001118 }
1119 return loc;
1120 }
1121
1122 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1123
buzbee2700f7e2014-03-07 09:46:20 -08001124 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125
1126 if (update) {
1127 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001128 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 }
1130 return loc;
1131}
1132
1133/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001134void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1136 RegLocation loc = mir_graph_->reg_location_[i];
1137 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1138 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001139 if (loc.fp) {
1140 if (loc.wide) {
1141 // Treat doubles as a unit, using upper half of fp_counts array.
1142 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1143 i++;
1144 } else {
1145 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1146 }
1147 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1149 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 }
1151}
1152
1153/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001154static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1156 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001157 // Note that we fall back to sorting on reg so we get stable output
1158 // on differing qsort implementations (such as on host and target or
1159 // between local host and build servers).
1160 return (op1->count == op2->count)
1161 ? (op1->s_reg - op2->s_reg)
1162 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163}
1164
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001165void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001166 LOG(INFO) << msg;
1167 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001168 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1169 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1170 } else {
1171 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1172 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 }
1174}
1175
1176/*
1177 * Note: some portions of this code required even if the kPromoteRegs
1178 * optimization is disabled.
1179 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001180void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001182 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001184 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1185 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001186 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187
1188 // Allow target code to add any special registers
1189 AdjustSpillMask();
1190
1191 /*
1192 * Simple register promotion. Just do a static count of the uses
1193 * of Dalvik registers. Note that we examine the SSA names, but
1194 * count based on original Dalvik register name. Count refs
1195 * separately based on type in order to give allocation
1196 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001197 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 * reg.
1199 * TUNING: replace with linear scan once we have the ability
1200 * to describe register live ranges for GC.
1201 */
1202 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001203 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001204 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001206 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001207 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 // Set ssa names for original Dalvik registers
1209 for (int i = 0; i < dalvik_regs; i++) {
1210 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1211 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001212
1213 // Set ssa names for compiler temporaries
1214 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1215 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1216 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1217 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1218 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001219 }
1220
1221 // Duplicate in upper half to represent possible fp double starting sregs.
1222 for (int i = 0; i < num_regs; i++) {
1223 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224 }
1225
1226 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001227 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229
1230 // Sort the count arrays
1231 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001232 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233
1234 if (cu_->verbose) {
1235 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001236 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 }
1238
1239 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1240 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001241 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1242 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1243 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1244 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1245 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1246 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1247 // Ignore result - if can't alloc double may still be able to alloc singles.
1248 AllocPreservedDouble(low_sreg);
1249 }
1250 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001251 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1252 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001253 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001254 }
1255 }
1256 }
1257
1258 // Promote core regs
1259 for (int i = 0; (i < num_regs) &&
1260 (core_regs[i].count >= promotion_threshold); i++) {
1261 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1262 if (promotion_map_[p_map_idx].core_location !=
1263 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001264 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1265 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001266 break; // No more left
1267 }
1268 }
1269 }
1270 }
1271
1272 // Now, update SSA names to new home locations
1273 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1274 RegLocation *curr = &mir_graph_->reg_location_[i];
1275 int p_map_idx = SRegToPMap(curr->s_reg_low);
1276 if (!curr->wide) {
1277 if (curr->fp) {
1278 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1279 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001280 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 curr->home = true;
1282 }
1283 } else {
1284 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1285 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001286 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 curr->home = true;
1288 }
1289 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001290 } else {
1291 if (curr->high_word) {
1292 continue;
1293 }
1294 if (curr->fp) {
1295 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001296 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 int low_reg = promotion_map_[p_map_idx].FpReg;
1298 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1299 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001300 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001301 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1302 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001303 if (cu_->instruction_set == kThumb2) {
1304 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1305 } else {
1306 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1307 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 curr->home = true;
1309 }
1310 }
1311 } else {
1312 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1313 && (promotion_map_[p_map_idx+1].core_location ==
1314 kLocPhysReg)) {
1315 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001316 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1317 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318 curr->home = true;
1319 }
1320 }
1321 }
1322 }
1323 if (cu_->verbose) {
1324 DumpPromotionMap();
1325 }
1326}
1327
1328/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001329int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001330 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001331 fp_spill_mask_, frame_size_, v_reg,
1332 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333}
1334
1335/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001336int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1338}
1339
1340/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001341RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001342 RegLocation gpr_res = LocCReturnWide();
1343 RegLocation fpr_res = LocCReturnDouble();
1344 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee082833c2014-05-17 23:16:26 -07001345 Clobber(res.reg);
1346 LockTemp(res.reg);
1347 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 return res;
1349}
1350
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001351RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 RegLocation gpr_res = LocCReturn();
1353 RegLocation fpr_res = LocCReturnFloat();
1354 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001355 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001357 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001358 } else {
buzbee091cc402014-03-31 10:14:40 -07001359 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 }
1361 return res;
1362}
1363
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001364void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001365 DoPromotion();
1366
1367 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1368 LOG(INFO) << "After Promotion";
1369 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1370 }
1371
1372 /* Set the frame size */
1373 frame_size_ = ComputeFrameSize();
1374}
1375
1376/*
1377 * Get the "real" sreg number associated with an s_reg slot. In general,
1378 * s_reg values passed through codegen are the SSA names created by
1379 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1380 * array. However, renaming is accomplished by simply replacing RegLocation
1381 * entries in the reglocation[] array. Therefore, when location
1382 * records for operands are first created, we need to ask the locRecord
1383 * identified by the dataflow pass what it's new name is.
1384 */
1385int Mir2Lir::GetSRegHi(int lowSreg) {
1386 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1387}
1388
buzbee091cc402014-03-31 10:14:40 -07001389bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001390 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001391 return true;
1392}
1393
Brian Carlstrom7940e442013-07-12 13:46:57 -07001394} // namespace art