blob: 600b3248410479a4dc77f66fb509650205494ab0 [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
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080017namespace art {
18
buzbeeb046e162012-10-30 15:48:42 -070019/* This file contains target-independent codegen and support. */
buzbee67bf8852011-08-17 17:51:35 -070020
buzbee31a4a6f2012-02-28 15:36:15 -080021/*
22 * Load an immediate value into a fixed or temp register. Target
23 * register is clobbered, and marked inUse.
24 */
25LIR* loadConstant(CompilationUnit* cUnit, int rDest, int value)
26{
Bill Buzbeea114add2012-05-03 15:00:40 -070027 if (oatIsTemp(cUnit, rDest)) {
28 oatClobber(cUnit, rDest);
29 oatMarkInUse(cUnit, rDest);
30 }
31 return loadConstantNoClobber(cUnit, rDest, value);
buzbee31a4a6f2012-02-28 15:36:15 -080032}
buzbee67bf8852011-08-17 17:51:35 -070033
34/* Load a word at base + displacement. Displacement must be word multiple */
buzbee31a4a6f2012-02-28 15:36:15 -080035LIR* loadWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
36 int rDest)
buzbee67bf8852011-08-17 17:51:35 -070037{
buzbee408ad162012-06-06 16:45:18 -070038 return loadBaseDisp(cUnit, rBase, displacement, rDest, kWord,
Bill Buzbeea114add2012-05-03 15:00:40 -070039 INVALID_SREG);
buzbee67bf8852011-08-17 17:51:35 -070040}
41
buzbee31a4a6f2012-02-28 15:36:15 -080042LIR* storeWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
43 int rSrc)
buzbee67bf8852011-08-17 17:51:35 -070044{
Bill Buzbeea114add2012-05-03 15:00:40 -070045 return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord);
buzbee67bf8852011-08-17 17:51:35 -070046}
47
48/*
49 * Load a Dalvik register into a physical register. Take care when
50 * using this routine, as it doesn't perform any bookkeeping regarding
51 * register liveness. That is the responsibility of the caller.
52 */
Ian Rogersab2b55d2012-03-18 00:06:11 -070053void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
buzbee67bf8852011-08-17 17:51:35 -070054{
Bill Buzbeea114add2012-05-03 15:00:40 -070055 rlSrc = oatUpdateLoc(cUnit, rlSrc);
56 if (rlSrc.location == kLocPhysReg) {
57 opRegCopy(cUnit, rDest, rlSrc.lowReg);
58 } else {
59 DCHECK((rlSrc.location == kLocDalvikFrame) ||
60 (rlSrc.location == kLocCompilerTemp));
buzbeef0504cd2012-11-13 16:31:10 -080061 loadWordDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlSrc.sRegLow), rDest);
Bill Buzbeea114add2012-05-03 15:00:40 -070062 }
buzbee67bf8852011-08-17 17:51:35 -070063}
64
65/*
66 * Similar to loadValueDirect, but clobbers and allocates the target
67 * register. Should be used when loading to a fixed register (for example,
68 * loading arguments to an out of line call.
69 */
Ian Rogersab2b55d2012-03-18 00:06:11 -070070void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
buzbee67bf8852011-08-17 17:51:35 -070071{
Bill Buzbeea114add2012-05-03 15:00:40 -070072 oatClobber(cUnit, rDest);
73 oatMarkInUse(cUnit, rDest);
74 loadValueDirect(cUnit, rlSrc, rDest);
buzbee67bf8852011-08-17 17:51:35 -070075}
76
77/*
78 * Load a Dalvik register pair into a physical register[s]. Take care when
79 * using this routine, as it doesn't perform any bookkeeping regarding
80 * register liveness. That is the responsibility of the caller.
81 */
buzbee31a4a6f2012-02-28 15:36:15 -080082void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc, int regLo,
Bill Buzbeea114add2012-05-03 15:00:40 -070083 int regHi)
buzbee67bf8852011-08-17 17:51:35 -070084{
Bill Buzbeea114add2012-05-03 15:00:40 -070085 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
86 if (rlSrc.location == kLocPhysReg) {
87 opRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
88 } else {
89 DCHECK((rlSrc.location == kLocDalvikFrame) ||
90 (rlSrc.location == kLocCompilerTemp));
buzbeef0504cd2012-11-13 16:31:10 -080091 loadBaseDispWide(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlSrc.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -070092 regLo, regHi, INVALID_SREG);
93 }
buzbee67bf8852011-08-17 17:51:35 -070094}
95
96/*
97 * Similar to loadValueDirect, but clobbers and allocates the target
98 * registers. Should be used when loading to a fixed registers (for example,
99 * loading arguments to an out of line call.
100 */
buzbee31a4a6f2012-02-28 15:36:15 -0800101void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
102 int regLo, int regHi)
buzbee67bf8852011-08-17 17:51:35 -0700103{
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 oatClobber(cUnit, regLo);
105 oatClobber(cUnit, regHi);
106 oatMarkInUse(cUnit, regLo);
107 oatMarkInUse(cUnit, regHi);
108 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
buzbee67bf8852011-08-17 17:51:35 -0700109}
110
buzbee31a4a6f2012-02-28 15:36:15 -0800111RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
112 RegisterClass opKind)
buzbee67bf8852011-08-17 17:51:35 -0700113{
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
115 if (rlSrc.location != kLocPhysReg) {
116 DCHECK((rlSrc.location == kLocDalvikFrame) ||
117 (rlSrc.location == kLocCompilerTemp));
118 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
119 rlSrc.location = kLocPhysReg;
120 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
121 }
122 return rlSrc;
buzbee67bf8852011-08-17 17:51:35 -0700123}
124
buzbee31a4a6f2012-02-28 15:36:15 -0800125void storeValue(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700126{
buzbee3d661942012-03-14 17:37:27 -0700127#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 /*
129 * Sanity checking - should never try to store to the same
130 * ssa name during the compilation of a single instruction
131 * without an intervening oatClobberSReg().
132 */
133 DCHECK((cUnit->liveSReg == INVALID_SREG) ||
134 (rlDest.sRegLow != cUnit->liveSReg));
135 cUnit->liveSReg = rlDest.sRegLow;
buzbee3d661942012-03-14 17:37:27 -0700136#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 LIR* defStart;
138 LIR* defEnd;
139 DCHECK(!rlDest.wide);
140 DCHECK(!rlSrc.wide);
141 rlSrc = oatUpdateLoc(cUnit, rlSrc);
142 rlDest = oatUpdateLoc(cUnit, rlDest);
143 if (rlSrc.location == kLocPhysReg) {
144 if (oatIsLive(cUnit, rlSrc.lowReg) ||
145 oatIsPromoted(cUnit, rlSrc.lowReg) ||
146 (rlDest.location == kLocPhysReg)) {
147 // Src is live/promoted or Dest has assigned reg.
148 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
149 opRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700150 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 // Just re-assign the registers. Dest gets Src's regs
152 rlDest.lowReg = rlSrc.lowReg;
153 oatClobber(cUnit, rlSrc.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700154 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 } else {
156 // Load Src either into promoted Dest or temps allocated for Dest
157 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
158 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
159 }
buzbee67bf8852011-08-17 17:51:35 -0700160
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 // Dest is now live and dirty (until/if we flush it to home location)
162 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
163 oatMarkDirty(cUnit, rlDest);
buzbee67bf8852011-08-17 17:51:35 -0700164
165
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 oatResetDefLoc(cUnit, rlDest);
167 if (oatIsDirty(cUnit, rlDest.lowReg) &&
168 oatLiveOut(cUnit, rlDest.sRegLow)) {
169 defStart = (LIR* )cUnit->lastLIRInsn;
buzbeef0504cd2012-11-13 16:31:10 -0800170 storeBaseDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlDest.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 rlDest.lowReg, kWord);
172 oatMarkClean(cUnit, rlDest);
173 defEnd = (LIR* )cUnit->lastLIRInsn;
174 oatMarkDef(cUnit, rlDest, defStart, defEnd);
175 }
buzbee67bf8852011-08-17 17:51:35 -0700176}
177
buzbee31a4a6f2012-02-28 15:36:15 -0800178RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc,
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 RegisterClass opKind)
buzbee67bf8852011-08-17 17:51:35 -0700180{
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 DCHECK(rlSrc.wide);
182 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
183 if (rlSrc.location != kLocPhysReg) {
184 DCHECK((rlSrc.location == kLocDalvikFrame) ||
185 (rlSrc.location == kLocCompilerTemp));
186 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
187 rlSrc.location = kLocPhysReg;
188 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
189 oatMarkLive(cUnit, rlSrc.highReg,
190 oatSRegHi(rlSrc.sRegLow));
191 }
192 return rlSrc;
buzbee67bf8852011-08-17 17:51:35 -0700193}
194
buzbee31a4a6f2012-02-28 15:36:15 -0800195void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700197{
buzbee3d661942012-03-14 17:37:27 -0700198#ifndef NDEBUG
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 /*
200 * Sanity checking - should never try to store to the same
201 * ssa name during the compilation of a single instruction
202 * without an intervening oatClobberSReg().
203 */
204 DCHECK((cUnit->liveSReg == INVALID_SREG) ||
205 (rlDest.sRegLow != cUnit->liveSReg));
206 cUnit->liveSReg = rlDest.sRegLow;
buzbee3d661942012-03-14 17:37:27 -0700207#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 LIR* defStart;
209 LIR* defEnd;
buzbeef0504cd2012-11-13 16:31:10 -0800210 DCHECK_EQ(fpReg(rlSrc.lowReg), fpReg(rlSrc.highReg));
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 DCHECK(rlDest.wide);
212 DCHECK(rlSrc.wide);
213 if (rlSrc.location == kLocPhysReg) {
214 if (oatIsLive(cUnit, rlSrc.lowReg) ||
215 oatIsLive(cUnit, rlSrc.highReg) ||
216 oatIsPromoted(cUnit, rlSrc.lowReg) ||
217 oatIsPromoted(cUnit, rlSrc.highReg) ||
218 (rlDest.location == kLocPhysReg)) {
219 // Src is live or promoted or Dest has assigned reg.
220 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
221 opRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
222 rlSrc.lowReg, rlSrc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700223 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 // Just re-assign the registers. Dest gets Src's regs
225 rlDest.lowReg = rlSrc.lowReg;
226 rlDest.highReg = rlSrc.highReg;
227 oatClobber(cUnit, rlSrc.lowReg);
228 oatClobber(cUnit, rlSrc.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700229 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 } else {
231 // Load Src either into promoted Dest or temps allocated for Dest
232 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
233 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg, rlDest.highReg);
234 }
buzbee67bf8852011-08-17 17:51:35 -0700235
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 // Dest is now live and dirty (until/if we flush it to home location)
237 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
238 oatMarkLive(cUnit, rlDest.highReg, oatSRegHi(rlDest.sRegLow));
239 oatMarkDirty(cUnit, rlDest);
240 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700241
242
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 oatResetDefLocWide(cUnit, rlDest);
244 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
245 oatIsDirty(cUnit, rlDest.highReg)) &&
246 (oatLiveOut(cUnit, rlDest.sRegLow) ||
247 oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) {
248 defStart = (LIR*)cUnit->lastLIRInsn;
249 DCHECK_EQ((SRegToVReg(cUnit, rlDest.sRegLow)+1),
250 SRegToVReg(cUnit, oatSRegHi(rlDest.sRegLow)));
buzbeef0504cd2012-11-13 16:31:10 -0800251 storeBaseDispWide(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlDest.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 rlDest.lowReg, rlDest.highReg);
253 oatMarkClean(cUnit, rlDest);
254 defEnd = (LIR*)cUnit->lastLIRInsn;
255 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
256 }
buzbee67bf8852011-08-17 17:51:35 -0700257}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800258
buzbeee1965672012-03-11 18:39:19 -0700259/* Utilities to load the current Method* */
buzbee31a4a6f2012-02-28 15:36:15 -0800260void loadCurrMethodDirect(CompilationUnit *cUnit, int rTgt)
261{
buzbeead8f15e2012-06-18 14:49:45 -0700262 loadValueDirectFixed(cUnit, cUnit->methodLoc, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800263}
264
buzbeee1965672012-03-11 18:39:19 -0700265RegLocation loadCurrMethod(CompilationUnit *cUnit)
buzbee31a4a6f2012-02-28 15:36:15 -0800266{
buzbeead8f15e2012-06-18 14:49:45 -0700267 return loadValue(cUnit, cUnit->methodLoc, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800268}
269
buzbee9c044ce2012-03-18 13:24:07 -0700270bool methodStarInReg(CompilationUnit* cUnit)
271{
buzbeead8f15e2012-06-18 14:49:45 -0700272 return (cUnit->methodLoc.location == kLocPhysReg);
buzbee9c044ce2012-03-18 13:24:07 -0700273}
274
buzbee31a4a6f2012-02-28 15:36:15 -0800275
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800276} // namespace art