blob: b183f9e245893ad245d714571907b5d44bb4270c [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -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
buzbee1bc37c62012-11-20 13:35:41 -080017#include "codegen_util.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080018#include "compiler/compiler_ir.h"
19#include "ralloc_util.h"
buzbee1bc37c62012-11-20 13:35:41 -080020
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080021namespace art {
22
buzbeeb046e162012-10-30 15:48:42 -070023/* This file contains target-independent codegen and support. */
buzbee67bf8852011-08-17 17:51:35 -070024
buzbee31a4a6f2012-02-28 15:36:15 -080025/*
26 * Load an immediate value into a fixed or temp register. Target
buzbeefa57c472012-11-21 12:06:18 -080027 * register is clobbered, and marked in_use.
buzbee31a4a6f2012-02-28 15:36:15 -080028 */
buzbee02031b12012-11-23 09:41:35 -080029LIR* Codegen::LoadConstant(CompilationUnit* cu, int r_dest, int value)
buzbee31a4a6f2012-02-28 15:36:15 -080030{
buzbeefa57c472012-11-21 12:06:18 -080031 if (IsTemp(cu, r_dest)) {
32 Clobber(cu, r_dest);
33 MarkInUse(cu, r_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -070034 }
buzbeefa57c472012-11-21 12:06:18 -080035 return LoadConstantNoClobber(cu, r_dest, value);
buzbee31a4a6f2012-02-28 15:36:15 -080036}
buzbee67bf8852011-08-17 17:51:35 -070037
buzbee5f61f672012-11-28 17:22:17 -080038/*
39 * Temporary workaround for Issue 7250540. If we're loading a constant zero into a
40 * promoted floating point register, also copy a zero into the int/ref identity of
41 * that sreg.
42 */
buzbee7da142f2012-11-29 16:33:42 -080043void Codegen::Workaround7250540(CompilationUnit* cu, RegLocation rl_dest, int zero_reg)
buzbee5f61f672012-11-28 17:22:17 -080044{
buzbee7da142f2012-11-29 16:33:42 -080045 if (rl_dest.fp) {
buzbee5f61f672012-11-28 17:22:17 -080046 int pmap_index = SRegToPMap(cu, rl_dest.s_reg_low);
47 if (cu->promotion_map[pmap_index].fp_location == kLocPhysReg) {
buzbee7da142f2012-11-29 16:33:42 -080048 // Now, determine if this vreg is ever used as a reference. If not, we're done.
buzbeed8506212012-12-20 14:15:05 -080049 if (!cu->gen_bitcode) {
50 // TUNING: We no longer have this info for QuickGBC - assume the worst
51 bool used_as_reference = false;
52 int base_vreg = SRegToVReg(cu, rl_dest.s_reg_low);
53 for (int i = 0; !used_as_reference && (i < cu->num_ssa_regs); i++) {
54 if (SRegToVReg(cu, cu->reg_location[i].s_reg_low) == base_vreg) {
55 used_as_reference |= cu->reg_location[i].ref;
56 }
buzbee7da142f2012-11-29 16:33:42 -080057 }
buzbeed8506212012-12-20 14:15:05 -080058 if (!used_as_reference) {
59 return;
60 }
buzbee7da142f2012-11-29 16:33:42 -080061 }
buzbee5f61f672012-11-28 17:22:17 -080062 if (cu->promotion_map[pmap_index].core_location == kLocPhysReg) {
63 // Promoted - just copy in a zero
buzbee7da142f2012-11-29 16:33:42 -080064 OpRegCopy(cu, cu->promotion_map[pmap_index].core_reg, zero_reg);
buzbee5f61f672012-11-28 17:22:17 -080065 } else {
66 // Lives in the frame, need to store.
buzbee7da142f2012-11-29 16:33:42 -080067 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_dest.s_reg_low), zero_reg, kWord);
buzbee5f61f672012-11-28 17:22:17 -080068 }
69 }
70 }
71}
72
buzbee67bf8852011-08-17 17:51:35 -070073/* Load a word at base + displacement. Displacement must be word multiple */
buzbee02031b12012-11-23 09:41:35 -080074LIR* Codegen::LoadWordDisp(CompilationUnit* cu, int rBase, int displacement, int r_dest)
buzbee67bf8852011-08-17 17:51:35 -070075{
buzbeefa57c472012-11-21 12:06:18 -080076 return LoadBaseDisp(cu, rBase, displacement, r_dest, kWord,
Bill Buzbeea114add2012-05-03 15:00:40 -070077 INVALID_SREG);
buzbee67bf8852011-08-17 17:51:35 -070078}
79
buzbee02031b12012-11-23 09:41:35 -080080LIR* Codegen::StoreWordDisp(CompilationUnit* cu, int rBase, int displacement, int r_src)
buzbee67bf8852011-08-17 17:51:35 -070081{
buzbeefa57c472012-11-21 12:06:18 -080082 return StoreBaseDisp(cu, rBase, displacement, r_src, kWord);
buzbee67bf8852011-08-17 17:51:35 -070083}
84
85/*
86 * Load a Dalvik register into a physical register. Take care when
87 * using this routine, as it doesn't perform any bookkeeping regarding
88 * register liveness. That is the responsibility of the caller.
89 */
buzbee02031b12012-11-23 09:41:35 -080090void Codegen::LoadValueDirect(CompilationUnit* cu, RegLocation rl_src, int r_dest)
buzbee67bf8852011-08-17 17:51:35 -070091{
buzbeefa57c472012-11-21 12:06:18 -080092 rl_src = UpdateLoc(cu, rl_src);
93 if (rl_src.location == kLocPhysReg) {
94 OpRegCopy(cu, r_dest, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -070095 } else {
buzbeefa57c472012-11-21 12:06:18 -080096 DCHECK((rl_src.location == kLocDalvikFrame) ||
97 (rl_src.location == kLocCompilerTemp));
buzbeee6285f92012-12-06 15:57:46 -080098 if (rl_src.is_const && InexpensiveConstant(r_dest, cu->constant_values[rl_src.orig_sreg])) {
99 LoadConstantNoClobber(cu, r_dest, cu->constant_values[rl_src.orig_sreg]);
100 } else {
101 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_src.s_reg_low), r_dest);
102 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 }
buzbee67bf8852011-08-17 17:51:35 -0700104}
105
106/*
buzbee52a77fc2012-11-20 19:50:46 -0800107 * Similar to LoadValueDirect, but clobbers and allocates the target
buzbee67bf8852011-08-17 17:51:35 -0700108 * register. Should be used when loading to a fixed register (for example,
109 * loading arguments to an out of line call.
110 */
buzbee02031b12012-11-23 09:41:35 -0800111void Codegen::LoadValueDirectFixed(CompilationUnit* cu, RegLocation rl_src, int r_dest)
buzbee67bf8852011-08-17 17:51:35 -0700112{
buzbeefa57c472012-11-21 12:06:18 -0800113 Clobber(cu, r_dest);
114 MarkInUse(cu, r_dest);
115 LoadValueDirect(cu, rl_src, r_dest);
buzbee67bf8852011-08-17 17:51:35 -0700116}
117
118/*
119 * Load a Dalvik register pair into a physical register[s]. Take care when
120 * using this routine, as it doesn't perform any bookkeeping regarding
121 * register liveness. That is the responsibility of the caller.
122 */
buzbee02031b12012-11-23 09:41:35 -0800123void Codegen::LoadValueDirectWide(CompilationUnit* cu, RegLocation rl_src, int reg_lo,
buzbeefa57c472012-11-21 12:06:18 -0800124 int reg_hi)
buzbee67bf8852011-08-17 17:51:35 -0700125{
buzbeefa57c472012-11-21 12:06:18 -0800126 rl_src = UpdateLocWide(cu, rl_src);
127 if (rl_src.location == kLocPhysReg) {
128 OpRegCopyWide(cu, reg_lo, reg_hi, rl_src.low_reg, rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 } else {
buzbeefa57c472012-11-21 12:06:18 -0800130 DCHECK((rl_src.location == kLocDalvikFrame) ||
131 (rl_src.location == kLocCompilerTemp));
132 LoadBaseDispWide(cu, TargetReg(kSp), SRegOffset(cu, rl_src.s_reg_low),
133 reg_lo, reg_hi, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 }
buzbee67bf8852011-08-17 17:51:35 -0700135}
136
137/*
buzbee52a77fc2012-11-20 19:50:46 -0800138 * Similar to LoadValueDirect, but clobbers and allocates the target
buzbee67bf8852011-08-17 17:51:35 -0700139 * registers. Should be used when loading to a fixed registers (for example,
140 * loading arguments to an out of line call.
141 */
buzbee02031b12012-11-23 09:41:35 -0800142void Codegen::LoadValueDirectWideFixed(CompilationUnit* cu, RegLocation rl_src, int reg_lo,
143 int reg_hi)
buzbee67bf8852011-08-17 17:51:35 -0700144{
buzbeefa57c472012-11-21 12:06:18 -0800145 Clobber(cu, reg_lo);
146 Clobber(cu, reg_hi);
147 MarkInUse(cu, reg_lo);
148 MarkInUse(cu, reg_hi);
149 LoadValueDirectWide(cu, rl_src, reg_lo, reg_hi);
buzbee67bf8852011-08-17 17:51:35 -0700150}
151
buzbee02031b12012-11-23 09:41:35 -0800152RegLocation Codegen::LoadValue(CompilationUnit* cu, RegLocation rl_src, RegisterClass op_kind)
buzbee67bf8852011-08-17 17:51:35 -0700153{
buzbeefa57c472012-11-21 12:06:18 -0800154 rl_src = EvalLoc(cu, rl_src, op_kind, false);
155 if (rl_src.location != kLocPhysReg) {
156 DCHECK((rl_src.location == kLocDalvikFrame) ||
157 (rl_src.location == kLocCompilerTemp));
158 LoadValueDirect(cu, rl_src, rl_src.low_reg);
159 rl_src.location = kLocPhysReg;
160 MarkLive(cu, rl_src.low_reg, rl_src.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 }
buzbeefa57c472012-11-21 12:06:18 -0800162 return rl_src;
buzbee67bf8852011-08-17 17:51:35 -0700163}
164
buzbee02031b12012-11-23 09:41:35 -0800165void Codegen::StoreValue(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee67bf8852011-08-17 17:51:35 -0700166{
buzbee3d661942012-03-14 17:37:27 -0700167#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 /*
169 * Sanity checking - should never try to store to the same
170 * ssa name during the compilation of a single instruction
buzbee52a77fc2012-11-20 19:50:46 -0800171 * without an intervening ClobberSReg().
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 */
buzbeefa57c472012-11-21 12:06:18 -0800173 DCHECK((cu->live_sreg == INVALID_SREG) ||
174 (rl_dest.s_reg_low != cu->live_sreg));
175 cu->live_sreg = rl_dest.s_reg_low;
buzbee3d661942012-03-14 17:37:27 -0700176#endif
buzbeefa57c472012-11-21 12:06:18 -0800177 LIR* def_start;
178 LIR* def_end;
179 DCHECK(!rl_dest.wide);
180 DCHECK(!rl_src.wide);
181 rl_src = UpdateLoc(cu, rl_src);
182 rl_dest = UpdateLoc(cu, rl_dest);
183 if (rl_src.location == kLocPhysReg) {
184 if (IsLive(cu, rl_src.low_reg) ||
185 IsPromoted(cu, rl_src.low_reg) ||
186 (rl_dest.location == kLocPhysReg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 // Src is live/promoted or Dest has assigned reg.
buzbeefa57c472012-11-21 12:06:18 -0800188 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
189 OpRegCopy(cu, rl_dest.low_reg, rl_src.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700190 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 // Just re-assign the registers. Dest gets Src's regs
buzbeefa57c472012-11-21 12:06:18 -0800192 rl_dest.low_reg = rl_src.low_reg;
193 Clobber(cu, rl_src.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700194 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 } else {
196 // Load Src either into promoted Dest or temps allocated for Dest
buzbeefa57c472012-11-21 12:06:18 -0800197 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
198 LoadValueDirect(cu, rl_src, rl_dest.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 }
buzbee67bf8852011-08-17 17:51:35 -0700200
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 // Dest is now live and dirty (until/if we flush it to home location)
buzbeefa57c472012-11-21 12:06:18 -0800202 MarkLive(cu, rl_dest.low_reg, rl_dest.s_reg_low);
203 MarkDirty(cu, rl_dest);
buzbee67bf8852011-08-17 17:51:35 -0700204
205
buzbeefa57c472012-11-21 12:06:18 -0800206 ResetDefLoc(cu, rl_dest);
207 if (IsDirty(cu, rl_dest.low_reg) &&
208 oat_live_out(cu, rl_dest.s_reg_low)) {
209 def_start = cu->last_lir_insn;
210 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_dest.s_reg_low),
211 rl_dest.low_reg, kWord);
212 MarkClean(cu, rl_dest);
213 def_end = cu->last_lir_insn;
buzbee5f61f672012-11-28 17:22:17 -0800214 if (!rl_dest.ref) {
215 // Exclude references from store elimination
216 MarkDef(cu, rl_dest, def_start, def_end);
217 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700218 }
buzbee67bf8852011-08-17 17:51:35 -0700219}
220
buzbee02031b12012-11-23 09:41:35 -0800221RegLocation Codegen::LoadValueWide(CompilationUnit* cu, RegLocation rl_src, RegisterClass op_kind)
buzbee67bf8852011-08-17 17:51:35 -0700222{
buzbeefa57c472012-11-21 12:06:18 -0800223 DCHECK(rl_src.wide);
224 rl_src = EvalLoc(cu, rl_src, op_kind, false);
225 if (rl_src.location != kLocPhysReg) {
226 DCHECK((rl_src.location == kLocDalvikFrame) ||
227 (rl_src.location == kLocCompilerTemp));
228 LoadValueDirectWide(cu, rl_src, rl_src.low_reg, rl_src.high_reg);
229 rl_src.location = kLocPhysReg;
230 MarkLive(cu, rl_src.low_reg, rl_src.s_reg_low);
231 MarkLive(cu, rl_src.high_reg,
232 GetSRegHi(rl_src.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 }
buzbeefa57c472012-11-21 12:06:18 -0800234 return rl_src;
buzbee67bf8852011-08-17 17:51:35 -0700235}
236
buzbee02031b12012-11-23 09:41:35 -0800237void Codegen::StoreValueWide(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee67bf8852011-08-17 17:51:35 -0700238{
buzbee3d661942012-03-14 17:37:27 -0700239#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 /*
241 * Sanity checking - should never try to store to the same
242 * ssa name during the compilation of a single instruction
buzbee52a77fc2012-11-20 19:50:46 -0800243 * without an intervening ClobberSReg().
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 */
buzbeefa57c472012-11-21 12:06:18 -0800245 DCHECK((cu->live_sreg == INVALID_SREG) ||
246 (rl_dest.s_reg_low != cu->live_sreg));
247 cu->live_sreg = rl_dest.s_reg_low;
buzbee3d661942012-03-14 17:37:27 -0700248#endif
buzbeefa57c472012-11-21 12:06:18 -0800249 LIR* def_start;
250 LIR* def_end;
buzbee02031b12012-11-23 09:41:35 -0800251 DCHECK_EQ(IsFpReg(rl_src.low_reg), IsFpReg(rl_src.high_reg));
buzbeefa57c472012-11-21 12:06:18 -0800252 DCHECK(rl_dest.wide);
253 DCHECK(rl_src.wide);
254 if (rl_src.location == kLocPhysReg) {
255 if (IsLive(cu, rl_src.low_reg) ||
256 IsLive(cu, rl_src.high_reg) ||
257 IsPromoted(cu, rl_src.low_reg) ||
258 IsPromoted(cu, rl_src.high_reg) ||
259 (rl_dest.location == kLocPhysReg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 // Src is live or promoted or Dest has assigned reg.
buzbeefa57c472012-11-21 12:06:18 -0800261 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
262 OpRegCopyWide(cu, rl_dest.low_reg, rl_dest.high_reg,
263 rl_src.low_reg, rl_src.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700264 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 // Just re-assign the registers. Dest gets Src's regs
buzbeefa57c472012-11-21 12:06:18 -0800266 rl_dest.low_reg = rl_src.low_reg;
267 rl_dest.high_reg = rl_src.high_reg;
268 Clobber(cu, rl_src.low_reg);
269 Clobber(cu, rl_src.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700270 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 } else {
272 // Load Src either into promoted Dest or temps allocated for Dest
buzbeefa57c472012-11-21 12:06:18 -0800273 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
274 LoadValueDirectWide(cu, rl_src, rl_dest.low_reg, rl_dest.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 }
buzbee67bf8852011-08-17 17:51:35 -0700276
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 // Dest is now live and dirty (until/if we flush it to home location)
buzbeefa57c472012-11-21 12:06:18 -0800278 MarkLive(cu, rl_dest.low_reg, rl_dest.s_reg_low);
279 MarkLive(cu, rl_dest.high_reg, GetSRegHi(rl_dest.s_reg_low));
280 MarkDirty(cu, rl_dest);
281 MarkPair(cu, rl_dest.low_reg, rl_dest.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700282
283
buzbeefa57c472012-11-21 12:06:18 -0800284 ResetDefLocWide(cu, rl_dest);
285 if ((IsDirty(cu, rl_dest.low_reg) ||
286 IsDirty(cu, rl_dest.high_reg)) &&
287 (oat_live_out(cu, rl_dest.s_reg_low) ||
288 oat_live_out(cu, GetSRegHi(rl_dest.s_reg_low)))) {
289 def_start = cu->last_lir_insn;
290 DCHECK_EQ((SRegToVReg(cu, rl_dest.s_reg_low)+1),
291 SRegToVReg(cu, GetSRegHi(rl_dest.s_reg_low)));
292 StoreBaseDispWide(cu, TargetReg(kSp), SRegOffset(cu, rl_dest.s_reg_low),
293 rl_dest.low_reg, rl_dest.high_reg);
294 MarkClean(cu, rl_dest);
295 def_end = cu->last_lir_insn;
296 MarkDefWide(cu, rl_dest, def_start, def_end);
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 }
buzbee67bf8852011-08-17 17:51:35 -0700298}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800299
buzbeee1965672012-03-11 18:39:19 -0700300/* Utilities to load the current Method* */
buzbee02031b12012-11-23 09:41:35 -0800301void Codegen::LoadCurrMethodDirect(CompilationUnit *cu, int r_tgt)
buzbee31a4a6f2012-02-28 15:36:15 -0800302{
buzbeefa57c472012-11-21 12:06:18 -0800303 LoadValueDirectFixed(cu, cu->method_loc, r_tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800304}
305
buzbee02031b12012-11-23 09:41:35 -0800306RegLocation Codegen::LoadCurrMethod(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800307{
buzbeefa57c472012-11-21 12:06:18 -0800308 return LoadValue(cu, cu->method_loc, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800309}
310
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800311} // namespace art