blob: fe08caa72c6b42882995e746503ede1934145b9f [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 "../compiler_ir.h"
18#include "ralloc_util.h"
19#include "codegen_util.h"
20
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.
49 bool used_as_reference = false;
50 int base_vreg = SRegToVReg(cu, rl_dest.s_reg_low);
51 for (int i = 0; !used_as_reference && (i < cu->num_ssa_regs); i++) {
52 if (SRegToVReg(cu, cu->reg_location[i].s_reg_low) == base_vreg) {
53 used_as_reference |= cu->reg_location[i].ref;
54 }
55 }
56 if (!used_as_reference) {
57 return;
58 }
buzbee5f61f672012-11-28 17:22:17 -080059 if (cu->promotion_map[pmap_index].core_location == kLocPhysReg) {
60 // Promoted - just copy in a zero
buzbee7da142f2012-11-29 16:33:42 -080061 OpRegCopy(cu, cu->promotion_map[pmap_index].core_reg, zero_reg);
buzbee5f61f672012-11-28 17:22:17 -080062 } else {
63 // Lives in the frame, need to store.
buzbee7da142f2012-11-29 16:33:42 -080064 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_dest.s_reg_low), zero_reg, kWord);
buzbee5f61f672012-11-28 17:22:17 -080065 }
66 }
67 }
68}
69
buzbee67bf8852011-08-17 17:51:35 -070070/* Load a word at base + displacement. Displacement must be word multiple */
buzbee02031b12012-11-23 09:41:35 -080071LIR* Codegen::LoadWordDisp(CompilationUnit* cu, int rBase, int displacement, int r_dest)
buzbee67bf8852011-08-17 17:51:35 -070072{
buzbeefa57c472012-11-21 12:06:18 -080073 return LoadBaseDisp(cu, rBase, displacement, r_dest, kWord,
Bill Buzbeea114add2012-05-03 15:00:40 -070074 INVALID_SREG);
buzbee67bf8852011-08-17 17:51:35 -070075}
76
buzbee02031b12012-11-23 09:41:35 -080077LIR* Codegen::StoreWordDisp(CompilationUnit* cu, int rBase, int displacement, int r_src)
buzbee67bf8852011-08-17 17:51:35 -070078{
buzbeefa57c472012-11-21 12:06:18 -080079 return StoreBaseDisp(cu, rBase, displacement, r_src, kWord);
buzbee67bf8852011-08-17 17:51:35 -070080}
81
82/*
83 * Load a Dalvik register into a physical register. Take care when
84 * using this routine, as it doesn't perform any bookkeeping regarding
85 * register liveness. That is the responsibility of the caller.
86 */
buzbee02031b12012-11-23 09:41:35 -080087void Codegen::LoadValueDirect(CompilationUnit* cu, RegLocation rl_src, int r_dest)
buzbee67bf8852011-08-17 17:51:35 -070088{
buzbeefa57c472012-11-21 12:06:18 -080089 rl_src = UpdateLoc(cu, rl_src);
90 if (rl_src.location == kLocPhysReg) {
91 OpRegCopy(cu, r_dest, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -070092 } else {
buzbeefa57c472012-11-21 12:06:18 -080093 DCHECK((rl_src.location == kLocDalvikFrame) ||
94 (rl_src.location == kLocCompilerTemp));
95 LoadWordDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_src.s_reg_low), r_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -070096 }
buzbee67bf8852011-08-17 17:51:35 -070097}
98
99/*
buzbee52a77fc2012-11-20 19:50:46 -0800100 * Similar to LoadValueDirect, but clobbers and allocates the target
buzbee67bf8852011-08-17 17:51:35 -0700101 * register. Should be used when loading to a fixed register (for example,
102 * loading arguments to an out of line call.
103 */
buzbee02031b12012-11-23 09:41:35 -0800104void Codegen::LoadValueDirectFixed(CompilationUnit* cu, RegLocation rl_src, int r_dest)
buzbee67bf8852011-08-17 17:51:35 -0700105{
buzbeefa57c472012-11-21 12:06:18 -0800106 Clobber(cu, r_dest);
107 MarkInUse(cu, r_dest);
108 LoadValueDirect(cu, rl_src, r_dest);
buzbee67bf8852011-08-17 17:51:35 -0700109}
110
111/*
112 * Load a Dalvik register pair into a physical register[s]. Take care when
113 * using this routine, as it doesn't perform any bookkeeping regarding
114 * register liveness. That is the responsibility of the caller.
115 */
buzbee02031b12012-11-23 09:41:35 -0800116void Codegen::LoadValueDirectWide(CompilationUnit* cu, RegLocation rl_src, int reg_lo,
buzbeefa57c472012-11-21 12:06:18 -0800117 int reg_hi)
buzbee67bf8852011-08-17 17:51:35 -0700118{
buzbeefa57c472012-11-21 12:06:18 -0800119 rl_src = UpdateLocWide(cu, rl_src);
120 if (rl_src.location == kLocPhysReg) {
121 OpRegCopyWide(cu, reg_lo, reg_hi, rl_src.low_reg, rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700122 } else {
buzbeefa57c472012-11-21 12:06:18 -0800123 DCHECK((rl_src.location == kLocDalvikFrame) ||
124 (rl_src.location == kLocCompilerTemp));
125 LoadBaseDispWide(cu, TargetReg(kSp), SRegOffset(cu, rl_src.s_reg_low),
126 reg_lo, reg_hi, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 }
buzbee67bf8852011-08-17 17:51:35 -0700128}
129
130/*
buzbee52a77fc2012-11-20 19:50:46 -0800131 * Similar to LoadValueDirect, but clobbers and allocates the target
buzbee67bf8852011-08-17 17:51:35 -0700132 * registers. Should be used when loading to a fixed registers (for example,
133 * loading arguments to an out of line call.
134 */
buzbee02031b12012-11-23 09:41:35 -0800135void Codegen::LoadValueDirectWideFixed(CompilationUnit* cu, RegLocation rl_src, int reg_lo,
136 int reg_hi)
buzbee67bf8852011-08-17 17:51:35 -0700137{
buzbeefa57c472012-11-21 12:06:18 -0800138 Clobber(cu, reg_lo);
139 Clobber(cu, reg_hi);
140 MarkInUse(cu, reg_lo);
141 MarkInUse(cu, reg_hi);
142 LoadValueDirectWide(cu, rl_src, reg_lo, reg_hi);
buzbee67bf8852011-08-17 17:51:35 -0700143}
144
buzbee02031b12012-11-23 09:41:35 -0800145RegLocation Codegen::LoadValue(CompilationUnit* cu, RegLocation rl_src, RegisterClass op_kind)
buzbee67bf8852011-08-17 17:51:35 -0700146{
buzbeefa57c472012-11-21 12:06:18 -0800147 rl_src = EvalLoc(cu, rl_src, op_kind, false);
148 if (rl_src.location != kLocPhysReg) {
149 DCHECK((rl_src.location == kLocDalvikFrame) ||
150 (rl_src.location == kLocCompilerTemp));
151 LoadValueDirect(cu, rl_src, rl_src.low_reg);
152 rl_src.location = kLocPhysReg;
153 MarkLive(cu, rl_src.low_reg, rl_src.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 }
buzbeefa57c472012-11-21 12:06:18 -0800155 return rl_src;
buzbee67bf8852011-08-17 17:51:35 -0700156}
157
buzbee02031b12012-11-23 09:41:35 -0800158void Codegen::StoreValue(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee67bf8852011-08-17 17:51:35 -0700159{
buzbee3d661942012-03-14 17:37:27 -0700160#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 /*
162 * Sanity checking - should never try to store to the same
163 * ssa name during the compilation of a single instruction
buzbee52a77fc2012-11-20 19:50:46 -0800164 * without an intervening ClobberSReg().
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 */
buzbeefa57c472012-11-21 12:06:18 -0800166 DCHECK((cu->live_sreg == INVALID_SREG) ||
167 (rl_dest.s_reg_low != cu->live_sreg));
168 cu->live_sreg = rl_dest.s_reg_low;
buzbee3d661942012-03-14 17:37:27 -0700169#endif
buzbeefa57c472012-11-21 12:06:18 -0800170 LIR* def_start;
171 LIR* def_end;
172 DCHECK(!rl_dest.wide);
173 DCHECK(!rl_src.wide);
174 rl_src = UpdateLoc(cu, rl_src);
175 rl_dest = UpdateLoc(cu, rl_dest);
176 if (rl_src.location == kLocPhysReg) {
177 if (IsLive(cu, rl_src.low_reg) ||
178 IsPromoted(cu, rl_src.low_reg) ||
179 (rl_dest.location == kLocPhysReg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 // Src is live/promoted or Dest has assigned reg.
buzbeefa57c472012-11-21 12:06:18 -0800181 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
182 OpRegCopy(cu, rl_dest.low_reg, rl_src.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700183 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 // Just re-assign the registers. Dest gets Src's regs
buzbeefa57c472012-11-21 12:06:18 -0800185 rl_dest.low_reg = rl_src.low_reg;
186 Clobber(cu, rl_src.low_reg);
buzbee67bf8852011-08-17 17:51:35 -0700187 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 } else {
189 // Load Src either into promoted Dest or temps allocated for Dest
buzbeefa57c472012-11-21 12:06:18 -0800190 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
191 LoadValueDirect(cu, rl_src, rl_dest.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 }
buzbee67bf8852011-08-17 17:51:35 -0700193
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 // Dest is now live and dirty (until/if we flush it to home location)
buzbeefa57c472012-11-21 12:06:18 -0800195 MarkLive(cu, rl_dest.low_reg, rl_dest.s_reg_low);
196 MarkDirty(cu, rl_dest);
buzbee67bf8852011-08-17 17:51:35 -0700197
198
buzbeefa57c472012-11-21 12:06:18 -0800199 ResetDefLoc(cu, rl_dest);
200 if (IsDirty(cu, rl_dest.low_reg) &&
201 oat_live_out(cu, rl_dest.s_reg_low)) {
202 def_start = cu->last_lir_insn;
203 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, rl_dest.s_reg_low),
204 rl_dest.low_reg, kWord);
205 MarkClean(cu, rl_dest);
206 def_end = cu->last_lir_insn;
buzbee5f61f672012-11-28 17:22:17 -0800207 if (!rl_dest.ref) {
208 // Exclude references from store elimination
209 MarkDef(cu, rl_dest, def_start, def_end);
210 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 }
buzbee67bf8852011-08-17 17:51:35 -0700212}
213
buzbee02031b12012-11-23 09:41:35 -0800214RegLocation Codegen::LoadValueWide(CompilationUnit* cu, RegLocation rl_src, RegisterClass op_kind)
buzbee67bf8852011-08-17 17:51:35 -0700215{
buzbeefa57c472012-11-21 12:06:18 -0800216 DCHECK(rl_src.wide);
217 rl_src = EvalLoc(cu, rl_src, op_kind, false);
218 if (rl_src.location != kLocPhysReg) {
219 DCHECK((rl_src.location == kLocDalvikFrame) ||
220 (rl_src.location == kLocCompilerTemp));
221 LoadValueDirectWide(cu, rl_src, rl_src.low_reg, rl_src.high_reg);
222 rl_src.location = kLocPhysReg;
223 MarkLive(cu, rl_src.low_reg, rl_src.s_reg_low);
224 MarkLive(cu, rl_src.high_reg,
225 GetSRegHi(rl_src.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 }
buzbeefa57c472012-11-21 12:06:18 -0800227 return rl_src;
buzbee67bf8852011-08-17 17:51:35 -0700228}
229
buzbee02031b12012-11-23 09:41:35 -0800230void Codegen::StoreValueWide(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee67bf8852011-08-17 17:51:35 -0700231{
buzbee3d661942012-03-14 17:37:27 -0700232#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 /*
234 * Sanity checking - should never try to store to the same
235 * ssa name during the compilation of a single instruction
buzbee52a77fc2012-11-20 19:50:46 -0800236 * without an intervening ClobberSReg().
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 */
buzbeefa57c472012-11-21 12:06:18 -0800238 DCHECK((cu->live_sreg == INVALID_SREG) ||
239 (rl_dest.s_reg_low != cu->live_sreg));
240 cu->live_sreg = rl_dest.s_reg_low;
buzbee3d661942012-03-14 17:37:27 -0700241#endif
buzbeefa57c472012-11-21 12:06:18 -0800242 LIR* def_start;
243 LIR* def_end;
buzbee02031b12012-11-23 09:41:35 -0800244 DCHECK_EQ(IsFpReg(rl_src.low_reg), IsFpReg(rl_src.high_reg));
buzbeefa57c472012-11-21 12:06:18 -0800245 DCHECK(rl_dest.wide);
246 DCHECK(rl_src.wide);
247 if (rl_src.location == kLocPhysReg) {
248 if (IsLive(cu, rl_src.low_reg) ||
249 IsLive(cu, rl_src.high_reg) ||
250 IsPromoted(cu, rl_src.low_reg) ||
251 IsPromoted(cu, rl_src.high_reg) ||
252 (rl_dest.location == kLocPhysReg)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 // Src is live or promoted or Dest has assigned reg.
buzbeefa57c472012-11-21 12:06:18 -0800254 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
255 OpRegCopyWide(cu, rl_dest.low_reg, rl_dest.high_reg,
256 rl_src.low_reg, rl_src.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700257 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 // Just re-assign the registers. Dest gets Src's regs
buzbeefa57c472012-11-21 12:06:18 -0800259 rl_dest.low_reg = rl_src.low_reg;
260 rl_dest.high_reg = rl_src.high_reg;
261 Clobber(cu, rl_src.low_reg);
262 Clobber(cu, rl_src.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700263 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 } else {
265 // Load Src either into promoted Dest or temps allocated for Dest
buzbeefa57c472012-11-21 12:06:18 -0800266 rl_dest = EvalLoc(cu, rl_dest, kAnyReg, false);
267 LoadValueDirectWide(cu, rl_src, rl_dest.low_reg, rl_dest.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 }
buzbee67bf8852011-08-17 17:51:35 -0700269
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 // Dest is now live and dirty (until/if we flush it to home location)
buzbeefa57c472012-11-21 12:06:18 -0800271 MarkLive(cu, rl_dest.low_reg, rl_dest.s_reg_low);
272 MarkLive(cu, rl_dest.high_reg, GetSRegHi(rl_dest.s_reg_low));
273 MarkDirty(cu, rl_dest);
274 MarkPair(cu, rl_dest.low_reg, rl_dest.high_reg);
buzbee67bf8852011-08-17 17:51:35 -0700275
276
buzbeefa57c472012-11-21 12:06:18 -0800277 ResetDefLocWide(cu, rl_dest);
278 if ((IsDirty(cu, rl_dest.low_reg) ||
279 IsDirty(cu, rl_dest.high_reg)) &&
280 (oat_live_out(cu, rl_dest.s_reg_low) ||
281 oat_live_out(cu, GetSRegHi(rl_dest.s_reg_low)))) {
282 def_start = cu->last_lir_insn;
283 DCHECK_EQ((SRegToVReg(cu, rl_dest.s_reg_low)+1),
284 SRegToVReg(cu, GetSRegHi(rl_dest.s_reg_low)));
285 StoreBaseDispWide(cu, TargetReg(kSp), SRegOffset(cu, rl_dest.s_reg_low),
286 rl_dest.low_reg, rl_dest.high_reg);
287 MarkClean(cu, rl_dest);
288 def_end = cu->last_lir_insn;
289 MarkDefWide(cu, rl_dest, def_start, def_end);
Bill Buzbeea114add2012-05-03 15:00:40 -0700290 }
buzbee67bf8852011-08-17 17:51:35 -0700291}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800292
buzbeee1965672012-03-11 18:39:19 -0700293/* Utilities to load the current Method* */
buzbee02031b12012-11-23 09:41:35 -0800294void Codegen::LoadCurrMethodDirect(CompilationUnit *cu, int r_tgt)
buzbee31a4a6f2012-02-28 15:36:15 -0800295{
buzbeefa57c472012-11-21 12:06:18 -0800296 LoadValueDirectFixed(cu, cu->method_loc, r_tgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800297}
298
buzbee02031b12012-11-23 09:41:35 -0800299RegLocation Codegen::LoadCurrMethod(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800300{
buzbeefa57c472012-11-21 12:06:18 -0800301 return LoadValue(cu, cu->method_loc, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800302}
303
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800304} // namespace art