blob: 996b5b7afb05b4cff7e0b4155ee3f88321388812 [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 */
29LIR* loadConstant(CompilationUnit* cUnit, int rDest, int value)
30{
Bill Buzbeea114add2012-05-03 15:00:40 -070031 if (oatIsTemp(cUnit, rDest)) {
32 oatClobber(cUnit, rDest);
33 oatMarkInUse(cUnit, rDest);
34 }
35 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 */
buzbee31a4a6f2012-02-28 15:36:15 -080039LIR* loadWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
40 int rDest)
buzbee67bf8852011-08-17 17:51:35 -070041{
buzbee408ad162012-06-06 16:45:18 -070042 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
buzbee31a4a6f2012-02-28 15:36:15 -080046LIR* storeWordDisp(CompilationUnit* cUnit, int rBase, int displacement,
47 int rSrc)
buzbee67bf8852011-08-17 17:51:35 -070048{
Bill Buzbeea114add2012-05-03 15:00:40 -070049 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 */
Ian Rogersab2b55d2012-03-18 00:06:11 -070057void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
buzbee67bf8852011-08-17 17:51:35 -070058{
Bill Buzbeea114add2012-05-03 15:00:40 -070059 rlSrc = oatUpdateLoc(cUnit, rlSrc);
60 if (rlSrc.location == kLocPhysReg) {
61 opRegCopy(cUnit, rDest, rlSrc.lowReg);
62 } else {
63 DCHECK((rlSrc.location == kLocDalvikFrame) ||
64 (rlSrc.location == kLocCompilerTemp));
buzbeef0504cd2012-11-13 16:31:10 -080065 loadWordDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlSrc.sRegLow), rDest);
Bill Buzbeea114add2012-05-03 15:00:40 -070066 }
buzbee67bf8852011-08-17 17:51:35 -070067}
68
69/*
70 * Similar to loadValueDirect, but clobbers and allocates the target
71 * register. Should be used when loading to a fixed register (for example,
72 * loading arguments to an out of line call.
73 */
Ian Rogersab2b55d2012-03-18 00:06:11 -070074void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc, int rDest)
buzbee67bf8852011-08-17 17:51:35 -070075{
Bill Buzbeea114add2012-05-03 15:00:40 -070076 oatClobber(cUnit, rDest);
77 oatMarkInUse(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 */
buzbee31a4a6f2012-02-28 15:36:15 -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{
Bill Buzbeea114add2012-05-03 15:00:40 -070089 rlSrc = oatUpdateLocWide(cUnit, rlSrc);
90 if (rlSrc.location == kLocPhysReg) {
91 opRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg);
92 } else {
93 DCHECK((rlSrc.location == kLocDalvikFrame) ||
94 (rlSrc.location == kLocCompilerTemp));
buzbeef0504cd2012-11-13 16:31:10 -080095 loadBaseDispWide(cUnit, targetReg(kSp), oatSRegOffset(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/*
101 * Similar to loadValueDirect, but clobbers and allocates the target
102 * registers. Should be used when loading to a fixed registers (for example,
103 * loading arguments to an out of line call.
104 */
buzbee31a4a6f2012-02-28 15:36:15 -0800105void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc,
106 int regLo, int regHi)
buzbee67bf8852011-08-17 17:51:35 -0700107{
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 oatClobber(cUnit, regLo);
109 oatClobber(cUnit, regHi);
110 oatMarkInUse(cUnit, regLo);
111 oatMarkInUse(cUnit, regHi);
112 loadValueDirectWide(cUnit, rlSrc, regLo, regHi);
buzbee67bf8852011-08-17 17:51:35 -0700113}
114
buzbee31a4a6f2012-02-28 15:36:15 -0800115RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc,
116 RegisterClass opKind)
buzbee67bf8852011-08-17 17:51:35 -0700117{
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
119 if (rlSrc.location != kLocPhysReg) {
120 DCHECK((rlSrc.location == kLocDalvikFrame) ||
121 (rlSrc.location == kLocCompilerTemp));
122 loadValueDirect(cUnit, rlSrc, rlSrc.lowReg);
123 rlSrc.location = kLocPhysReg;
124 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
125 }
126 return rlSrc;
buzbee67bf8852011-08-17 17:51:35 -0700127}
128
buzbee31a4a6f2012-02-28 15:36:15 -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
135 * without an intervening oatClobberSReg().
136 */
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);
145 rlSrc = oatUpdateLoc(cUnit, rlSrc);
146 rlDest = oatUpdateLoc(cUnit, rlDest);
147 if (rlSrc.location == kLocPhysReg) {
148 if (oatIsLive(cUnit, rlSrc.lowReg) ||
149 oatIsPromoted(cUnit, rlSrc.lowReg) ||
150 (rlDest.location == kLocPhysReg)) {
151 // Src is live/promoted or Dest has assigned reg.
152 rlDest = oatEvalLoc(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;
157 oatClobber(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
161 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
162 loadValueDirect(cUnit, rlSrc, rlDest.lowReg);
163 }
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)
166 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
167 oatMarkDirty(cUnit, rlDest);
buzbee67bf8852011-08-17 17:51:35 -0700168
169
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 oatResetDefLoc(cUnit, rlDest);
171 if (oatIsDirty(cUnit, rlDest.lowReg) &&
172 oatLiveOut(cUnit, rlDest.sRegLow)) {
buzbeecbd6d442012-11-17 14:11:25 -0800173 defStart = cUnit->lastLIRInsn;
buzbeef0504cd2012-11-13 16:31:10 -0800174 storeBaseDisp(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlDest.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 rlDest.lowReg, kWord);
176 oatMarkClean(cUnit, rlDest);
buzbeecbd6d442012-11-17 14:11:25 -0800177 defEnd = cUnit->lastLIRInsn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 oatMarkDef(cUnit, rlDest, defStart, defEnd);
179 }
buzbee67bf8852011-08-17 17:51:35 -0700180}
181
buzbee31a4a6f2012-02-28 15:36:15 -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);
186 rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false);
187 if (rlSrc.location != kLocPhysReg) {
188 DCHECK((rlSrc.location == kLocDalvikFrame) ||
189 (rlSrc.location == kLocCompilerTemp));
190 loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg);
191 rlSrc.location = kLocPhysReg;
192 oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow);
193 oatMarkLive(cUnit, rlSrc.highReg,
194 oatSRegHi(rlSrc.sRegLow));
195 }
196 return rlSrc;
buzbee67bf8852011-08-17 17:51:35 -0700197}
198
buzbee31a4a6f2012-02-28 15:36:15 -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
206 * without an intervening oatClobberSReg().
207 */
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;
buzbeef0504cd2012-11-13 16:31:10 -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) {
218 if (oatIsLive(cUnit, rlSrc.lowReg) ||
219 oatIsLive(cUnit, rlSrc.highReg) ||
220 oatIsPromoted(cUnit, rlSrc.lowReg) ||
221 oatIsPromoted(cUnit, rlSrc.highReg) ||
222 (rlDest.location == kLocPhysReg)) {
223 // Src is live or promoted or Dest has assigned reg.
224 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
225 opRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg,
226 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;
231 oatClobber(cUnit, rlSrc.lowReg);
232 oatClobber(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
236 rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false);
237 loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg, rlDest.highReg);
238 }
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)
241 oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow);
242 oatMarkLive(cUnit, rlDest.highReg, oatSRegHi(rlDest.sRegLow));
243 oatMarkDirty(cUnit, rlDest);
244 oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700245
246
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 oatResetDefLocWide(cUnit, rlDest);
248 if ((oatIsDirty(cUnit, rlDest.lowReg) ||
249 oatIsDirty(cUnit, rlDest.highReg)) &&
250 (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)));
buzbeef0504cd2012-11-13 16:31:10 -0800255 storeBaseDispWide(cUnit, targetReg(kSp), oatSRegOffset(cUnit, rlDest.sRegLow),
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 rlDest.lowReg, rlDest.highReg);
257 oatMarkClean(cUnit, rlDest);
buzbeecbd6d442012-11-17 14:11:25 -0800258 defEnd = cUnit->lastLIRInsn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 oatMarkDefWide(cUnit, rlDest, defStart, defEnd);
260 }
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* */
buzbee31a4a6f2012-02-28 15:36:15 -0800264void loadCurrMethodDirect(CompilationUnit *cUnit, int rTgt)
265{
buzbeead8f15e2012-06-18 14:49:45 -0700266 loadValueDirectFixed(cUnit, cUnit->methodLoc, rTgt);
buzbee31a4a6f2012-02-28 15:36:15 -0800267}
268
buzbeee1965672012-03-11 18:39:19 -0700269RegLocation loadCurrMethod(CompilationUnit *cUnit)
buzbee31a4a6f2012-02-28 15:36:15 -0800270{
buzbeead8f15e2012-06-18 14:49:45 -0700271 return loadValue(cUnit, cUnit->methodLoc, kCoreReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800272}
273
buzbee9c044ce2012-03-18 13:24:07 -0700274bool methodStarInReg(CompilationUnit* cUnit)
275{
buzbeead8f15e2012-06-18 14:49:45 -0700276 return (cUnit->methodLoc.location == kLocPhysReg);
buzbee9c044ce2012-03-18 13:24:07 -0700277}
278
buzbee31a4a6f2012-02-28 15:36:15 -0800279
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800280} // namespace art