blob: 81dabd448e2ddd5d58a6404516b2f111a86fd2ea [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
Vladimir Marko8dea81c2014-06-06 14:50:36 +010041Mir2Lir::RegisterInfo::RegisterInfo(RegStorage r, const ResourceMask& 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,
Vladimir Marko089142c2014-06-05 10:57:05 +010059 const ArrayRef<const RegStorage>& core_regs,
60 const ArrayRef<const RegStorage>& core64_regs,
61 const ArrayRef<const RegStorage>& sp_regs,
62 const ArrayRef<const RegStorage>& dp_regs,
63 const ArrayRef<const RegStorage>& reserved_regs,
64 const ArrayRef<const RegStorage>& reserved64_regs,
65 const ArrayRef<const RegStorage>& core_temps,
66 const ArrayRef<const RegStorage>& core64_temps,
67 const ArrayRef<const RegStorage>& sp_temps,
68 const ArrayRef<const RegStorage>& dp_temps) :
buzbeeb01bf152014-05-13 15:59:07 -070069 core_regs_(arena, core_regs.size()), next_core_reg_(0),
70 core64_regs_(arena, core64_regs.size()), next_core64_reg_(0),
71 sp_regs_(arena, sp_regs.size()), next_sp_reg_(0),
72 dp_regs_(arena, dp_regs.size()), next_dp_reg_(0), m2l_(m2l) {
buzbee091cc402014-03-31 10:14:40 -070073 // Initialize the fast lookup map.
74 m2l_->reginfo_map_.Reset();
buzbeeba574512014-05-12 15:13:16 -070075 if (kIsDebugBuild) {
76 m2l_->reginfo_map_.Resize(RegStorage::kMaxRegs);
77 for (unsigned i = 0; i < RegStorage::kMaxRegs; i++) {
78 m2l_->reginfo_map_.Insert(nullptr);
79 }
80 } else {
81 m2l_->reginfo_map_.SetSize(RegStorage::kMaxRegs);
buzbee091cc402014-03-31 10:14:40 -070082 }
83
84 // Construct the register pool.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010085 for (const RegStorage& reg : core_regs) {
buzbee091cc402014-03-31 10:14:40 -070086 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
87 m2l_->reginfo_map_.Put(reg.GetReg(), info);
88 core_regs_.Insert(info);
89 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +010090 for (const RegStorage& reg : core64_regs) {
buzbeeb01bf152014-05-13 15:59:07 -070091 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
92 m2l_->reginfo_map_.Put(reg.GetReg(), info);
93 core64_regs_.Insert(info);
94 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +010095 for (const RegStorage& reg : sp_regs) {
buzbee091cc402014-03-31 10:14:40 -070096 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
97 m2l_->reginfo_map_.Put(reg.GetReg(), info);
98 sp_regs_.Insert(info);
99 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100100 for (const RegStorage& reg : dp_regs) {
buzbee091cc402014-03-31 10:14:40 -0700101 RegisterInfo* info = new (arena) RegisterInfo(reg, m2l_->GetRegMaskCommon(reg));
102 m2l_->reginfo_map_.Put(reg.GetReg(), info);
103 dp_regs_.Insert(info);
104 }
105
106 // Keep special registers from being allocated.
107 for (RegStorage reg : reserved_regs) {
108 m2l_->MarkInUse(reg);
109 }
buzbeeb01bf152014-05-13 15:59:07 -0700110 for (RegStorage reg : reserved64_regs) {
111 m2l_->MarkInUse(reg);
112 }
buzbee091cc402014-03-31 10:14:40 -0700113
114 // Mark temp regs - all others not in use can be used for promotion
115 for (RegStorage reg : core_temps) {
116 m2l_->MarkTemp(reg);
117 }
buzbeeb01bf152014-05-13 15:59:07 -0700118 for (RegStorage reg : core64_temps) {
119 m2l_->MarkTemp(reg);
120 }
buzbee091cc402014-03-31 10:14:40 -0700121 for (RegStorage reg : sp_temps) {
122 m2l_->MarkTemp(reg);
123 }
124 for (RegStorage reg : dp_temps) {
125 m2l_->MarkTemp(reg);
126 }
127
128 // Add an entry for InvalidReg with zero'd mask.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100129 RegisterInfo* invalid_reg = new (arena) RegisterInfo(RegStorage::InvalidReg(), kEncodeNone);
buzbee091cc402014-03-31 10:14:40 -0700130 m2l_->reginfo_map_.Put(RegStorage::InvalidReg().GetReg(), invalid_reg);
buzbeea0cd2d72014-06-01 09:33:49 -0700131
132 // Existence of core64 registers implies wide references.
133 if (core64_regs_.Size() != 0) {
134 ref_regs_ = &core64_regs_;
135 next_ref_reg_ = &next_core64_reg_;
136 } else {
137 ref_regs_ = &core_regs_;
138 next_ref_reg_ = &next_core_reg_;
139 }
buzbee091cc402014-03-31 10:14:40 -0700140}
141
142void Mir2Lir::DumpRegPool(GrowableArray<RegisterInfo*>* regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 LOG(INFO) << "================================================";
buzbee091cc402014-03-31 10:14:40 -0700144 GrowableArray<RegisterInfo*>::Iterator it(regs);
145 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 LOG(INFO) << StringPrintf(
buzbee091cc402014-03-31 10:14:40 -0700147 "R[%d:%d:%c]: T:%d, U:%d, W:%d, p:%d, LV:%d, D:%d, SR:%d, DEF:%d",
148 info->GetReg().GetReg(), info->GetReg().GetRegNum(), info->GetReg().IsFloat() ? 'f' : 'c',
149 info->IsTemp(), info->InUse(), info->IsWide(), info->Partner().GetReg(), info->IsLive(),
150 info->IsDirty(), info->SReg(), info->DefStart() != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
152 LOG(INFO) << "================================================";
153}
154
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700155void Mir2Lir::DumpCoreRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700156 DumpRegPool(&reg_pool_->core_regs_);
buzbeea0cd2d72014-06-01 09:33:49 -0700157 DumpRegPool(&reg_pool_->core64_regs_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158}
159
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700160void Mir2Lir::DumpFpRegPool() {
buzbee091cc402014-03-31 10:14:40 -0700161 DumpRegPool(&reg_pool_->sp_regs_);
162 DumpRegPool(&reg_pool_->dp_regs_);
163}
164
165void Mir2Lir::DumpRegPools() {
166 LOG(INFO) << "Core registers";
167 DumpCoreRegPool();
168 LOG(INFO) << "FP registers";
169 DumpFpRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170}
171
buzbee2700f7e2014-03-07 09:46:20 -0800172void Mir2Lir::Clobber(RegStorage reg) {
buzbeeba574512014-05-12 15:13:16 -0700173 if (UNLIKELY(reg.IsPair())) {
buzbee30adc732014-05-09 15:10:18 -0700174 DCHECK(!GetRegInfo(reg.GetLow())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700175 Clobber(reg.GetLow());
buzbee30adc732014-05-09 15:10:18 -0700176 DCHECK(!GetRegInfo(reg.GetHigh())->IsAliased());
buzbeeba574512014-05-12 15:13:16 -0700177 Clobber(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800178 } else {
buzbee30adc732014-05-09 15:10:18 -0700179 RegisterInfo* info = GetRegInfo(reg);
buzbeeba574512014-05-12 15:13:16 -0700180 if (info->IsTemp() && !info->IsDead()) {
buzbee082833c2014-05-17 23:16:26 -0700181 if (info->GetReg() != info->Partner()) {
182 ClobberBody(GetRegInfo(info->Partner()));
183 }
buzbeeba574512014-05-12 15:13:16 -0700184 ClobberBody(info);
185 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700186 ClobberAliases(info, info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700187 } else {
188 RegisterInfo* master = info->Master();
189 if (info != master) {
190 ClobberBody(info->Master());
buzbee642fe342014-05-23 16:04:08 -0700191 ClobberAliases(info->Master(), info->StorageMask());
buzbeeba574512014-05-12 15:13:16 -0700192 }
193 }
buzbee30adc732014-05-09 15:10:18 -0700194 }
buzbee2700f7e2014-03-07 09:46:20 -0800195 }
196}
197
buzbee642fe342014-05-23 16:04:08 -0700198void Mir2Lir::ClobberAliases(RegisterInfo* info, uint32_t clobber_mask) {
buzbeeba574512014-05-12 15:13:16 -0700199 for (RegisterInfo* alias = info->GetAliasChain(); alias != nullptr;
200 alias = alias->GetAliasChain()) {
201 DCHECK(!alias->IsAliased()); // Only the master should be marked as alised.
buzbee642fe342014-05-23 16:04:08 -0700202 // Only clobber if we have overlap.
203 if ((alias->StorageMask() & clobber_mask) != 0) {
204 ClobberBody(alias);
205 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 }
207}
208
209/*
210 * Break the association between a Dalvik vreg and a physical temp register of either register
211 * class.
212 * TODO: Ideally, the public version of this code should not exist. Besides its local usage
213 * in the register utilities, is is also used by code gen routines to work around a deficiency in
214 * local register allocation, which fails to distinguish between the "in" and "out" identities
215 * of Dalvik vregs. This can result in useless register copies when the same Dalvik vreg
216 * is used both as the source and destination register of an operation in which the type
217 * changes (for example: INT_TO_FLOAT v1, v1). Revisit when improved register allocation is
218 * addressed.
219 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700220void Mir2Lir::ClobberSReg(int s_reg) {
buzbee091cc402014-03-31 10:14:40 -0700221 if (s_reg != INVALID_SREG) {
buzbee30adc732014-05-09 15:10:18 -0700222 if (kIsDebugBuild && s_reg == live_sreg_) {
223 live_sreg_ = INVALID_SREG;
224 }
225 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
226 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
227 if (info->SReg() == s_reg) {
buzbee082833c2014-05-17 23:16:26 -0700228 if (info->GetReg() != info->Partner()) {
229 // Dealing with a pair - clobber the other half.
230 DCHECK(!info->IsAliased());
231 ClobberBody(GetRegInfo(info->Partner()));
232 }
buzbeeba574512014-05-12 15:13:16 -0700233 ClobberBody(info);
buzbee30adc732014-05-09 15:10:18 -0700234 if (info->IsAliased()) {
buzbee642fe342014-05-23 16:04:08 -0700235 ClobberAliases(info, info->StorageMask());
buzbee30adc732014-05-09 15:10:18 -0700236 }
buzbee091cc402014-03-31 10:14:40 -0700237 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 }
239 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240}
241
242/*
243 * SSA names associated with the initial definitions of Dalvik
244 * registers are the same as the Dalvik register number (and
245 * thus take the same position in the promotion_map. However,
246 * the special Method* and compiler temp resisters use negative
247 * v_reg numbers to distinguish them and can have an arbitrary
248 * ssa name (above the last original Dalvik register). This function
249 * maps SSA names to positions in the promotion_map array.
250 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700251int Mir2Lir::SRegToPMap(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 DCHECK_LT(s_reg, mir_graph_->GetNumSSARegs());
253 DCHECK_GE(s_reg, 0);
254 int v_reg = mir_graph_->SRegToVReg(s_reg);
255 if (v_reg >= 0) {
256 DCHECK_LT(v_reg, cu_->num_dalvik_registers);
257 return v_reg;
258 } else {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800259 /*
260 * It must be the case that the v_reg for temporary is less than or equal to the
261 * base reg for temps. For that reason, "position" must be zero or positive.
262 */
263 unsigned int position = std::abs(v_reg) - std::abs(static_cast<int>(kVRegTempBaseReg));
264
265 // The temporaries are placed after dalvik registers in the promotion map
266 DCHECK_LT(position, mir_graph_->GetNumUsedCompilerTemps());
267 return cu_->num_dalvik_registers + position;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 }
269}
270
buzbee091cc402014-03-31 10:14:40 -0700271// TODO: refactor following Alloc/Record routines - much commonality.
buzbee2700f7e2014-03-07 09:46:20 -0800272void Mir2Lir::RecordCorePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 int p_map_idx = SRegToPMap(s_reg);
274 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700275 int reg_num = reg.GetRegNum();
276 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800277 core_spill_mask_ |= (1 << reg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 // Include reg for later sort
buzbee2700f7e2014-03-07 09:46:20 -0800279 core_vmap_table_.push_back(reg_num << VREG_NUM_WIDTH | (v_reg & ((1 << VREG_NUM_WIDTH) - 1)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 num_core_spills_++;
281 promotion_map_[p_map_idx].core_location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -0800282 promotion_map_[p_map_idx].core_reg = reg_num;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283}
284
buzbee091cc402014-03-31 10:14:40 -0700285/* Reserve a callee-save register. Return InvalidReg if none available */
buzbee2700f7e2014-03-07 09:46:20 -0800286RegStorage Mir2Lir::AllocPreservedCoreReg(int s_reg) {
buzbeea0cd2d72014-06-01 09:33:49 -0700287 // TODO: 64-bit and refreg update
buzbee2700f7e2014-03-07 09:46:20 -0800288 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700289 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->core_regs_);
290 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
291 if (!info->IsTemp() && !info->InUse()) {
292 res = info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 RecordCorePromotion(res, s_reg);
294 break;
295 }
296 }
297 return res;
298}
299
buzbee091cc402014-03-31 10:14:40 -0700300void Mir2Lir::RecordSinglePromotion(RegStorage reg, int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 int p_map_idx = SRegToPMap(s_reg);
302 int v_reg = mir_graph_->SRegToVReg(s_reg);
buzbee091cc402014-03-31 10:14:40 -0700303 GetRegInfo(reg)->MarkInUse();
304 MarkPreservedSingle(v_reg, reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -0700306 promotion_map_[p_map_idx].FpReg = reg.GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307}
308
buzbee091cc402014-03-31 10:14:40 -0700309// Reserve a callee-save sp single register.
buzbee2700f7e2014-03-07 09:46:20 -0800310RegStorage Mir2Lir::AllocPreservedSingle(int s_reg) {
311 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700312 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
313 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
314 if (!info->IsTemp() && !info->InUse()) {
315 res = info->GetReg();
316 RecordSinglePromotion(res, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 break;
318 }
319 }
320 return res;
321}
322
buzbee091cc402014-03-31 10:14:40 -0700323void Mir2Lir::RecordDoublePromotion(RegStorage reg, int s_reg) {
324 int p_map_idx = SRegToPMap(s_reg);
325 int v_reg = mir_graph_->SRegToVReg(s_reg);
326 GetRegInfo(reg)->MarkInUse();
327 MarkPreservedDouble(v_reg, reg);
328 promotion_map_[p_map_idx].fp_location = kLocPhysReg;
329 promotion_map_[p_map_idx].FpReg = reg.GetReg();
330}
331
332// Reserve a callee-save dp solo register.
buzbee2700f7e2014-03-07 09:46:20 -0800333RegStorage Mir2Lir::AllocPreservedDouble(int s_reg) {
334 RegStorage res;
buzbee091cc402014-03-31 10:14:40 -0700335 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->dp_regs_);
336 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
337 if (!info->IsTemp() && !info->InUse()) {
338 res = info->GetReg();
339 RecordDoublePromotion(res, s_reg);
340 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 }
343 return res;
344}
345
buzbee091cc402014-03-31 10:14:40 -0700346
347RegStorage Mir2Lir::AllocTempBody(GrowableArray<RegisterInfo*> &regs, int* next_temp, bool required) {
348 int num_regs = regs.Size();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 int next = *next_temp;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700350 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 if (next >= num_regs)
352 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700353 RegisterInfo* info = regs.Get(next);
buzbee30adc732014-05-09 15:10:18 -0700354 // Try to allocate a register that doesn't hold a live value.
buzbee082833c2014-05-17 23:16:26 -0700355 if (info->IsTemp() && !info->InUse() && info->IsDead()) {
buzbee091cc402014-03-31 10:14:40 -0700356 Clobber(info->GetReg());
357 info->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700358 /*
359 * NOTE: "wideness" is an attribute of how the container is used, not its physical size.
360 * The caller will set wideness as appropriate.
361 */
Douglas Leung2db3e262014-06-25 16:02:55 -0700362 if (info->IsWide()) {
363 RegisterInfo* partner = GetRegInfo(info->Partner());
364 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
365 DCHECK(partner->IsWide());
366 info->SetIsWide(false);
367 partner->SetIsWide(false);
368 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700370 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 }
372 next++;
373 }
374 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700375 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700376 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 if (next >= num_regs)
378 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700379 RegisterInfo* info = regs.Get(next);
380 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700381 // Got one. Kill it.
382 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700383 Clobber(info->GetReg());
384 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700385 if (info->IsWide()) {
386 RegisterInfo* partner = GetRegInfo(info->Partner());
387 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
388 DCHECK(partner->IsWide());
389 info->SetIsWide(false);
390 partner->SetIsWide(false);
391 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700393 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 }
395 next++;
396 }
397 if (required) {
398 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700399 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 LOG(FATAL) << "No free temp registers";
401 }
buzbee2700f7e2014-03-07 09:46:20 -0800402 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403}
404
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800406RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700407 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408}
409
buzbee2700f7e2014-03-07 09:46:20 -0800410RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700411 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412}
413
buzbeeb01bf152014-05-13 15:59:07 -0700414RegStorage Mir2Lir::AllocTempWide() {
415 RegStorage res;
416 if (reg_pool_->core64_regs_.Size() != 0) {
417 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, true);
418 } else {
419 RegStorage low_reg = AllocTemp();
420 RegStorage high_reg = AllocTemp();
421 res = RegStorage::MakeRegPair(low_reg, high_reg);
422 }
423 return res;
424}
425
buzbeea0cd2d72014-06-01 09:33:49 -0700426RegStorage Mir2Lir::AllocTempRef() {
427 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, true);
428 DCHECK(!res.IsPair());
429 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100430}
431
buzbee091cc402014-03-31 10:14:40 -0700432RegStorage Mir2Lir::AllocTempSingle() {
433 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
434 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
435 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436}
437
buzbee091cc402014-03-31 10:14:40 -0700438RegStorage Mir2Lir::AllocTempDouble() {
439 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
440 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
441 return res;
442}
443
buzbeeb01bf152014-05-13 15:59:07 -0700444RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
buzbeea0cd2d72014-06-01 09:33:49 -0700445 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700446 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
447 return AllocTempDouble();
448 }
449 return AllocTempWide();
450}
451
452RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
453 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
454 return AllocTempSingle();
buzbeea0cd2d72014-06-01 09:33:49 -0700455 } else if (reg_class == kRefReg) {
456 return AllocTempRef();
buzbeeb01bf152014-05-13 15:59:07 -0700457 }
458 return AllocTemp();
459}
460
buzbee091cc402014-03-31 10:14:40 -0700461RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
462 RegStorage res;
463 GrowableArray<RegisterInfo*>::Iterator it(&regs);
464 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
465 if ((info->SReg() == s_reg) && info->IsLive()) {
466 res = info->GetReg();
467 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 }
469 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 return res;
471}
472
buzbee091cc402014-03-31 10:14:40 -0700473RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
474 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700475 if (reg_class == kRefReg) {
476 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
477 }
478 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700479 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 }
buzbee091cc402014-03-31 10:14:40 -0700481 if (!reg.Valid() && (reg_class != kFPReg)) {
buzbee33ae5582014-06-12 14:56:32 -0700482 if (cu_->target64) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100483 reg = FindLiveReg(wide ? reg_pool_->core64_regs_ : reg_pool_->core_regs_, s_reg);
484 } else {
485 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
486 }
buzbee091cc402014-03-31 10:14:40 -0700487 }
488 if (reg.Valid()) {
buzbee33ae5582014-06-12 14:56:32 -0700489 if (wide && !reg.IsFloat() && !cu_->target64) {
buzbee30adc732014-05-09 15:10:18 -0700490 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700491 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
492 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700493 reg = RegStorage::MakeRegPair(reg, high_reg);
494 MarkWide(reg);
495 } else {
buzbee30adc732014-05-09 15:10:18 -0700496 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700497 reg = RegStorage::InvalidReg();
498 }
499 }
buzbee30adc732014-05-09 15:10:18 -0700500 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
501 // Width mismatch - don't try to reuse.
502 reg = RegStorage::InvalidReg();
503 }
504 }
505 if (reg.Valid()) {
506 if (reg.IsPair()) {
507 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
508 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
509 if (info_low->IsTemp()) {
510 info_low->MarkInUse();
511 }
512 if (info_high->IsTemp()) {
513 info_high->MarkInUse();
514 }
515 } else {
buzbee091cc402014-03-31 10:14:40 -0700516 RegisterInfo* info = GetRegInfo(reg);
517 if (info->IsTemp()) {
518 info->MarkInUse();
519 }
520 }
buzbee30adc732014-05-09 15:10:18 -0700521 } else {
522 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
523 ClobberSReg(s_reg);
524 if (wide) {
525 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700526 }
527 }
528 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529}
530
buzbee2700f7e2014-03-07 09:46:20 -0800531void Mir2Lir::FreeTemp(RegStorage reg) {
532 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700533 FreeTemp(reg.GetLow());
534 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800535 } else {
buzbee091cc402014-03-31 10:14:40 -0700536 RegisterInfo* p = GetRegInfo(reg);
537 if (p->IsTemp()) {
538 p->MarkFree();
539 p->SetIsWide(false);
540 p->SetPartner(reg);
541 }
buzbee2700f7e2014-03-07 09:46:20 -0800542 }
543}
544
buzbee082833c2014-05-17 23:16:26 -0700545void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
546 DCHECK(rl_keep.wide);
547 DCHECK(rl_free.wide);
548 int free_low = rl_free.reg.GetLowReg();
549 int free_high = rl_free.reg.GetHighReg();
550 int keep_low = rl_keep.reg.GetLowReg();
551 int keep_high = rl_keep.reg.GetHighReg();
552 if ((free_low != keep_low) && (free_low != keep_high) &&
553 (free_high != keep_low) && (free_high != keep_high)) {
554 // No overlap, free both
555 FreeTemp(rl_free.reg);
556 }
557}
558
buzbee262b2992014-03-27 11:22:43 -0700559bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700560 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800561 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700562 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
563 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700564 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700565 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800566 } else {
buzbee091cc402014-03-31 10:14:40 -0700567 RegisterInfo* p = GetRegInfo(reg);
568 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800569 }
buzbee091cc402014-03-31 10:14:40 -0700570 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571}
572
buzbee262b2992014-03-27 11:22:43 -0700573bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700574 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800575 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700576 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
577 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
578 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800579 } else {
buzbee091cc402014-03-31 10:14:40 -0700580 RegisterInfo* p = GetRegInfo(reg);
581 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800582 }
buzbee091cc402014-03-31 10:14:40 -0700583 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584}
585
buzbee262b2992014-03-27 11:22:43 -0700586bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700587 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800588 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700589 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
590 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
591 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800592 } else {
buzbee091cc402014-03-31 10:14:40 -0700593 RegisterInfo* p = GetRegInfo(reg);
594 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800595 }
buzbee091cc402014-03-31 10:14:40 -0700596 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597}
598
buzbee2700f7e2014-03-07 09:46:20 -0800599bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700600 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800601 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700602 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
603 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
604 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800605 } else {
buzbee091cc402014-03-31 10:14:40 -0700606 RegisterInfo* p = GetRegInfo(reg);
607 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800608 }
buzbee091cc402014-03-31 10:14:40 -0700609 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800610}
611
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612/*
613 * Similar to AllocTemp(), but forces the allocation of a specific
614 * register. No check is made to see if the register was previously
615 * allocated. Use with caution.
616 */
buzbee2700f7e2014-03-07 09:46:20 -0800617void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700618 DCHECK(IsTemp(reg));
619 if (reg.IsPair()) {
620 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
621 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
622 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700623 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700624 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700625 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700626 } else {
627 RegisterInfo* p = GetRegInfo(reg);
628 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700629 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700630 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631}
632
buzbee2700f7e2014-03-07 09:46:20 -0800633void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700634 if (reg.IsPair()) {
635 GetRegInfo(reg.GetLow())->ResetDefBody();
636 GetRegInfo(reg.GetHigh())->ResetDefBody();
637 } else {
638 GetRegInfo(reg)->ResetDefBody();
639 }
buzbee2700f7e2014-03-07 09:46:20 -0800640}
641
buzbee091cc402014-03-31 10:14:40 -0700642void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
643 RegisterInfo* info = nullptr;
644 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
645 if (IsTemp(rs)) {
646 info = GetRegInfo(reg);
647 }
648 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
649 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
650 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700652 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 break;
buzbee091cc402014-03-31 10:14:40 -0700654 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
656 }
657}
658
659/*
660 * Mark the beginning and end LIR of a def sequence. Note that
661 * on entry start points to the LIR prior to the beginning of the
662 * sequence.
663 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700664void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 DCHECK(!rl.wide);
666 DCHECK(start && start->next);
667 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700668 RegisterInfo* p = GetRegInfo(rl.reg);
669 p->SetDefStart(start->next);
670 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671}
672
673/*
674 * Mark the beginning and end LIR of a def sequence. Note that
675 * on entry start points to the LIR prior to the beginning of the
676 * sequence.
677 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700678void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 DCHECK(rl.wide);
680 DCHECK(start && start->next);
681 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700682 RegisterInfo* p;
683 if (rl.reg.IsPair()) {
684 p = GetRegInfo(rl.reg.GetLow());
685 ResetDef(rl.reg.GetHigh()); // Only track low of pair
686 } else {
687 p = GetRegInfo(rl.reg);
688 }
689 p->SetDefStart(start->next);
690 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691}
692
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700693void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700695 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
696 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 }
buzbee091cc402014-03-31 10:14:40 -0700698 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699}
700
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700701void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700703 // If pair, only track low reg of pair.
704 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
705 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
706 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 }
buzbee091cc402014-03-31 10:14:40 -0700708 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709}
710
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700711void Mir2Lir::ResetDefTracking() {
buzbeea0cd2d72014-06-01 09:33:49 -0700712 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
713 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee091cc402014-03-31 10:14:40 -0700714 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 }
716}
717
buzbeeba574512014-05-12 15:13:16 -0700718void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700719 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
720 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700721 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700722 }
723}
724
725void Mir2Lir::FlushRegWide(RegStorage reg) {
726 if (reg.IsPair()) {
727 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
728 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
729 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
730 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
731 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
732 if (!(info1->IsTemp() && info2->IsTemp())) {
733 /* Should not happen. If it does, there's a problem in eval_loc */
734 LOG(FATAL) << "Long half-temp, half-promoted";
735 }
736
737 info1->SetIsDirty(false);
738 info2->SetIsDirty(false);
739 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
740 info1 = info2;
741 }
742 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100743 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000744 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700745 }
746 } else {
747 RegisterInfo* info = GetRegInfo(reg);
748 if (info->IsLive() && info->IsDirty()) {
749 info->SetIsDirty(false);
750 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100751 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000752 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64, kNotVolatile);
buzbee091cc402014-03-31 10:14:40 -0700753 }
754 }
755}
756
757void Mir2Lir::FlushReg(RegStorage reg) {
758 DCHECK(!reg.IsPair());
759 RegisterInfo* info = GetRegInfo(reg);
760 if (info->IsLive() && info->IsDirty()) {
761 info->SetIsDirty(false);
762 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100763 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000764 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 }
766}
767
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800768void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700769 if (info->IsWide()) {
770 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800771 } else {
buzbee091cc402014-03-31 10:14:40 -0700772 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700773 }
774}
775
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700776void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700777 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
778 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700779 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700780 FlushSpecificReg(info);
781 }
buzbee30adc732014-05-09 15:10:18 -0700782 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700783 info->SetIsWide(false);
784 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785}
786
787
buzbee2700f7e2014-03-07 09:46:20 -0800788bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 if (reg_class == kAnyReg) {
790 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700791 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
792 /*
793 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
794 * with width here - that should be checked at a higher level (if needed).
795 */
buzbee091cc402014-03-31 10:14:40 -0700796 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700797 } else {
buzbee091cc402014-03-31 10:14:40 -0700798 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 }
800}
801
buzbee091cc402014-03-31 10:14:40 -0700802void Mir2Lir::MarkLive(RegLocation loc) {
803 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700804 if (!IsTemp(reg)) {
805 return;
806 }
buzbee091cc402014-03-31 10:14:40 -0700807 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700808 if (s_reg == INVALID_SREG) {
809 // Can't be live if no associated sreg.
810 if (reg.IsPair()) {
811 GetRegInfo(reg.GetLow())->MarkDead();
812 GetRegInfo(reg.GetHigh())->MarkDead();
813 } else {
814 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700815 }
buzbee082833c2014-05-17 23:16:26 -0700816 } else {
817 if (reg.IsPair()) {
818 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
819 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
820 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
821 (info_hi->SReg() == s_reg)) {
822 return; // Already live.
823 }
824 ClobberSReg(s_reg);
825 ClobberSReg(s_reg + 1);
826 info_lo->MarkLive(s_reg);
827 info_hi->MarkLive(s_reg + 1);
828 } else {
829 RegisterInfo* info = GetRegInfo(reg);
830 if (info->IsLive() && (info->SReg() == s_reg)) {
831 return; // Already live.
832 }
833 ClobberSReg(s_reg);
834 if (loc.wide) {
835 ClobberSReg(s_reg + 1);
836 }
837 info->MarkLive(s_reg);
838 }
839 if (loc.wide) {
840 MarkWide(reg);
841 } else {
842 MarkNarrow(reg);
843 }
buzbee091cc402014-03-31 10:14:40 -0700844 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700845}
846
buzbee2700f7e2014-03-07 09:46:20 -0800847void Mir2Lir::MarkTemp(RegStorage reg) {
848 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700850 tempreg_info_.Insert(info);
851 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700852}
853
buzbee2700f7e2014-03-07 09:46:20 -0800854void Mir2Lir::UnmarkTemp(RegStorage reg) {
855 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700856 RegisterInfo* info = GetRegInfo(reg);
857 tempreg_info_.Delete(info);
858 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800859}
860
buzbee091cc402014-03-31 10:14:40 -0700861void Mir2Lir::MarkWide(RegStorage reg) {
862 if (reg.IsPair()) {
863 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
864 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700865 // Unpair any old partners.
866 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
867 GetRegInfo(info_lo->Partner())->SetIsWide(false);
868 }
869 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
870 GetRegInfo(info_hi->Partner())->SetIsWide(false);
871 }
buzbee091cc402014-03-31 10:14:40 -0700872 info_lo->SetIsWide(true);
873 info_hi->SetIsWide(true);
874 info_lo->SetPartner(reg.GetHigh());
875 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800876 } else {
buzbee091cc402014-03-31 10:14:40 -0700877 RegisterInfo* info = GetRegInfo(reg);
878 info->SetIsWide(true);
879 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700880 }
881}
882
buzbee082833c2014-05-17 23:16:26 -0700883void Mir2Lir::MarkNarrow(RegStorage reg) {
884 DCHECK(!reg.IsPair());
885 RegisterInfo* info = GetRegInfo(reg);
886 info->SetIsWide(false);
887 info->SetPartner(reg);
888}
889
buzbee091cc402014-03-31 10:14:40 -0700890void Mir2Lir::MarkClean(RegLocation loc) {
891 if (loc.reg.IsPair()) {
892 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
893 info->SetIsDirty(false);
894 info = GetRegInfo(loc.reg.GetHigh());
895 info->SetIsDirty(false);
896 } else {
897 RegisterInfo* info = GetRegInfo(loc.reg);
898 info->SetIsDirty(false);
899 }
900}
901
902// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700903void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 if (loc.home) {
905 // If already home, can't be dirty
906 return;
907 }
buzbee091cc402014-03-31 10:14:40 -0700908 if (loc.reg.IsPair()) {
909 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
910 info->SetIsDirty(true);
911 info = GetRegInfo(loc.reg.GetHigh());
912 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800913 } else {
buzbee091cc402014-03-31 10:14:40 -0700914 RegisterInfo* info = GetRegInfo(loc.reg);
915 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700916 }
917}
918
buzbee2700f7e2014-03-07 09:46:20 -0800919void Mir2Lir::MarkInUse(RegStorage reg) {
920 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700921 GetRegInfo(reg.GetLow())->MarkInUse();
922 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800923 } else {
buzbee091cc402014-03-31 10:14:40 -0700924 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800925 }
926}
927
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700928bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700929 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700930 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700931 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
932 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700933 int my_sreg = info->SReg();
934 RegStorage partner_reg = info->Partner();
935 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700936 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700937 DCHECK(partner->IsWide());
938 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700939 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700940 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700941 if (my_sreg == INVALID_SREG) {
942 DCHECK_EQ(partner_sreg, INVALID_SREG);
943 } else {
944 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700945 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700946 }
947 }
buzbee082833c2014-05-17 23:16:26 -0700948 if (info->Master() != info) {
949 // Aliased.
950 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
951 // If I'm live, master should not be live, but should show liveness in alias set.
952 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
953 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700954 }
buzbee642fe342014-05-23 16:04:08 -0700955// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700956 }
957 if (info->IsAliased()) {
958 // Has child aliases.
959 DCHECK_EQ(info->Master(), info);
960 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
961 // Master live, no child should be dead - all should show liveness in set.
962 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
963 DCHECK(!p->IsDead());
964 DCHECK_EQ(p->SReg(), INVALID_SREG);
965 }
966 } else if (!info->IsDead()) {
967 // Master not live, one or more aliases must be.
968 bool live_alias = false;
969 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
970 live_alias |= p->IsLive();
971 }
972 DCHECK(live_alias);
973 }
974 }
975 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
976 // If not fully live, should have INVALID_SREG and def's should be null.
977 DCHECK(info->DefStart() == nullptr);
978 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700979 }
980 }
981 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982}
983
984/*
985 * Return an updated location record with current in-register status.
986 * If the value lives in live temps, reflect that fact. No code
987 * is generated. If the live value is part of an older pair,
988 * clobber both low and high.
989 * TUNING: clobbering both is a bit heavy-handed, but the alternative
990 * is a bit complex when dealing with FP regs. Examine code to see
991 * if it's worthwhile trying to be more clever here.
992 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700993RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 DCHECK(!loc.wide);
995 DCHECK(CheckCorePoolSanity());
996 if (loc.location != kLocPhysReg) {
997 DCHECK((loc.location == kLocDalvikFrame) ||
998 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700999 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
1000 if (reg.Valid()) {
1001 bool match = true;
1002 RegisterInfo* info = GetRegInfo(reg);
1003 match &= !reg.IsPair();
1004 match &= !info->IsWide();
1005 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001007 loc.reg = reg;
1008 } else {
1009 Clobber(reg);
1010 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001011 }
1012 }
1013 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014 return loc;
1015}
1016
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001017RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001018 DCHECK(loc.wide);
1019 DCHECK(CheckCorePoolSanity());
1020 if (loc.location != kLocPhysReg) {
1021 DCHECK((loc.location == kLocDalvikFrame) ||
1022 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001023 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1024 if (reg.Valid()) {
1025 bool match = true;
1026 if (reg.IsPair()) {
1027 // If we've got a register pair, make sure that it was last used as the same pair.
1028 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1029 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1030 match &= info_lo->IsWide();
1031 match &= info_hi->IsWide();
1032 match &= (info_lo->Partner() == info_hi->GetReg());
1033 match &= (info_hi->Partner() == info_lo->GetReg());
1034 } else {
1035 RegisterInfo* info = GetRegInfo(reg);
1036 match &= info->IsWide();
1037 match &= (info->GetReg() == info->Partner());
1038 }
1039 if (match) {
1040 loc.location = kLocPhysReg;
1041 loc.reg = reg;
1042 } else {
1043 Clobber(reg);
1044 FreeTemp(reg);
1045 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 }
1047 }
1048 return loc;
1049}
1050
Brian Carlstrom7940e442013-07-12 13:46:57 -07001051/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001052RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 if (loc.wide)
1054 return UpdateLocWide(loc);
1055 else
1056 return UpdateLoc(loc);
1057}
1058
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001059RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061
1062 loc = UpdateLocWide(loc);
1063
1064 /* If already in registers, we can assume proper form. Right reg class? */
1065 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001066 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001067 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001068 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001069 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001070 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001071 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001072 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001073 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001074 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001075 }
1076 return loc;
1077 }
1078
1079 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1080 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1081
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001082 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001083 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 if (update) {
1086 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001087 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089 return loc;
1090}
1091
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001092RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001093 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001094 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001095 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001096
1097 loc = UpdateLoc(loc);
1098
1099 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001100 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001101 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001102 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001103 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001104 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001105 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001106 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001107 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001108 }
1109 return loc;
1110 }
1111
1112 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1113
buzbee2700f7e2014-03-07 09:46:20 -08001114 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115
1116 if (update) {
1117 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001118 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 }
1120 return loc;
1121}
1122
1123/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001124void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1126 RegLocation loc = mir_graph_->reg_location_[i];
1127 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1128 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001129 if (loc.fp) {
1130 if (loc.wide) {
1131 // Treat doubles as a unit, using upper half of fp_counts array.
1132 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1133 i++;
1134 } else {
1135 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1136 }
1137 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001138 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1139 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001140 }
1141}
1142
1143/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001144static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001145 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1146 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001147 // Note that we fall back to sorting on reg so we get stable output
1148 // on differing qsort implementations (such as on host and target or
1149 // between local host and build servers).
1150 return (op1->count == op2->count)
1151 ? (op1->s_reg - op2->s_reg)
1152 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001153}
1154
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001155void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001156 LOG(INFO) << msg;
1157 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001158 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1159 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1160 } else {
1161 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1162 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001163 }
1164}
1165
1166/*
1167 * Note: some portions of this code required even if the kPromoteRegs
1168 * optimization is disabled.
1169 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001170void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001172 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001173 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001174 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1175 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001176 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001177
1178 // Allow target code to add any special registers
1179 AdjustSpillMask();
1180
1181 /*
1182 * Simple register promotion. Just do a static count of the uses
1183 * of Dalvik registers. Note that we examine the SSA names, but
1184 * count based on original Dalvik register name. Count refs
1185 * separately based on type in order to give allocation
1186 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001187 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001188 * reg.
1189 * TUNING: replace with linear scan once we have the ability
1190 * to describe register live ranges for GC.
1191 */
1192 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001193 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001194 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001195 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001196 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001197 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001198 // Set ssa names for original Dalvik registers
1199 for (int i = 0; i < dalvik_regs; i++) {
1200 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1201 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001202
1203 // Set ssa names for compiler temporaries
1204 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1205 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1206 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1207 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1208 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001209 }
1210
1211 // Duplicate in upper half to represent possible fp double starting sregs.
1212 for (int i = 0; i < num_regs; i++) {
1213 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001214 }
1215
1216 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001217 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001218
Brian Carlstrom7940e442013-07-12 13:46:57 -07001219
1220 // Sort the count arrays
1221 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001222 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001223
1224 if (cu_->verbose) {
1225 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001226 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001227 }
1228
1229 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1230 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001231 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1232 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1233 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1234 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1235 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1236 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1237 // Ignore result - if can't alloc double may still be able to alloc singles.
1238 AllocPreservedDouble(low_sreg);
1239 }
1240 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001241 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1242 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001243 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001244 }
1245 }
1246 }
1247
1248 // Promote core regs
1249 for (int i = 0; (i < num_regs) &&
1250 (core_regs[i].count >= promotion_threshold); i++) {
1251 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1252 if (promotion_map_[p_map_idx].core_location !=
1253 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001254 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1255 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001256 break; // No more left
1257 }
1258 }
1259 }
1260 }
1261
1262 // Now, update SSA names to new home locations
1263 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1264 RegLocation *curr = &mir_graph_->reg_location_[i];
1265 int p_map_idx = SRegToPMap(curr->s_reg_low);
1266 if (!curr->wide) {
1267 if (curr->fp) {
1268 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1269 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001270 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 curr->home = true;
1272 }
1273 } else {
1274 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1275 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001276 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001277 curr->home = true;
1278 }
1279 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001280 } else {
1281 if (curr->high_word) {
1282 continue;
1283 }
1284 if (curr->fp) {
1285 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001286 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001287 int low_reg = promotion_map_[p_map_idx].FpReg;
1288 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1289 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001290 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001291 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1292 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001293 if (cu_->instruction_set == kThumb2) {
1294 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1295 } else {
1296 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1297 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001298 curr->home = true;
1299 }
1300 }
1301 } else {
1302 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1303 && (promotion_map_[p_map_idx+1].core_location ==
1304 kLocPhysReg)) {
1305 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001306 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1307 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001308 curr->home = true;
1309 }
1310 }
1311 }
1312 }
1313 if (cu_->verbose) {
1314 DumpPromotionMap();
1315 }
1316}
1317
1318/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001319int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001320 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001321 fp_spill_mask_, frame_size_, v_reg,
1322 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001323}
1324
1325/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001326int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001327 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1328}
1329
1330/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001331RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1332 RegLocation res;
1333 switch (reg_class) {
1334 case kRefReg: LOG(FATAL); break;
1335 case kFPReg: res = LocCReturnDouble(); break;
1336 default: res = LocCReturnWide(); break;
1337 }
buzbee082833c2014-05-17 23:16:26 -07001338 Clobber(res.reg);
1339 LockTemp(res.reg);
1340 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001341 return res;
1342}
1343
buzbeea0cd2d72014-06-01 09:33:49 -07001344RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1345 RegLocation res;
1346 switch (reg_class) {
1347 case kRefReg: res = LocCReturnRef(); break;
1348 case kFPReg: res = LocCReturnFloat(); break;
1349 default: res = LocCReturn(); break;
1350 }
buzbee091cc402014-03-31 10:14:40 -07001351 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001352 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001353 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001354 } else {
buzbee091cc402014-03-31 10:14:40 -07001355 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001356 }
1357 return res;
1358}
1359
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001360void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001361 DoPromotion();
1362
1363 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1364 LOG(INFO) << "After Promotion";
1365 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1366 }
1367
1368 /* Set the frame size */
1369 frame_size_ = ComputeFrameSize();
1370}
1371
1372/*
1373 * Get the "real" sreg number associated with an s_reg slot. In general,
1374 * s_reg values passed through codegen are the SSA names created by
1375 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1376 * array. However, renaming is accomplished by simply replacing RegLocation
1377 * entries in the reglocation[] array. Therefore, when location
1378 * records for operands are first created, we need to ask the locRecord
1379 * identified by the dataflow pass what it's new name is.
1380 */
1381int Mir2Lir::GetSRegHi(int lowSreg) {
1382 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1383}
1384
buzbee091cc402014-03-31 10:14:40 -07001385bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001386 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001387 return true;
1388}
1389
Brian Carlstrom7940e442013-07-12 13:46:57 -07001390} // namespace art