blob: 8e72227195cc917d499ef130e8ceeb610dc00db7 [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 for the Thumb2 ISA and is intended to be
19 * includes by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 */
24
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
26
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080027namespace art {
28
buzbee16da88c2012-03-20 10:38:17 -070029/* Return a RegLocation that describes an in-register argument */
30RegLocation argLoc(CompilationUnit* cUnit, RegLocation loc, int sReg)
31{
32 loc.location = kLocPhysReg;
33 int base = SRegToVReg(cUnit, sReg) - cUnit->numRegs;
34 loc.sRegLow = sReg;
35 loc.lowReg = rARG1 + base;
36 loc.home = true;
37 if (loc.wide) {
38 loc.highReg = loc.lowReg + 1;
39 oatLockTemp(cUnit, loc.lowReg);
40 oatLockTemp(cUnit, loc.highReg);
41 } else {
42 oatLockTemp(cUnit, loc.lowReg);
43 }
44 return loc;
45}
46
47/* Find the next MIR, which may be in a following basic block */
48MIR* getNextMir(CompilationUnit* cUnit, BasicBlock** pBb, MIR* mir)
49{
50 BasicBlock* bb = *pBb;
51 MIR* origMir = mir;
52 while (bb != NULL) {
53 if (mir != NULL) {
54 mir = mir->next;
55 }
56 if (mir != NULL) {
57 return mir;
58 } else {
59 bb = bb->fallThrough;
60 *pBb = bb;
61 if (bb) {
62 mir = bb->firstMIRInsn;
63 if (mir != NULL) {
64 return mir;
65 }
66 }
67 }
68 }
69 return origMir;
70}
71
72/* Used for the "printMe" listing */
73void genPrintLabel(CompilationUnit *cUnit, MIR* mir)
74{
75 LIR* boundaryLIR;
76 /* Mark the beginning of a Dalvik instruction for line tracking */
77 char* instStr = cUnit->printMe ?
78 oatGetDalvikDisassembly(cUnit, mir->dalvikInsn, "") : NULL;
79 boundaryLIR = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary,
80 (intptr_t) instStr);
81 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
82 (LIR*)boundaryLIR));
83 /* Don't generate the SSA annotation unless verbose mode is on */
84 if (cUnit->printMe && mir->ssaRep) {
85 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
86 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
87 }
88}
89
90MIR* specialIGet(CompilationUnit* cUnit, BasicBlock** bb, MIR* mir,
91 OpSize size, bool longOrDouble, bool isObject)
92{
93 int fieldOffset;
94 bool isVolatile;
95 uint32_t fieldIdx = mir->dalvikInsn.vC;
96 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
97 false);
98 if (!fastPath) {
99 return NULL;
100 }
101 mir->optimizationFlags |= MIR_IGNORE_NULL_CHECK;
102 genPrintLabel(cUnit, mir);
103 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
104 rlObj = argLoc(cUnit, rlObj, mir->ssaRep->uses[0]);
105 RegLocation rlDest;
106 if (longOrDouble) {
107 rlDest = oatGetReturnWide(cUnit, false);
108 } else {
109 rlDest = oatGetReturn(cUnit, false);
110 }
111 genIGet(cUnit, mir, size, rlDest, rlObj, longOrDouble, isObject);
112 return getNextMir(cUnit, bb, mir);
113}
114
115MIR* specialIPut(CompilationUnit* cUnit, BasicBlock** bb, MIR* mir,
116 OpSize size, bool longOrDouble, bool isObject)
117{
118 int fieldOffset;
119 bool isVolatile;
120 uint32_t fieldIdx = mir->dalvikInsn.vC;
121 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
122 false);
123 if (!fastPath) {
124 return NULL;
125 }
126 mir->optimizationFlags |= MIR_IGNORE_NULL_CHECK;
127 genPrintLabel(cUnit, mir);
128 RegLocation rlSrc;
129 RegLocation rlObj;
130 int sSreg = mir->ssaRep->uses[0];
131 int oSreg;
132 if (longOrDouble) {
133 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
134 rlObj = oatGetSrc(cUnit, mir, 2);
135 oSreg = mir->ssaRep->uses[2];
136 } else {
137 rlSrc = oatGetSrc(cUnit, mir, 0);
138 rlObj = oatGetSrc(cUnit, mir, 1);
139 oSreg = mir->ssaRep->uses[1];
140 }
141 rlSrc = argLoc(cUnit, rlSrc, sSreg);
142 rlObj = argLoc(cUnit, rlObj, oSreg);
143 genIPut(cUnit, mir, size, rlSrc, rlObj, longOrDouble, isObject);
144 return getNextMir(cUnit, bb, mir);
145}
146
147/*
148 * Special-case code genration for simple non-throwing leaf methods.
149 */
150void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
151 SpecialCaseHandler specialCase)
152{
153 cUnit->currentDalvikOffset = mir->offset;
154 MIR* nextMir = NULL;
155 switch(specialCase) {
156 case kNullMethod:
157 DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID);
158 nextMir = mir;
159 break;
160 case kConstFunction:
161 genPrintLabel(cUnit, mir);
162 loadConstant(cUnit, rRET0, mir->dalvikInsn.vB);
163 nextMir = getNextMir(cUnit, &bb, mir);
164 break;
165 case kIGet:
166 nextMir = specialIGet(cUnit, &bb, mir, kWord, false, false);
167 break;;
168 case kIGetBoolean:
169 case kIGetByte:
170 nextMir = specialIGet(cUnit, &bb, mir, kUnsignedByte, false, false);
171 break;;
172 case kIGetObject:
173 nextMir = specialIGet(cUnit, &bb, mir, kWord, false, true);
174 break;;
175 case kIGetChar:
176 nextMir = specialIGet(cUnit, &bb, mir, kUnsignedHalf, false, false);
177 break;;
178 case kIGetShort:
179 nextMir = specialIGet(cUnit, &bb, mir, kSignedHalf, false, false);
180 break;;
181 case kIGetWide:
182 nextMir = specialIGet(cUnit, &bb, mir, kLong, true, false);
183 break;;
184 case kIPut:
185 nextMir = specialIPut(cUnit, &bb, mir, kWord, false, false);
186 break;;
187 case kIPutBoolean:
188 case kIPutByte:
189 nextMir = specialIPut(cUnit, &bb, mir, kUnsignedByte, false, false);
190 break;;
191 case kIPutObject:
192 nextMir = specialIPut(cUnit, &bb, mir, kWord, false, true);
193 break;;
194 case kIPutChar:
195 nextMir = specialIPut(cUnit, &bb, mir, kUnsignedHalf, false, false);
196 break;;
197 case kIPutShort:
198 nextMir = specialIPut(cUnit, &bb, mir, kSignedHalf, false, false);
199 break;;
200 case kIPutWide:
201 nextMir = specialIPut(cUnit, &bb, mir, kLong, true, false);
202 break;;
203 default:
204 return;
205 }
206 if (nextMir != NULL) {
207 cUnit->currentDalvikOffset = nextMir->offset;
208 genPrintLabel(cUnit, nextMir);
209 newLIR1(cUnit, kThumbBx, rLR);
210 cUnit->coreSpillMask = 0;
211 cUnit->numCoreSpills = 0;
212 cUnit->fpSpillMask = 0;
213 cUnit->numFPSpills = 0;
214 cUnit->frameSize = 0;
215 cUnit->coreVmapTable.clear();
216 cUnit->fpVmapTable.clear();
217 }
218}
buzbee67bf8852011-08-17 17:51:35 -0700219
220/*
221 * Generate a Thumb2 IT instruction, which can nullify up to
222 * four subsequent instructions based on a condition and its
223 * inverse. The condition applies to the first instruction, which
224 * is executed if the condition is met. The string "guide" consists
225 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
226 * A "T" means the instruction is executed if the condition is
227 * met, and an "E" means the instruction is executed if the condition
228 * is not met.
229 */
buzbee82488f52012-03-02 08:20:26 -0800230LIR* opIT(CompilationUnit* cUnit, ArmConditionCode code, const char* guide)
buzbee67bf8852011-08-17 17:51:35 -0700231{
232 int mask;
233 int condBit = code & 1;
234 int altBit = condBit ^ 1;
235 int mask3 = 0;
236 int mask2 = 0;
237 int mask1 = 0;
238
239 //Note: case fallthroughs intentional
240 switch(strlen(guide)) {
241 case 3:
242 mask1 = (guide[2] == 'T') ? condBit : altBit;
243 case 2:
244 mask2 = (guide[1] == 'T') ? condBit : altBit;
245 case 1:
246 mask3 = (guide[0] == 'T') ? condBit : altBit;
247 break;
248 case 0:
249 break;
250 default:
buzbee82488f52012-03-02 08:20:26 -0800251 LOG(FATAL) << "OAT: bad case in opIT";
buzbee67bf8852011-08-17 17:51:35 -0700252 }
253 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
254 (1 << (3 - strlen(guide)));
255 return newLIR2(cUnit, kThumb2It, code, mask);
256}
257
258/*
buzbee67bf8852011-08-17 17:51:35 -0700259 * The sparse table in the literal pool is an array of <key,displacement>
260 * pairs. For each set, we'll load them as a pair using ldmia.
261 * This means that the register number of the temp we use for the key
262 * must be lower than the reg for the displacement.
263 *
264 * The test loop will look something like:
265 *
266 * adr rBase, <table>
267 * ldr rVal, [rSP, vRegOff]
268 * mov rIdx, #tableSize
269 * lp:
270 * ldmia rBase!, {rKey, rDisp}
271 * sub rIdx, #1
272 * cmp rVal, rKey
273 * ifeq
274 * add rPC, rDisp ; This is the branch from which we compute displacement
275 * cbnz rIdx, lp
276 */
buzbee31a4a6f2012-02-28 15:36:15 -0800277void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700278{
279 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
280 if (cUnit->printMe) {
281 dumpSparseSwitchTable(table);
282 }
283 // Add the table to the list - we'll process it later
buzbeeba938cb2012-02-03 14:47:55 -0800284 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
buzbee5abfa3e2012-01-31 17:01:43 -0800285 true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700286 tabRec->table = table;
287 tabRec->vaddr = mir->offset;
288 int size = table[1];
buzbee31a4a6f2012-02-28 15:36:15 -0800289 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
290 kAllocLIR);
buzbeeba938cb2012-02-03 14:47:55 -0800291 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700292
293 // Get the switch value
294 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
295 int rBase = oatAllocTemp(cUnit);
296 /* Allocate key and disp temps */
297 int rKey = oatAllocTemp(cUnit);
298 int rDisp = oatAllocTemp(cUnit);
299 // Make sure rKey's register number is less than rDisp's number for ldmia
300 if (rKey > rDisp) {
301 int tmp = rDisp;
302 rDisp = rKey;
303 rKey = tmp;
304 }
305 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700306 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700307 // Set up rIdx
308 int rIdx = oatAllocTemp(cUnit);
309 loadConstant(cUnit, rIdx, size);
310 // Establish loop branch target
buzbee31a4a6f2012-02-28 15:36:15 -0800311 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee67bf8852011-08-17 17:51:35 -0700312 // Load next key/disp
313 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
314 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
315 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
buzbee82488f52012-03-02 08:20:26 -0800316 opIT(cUnit, kArmCondEq, "");
buzbee31a4a6f2012-02-28 15:36:15 -0800317 LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
buzbeec5159d52012-03-03 11:48:39 -0800318 tabRec->anchor = switchBranch;
buzbee67bf8852011-08-17 17:51:35 -0700319 // Needs to use setflags encoding here
320 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800321 opCondBranch(cUnit, kCondNe, target);
buzbee67bf8852011-08-17 17:51:35 -0700322}
323
324
buzbee31a4a6f2012-02-28 15:36:15 -0800325void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700326{
327 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
328 if (cUnit->printMe) {
329 dumpPackedSwitchTable(table);
330 }
331 // Add the table to the list - we'll process it later
buzbeeba938cb2012-02-03 14:47:55 -0800332 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
buzbee5abfa3e2012-01-31 17:01:43 -0800333 true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700334 tabRec->table = table;
335 tabRec->vaddr = mir->offset;
336 int size = table[1];
buzbee31a4a6f2012-02-28 15:36:15 -0800337 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
buzbee5abfa3e2012-01-31 17:01:43 -0800338 kAllocLIR);
buzbeeba938cb2012-02-03 14:47:55 -0800339 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700340
341 // Get the switch value
342 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
343 int tableBase = oatAllocTemp(cUnit);
344 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700345 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700346 int lowKey = s4FromSwitchData(&table[2]);
347 int keyReg;
348 // Remove the bias, if necessary
349 if (lowKey == 0) {
350 keyReg = rlSrc.lowReg;
351 } else {
352 keyReg = oatAllocTemp(cUnit);
353 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
354 }
355 // Bounds check - if < 0 or >= size continue following switch
356 opRegImm(cUnit, kOpCmp, keyReg, size-1);
buzbee82488f52012-03-02 08:20:26 -0800357 LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700358
359 // Load the displacement from the switch table
360 int dispReg = oatAllocTemp(cUnit);
361 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
362
363 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee31a4a6f2012-02-28 15:36:15 -0800364 LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
buzbeec5159d52012-03-03 11:48:39 -0800365 tabRec->anchor = switchBranch;
buzbee67bf8852011-08-17 17:51:35 -0700366
367 /* branchOver target here */
buzbee31a4a6f2012-02-28 15:36:15 -0800368 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800369 branchOver->target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700370}
371
372/*
373 * Array data table format:
374 * ushort ident = 0x0300 magic value
375 * ushort width width of each element in the table
376 * uint size number of elements in the table
377 * ubyte data[size*width] table of data values (may contain a single-byte
378 * padding at the end)
379 *
380 * Total size is 4+(width * size + 1)/2 16-bit code units.
381 */
buzbee31a4a6f2012-02-28 15:36:15 -0800382void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700383{
384 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
385 // Add the table to the list - we'll process it later
386 FillArrayData *tabRec = (FillArrayData *)
buzbeeba938cb2012-02-03 14:47:55 -0800387 oatNew(cUnit, sizeof(FillArrayData), true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700388 tabRec->table = table;
389 tabRec->vaddr = mir->offset;
390 u2 width = tabRec->table[1];
391 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
392 tabRec->size = (size * width) + 8;
393
buzbeeba938cb2012-02-03 14:47:55 -0800394 oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700395
396 // Making a call - use explicit registers
397 oatFlushAllRegs(cUnit); /* Everything to home location */
398 loadValueDirectFixed(cUnit, rlSrc, r0);
399 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700400 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700401 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700402 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700403 oatClobberCalleeSave(cUnit);
404 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700405}
406
buzbee31a4a6f2012-02-28 15:36:15 -0800407void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700408{
409 RegLocation rlResult;
410 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
411 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
412 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
413 storeValue(cUnit, rlDest, rlResult);
414}
415
buzbee31a4a6f2012-02-28 15:36:15 -0800416void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700417{
418 RegLocation rlResult;
419 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
420 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
421 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
422 S2D(rlSrc.lowReg, rlSrc.highReg));
423 storeValueWide(cUnit, rlDest, rlResult);
424}
425
buzbee67bf8852011-08-17 17:51:35 -0700426/*
427 * Handle simple case (thin lock) inline. If it's complicated, bail
428 * out to the heavyweight lock/unlock routines. We'll use dedicated
429 * registers here in order to be in the right position in case we
430 * to bail to dvm[Lock/Unlock]Object(self, object)
431 *
432 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
433 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
434 * r2 -> intial contents of object->lock, later result of strex
435 * r3 -> self->threadId
436 * r12 -> allow to be used by utilities as general temp
437 *
438 * The result of the strex is 0 if we acquire the lock.
439 *
440 * See comments in Sync.c for the layout of the lock word.
441 * Of particular interest to this code is the test for the
442 * simple case - which we handle inline. For monitor enter, the
443 * simple case is thin lock, held by no-one. For monitor exit,
444 * the simple case is thin lock, held by the unlocking thread with
445 * a recurse count of 0.
446 *
447 * A minor complication is that there is a field in the lock word
448 * unrelated to locking: the hash state. This field must be ignored, but
449 * preserved.
450 *
451 */
buzbee31a4a6f2012-02-28 15:36:15 -0800452void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700453{
buzbee67bf8852011-08-17 17:51:35 -0700454 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700455 DCHECK_EQ(LW_SHAPE_THIN, 0);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700456 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700457 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700458 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
459 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
460 newLIR3(cUnit, kThumb2Ldrex, r1, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700461 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700462 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700463 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700464 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700465 newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1);
466 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee05eba362012-03-10 20:11:27 -0800467 opRegImm(cUnit, kOpCmp, r1, 0);
468 opIT(cUnit, kArmCondEq, "");
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700469 newLIR4(cUnit, kThumb2Strex, r1, r2, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700470 Object::MonitorOffset().Int32Value() >> 2);
buzbee05eba362012-03-10 20:11:27 -0800471 opRegImm(cUnit, kOpCmp, r1, 0);
472 opIT(cUnit, kArmCondNe, "T");
buzbee1b4c8592011-08-31 10:43:51 -0700473 // Go expensive route - artLockObjectFromCode(self, obj);
474 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700475 rLR);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700476 oatClobberCalleeSave(cUnit);
477 opReg(cUnit, kOpBlx, rLR);
buzbee05eba362012-03-10 20:11:27 -0800478 oatGenMemBarrier(cUnit, kSY);
buzbee67bf8852011-08-17 17:51:35 -0700479}
480
481/*
482 * For monitor unlock, we don't have to use ldrex/strex. Once
483 * we've determined that the lock is thin and that we own it with
484 * a zero recursion count, it's safe to punch it back to the
485 * initial, unlock thin state with a store word.
486 */
buzbee31a4a6f2012-02-28 15:36:15 -0800487void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700488{
Elliott Hughes5f791332011-09-15 17:45:30 -0700489 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700490 oatFlushAllRegs(cUnit);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700491 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700492 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700493 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
494 loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock
495 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
buzbee67bf8852011-08-17 17:51:35 -0700496 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700497 opRegRegImm(cUnit, kOpAnd, r3, r1, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -0700498 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700499 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
500 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
501 opRegReg(cUnit, kOpSub, r1, r2);
buzbee05eba362012-03-10 20:11:27 -0800502 opIT(cUnit, kArmCondEq, "EE");
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700503 storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700504 // Go expensive route - UnlockObjectFromCode(obj);
buzbee1b4c8592011-08-31 10:43:51 -0700505 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700506 rLR);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700507 oatClobberCalleeSave(cUnit);
508 opReg(cUnit, kOpBlx, rLR);
buzbee05eba362012-03-10 20:11:27 -0800509 oatGenMemBarrier(cUnit, kSY);
buzbee67bf8852011-08-17 17:51:35 -0700510}
511
512/*
513 * 64-bit 3way compare function.
514 * mov rX, #-1
515 * cmp op1hi, op2hi
516 * blt done
517 * bgt flip
518 * sub rX, op1lo, op2lo (treat as unsigned)
519 * beq done
520 * ite hi
521 * mov(hi) rX, #-1
522 * mov(!hi) rX, #1
523 * flip:
524 * neg rX
525 * done:
526 */
buzbee31a4a6f2012-02-28 15:36:15 -0800527void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
528 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee67bf8852011-08-17 17:51:35 -0700529{
buzbee31a4a6f2012-02-28 15:36:15 -0800530 LIR* target1;
531 LIR* target2;
buzbee67bf8852011-08-17 17:51:35 -0700532 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
533 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -0700534 int tReg = oatAllocTemp(cUnit);
535 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -0700536 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
buzbee82488f52012-03-02 08:20:26 -0800537 LIR* branch1 = opCondBranch(cUnit, kCondLt, NULL);
538 LIR* branch2 = opCondBranch(cUnit, kCondGt, NULL);
buzbeeb29e4d12011-09-26 15:05:48 -0700539 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800540 LIR* branch3 = opCondBranch(cUnit, kCondEq, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700541
buzbee82488f52012-03-02 08:20:26 -0800542 opIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -0700543 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
544 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -0700545 genBarrier(cUnit);
546
buzbee31a4a6f2012-02-28 15:36:15 -0800547 target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbeeb29e4d12011-09-26 15:05:48 -0700548 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -0700549
buzbee31a4a6f2012-02-28 15:36:15 -0800550 target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee67bf8852011-08-17 17:51:35 -0700551
buzbeeb29e4d12011-09-26 15:05:48 -0700552 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
553 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -0700554 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -0700555 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -0700556
buzbee31a4a6f2012-02-28 15:36:15 -0800557 branch1->target = (LIR*)target1;
558 branch2->target = (LIR*)target2;
559 branch3->target = branch1->target;
buzbee67bf8852011-08-17 17:51:35 -0700560}
561
buzbee67bf8852011-08-17 17:51:35 -0700562/*
buzbee31a4a6f2012-02-28 15:36:15 -0800563 * Generate a register comparison to an immediate and branch. Caller
564 * is responsible for setting branch target field.
buzbee67bf8852011-08-17 17:51:35 -0700565 */
buzbee82488f52012-03-02 08:20:26 -0800566LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg,
567 int checkValue, LIR* target)
buzbee67bf8852011-08-17 17:51:35 -0700568{
buzbee31a4a6f2012-02-28 15:36:15 -0800569 LIR* branch;
570 int modImm;
571 ArmConditionCode armCond = oatArmConditionEncoding(cond);
572 if ((LOWREG(reg)) && (checkValue == 0) &&
573 ((armCond == kArmCondEq) || (armCond == kArmCondNe))) {
574 branch = newLIR2(cUnit,
575 (armCond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
576 reg, 0);
buzbee67bf8852011-08-17 17:51:35 -0700577 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800578 modImm = modifiedImmediate(checkValue);
579 if (LOWREG(reg) && ((checkValue & 0xff) == checkValue)) {
580 newLIR2(cUnit, kThumbCmpRI8, reg, checkValue);
581 } else if (modImm >= 0) {
582 newLIR2(cUnit, kThumb2CmpRI8, reg, modImm);
buzbee67bf8852011-08-17 17:51:35 -0700583 } else {
buzbee58f92742011-10-01 11:22:17 -0700584 int tReg = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800585 loadConstant(cUnit, tReg, checkValue);
586 opRegReg(cUnit, kOpCmp, reg, tReg);
buzbee58f92742011-10-01 11:22:17 -0700587 }
buzbee31a4a6f2012-02-28 15:36:15 -0800588 branch = newLIR2(cUnit, kThumbBCond, 0, armCond);
buzbee67bf8852011-08-17 17:51:35 -0700589 }
buzbee82488f52012-03-02 08:20:26 -0800590 branch->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800591 return branch;
592}
buzbee82488f52012-03-02 08:20:26 -0800593LIR* opRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800594{
595 LIR* res;
596 ArmOpcode opcode;
597 if (FPREG(rDest) || FPREG(rSrc))
598 return fpRegCopy(cUnit, rDest, rSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800599 if (LOWREG(rDest) && LOWREG(rSrc))
600 opcode = kThumbMovRR;
601 else if (!LOWREG(rDest) && !LOWREG(rSrc))
602 opcode = kThumbMovRR_H2H;
603 else if (LOWREG(rDest))
604 opcode = kThumbMovRR_H2L;
605 else
606 opcode = kThumbMovRR_L2H;
buzbeea2ebdd72012-03-04 14:57:06 -0800607 res = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, rDest, rSrc);
buzbee86a4bce2012-03-06 18:15:00 -0800608 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && rDest == rSrc) {
buzbee31a4a6f2012-02-28 15:36:15 -0800609 res->flags.isNop = true;
610 }
611 return res;
buzbee67bf8852011-08-17 17:51:35 -0700612}
613
buzbee82488f52012-03-02 08:20:26 -0800614LIR* opRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
buzbee67bf8852011-08-17 17:51:35 -0700615{
buzbee82488f52012-03-02 08:20:26 -0800616 LIR* res = opRegCopyNoInsert(cUnit, rDest, rSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800617 oatAppendLIR(cUnit, (LIR*)res);
618 return res;
619}
buzbee67bf8852011-08-17 17:51:35 -0700620
buzbee82488f52012-03-02 08:20:26 -0800621void opRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
buzbee31a4a6f2012-02-28 15:36:15 -0800622 int srcLo, int srcHi)
623{
624 bool destFP = FPREG(destLo) && FPREG(destHi);
625 bool srcFP = FPREG(srcLo) && FPREG(srcHi);
626 DCHECK_EQ(FPREG(srcLo), FPREG(srcHi));
627 DCHECK_EQ(FPREG(destLo), FPREG(destHi));
628 if (destFP) {
629 if (srcFP) {
buzbee82488f52012-03-02 08:20:26 -0800630 opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi));
buzbee67bf8852011-08-17 17:51:35 -0700631 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800632 newLIR3(cUnit, kThumb2Fmdrr, S2D(destLo, destHi), srcLo, srcHi);
633 }
634 } else {
635 if (srcFP) {
636 newLIR3(cUnit, kThumb2Fmrrd, destLo, destHi, S2D(srcLo, srcHi));
637 } else {
638 // Handle overlap
639 if (srcHi == destLo) {
buzbee82488f52012-03-02 08:20:26 -0800640 opRegCopy(cUnit, destHi, srcHi);
641 opRegCopy(cUnit, destLo, srcLo);
buzbee67bf8852011-08-17 17:51:35 -0700642 } else {
buzbee82488f52012-03-02 08:20:26 -0800643 opRegCopy(cUnit, destLo, srcLo);
644 opRegCopy(cUnit, destHi, srcHi);
buzbee67bf8852011-08-17 17:51:35 -0700645 }
646 }
647 }
buzbee67bf8852011-08-17 17:51:35 -0700648}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800649
buzbee31a4a6f2012-02-28 15:36:15 -0800650
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800651} // namespace art