blob: bcc077bc181cad7fe2c8355c2e7e5eb9b68c7207 [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,
60 const std::vector<RegStorage>& sp_regs,
61 const std::vector<RegStorage>& dp_regs,
62 const std::vector<RegStorage>& reserved_regs,
63 const std::vector<RegStorage>& core_temps,
64 const std::vector<RegStorage>& sp_temps,
65 const std::vector<RegStorage>& dp_temps) :
66 core_regs_(arena, core_regs.size()), next_core_reg_(0), sp_regs_(arena, sp_regs.size()),
67 next_sp_reg_(0), dp_regs_(arena, dp_regs.size()), next_dp_reg_(0), m2l_(m2l) {
68 // Initialize the fast lookup map.
69 m2l_->reginfo_map_.Reset();
buzbeeba574512014-05-12 15:13:16 -070070 if (kIsDebugBuild) {
71 m2l_->reginfo_map_.Resize(RegStorage::kMaxRegs);
72 for (unsigned i = 0; i < RegStorage::kMaxRegs; i++) {
73 m2l_->reginfo_map_.Insert(nullptr);
74 }
75 } else {
76 m2l_->reginfo_map_.SetSize(RegStorage::kMaxRegs);
buzbee091cc402014-03-31 10:14:40 -070077 }
78
79 // Construct the register pool.
80 for (RegStorage reg : core_regs) {
81 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
82 m2l_->reginfo_map_.Put(reg.GetReg(), info);
83 core_regs_.Insert(info);
84 }
85 for (RegStorage reg : sp_regs) {
86 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
87 m2l_->reginfo_map_.Put(reg.GetReg(), info);
88 sp_regs_.Insert(info);
89 }
90 for (RegStorage reg : dp_regs) {
91 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
92 m2l_->reginfo_map_.Put(reg.GetReg(), info);
93 dp_regs_.Insert(info);
94 }
95
96 // Keep special registers from being allocated.
97 for (RegStorage reg : reserved_regs) {
98 m2l_->MarkInUse(reg);
99 }
100
101 // Mark temp regs - all others not in use can be used for promotion
102 for (RegStorage reg : core_temps) {
103 m2l_->MarkTemp(reg);
104 }
105 for (RegStorage reg : sp_temps) {
106 m2l_->MarkTemp(reg);
107 }
108 for (RegStorage reg : dp_temps) {
109 m2l_->MarkTemp(reg);
110 }
111
112 // Add an entry for InvalidReg with zero'd mask.
113 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), 0);
114 m2l_->reginfo_map_.Put(RegStorage::InvalidReg().GetReg(), invalid_reg);
115}
116
117void Mir2Lir::DumpRegPool(GrowableArray<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 LOG(INFO) << "================================================";
buzbee091cc402014-03-31 10:14:40 -0700119 GrowableArray<RegisterInfo*>::Iterator it(regs);
120 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700122 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
123 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
124 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
125 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 }
127 LOG(INFO) << "================================================";
128}
129
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700130void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700131 DumpRegPool(&reg_pool_->core_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132}
133
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700134void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700135 DumpRegPool(&reg_pool_->sp_regs_);
136 DumpRegPool(&reg_pool_->dp_regs_);
137}
138
139void Mir2Lir::DumpRegPools() {
140 LOG(INFO) << "Core registers";
141 DumpCoreRegPool();
142 LOG(INFO) << "FP registers";
143 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144}
145
buzbee2700f7e2014-03-07 09:46:20 -0800146void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700147 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700148 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700149 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700150 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700151 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800152 } else {
buzbee30adc732014-05-09 15:10:18 -0700153 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700154 if (info->IsTemp() && !info->IsDead()) {
155 ClobberBody(info);
156 if (info->IsAliased()) {
157 ClobberAliases(info);
158 } else {
159 RegisterInfo* master = info->Master();
160 if (info != master) {
161 ClobberBody(info->Master());
162 }
163 }
buzbee30adc732014-05-09 15:10:18 -0700164 }
buzbee2700f7e2014-03-07 09:46:20 -0800165 }
166}
167
buzbee30adc732014-05-09 15:10:18 -0700168void Mir2Lir::ClobberAliases(RegisterInfo* info) {
buzbeeba574512014-05-12 15:13:16 -0700169 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
170 alias = alias->GetAliasChain()) {
171 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
172 if (alias->SReg() != INVALID_SREG) {
173 alias->SetSReg(INVALID_SREG);
174 alias->ResetDefBody();
175 if (alias->IsWide()) {
176 alias->SetIsWide(false);
177 if (alias->GetReg() != alias->Partner()) {
178 RegisterInfo* p = GetRegInfo(alias->Partner());
179 p->SetIsWide(false);
180 p->MarkDead();
181 p->ResetDefBody();
182 }
183 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 }
185 }
186}
187
188/*
189 * Break the association between a Dalvik vreg and a physical temp register of either register
190 * class.
191 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
192 * in the register utilities, is is also used by code gen routines to work around a deficiency in
193 * local register allocation, which fails to distinguish between the "in" and "out" identities
194 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
195 * is used both as the source and destination register of an operation in which the type
196 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
197 * addressed.
198 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700199void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700200 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700201 if (kIsDebugBuild && s_reg == live_sreg_) {
202 live_sreg_ = INVALID_SREG;
203 }
204 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
205 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
206 if (info->SReg() == s_reg) {
buzbeeba574512014-05-12 15:13:16 -0700207 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700208 if (info->IsAliased()) {
buzbee30adc732014-05-09 15:10:18 -0700209 ClobberAliases(info);
210 }
buzbee091cc402014-03-31 10:14:40 -0700211 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 }
213 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214}
215
216/*
217 * SSA names associated with the initial definitions of Dalvik
218 * registers are the same as the Dalvik register number (and
219 * thus take the same position in the promotion_map. However,
220 * the special Method* and compiler temp resisters use negative
221 * v_reg numbers to distinguish them and can have an arbitrary
222 * ssa name (above the last original Dalvik register). This function
223 * maps SSA names to positions in the promotion_map array.
224 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700225int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
227 DCHECK_GE(s_reg, 0);
228 int v_reg = mir_graph_->SRegToVReg(s_reg);
229 if (v_reg >= 0) {
230 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
231 return v_reg;
232 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800233 /*
234 * It must be the case that the v_reg for temporary is less than or equal to the
235 * base reg for temps. For that reason, "position" must be zero or positive.
236 */
237 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
238
239 // The temporaries are placed after dalvik registers in the promotion map
240 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
241 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 }
243}
244
buzbee091cc402014-03-31 10:14:40 -0700245// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800246void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 int p_map_idx = SRegToPMap(s_reg);
248 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700249 int reg_num = reg.GetRegNum();
250 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800251 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800253 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 num_core_spills_++;
255 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800256 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257}
258
buzbee091cc402014-03-31 10:14:40 -0700259/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800260RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
261 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700262 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
263 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
264 if (!info->IsTemp() && !info->InUse()) {
265 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 RecordCorePromotion(res, s_reg);
267 break;
268 }
269 }
270 return res;
271}
272
buzbee091cc402014-03-31 10:14:40 -0700273void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 int p_map_idx = SRegToPMap(s_reg);
275 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700276 GetRegInfo(reg)->MarkInUse();
277 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700279 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280}
281
buzbee091cc402014-03-31 10:14:40 -0700282// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800283RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
284 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700285 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
286 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
287 if (!info->IsTemp() && !info->InUse()) {
288 res = info->GetReg();
289 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 break;
291 }
292 }
293 return res;
294}
295
buzbee091cc402014-03-31 10:14:40 -0700296void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
297 int p_map_idx = SRegToPMap(s_reg);
298 int v_reg = mir_graph_->SRegToVReg(s_reg);
299 GetRegInfo(reg)->MarkInUse();
300 MarkPreservedDouble(v_reg, reg);
301 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
302 promotion_map_[p_map_idx].FpReg = reg.GetReg();
303}
304
305// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800306RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
307 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700308 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
309 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
310 if (!info->IsTemp() && !info->InUse()) {
311 res = info->GetReg();
312 RecordDoublePromotion(res, s_reg);
313 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 }
316 return res;
317}
318
buzbee091cc402014-03-31 10:14:40 -0700319
320RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
321 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700323 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 if (next >= num_regs)
325 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700326 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700327 // Try to allocate a register that doesn't hold a live value.
buzbee091cc402014-03-31 10:14:40 -0700328 if (info->IsTemp() && !info->InUse() && !info->IsLive()) {
329 Clobber(info->GetReg());
330 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700331 /*
332 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
333 * The caller will set wideness as appropriate.
334 */
buzbee091cc402014-03-31 10:14:40 -0700335 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700337 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 }
339 next++;
340 }
341 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700342 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700343 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 if (next >= num_regs)
345 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700346 RegisterInfo* info = regs.Get(next);
347 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700348 // Got one. Kill it.
349 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700350 Clobber(info->GetReg());
351 info->MarkInUse();
352 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700354 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 }
356 next++;
357 }
358 if (required) {
359 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700360 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 LOG(FATAL) << "No free temp registers";
362 }
buzbee2700f7e2014-03-07 09:46:20 -0800363 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364}
365
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800367RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700368 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369}
370
buzbee2700f7e2014-03-07 09:46:20 -0800371RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700372 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373}
374
buzbee091cc402014-03-31 10:14:40 -0700375RegStorage Mir2Lir::AllocTempSingle() {
376 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
377 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
378 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379}
380
buzbee091cc402014-03-31 10:14:40 -0700381RegStorage Mir2Lir::AllocTempDouble() {
382 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
383 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
384 return res;
385}
386
387RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
388 RegStorage res;
389 GrowableArray<RegisterInfo*>::Iterator it(&regs);
390 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
391 if ((info->SReg() == s_reg) && info->IsLive()) {
392 res = info->GetReg();
393 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 }
395 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 return res;
397}
398
buzbee091cc402014-03-31 10:14:40 -0700399RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
400 RegStorage reg;
401 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
402 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
403 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 }
buzbee091cc402014-03-31 10:14:40 -0700405 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee30adc732014-05-09 15:10:18 -0700406 // TODO: add 64-bit core pool similar to above.
buzbee091cc402014-03-31 10:14:40 -0700407 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
408 }
409 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700410 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
411 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700412 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
413 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700414 reg = RegStorage::MakeRegPair(reg, high_reg);
415 MarkWide(reg);
416 } else {
buzbee30adc732014-05-09 15:10:18 -0700417 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700418 reg = RegStorage::InvalidReg();
419 }
420 }
buzbee30adc732014-05-09 15:10:18 -0700421 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
422 // Width mismatch - don't try to reuse.
423 reg = RegStorage::InvalidReg();
424 }
425 }
426 if (reg.Valid()) {
427 if (reg.IsPair()) {
428 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
429 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
430 if (info_low->IsTemp()) {
431 info_low->MarkInUse();
432 }
433 if (info_high->IsTemp()) {
434 info_high->MarkInUse();
435 }
436 } else {
buzbee091cc402014-03-31 10:14:40 -0700437 RegisterInfo* info = GetRegInfo(reg);
438 if (info->IsTemp()) {
439 info->MarkInUse();
440 }
441 }
buzbee30adc732014-05-09 15:10:18 -0700442 } else {
443 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
444 ClobberSReg(s_reg);
445 if (wide) {
446 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700447 }
448 }
449 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450}
451
buzbee2700f7e2014-03-07 09:46:20 -0800452void Mir2Lir::FreeTemp(RegStorage reg) {
453 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700454 FreeTemp(reg.GetLow());
455 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800456 } else {
buzbee091cc402014-03-31 10:14:40 -0700457 RegisterInfo* p = GetRegInfo(reg);
458 if (p->IsTemp()) {
459 p->MarkFree();
460 p->SetIsWide(false);
461 p->SetPartner(reg);
462 }
buzbee2700f7e2014-03-07 09:46:20 -0800463 }
464}
465
buzbee262b2992014-03-27 11:22:43 -0700466bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700467 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800468 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700469 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
470 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700471 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700472 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800473 } else {
buzbee091cc402014-03-31 10:14:40 -0700474 RegisterInfo* p = GetRegInfo(reg);
475 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800476 }
buzbee091cc402014-03-31 10:14:40 -0700477 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478}
479
buzbee262b2992014-03-27 11:22:43 -0700480bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700481 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800482 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700483 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
484 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
485 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800486 } else {
buzbee091cc402014-03-31 10:14:40 -0700487 RegisterInfo* p = GetRegInfo(reg);
488 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800489 }
buzbee091cc402014-03-31 10:14:40 -0700490 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491}
492
buzbee262b2992014-03-27 11:22:43 -0700493bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700494 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800495 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700496 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
497 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
498 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800499 } else {
buzbee091cc402014-03-31 10:14:40 -0700500 RegisterInfo* p = GetRegInfo(reg);
501 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800502 }
buzbee091cc402014-03-31 10:14:40 -0700503 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504}
505
buzbee2700f7e2014-03-07 09:46:20 -0800506bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700507 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800508 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700509 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
510 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
511 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800512 } else {
buzbee091cc402014-03-31 10:14:40 -0700513 RegisterInfo* p = GetRegInfo(reg);
514 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800515 }
buzbee091cc402014-03-31 10:14:40 -0700516 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800517}
518
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519/*
520 * Similar to AllocTemp(), but forces the allocation of a specific
521 * register. No check is made to see if the register was previously
522 * allocated. Use with caution.
523 */
buzbee2700f7e2014-03-07 09:46:20 -0800524void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700525 DCHECK(IsTemp(reg));
526 if (reg.IsPair()) {
527 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
528 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
529 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700530 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700531 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700532 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700533 } else {
534 RegisterInfo* p = GetRegInfo(reg);
535 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700536 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700537 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538}
539
buzbee2700f7e2014-03-07 09:46:20 -0800540void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700541 if (reg.IsPair()) {
542 GetRegInfo(reg.GetLow())->ResetDefBody();
543 GetRegInfo(reg.GetHigh())->ResetDefBody();
544 } else {
545 GetRegInfo(reg)->ResetDefBody();
546 }
buzbee2700f7e2014-03-07 09:46:20 -0800547}
548
buzbee091cc402014-03-31 10:14:40 -0700549void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
550 RegisterInfo* info = nullptr;
551 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
552 if (IsTemp(rs)) {
553 info = GetRegInfo(reg);
554 }
555 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
556 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
557 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700559 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 break;
buzbee091cc402014-03-31 10:14:40 -0700561 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 }
563 }
564}
565
566/*
567 * Mark the beginning and end LIR of a def sequence. Note that
568 * on entry start points to the LIR prior to the beginning of the
569 * sequence.
570 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700571void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 DCHECK(!rl.wide);
573 DCHECK(start && start->next);
574 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700575 RegisterInfo* p = GetRegInfo(rl.reg);
576 p->SetDefStart(start->next);
577 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578}
579
580/*
581 * Mark the beginning and end LIR of a def sequence. Note that
582 * on entry start points to the LIR prior to the beginning of the
583 * sequence.
584 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700585void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586 DCHECK(rl.wide);
587 DCHECK(start && start->next);
588 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700589 RegisterInfo* p;
590 if (rl.reg.IsPair()) {
591 p = GetRegInfo(rl.reg.GetLow());
592 ResetDef(rl.reg.GetHigh()); // Only track low of pair
593 } else {
594 p = GetRegInfo(rl.reg);
595 }
596 p->SetDefStart(start->next);
597 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598}
599
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700600RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 DCHECK(rl.wide);
602 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700603 if (rl.reg.IsPair()) {
604 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
605 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
606 if (info_lo->IsTemp()) {
607 info_lo->SetIsWide(false);
608 info_lo->ResetDefBody();
609 }
610 if (info_hi->IsTemp()) {
611 info_hi->SetIsWide(false);
612 info_hi->ResetDefBody();
613 }
614 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700615 } else {
616 /*
617 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
618 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
619 * this code. Will probably want to make this a virtual function.
620 */
621 // Can't narrow 64-bit register. Clobber.
622 if (GetRegInfo(rl.reg)->IsTemp()) {
623 Clobber(rl.reg);
624 FreeTemp(rl.reg);
625 }
626 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 }
629 rl.wide = false;
630 return rl;
631}
632
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700633void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700635 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
636 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
buzbee091cc402014-03-31 10:14:40 -0700638 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639}
640
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700641void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700643 // If pair, only track low reg of pair.
644 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
645 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
646 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 }
buzbee091cc402014-03-31 10:14:40 -0700648 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649}
650
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700651void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700652 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
653 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
654 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
buzbee091cc402014-03-31 10:14:40 -0700656 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
657 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
658 info->ResetDefBody();
659 }
660 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
661 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
662 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 }
664}
665
buzbeeba574512014-05-12 15:13:16 -0700666void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700667 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
668 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700669 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700670 }
671}
672
673void Mir2Lir::FlushRegWide(RegStorage reg) {
674 if (reg.IsPair()) {
675 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
676 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
677 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
678 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
679 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
680 if (!(info1->IsTemp() && info2->IsTemp())) {
681 /* Should not happen. If it does, there's a problem in eval_loc */
682 LOG(FATAL) << "Long half-temp, half-promoted";
683 }
684
685 info1->SetIsDirty(false);
686 info2->SetIsDirty(false);
687 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
688 info1 = info2;
689 }
690 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100691 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700692 }
693 } else {
694 RegisterInfo* info = GetRegInfo(reg);
695 if (info->IsLive() && info->IsDirty()) {
696 info->SetIsDirty(false);
697 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100698 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700699 }
700 }
701}
702
703void Mir2Lir::FlushReg(RegStorage reg) {
704 DCHECK(!reg.IsPair());
705 RegisterInfo* info = GetRegInfo(reg);
706 if (info->IsLive() && info->IsDirty()) {
707 info->SetIsDirty(false);
708 int v_reg = mir_graph_->SRegToVReg(info->SReg());
709 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 }
711}
712
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800713void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700714 if (info->IsWide()) {
715 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800716 } else {
buzbee091cc402014-03-31 10:14:40 -0700717 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 }
719}
720
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700721void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700722 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
723 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700724 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700725 FlushSpecificReg(info);
726 }
buzbee30adc732014-05-09 15:10:18 -0700727 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700728 info->SetSReg(INVALID_SREG);
729 info->ResetDefBody();
730 info->SetIsWide(false);
731 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732}
733
734
buzbee2700f7e2014-03-07 09:46:20 -0800735bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 if (reg_class == kAnyReg) {
737 return true;
738 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700739 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700740 } else {
buzbee091cc402014-03-31 10:14:40 -0700741 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 }
743}
744
buzbee091cc402014-03-31 10:14:40 -0700745void Mir2Lir::MarkLiveReg(RegStorage reg, int s_reg) {
746 RegisterInfo* info = GetRegInfo(reg);
747 if ((info->SReg() == s_reg) && info->IsLive()) {
748 return; // Already live.
749 }
750 if (s_reg != INVALID_SREG) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700751 ClobberSReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700752 if (info->IsTemp()) {
buzbee30adc732014-05-09 15:10:18 -0700753 info->MarkLive();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 }
755 } else {
buzbee091cc402014-03-31 10:14:40 -0700756 // Can't be live if no associated s_reg.
757 DCHECK(info->IsTemp());
buzbee30adc732014-05-09 15:10:18 -0700758 info->MarkDead();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 }
buzbee091cc402014-03-31 10:14:40 -0700760 info->SetSReg(s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761}
762
buzbee091cc402014-03-31 10:14:40 -0700763void Mir2Lir::MarkLive(RegLocation loc) {
764 RegStorage reg = loc.reg;
765 int s_reg = loc.s_reg_low;
766 if (reg.IsPair()) {
767 MarkLiveReg(reg.GetLow(), s_reg);
768 MarkLiveReg(reg.GetHigh(), s_reg+1);
769 } else {
770 if (loc.wide) {
771 ClobberSReg(s_reg + 1);
772 }
773 MarkLiveReg(reg, s_reg);
774 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700775}
776
buzbee2700f7e2014-03-07 09:46:20 -0800777void Mir2Lir::MarkTemp(RegStorage reg) {
778 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700780 tempreg_info_.Insert(info);
781 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700782}
783
buzbee2700f7e2014-03-07 09:46:20 -0800784void Mir2Lir::UnmarkTemp(RegStorage reg) {
785 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700786 RegisterInfo* info = GetRegInfo(reg);
787 tempreg_info_.Delete(info);
788 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800789}
790
buzbee091cc402014-03-31 10:14:40 -0700791void Mir2Lir::MarkWide(RegStorage reg) {
792 if (reg.IsPair()) {
793 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
794 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
795 info_lo->SetIsWide(true);
796 info_hi->SetIsWide(true);
797 info_lo->SetPartner(reg.GetHigh());
798 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800799 } else {
buzbee091cc402014-03-31 10:14:40 -0700800 RegisterInfo* info = GetRegInfo(reg);
801 info->SetIsWide(true);
802 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 }
804}
805
buzbee091cc402014-03-31 10:14:40 -0700806void Mir2Lir::MarkClean(RegLocation loc) {
807 if (loc.reg.IsPair()) {
808 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
809 info->SetIsDirty(false);
810 info = GetRegInfo(loc.reg.GetHigh());
811 info->SetIsDirty(false);
812 } else {
813 RegisterInfo* info = GetRegInfo(loc.reg);
814 info->SetIsDirty(false);
815 }
816}
817
818// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700819void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 if (loc.home) {
821 // If already home, can't be dirty
822 return;
823 }
buzbee091cc402014-03-31 10:14:40 -0700824 if (loc.reg.IsPair()) {
825 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
826 info->SetIsDirty(true);
827 info = GetRegInfo(loc.reg.GetHigh());
828 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800829 } else {
buzbee091cc402014-03-31 10:14:40 -0700830 RegisterInfo* info = GetRegInfo(loc.reg);
831 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 }
833}
834
buzbee2700f7e2014-03-07 09:46:20 -0800835void Mir2Lir::MarkInUse(RegStorage reg) {
836 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700837 GetRegInfo(reg.GetLow())->MarkInUse();
838 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800839 } else {
buzbee091cc402014-03-31 10:14:40 -0700840 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800841 }
842}
843
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700844bool Mir2Lir::CheckCorePoolSanity() {
buzbee091cc402014-03-31 10:14:40 -0700845 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
846 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
847 RegStorage my_reg = info->GetReg();
848 if (info->IsWide() && my_reg.IsPair()) {
849 int my_sreg = info->SReg();
850 RegStorage partner_reg = info->Partner();
851 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700852 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700853 DCHECK(partner->IsWide());
854 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
855 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700856 if (my_sreg == INVALID_SREG) {
857 DCHECK_EQ(partner_sreg, INVALID_SREG);
858 } else {
859 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700860 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700861 }
buzbee091cc402014-03-31 10:14:40 -0700862 } else {
863 // TODO: add whatever sanity checks might be useful for 64BitSolo regs here.
864 // TODO: sanity checks for floating point pools?
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700865 }
buzbee091cc402014-03-31 10:14:40 -0700866 if (!info->IsLive()) {
867 DCHECK(info->DefStart() == NULL);
868 DCHECK(info->DefEnd() == NULL);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700869 }
870 }
871 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872}
873
874/*
875 * Return an updated location record with current in-register status.
876 * If the value lives in live temps, reflect that fact. No code
877 * is generated. If the live value is part of an older pair,
878 * clobber both low and high.
879 * TUNING: clobbering both is a bit heavy-handed, but the alternative
880 * is a bit complex when dealing with FP regs. Examine code to see
881 * if it's worthwhile trying to be more clever here.
882 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700883RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 DCHECK(!loc.wide);
885 DCHECK(CheckCorePoolSanity());
886 if (loc.location != kLocPhysReg) {
887 DCHECK((loc.location == kLocDalvikFrame) ||
888 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700889 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
890 if (reg.Valid()) {
891 bool match = true;
892 RegisterInfo* info = GetRegInfo(reg);
893 match &= !reg.IsPair();
894 match &= !info->IsWide();
895 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700896 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700897 loc.reg = reg;
898 } else {
899 Clobber(reg);
900 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 }
902 }
903 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 return loc;
905}
906
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700907RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 DCHECK(loc.wide);
909 DCHECK(CheckCorePoolSanity());
910 if (loc.location != kLocPhysReg) {
911 DCHECK((loc.location == kLocDalvikFrame) ||
912 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700913 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
914 if (reg.Valid()) {
915 bool match = true;
916 if (reg.IsPair()) {
917 // If we've got a register pair, make sure that it was last used as the same pair.
918 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
919 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
920 match &= info_lo->IsWide();
921 match &= info_hi->IsWide();
922 match &= (info_lo->Partner() == info_hi->GetReg());
923 match &= (info_hi->Partner() == info_lo->GetReg());
924 } else {
925 RegisterInfo* info = GetRegInfo(reg);
926 match &= info->IsWide();
927 match &= (info->GetReg() == info->Partner());
928 }
929 if (match) {
930 loc.location = kLocPhysReg;
931 loc.reg = reg;
932 } else {
933 Clobber(reg);
934 FreeTemp(reg);
935 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936 }
937 }
938 return loc;
939}
940
Brian Carlstrom7940e442013-07-12 13:46:57 -0700941/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700942RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 if (loc.wide)
944 return UpdateLocWide(loc);
945 else
946 return UpdateLoc(loc);
947}
948
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700949RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700951
952 loc = UpdateLocWide(loc);
953
954 /* If already in registers, we can assume proper form. Right reg class? */
955 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800956 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +0100957 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000958 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -0700959 // Associate the old sreg with the new register and clobber the old register.
960 GetRegInfo(new_regs)->SetSReg(GetRegInfo(loc.reg)->SReg());
buzbee2700f7e2014-03-07 09:46:20 -0800961 Clobber(loc.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000962 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -0700963 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 }
965 return loc;
966 }
967
968 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
969 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
970
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000971 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -0700972 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 if (update) {
975 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700976 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700977 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 return loc;
979}
980
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700981RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -0700982 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -0700984 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985
986 loc = UpdateLoc(loc);
987
988 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800989 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +0100990 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -0800991 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -0700992 // Associate the old sreg with the new register and clobber the old register.
993 GetRegInfo(new_reg)->SetSReg(GetRegInfo(loc.reg)->SReg());
buzbee2700f7e2014-03-07 09:46:20 -0800994 Clobber(loc.reg);
995 loc.reg = new_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 }
997 return loc;
998 }
999
1000 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1001
buzbee2700f7e2014-03-07 09:46:20 -08001002 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003
1004 if (update) {
1005 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001006 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 }
1008 return loc;
1009}
1010
1011/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001012void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1014 RegLocation loc = mir_graph_->reg_location_[i];
1015 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1016 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001017 if (loc.fp) {
1018 if (loc.wide) {
1019 // Treat doubles as a unit, using upper half of fp_counts array.
1020 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1021 i++;
1022 } else {
1023 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1024 }
1025 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1027 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 }
1029}
1030
1031/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001032static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001033 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1034 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001035 // Note that we fall back to sorting on reg so we get stable output
1036 // on differing qsort implementations (such as on host and target or
1037 // between local host and build servers).
1038 return (op1->count == op2->count)
1039 ? (op1->s_reg - op2->s_reg)
1040 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041}
1042
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001043void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 LOG(INFO) << msg;
1045 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001046 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1047 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1048 } else {
1049 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1050 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051 }
1052}
1053
1054/*
1055 * Note: some portions of this code required even if the kPromoteRegs
1056 * optimization is disabled.
1057 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001058void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001059 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001060 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001062 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1063 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001064 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065
1066 // Allow target code to add any special registers
1067 AdjustSpillMask();
1068
1069 /*
1070 * Simple register promotion. Just do a static count of the uses
1071 * of Dalvik registers. Note that we examine the SSA names, but
1072 * count based on original Dalvik register name. Count refs
1073 * separately based on type in order to give allocation
1074 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001075 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 * reg.
1077 * TUNING: replace with linear scan once we have the ability
1078 * to describe register live ranges for GC.
1079 */
1080 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001081 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001082 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001084 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001085 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001086 // Set ssa names for original Dalvik registers
1087 for (int i = 0; i < dalvik_regs; i++) {
1088 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1089 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001090
1091 // Set ssa names for compiler temporaries
1092 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1093 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1094 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1095 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1096 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001097 }
1098
1099 // Duplicate in upper half to represent possible fp double starting sregs.
1100 for (int i = 0; i < num_regs; i++) {
1101 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 }
1103
1104 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001105 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106
Brian Carlstrom7940e442013-07-12 13:46:57 -07001107
1108 // Sort the count arrays
1109 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001110 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111
1112 if (cu_->verbose) {
1113 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001114 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115 }
1116
1117 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1118 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001119 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1120 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1121 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1122 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1123 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1124 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1125 // Ignore result - if can't alloc double may still be able to alloc singles.
1126 AllocPreservedDouble(low_sreg);
1127 }
1128 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001129 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1130 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001131 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 }
1133 }
1134 }
1135
1136 // Promote core regs
1137 for (int i = 0; (i < num_regs) &&
1138 (core_regs[i].count >= promotion_threshold); i++) {
1139 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1140 if (promotion_map_[p_map_idx].core_location !=
1141 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001142 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1143 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 break; // No more left
1145 }
1146 }
1147 }
1148 }
1149
1150 // Now, update SSA names to new home locations
1151 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1152 RegLocation *curr = &mir_graph_->reg_location_[i];
1153 int p_map_idx = SRegToPMap(curr->s_reg_low);
1154 if (!curr->wide) {
1155 if (curr->fp) {
1156 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1157 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001158 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001159 curr->home = true;
1160 }
1161 } else {
1162 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1163 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001164 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 curr->home = true;
1166 }
1167 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001168 } else {
1169 if (curr->high_word) {
1170 continue;
1171 }
1172 if (curr->fp) {
1173 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001174 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001175 int low_reg = promotion_map_[p_map_idx].FpReg;
1176 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1177 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001178 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1180 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001181 if (cu_->instruction_set == kThumb2) {
1182 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1183 } else {
1184 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1185 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001186 curr->home = true;
1187 }
1188 }
1189 } else {
1190 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1191 && (promotion_map_[p_map_idx+1].core_location ==
1192 kLocPhysReg)) {
1193 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001194 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1195 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196 curr->home = true;
1197 }
1198 }
1199 }
1200 }
1201 if (cu_->verbose) {
1202 DumpPromotionMap();
1203 }
1204}
1205
1206/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001207int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001209 fp_spill_mask_, frame_size_, v_reg,
1210 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001211}
1212
1213/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001214int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001215 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1216}
1217
1218/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001219RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001220 RegLocation gpr_res = LocCReturnWide();
1221 RegLocation fpr_res = LocCReturnDouble();
1222 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001223 if (res.reg.IsPair()) {
1224 Clobber(res.reg);
1225 LockTemp(res.reg);
1226 // Does this wide value live in two registers or one vector register?
1227 if (res.reg.GetLowReg() != res.reg.GetHighReg()) {
1228 // FIXME: I think we want to mark these as wide as well.
1229 MarkWide(res.reg);
1230 }
1231 } else {
1232 Clobber(res.reg);
1233 LockTemp(res.reg);
1234 MarkWide(res.reg);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001235 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001236 return res;
1237}
1238
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001239RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 RegLocation gpr_res = LocCReturn();
1241 RegLocation fpr_res = LocCReturnFloat();
1242 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001243 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001245 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001246 } else {
buzbee091cc402014-03-31 10:14:40 -07001247 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001248 }
1249 return res;
1250}
1251
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001252void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001253 DoPromotion();
1254
1255 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1256 LOG(INFO) << "After Promotion";
1257 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1258 }
1259
1260 /* Set the frame size */
1261 frame_size_ = ComputeFrameSize();
1262}
1263
1264/*
1265 * Get the "real" sreg number associated with an s_reg slot. In general,
1266 * s_reg values passed through codegen are the SSA names created by
1267 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1268 * array. However, renaming is accomplished by simply replacing RegLocation
1269 * entries in the reglocation[] array. Therefore, when location
1270 * records for operands are first created, we need to ask the locRecord
1271 * identified by the dataflow pass what it's new name is.
1272 */
1273int Mir2Lir::GetSRegHi(int lowSreg) {
1274 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1275}
1276
buzbee091cc402014-03-31 10:14:40 -07001277bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001278 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001279 return true;
1280}
1281
Brian Carlstrom7940e442013-07-12 13:46:57 -07001282} // namespace art