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