blob: 8c0f2bb7e2690b9329c51561399f66d79fd48edc [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()) {
176 ClobberAliases(info);
177 } else {
178 RegisterInfo* master = info->Master();
179 if (info != master) {
180 ClobberBody(info->Master());
181 }
182 }
buzbee30adc732014-05-09 15:10:18 -0700183 }
buzbee2700f7e2014-03-07 09:46:20 -0800184 }
185}
186
buzbee30adc732014-05-09 15:10:18 -0700187void Mir2Lir::ClobberAliases(RegisterInfo* info) {
buzbeeba574512014-05-12 15:13:16 -0700188 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
189 alias = alias->GetAliasChain()) {
190 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee082833c2014-05-17 23:16:26 -0700191 ClobberBody(alias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 }
193}
194
195/*
196 * Break the association between a Dalvik vreg and a physical temp register of either register
197 * class.
198 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
199 * in the register utilities, is is also used by code gen routines to work around a deficiency in
200 * local register allocation, which fails to distinguish between the "in" and "out" identities
201 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
202 * is used both as the source and destination register of an operation in which the type
203 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
204 * addressed.
205 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700206void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700207 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700208 if (kIsDebugBuild && s_reg == live_sreg_) {
209 live_sreg_ = INVALID_SREG;
210 }
211 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
212 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
213 if (info->SReg() == s_reg) {
buzbee082833c2014-05-17 23:16:26 -0700214 if (info->GetReg() != info->Partner()) {
215 // Dealing with a pair - clobber the other half.
216 DCHECK(!info->IsAliased());
217 ClobberBody(GetRegInfo(info->Partner()));
218 }
buzbeeba574512014-05-12 15:13:16 -0700219 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700220 if (info->IsAliased()) {
buzbee30adc732014-05-09 15:10:18 -0700221 ClobberAliases(info);
222 }
buzbee091cc402014-03-31 10:14:40 -0700223 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 }
225 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226}
227
228/*
229 * SSA names associated with the initial definitions of Dalvik
230 * registers are the same as the Dalvik register number (and
231 * thus take the same position in the promotion_map. However,
232 * the special Method* and compiler temp resisters use negative
233 * v_reg numbers to distinguish them and can have an arbitrary
234 * ssa name (above the last original Dalvik register). This function
235 * maps SSA names to positions in the promotion_map array.
236 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700237int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
239 DCHECK_GE(s_reg, 0);
240 int v_reg = mir_graph_->SRegToVReg(s_reg);
241 if (v_reg >= 0) {
242 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
243 return v_reg;
244 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800245 /*
246 * It must be the case that the v_reg for temporary is less than or equal to the
247 * base reg for temps. For that reason, "position" must be zero or positive.
248 */
249 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
250
251 // The temporaries are placed after dalvik registers in the promotion map
252 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
253 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 }
255}
256
buzbee091cc402014-03-31 10:14:40 -0700257// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800258void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 int p_map_idx = SRegToPMap(s_reg);
260 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700261 int reg_num = reg.GetRegNum();
262 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800263 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800265 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 num_core_spills_++;
267 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800268 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269}
270
buzbee091cc402014-03-31 10:14:40 -0700271/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800272RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
273 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700274 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
275 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
276 if (!info->IsTemp() && !info->InUse()) {
277 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 RecordCorePromotion(res, s_reg);
279 break;
280 }
281 }
282 return res;
283}
284
buzbee091cc402014-03-31 10:14:40 -0700285void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 int p_map_idx = SRegToPMap(s_reg);
287 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700288 GetRegInfo(reg)->MarkInUse();
289 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700291 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292}
293
buzbee091cc402014-03-31 10:14:40 -0700294// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800295RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
296 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700297 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
298 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
299 if (!info->IsTemp() && !info->InUse()) {
300 res = info->GetReg();
301 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 break;
303 }
304 }
305 return res;
306}
307
buzbee091cc402014-03-31 10:14:40 -0700308void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
309 int p_map_idx = SRegToPMap(s_reg);
310 int v_reg = mir_graph_->SRegToVReg(s_reg);
311 GetRegInfo(reg)->MarkInUse();
312 MarkPreservedDouble(v_reg, reg);
313 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
314 promotion_map_[p_map_idx].FpReg = reg.GetReg();
315}
316
317// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800318RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
319 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700320 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
321 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
322 if (!info->IsTemp() && !info->InUse()) {
323 res = info->GetReg();
324 RecordDoublePromotion(res, s_reg);
325 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 }
328 return res;
329}
330
buzbee091cc402014-03-31 10:14:40 -0700331
332RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
333 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700335 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 if (next >= num_regs)
337 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700338 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700339 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700340 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee091cc402014-03-31 10:14:40 -0700341 Clobber(info->GetReg());
342 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700343 /*
344 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
345 * The caller will set wideness as appropriate.
346 */
buzbee091cc402014-03-31 10:14:40 -0700347 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700349 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 }
351 next++;
352 }
353 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700354 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700355 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 if (next >= num_regs)
357 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700358 RegisterInfo* info = regs.Get(next);
359 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700360 // Got one. Kill it.
361 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700362 Clobber(info->GetReg());
363 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700364 if (info->IsWide()) {
365 RegisterInfo* partner = GetRegInfo(info->Partner());
366 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
367 DCHECK(partner->IsWide());
368 info->SetIsWide(false);
369 partner->SetIsWide(false);
370 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700372 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 }
374 next++;
375 }
376 if (required) {
377 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700378 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 LOG(FATAL) << "No free temp registers";
380 }
buzbee2700f7e2014-03-07 09:46:20 -0800381 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382}
383
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800385RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700386 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387}
388
buzbee2700f7e2014-03-07 09:46:20 -0800389RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700390 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391}
392
buzbeeb01bf152014-05-13 15:59:07 -0700393RegStorage Mir2Lir::AllocTempWide() {
394 RegStorage res;
395 if (reg_pool_->core64_regs_.Size() != 0) {
396 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, true);
397 } else {
398 RegStorage low_reg = AllocTemp();
399 RegStorage high_reg = AllocTemp();
400 res = RegStorage::MakeRegPair(low_reg, high_reg);
401 }
402 return res;
403}
404
buzbee091cc402014-03-31 10:14:40 -0700405RegStorage Mir2Lir::AllocTempSingle() {
406 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
407 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
408 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409}
410
buzbee091cc402014-03-31 10:14:40 -0700411RegStorage Mir2Lir::AllocTempDouble() {
412 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
413 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
414 return res;
415}
416
buzbeeb01bf152014-05-13 15:59:07 -0700417RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
418 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
419 return AllocTempDouble();
420 }
421 return AllocTempWide();
422}
423
424RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
425 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
426 return AllocTempSingle();
427 }
428 return AllocTemp();
429}
430
buzbee091cc402014-03-31 10:14:40 -0700431RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
432 RegStorage res;
433 GrowableArray<RegisterInfo*>::Iterator it(&regs);
434 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
435 if ((info->SReg() == s_reg) && info->IsLive()) {
436 res = info->GetReg();
437 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 return res;
441}
442
buzbee091cc402014-03-31 10:14:40 -0700443RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
444 RegStorage reg;
445 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
446 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
447 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 }
buzbee091cc402014-03-31 10:14:40 -0700449 if (!reg.Valid() && (reg_class != kFPReg)) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100450 if (Is64BitInstructionSet(cu_->instruction_set)) {
451 reg = FindLiveReg(wide ? reg_pool_->core64_regs_ : reg_pool_->core_regs_, s_reg);
452 } else {
453 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
454 }
buzbee091cc402014-03-31 10:14:40 -0700455 }
456 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700457 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
458 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700459 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
460 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700461 reg = RegStorage::MakeRegPair(reg, high_reg);
462 MarkWide(reg);
463 } else {
buzbee30adc732014-05-09 15:10:18 -0700464 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700465 reg = RegStorage::InvalidReg();
466 }
467 }
buzbee30adc732014-05-09 15:10:18 -0700468 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
469 // Width mismatch - don't try to reuse.
470 reg = RegStorage::InvalidReg();
471 }
472 }
473 if (reg.Valid()) {
474 if (reg.IsPair()) {
475 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
476 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
477 if (info_low->IsTemp()) {
478 info_low->MarkInUse();
479 }
480 if (info_high->IsTemp()) {
481 info_high->MarkInUse();
482 }
483 } else {
buzbee091cc402014-03-31 10:14:40 -0700484 RegisterInfo* info = GetRegInfo(reg);
485 if (info->IsTemp()) {
486 info->MarkInUse();
487 }
488 }
buzbee30adc732014-05-09 15:10:18 -0700489 } else {
490 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
491 ClobberSReg(s_reg);
492 if (wide) {
493 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700494 }
495 }
496 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700497}
498
buzbee2700f7e2014-03-07 09:46:20 -0800499void Mir2Lir::FreeTemp(RegStorage reg) {
500 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700501 FreeTemp(reg.GetLow());
502 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800503 } else {
buzbee091cc402014-03-31 10:14:40 -0700504 RegisterInfo* p = GetRegInfo(reg);
505 if (p->IsTemp()) {
506 p->MarkFree();
507 p->SetIsWide(false);
508 p->SetPartner(reg);
509 }
buzbee2700f7e2014-03-07 09:46:20 -0800510 }
511}
512
buzbee082833c2014-05-17 23:16:26 -0700513void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
514 DCHECK(rl_keep.wide);
515 DCHECK(rl_free.wide);
516 int free_low = rl_free.reg.GetLowReg();
517 int free_high = rl_free.reg.GetHighReg();
518 int keep_low = rl_keep.reg.GetLowReg();
519 int keep_high = rl_keep.reg.GetHighReg();
520 if ((free_low != keep_low) && (free_low != keep_high) &&
521 (free_high != keep_low) && (free_high != keep_high)) {
522 // No overlap, free both
523 FreeTemp(rl_free.reg);
524 }
525}
526
buzbee262b2992014-03-27 11:22:43 -0700527bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700528 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800529 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700530 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
531 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700532 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700533 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800534 } else {
buzbee091cc402014-03-31 10:14:40 -0700535 RegisterInfo* p = GetRegInfo(reg);
536 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800537 }
buzbee091cc402014-03-31 10:14:40 -0700538 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539}
540
buzbee262b2992014-03-27 11:22:43 -0700541bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700542 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800543 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700544 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
545 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
546 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800547 } else {
buzbee091cc402014-03-31 10:14:40 -0700548 RegisterInfo* p = GetRegInfo(reg);
549 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800550 }
buzbee091cc402014-03-31 10:14:40 -0700551 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552}
553
buzbee262b2992014-03-27 11:22:43 -0700554bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700555 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800556 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700557 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
558 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
559 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800560 } else {
buzbee091cc402014-03-31 10:14:40 -0700561 RegisterInfo* p = GetRegInfo(reg);
562 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800563 }
buzbee091cc402014-03-31 10:14:40 -0700564 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565}
566
buzbee2700f7e2014-03-07 09:46:20 -0800567bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700568 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800569 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700570 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
571 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
572 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800573 } else {
buzbee091cc402014-03-31 10:14:40 -0700574 RegisterInfo* p = GetRegInfo(reg);
575 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800576 }
buzbee091cc402014-03-31 10:14:40 -0700577 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800578}
579
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580/*
581 * Similar to AllocTemp(), but forces the allocation of a specific
582 * register. No check is made to see if the register was previously
583 * allocated. Use with caution.
584 */
buzbee2700f7e2014-03-07 09:46:20 -0800585void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700586 DCHECK(IsTemp(reg));
587 if (reg.IsPair()) {
588 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
589 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
590 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700591 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700592 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700593 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700594 } else {
595 RegisterInfo* p = GetRegInfo(reg);
596 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700597 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700598 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599}
600
buzbee2700f7e2014-03-07 09:46:20 -0800601void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700602 if (reg.IsPair()) {
603 GetRegInfo(reg.GetLow())->ResetDefBody();
604 GetRegInfo(reg.GetHigh())->ResetDefBody();
605 } else {
606 GetRegInfo(reg)->ResetDefBody();
607 }
buzbee2700f7e2014-03-07 09:46:20 -0800608}
609
buzbee091cc402014-03-31 10:14:40 -0700610void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
611 RegisterInfo* info = nullptr;
612 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
613 if (IsTemp(rs)) {
614 info = GetRegInfo(reg);
615 }
616 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
617 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
618 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700620 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 break;
buzbee091cc402014-03-31 10:14:40 -0700622 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 }
624 }
625}
626
627/*
628 * Mark the beginning and end LIR of a def sequence. Note that
629 * on entry start points to the LIR prior to the beginning of the
630 * sequence.
631 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700632void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 DCHECK(!rl.wide);
634 DCHECK(start && start->next);
635 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700636 RegisterInfo* p = GetRegInfo(rl.reg);
637 p->SetDefStart(start->next);
638 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639}
640
641/*
642 * Mark the beginning and end LIR of a def sequence. Note that
643 * on entry start points to the LIR prior to the beginning of the
644 * sequence.
645 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700646void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 DCHECK(rl.wide);
648 DCHECK(start && start->next);
649 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700650 RegisterInfo* p;
651 if (rl.reg.IsPair()) {
652 p = GetRegInfo(rl.reg.GetLow());
653 ResetDef(rl.reg.GetHigh()); // Only track low of pair
654 } else {
655 p = GetRegInfo(rl.reg);
656 }
657 p->SetDefStart(start->next);
658 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659}
660
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700661RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 DCHECK(rl.wide);
663 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700664 if (rl.reg.IsPair()) {
665 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
666 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
667 if (info_lo->IsTemp()) {
668 info_lo->SetIsWide(false);
669 info_lo->ResetDefBody();
670 }
671 if (info_hi->IsTemp()) {
672 info_hi->SetIsWide(false);
673 info_hi->ResetDefBody();
674 }
675 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700676 } else {
677 /*
678 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
679 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
680 * this code. Will probably want to make this a virtual function.
681 */
682 // Can't narrow 64-bit register. Clobber.
683 if (GetRegInfo(rl.reg)->IsTemp()) {
684 Clobber(rl.reg);
685 FreeTemp(rl.reg);
686 }
687 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 }
690 rl.wide = false;
691 return rl;
692}
693
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700694void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700696 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
697 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700698 }
buzbee091cc402014-03-31 10:14:40 -0700699 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700}
701
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700702void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700704 // If pair, only track low reg of pair.
705 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
706 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
707 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 }
buzbee091cc402014-03-31 10:14:40 -0700709 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710}
711
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700712void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700713 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
714 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
715 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 }
buzbee091cc402014-03-31 10:14:40 -0700717 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
718 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
719 info->ResetDefBody();
720 }
721 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
722 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
723 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700724 }
725}
726
buzbeeba574512014-05-12 15:13:16 -0700727void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700728 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
729 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700730 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700731 }
732}
733
734void Mir2Lir::FlushRegWide(RegStorage reg) {
735 if (reg.IsPair()) {
736 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
737 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
738 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
739 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
740 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
741 if (!(info1->IsTemp() && info2->IsTemp())) {
742 /* Should not happen. If it does, there's a problem in eval_loc */
743 LOG(FATAL) << "Long half-temp, half-promoted";
744 }
745
746 info1->SetIsDirty(false);
747 info2->SetIsDirty(false);
748 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
749 info1 = info2;
750 }
751 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100752 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700753 }
754 } else {
755 RegisterInfo* info = GetRegInfo(reg);
756 if (info->IsLive() && info->IsDirty()) {
757 info->SetIsDirty(false);
758 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100759 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700760 }
761 }
762}
763
764void Mir2Lir::FlushReg(RegStorage reg) {
765 DCHECK(!reg.IsPair());
766 RegisterInfo* info = GetRegInfo(reg);
767 if (info->IsLive() && info->IsDirty()) {
768 info->SetIsDirty(false);
769 int v_reg = mir_graph_->SRegToVReg(info->SReg());
770 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 }
772}
773
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800774void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700775 if (info->IsWide()) {
776 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800777 } else {
buzbee091cc402014-03-31 10:14:40 -0700778 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 }
780}
781
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700782void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700783 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
784 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700785 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700786 FlushSpecificReg(info);
787 }
buzbee30adc732014-05-09 15:10:18 -0700788 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700789 info->SetIsWide(false);
790 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791}
792
793
buzbee2700f7e2014-03-07 09:46:20 -0800794bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795 if (reg_class == kAnyReg) {
796 return true;
797 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700798 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 } else {
buzbee091cc402014-03-31 10:14:40 -0700800 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700801 }
802}
803
buzbee091cc402014-03-31 10:14:40 -0700804void Mir2Lir::MarkLive(RegLocation loc) {
805 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700806 if (!IsTemp(reg)) {
807 return;
808 }
buzbee091cc402014-03-31 10:14:40 -0700809 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700810 if (s_reg == INVALID_SREG) {
811 // Can't be live if no associated sreg.
812 if (reg.IsPair()) {
813 GetRegInfo(reg.GetLow())->MarkDead();
814 GetRegInfo(reg.GetHigh())->MarkDead();
815 } else {
816 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700817 }
buzbee082833c2014-05-17 23:16:26 -0700818 } else {
819 if (reg.IsPair()) {
820 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
821 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
822 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
823 (info_hi->SReg() == s_reg)) {
824 return; // Already live.
825 }
826 ClobberSReg(s_reg);
827 ClobberSReg(s_reg + 1);
828 info_lo->MarkLive(s_reg);
829 info_hi->MarkLive(s_reg + 1);
830 } else {
831 RegisterInfo* info = GetRegInfo(reg);
832 if (info->IsLive() && (info->SReg() == s_reg)) {
833 return; // Already live.
834 }
835 ClobberSReg(s_reg);
836 if (loc.wide) {
837 ClobberSReg(s_reg + 1);
838 }
839 info->MarkLive(s_reg);
840 }
841 if (loc.wide) {
842 MarkWide(reg);
843 } else {
844 MarkNarrow(reg);
845 }
buzbee091cc402014-03-31 10:14:40 -0700846 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700847}
848
buzbee2700f7e2014-03-07 09:46:20 -0800849void Mir2Lir::MarkTemp(RegStorage reg) {
850 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700851 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700852 tempreg_info_.Insert(info);
853 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700854}
855
buzbee2700f7e2014-03-07 09:46:20 -0800856void Mir2Lir::UnmarkTemp(RegStorage reg) {
857 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700858 RegisterInfo* info = GetRegInfo(reg);
859 tempreg_info_.Delete(info);
860 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800861}
862
buzbee091cc402014-03-31 10:14:40 -0700863void Mir2Lir::MarkWide(RegStorage reg) {
864 if (reg.IsPair()) {
865 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
866 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700867 // Unpair any old partners.
868 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
869 GetRegInfo(info_lo->Partner())->SetIsWide(false);
870 }
871 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
872 GetRegInfo(info_hi->Partner())->SetIsWide(false);
873 }
buzbee091cc402014-03-31 10:14:40 -0700874 info_lo->SetIsWide(true);
875 info_hi->SetIsWide(true);
876 info_lo->SetPartner(reg.GetHigh());
877 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800878 } else {
buzbee091cc402014-03-31 10:14:40 -0700879 RegisterInfo* info = GetRegInfo(reg);
880 info->SetIsWide(true);
881 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700882 }
883}
884
buzbee082833c2014-05-17 23:16:26 -0700885void Mir2Lir::MarkNarrow(RegStorage reg) {
886 DCHECK(!reg.IsPair());
887 RegisterInfo* info = GetRegInfo(reg);
888 info->SetIsWide(false);
889 info->SetPartner(reg);
890}
891
buzbee091cc402014-03-31 10:14:40 -0700892void Mir2Lir::MarkClean(RegLocation loc) {
893 if (loc.reg.IsPair()) {
894 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
895 info->SetIsDirty(false);
896 info = GetRegInfo(loc.reg.GetHigh());
897 info->SetIsDirty(false);
898 } else {
899 RegisterInfo* info = GetRegInfo(loc.reg);
900 info->SetIsDirty(false);
901 }
902}
903
904// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700905void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700906 if (loc.home) {
907 // If already home, can't be dirty
908 return;
909 }
buzbee091cc402014-03-31 10:14:40 -0700910 if (loc.reg.IsPair()) {
911 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
912 info->SetIsDirty(true);
913 info = GetRegInfo(loc.reg.GetHigh());
914 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800915 } else {
buzbee091cc402014-03-31 10:14:40 -0700916 RegisterInfo* info = GetRegInfo(loc.reg);
917 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 }
919}
920
buzbee2700f7e2014-03-07 09:46:20 -0800921void Mir2Lir::MarkInUse(RegStorage reg) {
922 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700923 GetRegInfo(reg.GetLow())->MarkInUse();
924 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800925 } else {
buzbee091cc402014-03-31 10:14:40 -0700926 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800927 }
928}
929
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700930bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700931 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700932 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700933 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
934 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700935 int my_sreg = info->SReg();
936 RegStorage partner_reg = info->Partner();
937 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700938 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700939 DCHECK(partner->IsWide());
940 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700941 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700942 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700943 if (my_sreg == INVALID_SREG) {
944 DCHECK_EQ(partner_sreg, INVALID_SREG);
945 } else {
946 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700947 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700948 }
949 }
buzbee082833c2014-05-17 23:16:26 -0700950 if (info->Master() != info) {
951 // Aliased.
952 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
953 // If I'm live, master should not be live, but should show liveness in alias set.
954 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
955 DCHECK(!info->Master()->IsDead());
956 } else if (!info->IsDead()) {
957 // If I'm not live, but there is liveness in the set master must be live.
958 DCHECK_EQ(info->SReg(), INVALID_SREG);
959 DCHECK(info->Master()->IsLive());
960 }
961 }
962 if (info->IsAliased()) {
963 // Has child aliases.
964 DCHECK_EQ(info->Master(), info);
965 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
966 // Master live, no child should be dead - all should show liveness in set.
967 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
968 DCHECK(!p->IsDead());
969 DCHECK_EQ(p->SReg(), INVALID_SREG);
970 }
971 } else if (!info->IsDead()) {
972 // Master not live, one or more aliases must be.
973 bool live_alias = false;
974 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
975 live_alias |= p->IsLive();
976 }
977 DCHECK(live_alias);
978 }
979 }
980 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
981 // If not fully live, should have INVALID_SREG and def's should be null.
982 DCHECK(info->DefStart() == nullptr);
983 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700984 }
985 }
986 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700987}
988
989/*
990 * Return an updated location record with current in-register status.
991 * If the value lives in live temps, reflect that fact. No code
992 * is generated. If the live value is part of an older pair,
993 * clobber both low and high.
994 * TUNING: clobbering both is a bit heavy-handed, but the alternative
995 * is a bit complex when dealing with FP regs. Examine code to see
996 * if it's worthwhile trying to be more clever here.
997 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700998RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700999 DCHECK(!loc.wide);
1000 DCHECK(CheckCorePoolSanity());
1001 if (loc.location != kLocPhysReg) {
1002 DCHECK((loc.location == kLocDalvikFrame) ||
1003 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001004 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
1005 if (reg.Valid()) {
1006 bool match = true;
1007 RegisterInfo* info = GetRegInfo(reg);
1008 match &= !reg.IsPair();
1009 match &= !info->IsWide();
1010 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001012 loc.reg = reg;
1013 } else {
1014 Clobber(reg);
1015 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 }
1017 }
1018 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 return loc;
1020}
1021
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001022RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001023 DCHECK(loc.wide);
1024 DCHECK(CheckCorePoolSanity());
1025 if (loc.location != kLocPhysReg) {
1026 DCHECK((loc.location == kLocDalvikFrame) ||
1027 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001028 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1029 if (reg.Valid()) {
1030 bool match = true;
1031 if (reg.IsPair()) {
1032 // If we've got a register pair, make sure that it was last used as the same pair.
1033 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1034 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1035 match &= info_lo->IsWide();
1036 match &= info_hi->IsWide();
1037 match &= (info_lo->Partner() == info_hi->GetReg());
1038 match &= (info_hi->Partner() == info_lo->GetReg());
1039 } else {
1040 RegisterInfo* info = GetRegInfo(reg);
1041 match &= info->IsWide();
1042 match &= (info->GetReg() == info->Partner());
1043 }
1044 if (match) {
1045 loc.location = kLocPhysReg;
1046 loc.reg = reg;
1047 } else {
1048 Clobber(reg);
1049 FreeTemp(reg);
1050 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 }
1052 }
1053 return loc;
1054}
1055
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001057RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 if (loc.wide)
1059 return UpdateLocWide(loc);
1060 else
1061 return UpdateLoc(loc);
1062}
1063
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001064RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066
1067 loc = UpdateLocWide(loc);
1068
1069 /* If already in registers, we can assume proper form. Right reg class? */
1070 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001071 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001072 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001073 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001074 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001075 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001076 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001077 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001078 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001079 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 }
1081 return loc;
1082 }
1083
1084 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1085 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1086
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001087 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001088 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090 if (update) {
1091 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001092 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 return loc;
1095}
1096
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001097RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001098 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001100 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001101
1102 loc = UpdateLoc(loc);
1103
1104 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001105 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001106 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001107 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001108 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001109 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001110 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001111 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001112 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 }
1114 return loc;
1115 }
1116
1117 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1118
buzbee2700f7e2014-03-07 09:46:20 -08001119 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120
1121 if (update) {
1122 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001123 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001124 }
1125 return loc;
1126}
1127
1128/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001129void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1131 RegLocation loc = mir_graph_->reg_location_[i];
1132 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1133 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001134 if (loc.fp) {
1135 if (loc.wide) {
1136 // Treat doubles as a unit, using upper half of fp_counts array.
1137 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1138 i++;
1139 } else {
1140 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1141 }
1142 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001143 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1144 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 }
1146}
1147
1148/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001149static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1151 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001152 // Note that we fall back to sorting on reg so we get stable output
1153 // on differing qsort implementations (such as on host and target or
1154 // between local host and build servers).
1155 return (op1->count == op2->count)
1156 ? (op1->s_reg - op2->s_reg)
1157 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158}
1159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001160void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 LOG(INFO) << msg;
1162 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001163 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1164 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1165 } else {
1166 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1167 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 }
1169}
1170
1171/*
1172 * Note: some portions of this code required even if the kPromoteRegs
1173 * optimization is disabled.
1174 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001175void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001177 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001179 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1180 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001181 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182
1183 // Allow target code to add any special registers
1184 AdjustSpillMask();
1185
1186 /*
1187 * Simple register promotion. Just do a static count of the uses
1188 * of Dalvik registers. Note that we examine the SSA names, but
1189 * count based on original Dalvik register name. Count refs
1190 * separately based on type in order to give allocation
1191 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001192 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 * reg.
1194 * TUNING: replace with linear scan once we have the ability
1195 * to describe register live ranges for GC.
1196 */
1197 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001198 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001199 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001201 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001202 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001203 // Set ssa names for original Dalvik registers
1204 for (int i = 0; i < dalvik_regs; i++) {
1205 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1206 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001207
1208 // Set ssa names for compiler temporaries
1209 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1210 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1211 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1212 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1213 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001214 }
1215
1216 // Duplicate in upper half to represent possible fp double starting sregs.
1217 for (int i = 0; i < num_regs; i++) {
1218 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219 }
1220
1221 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001222 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223
Brian Carlstrom7940e442013-07-12 13:46:57 -07001224
1225 // Sort the count arrays
1226 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001227 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001228
1229 if (cu_->verbose) {
1230 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001231 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001232 }
1233
1234 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1235 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001236 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1237 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1238 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1239 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1240 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1241 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1242 // Ignore result - if can't alloc double may still be able to alloc singles.
1243 AllocPreservedDouble(low_sreg);
1244 }
1245 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001246 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1247 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001248 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001249 }
1250 }
1251 }
1252
1253 // Promote core regs
1254 for (int i = 0; (i < num_regs) &&
1255 (core_regs[i].count >= promotion_threshold); i++) {
1256 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1257 if (promotion_map_[p_map_idx].core_location !=
1258 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001259 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1260 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001261 break; // No more left
1262 }
1263 }
1264 }
1265 }
1266
1267 // Now, update SSA names to new home locations
1268 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1269 RegLocation *curr = &mir_graph_->reg_location_[i];
1270 int p_map_idx = SRegToPMap(curr->s_reg_low);
1271 if (!curr->wide) {
1272 if (curr->fp) {
1273 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1274 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001275 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001276 curr->home = true;
1277 }
1278 } else {
1279 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1280 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001281 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282 curr->home = true;
1283 }
1284 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 } else {
1286 if (curr->high_word) {
1287 continue;
1288 }
1289 if (curr->fp) {
1290 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001291 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 int low_reg = promotion_map_[p_map_idx].FpReg;
1293 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1294 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001295 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001296 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1297 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001298 if (cu_->instruction_set == kThumb2) {
1299 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1300 } else {
1301 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1302 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001303 curr->home = true;
1304 }
1305 }
1306 } else {
1307 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1308 && (promotion_map_[p_map_idx+1].core_location ==
1309 kLocPhysReg)) {
1310 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001311 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1312 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001313 curr->home = true;
1314 }
1315 }
1316 }
1317 }
1318 if (cu_->verbose) {
1319 DumpPromotionMap();
1320 }
1321}
1322
1323/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001324int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001325 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001326 fp_spill_mask_, frame_size_, v_reg,
1327 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001328}
1329
1330/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001331int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001332 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1333}
1334
1335/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001336RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 RegLocation gpr_res = LocCReturnWide();
1338 RegLocation fpr_res = LocCReturnDouble();
1339 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee082833c2014-05-17 23:16:26 -07001340 Clobber(res.reg);
1341 LockTemp(res.reg);
1342 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001343 return res;
1344}
1345
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001346RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001347 RegLocation gpr_res = LocCReturn();
1348 RegLocation fpr_res = LocCReturnFloat();
1349 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001350 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001351 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001352 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001353 } else {
buzbee091cc402014-03-31 10:14:40 -07001354 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 }
1356 return res;
1357}
1358
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001359void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001360 DoPromotion();
1361
1362 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1363 LOG(INFO) << "After Promotion";
1364 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1365 }
1366
1367 /* Set the frame size */
1368 frame_size_ = ComputeFrameSize();
1369}
1370
1371/*
1372 * Get the "real" sreg number associated with an s_reg slot. In general,
1373 * s_reg values passed through codegen are the SSA names created by
1374 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1375 * array. However, renaming is accomplished by simply replacing RegLocation
1376 * entries in the reglocation[] array. Therefore, when location
1377 * records for operands are first created, we need to ask the locRecord
1378 * identified by the dataflow pass what it's new name is.
1379 */
1380int Mir2Lir::GetSRegHi(int lowSreg) {
1381 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1382}
1383
buzbee091cc402014-03-31 10:14:40 -07001384bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001385 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001386 return true;
1387}
1388
Brian Carlstrom7940e442013-07-12 13:46:57 -07001389} // namespace art