blob: ca9a3ab64f0242e02173430106a2cf290168ad28 [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),
43 s_reg_(INVALID_SREG), def_use_mask_(mask), master_(this) {
buzbee091cc402014-03-31 10:14:40 -070044 switch (r.StorageSize()) {
45 case 0: storage_mask_ = 0xffffffff; break;
46 case 4: storage_mask_ = 0x00000001; break;
47 case 8: storage_mask_ = 0x00000003; break;
48 case 16: storage_mask_ = 0x0000000f; break;
49 case 32: storage_mask_ = 0x000000ff; break;
50 case 64: storage_mask_ = 0x0000ffff; break;
51 case 128: storage_mask_ = 0xffffffff; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 }
buzbee091cc402014-03-31 10:14:40 -070053 used_storage_ = r.Valid() ? ~storage_mask_ : storage_mask_;
buzbee30adc732014-05-09 15:10:18 -070054 liveness_ = used_storage_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070055}
56
buzbee091cc402014-03-31 10:14:40 -070057Mir2Lir::RegisterPool::RegisterPool(Mir2Lir* m2l, ArenaAllocator* arena,
58 const std::vector<RegStorage>& core_regs,
59 const std::vector<RegStorage>& sp_regs,
60 const std::vector<RegStorage>& dp_regs,
61 const std::vector<RegStorage>& reserved_regs,
62 const std::vector<RegStorage>& core_temps,
63 const std::vector<RegStorage>& sp_temps,
64 const std::vector<RegStorage>& dp_temps) :
65 core_regs_(arena, core_regs.size()), next_core_reg_(0), sp_regs_(arena, sp_regs.size()),
66 next_sp_reg_(0), dp_regs_(arena, dp_regs.size()), next_dp_reg_(0), m2l_(m2l) {
67 // Initialize the fast lookup map.
68 m2l_->reginfo_map_.Reset();
69 m2l_->reginfo_map_.Resize(RegStorage::kMaxRegs);
70 for (unsigned i = 0; i < RegStorage::kMaxRegs; i++) {
71 m2l_->reginfo_map_.Insert(nullptr);
72 }
73
74 // Construct the register pool.
75 for (RegStorage reg : core_regs) {
76 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
77 m2l_->reginfo_map_.Put(reg.GetReg(), info);
78 core_regs_.Insert(info);
79 }
80 for (RegStorage reg : sp_regs) {
81 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
82 m2l_->reginfo_map_.Put(reg.GetReg(), info);
83 sp_regs_.Insert(info);
84 }
85 for (RegStorage reg : dp_regs) {
86 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
87 m2l_->reginfo_map_.Put(reg.GetReg(), info);
88 dp_regs_.Insert(info);
89 }
90
91 // Keep special registers from being allocated.
92 for (RegStorage reg : reserved_regs) {
93 m2l_->MarkInUse(reg);
94 }
95
96 // Mark temp regs - all others not in use can be used for promotion
97 for (RegStorage reg : core_temps) {
98 m2l_->MarkTemp(reg);
99 }
100 for (RegStorage reg : sp_temps) {
101 m2l_->MarkTemp(reg);
102 }
103 for (RegStorage reg : dp_temps) {
104 m2l_->MarkTemp(reg);
105 }
106
107 // Add an entry for InvalidReg with zero'd mask.
108 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), 0);
109 m2l_->reginfo_map_.Put(RegStorage::InvalidReg().GetReg(), invalid_reg);
110}
111
112void Mir2Lir::DumpRegPool(GrowableArray<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 LOG(INFO) << "================================================";
buzbee091cc402014-03-31 10:14:40 -0700114 GrowableArray<RegisterInfo*>::Iterator it(regs);
115 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700117 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
118 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
119 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
120 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 }
122 LOG(INFO) << "================================================";
123}
124
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700125void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700126 DumpRegPool(&reg_pool_->core_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127}
128
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700129void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700130 DumpRegPool(&reg_pool_->sp_regs_);
131 DumpRegPool(&reg_pool_->dp_regs_);
132}
133
134void Mir2Lir::DumpRegPools() {
135 LOG(INFO) << "Core registers";
136 DumpCoreRegPool();
137 LOG(INFO) << "FP registers";
138 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139}
140
buzbee2700f7e2014-03-07 09:46:20 -0800141void Mir2Lir::Clobber(RegStorage reg) {
142 if (reg.IsPair()) {
buzbee30adc732014-05-09 15:10:18 -0700143 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbee091cc402014-03-31 10:14:40 -0700144 ClobberBody(GetRegInfo(reg.GetLow()));
buzbee30adc732014-05-09 15:10:18 -0700145 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbee091cc402014-03-31 10:14:40 -0700146 ClobberBody(GetRegInfo(reg.GetHigh()));
buzbee2700f7e2014-03-07 09:46:20 -0800147 } else {
buzbee30adc732014-05-09 15:10:18 -0700148 RegisterInfo* info = GetRegInfo(reg);
149 if (info->IsAliased()) {
150 ClobberAliases(info);
151 } else if (info != info->Master() && info->Master()->SReg() != INVALID_SREG) {
152 ClobberBody(info->Master());
153 }
154 ClobberBody(info);
buzbee2700f7e2014-03-07 09:46:20 -0800155 }
156}
157
buzbee30adc732014-05-09 15:10:18 -0700158void Mir2Lir::ClobberAliases(RegisterInfo* info) {
159 DCHECK(info->IsAliased());
160 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
161 for (RegisterInfo* tmpreg_info = iter.Next(); tmpreg_info != NULL; tmpreg_info = iter.Next()) {
162 if (tmpreg_info->Master() == info) {
163 // tmpreg_info is an alias of info.
164 ClobberBody(tmpreg_info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 }
166 }
167}
168
169/*
170 * Break the association between a Dalvik vreg and a physical temp register of either register
171 * class.
172 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
173 * in the register utilities, is is also used by code gen routines to work around a deficiency in
174 * local register allocation, which fails to distinguish between the "in" and "out" identities
175 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
176 * is used both as the source and destination register of an operation in which the type
177 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
178 * addressed.
179 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700181 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700182 if (kIsDebugBuild && s_reg == live_sreg_) {
183 live_sreg_ = INVALID_SREG;
184 }
185 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
186 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
187 if (info->SReg() == s_reg) {
188 if (info->IsAliased()) {
189 // TUNING: if this gets hot, we could add links to follow - aliasing is static.
190 ClobberAliases(info);
191 }
192 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700193 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 }
195 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196}
197
198/*
199 * SSA names associated with the initial definitions of Dalvik
200 * registers are the same as the Dalvik register number (and
201 * thus take the same position in the promotion_map. However,
202 * the special Method* and compiler temp resisters use negative
203 * v_reg numbers to distinguish them and can have an arbitrary
204 * ssa name (above the last original Dalvik register). This function
205 * maps SSA names to positions in the promotion_map array.
206 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700207int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
209 DCHECK_GE(s_reg, 0);
210 int v_reg = mir_graph_->SRegToVReg(s_reg);
211 if (v_reg >= 0) {
212 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
213 return v_reg;
214 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800215 /*
216 * It must be the case that the v_reg for temporary is less than or equal to the
217 * base reg for temps. For that reason, "position" must be zero or positive.
218 */
219 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
220
221 // The temporaries are placed after dalvik registers in the promotion map
222 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
223 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 }
225}
226
buzbee091cc402014-03-31 10:14:40 -0700227// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800228void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 int p_map_idx = SRegToPMap(s_reg);
230 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700231 int reg_num = reg.GetRegNum();
232 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800233 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800235 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 num_core_spills_++;
237 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800238 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239}
240
buzbee091cc402014-03-31 10:14:40 -0700241/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800242RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
243 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700244 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
245 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
246 if (!info->IsTemp() && !info->InUse()) {
247 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 RecordCorePromotion(res, s_reg);
249 break;
250 }
251 }
252 return res;
253}
254
buzbee091cc402014-03-31 10:14:40 -0700255void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 int p_map_idx = SRegToPMap(s_reg);
257 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700258 GetRegInfo(reg)->MarkInUse();
259 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700261 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262}
263
buzbee091cc402014-03-31 10:14:40 -0700264// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800265RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
266 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700267 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
268 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
269 if (!info->IsTemp() && !info->InUse()) {
270 res = info->GetReg();
271 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 break;
273 }
274 }
275 return res;
276}
277
buzbee091cc402014-03-31 10:14:40 -0700278void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
279 int p_map_idx = SRegToPMap(s_reg);
280 int v_reg = mir_graph_->SRegToVReg(s_reg);
281 GetRegInfo(reg)->MarkInUse();
282 MarkPreservedDouble(v_reg, reg);
283 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
284 promotion_map_[p_map_idx].FpReg = reg.GetReg();
285}
286
287// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800288RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
289 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700290 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
291 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
292 if (!info->IsTemp() && !info->InUse()) {
293 res = info->GetReg();
294 RecordDoublePromotion(res, s_reg);
295 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 }
298 return res;
299}
300
buzbee091cc402014-03-31 10:14:40 -0700301
302RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
303 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700305 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 if (next >= num_regs)
307 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700308 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700309 // Try to allocate a register that doesn't hold a live value.
buzbee091cc402014-03-31 10:14:40 -0700310 if (info->IsTemp() && !info->InUse() && !info->IsLive()) {
311 Clobber(info->GetReg());
312 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700313 /*
314 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
315 * The caller will set wideness as appropriate.
316 */
buzbee091cc402014-03-31 10:14:40 -0700317 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700319 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 }
321 next++;
322 }
323 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700324 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700325 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 if (next >= num_regs)
327 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700328 RegisterInfo* info = regs.Get(next);
329 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700330 // Got one. Kill it.
331 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700332 Clobber(info->GetReg());
333 info->MarkInUse();
334 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700336 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 }
338 next++;
339 }
340 if (required) {
341 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700342 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 LOG(FATAL) << "No free temp registers";
344 }
buzbee2700f7e2014-03-07 09:46:20 -0800345 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346}
347
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800349RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700350 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351}
352
buzbee2700f7e2014-03-07 09:46:20 -0800353RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700354 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355}
356
buzbee091cc402014-03-31 10:14:40 -0700357RegStorage Mir2Lir::AllocTempSingle() {
358 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
359 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
360 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361}
362
buzbee091cc402014-03-31 10:14:40 -0700363RegStorage Mir2Lir::AllocTempDouble() {
364 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
365 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
366 return res;
367}
368
369RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
370 RegStorage res;
371 GrowableArray<RegisterInfo*>::Iterator it(&regs);
372 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
373 if ((info->SReg() == s_reg) && info->IsLive()) {
374 res = info->GetReg();
375 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 }
377 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 return res;
379}
380
buzbee091cc402014-03-31 10:14:40 -0700381RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
382 RegStorage reg;
383 // TODO: might be worth a sanity check here to verify at most 1 live reg per s_reg.
384 if ((reg_class == kAnyReg) || (reg_class == kFPReg)) {
385 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 }
buzbee091cc402014-03-31 10:14:40 -0700387 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee30adc732014-05-09 15:10:18 -0700388 // TODO: add 64-bit core pool similar to above.
buzbee091cc402014-03-31 10:14:40 -0700389 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
390 }
391 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700392 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
393 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700394 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
395 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700396 reg = RegStorage::MakeRegPair(reg, high_reg);
397 MarkWide(reg);
398 } else {
buzbee30adc732014-05-09 15:10:18 -0700399 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700400 reg = RegStorage::InvalidReg();
401 }
402 }
buzbee30adc732014-05-09 15:10:18 -0700403 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
404 // Width mismatch - don't try to reuse.
405 reg = RegStorage::InvalidReg();
406 }
407 }
408 if (reg.Valid()) {
409 if (reg.IsPair()) {
410 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
411 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
412 if (info_low->IsTemp()) {
413 info_low->MarkInUse();
414 }
415 if (info_high->IsTemp()) {
416 info_high->MarkInUse();
417 }
418 } else {
buzbee091cc402014-03-31 10:14:40 -0700419 RegisterInfo* info = GetRegInfo(reg);
420 if (info->IsTemp()) {
421 info->MarkInUse();
422 }
423 }
buzbee30adc732014-05-09 15:10:18 -0700424 } else {
425 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
426 ClobberSReg(s_reg);
427 if (wide) {
428 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700429 }
430 }
431 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432}
433
buzbee2700f7e2014-03-07 09:46:20 -0800434void Mir2Lir::FreeTemp(RegStorage reg) {
435 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700436 FreeTemp(reg.GetLow());
437 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800438 } else {
buzbee091cc402014-03-31 10:14:40 -0700439 RegisterInfo* p = GetRegInfo(reg);
440 if (p->IsTemp()) {
441 p->MarkFree();
442 p->SetIsWide(false);
443 p->SetPartner(reg);
444 }
buzbee2700f7e2014-03-07 09:46:20 -0800445 }
446}
447
buzbee262b2992014-03-27 11:22:43 -0700448bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700449 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800450 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700451 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
452 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700453 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700454 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800455 } else {
buzbee091cc402014-03-31 10:14:40 -0700456 RegisterInfo* p = GetRegInfo(reg);
457 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800458 }
buzbee091cc402014-03-31 10:14:40 -0700459 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460}
461
buzbee262b2992014-03-27 11:22:43 -0700462bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700463 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800464 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700465 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
466 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
467 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800468 } else {
buzbee091cc402014-03-31 10:14:40 -0700469 RegisterInfo* p = GetRegInfo(reg);
470 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800471 }
buzbee091cc402014-03-31 10:14:40 -0700472 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473}
474
buzbee262b2992014-03-27 11:22:43 -0700475bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700476 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800477 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700478 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
479 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
480 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800481 } else {
buzbee091cc402014-03-31 10:14:40 -0700482 RegisterInfo* p = GetRegInfo(reg);
483 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800484 }
buzbee091cc402014-03-31 10:14:40 -0700485 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486}
487
buzbee2700f7e2014-03-07 09:46:20 -0800488bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700489 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800490 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700491 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
492 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
493 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800494 } else {
buzbee091cc402014-03-31 10:14:40 -0700495 RegisterInfo* p = GetRegInfo(reg);
496 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800497 }
buzbee091cc402014-03-31 10:14:40 -0700498 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800499}
500
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501/*
502 * Similar to AllocTemp(), but forces the allocation of a specific
503 * register. No check is made to see if the register was previously
504 * allocated. Use with caution.
505 */
buzbee2700f7e2014-03-07 09:46:20 -0800506void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700507 DCHECK(IsTemp(reg));
508 if (reg.IsPair()) {
509 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
510 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
511 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700512 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700513 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700514 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700515 } else {
516 RegisterInfo* p = GetRegInfo(reg);
517 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700518 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700519 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520}
521
buzbee2700f7e2014-03-07 09:46:20 -0800522void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700523 if (reg.IsPair()) {
524 GetRegInfo(reg.GetLow())->ResetDefBody();
525 GetRegInfo(reg.GetHigh())->ResetDefBody();
526 } else {
527 GetRegInfo(reg)->ResetDefBody();
528 }
buzbee2700f7e2014-03-07 09:46:20 -0800529}
530
buzbee091cc402014-03-31 10:14:40 -0700531void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
532 RegisterInfo* info = nullptr;
533 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
534 if (IsTemp(rs)) {
535 info = GetRegInfo(reg);
536 }
537 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
538 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
539 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700541 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 break;
buzbee091cc402014-03-31 10:14:40 -0700543 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 }
545 }
546}
547
548/*
549 * Mark the beginning and end LIR of a def sequence. Note that
550 * on entry start points to the LIR prior to the beginning of the
551 * sequence.
552 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700553void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700554 DCHECK(!rl.wide);
555 DCHECK(start && start->next);
556 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700557 RegisterInfo* p = GetRegInfo(rl.reg);
558 p->SetDefStart(start->next);
559 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560}
561
562/*
563 * Mark the beginning and end LIR of a def sequence. Note that
564 * on entry start points to the LIR prior to the beginning of the
565 * sequence.
566 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700567void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 DCHECK(rl.wide);
569 DCHECK(start && start->next);
570 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700571 RegisterInfo* p;
572 if (rl.reg.IsPair()) {
573 p = GetRegInfo(rl.reg.GetLow());
574 ResetDef(rl.reg.GetHigh()); // Only track low of pair
575 } else {
576 p = GetRegInfo(rl.reg);
577 }
578 p->SetDefStart(start->next);
579 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580}
581
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700582RegLocation Mir2Lir::WideToNarrow(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 DCHECK(rl.wide);
584 if (rl.location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700585 if (rl.reg.IsPair()) {
586 RegisterInfo* info_lo = GetRegInfo(rl.reg.GetLow());
587 RegisterInfo* info_hi = GetRegInfo(rl.reg.GetHigh());
588 if (info_lo->IsTemp()) {
589 info_lo->SetIsWide(false);
590 info_lo->ResetDefBody();
591 }
592 if (info_hi->IsTemp()) {
593 info_hi->SetIsWide(false);
594 info_hi->ResetDefBody();
595 }
596 rl.reg = rl.reg.GetLow();
buzbee30adc732014-05-09 15:10:18 -0700597 } else {
598 /*
599 * TODO: If not a pair, we can't just drop the high register. On some targets, we may be
600 * able to re-cast the 64-bit register as 32 bits, so it might be worthwhile to revisit
601 * this code. Will probably want to make this a virtual function.
602 */
603 // Can't narrow 64-bit register. Clobber.
604 if (GetRegInfo(rl.reg)->IsTemp()) {
605 Clobber(rl.reg);
606 FreeTemp(rl.reg);
607 }
608 rl.location = kLocDalvikFrame;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 }
611 rl.wide = false;
612 return rl;
613}
614
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700615void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700617 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
618 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 }
buzbee091cc402014-03-31 10:14:40 -0700620 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621}
622
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700623void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700625 // If pair, only track low reg of pair.
626 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
627 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
628 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 }
buzbee091cc402014-03-31 10:14:40 -0700630 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631}
632
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700633void Mir2Lir::ResetDefTracking() {
buzbee091cc402014-03-31 10:14:40 -0700634 GrowableArray<RegisterInfo*>::Iterator core_it(&reg_pool_->core_regs_);
635 for (RegisterInfo* info = core_it.Next(); info != nullptr; info = core_it.Next()) {
636 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
buzbee091cc402014-03-31 10:14:40 -0700638 GrowableArray<RegisterInfo*>::Iterator sp_it(&reg_pool_->core_regs_);
639 for (RegisterInfo* info = sp_it.Next(); info != nullptr; info = sp_it.Next()) {
640 info->ResetDefBody();
641 }
642 GrowableArray<RegisterInfo*>::Iterator dp_it(&reg_pool_->core_regs_);
643 for (RegisterInfo* info = dp_it.Next(); info != nullptr; info = dp_it.Next()) {
644 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 }
646}
647
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700648void Mir2Lir::ClobberAllRegs() {
buzbeebd663de2013-09-10 15:41:31 -0700649 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
650 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700651 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700652 }
653}
654
655void Mir2Lir::FlushRegWide(RegStorage reg) {
656 if (reg.IsPair()) {
657 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
658 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
659 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
660 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
661 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
662 if (!(info1->IsTemp() && info2->IsTemp())) {
663 /* Should not happen. If it does, there's a problem in eval_loc */
664 LOG(FATAL) << "Long half-temp, half-promoted";
665 }
666
667 info1->SetIsDirty(false);
668 info2->SetIsDirty(false);
669 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
670 info1 = info2;
671 }
672 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100673 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700674 }
675 } else {
676 RegisterInfo* info = GetRegInfo(reg);
677 if (info->IsLive() && info->IsDirty()) {
678 info->SetIsDirty(false);
679 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko455759b2014-05-06 20:49:36 +0100680 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700681 }
682 }
683}
684
685void Mir2Lir::FlushReg(RegStorage reg) {
686 DCHECK(!reg.IsPair());
687 RegisterInfo* info = GetRegInfo(reg);
688 if (info->IsLive() && info->IsDirty()) {
689 info->SetIsDirty(false);
690 int v_reg = mir_graph_->SRegToVReg(info->SReg());
691 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 }
693}
694
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800695void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700696 if (info->IsWide()) {
697 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800698 } else {
buzbee091cc402014-03-31 10:14:40 -0700699 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 }
701}
702
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700703void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700704 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
705 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
706 if (info->IsLive() && info->IsDirty()) {
707 FlushSpecificReg(info);
708 }
709 DCHECK(info->IsTemp());
buzbee30adc732014-05-09 15:10:18 -0700710 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700711 info->SetSReg(INVALID_SREG);
712 info->ResetDefBody();
713 info->SetIsWide(false);
714 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715}
716
717
buzbee2700f7e2014-03-07 09:46:20 -0800718bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 if (reg_class == kAnyReg) {
720 return true;
721 } else if (reg_class == kCoreReg) {
buzbee091cc402014-03-31 10:14:40 -0700722 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 } else {
buzbee091cc402014-03-31 10:14:40 -0700724 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700725 }
726}
727
buzbee091cc402014-03-31 10:14:40 -0700728void Mir2Lir::MarkLiveReg(RegStorage reg, int s_reg) {
729 RegisterInfo* info = GetRegInfo(reg);
730 if ((info->SReg() == s_reg) && info->IsLive()) {
731 return; // Already live.
732 }
733 if (s_reg != INVALID_SREG) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 ClobberSReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700735 if (info->IsTemp()) {
buzbee30adc732014-05-09 15:10:18 -0700736 info->MarkLive();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700737 }
738 } else {
buzbee091cc402014-03-31 10:14:40 -0700739 // Can't be live if no associated s_reg.
740 DCHECK(info->IsTemp());
buzbee30adc732014-05-09 15:10:18 -0700741 info->MarkDead();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742 }
buzbee091cc402014-03-31 10:14:40 -0700743 info->SetSReg(s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744}
745
buzbee091cc402014-03-31 10:14:40 -0700746void Mir2Lir::MarkLive(RegLocation loc) {
747 RegStorage reg = loc.reg;
748 int s_reg = loc.s_reg_low;
749 if (reg.IsPair()) {
750 MarkLiveReg(reg.GetLow(), s_reg);
751 MarkLiveReg(reg.GetHigh(), s_reg+1);
752 } else {
753 if (loc.wide) {
754 ClobberSReg(s_reg + 1);
755 }
756 MarkLiveReg(reg, s_reg);
757 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758}
759
buzbee2700f7e2014-03-07 09:46:20 -0800760void Mir2Lir::MarkTemp(RegStorage reg) {
761 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700763 tempreg_info_.Insert(info);
764 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765}
766
buzbee2700f7e2014-03-07 09:46:20 -0800767void Mir2Lir::UnmarkTemp(RegStorage reg) {
768 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700769 RegisterInfo* info = GetRegInfo(reg);
770 tempreg_info_.Delete(info);
771 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800772}
773
buzbee091cc402014-03-31 10:14:40 -0700774void Mir2Lir::MarkWide(RegStorage reg) {
775 if (reg.IsPair()) {
776 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
777 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
778 info_lo->SetIsWide(true);
779 info_hi->SetIsWide(true);
780 info_lo->SetPartner(reg.GetHigh());
781 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800782 } else {
buzbee091cc402014-03-31 10:14:40 -0700783 RegisterInfo* info = GetRegInfo(reg);
784 info->SetIsWide(true);
785 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700786 }
787}
788
buzbee091cc402014-03-31 10:14:40 -0700789void Mir2Lir::MarkClean(RegLocation loc) {
790 if (loc.reg.IsPair()) {
791 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
792 info->SetIsDirty(false);
793 info = GetRegInfo(loc.reg.GetHigh());
794 info->SetIsDirty(false);
795 } else {
796 RegisterInfo* info = GetRegInfo(loc.reg);
797 info->SetIsDirty(false);
798 }
799}
800
801// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700802void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 if (loc.home) {
804 // If already home, can't be dirty
805 return;
806 }
buzbee091cc402014-03-31 10:14:40 -0700807 if (loc.reg.IsPair()) {
808 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
809 info->SetIsDirty(true);
810 info = GetRegInfo(loc.reg.GetHigh());
811 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800812 } else {
buzbee091cc402014-03-31 10:14:40 -0700813 RegisterInfo* info = GetRegInfo(loc.reg);
814 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700815 }
816}
817
buzbee2700f7e2014-03-07 09:46:20 -0800818void Mir2Lir::MarkInUse(RegStorage reg) {
819 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700820 GetRegInfo(reg.GetLow())->MarkInUse();
821 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800822 } else {
buzbee091cc402014-03-31 10:14:40 -0700823 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800824 }
825}
826
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700827bool Mir2Lir::CheckCorePoolSanity() {
buzbee091cc402014-03-31 10:14:40 -0700828 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
829 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
830 RegStorage my_reg = info->GetReg();
831 if (info->IsWide() && my_reg.IsPair()) {
832 int my_sreg = info->SReg();
833 RegStorage partner_reg = info->Partner();
834 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700835 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700836 DCHECK(partner->IsWide());
837 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
838 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700839 if (my_sreg == INVALID_SREG) {
840 DCHECK_EQ(partner_sreg, INVALID_SREG);
841 } else {
842 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700843 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700844 }
buzbee091cc402014-03-31 10:14:40 -0700845 } else {
846 // TODO: add whatever sanity checks might be useful for 64BitSolo regs here.
847 // TODO: sanity checks for floating point pools?
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700848 }
buzbee091cc402014-03-31 10:14:40 -0700849 if (!info->IsLive()) {
850 DCHECK(info->DefStart() == NULL);
851 DCHECK(info->DefEnd() == NULL);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700852 }
853 }
854 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855}
856
857/*
858 * Return an updated location record with current in-register status.
859 * If the value lives in live temps, reflect that fact. No code
860 * is generated. If the live value is part of an older pair,
861 * clobber both low and high.
862 * TUNING: clobbering both is a bit heavy-handed, but the alternative
863 * is a bit complex when dealing with FP regs. Examine code to see
864 * if it's worthwhile trying to be more clever here.
865 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700866RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 DCHECK(!loc.wide);
868 DCHECK(CheckCorePoolSanity());
869 if (loc.location != kLocPhysReg) {
870 DCHECK((loc.location == kLocDalvikFrame) ||
871 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700872 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
873 if (reg.Valid()) {
874 bool match = true;
875 RegisterInfo* info = GetRegInfo(reg);
876 match &= !reg.IsPair();
877 match &= !info->IsWide();
878 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700879 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700880 loc.reg = reg;
881 } else {
882 Clobber(reg);
883 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700884 }
885 }
886 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700887 return loc;
888}
889
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700890RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700891 DCHECK(loc.wide);
892 DCHECK(CheckCorePoolSanity());
893 if (loc.location != kLocPhysReg) {
894 DCHECK((loc.location == kLocDalvikFrame) ||
895 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700896 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
897 if (reg.Valid()) {
898 bool match = true;
899 if (reg.IsPair()) {
900 // If we've got a register pair, make sure that it was last used as the same pair.
901 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
902 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
903 match &= info_lo->IsWide();
904 match &= info_hi->IsWide();
905 match &= (info_lo->Partner() == info_hi->GetReg());
906 match &= (info_hi->Partner() == info_lo->GetReg());
907 } else {
908 RegisterInfo* info = GetRegInfo(reg);
909 match &= info->IsWide();
910 match &= (info->GetReg() == info->Partner());
911 }
912 if (match) {
913 loc.location = kLocPhysReg;
914 loc.reg = reg;
915 } else {
916 Clobber(reg);
917 FreeTemp(reg);
918 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919 }
920 }
921 return loc;
922}
923
Brian Carlstrom7940e442013-07-12 13:46:57 -0700924/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700925RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700926 if (loc.wide)
927 return UpdateLocWide(loc);
928 else
929 return UpdateLoc(loc);
930}
931
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700932RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700933 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934
935 loc = UpdateLocWide(loc);
936
937 /* If already in registers, we can assume proper form. Right reg class? */
938 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800939 if (!RegClassMatches(reg_class, loc.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700940 /* Wrong register class. Reallocate and copy */
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000941 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee2700f7e2014-03-07 09:46:20 -0800942 OpRegCopyWide(new_regs, loc.reg);
buzbee091cc402014-03-31 10:14:40 -0700943 // Associate the old sreg with the new register and clobber the old register.
944 GetRegInfo(new_regs)->SetSReg(GetRegInfo(loc.reg)->SReg());
buzbee2700f7e2014-03-07 09:46:20 -0800945 Clobber(loc.reg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000946 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -0700947 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700948 }
949 return loc;
950 }
951
952 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
953 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
954
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000955 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -0700956 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957
Brian Carlstrom7940e442013-07-12 13:46:57 -0700958 if (update) {
959 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700960 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 return loc;
963}
964
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700965RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -0700966 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -0700968 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700969
970 loc = UpdateLoc(loc);
971
972 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -0800973 if (!RegClassMatches(reg_class, loc.reg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700974 /* Wrong register class. Realloc, copy and transfer ownership */
buzbee2700f7e2014-03-07 09:46:20 -0800975 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
976 OpRegCopy(new_reg, loc.reg);
buzbee091cc402014-03-31 10:14:40 -0700977 // Associate the old sreg with the new register and clobber the old register.
978 GetRegInfo(new_reg)->SetSReg(GetRegInfo(loc.reg)->SReg());
buzbee2700f7e2014-03-07 09:46:20 -0800979 Clobber(loc.reg);
980 loc.reg = new_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700981 }
982 return loc;
983 }
984
985 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
986
buzbee2700f7e2014-03-07 09:46:20 -0800987 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988
989 if (update) {
990 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700991 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700992 }
993 return loc;
994}
995
996/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -0700997void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
999 RegLocation loc = mir_graph_->reg_location_[i];
1000 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1001 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001002 if (loc.fp) {
1003 if (loc.wide) {
1004 // Treat doubles as a unit, using upper half of fp_counts array.
1005 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1006 i++;
1007 } else {
1008 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1009 }
1010 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
1014}
1015
1016/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001017static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1019 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001020 // Note that we fall back to sorting on reg so we get stable output
1021 // on differing qsort implementations (such as on host and target or
1022 // between local host and build servers).
1023 return (op1->count == op2->count)
1024 ? (op1->s_reg - op2->s_reg)
1025 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026}
1027
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001028void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 LOG(INFO) << msg;
1030 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001031 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1032 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1033 } else {
1034 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1035 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 }
1037}
1038
1039/*
1040 * Note: some portions of this code required even if the kPromoteRegs
1041 * optimization is disabled.
1042 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001043void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001045 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001047 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1048 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001049 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001050
1051 // Allow target code to add any special registers
1052 AdjustSpillMask();
1053
1054 /*
1055 * Simple register promotion. Just do a static count of the uses
1056 * of Dalvik registers. Note that we examine the SSA names, but
1057 * count based on original Dalvik register name. Count refs
1058 * separately based on type in order to give allocation
1059 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001060 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 * reg.
1062 * TUNING: replace with linear scan once we have the ability
1063 * to describe register live ranges for GC.
1064 */
1065 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001066 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001067 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001068 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001069 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001070 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071 // Set ssa names for original Dalvik registers
1072 for (int i = 0; i < dalvik_regs; i++) {
1073 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1074 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001075
1076 // Set ssa names for compiler temporaries
1077 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1078 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1079 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1080 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1081 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001082 }
1083
1084 // Duplicate in upper half to represent possible fp double starting sregs.
1085 for (int i = 0; i < num_regs; i++) {
1086 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001087 }
1088
1089 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001090 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091
Brian Carlstrom7940e442013-07-12 13:46:57 -07001092
1093 // Sort the count arrays
1094 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001095 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096
1097 if (cu_->verbose) {
1098 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001099 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 }
1101
1102 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1103 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001104 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1105 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1106 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1107 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1108 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1109 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1110 // Ignore result - if can't alloc double may still be able to alloc singles.
1111 AllocPreservedDouble(low_sreg);
1112 }
1113 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001114 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1115 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001116 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001117 }
1118 }
1119 }
1120
1121 // Promote core regs
1122 for (int i = 0; (i < num_regs) &&
1123 (core_regs[i].count >= promotion_threshold); i++) {
1124 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1125 if (promotion_map_[p_map_idx].core_location !=
1126 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001127 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1128 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001129 break; // No more left
1130 }
1131 }
1132 }
1133 }
1134
1135 // Now, update SSA names to new home locations
1136 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1137 RegLocation *curr = &mir_graph_->reg_location_[i];
1138 int p_map_idx = SRegToPMap(curr->s_reg_low);
1139 if (!curr->wide) {
1140 if (curr->fp) {
1141 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1142 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001143 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001144 curr->home = true;
1145 }
1146 } else {
1147 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1148 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001149 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 curr->home = true;
1151 }
1152 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153 } else {
1154 if (curr->high_word) {
1155 continue;
1156 }
1157 if (curr->fp) {
1158 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001159 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001160 int low_reg = promotion_map_[p_map_idx].FpReg;
1161 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1162 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001163 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1165 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001166 if (cu_->instruction_set == kThumb2) {
1167 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1168 } else {
1169 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1170 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 curr->home = true;
1172 }
1173 }
1174 } else {
1175 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1176 && (promotion_map_[p_map_idx+1].core_location ==
1177 kLocPhysReg)) {
1178 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001179 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1180 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001181 curr->home = true;
1182 }
1183 }
1184 }
1185 }
1186 if (cu_->verbose) {
1187 DumpPromotionMap();
1188 }
1189}
1190
1191/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001192int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001193 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001194 fp_spill_mask_, frame_size_, v_reg,
1195 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001196}
1197
1198/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001199int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001200 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1201}
1202
1203/* Mark register usage state and return long retloc */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001204RegLocation Mir2Lir::GetReturnWide(bool is_double) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001205 RegLocation gpr_res = LocCReturnWide();
1206 RegLocation fpr_res = LocCReturnDouble();
1207 RegLocation res = is_double ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001208 if (res.reg.IsPair()) {
1209 Clobber(res.reg);
1210 LockTemp(res.reg);
1211 // Does this wide value live in two registers or one vector register?
1212 if (res.reg.GetLowReg() != res.reg.GetHighReg()) {
1213 // FIXME: I think we want to mark these as wide as well.
1214 MarkWide(res.reg);
1215 }
1216 } else {
1217 Clobber(res.reg);
1218 LockTemp(res.reg);
1219 MarkWide(res.reg);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +00001220 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 return res;
1222}
1223
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001224RegLocation Mir2Lir::GetReturn(bool is_float) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001225 RegLocation gpr_res = LocCReturn();
1226 RegLocation fpr_res = LocCReturnFloat();
1227 RegLocation res = is_float ? fpr_res : gpr_res;
buzbee091cc402014-03-31 10:14:40 -07001228 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001229 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001230 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001231 } else {
buzbee091cc402014-03-31 10:14:40 -07001232 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001233 }
1234 return res;
1235}
1236
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001237void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 DoPromotion();
1239
1240 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1241 LOG(INFO) << "After Promotion";
1242 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1243 }
1244
1245 /* Set the frame size */
1246 frame_size_ = ComputeFrameSize();
1247}
1248
1249/*
1250 * Get the "real" sreg number associated with an s_reg slot. In general,
1251 * s_reg values passed through codegen are the SSA names created by
1252 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1253 * array. However, renaming is accomplished by simply replacing RegLocation
1254 * entries in the reglocation[] array. Therefore, when location
1255 * records for operands are first created, we need to ask the locRecord
1256 * identified by the dataflow pass what it's new name is.
1257 */
1258int Mir2Lir::GetSRegHi(int lowSreg) {
1259 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1260}
1261
buzbee091cc402014-03-31 10:14:40 -07001262bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001263 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001264 return true;
1265}
1266
Brian Carlstrom7940e442013-07-12 13:46:57 -07001267} // namespace art