blob: 26c17ef17bd5bd038d84ed8f260568dfe7401f0f [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
buzbee67bf8852011-08-17 17:51:35 -070019/*
20 * This file contains codegen and support common to all supported
21 * ARM variants. It is included by:
22 *
23 * Codegen-$(TARGET_ARCH_VARIANT).c
24 *
25 * which combines this common code with specific support found in the
26 * applicable directory below this one.
27 */
28
29/* Track exercised opcodes */
30static int opcodeCoverage[kNumPackedOpcodes];
31
buzbeeed3e9302011-09-23 17:34:19 -070032STATIC void setMemRefType(ArmLIR* lir, bool isLoad, int memType)
buzbee67bf8852011-08-17 17:51:35 -070033{
34 u8 *maskPtr;
35 u8 mask = ENCODE_MEM;;
buzbeeed3e9302011-09-23 17:34:19 -070036 DCHECK(EncodingMap[lir->opcode].flags & (IS_LOAD | IS_STORE));
buzbee67bf8852011-08-17 17:51:35 -070037 if (isLoad) {
38 maskPtr = &lir->useMask;
39 } else {
40 maskPtr = &lir->defMask;
41 }
42 /* Clear out the memref flags */
43 *maskPtr &= ~mask;
44 /* ..and then add back the one we need */
45 switch(memType) {
46 case kLiteral:
buzbeeed3e9302011-09-23 17:34:19 -070047 DCHECK(isLoad);
buzbee67bf8852011-08-17 17:51:35 -070048 *maskPtr |= ENCODE_LITERAL;
49 break;
50 case kDalvikReg:
51 *maskPtr |= ENCODE_DALVIK_REG;
52 break;
53 case kHeapRef:
54 *maskPtr |= ENCODE_HEAP_REF;
55 break;
56 case kMustNotAlias:
57 /* Currently only loads can be marked as kMustNotAlias */
buzbeeed3e9302011-09-23 17:34:19 -070058 DCHECK(!(EncodingMap[lir->opcode].flags & IS_STORE));
buzbee67bf8852011-08-17 17:51:35 -070059 *maskPtr |= ENCODE_MUST_NOT_ALIAS;
60 break;
61 default:
62 LOG(FATAL) << "Oat: invalid memref kind - " << memType;
63 }
64}
65
66/*
67 * Mark load/store instructions that access Dalvik registers through r5FP +
68 * offset.
69 */
buzbeeed3e9302011-09-23 17:34:19 -070070STATIC void annotateDalvikRegAccess(ArmLIR* lir, int regId, bool isLoad)
buzbee67bf8852011-08-17 17:51:35 -070071{
72 setMemRefType(lir, isLoad, kDalvikReg);
73
74 /*
75 * Store the Dalvik register id in aliasInfo. Mark he MSB if it is a 64-bit
76 * access.
77 */
78 lir->aliasInfo = regId;
79 if (DOUBLEREG(lir->operands[0])) {
80 lir->aliasInfo |= 0x80000000;
81 }
82}
83
84/*
85 * Decode the register id.
86 */
buzbeeed3e9302011-09-23 17:34:19 -070087STATIC inline u8 getRegMaskCommon(int reg)
buzbee67bf8852011-08-17 17:51:35 -070088{
89 u8 seed;
90 int shift;
91 int regId = reg & 0x1f;
92
93 /*
94 * Each double register is equal to a pair of single-precision FP registers
95 */
96 seed = DOUBLEREG(reg) ? 3 : 1;
97 /* FP register starts at bit position 16 */
98 shift = FPREG(reg) ? kFPReg0 : 0;
99 /* Expand the double register id into single offset */
100 shift += regId;
101 return (seed << shift);
102}
103
104/*
105 * Mark the corresponding bit(s).
106 */
buzbeeed3e9302011-09-23 17:34:19 -0700107STATIC inline void setupRegMask(u8* mask, int reg)
buzbee67bf8852011-08-17 17:51:35 -0700108{
109 *mask |= getRegMaskCommon(reg);
110}
111
112/*
113 * Set up the proper fields in the resource mask
114 */
buzbeeed3e9302011-09-23 17:34:19 -0700115STATIC void setupResourceMasks(ArmLIR* lir)
buzbee67bf8852011-08-17 17:51:35 -0700116{
117 int opcode = lir->opcode;
118 int flags;
119
120 if (opcode <= 0) {
121 lir->useMask = lir->defMask = 0;
122 return;
123 }
124
125 flags = EncodingMap[lir->opcode].flags;
126
127 /* Set up the mask for resources that are updated */
128 if (flags & (IS_LOAD | IS_STORE)) {
129 /* Default to heap - will catch specialized classes later */
130 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
131 }
132
133 /*
134 * Conservatively assume the branch here will call out a function that in
135 * turn will trash everything.
136 */
137 if (flags & IS_BRANCH) {
138 lir->defMask = lir->useMask = ENCODE_ALL;
139 return;
140 }
141
142 if (flags & REG_DEF0) {
143 setupRegMask(&lir->defMask, lir->operands[0]);
144 }
145
146 if (flags & REG_DEF1) {
147 setupRegMask(&lir->defMask, lir->operands[1]);
148 }
149
150 if (flags & REG_DEF_SP) {
151 lir->defMask |= ENCODE_REG_SP;
152 }
153
154 if (flags & REG_DEF_LR) {
155 lir->defMask |= ENCODE_REG_LR;
156 }
157
158 if (flags & REG_DEF_LIST0) {
159 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
160 }
161
162 if (flags & REG_DEF_LIST1) {
163 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
164 }
165
166 if (flags & REG_DEF_FPCS_LIST0) {
167 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
168 }
169
buzbeef48e9712011-09-15 17:54:28 -0700170 if (flags & REG_DEF_FPCS_LIST2) {
171 for (int i = 0; i < lir->operands[2]; i++) {
172 setupRegMask(&lir->defMask, lir->operands[1] + i);
173 }
174 }
175
buzbee67bf8852011-08-17 17:51:35 -0700176 if (flags & SETS_CCODES) {
177 lir->defMask |= ENCODE_CCODE;
178 }
179
180 /* Conservatively treat the IT block */
181 if (flags & IS_IT) {
182 lir->defMask = ENCODE_ALL;
183 }
184
185 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
186 int i;
187
188 for (i = 0; i < 4; i++) {
189 if (flags & (1 << (kRegUse0 + i))) {
190 setupRegMask(&lir->useMask, lir->operands[i]);
191 }
192 }
193 }
194
195 if (flags & REG_USE_PC) {
196 lir->useMask |= ENCODE_REG_PC;
197 }
198
199 if (flags & REG_USE_SP) {
200 lir->useMask |= ENCODE_REG_SP;
201 }
202
203 if (flags & REG_USE_LIST0) {
204 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
205 }
206
207 if (flags & REG_USE_LIST1) {
208 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
209 }
210
211 if (flags & REG_USE_FPCS_LIST0) {
212 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
213 }
214
215 if (flags & REG_USE_FPCS_LIST2) {
buzbeef48e9712011-09-15 17:54:28 -0700216 for (int i = 0; i < lir->operands[2]; i++) {
217 setupRegMask(&lir->useMask, lir->operands[1] + i);
218 }
buzbee67bf8852011-08-17 17:51:35 -0700219 }
220
221 if (flags & USES_CCODES) {
222 lir->useMask |= ENCODE_CCODE;
223 }
224
225 /* Fixup for kThumbPush/lr and kThumbPop/pc */
226 if (opcode == kThumbPush || opcode == kThumbPop) {
227 u8 r8Mask = getRegMaskCommon(r8);
228 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
229 lir->useMask &= ~r8Mask;
230 lir->useMask |= ENCODE_REG_LR;
231 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
232 lir->defMask &= ~r8Mask;
233 lir->defMask |= ENCODE_REG_PC;
234 }
235 }
236}
237
238/*
buzbee67bf8852011-08-17 17:51:35 -0700239 * The following are building blocks to construct low-level IRs with 0 - 4
240 * operands.
241 */
buzbeeed3e9302011-09-23 17:34:19 -0700242STATIC ArmLIR* newLIR0(CompilationUnit* cUnit, ArmOpcode opcode)
buzbee67bf8852011-08-17 17:51:35 -0700243{
244 ArmLIR* insn = (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
buzbeeed3e9302011-09-23 17:34:19 -0700245 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND));
buzbee67bf8852011-08-17 17:51:35 -0700246 insn->opcode = opcode;
247 setupResourceMasks(insn);
248 insn->generic.dalvikOffset = cUnit->currentDalvikOffset;
249 oatAppendLIR(cUnit, (LIR*) insn);
250 return insn;
251}
252
buzbeeed3e9302011-09-23 17:34:19 -0700253STATIC ArmLIR* newLIR1(CompilationUnit* cUnit, ArmOpcode opcode,
buzbee67bf8852011-08-17 17:51:35 -0700254 int dest)
255{
256 ArmLIR* insn = (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
buzbeeed3e9302011-09-23 17:34:19 -0700257 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP));
buzbee67bf8852011-08-17 17:51:35 -0700258 insn->opcode = opcode;
259 insn->operands[0] = dest;
260 setupResourceMasks(insn);
261 insn->generic.dalvikOffset = cUnit->currentDalvikOffset;
262 oatAppendLIR(cUnit, (LIR*) insn);
263 return insn;
264}
265
buzbeeed3e9302011-09-23 17:34:19 -0700266STATIC ArmLIR* newLIR2(CompilationUnit* cUnit, ArmOpcode opcode,
buzbee67bf8852011-08-17 17:51:35 -0700267 int dest, int src1)
268{
269 ArmLIR* insn = (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
buzbeeed3e9302011-09-23 17:34:19 -0700270 DCHECK(isPseudoOpcode(opcode) ||
buzbee67bf8852011-08-17 17:51:35 -0700271 (EncodingMap[opcode].flags & IS_BINARY_OP));
272 insn->opcode = opcode;
273 insn->operands[0] = dest;
274 insn->operands[1] = src1;
275 setupResourceMasks(insn);
276 insn->generic.dalvikOffset = cUnit->currentDalvikOffset;
277 oatAppendLIR(cUnit, (LIR*) insn);
278 return insn;
279}
280
buzbeeed3e9302011-09-23 17:34:19 -0700281STATIC ArmLIR* newLIR3(CompilationUnit* cUnit, ArmOpcode opcode,
buzbee67bf8852011-08-17 17:51:35 -0700282 int dest, int src1, int src2)
283{
284 ArmLIR* insn = (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700285 DCHECK(isPseudoOpcode(opcode) ||
286 (EncodingMap[opcode].flags & IS_TERTIARY_OP))
287 << (int)opcode << " "
Ian Rogersa3760aa2011-11-14 14:32:37 -0800288 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700289 << cUnit->currentDalvikOffset;
buzbee67bf8852011-08-17 17:51:35 -0700290 insn->opcode = opcode;
291 insn->operands[0] = dest;
292 insn->operands[1] = src1;
293 insn->operands[2] = src2;
294 setupResourceMasks(insn);
295 insn->generic.dalvikOffset = cUnit->currentDalvikOffset;
296 oatAppendLIR(cUnit, (LIR*) insn);
297 return insn;
298}
299
300#if defined(_ARMV7_A) || defined(_ARMV7_A_NEON)
buzbeeed3e9302011-09-23 17:34:19 -0700301STATIC ArmLIR* newLIR4(CompilationUnit* cUnit, ArmOpcode opcode,
buzbee67bf8852011-08-17 17:51:35 -0700302 int dest, int src1, int src2, int info)
303{
304 ArmLIR* insn = (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
buzbeeed3e9302011-09-23 17:34:19 -0700305 DCHECK(isPseudoOpcode(opcode) ||
buzbee67bf8852011-08-17 17:51:35 -0700306 (EncodingMap[opcode].flags & IS_QUAD_OP));
307 insn->opcode = opcode;
308 insn->operands[0] = dest;
309 insn->operands[1] = src1;
310 insn->operands[2] = src2;
311 insn->operands[3] = info;
312 setupResourceMasks(insn);
313 insn->generic.dalvikOffset = cUnit->currentDalvikOffset;
314 oatAppendLIR(cUnit, (LIR*) insn);
315 return insn;
316}
317#endif
318
319/*
320 * Search the existing constants in the literal pool for an exact or close match
321 * within specified delta (greater or equal to 0).
322 */
buzbeeed3e9302011-09-23 17:34:19 -0700323STATIC ArmLIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
buzbee67bf8852011-08-17 17:51:35 -0700324{
325 while (dataTarget) {
326 if (((unsigned) (value - ((ArmLIR* ) dataTarget)->operands[0])) <=
327 delta)
328 return (ArmLIR* ) dataTarget;
329 dataTarget = dataTarget->next;
330 }
331 return NULL;
332}
333
buzbee03fa2632011-09-20 17:10:57 -0700334/* Search the existing constants in the literal pool for an exact wide match */
buzbeeed3e9302011-09-23 17:34:19 -0700335STATIC ArmLIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
buzbee03fa2632011-09-20 17:10:57 -0700336{
337 bool loMatch = false;
338 LIR* loTarget = NULL;
339 while (dataTarget) {
340 if (loMatch && (((ArmLIR*)dataTarget)->operands[0] == valHi)) {
341 return (ArmLIR*)loTarget;
342 }
343 loMatch = false;
344 if (((ArmLIR*)dataTarget)->operands[0] == valLo) {
345 loMatch = true;
346 loTarget = dataTarget;
347 }
348 dataTarget = dataTarget->next;
349 }
350 return NULL;
351}
352
buzbee67bf8852011-08-17 17:51:35 -0700353/*
354 * The following are building blocks to insert constants into the pool or
355 * instruction streams.
356 */
357
358/* Add a 32-bit constant either in the constant pool or mixed with code */
buzbeeed3e9302011-09-23 17:34:19 -0700359STATIC ArmLIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP,
buzbee67bf8852011-08-17 17:51:35 -0700360 int value)
361{
362 /* Add the constant to the literal pool */
363 if (constantListP) {
364 ArmLIR* newValue = (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
365 newValue->operands[0] = value;
366 newValue->generic.next = *constantListP;
367 *constantListP = (LIR*) newValue;
368 return newValue;
369 } else {
370 /* Add the constant in the middle of code stream */
371 newLIR1(cUnit, kArm16BitData, (value & 0xffff));
372 newLIR1(cUnit, kArm16BitData, (value >> 16));
373 }
374 return NULL;
375}
376
buzbee03fa2632011-09-20 17:10:57 -0700377/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeeed3e9302011-09-23 17:34:19 -0700378STATIC ArmLIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
buzbee03fa2632011-09-20 17:10:57 -0700379 int valLo, int valHi)
380{
381 ArmLIR* res;
382 //NOTE: hard-coded little endian
383 if (constantListP == NULL) {
384 res = addWordData(cUnit, NULL, valLo);
385 addWordData(cUnit, NULL, valHi);
386 } else {
387 // Insert high word into list first
388 addWordData(cUnit, constantListP, valHi);
389 res = addWordData(cUnit, constantListP, valLo);
390 }
391 return res;
392}
393
buzbee67bf8852011-08-17 17:51:35 -0700394/*
395 * Generate an kArmPseudoBarrier marker to indicate the boundary of special
396 * blocks.
397 */
buzbeeed3e9302011-09-23 17:34:19 -0700398STATIC void genBarrier(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700399{
400 ArmLIR* barrier = newLIR0(cUnit, kArmPseudoBarrier);
401 /* Mark all resources as being clobbered */
402 barrier->defMask = -1;
403}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800404
405} // namespace art