blob: e5ca460e880fc87090ba93bcba7ce67d6b4c7c6f [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
buzbee091cc402014-03-31 10:14:40 -0700409RegStorage Mir2Lir::AllocTempSingle() {
410 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
411 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
412 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413}
414
buzbee091cc402014-03-31 10:14:40 -0700415RegStorage Mir2Lir::AllocTempDouble() {
416 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
417 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
418 return res;
419}
420
buzbeeb01bf152014-05-13 15:59:07 -0700421RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
422 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
423 return AllocTempDouble();
424 }
425 return AllocTempWide();
426}
427
428RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
429 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
430 return AllocTempSingle();
431 }
432 return AllocTemp();
433}
434
buzbee091cc402014-03-31 10:14:40 -0700435RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
436 RegStorage res;
437 GrowableArray<RegisterInfo*>::Iterator it(&regs);
438 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
439 if ((info->SReg() == s_reg) && info->IsLive()) {
440 res = info->GetReg();
441 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 }
443 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 return res;
445}
446
buzbee091cc402014-03-31 10:14:40 -0700447RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
448 RegStorage reg;
449 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
450 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
451 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 }
buzbee091cc402014-03-31 10:14:40 -0700453 if (!reg.Valid() && (reg_class != kFPReg)) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100454 if (Is64BitInstructionSet(cu_->instruction_set)) {
455 reg = FindLiveReg(wide ? reg_pool_->core64_regs_ : reg_pool_->core_regs_, s_reg);
456 } else {
457 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
458 }
buzbee091cc402014-03-31 10:14:40 -0700459 }
460 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700461 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
462 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700463 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
464 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700465 reg = RegStorage::MakeRegPair(reg, high_reg);
466 MarkWide(reg);
467 } else {
buzbee30adc732014-05-09 15:10:18 -0700468 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700469 reg = RegStorage::InvalidReg();
470 }
471 }
buzbee30adc732014-05-09 15:10:18 -0700472 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
473 // Width mismatch - don't try to reuse.
474 reg = RegStorage::InvalidReg();
475 }
476 }
477 if (reg.Valid()) {
478 if (reg.IsPair()) {
479 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
480 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
481 if (info_low->IsTemp()) {
482 info_low->MarkInUse();
483 }
484 if (info_high->IsTemp()) {
485 info_high->MarkInUse();
486 }
487 } else {
buzbee091cc402014-03-31 10:14:40 -0700488 RegisterInfo* info = GetRegInfo(reg);
489 if (info->IsTemp()) {
490 info->MarkInUse();
491 }
492 }
buzbee30adc732014-05-09 15:10:18 -0700493 } else {
494 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
495 ClobberSReg(s_reg);
496 if (wide) {
497 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700498 }
499 }
500 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501}
502
buzbee2700f7e2014-03-07 09:46:20 -0800503void Mir2Lir::FreeTemp(RegStorage reg) {
504 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700505 FreeTemp(reg.GetLow());
506 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800507 } else {
buzbee091cc402014-03-31 10:14:40 -0700508 RegisterInfo* p = GetRegInfo(reg);
509 if (p->IsTemp()) {
510 p->MarkFree();
511 p->SetIsWide(false);
512 p->SetPartner(reg);
513 }
buzbee2700f7e2014-03-07 09:46:20 -0800514 }
515}
516
buzbee082833c2014-05-17 23:16:26 -0700517void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
518 DCHECK(rl_keep.wide);
519 DCHECK(rl_free.wide);
520 int free_low = rl_free.reg.GetLowReg();
521 int free_high = rl_free.reg.GetHighReg();
522 int keep_low = rl_keep.reg.GetLowReg();
523 int keep_high = rl_keep.reg.GetHighReg();
524 if ((free_low != keep_low) && (free_low != keep_high) &&
525 (free_high != keep_low) && (free_high != keep_high)) {
526 // No overlap, free both
527 FreeTemp(rl_free.reg);
528 }
529}
530
buzbee262b2992014-03-27 11:22:43 -0700531bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700532 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800533 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700534 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
535 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700536 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700537 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800538 } else {
buzbee091cc402014-03-31 10:14:40 -0700539 RegisterInfo* p = GetRegInfo(reg);
540 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800541 }
buzbee091cc402014-03-31 10:14:40 -0700542 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543}
544
buzbee262b2992014-03-27 11:22:43 -0700545bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700546 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800547 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700548 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
549 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
550 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800551 } else {
buzbee091cc402014-03-31 10:14:40 -0700552 RegisterInfo* p = GetRegInfo(reg);
553 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800554 }
buzbee091cc402014-03-31 10:14:40 -0700555 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556}
557
buzbee262b2992014-03-27 11:22:43 -0700558bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700559 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800560 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700561 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
562 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
563 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800564 } else {
buzbee091cc402014-03-31 10:14:40 -0700565 RegisterInfo* p = GetRegInfo(reg);
566 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800567 }
buzbee091cc402014-03-31 10:14:40 -0700568 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569}
570
buzbee2700f7e2014-03-07 09:46:20 -0800571bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700572 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800573 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700574 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
575 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
576 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800577 } else {
buzbee091cc402014-03-31 10:14:40 -0700578 RegisterInfo* p = GetRegInfo(reg);
579 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800580 }
buzbee091cc402014-03-31 10:14:40 -0700581 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800582}
583
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584/*
585 * Similar to AllocTemp(), but forces the allocation of a specific
586 * register. No check is made to see if the register was previously
587 * allocated. Use with caution.
588 */
buzbee2700f7e2014-03-07 09:46:20 -0800589void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700590 DCHECK(IsTemp(reg));
591 if (reg.IsPair()) {
592 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
593 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
594 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700595 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700596 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700597 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700598 } else {
599 RegisterInfo* p = GetRegInfo(reg);
600 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700601 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700602 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603}
604
buzbee2700f7e2014-03-07 09:46:20 -0800605void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700606 if (reg.IsPair()) {
607 GetRegInfo(reg.GetLow())->ResetDefBody();
608 GetRegInfo(reg.GetHigh())->ResetDefBody();
609 } else {
610 GetRegInfo(reg)->ResetDefBody();
611 }
buzbee2700f7e2014-03-07 09:46:20 -0800612}
613
buzbee091cc402014-03-31 10:14:40 -0700614void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
615 RegisterInfo* info = nullptr;
616 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
617 if (IsTemp(rs)) {
618 info = GetRegInfo(reg);
619 }
620 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
621 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
622 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700624 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 break;
buzbee091cc402014-03-31 10:14:40 -0700626 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 }
628 }
629}
630
631/*
632 * Mark the beginning and end LIR of a def sequence. Note that
633 * on entry start points to the LIR prior to the beginning of the
634 * sequence.
635 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700636void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 DCHECK(!rl.wide);
638 DCHECK(start && start->next);
639 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700640 RegisterInfo* p = GetRegInfo(rl.reg);
641 p->SetDefStart(start->next);
642 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643}
644
645/*
646 * Mark the beginning and end LIR of a def sequence. Note that
647 * on entry start points to the LIR prior to the beginning of the
648 * sequence.
649 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700650void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 DCHECK(rl.wide);
652 DCHECK(start && start->next);
653 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700654 RegisterInfo* p;
655 if (rl.reg.IsPair()) {
656 p = GetRegInfo(rl.reg.GetLow());
657 ResetDef(rl.reg.GetHigh()); // Only track low of pair
658 } else {
659 p = GetRegInfo(rl.reg);
660 }
661 p->SetDefStart(start->next);
662 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663}
664
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700665RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 DCHECK(rl.wide);
667 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700668 if (rl.reg.IsPair()) {
669 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
670 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
671 if (info_lo->IsTemp()) {
672 info_lo->SetIsWide(false);
673 info_lo->ResetDefBody();
674 }
675 if (info_hi->IsTemp()) {
676 info_hi->SetIsWide(false);
677 info_hi->ResetDefBody();
678 }
679 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700680 } else {
681 /*
682 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
683 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
684 * this code. Will probably want to make this a virtual function.
685 */
686 // Can't narrow 64-bit register. Clobber.
687 if (GetRegInfo(rl.reg)->IsTemp()) {
688 Clobber(rl.reg);
689 FreeTemp(rl.reg);
690 }
691 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 }
694 rl.wide = false;
695 return rl;
696}
697
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700698void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700700 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
701 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 }
buzbee091cc402014-03-31 10:14:40 -0700703 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704}
705
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700708 // If pair, only track low reg of pair.
709 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
710 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
711 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 }
buzbee091cc402014-03-31 10:14:40 -0700713 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714}
715
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700716void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700717 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
718 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
719 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 }
buzbee091cc402014-03-31 10:14:40 -0700721 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
722 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
723 info->ResetDefBody();
724 }
725 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
726 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
727 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 }
729}
730
buzbeeba574512014-05-12 15:13:16 -0700731void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700732 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
733 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700734 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700735 }
736}
737
738void Mir2Lir::FlushRegWide(RegStorage reg) {
739 if (reg.IsPair()) {
740 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
741 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
742 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
743 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
744 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
745 if (!(info1->IsTemp() && info2->IsTemp())) {
746 /* Should not happen. If it does, there's a problem in eval_loc */
747 LOG(FATAL) << "Long half-temp, half-promoted";
748 }
749
750 info1->SetIsDirty(false);
751 info2->SetIsDirty(false);
752 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
753 info1 = info2;
754 }
755 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100756 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700757 }
758 } else {
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 Marko455759b2014-05-06 20:49:36 +0100763 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700764 }
765 }
766}
767
768void Mir2Lir::FlushReg(RegStorage reg) {
769 DCHECK(!reg.IsPair());
770 RegisterInfo* info = GetRegInfo(reg);
771 if (info->IsLive() && info->IsDirty()) {
772 info->SetIsDirty(false);
773 int v_reg = mir_graph_->SRegToVReg(info->SReg());
774 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775 }
776}
777
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800778void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700779 if (info->IsWide()) {
780 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800781 } else {
buzbee091cc402014-03-31 10:14:40 -0700782 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 }
784}
785
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700786void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700787 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
788 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700789 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700790 FlushSpecificReg(info);
791 }
buzbee30adc732014-05-09 15:10:18 -0700792 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700793 info->SetIsWide(false);
794 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795}
796
797
buzbee2700f7e2014-03-07 09:46:20 -0800798bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 if (reg_class == kAnyReg) {
800 return true;
801 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700802 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 } else {
buzbee091cc402014-03-31 10:14:40 -0700804 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700805 }
806}
807
buzbee091cc402014-03-31 10:14:40 -0700808void Mir2Lir::MarkLive(RegLocation loc) {
809 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700810 if (!IsTemp(reg)) {
811 return;
812 }
buzbee091cc402014-03-31 10:14:40 -0700813 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700814 if (s_reg == INVALID_SREG) {
815 // Can't be live if no associated sreg.
816 if (reg.IsPair()) {
817 GetRegInfo(reg.GetLow())->MarkDead();
818 GetRegInfo(reg.GetHigh())->MarkDead();
819 } else {
820 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700821 }
buzbee082833c2014-05-17 23:16:26 -0700822 } else {
823 if (reg.IsPair()) {
824 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
825 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
826 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
827 (info_hi->SReg() == s_reg)) {
828 return; // Already live.
829 }
830 ClobberSReg(s_reg);
831 ClobberSReg(s_reg + 1);
832 info_lo->MarkLive(s_reg);
833 info_hi->MarkLive(s_reg + 1);
834 } else {
835 RegisterInfo* info = GetRegInfo(reg);
836 if (info->IsLive() && (info->SReg() == s_reg)) {
837 return; // Already live.
838 }
839 ClobberSReg(s_reg);
840 if (loc.wide) {
841 ClobberSReg(s_reg + 1);
842 }
843 info->MarkLive(s_reg);
844 }
845 if (loc.wide) {
846 MarkWide(reg);
847 } else {
848 MarkNarrow(reg);
849 }
buzbee091cc402014-03-31 10:14:40 -0700850 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851}
852
buzbee2700f7e2014-03-07 09:46:20 -0800853void Mir2Lir::MarkTemp(RegStorage reg) {
854 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700856 tempreg_info_.Insert(info);
857 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700858}
859
buzbee2700f7e2014-03-07 09:46:20 -0800860void Mir2Lir::UnmarkTemp(RegStorage reg) {
861 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700862 RegisterInfo* info = GetRegInfo(reg);
863 tempreg_info_.Delete(info);
864 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800865}
866
buzbee091cc402014-03-31 10:14:40 -0700867void Mir2Lir::MarkWide(RegStorage reg) {
868 if (reg.IsPair()) {
869 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
870 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700871 // Unpair any old partners.
872 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
873 GetRegInfo(info_lo->Partner())->SetIsWide(false);
874 }
875 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
876 GetRegInfo(info_hi->Partner())->SetIsWide(false);
877 }
buzbee091cc402014-03-31 10:14:40 -0700878 info_lo->SetIsWide(true);
879 info_hi->SetIsWide(true);
880 info_lo->SetPartner(reg.GetHigh());
881 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800882 } else {
buzbee091cc402014-03-31 10:14:40 -0700883 RegisterInfo* info = GetRegInfo(reg);
884 info->SetIsWide(true);
885 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 }
887}
888
buzbee082833c2014-05-17 23:16:26 -0700889void Mir2Lir::MarkNarrow(RegStorage reg) {
890 DCHECK(!reg.IsPair());
891 RegisterInfo* info = GetRegInfo(reg);
892 info->SetIsWide(false);
893 info->SetPartner(reg);
894}
895
buzbee091cc402014-03-31 10:14:40 -0700896void Mir2Lir::MarkClean(RegLocation loc) {
897 if (loc.reg.IsPair()) {
898 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
899 info->SetIsDirty(false);
900 info = GetRegInfo(loc.reg.GetHigh());
901 info->SetIsDirty(false);
902 } else {
903 RegisterInfo* info = GetRegInfo(loc.reg);
904 info->SetIsDirty(false);
905 }
906}
907
908// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700909void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 if (loc.home) {
911 // If already home, can't be dirty
912 return;
913 }
buzbee091cc402014-03-31 10:14:40 -0700914 if (loc.reg.IsPair()) {
915 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
916 info->SetIsDirty(true);
917 info = GetRegInfo(loc.reg.GetHigh());
918 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800919 } else {
buzbee091cc402014-03-31 10:14:40 -0700920 RegisterInfo* info = GetRegInfo(loc.reg);
921 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 }
923}
924
buzbee2700f7e2014-03-07 09:46:20 -0800925void Mir2Lir::MarkInUse(RegStorage reg) {
926 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700927 GetRegInfo(reg.GetLow())->MarkInUse();
928 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800929 } else {
buzbee091cc402014-03-31 10:14:40 -0700930 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800931 }
932}
933
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700934bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700935 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700936 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700937 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
938 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700939 int my_sreg = info->SReg();
940 RegStorage partner_reg = info->Partner();
941 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700942 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700943 DCHECK(partner->IsWide());
944 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700945 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700946 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700947 if (my_sreg == INVALID_SREG) {
948 DCHECK_EQ(partner_sreg, INVALID_SREG);
949 } else {
950 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700951 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700952 }
953 }
buzbee082833c2014-05-17 23:16:26 -0700954 if (info->Master() != info) {
955 // Aliased.
956 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
957 // If I'm live, master should not be live, but should show liveness in alias set.
958 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
959 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700960 }
buzbee642fe342014-05-23 16:04:08 -0700961// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700962 }
963 if (info->IsAliased()) {
964 // Has child aliases.
965 DCHECK_EQ(info->Master(), info);
966 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
967 // Master live, no child should be dead - all should show liveness in set.
968 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
969 DCHECK(!p->IsDead());
970 DCHECK_EQ(p->SReg(), INVALID_SREG);
971 }
972 } else if (!info->IsDead()) {
973 // Master not live, one or more aliases must be.
974 bool live_alias = false;
975 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
976 live_alias |= p->IsLive();
977 }
978 DCHECK(live_alias);
979 }
980 }
981 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
982 // If not fully live, should have INVALID_SREG and def's should be null.
983 DCHECK(info->DefStart() == nullptr);
984 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700985 }
986 }
987 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988}
989
990/*
991 * Return an updated location record with current in-register status.
992 * If the value lives in live temps, reflect that fact. No code
993 * is generated. If the live value is part of an older pair,
994 * clobber both low and high.
995 * TUNING: clobbering both is a bit heavy-handed, but the alternative
996 * is a bit complex when dealing with FP regs. Examine code to see
997 * if it's worthwhile trying to be more clever here.
998 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700999RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 DCHECK(!loc.wide);
1001 DCHECK(CheckCorePoolSanity());
1002 if (loc.location != kLocPhysReg) {
1003 DCHECK((loc.location == kLocDalvikFrame) ||
1004 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001005 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
1006 if (reg.Valid()) {
1007 bool match = true;
1008 RegisterInfo* info = GetRegInfo(reg);
1009 match &= !reg.IsPair();
1010 match &= !info->IsWide();
1011 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001013 loc.reg = reg;
1014 } else {
1015 Clobber(reg);
1016 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 }
1018 }
1019 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 return loc;
1021}
1022
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001023RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 DCHECK(loc.wide);
1025 DCHECK(CheckCorePoolSanity());
1026 if (loc.location != kLocPhysReg) {
1027 DCHECK((loc.location == kLocDalvikFrame) ||
1028 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001029 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1030 if (reg.Valid()) {
1031 bool match = true;
1032 if (reg.IsPair()) {
1033 // If we've got a register pair, make sure that it was last used as the same pair.
1034 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1035 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1036 match &= info_lo->IsWide();
1037 match &= info_hi->IsWide();
1038 match &= (info_lo->Partner() == info_hi->GetReg());
1039 match &= (info_hi->Partner() == info_lo->GetReg());
1040 } else {
1041 RegisterInfo* info = GetRegInfo(reg);
1042 match &= info->IsWide();
1043 match &= (info->GetReg() == info->Partner());
1044 }
1045 if (match) {
1046 loc.location = kLocPhysReg;
1047 loc.reg = reg;
1048 } else {
1049 Clobber(reg);
1050 FreeTemp(reg);
1051 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 }
1053 }
1054 return loc;
1055}
1056
Brian Carlstrom7940e442013-07-12 13:46:57 -07001057/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001058RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 if (loc.wide)
1060 return UpdateLocWide(loc);
1061 else
1062 return UpdateLoc(loc);
1063}
1064
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001065RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067
1068 loc = UpdateLocWide(loc);
1069
1070 /* If already in registers, we can assume proper form. Right reg class? */
1071 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001072 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001073 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001074 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001075 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001076 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001077 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001078 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001079 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001080 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 }
1082 return loc;
1083 }
1084
1085 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1086 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1087
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001088 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001089 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 if (update) {
1092 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001093 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 return loc;
1096}
1097
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001098RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001099 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001101 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102
1103 loc = UpdateLoc(loc);
1104
1105 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001106 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001107 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001108 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001109 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001110 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001111 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001112 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001113 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001114 }
1115 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);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001121
1122 if (update) {
1123 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001124 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 }
1126 return loc;
1127}
1128
1129/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001130void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1132 RegLocation loc = mir_graph_->reg_location_[i];
1133 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1134 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001135 if (loc.fp) {
1136 if (loc.wide) {
1137 // Treat doubles as a unit, using upper half of fp_counts array.
1138 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1139 i++;
1140 } else {
1141 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1142 }
1143 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1145 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001146 }
1147}
1148
1149/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001150static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1152 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001153 // Note that we fall back to sorting on reg so we get stable output
1154 // on differing qsort implementations (such as on host and target or
1155 // between local host and build servers).
1156 return (op1->count == op2->count)
1157 ? (op1->s_reg - op2->s_reg)
1158 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159}
1160
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001161void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001162 LOG(INFO) << msg;
1163 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001164 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1165 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1166 } else {
1167 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1168 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001169 }
1170}
1171
1172/*
1173 * Note: some portions of this code required even if the kPromoteRegs
1174 * optimization is disabled.
1175 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001176void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001178 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001180 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1181 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001182 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183
1184 // Allow target code to add any special registers
1185 AdjustSpillMask();
1186
1187 /*
1188 * Simple register promotion. Just do a static count of the uses
1189 * of Dalvik registers. Note that we examine the SSA names, but
1190 * count based on original Dalvik register name. Count refs
1191 * separately based on type in order to give allocation
1192 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001193 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001194 * reg.
1195 * TUNING: replace with linear scan once we have the ability
1196 * to describe register live ranges for GC.
1197 */
1198 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001199 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001200 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001201 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001202 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001203 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 // Set ssa names for original Dalvik registers
1205 for (int i = 0; i < dalvik_regs; i++) {
1206 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1207 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001208
1209 // Set ssa names for compiler temporaries
1210 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1211 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1212 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1213 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1214 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001215 }
1216
1217 // Duplicate in upper half to represent possible fp double starting sregs.
1218 for (int i = 0; i < num_regs; i++) {
1219 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 }
1221
1222 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001223 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225
1226 // Sort the count arrays
1227 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001228 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229
1230 if (cu_->verbose) {
1231 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001232 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 }
1234
1235 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1236 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001237 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1238 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1239 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1240 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1241 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1242 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1243 // Ignore result - if can't alloc double may still be able to alloc singles.
1244 AllocPreservedDouble(low_sreg);
1245 }
1246 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001247 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1248 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001249 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 }
1251 }
1252 }
1253
1254 // Promote core regs
1255 for (int i = 0; (i < num_regs) &&
1256 (core_regs[i].count >= promotion_threshold); i++) {
1257 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1258 if (promotion_map_[p_map_idx].core_location !=
1259 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001260 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1261 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001262 break; // No more left
1263 }
1264 }
1265 }
1266 }
1267
1268 // Now, update SSA names to new home locations
1269 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1270 RegLocation *curr = &mir_graph_->reg_location_[i];
1271 int p_map_idx = SRegToPMap(curr->s_reg_low);
1272 if (!curr->wide) {
1273 if (curr->fp) {
1274 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1275 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001276 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 curr->home = true;
1278 }
1279 } else {
1280 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1281 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001282 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283 curr->home = true;
1284 }
1285 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001286 } else {
1287 if (curr->high_word) {
1288 continue;
1289 }
1290 if (curr->fp) {
1291 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001292 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001293 int low_reg = promotion_map_[p_map_idx].FpReg;
1294 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1295 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001296 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001297 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1298 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001299 if (cu_->instruction_set == kThumb2) {
1300 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1301 } else {
1302 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1303 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001304 curr->home = true;
1305 }
1306 }
1307 } else {
1308 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1309 && (promotion_map_[p_map_idx+1].core_location ==
1310 kLocPhysReg)) {
1311 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001312 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1313 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 curr->home = true;
1315 }
1316 }
1317 }
1318 }
1319 if (cu_->verbose) {
1320 DumpPromotionMap();
1321 }
1322}
1323
1324/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001325int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001326 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001327 fp_spill_mask_, frame_size_, v_reg,
1328 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001329}
1330
1331/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001332int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001333 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1334}
1335
1336/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001337RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338 RegLocation gpr_res = LocCReturnWide();
1339 RegLocation fpr_res = LocCReturnDouble();
1340 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee082833c2014-05-17 23:16:26 -07001341 Clobber(res.reg);
1342 LockTemp(res.reg);
1343 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344 return res;
1345}
1346
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001347RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 RegLocation gpr_res = LocCReturn();
1349 RegLocation fpr_res = LocCReturnFloat();
1350 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001351 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001353 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 } else {
buzbee091cc402014-03-31 10:14:40 -07001355 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 }
1357 return res;
1358}
1359
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001360void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 DoPromotion();
1362
1363 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1364 LOG(INFO) << "After Promotion";
1365 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1366 }
1367
1368 /* Set the frame size */
1369 frame_size_ = ComputeFrameSize();
1370}
1371
1372/*
1373 * Get the "real" sreg number associated with an s_reg slot. In general,
1374 * s_reg values passed through codegen are the SSA names created by
1375 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1376 * array. However, renaming is accomplished by simply replacing RegLocation
1377 * entries in the reglocation[] array. Therefore, when location
1378 * records for operands are first created, we need to ask the locRecord
1379 * identified by the dataflow pass what it's new name is.
1380 */
1381int Mir2Lir::GetSRegHi(int lowSreg) {
1382 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1383}
1384
buzbee091cc402014-03-31 10:14:40 -07001385bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001386 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 return true;
1388}
1389
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390} // namespace art