blob: 06d05e2274eb2f1d195958e51f70f1e643a86636 [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()) {
buzbee082833c2014-05-17 23:16:26 -0700155 if (info->GetReg() != info->Partner()) {
156 ClobberBody(GetRegInfo(info->Partner()));
157 }
buzbeeba574512014-05-12 15:13:16 -0700158 ClobberBody(info);
159 if (info->IsAliased()) {
160 ClobberAliases(info);
161 } else {
162 RegisterInfo* master = info->Master();
163 if (info != master) {
164 ClobberBody(info->Master());
165 }
166 }
buzbee30adc732014-05-09 15:10:18 -0700167 }
buzbee2700f7e2014-03-07 09:46:20 -0800168 }
169}
170
buzbee30adc732014-05-09 15:10:18 -0700171void Mir2Lir::ClobberAliases(RegisterInfo* info) {
buzbeeba574512014-05-12 15:13:16 -0700172 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
173 alias = alias->GetAliasChain()) {
174 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee082833c2014-05-17 23:16:26 -0700175 ClobberBody(alias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 }
177}
178
179/*
180 * Break the association between a Dalvik vreg and a physical temp register of either register
181 * class.
182 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
183 * in the register utilities, is is also used by code gen routines to work around a deficiency in
184 * local register allocation, which fails to distinguish between the "in" and "out" identities
185 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
186 * is used both as the source and destination register of an operation in which the type
187 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
188 * addressed.
189 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700190void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700191 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700192 if (kIsDebugBuild && s_reg == live_sreg_) {
193 live_sreg_ = INVALID_SREG;
194 }
195 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
196 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
197 if (info->SReg() == s_reg) {
buzbee082833c2014-05-17 23:16:26 -0700198 if (info->GetReg() != info->Partner()) {
199 // Dealing with a pair - clobber the other half.
200 DCHECK(!info->IsAliased());
201 ClobberBody(GetRegInfo(info->Partner()));
202 }
buzbeeba574512014-05-12 15:13:16 -0700203 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700204 if (info->IsAliased()) {
buzbee30adc732014-05-09 15:10:18 -0700205 ClobberAliases(info);
206 }
buzbee091cc402014-03-31 10:14:40 -0700207 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 }
209 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210}
211
212/*
213 * SSA names associated with the initial definitions of Dalvik
214 * registers are the same as the Dalvik register number (and
215 * thus take the same position in the promotion_map. However,
216 * the special Method* and compiler temp resisters use negative
217 * v_reg numbers to distinguish them and can have an arbitrary
218 * ssa name (above the last original Dalvik register). This function
219 * maps SSA names to positions in the promotion_map array.
220 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700221int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
223 DCHECK_GE(s_reg, 0);
224 int v_reg = mir_graph_->SRegToVReg(s_reg);
225 if (v_reg >= 0) {
226 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
227 return v_reg;
228 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800229 /*
230 * It must be the case that the v_reg for temporary is less than or equal to the
231 * base reg for temps. For that reason, "position" must be zero or positive.
232 */
233 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
234
235 // The temporaries are placed after dalvik registers in the promotion map
236 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
237 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 }
239}
240
buzbee091cc402014-03-31 10:14:40 -0700241// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800242void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 int p_map_idx = SRegToPMap(s_reg);
244 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700245 int reg_num = reg.GetRegNum();
246 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800247 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800249 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 num_core_spills_++;
251 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800252 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253}
254
buzbee091cc402014-03-31 10:14:40 -0700255/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800256RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
257 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700258 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
259 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
260 if (!info->IsTemp() && !info->InUse()) {
261 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 RecordCorePromotion(res, s_reg);
263 break;
264 }
265 }
266 return res;
267}
268
buzbee091cc402014-03-31 10:14:40 -0700269void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 int p_map_idx = SRegToPMap(s_reg);
271 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700272 GetRegInfo(reg)->MarkInUse();
273 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700275 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276}
277
buzbee091cc402014-03-31 10:14:40 -0700278// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800279RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
280 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700281 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
282 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
283 if (!info->IsTemp() && !info->InUse()) {
284 res = info->GetReg();
285 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 break;
287 }
288 }
289 return res;
290}
291
buzbee091cc402014-03-31 10:14:40 -0700292void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
293 int p_map_idx = SRegToPMap(s_reg);
294 int v_reg = mir_graph_->SRegToVReg(s_reg);
295 GetRegInfo(reg)->MarkInUse();
296 MarkPreservedDouble(v_reg, reg);
297 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
298 promotion_map_[p_map_idx].FpReg = reg.GetReg();
299}
300
301// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800302RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
303 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700304 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
305 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
306 if (!info->IsTemp() && !info->InUse()) {
307 res = info->GetReg();
308 RecordDoublePromotion(res, s_reg);
309 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
312 return res;
313}
314
buzbee091cc402014-03-31 10:14:40 -0700315
316RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
317 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700319 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 if (next >= num_regs)
321 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700322 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700323 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700324 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee091cc402014-03-31 10:14:40 -0700325 Clobber(info->GetReg());
326 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700327 /*
328 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
329 * The caller will set wideness as appropriate.
330 */
buzbee091cc402014-03-31 10:14:40 -0700331 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700333 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 }
335 next++;
336 }
337 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700338 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700339 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 if (next >= num_regs)
341 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700342 RegisterInfo* info = regs.Get(next);
343 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700344 // Got one. Kill it.
345 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700346 Clobber(info->GetReg());
347 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700348 if (info->IsWide()) {
349 RegisterInfo* partner = GetRegInfo(info->Partner());
350 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
351 DCHECK(partner->IsWide());
352 info->SetIsWide(false);
353 partner->SetIsWide(false);
354 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700356 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 }
358 next++;
359 }
360 if (required) {
361 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700362 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 LOG(FATAL) << "No free temp registers";
364 }
buzbee2700f7e2014-03-07 09:46:20 -0800365 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366}
367
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800369RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700370 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371}
372
buzbee2700f7e2014-03-07 09:46:20 -0800373RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700374 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375}
376
buzbee091cc402014-03-31 10:14:40 -0700377RegStorage Mir2Lir::AllocTempSingle() {
378 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
379 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
380 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381}
382
buzbee091cc402014-03-31 10:14:40 -0700383RegStorage Mir2Lir::AllocTempDouble() {
384 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
385 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
386 return res;
387}
388
389RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
390 RegStorage res;
391 GrowableArray<RegisterInfo*>::Iterator it(&regs);
392 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
393 if ((info->SReg() == s_reg) && info->IsLive()) {
394 res = info->GetReg();
395 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 }
397 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700398 return res;
399}
400
buzbee091cc402014-03-31 10:14:40 -0700401RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
402 RegStorage reg;
403 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
404 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
405 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 }
buzbee091cc402014-03-31 10:14:40 -0700407 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee30adc732014-05-09 15:10:18 -0700408 // TODO: add 64-bit core pool similar to above.
buzbee091cc402014-03-31 10:14:40 -0700409 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
410 }
411 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700412 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
413 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700414 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
415 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700416 reg = RegStorage::MakeRegPair(reg, high_reg);
417 MarkWide(reg);
418 } else {
buzbee30adc732014-05-09 15:10:18 -0700419 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700420 reg = RegStorage::InvalidReg();
421 }
422 }
buzbee30adc732014-05-09 15:10:18 -0700423 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
424 // Width mismatch - don't try to reuse.
425 reg = RegStorage::InvalidReg();
426 }
427 }
428 if (reg.Valid()) {
429 if (reg.IsPair()) {
430 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
431 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
432 if (info_low->IsTemp()) {
433 info_low->MarkInUse();
434 }
435 if (info_high->IsTemp()) {
436 info_high->MarkInUse();
437 }
438 } else {
buzbee091cc402014-03-31 10:14:40 -0700439 RegisterInfo* info = GetRegInfo(reg);
440 if (info->IsTemp()) {
441 info->MarkInUse();
442 }
443 }
buzbee30adc732014-05-09 15:10:18 -0700444 } else {
445 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
446 ClobberSReg(s_reg);
447 if (wide) {
448 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700449 }
450 }
451 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452}
453
buzbee2700f7e2014-03-07 09:46:20 -0800454void Mir2Lir::FreeTemp(RegStorage reg) {
455 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700456 FreeTemp(reg.GetLow());
457 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800458 } else {
buzbee091cc402014-03-31 10:14:40 -0700459 RegisterInfo* p = GetRegInfo(reg);
460 if (p->IsTemp()) {
461 p->MarkFree();
462 p->SetIsWide(false);
463 p->SetPartner(reg);
464 }
buzbee2700f7e2014-03-07 09:46:20 -0800465 }
466}
467
buzbee082833c2014-05-17 23:16:26 -0700468void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
469 DCHECK(rl_keep.wide);
470 DCHECK(rl_free.wide);
471 int free_low = rl_free.reg.GetLowReg();
472 int free_high = rl_free.reg.GetHighReg();
473 int keep_low = rl_keep.reg.GetLowReg();
474 int keep_high = rl_keep.reg.GetHighReg();
475 if ((free_low != keep_low) && (free_low != keep_high) &&
476 (free_high != keep_low) && (free_high != keep_high)) {
477 // No overlap, free both
478 FreeTemp(rl_free.reg);
479 }
480}
481
buzbee262b2992014-03-27 11:22:43 -0700482bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700483 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800484 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700485 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
486 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700487 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700488 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800489 } else {
buzbee091cc402014-03-31 10:14:40 -0700490 RegisterInfo* p = GetRegInfo(reg);
491 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800492 }
buzbee091cc402014-03-31 10:14:40 -0700493 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494}
495
buzbee262b2992014-03-27 11:22:43 -0700496bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700497 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800498 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700499 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
500 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
501 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800502 } else {
buzbee091cc402014-03-31 10:14:40 -0700503 RegisterInfo* p = GetRegInfo(reg);
504 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800505 }
buzbee091cc402014-03-31 10:14:40 -0700506 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507}
508
buzbee262b2992014-03-27 11:22:43 -0700509bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700510 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800511 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700512 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
513 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
514 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800515 } else {
buzbee091cc402014-03-31 10:14:40 -0700516 RegisterInfo* p = GetRegInfo(reg);
517 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800518 }
buzbee091cc402014-03-31 10:14:40 -0700519 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520}
521
buzbee2700f7e2014-03-07 09:46:20 -0800522bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700523 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800524 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700525 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
526 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
527 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800528 } else {
buzbee091cc402014-03-31 10:14:40 -0700529 RegisterInfo* p = GetRegInfo(reg);
530 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800531 }
buzbee091cc402014-03-31 10:14:40 -0700532 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800533}
534
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535/*
536 * Similar to AllocTemp(), but forces the allocation of a specific
537 * register. No check is made to see if the register was previously
538 * allocated. Use with caution.
539 */
buzbee2700f7e2014-03-07 09:46:20 -0800540void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700541 DCHECK(IsTemp(reg));
542 if (reg.IsPair()) {
543 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
544 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
545 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700546 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700547 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700548 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700549 } else {
550 RegisterInfo* p = GetRegInfo(reg);
551 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700552 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700553 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554}
555
buzbee2700f7e2014-03-07 09:46:20 -0800556void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700557 if (reg.IsPair()) {
558 GetRegInfo(reg.GetLow())->ResetDefBody();
559 GetRegInfo(reg.GetHigh())->ResetDefBody();
560 } else {
561 GetRegInfo(reg)->ResetDefBody();
562 }
buzbee2700f7e2014-03-07 09:46:20 -0800563}
564
buzbee091cc402014-03-31 10:14:40 -0700565void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
566 RegisterInfo* info = nullptr;
567 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
568 if (IsTemp(rs)) {
569 info = GetRegInfo(reg);
570 }
571 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
572 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
573 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700575 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 break;
buzbee091cc402014-03-31 10:14:40 -0700577 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578 }
579 }
580}
581
582/*
583 * Mark the beginning and end LIR of a def sequence. Note that
584 * on entry start points to the LIR prior to the beginning of the
585 * sequence.
586 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700587void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 DCHECK(!rl.wide);
589 DCHECK(start && start->next);
590 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700591 RegisterInfo* p = GetRegInfo(rl.reg);
592 p->SetDefStart(start->next);
593 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594}
595
596/*
597 * Mark the beginning and end LIR of a def sequence. Note that
598 * on entry start points to the LIR prior to the beginning of the
599 * sequence.
600 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700601void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 DCHECK(rl.wide);
603 DCHECK(start && start->next);
604 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700605 RegisterInfo* p;
606 if (rl.reg.IsPair()) {
607 p = GetRegInfo(rl.reg.GetLow());
608 ResetDef(rl.reg.GetHigh()); // Only track low of pair
609 } else {
610 p = GetRegInfo(rl.reg);
611 }
612 p->SetDefStart(start->next);
613 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614}
615
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700616RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 DCHECK(rl.wide);
618 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700619 if (rl.reg.IsPair()) {
620 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
621 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
622 if (info_lo->IsTemp()) {
623 info_lo->SetIsWide(false);
624 info_lo->ResetDefBody();
625 }
626 if (info_hi->IsTemp()) {
627 info_hi->SetIsWide(false);
628 info_hi->ResetDefBody();
629 }
630 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700631 } else {
632 /*
633 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
634 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
635 * this code. Will probably want to make this a virtual function.
636 */
637 // Can't narrow 64-bit register. Clobber.
638 if (GetRegInfo(rl.reg)->IsTemp()) {
639 Clobber(rl.reg);
640 FreeTemp(rl.reg);
641 }
642 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 }
645 rl.wide = false;
646 return rl;
647}
648
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700649void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700651 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
652 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 }
buzbee091cc402014-03-31 10:14:40 -0700654 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655}
656
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700657void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700658 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700659 // If pair, only track low reg of pair.
660 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
661 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
662 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 }
buzbee091cc402014-03-31 10:14:40 -0700664 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665}
666
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700667void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700668 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
669 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
670 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 }
buzbee091cc402014-03-31 10:14:40 -0700672 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
673 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
674 info->ResetDefBody();
675 }
676 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
677 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
678 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 }
680}
681
buzbeeba574512014-05-12 15:13:16 -0700682void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700683 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
684 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700685 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700686 }
687}
688
689void Mir2Lir::FlushRegWide(RegStorage reg) {
690 if (reg.IsPair()) {
691 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
692 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
693 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
694 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
695 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
696 if (!(info1->IsTemp() && info2->IsTemp())) {
697 /* Should not happen. If it does, there's a problem in eval_loc */
698 LOG(FATAL) << "Long half-temp, half-promoted";
699 }
700
701 info1->SetIsDirty(false);
702 info2->SetIsDirty(false);
703 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
704 info1 = info2;
705 }
706 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100707 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700708 }
709 } else {
710 RegisterInfo* info = GetRegInfo(reg);
711 if (info->IsLive() && info->IsDirty()) {
712 info->SetIsDirty(false);
713 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100714 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700715 }
716 }
717}
718
719void Mir2Lir::FlushReg(RegStorage reg) {
720 DCHECK(!reg.IsPair());
721 RegisterInfo* info = GetRegInfo(reg);
722 if (info->IsLive() && info->IsDirty()) {
723 info->SetIsDirty(false);
724 int v_reg = mir_graph_->SRegToVReg(info->SReg());
725 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700726 }
727}
728
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800729void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700730 if (info->IsWide()) {
731 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800732 } else {
buzbee091cc402014-03-31 10:14:40 -0700733 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 }
735}
736
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700737void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700738 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
739 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700740 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700741 FlushSpecificReg(info);
742 }
buzbee30adc732014-05-09 15:10:18 -0700743 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700744 info->SetIsWide(false);
745 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746}
747
748
buzbee2700f7e2014-03-07 09:46:20 -0800749bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 if (reg_class == kAnyReg) {
751 return true;
752 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700753 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 } else {
buzbee091cc402014-03-31 10:14:40 -0700755 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700756 }
757}
758
buzbee091cc402014-03-31 10:14:40 -0700759void Mir2Lir::MarkLive(RegLocation loc) {
760 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700761 if (!IsTemp(reg)) {
762 return;
763 }
buzbee091cc402014-03-31 10:14:40 -0700764 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700765 if (s_reg == INVALID_SREG) {
766 // Can't be live if no associated sreg.
767 if (reg.IsPair()) {
768 GetRegInfo(reg.GetLow())->MarkDead();
769 GetRegInfo(reg.GetHigh())->MarkDead();
770 } else {
771 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700772 }
buzbee082833c2014-05-17 23:16:26 -0700773 } else {
774 if (reg.IsPair()) {
775 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
776 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
777 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
778 (info_hi->SReg() == s_reg)) {
779 return; // Already live.
780 }
781 ClobberSReg(s_reg);
782 ClobberSReg(s_reg + 1);
783 info_lo->MarkLive(s_reg);
784 info_hi->MarkLive(s_reg + 1);
785 } else {
786 RegisterInfo* info = GetRegInfo(reg);
787 if (info->IsLive() && (info->SReg() == s_reg)) {
788 return; // Already live.
789 }
790 ClobberSReg(s_reg);
791 if (loc.wide) {
792 ClobberSReg(s_reg + 1);
793 }
794 info->MarkLive(s_reg);
795 }
796 if (loc.wide) {
797 MarkWide(reg);
798 } else {
799 MarkNarrow(reg);
800 }
buzbee091cc402014-03-31 10:14:40 -0700801 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802}
803
buzbee2700f7e2014-03-07 09:46:20 -0800804void Mir2Lir::MarkTemp(RegStorage reg) {
805 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700807 tempreg_info_.Insert(info);
808 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809}
810
buzbee2700f7e2014-03-07 09:46:20 -0800811void Mir2Lir::UnmarkTemp(RegStorage reg) {
812 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700813 RegisterInfo* info = GetRegInfo(reg);
814 tempreg_info_.Delete(info);
815 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800816}
817
buzbee091cc402014-03-31 10:14:40 -0700818void Mir2Lir::MarkWide(RegStorage reg) {
819 if (reg.IsPair()) {
820 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
821 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700822 // Unpair any old partners.
823 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
824 GetRegInfo(info_lo->Partner())->SetIsWide(false);
825 }
826 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
827 GetRegInfo(info_hi->Partner())->SetIsWide(false);
828 }
buzbee091cc402014-03-31 10:14:40 -0700829 info_lo->SetIsWide(true);
830 info_hi->SetIsWide(true);
831 info_lo->SetPartner(reg.GetHigh());
832 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800833 } else {
buzbee091cc402014-03-31 10:14:40 -0700834 RegisterInfo* info = GetRegInfo(reg);
835 info->SetIsWide(true);
836 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 }
838}
839
buzbee082833c2014-05-17 23:16:26 -0700840void Mir2Lir::MarkNarrow(RegStorage reg) {
841 DCHECK(!reg.IsPair());
842 RegisterInfo* info = GetRegInfo(reg);
843 info->SetIsWide(false);
844 info->SetPartner(reg);
845}
846
buzbee091cc402014-03-31 10:14:40 -0700847void Mir2Lir::MarkClean(RegLocation loc) {
848 if (loc.reg.IsPair()) {
849 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
850 info->SetIsDirty(false);
851 info = GetRegInfo(loc.reg.GetHigh());
852 info->SetIsDirty(false);
853 } else {
854 RegisterInfo* info = GetRegInfo(loc.reg);
855 info->SetIsDirty(false);
856 }
857}
858
859// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700860void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700861 if (loc.home) {
862 // If already home, can't be dirty
863 return;
864 }
buzbee091cc402014-03-31 10:14:40 -0700865 if (loc.reg.IsPair()) {
866 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
867 info->SetIsDirty(true);
868 info = GetRegInfo(loc.reg.GetHigh());
869 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800870 } else {
buzbee091cc402014-03-31 10:14:40 -0700871 RegisterInfo* info = GetRegInfo(loc.reg);
872 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873 }
874}
875
buzbee2700f7e2014-03-07 09:46:20 -0800876void Mir2Lir::MarkInUse(RegStorage reg) {
877 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700878 GetRegInfo(reg.GetLow())->MarkInUse();
879 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800880 } else {
buzbee091cc402014-03-31 10:14:40 -0700881 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800882 }
883}
884
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700885bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700886 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700887 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700888 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
889 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700890 int my_sreg = info->SReg();
891 RegStorage partner_reg = info->Partner();
892 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700893 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700894 DCHECK(partner->IsWide());
895 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700896 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700897 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700898 if (my_sreg == INVALID_SREG) {
899 DCHECK_EQ(partner_sreg, INVALID_SREG);
900 } else {
901 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700902 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700903 }
904 }
buzbee082833c2014-05-17 23:16:26 -0700905 if (info->Master() != info) {
906 // Aliased.
907 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
908 // If I'm live, master should not be live, but should show liveness in alias set.
909 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
910 DCHECK(!info->Master()->IsDead());
911 } else if (!info->IsDead()) {
912 // If I'm not live, but there is liveness in the set master must be live.
913 DCHECK_EQ(info->SReg(), INVALID_SREG);
914 DCHECK(info->Master()->IsLive());
915 }
916 }
917 if (info->IsAliased()) {
918 // Has child aliases.
919 DCHECK_EQ(info->Master(), info);
920 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
921 // Master live, no child should be dead - all should show liveness in set.
922 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
923 DCHECK(!p->IsDead());
924 DCHECK_EQ(p->SReg(), INVALID_SREG);
925 }
926 } else if (!info->IsDead()) {
927 // Master not live, one or more aliases must be.
928 bool live_alias = false;
929 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
930 live_alias |= p->IsLive();
931 }
932 DCHECK(live_alias);
933 }
934 }
935 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
936 // If not fully live, should have INVALID_SREG and def's should be null.
937 DCHECK(info->DefStart() == nullptr);
938 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700939 }
940 }
941 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942}
943
944/*
945 * Return an updated location record with current in-register status.
946 * If the value lives in live temps, reflect that fact. No code
947 * is generated. If the live value is part of an older pair,
948 * clobber both low and high.
949 * TUNING: clobbering both is a bit heavy-handed, but the alternative
950 * is a bit complex when dealing with FP regs. Examine code to see
951 * if it's worthwhile trying to be more clever here.
952 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700953RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 DCHECK(!loc.wide);
955 DCHECK(CheckCorePoolSanity());
956 if (loc.location != kLocPhysReg) {
957 DCHECK((loc.location == kLocDalvikFrame) ||
958 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700959 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
960 if (reg.Valid()) {
961 bool match = true;
962 RegisterInfo* info = GetRegInfo(reg);
963 match &= !reg.IsPair();
964 match &= !info->IsWide();
965 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700966 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700967 loc.reg = reg;
968 } else {
969 Clobber(reg);
970 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 }
972 }
973 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 return loc;
975}
976
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700977RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 DCHECK(loc.wide);
979 DCHECK(CheckCorePoolSanity());
980 if (loc.location != kLocPhysReg) {
981 DCHECK((loc.location == kLocDalvikFrame) ||
982 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700983 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
984 if (reg.Valid()) {
985 bool match = true;
986 if (reg.IsPair()) {
987 // If we've got a register pair, make sure that it was last used as the same pair.
988 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
989 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
990 match &= info_lo->IsWide();
991 match &= info_hi->IsWide();
992 match &= (info_lo->Partner() == info_hi->GetReg());
993 match &= (info_hi->Partner() == info_lo->GetReg());
994 } else {
995 RegisterInfo* info = GetRegInfo(reg);
996 match &= info->IsWide();
997 match &= (info->GetReg() == info->Partner());
998 }
999 if (match) {
1000 loc.location = kLocPhysReg;
1001 loc.reg = reg;
1002 } else {
1003 Clobber(reg);
1004 FreeTemp(reg);
1005 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 }
1007 }
1008 return loc;
1009}
1010
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001012RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 if (loc.wide)
1014 return UpdateLocWide(loc);
1015 else
1016 return UpdateLoc(loc);
1017}
1018
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001019RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021
1022 loc = UpdateLocWide(loc);
1023
1024 /* If already in registers, we can assume proper form. Right reg class? */
1025 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001026 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001027 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001028 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001029 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001030 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001031 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001032 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001033 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001034 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
1036 return loc;
1037 }
1038
1039 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1040 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1041
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001042 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001043 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 if (update) {
1046 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001047 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001049 return loc;
1050}
1051
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001052RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001053 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001055 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056
1057 loc = UpdateLoc(loc);
1058
1059 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001060 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001061 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001062 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001063 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001064 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001065 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001066 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001067 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 }
1069 return loc;
1070 }
1071
1072 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1073
buzbee2700f7e2014-03-07 09:46:20 -08001074 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075
1076 if (update) {
1077 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001078 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
1080 return loc;
1081}
1082
1083/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001084void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1086 RegLocation loc = mir_graph_->reg_location_[i];
1087 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1088 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001089 if (loc.fp) {
1090 if (loc.wide) {
1091 // Treat doubles as a unit, using upper half of fp_counts array.
1092 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1093 i++;
1094 } else {
1095 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1096 }
1097 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1099 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 }
1101}
1102
1103/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001104static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001105 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1106 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001107 // Note that we fall back to sorting on reg so we get stable output
1108 // on differing qsort implementations (such as on host and target or
1109 // between local host and build servers).
1110 return (op1->count == op2->count)
1111 ? (op1->s_reg - op2->s_reg)
1112 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113}
1114
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001115void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 LOG(INFO) << msg;
1117 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001118 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1119 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1120 } else {
1121 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1122 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123 }
1124}
1125
1126/*
1127 * Note: some portions of this code required even if the kPromoteRegs
1128 * optimization is disabled.
1129 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001130void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001131 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001132 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001133 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001134 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1135 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001136 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137
1138 // Allow target code to add any special registers
1139 AdjustSpillMask();
1140
1141 /*
1142 * Simple register promotion. Just do a static count of the uses
1143 * of Dalvik registers. Note that we examine the SSA names, but
1144 * count based on original Dalvik register name. Count refs
1145 * separately based on type in order to give allocation
1146 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001147 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001148 * reg.
1149 * TUNING: replace with linear scan once we have the ability
1150 * to describe register live ranges for GC.
1151 */
1152 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001153 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001154 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001155 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001156 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001157 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001158 // Set ssa names for original Dalvik registers
1159 for (int i = 0; i < dalvik_regs; i++) {
1160 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1161 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001162
1163 // Set ssa names for compiler temporaries
1164 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1165 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1166 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1167 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1168 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001169 }
1170
1171 // Duplicate in upper half to represent possible fp double starting sregs.
1172 for (int i = 0; i < num_regs; i++) {
1173 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001174 }
1175
1176 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001177 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178
Brian Carlstrom7940e442013-07-12 13:46:57 -07001179
1180 // Sort the count arrays
1181 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001182 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001183
1184 if (cu_->verbose) {
1185 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001186 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001187 }
1188
1189 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1190 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001191 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1192 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1193 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1194 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1195 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1196 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1197 // Ignore result - if can't alloc double may still be able to alloc singles.
1198 AllocPreservedDouble(low_sreg);
1199 }
1200 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001201 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1202 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001203 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001204 }
1205 }
1206 }
1207
1208 // Promote core regs
1209 for (int i = 0; (i < num_regs) &&
1210 (core_regs[i].count >= promotion_threshold); i++) {
1211 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1212 if (promotion_map_[p_map_idx].core_location !=
1213 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001214 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1215 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001216 break; // No more left
1217 }
1218 }
1219 }
1220 }
1221
1222 // Now, update SSA names to new home locations
1223 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1224 RegLocation *curr = &mir_graph_->reg_location_[i];
1225 int p_map_idx = SRegToPMap(curr->s_reg_low);
1226 if (!curr->wide) {
1227 if (curr->fp) {
1228 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1229 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001230 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 curr->home = true;
1232 }
1233 } else {
1234 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1235 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001236 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001237 curr->home = true;
1238 }
1239 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001240 } else {
1241 if (curr->high_word) {
1242 continue;
1243 }
1244 if (curr->fp) {
1245 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001246 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001247 int low_reg = promotion_map_[p_map_idx].FpReg;
1248 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1249 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001250 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001251 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1252 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001253 if (cu_->instruction_set == kThumb2) {
1254 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1255 } else {
1256 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1257 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001258 curr->home = true;
1259 }
1260 }
1261 } else {
1262 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1263 && (promotion_map_[p_map_idx+1].core_location ==
1264 kLocPhysReg)) {
1265 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001266 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1267 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001268 curr->home = true;
1269 }
1270 }
1271 }
1272 }
1273 if (cu_->verbose) {
1274 DumpPromotionMap();
1275 }
1276}
1277
1278/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001279int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001281 fp_spill_mask_, frame_size_, v_reg,
1282 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001283}
1284
1285/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001286int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1288}
1289
1290/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001291RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 RegLocation gpr_res = LocCReturnWide();
1293 RegLocation fpr_res = LocCReturnDouble();
1294 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee082833c2014-05-17 23:16:26 -07001295 Clobber(res.reg);
1296 LockTemp(res.reg);
1297 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 return res;
1299}
1300
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001301RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 RegLocation gpr_res = LocCReturn();
1303 RegLocation fpr_res = LocCReturnFloat();
1304 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001305 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001306 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001307 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 } else {
buzbee091cc402014-03-31 10:14:40 -07001309 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001310 }
1311 return res;
1312}
1313
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001314void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001315 DoPromotion();
1316
1317 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1318 LOG(INFO) << "After Promotion";
1319 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1320 }
1321
1322 /* Set the frame size */
1323 frame_size_ = ComputeFrameSize();
1324}
1325
1326/*
1327 * Get the "real" sreg number associated with an s_reg slot. In general,
1328 * s_reg values passed through codegen are the SSA names created by
1329 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1330 * array. However, renaming is accomplished by simply replacing RegLocation
1331 * entries in the reglocation[] array. Therefore, when location
1332 * records for operands are first created, we need to ask the locRecord
1333 * identified by the dataflow pass what it's new name is.
1334 */
1335int Mir2Lir::GetSRegHi(int lowSreg) {
1336 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1337}
1338
buzbee091cc402014-03-31 10:14:40 -07001339bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001340 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 return true;
1342}
1343
Brian Carlstrom7940e442013-07-12 13:46:57 -07001344} // namespace art