blob: 7262c134dce8c1d258f008b06d58f79fa004c02c [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
27 * register is clobbered, and marked inUse.
28 */
buzbee52a77fc2012-11-20 19:50:46 -080029LIR* LoadConstant(CompilationUnit* cUnit, int rDest, int value)
buzbee31a4a6f2012-02-28 15:36:15 -080030{
buzbee52a77fc2012-11-20 19:50:46 -080031 if (IsTemp(cUnit, rDest)) {
32 Clobber(cUnit, rDest);
33 MarkInUse(cUnit, rDest);
Bill Buzbeea114add2012-05-03 15:00:40 -070034 }
buzbee52a77fc2012-11-20 19:50:46 -080035 return LoadConstantNoClobber(cUnit, rDest, value);
buzbee31a4a6f2012-02-28 15:36:15 -080036}
buzbee67bf8852011-08-17 17:51:35 -070037
38/* Load a word at base + displacement. Displacement must be word multiple */
buzbee52a77fc2012-11-20 19:50:46 -080039LIR* LoadWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
buzbee31a4a6f2012-02-28 15:36:15 -080040 int rDest)
buzbee67bf8852011-08-17 17:51:35 -070041{
buzbee52a77fc2012-11-20 19:50:46 -080042 return LoadBaseDisp(cUnit, rBase, displacement, rDest, kWord,
Bill Buzbeea114add2012-05-03 15:00:40 -070043 INVALID_SREG);
buzbee67bf8852011-08-17 17:51:35 -070044}
45
buzbee52a77fc2012-11-20 19:50:46 -080046LIR* StoreWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
buzbee31a4a6f2012-02-28 15:36:15 -080047 int rSrc)
buzbee67bf8852011-08-17 17:51:35 -070048{
buzbee52a77fc2012-11-20 19:50:46 -080049 return StoreBaseDisp(cUnit, rBase, displacement, rSrc, kWord);
buzbee67bf8852011-08-17 17:51:35 -070050}
51
52/*
53 * Load a Dalvik register into a physical register. Take care when
54 * using this routine, as it doesn't perform any bookkeeping regarding
55 * register liveness. That is the responsibility of the caller.
56 */
buzbee52a77fc2012-11-20 19:50:46 -080057void LoadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
buzbee67bf8852011-08-17 17:51:35 -070058{
buzbee52a77fc2012-11-20 19:50:46 -080059 rlSrc = UpdateLoc(cUnit, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -070060 if (rlSrc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -080061 OpRegCopy(cUnit, rDest, rlSrc.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -070062 } else {
63 DCHECK((rlSrc.location == kLocDalvikFrame) ||
64 (rlSrc.location == kLocCompilerTemp));
buzbee52a77fc2012-11-20 19:50:46 -080065 LoadWordDisp(cUnit, TargetReg(kSp), SRegOffset(cUnit, rlSrc.sRegLow), rDest);
Bill Buzbeea114add2012-05-03 15:00:40 -070066 }
buzbee67bf8852011-08-17 17:51:35 -070067}
68
69/*
buzbee52a77fc2012-11-20 19:50:46 -080070 * Similar to LoadValueDirect, but clobbers and allocates the target
buzbee67bf8852011-08-17 17:51:35 -070071 * register. Should be used when loading to a fixed register (for example,
72 * loading arguments to an out of line call.
73 */
buzbee52a77fc2012-11-20 19:50:46 -080074void LoadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
buzbee67bf8852011-08-17 17:51:35 -070075{
buzbee52a77fc2012-11-20 19:50:46 -080076 Clobber(cUnit, rDest);
77 MarkInUse(cUnit, rDest);
78 LoadValueDirect(cUnit, rlSrc, rDest);
buzbee67bf8852011-08-17 17:51:35 -070079}
80
81/*
82 * Load a Dalvik register pair into a physical register[s]. Take care when
83 * using this routine, as it doesn't perform any bookkeeping regarding
84 * register liveness. That is the responsibility of the caller.
85 */
buzbee52a77fc2012-11-20 19:50:46 -080086void LoadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc, int regLo,
Bill Buzbeea114add2012-05-03 15:00:40 -070087 int regHi)
buzbee67bf8852011-08-17 17:51:35 -070088{
buzbee52a77fc2012-11-20 19:50:46 -080089 rlSrc = UpdateLocWide(cUnit, rlSrc);
Bill Buzbeea114add2012-05-03 15:00:40 -070090 if (rlSrc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -080091 OpRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -070092 } else {
93 DCHECK((rlSrc.location == kLocDalvikFrame) ||
94 (rlSrc.location == kLocCompilerTemp));
buzbee52a77fc2012-11-20 19:50:46 -080095 LoadBaseDispWide(cUnit, TargetReg(kSp), SRegOffset(cUnit, rlSrc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -070096 regLo, regHi, INVALID_SREG);
97 }
buzbee67bf8852011-08-17 17:51:35 -070098}
99
100/*
buzbee52a77fc2012-11-20 19:50:46 -0800101 * Similar to LoadValueDirect, but clobbers and allocates the target
buzbee67bf8852011-08-17 17:51:35 -0700102 * registers. Should be used when loading to a fixed registers (for example,
103 * loading arguments to an out of line call.
104 */
buzbee52a77fc2012-11-20 19:50:46 -0800105void LoadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee31a4a6f2012-02-28 15:36:15 -0800106 int regLo, int regHi)
buzbee67bf8852011-08-17 17:51:35 -0700107{
buzbee52a77fc2012-11-20 19:50:46 -0800108 Clobber(cUnit, regLo);
109 Clobber(cUnit, regHi);
110 MarkInUse(cUnit, regLo);
111 MarkInUse(cUnit, regHi);
112 LoadValueDirectWide(cUnit, rlSrc, regLo, regHi);
buzbee67bf8852011-08-17 17:51:35 -0700113}
114
buzbee52a77fc2012-11-20 19:50:46 -0800115RegLocation LoadValue(CompilationUnit* cUnit, RegLocation rlSrc,
buzbee31a4a6f2012-02-28 15:36:15 -0800116 RegisterClass opKind)
buzbee67bf8852011-08-17 17:51:35 -0700117{
buzbee52a77fc2012-11-20 19:50:46 -0800118 rlSrc = EvalLoc(cUnit, rlSrc, opKind, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 if (rlSrc.location != kLocPhysReg) {
120 DCHECK((rlSrc.location == kLocDalvikFrame) ||
121 (rlSrc.location == kLocCompilerTemp));
buzbee52a77fc2012-11-20 19:50:46 -0800122 LoadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 rlSrc.location = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800124 MarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 }
126 return rlSrc;
buzbee67bf8852011-08-17 17:51:35 -0700127}
128
buzbee52a77fc2012-11-20 19:50:46 -0800129void StoreValue(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700130{
buzbee3d661942012-03-14 17:37:27 -0700131#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 /*
133 * Sanity checking - should never try to store to the same
134 * ssa name during the compilation of a single instruction
buzbee52a77fc2012-11-20 19:50:46 -0800135 * without an intervening ClobberSReg().
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 */
137 DCHECK((cUnit->liveSReg == INVALID_SREG) ||
138 (rlDest.sRegLow != cUnit->liveSReg));
139 cUnit->liveSReg = rlDest.sRegLow;
buzbee3d661942012-03-14 17:37:27 -0700140#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 LIR* defStart;
142 LIR* defEnd;
143 DCHECK(!rlDest.wide);
144 DCHECK(!rlSrc.wide);
buzbee52a77fc2012-11-20 19:50:46 -0800145 rlSrc = UpdateLoc(cUnit, rlSrc);
146 rlDest = UpdateLoc(cUnit, rlDest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 if (rlSrc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800148 if (IsLive(cUnit, rlSrc.lowReg) ||
149 IsPromoted(cUnit, rlSrc.lowReg) ||
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 (rlDest.location == kLocPhysReg)) {
151 // Src is live/promoted or Dest has assigned reg.
buzbee52a77fc2012-11-20 19:50:46 -0800152 rlDest = EvalLoc(cUnit, rlDest, kAnyReg, false);
153 OpRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700154 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 // Just re-assign the registers. Dest gets Src's regs
156 rlDest.lowReg = rlSrc.lowReg;
buzbee52a77fc2012-11-20 19:50:46 -0800157 Clobber(cUnit, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700158 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 } else {
160 // Load Src either into promoted Dest or temps allocated for Dest
buzbee52a77fc2012-11-20 19:50:46 -0800161 rlDest = EvalLoc(cUnit, rlDest, kAnyReg, false);
162 LoadValueDirect(cUnit, rlSrc, rlDest.lowReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 }
buzbee67bf8852011-08-17 17:51:35 -0700164
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 // Dest is now live and dirty (until/if we flush it to home location)
buzbee52a77fc2012-11-20 19:50:46 -0800166 MarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
167 MarkDirty(cUnit, rlDest);
buzbee67bf8852011-08-17 17:51:35 -0700168
169
buzbee52a77fc2012-11-20 19:50:46 -0800170 ResetDefLoc(cUnit, rlDest);
171 if (IsDirty(cUnit, rlDest.lowReg) &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 oatLiveOut(cUnit, rlDest.sRegLow)) {
buzbeecbd6d442012-11-17 14:11:25 -0800173 defStart = cUnit->lastLIRInsn;
buzbee52a77fc2012-11-20 19:50:46 -0800174 StoreBaseDisp(cUnit, TargetReg(kSp), SRegOffset(cUnit, rlDest.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 rlDest.lowReg, kWord);
buzbee52a77fc2012-11-20 19:50:46 -0800176 MarkClean(cUnit, rlDest);
buzbeecbd6d442012-11-17 14:11:25 -0800177 defEnd = cUnit->lastLIRInsn;
buzbee52a77fc2012-11-20 19:50:46 -0800178 MarkDef(cUnit, rlDest, defStart, defEnd);
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 }
buzbee67bf8852011-08-17 17:51:35 -0700180}
181
buzbee52a77fc2012-11-20 19:50:46 -0800182RegLocation LoadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 RegisterClass opKind)
buzbee67bf8852011-08-17 17:51:35 -0700184{
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 DCHECK(rlSrc.wide);
buzbee52a77fc2012-11-20 19:50:46 -0800186 rlSrc = EvalLoc(cUnit, rlSrc, opKind, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 if (rlSrc.location != kLocPhysReg) {
188 DCHECK((rlSrc.location == kLocDalvikFrame) ||
189 (rlSrc.location == kLocCompilerTemp));
buzbee52a77fc2012-11-20 19:50:46 -0800190 LoadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 rlSrc.location = kLocPhysReg;
buzbee52a77fc2012-11-20 19:50:46 -0800192 MarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
193 MarkLive(cUnit, rlSrc.highReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 oatSRegHi(rlSrc.sRegLow));
195 }
196 return rlSrc;
buzbee67bf8852011-08-17 17:51:35 -0700197}
198
buzbee52a77fc2012-11-20 19:50:46 -0800199void StoreValueWide(CompilationUnit* cUnit, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700201{
buzbee3d661942012-03-14 17:37:27 -0700202#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 /*
204 * Sanity checking - should never try to store to the same
205 * ssa name during the compilation of a single instruction
buzbee52a77fc2012-11-20 19:50:46 -0800206 * without an intervening ClobberSReg().
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 */
208 DCHECK((cUnit->liveSReg == INVALID_SREG) ||
209 (rlDest.sRegLow != cUnit->liveSReg));
210 cUnit->liveSReg = rlDest.sRegLow;
buzbee3d661942012-03-14 17:37:27 -0700211#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 LIR* defStart;
213 LIR* defEnd;
buzbee52a77fc2012-11-20 19:50:46 -0800214 DCHECK_EQ(FpReg(rlSrc.lowReg), FpReg(rlSrc.highReg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 DCHECK(rlDest.wide);
216 DCHECK(rlSrc.wide);
217 if (rlSrc.location == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800218 if (IsLive(cUnit, rlSrc.lowReg) ||
219 IsLive(cUnit, rlSrc.highReg) ||
220 IsPromoted(cUnit, rlSrc.lowReg) ||
221 IsPromoted(cUnit, rlSrc.highReg) ||
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 (rlDest.location == kLocPhysReg)) {
223 // Src is live or promoted or Dest has assigned reg.
buzbee52a77fc2012-11-20 19:50:46 -0800224 rlDest = EvalLoc(cUnit, rlDest, kAnyReg, false);
225 OpRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 rlSrc.lowReg, rlSrc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700227 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 // Just re-assign the registers. Dest gets Src's regs
229 rlDest.lowReg = rlSrc.lowReg;
230 rlDest.highReg = rlSrc.highReg;
buzbee52a77fc2012-11-20 19:50:46 -0800231 Clobber(cUnit, rlSrc.lowReg);
232 Clobber(cUnit, rlSrc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700233 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 } else {
235 // Load Src either into promoted Dest or temps allocated for Dest
buzbee52a77fc2012-11-20 19:50:46 -0800236 rlDest = EvalLoc(cUnit, rlDest, kAnyReg, false);
237 LoadValueDirectWide(cUnit, rlSrc, rlDest.lowReg, rlDest.highReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700238 }
buzbee67bf8852011-08-17 17:51:35 -0700239
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 // Dest is now live and dirty (until/if we flush it to home location)
buzbee52a77fc2012-11-20 19:50:46 -0800241 MarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
242 MarkLive(cUnit, rlDest.highReg, oatSRegHi(rlDest.sRegLow));
243 MarkDirty(cUnit, rlDest);
244 MarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700245
246
buzbee52a77fc2012-11-20 19:50:46 -0800247 ResetDefLocWide(cUnit, rlDest);
248 if ((IsDirty(cUnit, rlDest.lowReg) ||
249 IsDirty(cUnit, rlDest.highReg)) &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 (oatLiveOut(cUnit, rlDest.sRegLow) ||
251 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
buzbeecbd6d442012-11-17 14:11:25 -0800252 defStart = cUnit->lastLIRInsn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 DCHECK_EQ((SRegToVReg(cUnit, rlDest.sRegLow)+1),
254 SRegToVReg(cUnit, oatSRegHi(rlDest.sRegLow)));
buzbee52a77fc2012-11-20 19:50:46 -0800255 StoreBaseDispWide(cUnit, TargetReg(kSp), SRegOffset(cUnit, rlDest.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 rlDest.lowReg, rlDest.highReg);
buzbee52a77fc2012-11-20 19:50:46 -0800257 MarkClean(cUnit, rlDest);
buzbeecbd6d442012-11-17 14:11:25 -0800258 defEnd = cUnit->lastLIRInsn;
buzbee52a77fc2012-11-20 19:50:46 -0800259 MarkDefWide(cUnit, rlDest, defStart, defEnd);
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 }
buzbee67bf8852011-08-17 17:51:35 -0700261}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800262
buzbeee1965672012-03-11 18:39:19 -0700263/* Utilities to load the current Method* */
buzbee52a77fc2012-11-20 19:50:46 -0800264void LoadCurrMethodDirect(CompilationUnit *cUnit, int rTgt)
buzbee31a4a6f2012-02-28 15:36:15 -0800265{
buzbee52a77fc2012-11-20 19:50:46 -0800266 LoadValueDirectFixed(cUnit, cUnit->methodLoc, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800267}
268
buzbee52a77fc2012-11-20 19:50:46 -0800269RegLocation LoadCurrMethod(CompilationUnit *cUnit)
buzbee31a4a6f2012-02-28 15:36:15 -0800270{
buzbee52a77fc2012-11-20 19:50:46 -0800271 return LoadValue(cUnit, cUnit->methodLoc, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800272}
273
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800274} // namespace art