blob: cae59c88c1b63035731c8a6d5c7fa0a931c22d6f [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 */
buzbee091cc402014-03-31 10:14:40 -0700362 info->SetIsWide(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700364 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 }
366 next++;
367 }
368 next = *next_temp;
buzbee30adc732014-05-09 15:10:18 -0700369 // No free non-live regs. Anything we can kill?
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700370 for (int i = 0; i< num_regs; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 if (next >= num_regs)
372 next = 0;
buzbee091cc402014-03-31 10:14:40 -0700373 RegisterInfo* info = regs.Get(next);
374 if (info->IsTemp() && !info->InUse()) {
buzbee30adc732014-05-09 15:10:18 -0700375 // Got one. Kill it.
376 ClobberSReg(info->SReg());
buzbee091cc402014-03-31 10:14:40 -0700377 Clobber(info->GetReg());
378 info->MarkInUse();
buzbee082833c2014-05-17 23:16:26 -0700379 if (info->IsWide()) {
380 RegisterInfo* partner = GetRegInfo(info->Partner());
381 DCHECK_EQ(info->GetReg().GetRegNum(), partner->Partner().GetRegNum());
382 DCHECK(partner->IsWide());
383 info->SetIsWide(false);
384 partner->SetIsWide(false);
385 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 *next_temp = next + 1;
buzbee091cc402014-03-31 10:14:40 -0700387 return info->GetReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 }
389 next++;
390 }
391 if (required) {
392 CodegenDump();
buzbee091cc402014-03-31 10:14:40 -0700393 DumpRegPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 LOG(FATAL) << "No free temp registers";
395 }
buzbee2700f7e2014-03-07 09:46:20 -0800396 return RegStorage::InvalidReg(); // No register available
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397}
398
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399/* Return a temp if one is available, -1 otherwise */
buzbee2700f7e2014-03-07 09:46:20 -0800400RegStorage Mir2Lir::AllocFreeTemp() {
buzbee091cc402014-03-31 10:14:40 -0700401 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402}
403
buzbee2700f7e2014-03-07 09:46:20 -0800404RegStorage Mir2Lir::AllocTemp() {
buzbee091cc402014-03-31 10:14:40 -0700405 return AllocTempBody(reg_pool_->core_regs_, &reg_pool_->next_core_reg_, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406}
407
buzbeeb01bf152014-05-13 15:59:07 -0700408RegStorage Mir2Lir::AllocTempWide() {
409 RegStorage res;
410 if (reg_pool_->core64_regs_.Size() != 0) {
411 res = AllocTempBody(reg_pool_->core64_regs_, &reg_pool_->next_core64_reg_, true);
412 } else {
413 RegStorage low_reg = AllocTemp();
414 RegStorage high_reg = AllocTemp();
415 res = RegStorage::MakeRegPair(low_reg, high_reg);
416 }
417 return res;
418}
419
buzbeea0cd2d72014-06-01 09:33:49 -0700420RegStorage Mir2Lir::AllocTempRef() {
421 RegStorage res = AllocTempBody(*reg_pool_->ref_regs_, reg_pool_->next_ref_reg_, true);
422 DCHECK(!res.IsPair());
423 return res;
Matteo Franchin0955f7e2014-05-23 17:32:52 +0100424}
425
buzbee091cc402014-03-31 10:14:40 -0700426RegStorage Mir2Lir::AllocTempSingle() {
427 RegStorage res = AllocTempBody(reg_pool_->sp_regs_, &reg_pool_->next_sp_reg_, true);
428 DCHECK(res.IsSingle()) << "Reg: 0x" << std::hex << res.GetRawBits();
429 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430}
431
buzbee091cc402014-03-31 10:14:40 -0700432RegStorage Mir2Lir::AllocTempDouble() {
433 RegStorage res = AllocTempBody(reg_pool_->dp_regs_, &reg_pool_->next_dp_reg_, true);
434 DCHECK(res.IsDouble()) << "Reg: 0x" << std::hex << res.GetRawBits();
435 return res;
436}
437
buzbeeb01bf152014-05-13 15:59:07 -0700438RegStorage Mir2Lir::AllocTypedTempWide(bool fp_hint, int reg_class) {
buzbeea0cd2d72014-06-01 09:33:49 -0700439 DCHECK_NE(reg_class, kRefReg); // NOTE: the Dalvik width of a reference is always 32 bits.
buzbeeb01bf152014-05-13 15:59:07 -0700440 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
441 return AllocTempDouble();
442 }
443 return AllocTempWide();
444}
445
446RegStorage Mir2Lir::AllocTypedTemp(bool fp_hint, int reg_class) {
447 if (((reg_class == kAnyReg) && fp_hint) || (reg_class == kFPReg)) {
448 return AllocTempSingle();
buzbeea0cd2d72014-06-01 09:33:49 -0700449 } else if (reg_class == kRefReg) {
450 return AllocTempRef();
buzbeeb01bf152014-05-13 15:59:07 -0700451 }
452 return AllocTemp();
453}
454
buzbee091cc402014-03-31 10:14:40 -0700455RegStorage Mir2Lir::FindLiveReg(GrowableArray<RegisterInfo*> &regs, int s_reg) {
456 RegStorage res;
457 GrowableArray<RegisterInfo*>::Iterator it(&regs);
458 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
459 if ((info->SReg() == s_reg) && info->IsLive()) {
460 res = info->GetReg();
461 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 }
463 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 return res;
465}
466
buzbee091cc402014-03-31 10:14:40 -0700467RegStorage Mir2Lir::AllocLiveReg(int s_reg, int reg_class, bool wide) {
468 RegStorage reg;
buzbeea0cd2d72014-06-01 09:33:49 -0700469 if (reg_class == kRefReg) {
470 reg = FindLiveReg(*reg_pool_->ref_regs_, s_reg);
471 }
472 if (!reg.Valid() && ((reg_class == kAnyReg) || (reg_class == kFPReg))) {
buzbee091cc402014-03-31 10:14:40 -0700473 reg = FindLiveReg(wide ? reg_pool_->dp_regs_ : reg_pool_->sp_regs_, s_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 }
buzbee091cc402014-03-31 10:14:40 -0700475 if (!reg.Valid() && (reg_class != kFPReg)) {
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100476 if (Is64BitInstructionSet(cu_->instruction_set)) {
477 reg = FindLiveReg(wide ? reg_pool_->core64_regs_ : reg_pool_->core_regs_, s_reg);
478 } else {
479 reg = FindLiveReg(reg_pool_->core_regs_, s_reg);
480 }
buzbee091cc402014-03-31 10:14:40 -0700481 }
482 if (reg.Valid()) {
buzbee30adc732014-05-09 15:10:18 -0700483 if (wide && !reg.IsFloat() && !Is64BitInstructionSet(cu_->instruction_set)) {
484 // Only allow reg pairs for core regs on 32-bit targets.
buzbee091cc402014-03-31 10:14:40 -0700485 RegStorage high_reg = FindLiveReg(reg_pool_->core_regs_, s_reg + 1);
486 if (high_reg.Valid()) {
buzbee091cc402014-03-31 10:14:40 -0700487 reg = RegStorage::MakeRegPair(reg, high_reg);
488 MarkWide(reg);
489 } else {
buzbee30adc732014-05-09 15:10:18 -0700490 // Only half available.
buzbee091cc402014-03-31 10:14:40 -0700491 reg = RegStorage::InvalidReg();
492 }
493 }
buzbee30adc732014-05-09 15:10:18 -0700494 if (reg.Valid() && (wide != GetRegInfo(reg)->IsWide())) {
495 // Width mismatch - don't try to reuse.
496 reg = RegStorage::InvalidReg();
497 }
498 }
499 if (reg.Valid()) {
500 if (reg.IsPair()) {
501 RegisterInfo* info_low = GetRegInfo(reg.GetLow());
502 RegisterInfo* info_high = GetRegInfo(reg.GetHigh());
503 if (info_low->IsTemp()) {
504 info_low->MarkInUse();
505 }
506 if (info_high->IsTemp()) {
507 info_high->MarkInUse();
508 }
509 } else {
buzbee091cc402014-03-31 10:14:40 -0700510 RegisterInfo* info = GetRegInfo(reg);
511 if (info->IsTemp()) {
512 info->MarkInUse();
513 }
514 }
buzbee30adc732014-05-09 15:10:18 -0700515 } else {
516 // Either not found, or something didn't match up. Clobber to prevent any stale instances.
517 ClobberSReg(s_reg);
518 if (wide) {
519 ClobberSReg(s_reg + 1);
buzbee091cc402014-03-31 10:14:40 -0700520 }
521 }
522 return reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523}
524
buzbee2700f7e2014-03-07 09:46:20 -0800525void Mir2Lir::FreeTemp(RegStorage reg) {
526 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700527 FreeTemp(reg.GetLow());
528 FreeTemp(reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800529 } else {
buzbee091cc402014-03-31 10:14:40 -0700530 RegisterInfo* p = GetRegInfo(reg);
531 if (p->IsTemp()) {
532 p->MarkFree();
533 p->SetIsWide(false);
534 p->SetPartner(reg);
535 }
buzbee2700f7e2014-03-07 09:46:20 -0800536 }
537}
538
buzbee082833c2014-05-17 23:16:26 -0700539void Mir2Lir::FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free) {
540 DCHECK(rl_keep.wide);
541 DCHECK(rl_free.wide);
542 int free_low = rl_free.reg.GetLowReg();
543 int free_high = rl_free.reg.GetHighReg();
544 int keep_low = rl_keep.reg.GetLowReg();
545 int keep_high = rl_keep.reg.GetHighReg();
546 if ((free_low != keep_low) && (free_low != keep_high) &&
547 (free_high != keep_low) && (free_high != keep_high)) {
548 // No overlap, free both
549 FreeTemp(rl_free.reg);
550 }
551}
552
buzbee262b2992014-03-27 11:22:43 -0700553bool Mir2Lir::IsLive(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700554 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800555 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700556 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
557 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
buzbee30adc732014-05-09 15:10:18 -0700558 DCHECK_EQ(p_lo->IsLive(), p_hi->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700559 res = p_lo->IsLive() || p_hi->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800560 } else {
buzbee091cc402014-03-31 10:14:40 -0700561 RegisterInfo* p = GetRegInfo(reg);
562 res = p->IsLive();
buzbee2700f7e2014-03-07 09:46:20 -0800563 }
buzbee091cc402014-03-31 10:14:40 -0700564 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565}
566
buzbee262b2992014-03-27 11:22:43 -0700567bool Mir2Lir::IsTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700568 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800569 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700570 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
571 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
572 res = p_lo->IsTemp() || p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800573 } else {
buzbee091cc402014-03-31 10:14:40 -0700574 RegisterInfo* p = GetRegInfo(reg);
575 res = p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800576 }
buzbee091cc402014-03-31 10:14:40 -0700577 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700578}
579
buzbee262b2992014-03-27 11:22:43 -0700580bool Mir2Lir::IsPromoted(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700581 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800582 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700583 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
584 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
585 res = !p_lo->IsTemp() || !p_hi->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800586 } else {
buzbee091cc402014-03-31 10:14:40 -0700587 RegisterInfo* p = GetRegInfo(reg);
588 res = !p->IsTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800589 }
buzbee091cc402014-03-31 10:14:40 -0700590 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591}
592
buzbee2700f7e2014-03-07 09:46:20 -0800593bool Mir2Lir::IsDirty(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700594 bool res;
buzbee2700f7e2014-03-07 09:46:20 -0800595 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700596 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
597 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
598 res = p_lo->IsDirty() || p_hi->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800599 } else {
buzbee091cc402014-03-31 10:14:40 -0700600 RegisterInfo* p = GetRegInfo(reg);
601 res = p->IsDirty();
buzbee2700f7e2014-03-07 09:46:20 -0800602 }
buzbee091cc402014-03-31 10:14:40 -0700603 return res;
buzbee2700f7e2014-03-07 09:46:20 -0800604}
605
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606/*
607 * Similar to AllocTemp(), but forces the allocation of a specific
608 * register. No check is made to see if the register was previously
609 * allocated. Use with caution.
610 */
buzbee2700f7e2014-03-07 09:46:20 -0800611void Mir2Lir::LockTemp(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700612 DCHECK(IsTemp(reg));
613 if (reg.IsPair()) {
614 RegisterInfo* p_lo = GetRegInfo(reg.GetLow());
615 RegisterInfo* p_hi = GetRegInfo(reg.GetHigh());
616 p_lo->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700617 p_lo->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700618 p_hi->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700619 p_hi->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700620 } else {
621 RegisterInfo* p = GetRegInfo(reg);
622 p->MarkInUse();
buzbee30adc732014-05-09 15:10:18 -0700623 p->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700624 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625}
626
buzbee2700f7e2014-03-07 09:46:20 -0800627void Mir2Lir::ResetDef(RegStorage reg) {
buzbee091cc402014-03-31 10:14:40 -0700628 if (reg.IsPair()) {
629 GetRegInfo(reg.GetLow())->ResetDefBody();
630 GetRegInfo(reg.GetHigh())->ResetDefBody();
631 } else {
632 GetRegInfo(reg)->ResetDefBody();
633 }
buzbee2700f7e2014-03-07 09:46:20 -0800634}
635
buzbee091cc402014-03-31 10:14:40 -0700636void Mir2Lir::NullifyRange(RegStorage reg, int s_reg) {
637 RegisterInfo* info = nullptr;
638 RegStorage rs = reg.IsPair() ? reg.GetLow() : reg;
639 if (IsTemp(rs)) {
640 info = GetRegInfo(reg);
641 }
642 if ((info != nullptr) && (info->DefStart() != nullptr) && (info->DefEnd() != nullptr)) {
643 DCHECK_EQ(info->SReg(), s_reg); // Make sure we're on the same page.
644 for (LIR* p = info->DefStart();; p = p->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 NopLIR(p);
buzbee091cc402014-03-31 10:14:40 -0700646 if (p == info->DefEnd()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 break;
buzbee091cc402014-03-31 10:14:40 -0700648 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 }
650 }
651}
652
653/*
654 * Mark the beginning and end LIR of a def sequence. Note that
655 * on entry start points to the LIR prior to the beginning of the
656 * sequence.
657 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700658void Mir2Lir::MarkDef(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 DCHECK(!rl.wide);
660 DCHECK(start && start->next);
661 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700662 RegisterInfo* p = GetRegInfo(rl.reg);
663 p->SetDefStart(start->next);
664 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665}
666
667/*
668 * Mark the beginning and end LIR of a def sequence. Note that
669 * on entry start points to the LIR prior to the beginning of the
670 * sequence.
671 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700672void Mir2Lir::MarkDefWide(RegLocation rl, LIR *start, LIR *finish) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 DCHECK(rl.wide);
674 DCHECK(start && start->next);
675 DCHECK(finish);
buzbee091cc402014-03-31 10:14:40 -0700676 RegisterInfo* p;
677 if (rl.reg.IsPair()) {
678 p = GetRegInfo(rl.reg.GetLow());
679 ResetDef(rl.reg.GetHigh()); // Only track low of pair
680 } else {
681 p = GetRegInfo(rl.reg);
682 }
683 p->SetDefStart(start->next);
684 p->SetDefEnd(finish);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685}
686
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700687void Mir2Lir::ResetDefLoc(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 DCHECK(!rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700689 if (IsTemp(rl.reg) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
690 NullifyRange(rl.reg, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700691 }
buzbee091cc402014-03-31 10:14:40 -0700692 ResetDef(rl.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693}
694
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700695void Mir2Lir::ResetDefLocWide(RegLocation rl) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 DCHECK(rl.wide);
buzbee091cc402014-03-31 10:14:40 -0700697 // If pair, only track low reg of pair.
698 RegStorage rs = rl.reg.IsPair() ? rl.reg.GetLow() : rl.reg;
699 if (IsTemp(rs) && !(cu_->disable_opt & (1 << kSuppressLoads))) {
700 NullifyRange(rs, rl.s_reg_low);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 }
buzbee091cc402014-03-31 10:14:40 -0700702 ResetDef(rs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703}
704
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700705void Mir2Lir::ResetDefTracking() {
buzbeea0cd2d72014-06-01 09:33:49 -0700706 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
707 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee091cc402014-03-31 10:14:40 -0700708 info->ResetDefBody();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709 }
710}
711
buzbeeba574512014-05-12 15:13:16 -0700712void Mir2Lir::ClobberAllTemps() {
buzbeebd663de2013-09-10 15:41:31 -0700713 GrowableArray<RegisterInfo*>::Iterator iter(&tempreg_info_);
714 for (RegisterInfo* info = iter.Next(); info != NULL; info = iter.Next()) {
buzbee30adc732014-05-09 15:10:18 -0700715 ClobberBody(info);
buzbee091cc402014-03-31 10:14:40 -0700716 }
717}
718
719void Mir2Lir::FlushRegWide(RegStorage reg) {
720 if (reg.IsPair()) {
721 RegisterInfo* info1 = GetRegInfo(reg.GetLow());
722 RegisterInfo* info2 = GetRegInfo(reg.GetHigh());
723 DCHECK(info1 && info2 && info1->IsWide() && info2->IsWide() &&
724 (info1->Partner() == info2->GetReg()) && (info2->Partner() == info1->GetReg()));
725 if ((info1->IsLive() && info1->IsDirty()) || (info2->IsLive() && info2->IsDirty())) {
726 if (!(info1->IsTemp() && info2->IsTemp())) {
727 /* Should not happen. If it does, there's a problem in eval_loc */
728 LOG(FATAL) << "Long half-temp, half-promoted";
729 }
730
731 info1->SetIsDirty(false);
732 info2->SetIsDirty(false);
733 if (mir_graph_->SRegToVReg(info2->SReg()) < mir_graph_->SRegToVReg(info1->SReg())) {
734 info1 = info2;
735 }
736 int v_reg = mir_graph_->SRegToVReg(info1->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100737 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Vladimir Marko455759b2014-05-06 20:49:36 +0100738 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700739 }
740 } else {
741 RegisterInfo* info = GetRegInfo(reg);
742 if (info->IsLive() && info->IsDirty()) {
743 info->SetIsDirty(false);
744 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100745 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Vladimir Marko455759b2014-05-06 20:49:36 +0100746 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, k64);
buzbee091cc402014-03-31 10:14:40 -0700747 }
748 }
749}
750
751void Mir2Lir::FlushReg(RegStorage reg) {
752 DCHECK(!reg.IsPair());
753 RegisterInfo* info = GetRegInfo(reg);
754 if (info->IsLive() && info->IsDirty()) {
755 info->SetIsDirty(false);
756 int v_reg = mir_graph_->SRegToVReg(info->SReg());
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100757 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
buzbee091cc402014-03-31 10:14:40 -0700758 StoreBaseDisp(TargetReg(kSp), VRegOffset(v_reg), reg, kWord);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700759 }
760}
761
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800762void Mir2Lir::FlushSpecificReg(RegisterInfo* info) {
buzbee091cc402014-03-31 10:14:40 -0700763 if (info->IsWide()) {
764 FlushRegWide(info->GetReg());
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800765 } else {
buzbee091cc402014-03-31 10:14:40 -0700766 FlushReg(info->GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700767 }
768}
769
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700770void Mir2Lir::FlushAllRegs() {
buzbee091cc402014-03-31 10:14:40 -0700771 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
772 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbeeba574512014-05-12 15:13:16 -0700773 if (info->IsDirty() && info->IsLive()) {
buzbee091cc402014-03-31 10:14:40 -0700774 FlushSpecificReg(info);
775 }
buzbee30adc732014-05-09 15:10:18 -0700776 info->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700777 info->SetIsWide(false);
778 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779}
780
781
buzbee2700f7e2014-03-07 09:46:20 -0800782bool Mir2Lir::RegClassMatches(int reg_class, RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 if (reg_class == kAnyReg) {
784 return true;
buzbeea0cd2d72014-06-01 09:33:49 -0700785 } else if ((reg_class == kCoreReg) || (reg_class == kRefReg)) {
786 /*
787 * For this purpose, consider Core and Ref to be the same class. We aren't dealing
788 * with width here - that should be checked at a higher level (if needed).
789 */
buzbee091cc402014-03-31 10:14:40 -0700790 return !reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700791 } else {
buzbee091cc402014-03-31 10:14:40 -0700792 return reg.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793 }
794}
795
buzbee091cc402014-03-31 10:14:40 -0700796void Mir2Lir::MarkLive(RegLocation loc) {
797 RegStorage reg = loc.reg;
buzbee082833c2014-05-17 23:16:26 -0700798 if (!IsTemp(reg)) {
799 return;
800 }
buzbee091cc402014-03-31 10:14:40 -0700801 int s_reg = loc.s_reg_low;
buzbee082833c2014-05-17 23:16:26 -0700802 if (s_reg == INVALID_SREG) {
803 // Can't be live if no associated sreg.
804 if (reg.IsPair()) {
805 GetRegInfo(reg.GetLow())->MarkDead();
806 GetRegInfo(reg.GetHigh())->MarkDead();
807 } else {
808 GetRegInfo(reg)->MarkDead();
buzbee091cc402014-03-31 10:14:40 -0700809 }
buzbee082833c2014-05-17 23:16:26 -0700810 } else {
811 if (reg.IsPair()) {
812 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
813 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
814 if (info_lo->IsLive() && (info_lo->SReg() == s_reg) && info_hi->IsLive() &&
815 (info_hi->SReg() == s_reg)) {
816 return; // Already live.
817 }
818 ClobberSReg(s_reg);
819 ClobberSReg(s_reg + 1);
820 info_lo->MarkLive(s_reg);
821 info_hi->MarkLive(s_reg + 1);
822 } else {
823 RegisterInfo* info = GetRegInfo(reg);
824 if (info->IsLive() && (info->SReg() == s_reg)) {
825 return; // Already live.
826 }
827 ClobberSReg(s_reg);
828 if (loc.wide) {
829 ClobberSReg(s_reg + 1);
830 }
831 info->MarkLive(s_reg);
832 }
833 if (loc.wide) {
834 MarkWide(reg);
835 } else {
836 MarkNarrow(reg);
837 }
buzbee091cc402014-03-31 10:14:40 -0700838 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839}
840
buzbee2700f7e2014-03-07 09:46:20 -0800841void Mir2Lir::MarkTemp(RegStorage reg) {
842 DCHECK(!reg.IsPair());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 RegisterInfo* info = GetRegInfo(reg);
buzbee091cc402014-03-31 10:14:40 -0700844 tempreg_info_.Insert(info);
845 info->SetIsTemp(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846}
847
buzbee2700f7e2014-03-07 09:46:20 -0800848void Mir2Lir::UnmarkTemp(RegStorage reg) {
849 DCHECK(!reg.IsPair());
buzbee091cc402014-03-31 10:14:40 -0700850 RegisterInfo* info = GetRegInfo(reg);
851 tempreg_info_.Delete(info);
852 info->SetIsTemp(false);
buzbee2700f7e2014-03-07 09:46:20 -0800853}
854
buzbee091cc402014-03-31 10:14:40 -0700855void Mir2Lir::MarkWide(RegStorage reg) {
856 if (reg.IsPair()) {
857 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
858 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
buzbee082833c2014-05-17 23:16:26 -0700859 // Unpair any old partners.
860 if (info_lo->IsWide() && info_lo->Partner() != info_hi->GetReg()) {
861 GetRegInfo(info_lo->Partner())->SetIsWide(false);
862 }
863 if (info_hi->IsWide() && info_hi->Partner() != info_lo->GetReg()) {
864 GetRegInfo(info_hi->Partner())->SetIsWide(false);
865 }
buzbee091cc402014-03-31 10:14:40 -0700866 info_lo->SetIsWide(true);
867 info_hi->SetIsWide(true);
868 info_lo->SetPartner(reg.GetHigh());
869 info_hi->SetPartner(reg.GetLow());
buzbee2700f7e2014-03-07 09:46:20 -0800870 } else {
buzbee091cc402014-03-31 10:14:40 -0700871 RegisterInfo* info = GetRegInfo(reg);
872 info->SetIsWide(true);
873 info->SetPartner(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 }
875}
876
buzbee082833c2014-05-17 23:16:26 -0700877void Mir2Lir::MarkNarrow(RegStorage reg) {
878 DCHECK(!reg.IsPair());
879 RegisterInfo* info = GetRegInfo(reg);
880 info->SetIsWide(false);
881 info->SetPartner(reg);
882}
883
buzbee091cc402014-03-31 10:14:40 -0700884void Mir2Lir::MarkClean(RegLocation loc) {
885 if (loc.reg.IsPair()) {
886 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
887 info->SetIsDirty(false);
888 info = GetRegInfo(loc.reg.GetHigh());
889 info->SetIsDirty(false);
890 } else {
891 RegisterInfo* info = GetRegInfo(loc.reg);
892 info->SetIsDirty(false);
893 }
894}
895
896// FIXME: need to verify rules/assumptions about how wide values are treated in 64BitSolos.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700897void Mir2Lir::MarkDirty(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 if (loc.home) {
899 // If already home, can't be dirty
900 return;
901 }
buzbee091cc402014-03-31 10:14:40 -0700902 if (loc.reg.IsPair()) {
903 RegisterInfo* info = GetRegInfo(loc.reg.GetLow());
904 info->SetIsDirty(true);
905 info = GetRegInfo(loc.reg.GetHigh());
906 info->SetIsDirty(true);
buzbee2700f7e2014-03-07 09:46:20 -0800907 } else {
buzbee091cc402014-03-31 10:14:40 -0700908 RegisterInfo* info = GetRegInfo(loc.reg);
909 info->SetIsDirty(true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700910 }
911}
912
buzbee2700f7e2014-03-07 09:46:20 -0800913void Mir2Lir::MarkInUse(RegStorage reg) {
914 if (reg.IsPair()) {
buzbee091cc402014-03-31 10:14:40 -0700915 GetRegInfo(reg.GetLow())->MarkInUse();
916 GetRegInfo(reg.GetHigh())->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800917 } else {
buzbee091cc402014-03-31 10:14:40 -0700918 GetRegInfo(reg)->MarkInUse();
buzbee2700f7e2014-03-07 09:46:20 -0800919 }
920}
921
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700922bool Mir2Lir::CheckCorePoolSanity() {
buzbee082833c2014-05-17 23:16:26 -0700923 GrowableArray<RegisterInfo*>::Iterator it(&tempreg_info_);
buzbee091cc402014-03-31 10:14:40 -0700924 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
buzbee082833c2014-05-17 23:16:26 -0700925 if (info->IsTemp() && info->IsLive() && info->IsWide()) {
926 RegStorage my_reg = info->GetReg();
buzbee091cc402014-03-31 10:14:40 -0700927 int my_sreg = info->SReg();
928 RegStorage partner_reg = info->Partner();
929 RegisterInfo* partner = GetRegInfo(partner_reg);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700930 DCHECK(partner != NULL);
buzbee091cc402014-03-31 10:14:40 -0700931 DCHECK(partner->IsWide());
932 DCHECK_EQ(my_reg.GetReg(), partner->Partner().GetReg());
buzbee082833c2014-05-17 23:16:26 -0700933 DCHECK(partner->IsLive());
buzbee091cc402014-03-31 10:14:40 -0700934 int partner_sreg = partner->SReg();
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700935 if (my_sreg == INVALID_SREG) {
936 DCHECK_EQ(partner_sreg, INVALID_SREG);
937 } else {
938 int diff = my_sreg - partner_sreg;
buzbee091cc402014-03-31 10:14:40 -0700939 DCHECK((diff == 0) || (diff == -1) || (diff == 1));
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700940 }
941 }
buzbee082833c2014-05-17 23:16:26 -0700942 if (info->Master() != info) {
943 // Aliased.
944 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
945 // If I'm live, master should not be live, but should show liveness in alias set.
946 DCHECK_EQ(info->Master()->SReg(), INVALID_SREG);
947 DCHECK(!info->Master()->IsDead());
buzbee082833c2014-05-17 23:16:26 -0700948 }
buzbee642fe342014-05-23 16:04:08 -0700949// TODO: Add checks in !info->IsDead() case to ensure every live bit is owned by exactly 1 reg.
buzbee082833c2014-05-17 23:16:26 -0700950 }
951 if (info->IsAliased()) {
952 // Has child aliases.
953 DCHECK_EQ(info->Master(), info);
954 if (info->IsLive() && (info->SReg() != INVALID_SREG)) {
955 // Master live, no child should be dead - all should show liveness in set.
956 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
957 DCHECK(!p->IsDead());
958 DCHECK_EQ(p->SReg(), INVALID_SREG);
959 }
960 } else if (!info->IsDead()) {
961 // Master not live, one or more aliases must be.
962 bool live_alias = false;
963 for (RegisterInfo* p = info->GetAliasChain(); p != nullptr; p = p->GetAliasChain()) {
964 live_alias |= p->IsLive();
965 }
966 DCHECK(live_alias);
967 }
968 }
969 if (info->IsLive() && (info->SReg() == INVALID_SREG)) {
970 // If not fully live, should have INVALID_SREG and def's should be null.
971 DCHECK(info->DefStart() == nullptr);
972 DCHECK(info->DefEnd() == nullptr);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700973 }
974 }
975 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700976}
977
978/*
979 * Return an updated location record with current in-register status.
980 * If the value lives in live temps, reflect that fact. No code
981 * is generated. If the live value is part of an older pair,
982 * clobber both low and high.
983 * TUNING: clobbering both is a bit heavy-handed, but the alternative
984 * is a bit complex when dealing with FP regs. Examine code to see
985 * if it's worthwhile trying to be more clever here.
986 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700987RegLocation Mir2Lir::UpdateLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 DCHECK(!loc.wide);
989 DCHECK(CheckCorePoolSanity());
990 if (loc.location != kLocPhysReg) {
991 DCHECK((loc.location == kLocDalvikFrame) ||
992 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -0700993 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, false);
994 if (reg.Valid()) {
995 bool match = true;
996 RegisterInfo* info = GetRegInfo(reg);
997 match &= !reg.IsPair();
998 match &= !info->IsWide();
999 if (match) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001001 loc.reg = reg;
1002 } else {
1003 Clobber(reg);
1004 FreeTemp(reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005 }
1006 }
1007 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001008 return loc;
1009}
1010
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001011RegLocation Mir2Lir::UpdateLocWide(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 DCHECK(loc.wide);
1013 DCHECK(CheckCorePoolSanity());
1014 if (loc.location != kLocPhysReg) {
1015 DCHECK((loc.location == kLocDalvikFrame) ||
1016 (loc.location == kLocCompilerTemp));
buzbee091cc402014-03-31 10:14:40 -07001017 RegStorage reg = AllocLiveReg(loc.s_reg_low, kAnyReg, true);
1018 if (reg.Valid()) {
1019 bool match = true;
1020 if (reg.IsPair()) {
1021 // If we've got a register pair, make sure that it was last used as the same pair.
1022 RegisterInfo* info_lo = GetRegInfo(reg.GetLow());
1023 RegisterInfo* info_hi = GetRegInfo(reg.GetHigh());
1024 match &= info_lo->IsWide();
1025 match &= info_hi->IsWide();
1026 match &= (info_lo->Partner() == info_hi->GetReg());
1027 match &= (info_hi->Partner() == info_lo->GetReg());
1028 } else {
1029 RegisterInfo* info = GetRegInfo(reg);
1030 match &= info->IsWide();
1031 match &= (info->GetReg() == info->Partner());
1032 }
1033 if (match) {
1034 loc.location = kLocPhysReg;
1035 loc.reg = reg;
1036 } else {
1037 Clobber(reg);
1038 FreeTemp(reg);
1039 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 }
1041 }
1042 return loc;
1043}
1044
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045/* For use in cases we don't know (or care) width */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001046RegLocation Mir2Lir::UpdateRawLoc(RegLocation loc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 if (loc.wide)
1048 return UpdateLocWide(loc);
1049 else
1050 return UpdateLoc(loc);
1051}
1052
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001053RegLocation Mir2Lir::EvalLocWide(RegLocation loc, int reg_class, bool update) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 DCHECK(loc.wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001055
1056 loc = UpdateLocWide(loc);
1057
1058 /* If already in registers, we can assume proper form. Right reg class? */
1059 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001060 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001061 // Wrong register class. Reallocate and transfer ownership.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001062 RegStorage new_regs = AllocTypedTempWide(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001063 // Clobber the old regs.
buzbee2700f7e2014-03-07 09:46:20 -08001064 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001065 // ...and mark the new ones live.
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001066 loc.reg = new_regs;
buzbee091cc402014-03-31 10:14:40 -07001067 MarkWide(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001068 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069 }
1070 return loc;
1071 }
1072
1073 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1074 DCHECK_NE(GetSRegHi(loc.s_reg_low), INVALID_SREG);
1075
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001076 loc.reg = AllocTypedTempWide(loc.fp, reg_class);
buzbee091cc402014-03-31 10:14:40 -07001077 MarkWide(loc.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 if (update) {
1080 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001081 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001082 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001083 return loc;
1084}
1085
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001086RegLocation Mir2Lir::EvalLoc(RegLocation loc, int reg_class, bool update) {
buzbee091cc402014-03-31 10:14:40 -07001087 if (loc.wide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 return EvalLocWide(loc, reg_class, update);
buzbee091cc402014-03-31 10:14:40 -07001089 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001090
1091 loc = UpdateLoc(loc);
1092
1093 if (loc.location == kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001094 if (!RegClassMatches(reg_class, loc.reg)) {
Vladimir Marko0dc242d2014-05-12 16:22:14 +01001095 // Wrong register class. Reallocate and transfer ownership.
buzbee2700f7e2014-03-07 09:46:20 -08001096 RegStorage new_reg = AllocTypedTemp(loc.fp, reg_class);
buzbee082833c2014-05-17 23:16:26 -07001097 // Clobber the old reg.
buzbee2700f7e2014-03-07 09:46:20 -08001098 Clobber(loc.reg);
buzbee082833c2014-05-17 23:16:26 -07001099 // ...and mark the new one live.
buzbee2700f7e2014-03-07 09:46:20 -08001100 loc.reg = new_reg;
buzbee082833c2014-05-17 23:16:26 -07001101 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102 }
1103 return loc;
1104 }
1105
1106 DCHECK_NE(loc.s_reg_low, INVALID_SREG);
1107
buzbee2700f7e2014-03-07 09:46:20 -08001108 loc.reg = AllocTypedTemp(loc.fp, reg_class);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109
1110 if (update) {
1111 loc.location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001112 MarkLive(loc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001113 }
1114 return loc;
1115}
1116
1117/* USE SSA names to count references of base Dalvik v_regs. */
buzbeec729a6b2013-09-14 16:04:31 -07001118void Mir2Lir::CountRefs(RefCounts* core_counts, RefCounts* fp_counts, size_t num_regs) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1120 RegLocation loc = mir_graph_->reg_location_[i];
1121 RefCounts* counts = loc.fp ? fp_counts : core_counts;
1122 int p_map_idx = SRegToPMap(loc.s_reg_low);
buzbeec729a6b2013-09-14 16:04:31 -07001123 if (loc.fp) {
1124 if (loc.wide) {
1125 // Treat doubles as a unit, using upper half of fp_counts array.
1126 counts[p_map_idx + num_regs].count += mir_graph_->GetUseCount(i);
1127 i++;
1128 } else {
1129 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1130 }
1131 } else if (!IsInexpensiveConstant(loc)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001132 counts[p_map_idx].count += mir_graph_->GetUseCount(i);
1133 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 }
1135}
1136
1137/* qsort callback function, sort descending */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001138static int SortCounts(const void *val1, const void *val2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 const Mir2Lir::RefCounts* op1 = reinterpret_cast<const Mir2Lir::RefCounts*>(val1);
1140 const Mir2Lir::RefCounts* op2 = reinterpret_cast<const Mir2Lir::RefCounts*>(val2);
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001141 // Note that we fall back to sorting on reg so we get stable output
1142 // on differing qsort implementations (such as on host and target or
1143 // between local host and build servers).
1144 return (op1->count == op2->count)
1145 ? (op1->s_reg - op2->s_reg)
1146 : (op1->count < op2->count ? 1 : -1);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001147}
1148
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001149void Mir2Lir::DumpCounts(const RefCounts* arr, int size, const char* msg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150 LOG(INFO) << msg;
1151 for (int i = 0; i < size; i++) {
buzbeec729a6b2013-09-14 16:04:31 -07001152 if ((arr[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1153 LOG(INFO) << "s_reg[D" << (arr[i].s_reg & ~STARTING_DOUBLE_SREG) << "]: " << arr[i].count;
1154 } else {
1155 LOG(INFO) << "s_reg[" << arr[i].s_reg << "]: " << arr[i].count;
1156 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001157 }
1158}
1159
1160/*
1161 * Note: some portions of this code required even if the kPromoteRegs
1162 * optimization is disabled.
1163 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001164void Mir2Lir::DoPromotion() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 int dalvik_regs = cu_->num_dalvik_registers;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001166 int num_regs = dalvik_regs + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001167 const int promotion_threshold = 1;
buzbeed69835d2014-02-03 14:40:27 -08001168 // Allocate the promotion map - one entry for each Dalvik vReg or compiler temp
1169 promotion_map_ = static_cast<PromotionMap*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001170 (arena_->Alloc(num_regs * sizeof(promotion_map_[0]), kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001171
1172 // Allow target code to add any special registers
1173 AdjustSpillMask();
1174
1175 /*
1176 * Simple register promotion. Just do a static count of the uses
1177 * of Dalvik registers. Note that we examine the SSA names, but
1178 * count based on original Dalvik register name. Count refs
1179 * separately based on type in order to give allocation
1180 * preference to fp doubles - which must be allocated sequential
buzbeec729a6b2013-09-14 16:04:31 -07001181 * physical single fp registers starting with an even-numbered
Brian Carlstrom7940e442013-07-12 13:46:57 -07001182 * reg.
1183 * TUNING: replace with linear scan once we have the ability
1184 * to describe register live ranges for GC.
1185 */
1186 RefCounts *core_regs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001187 static_cast<RefCounts*>(arena_->Alloc(sizeof(RefCounts) * num_regs,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001188 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 RefCounts *FpRegs =
buzbeec729a6b2013-09-14 16:04:31 -07001190 static_cast<RefCounts *>(arena_->Alloc(sizeof(RefCounts) * num_regs * 2,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001191 kArenaAllocRegAlloc));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001192 // Set ssa names for original Dalvik registers
1193 for (int i = 0; i < dalvik_regs; i++) {
1194 core_regs[i].s_reg = FpRegs[i].s_reg = i;
1195 }
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001196
1197 // Set ssa names for compiler temporaries
1198 for (unsigned int ct_idx = 0; ct_idx < mir_graph_->GetNumUsedCompilerTemps(); ct_idx++) {
1199 CompilerTemp* ct = mir_graph_->GetCompilerTemp(ct_idx);
1200 core_regs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1201 FpRegs[dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
1202 FpRegs[num_regs + dalvik_regs + ct_idx].s_reg = ct->s_reg_low;
buzbeec729a6b2013-09-14 16:04:31 -07001203 }
1204
1205 // Duplicate in upper half to represent possible fp double starting sregs.
1206 for (int i = 0; i < num_regs; i++) {
1207 FpRegs[num_regs + i].s_reg = FpRegs[i].s_reg | STARTING_DOUBLE_SREG;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001208 }
1209
1210 // Sum use counts of SSA regs by original Dalvik vreg.
buzbeec729a6b2013-09-14 16:04:31 -07001211 CountRefs(core_regs, FpRegs, num_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001212
Brian Carlstrom7940e442013-07-12 13:46:57 -07001213
1214 // Sort the count arrays
1215 qsort(core_regs, num_regs, sizeof(RefCounts), SortCounts);
buzbeec729a6b2013-09-14 16:04:31 -07001216 qsort(FpRegs, num_regs * 2, sizeof(RefCounts), SortCounts);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001217
1218 if (cu_->verbose) {
1219 DumpCounts(core_regs, num_regs, "Core regs after sort");
buzbeec729a6b2013-09-14 16:04:31 -07001220 DumpCounts(FpRegs, num_regs * 2, "Fp regs after sort");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001221 }
1222
1223 if (!(cu_->disable_opt & (1 << kPromoteRegs))) {
1224 // Promote FpRegs
buzbeec729a6b2013-09-14 16:04:31 -07001225 for (int i = 0; (i < (num_regs * 2)) && (FpRegs[i].count >= promotion_threshold); i++) {
1226 int p_map_idx = SRegToPMap(FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG);
1227 if ((FpRegs[i].s_reg & STARTING_DOUBLE_SREG) != 0) {
1228 if ((promotion_map_[p_map_idx].fp_location != kLocPhysReg) &&
1229 (promotion_map_[p_map_idx + 1].fp_location != kLocPhysReg)) {
1230 int low_sreg = FpRegs[i].s_reg & ~STARTING_DOUBLE_SREG;
1231 // Ignore result - if can't alloc double may still be able to alloc singles.
1232 AllocPreservedDouble(low_sreg);
1233 }
1234 } else if (promotion_map_[p_map_idx].fp_location != kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001235 RegStorage reg = AllocPreservedSingle(FpRegs[i].s_reg);
1236 if (!reg.Valid()) {
buzbeec729a6b2013-09-14 16:04:31 -07001237 break; // No more left.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001238 }
1239 }
1240 }
1241
1242 // Promote core regs
1243 for (int i = 0; (i < num_regs) &&
1244 (core_regs[i].count >= promotion_threshold); i++) {
1245 int p_map_idx = SRegToPMap(core_regs[i].s_reg);
1246 if (promotion_map_[p_map_idx].core_location !=
1247 kLocPhysReg) {
buzbee2700f7e2014-03-07 09:46:20 -08001248 RegStorage reg = AllocPreservedCoreReg(core_regs[i].s_reg);
1249 if (!reg.Valid()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001250 break; // No more left
1251 }
1252 }
1253 }
1254 }
1255
1256 // Now, update SSA names to new home locations
1257 for (int i = 0; i < mir_graph_->GetNumSSARegs(); i++) {
1258 RegLocation *curr = &mir_graph_->reg_location_[i];
1259 int p_map_idx = SRegToPMap(curr->s_reg_low);
1260 if (!curr->wide) {
1261 if (curr->fp) {
1262 if (promotion_map_[p_map_idx].fp_location == kLocPhysReg) {
1263 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001264 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].FpReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001265 curr->home = true;
1266 }
1267 } else {
1268 if (promotion_map_[p_map_idx].core_location == kLocPhysReg) {
1269 curr->location = kLocPhysReg;
buzbee2700f7e2014-03-07 09:46:20 -08001270 curr->reg = RegStorage::Solo32(promotion_map_[p_map_idx].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001271 curr->home = true;
1272 }
1273 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001274 } else {
1275 if (curr->high_word) {
1276 continue;
1277 }
1278 if (curr->fp) {
1279 if ((promotion_map_[p_map_idx].fp_location == kLocPhysReg) &&
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001280 (promotion_map_[p_map_idx+1].fp_location == kLocPhysReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001281 int low_reg = promotion_map_[p_map_idx].FpReg;
1282 int high_reg = promotion_map_[p_map_idx+1].FpReg;
1283 // Doubles require pair of singles starting at even reg
buzbee091cc402014-03-31 10:14:40 -07001284 // TODO: move target-specific restrictions out of here.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001285 if (((low_reg & 0x1) == 0) && ((low_reg + 1) == high_reg)) {
1286 curr->location = kLocPhysReg;
buzbee091cc402014-03-31 10:14:40 -07001287 if (cu_->instruction_set == kThumb2) {
1288 curr->reg = RegStorage::FloatSolo64(RegStorage::RegNum(low_reg) >> 1);
1289 } else {
1290 curr->reg = RegStorage(RegStorage::k64BitPair, low_reg, high_reg);
1291 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001292 curr->home = true;
1293 }
1294 }
1295 } else {
1296 if ((promotion_map_[p_map_idx].core_location == kLocPhysReg)
1297 && (promotion_map_[p_map_idx+1].core_location ==
1298 kLocPhysReg)) {
1299 curr->location = kLocPhysReg;
Bill Buzbee00e1ec62014-02-27 23:44:13 +00001300 curr->reg = RegStorage(RegStorage::k64BitPair, promotion_map_[p_map_idx].core_reg,
1301 promotion_map_[p_map_idx+1].core_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001302 curr->home = true;
1303 }
1304 }
1305 }
1306 }
1307 if (cu_->verbose) {
1308 DumpPromotionMap();
1309 }
1310}
1311
1312/* Returns sp-relative offset in bytes for a VReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001313int Mir2Lir::VRegOffset(int v_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001314 return StackVisitor::GetVRegOffset(cu_->code_item, core_spill_mask_,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001315 fp_spill_mask_, frame_size_, v_reg,
1316 cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001317}
1318
1319/* Returns sp-relative offset in bytes for a SReg */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001320int Mir2Lir::SRegOffset(int s_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001321 return VRegOffset(mir_graph_->SRegToVReg(s_reg));
1322}
1323
1324/* Mark register usage state and return long retloc */
buzbeea0cd2d72014-06-01 09:33:49 -07001325RegLocation Mir2Lir::GetReturnWide(RegisterClass reg_class) {
1326 RegLocation res;
1327 switch (reg_class) {
1328 case kRefReg: LOG(FATAL); break;
1329 case kFPReg: res = LocCReturnDouble(); break;
1330 default: res = LocCReturnWide(); break;
1331 }
buzbee082833c2014-05-17 23:16:26 -07001332 Clobber(res.reg);
1333 LockTemp(res.reg);
1334 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001335 return res;
1336}
1337
buzbeea0cd2d72014-06-01 09:33:49 -07001338RegLocation Mir2Lir::GetReturn(RegisterClass reg_class) {
1339 RegLocation res;
1340 switch (reg_class) {
1341 case kRefReg: res = LocCReturnRef(); break;
1342 case kFPReg: res = LocCReturnFloat(); break;
1343 default: res = LocCReturn(); break;
1344 }
buzbee091cc402014-03-31 10:14:40 -07001345 Clobber(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001346 if (cu_->instruction_set == kMips) {
buzbee091cc402014-03-31 10:14:40 -07001347 MarkInUse(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001348 } else {
buzbee091cc402014-03-31 10:14:40 -07001349 LockTemp(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001350 }
1351 return res;
1352}
1353
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001354void Mir2Lir::SimpleRegAlloc() {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001355 DoPromotion();
1356
1357 if (cu_->verbose && !(cu_->disable_opt & (1 << kPromoteRegs))) {
1358 LOG(INFO) << "After Promotion";
1359 mir_graph_->DumpRegLocTable(mir_graph_->reg_location_, mir_graph_->GetNumSSARegs());
1360 }
1361
1362 /* Set the frame size */
1363 frame_size_ = ComputeFrameSize();
1364}
1365
1366/*
1367 * Get the "real" sreg number associated with an s_reg slot. In general,
1368 * s_reg values passed through codegen are the SSA names created by
1369 * dataflow analysis and refer to slot numbers in the mir_graph_->reg_location
1370 * array. However, renaming is accomplished by simply replacing RegLocation
1371 * entries in the reglocation[] array. Therefore, when location
1372 * records for operands are first created, we need to ask the locRecord
1373 * identified by the dataflow pass what it's new name is.
1374 */
1375int Mir2Lir::GetSRegHi(int lowSreg) {
1376 return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1;
1377}
1378
buzbee091cc402014-03-31 10:14:40 -07001379bool Mir2Lir::LiveOut(int s_reg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001380 // For now.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001381 return true;
1382}
1383
Brian Carlstrom7940e442013-07-12 13:46:57 -07001384} // namespace art