blob: 557e87b470fe0853f19a0d21b2b1f604dae193d2 [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
buzbee67bf8852011-08-17 17:51:35 -070029
30/*
31 * Generate a Thumb2 IT instruction, which can nullify up to
32 * four subsequent instructions based on a condition and its
33 * inverse. The condition applies to the first instruction, which
34 * is executed if the condition is met. The string "guide" consists
35 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
36 * A "T" means the instruction is executed if the condition is
37 * met, and an "E" means the instruction is executed if the condition
38 * is not met.
39 */
buzbee82488f52012-03-02 08:20:26 -080040LIR* opIT(CompilationUnit* cUnit, ArmConditionCode code, const char* guide)
buzbee67bf8852011-08-17 17:51:35 -070041{
42 int mask;
43 int condBit = code & 1;
44 int altBit = condBit ^ 1;
45 int mask3 = 0;
46 int mask2 = 0;
47 int mask1 = 0;
48
49 //Note: case fallthroughs intentional
50 switch(strlen(guide)) {
51 case 3:
52 mask1 = (guide[2] == 'T') ? condBit : altBit;
53 case 2:
54 mask2 = (guide[1] == 'T') ? condBit : altBit;
55 case 1:
56 mask3 = (guide[0] == 'T') ? condBit : altBit;
57 break;
58 case 0:
59 break;
60 default:
buzbee82488f52012-03-02 08:20:26 -080061 LOG(FATAL) << "OAT: bad case in opIT";
buzbee67bf8852011-08-17 17:51:35 -070062 }
63 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
64 (1 << (3 - strlen(guide)));
65 return newLIR2(cUnit, kThumb2It, code, mask);
66}
67
68/*
buzbee67bf8852011-08-17 17:51:35 -070069 * The sparse table in the literal pool is an array of <key,displacement>
70 * pairs. For each set, we'll load them as a pair using ldmia.
71 * This means that the register number of the temp we use for the key
72 * must be lower than the reg for the displacement.
73 *
74 * The test loop will look something like:
75 *
76 * adr rBase, <table>
77 * ldr rVal, [rSP, vRegOff]
78 * mov rIdx, #tableSize
79 * lp:
80 * ldmia rBase!, {rKey, rDisp}
81 * sub rIdx, #1
82 * cmp rVal, rKey
83 * ifeq
84 * add rPC, rDisp ; This is the branch from which we compute displacement
85 * cbnz rIdx, lp
86 */
buzbee31a4a6f2012-02-28 15:36:15 -080087void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -070088{
89 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
90 if (cUnit->printMe) {
91 dumpSparseSwitchTable(table);
92 }
93 // Add the table to the list - we'll process it later
buzbeeba938cb2012-02-03 14:47:55 -080094 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
buzbee5abfa3e2012-01-31 17:01:43 -080095 true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -070096 tabRec->table = table;
97 tabRec->vaddr = mir->offset;
98 int size = table[1];
buzbee31a4a6f2012-02-28 15:36:15 -080099 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
100 kAllocLIR);
buzbeeba938cb2012-02-03 14:47:55 -0800101 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700102
103 // Get the switch value
104 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
105 int rBase = oatAllocTemp(cUnit);
106 /* Allocate key and disp temps */
107 int rKey = oatAllocTemp(cUnit);
108 int rDisp = oatAllocTemp(cUnit);
109 // Make sure rKey's register number is less than rDisp's number for ldmia
110 if (rKey > rDisp) {
111 int tmp = rDisp;
112 rDisp = rKey;
113 rKey = tmp;
114 }
115 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700116 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700117 // Set up rIdx
118 int rIdx = oatAllocTemp(cUnit);
119 loadConstant(cUnit, rIdx, size);
120 // Establish loop branch target
buzbee31a4a6f2012-02-28 15:36:15 -0800121 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee67bf8852011-08-17 17:51:35 -0700122 // Load next key/disp
123 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
124 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
125 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
buzbee82488f52012-03-02 08:20:26 -0800126 opIT(cUnit, kArmCondEq, "");
buzbee31a4a6f2012-02-28 15:36:15 -0800127 LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
buzbeec5159d52012-03-03 11:48:39 -0800128 tabRec->anchor = switchBranch;
buzbee67bf8852011-08-17 17:51:35 -0700129 // Needs to use setflags encoding here
130 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800131 opCondBranch(cUnit, kCondNe, target);
buzbee67bf8852011-08-17 17:51:35 -0700132}
133
134
buzbee31a4a6f2012-02-28 15:36:15 -0800135void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700136{
137 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
138 if (cUnit->printMe) {
139 dumpPackedSwitchTable(table);
140 }
141 // Add the table to the list - we'll process it later
buzbeeba938cb2012-02-03 14:47:55 -0800142 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
buzbee5abfa3e2012-01-31 17:01:43 -0800143 true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700144 tabRec->table = table;
145 tabRec->vaddr = mir->offset;
146 int size = table[1];
buzbee31a4a6f2012-02-28 15:36:15 -0800147 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
buzbee5abfa3e2012-01-31 17:01:43 -0800148 kAllocLIR);
buzbeeba938cb2012-02-03 14:47:55 -0800149 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700150
151 // Get the switch value
152 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
153 int tableBase = oatAllocTemp(cUnit);
154 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700155 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700156 int lowKey = s4FromSwitchData(&table[2]);
157 int keyReg;
158 // Remove the bias, if necessary
159 if (lowKey == 0) {
160 keyReg = rlSrc.lowReg;
161 } else {
162 keyReg = oatAllocTemp(cUnit);
163 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
164 }
165 // Bounds check - if < 0 or >= size continue following switch
166 opRegImm(cUnit, kOpCmp, keyReg, size-1);
buzbee82488f52012-03-02 08:20:26 -0800167 LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700168
169 // Load the displacement from the switch table
170 int dispReg = oatAllocTemp(cUnit);
171 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
172
173 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee31a4a6f2012-02-28 15:36:15 -0800174 LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
buzbeec5159d52012-03-03 11:48:39 -0800175 tabRec->anchor = switchBranch;
buzbee67bf8852011-08-17 17:51:35 -0700176
177 /* branchOver target here */
buzbee31a4a6f2012-02-28 15:36:15 -0800178 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800179 branchOver->target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700180}
181
182/*
183 * Array data table format:
184 * ushort ident = 0x0300 magic value
185 * ushort width width of each element in the table
186 * uint size number of elements in the table
187 * ubyte data[size*width] table of data values (may contain a single-byte
188 * padding at the end)
189 *
190 * Total size is 4+(width * size + 1)/2 16-bit code units.
191 */
buzbee31a4a6f2012-02-28 15:36:15 -0800192void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700193{
194 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
195 // Add the table to the list - we'll process it later
196 FillArrayData *tabRec = (FillArrayData *)
buzbeeba938cb2012-02-03 14:47:55 -0800197 oatNew(cUnit, sizeof(FillArrayData), true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700198 tabRec->table = table;
199 tabRec->vaddr = mir->offset;
200 u2 width = tabRec->table[1];
201 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
202 tabRec->size = (size * width) + 8;
203
buzbeeba938cb2012-02-03 14:47:55 -0800204 oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700205
206 // Making a call - use explicit registers
207 oatFlushAllRegs(cUnit); /* Everything to home location */
208 loadValueDirectFixed(cUnit, rlSrc, r0);
209 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700210 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700211 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700212 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersff1ed472011-09-20 13:46:24 -0700213 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700214}
215
buzbee31a4a6f2012-02-28 15:36:15 -0800216void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700217{
218 RegLocation rlResult;
219 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
220 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
221 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
222 storeValue(cUnit, rlDest, rlResult);
223}
224
buzbee31a4a6f2012-02-28 15:36:15 -0800225void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700226{
227 RegLocation rlResult;
228 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
229 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
230 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
231 S2D(rlSrc.lowReg, rlSrc.highReg));
232 storeValueWide(cUnit, rlDest, rlResult);
233}
234
buzbee67bf8852011-08-17 17:51:35 -0700235/*
236 * Handle simple case (thin lock) inline. If it's complicated, bail
237 * out to the heavyweight lock/unlock routines. We'll use dedicated
238 * registers here in order to be in the right position in case we
239 * to bail to dvm[Lock/Unlock]Object(self, object)
240 *
241 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
242 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
243 * r2 -> intial contents of object->lock, later result of strex
244 * r3 -> self->threadId
245 * r12 -> allow to be used by utilities as general temp
246 *
247 * The result of the strex is 0 if we acquire the lock.
248 *
249 * See comments in Sync.c for the layout of the lock word.
250 * Of particular interest to this code is the test for the
251 * simple case - which we handle inline. For monitor enter, the
252 * simple case is thin lock, held by no-one. For monitor exit,
253 * the simple case is thin lock, held by the unlocking thread with
254 * a recurse count of 0.
255 *
256 * A minor complication is that there is a field in the lock word
257 * unrelated to locking: the hash state. This field must be ignored, but
258 * preserved.
259 *
260 */
buzbee31a4a6f2012-02-28 15:36:15 -0800261void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700262{
buzbee31a4a6f2012-02-28 15:36:15 -0800263 LIR* target;
264 LIR* hopTarget;
265 LIR* branch;
266 LIR* hopBranch;
buzbee67bf8852011-08-17 17:51:35 -0700267
268 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700269 DCHECK_EQ(LW_SHAPE_THIN, 0);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700270 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700271 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700272 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
273 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
274 newLIR3(cUnit, kThumb2Ldrex, r1, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700276 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700277 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700278 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700279 newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1);
280 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
281 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r1, 0);
282 newLIR4(cUnit, kThumb2Strex, r1, r2, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700284 oatGenMemBarrier(cUnit, kSY);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700285 branch = newLIR2(cUnit, kThumb2Cbz, r1, 0);
buzbee67bf8852011-08-17 17:51:35 -0700286
buzbee31a4a6f2012-02-28 15:36:15 -0800287 hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800288 hopBranch->target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700289
buzbee1b4c8592011-08-31 10:43:51 -0700290 // Go expensive route - artLockObjectFromCode(self, obj);
291 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700292 rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -0700293 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700294
295 // Resume here
buzbee31a4a6f2012-02-28 15:36:15 -0800296 target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800297 branch->target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700298}
299
300/*
301 * For monitor unlock, we don't have to use ldrex/strex. Once
302 * we've determined that the lock is thin and that we own it with
303 * a zero recursion count, it's safe to punch it back to the
304 * initial, unlock thin state with a store word.
305 */
buzbee31a4a6f2012-02-28 15:36:15 -0800306void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700307{
buzbee31a4a6f2012-02-28 15:36:15 -0800308 LIR* target;
309 LIR* branch;
310 LIR* hopTarget;
311 LIR* hopBranch;
buzbee67bf8852011-08-17 17:51:35 -0700312
Elliott Hughes5f791332011-09-15 17:45:30 -0700313 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700314 oatFlushAllRegs(cUnit);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700315 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700316 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700317 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
318 loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock
319 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
buzbee67bf8852011-08-17 17:51:35 -0700320 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700321 opRegRegImm(cUnit, kOpAnd, r3, r1, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -0700322 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700323 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
324 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
325 opRegReg(cUnit, kOpSub, r1, r2);
buzbee82488f52012-03-02 08:20:26 -0800326 hopBranch = opCondBranch(cUnit, kCondNe, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700327 oatGenMemBarrier(cUnit, kSY);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700328 storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700329 branch = opNone(cUnit, kOpUncondBr);
330
buzbee31a4a6f2012-02-28 15:36:15 -0800331 hopTarget = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800332 hopBranch->target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700333
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700334 // Go expensive route - UnlockObjectFromCode(obj);
buzbee1b4c8592011-08-31 10:43:51 -0700335 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700336 rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -0700337 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700338
339 // Resume here
buzbee31a4a6f2012-02-28 15:36:15 -0800340 target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800341 branch->target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700342}
343
344/*
345 * 64-bit 3way compare function.
346 * mov rX, #-1
347 * cmp op1hi, op2hi
348 * blt done
349 * bgt flip
350 * sub rX, op1lo, op2lo (treat as unsigned)
351 * beq done
352 * ite hi
353 * mov(hi) rX, #-1
354 * mov(!hi) rX, #1
355 * flip:
356 * neg rX
357 * done:
358 */
buzbee31a4a6f2012-02-28 15:36:15 -0800359void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
360 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee67bf8852011-08-17 17:51:35 -0700361{
buzbee31a4a6f2012-02-28 15:36:15 -0800362 LIR* target1;
363 LIR* target2;
buzbee67bf8852011-08-17 17:51:35 -0700364 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
365 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -0700366 int tReg = oatAllocTemp(cUnit);
367 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -0700368 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
buzbee82488f52012-03-02 08:20:26 -0800369 LIR* branch1 = opCondBranch(cUnit, kCondLt, NULL);
370 LIR* branch2 = opCondBranch(cUnit, kCondGt, NULL);
buzbeeb29e4d12011-09-26 15:05:48 -0700371 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800372 LIR* branch3 = opCondBranch(cUnit, kCondEq, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700373
buzbee82488f52012-03-02 08:20:26 -0800374 opIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -0700375 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
376 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -0700377 genBarrier(cUnit);
378
buzbee31a4a6f2012-02-28 15:36:15 -0800379 target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbeeb29e4d12011-09-26 15:05:48 -0700380 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -0700381
buzbee31a4a6f2012-02-28 15:36:15 -0800382 target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee67bf8852011-08-17 17:51:35 -0700383
buzbeeb29e4d12011-09-26 15:05:48 -0700384 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
385 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -0700386 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -0700387 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -0700388
buzbee31a4a6f2012-02-28 15:36:15 -0800389 branch1->target = (LIR*)target1;
390 branch2->target = (LIR*)target2;
391 branch3->target = branch1->target;
buzbee67bf8852011-08-17 17:51:35 -0700392}
393
buzbee67bf8852011-08-17 17:51:35 -0700394/*
buzbee31a4a6f2012-02-28 15:36:15 -0800395 * Generate a register comparison to an immediate and branch. Caller
396 * is responsible for setting branch target field.
buzbee67bf8852011-08-17 17:51:35 -0700397 */
buzbee82488f52012-03-02 08:20:26 -0800398LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg,
399 int checkValue, LIR* target)
buzbee67bf8852011-08-17 17:51:35 -0700400{
buzbee31a4a6f2012-02-28 15:36:15 -0800401 LIR* branch;
402 int modImm;
403 ArmConditionCode armCond = oatArmConditionEncoding(cond);
404 if ((LOWREG(reg)) && (checkValue == 0) &&
405 ((armCond == kArmCondEq) || (armCond == kArmCondNe))) {
406 branch = newLIR2(cUnit,
407 (armCond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
408 reg, 0);
buzbee67bf8852011-08-17 17:51:35 -0700409 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800410 modImm = modifiedImmediate(checkValue);
411 if (LOWREG(reg) && ((checkValue & 0xff) == checkValue)) {
412 newLIR2(cUnit, kThumbCmpRI8, reg, checkValue);
413 } else if (modImm >= 0) {
414 newLIR2(cUnit, kThumb2CmpRI8, reg, modImm);
buzbee67bf8852011-08-17 17:51:35 -0700415 } else {
buzbee58f92742011-10-01 11:22:17 -0700416 int tReg = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800417 loadConstant(cUnit, tReg, checkValue);
418 opRegReg(cUnit, kOpCmp, reg, tReg);
buzbee58f92742011-10-01 11:22:17 -0700419 }
buzbee31a4a6f2012-02-28 15:36:15 -0800420 branch = newLIR2(cUnit, kThumbBCond, 0, armCond);
buzbee67bf8852011-08-17 17:51:35 -0700421 }
buzbee82488f52012-03-02 08:20:26 -0800422 branch->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800423 return branch;
424}
buzbee82488f52012-03-02 08:20:26 -0800425LIR* opRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800426{
427 LIR* res;
428 ArmOpcode opcode;
429 if (FPREG(rDest) || FPREG(rSrc))
430 return fpRegCopy(cUnit, rDest, rSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800431 if (LOWREG(rDest) && LOWREG(rSrc))
432 opcode = kThumbMovRR;
433 else if (!LOWREG(rDest) && !LOWREG(rSrc))
434 opcode = kThumbMovRR_H2H;
435 else if (LOWREG(rDest))
436 opcode = kThumbMovRR_H2L;
437 else
438 opcode = kThumbMovRR_L2H;
buzbeea2ebdd72012-03-04 14:57:06 -0800439 res = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, rDest, rSrc);
buzbee86a4bce2012-03-06 18:15:00 -0800440 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && rDest == rSrc) {
buzbee31a4a6f2012-02-28 15:36:15 -0800441 res->flags.isNop = true;
442 }
443 return res;
buzbee67bf8852011-08-17 17:51:35 -0700444}
445
buzbee82488f52012-03-02 08:20:26 -0800446LIR* opRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
buzbee67bf8852011-08-17 17:51:35 -0700447{
buzbee82488f52012-03-02 08:20:26 -0800448 LIR* res = opRegCopyNoInsert(cUnit, rDest, rSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800449 oatAppendLIR(cUnit, (LIR*)res);
450 return res;
451}
buzbee67bf8852011-08-17 17:51:35 -0700452
buzbee82488f52012-03-02 08:20:26 -0800453void opRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
buzbee31a4a6f2012-02-28 15:36:15 -0800454 int srcLo, int srcHi)
455{
456 bool destFP = FPREG(destLo) && FPREG(destHi);
457 bool srcFP = FPREG(srcLo) && FPREG(srcHi);
458 DCHECK_EQ(FPREG(srcLo), FPREG(srcHi));
459 DCHECK_EQ(FPREG(destLo), FPREG(destHi));
460 if (destFP) {
461 if (srcFP) {
buzbee82488f52012-03-02 08:20:26 -0800462 opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi));
buzbee67bf8852011-08-17 17:51:35 -0700463 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800464 newLIR3(cUnit, kThumb2Fmdrr, S2D(destLo, destHi), srcLo, srcHi);
465 }
466 } else {
467 if (srcFP) {
468 newLIR3(cUnit, kThumb2Fmrrd, destLo, destHi, S2D(srcLo, srcHi));
469 } else {
470 // Handle overlap
471 if (srcHi == destLo) {
buzbee82488f52012-03-02 08:20:26 -0800472 opRegCopy(cUnit, destHi, srcHi);
473 opRegCopy(cUnit, destLo, srcLo);
buzbee67bf8852011-08-17 17:51:35 -0700474 } else {
buzbee82488f52012-03-02 08:20:26 -0800475 opRegCopy(cUnit, destLo, srcLo);
476 opRegCopy(cUnit, destHi, srcHi);
buzbee67bf8852011-08-17 17:51:35 -0700477 }
478 }
479 }
buzbee67bf8852011-08-17 17:51:35 -0700480}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800481
buzbee31a4a6f2012-02-28 15:36:15 -0800482
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800483} // namespace art