buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 25 | #include "oat_compilation_unit.h" |
| 26 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 27 | namespace art { |
| 28 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 29 | |
| 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 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 40 | LIR* genIT(CompilationUnit* cUnit, ArmConditionCode code, const char* guide) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 41 | { |
| 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: |
| 61 | LOG(FATAL) << "OAT: bad case in genIT"; |
| 62 | } |
| 63 | mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) | |
| 64 | (1 << (3 - strlen(guide))); |
| 65 | return newLIR2(cUnit, kThumb2It, code, mask); |
| 66 | } |
| 67 | |
| 68 | /* |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 69 | * 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 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 87 | void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 88 | { |
| 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 |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 94 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 95 | true, kAllocData); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 96 | tabRec->table = table; |
| 97 | tabRec->vaddr = mir->offset; |
| 98 | int size = table[1]; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 99 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, |
| 100 | kAllocLIR); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 101 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 102 | |
| 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 |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 116 | newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 117 | // Set up rIdx |
| 118 | int rIdx = oatAllocTemp(cUnit); |
| 119 | loadConstant(cUnit, rIdx, size); |
| 120 | // Establish loop branch target |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 121 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 122 | target->defMask = ENCODE_ALL; |
| 123 | // Load next key/disp |
| 124 | newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp)); |
| 125 | opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg); |
| 126 | // Go if match. NOTE: No instruction set switch here - must stay Thumb2 |
| 127 | genIT(cUnit, kArmCondEq, ""); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 128 | LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 129 | tabRec->bxInst = switchBranch; |
| 130 | // Needs to use setflags encoding here |
| 131 | newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 132 | LIR* branch = opCondBranch(cUnit, kCondNe); |
| 133 | branch->target = (LIR*)target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 137 | void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 138 | { |
| 139 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 140 | if (cUnit->printMe) { |
| 141 | dumpPackedSwitchTable(table); |
| 142 | } |
| 143 | // Add the table to the list - we'll process it later |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 144 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 145 | true, kAllocData); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 146 | tabRec->table = table; |
| 147 | tabRec->vaddr = mir->offset; |
| 148 | int size = table[1]; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 149 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 150 | kAllocLIR); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 151 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 152 | |
| 153 | // Get the switch value |
| 154 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 155 | int tableBase = oatAllocTemp(cUnit); |
| 156 | // Materialize a pointer to the switch table |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 157 | newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 158 | int lowKey = s4FromSwitchData(&table[2]); |
| 159 | int keyReg; |
| 160 | // Remove the bias, if necessary |
| 161 | if (lowKey == 0) { |
| 162 | keyReg = rlSrc.lowReg; |
| 163 | } else { |
| 164 | keyReg = oatAllocTemp(cUnit); |
| 165 | opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey); |
| 166 | } |
| 167 | // Bounds check - if < 0 or >= size continue following switch |
| 168 | opRegImm(cUnit, kOpCmp, keyReg, size-1); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 169 | LIR* branchOver = opCondBranch(cUnit, kCondHi); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 170 | |
| 171 | // Load the displacement from the switch table |
| 172 | int dispReg = oatAllocTemp(cUnit); |
| 173 | loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord); |
| 174 | |
| 175 | // ..and go! NOTE: No instruction set switch here - must stay Thumb2 |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 176 | LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 177 | tabRec->bxInst = switchBranch; |
| 178 | |
| 179 | /* branchOver target here */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 180 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 181 | target->defMask = ENCODE_ALL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 182 | branchOver->target = (LIR*)target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Array data table format: |
| 187 | * ushort ident = 0x0300 magic value |
| 188 | * ushort width width of each element in the table |
| 189 | * uint size number of elements in the table |
| 190 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 191 | * padding at the end) |
| 192 | * |
| 193 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 194 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 195 | void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 196 | { |
| 197 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 198 | // Add the table to the list - we'll process it later |
| 199 | FillArrayData *tabRec = (FillArrayData *) |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 200 | oatNew(cUnit, sizeof(FillArrayData), true, kAllocData); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 201 | tabRec->table = table; |
| 202 | tabRec->vaddr = mir->offset; |
| 203 | u2 width = tabRec->table[1]; |
| 204 | u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16); |
| 205 | tabRec->size = (size * width) + 8; |
| 206 | |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 207 | oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 208 | |
| 209 | // Making a call - use explicit registers |
| 210 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 211 | loadValueDirectFixed(cUnit, rlSrc, r0); |
| 212 | loadWordDisp(cUnit, rSELF, |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 213 | OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR); |
buzbee | e6d6196 | 2011-08-27 11:58:19 -0700 | [diff] [blame] | 214 | // Materialize a pointer to the fill data image |
buzbee | 03fa263 | 2011-09-20 17:10:57 -0700 | [diff] [blame] | 215 | newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 216 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 219 | void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 220 | { |
| 221 | RegLocation rlResult; |
| 222 | rlSrc = loadValue(cUnit, rlSrc, kFPReg); |
| 223 | rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true); |
| 224 | newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg); |
| 225 | storeValue(cUnit, rlDest, rlResult); |
| 226 | } |
| 227 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 228 | void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 229 | { |
| 230 | RegLocation rlResult; |
| 231 | rlSrc = loadValueWide(cUnit, rlSrc, kFPReg); |
| 232 | rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true); |
| 233 | newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg), |
| 234 | S2D(rlSrc.lowReg, rlSrc.highReg)); |
| 235 | storeValueWide(cUnit, rlDest, rlResult); |
| 236 | } |
| 237 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Handle simple case (thin lock) inline. If it's complicated, bail |
| 240 | * out to the heavyweight lock/unlock routines. We'll use dedicated |
| 241 | * registers here in order to be in the right position in case we |
| 242 | * to bail to dvm[Lock/Unlock]Object(self, object) |
| 243 | * |
| 244 | * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object |
| 245 | * r1 -> object [arg1 for dvm[Lock/Unlock]Object |
| 246 | * r2 -> intial contents of object->lock, later result of strex |
| 247 | * r3 -> self->threadId |
| 248 | * r12 -> allow to be used by utilities as general temp |
| 249 | * |
| 250 | * The result of the strex is 0 if we acquire the lock. |
| 251 | * |
| 252 | * See comments in Sync.c for the layout of the lock word. |
| 253 | * Of particular interest to this code is the test for the |
| 254 | * simple case - which we handle inline. For monitor enter, the |
| 255 | * simple case is thin lock, held by no-one. For monitor exit, |
| 256 | * the simple case is thin lock, held by the unlocking thread with |
| 257 | * a recurse count of 0. |
| 258 | * |
| 259 | * A minor complication is that there is a field in the lock word |
| 260 | * unrelated to locking: the hash state. This field must be ignored, but |
| 261 | * preserved. |
| 262 | * |
| 263 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 264 | void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 265 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 266 | LIR* target; |
| 267 | LIR* hopTarget; |
| 268 | LIR* branch; |
| 269 | LIR* hopBranch; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 270 | |
| 271 | oatFlushAllRegs(cUnit); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 272 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 273 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj |
buzbee | 2e748f3 | 2011-08-29 21:02:19 -0700 | [diff] [blame] | 274 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 275 | genNullCheck(cUnit, rlSrc.sRegLow, r0, mir); |
| 276 | loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 277 | newLIR3(cUnit, kThumb2Ldrex, r1, r0, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 278 | Object::MonitorOffset().Int32Value() >> 2); // Get object->lock |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 279 | // Align owner |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 280 | opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 281 | // Is lock unheld on lock or held by us (==threadId) on unlock? |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 282 | newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1); |
| 283 | newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 284 | hopBranch = newLIR2(cUnit, kThumb2Cbnz, r1, 0); |
| 285 | newLIR4(cUnit, kThumb2Strex, r1, r2, r0, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 286 | Object::MonitorOffset().Int32Value() >> 2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 287 | oatGenMemBarrier(cUnit, kSY); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 288 | branch = newLIR2(cUnit, kThumb2Cbz, r1, 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 289 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 290 | hopTarget = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 291 | hopTarget->defMask = ENCODE_ALL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 292 | hopBranch->target = (LIR*)hopTarget; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 293 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 294 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 295 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 296 | rLR); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 297 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 298 | |
| 299 | // Resume here |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 300 | target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 301 | target->defMask = ENCODE_ALL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 302 | branch->target = (LIR*)target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * For monitor unlock, we don't have to use ldrex/strex. Once |
| 307 | * we've determined that the lock is thin and that we own it with |
| 308 | * a zero recursion count, it's safe to punch it back to the |
| 309 | * initial, unlock thin state with a store word. |
| 310 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 311 | void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 312 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 313 | LIR* target; |
| 314 | LIR* branch; |
| 315 | LIR* hopTarget; |
| 316 | LIR* hopBranch; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 317 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 318 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 319 | oatFlushAllRegs(cUnit); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 320 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj |
buzbee | 2e748f3 | 2011-08-29 21:02:19 -0700 | [diff] [blame] | 321 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 322 | genNullCheck(cUnit, rlSrc.sRegLow, r0, mir); |
| 323 | loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock |
| 324 | loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 325 | // Is lock unheld on lock or held by us (==threadId) on unlock? |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 326 | opRegRegImm(cUnit, kOpAnd, r3, r1, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT)); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 327 | // Align owner |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 328 | opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
| 329 | newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 330 | opRegReg(cUnit, kOpSub, r1, r2); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 331 | hopBranch = opCondBranch(cUnit, kCondNe); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 332 | oatGenMemBarrier(cUnit, kSY); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 333 | storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 334 | branch = opNone(cUnit, kOpUncondBr); |
| 335 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 336 | hopTarget = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 337 | hopTarget->defMask = ENCODE_ALL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 338 | hopBranch->target = (LIR*)hopTarget; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 339 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 340 | // Go expensive route - UnlockObjectFromCode(obj); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 341 | loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 342 | rLR); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 343 | callRuntimeHelper(cUnit, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 344 | |
| 345 | // Resume here |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 346 | target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 347 | target->defMask = ENCODE_ALL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 348 | branch->target = (LIR*)target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
| 352 | * 64-bit 3way compare function. |
| 353 | * mov rX, #-1 |
| 354 | * cmp op1hi, op2hi |
| 355 | * blt done |
| 356 | * bgt flip |
| 357 | * sub rX, op1lo, op2lo (treat as unsigned) |
| 358 | * beq done |
| 359 | * ite hi |
| 360 | * mov(hi) rX, #-1 |
| 361 | * mov(!hi) rX, #1 |
| 362 | * flip: |
| 363 | * neg rX |
| 364 | * done: |
| 365 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 366 | void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 367 | RegLocation rlSrc1, RegLocation rlSrc2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 368 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 369 | LIR* target1; |
| 370 | LIR* target2; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 371 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 372 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 373 | int tReg = oatAllocTemp(cUnit); |
| 374 | loadConstant(cUnit, tReg, -1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 375 | opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 376 | LIR* branch1 = opCondBranch(cUnit, kCondLt); |
| 377 | LIR* branch2 = opCondBranch(cUnit, kCondGt); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 378 | opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 379 | LIR* branch3 = opCondBranch(cUnit, kCondEq); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 380 | |
| 381 | genIT(cUnit, kArmCondHi, "E"); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 382 | newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1)); |
| 383 | loadConstant(cUnit, tReg, 1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 384 | genBarrier(cUnit); |
| 385 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 386 | target2 = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 387 | target2->defMask = -1; |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 388 | opRegReg(cUnit, kOpNeg, tReg, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 389 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 390 | target1 = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 391 | target1->defMask = -1; |
| 392 | |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 393 | RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change |
| 394 | rlTemp.lowReg = tReg; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 395 | storeValue(cUnit, rlDest, rlTemp); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 396 | oatFreeTemp(cUnit, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 397 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 398 | branch1->target = (LIR*)target1; |
| 399 | branch2->target = (LIR*)target2; |
| 400 | branch3->target = branch1->target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 401 | } |
| 402 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 403 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 404 | * Generate a register comparison to an immediate and branch. Caller |
| 405 | * is responsible for setting branch target field. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 406 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 407 | LIR* genCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg, |
| 408 | int checkValue) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 409 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 410 | LIR* branch; |
| 411 | int modImm; |
| 412 | ArmConditionCode armCond = oatArmConditionEncoding(cond); |
| 413 | if ((LOWREG(reg)) && (checkValue == 0) && |
| 414 | ((armCond == kArmCondEq) || (armCond == kArmCondNe))) { |
| 415 | branch = newLIR2(cUnit, |
| 416 | (armCond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz, |
| 417 | reg, 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 418 | } else { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 419 | modImm = modifiedImmediate(checkValue); |
| 420 | if (LOWREG(reg) && ((checkValue & 0xff) == checkValue)) { |
| 421 | newLIR2(cUnit, kThumbCmpRI8, reg, checkValue); |
| 422 | } else if (modImm >= 0) { |
| 423 | newLIR2(cUnit, kThumb2CmpRI8, reg, modImm); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 424 | } else { |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 425 | int tReg = oatAllocTemp(cUnit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 426 | loadConstant(cUnit, tReg, checkValue); |
| 427 | opRegReg(cUnit, kOpCmp, reg, tReg); |
buzbee | 58f9274 | 2011-10-01 11:22:17 -0700 | [diff] [blame] | 428 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 429 | branch = newLIR2(cUnit, kThumbBCond, 0, armCond); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 430 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 431 | return branch; |
| 432 | } |
| 433 | LIR* genRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc) |
| 434 | { |
| 435 | LIR* res; |
| 436 | ArmOpcode opcode; |
| 437 | if (FPREG(rDest) || FPREG(rSrc)) |
| 438 | return fpRegCopy(cUnit, rDest, rSrc); |
| 439 | res = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR); |
| 440 | res->dalvikOffset = cUnit->currentDalvikOffset; |
| 441 | if (LOWREG(rDest) && LOWREG(rSrc)) |
| 442 | opcode = kThumbMovRR; |
| 443 | else if (!LOWREG(rDest) && !LOWREG(rSrc)) |
| 444 | opcode = kThumbMovRR_H2H; |
| 445 | else if (LOWREG(rDest)) |
| 446 | opcode = kThumbMovRR_H2L; |
| 447 | else |
| 448 | opcode = kThumbMovRR_L2H; |
| 449 | |
| 450 | res->operands[0] = rDest; |
| 451 | res->operands[1] = rSrc; |
| 452 | res->opcode = opcode; |
| 453 | setupResourceMasks(res); |
| 454 | if (rDest == rSrc) { |
| 455 | res->flags.isNop = true; |
| 456 | } |
| 457 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 458 | } |
| 459 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 460 | LIR* genRegCopy(CompilationUnit* cUnit, int rDest, int rSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 461 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 462 | LIR* res = genRegCopyNoInsert(cUnit, rDest, rSrc); |
| 463 | oatAppendLIR(cUnit, (LIR*)res); |
| 464 | return res; |
| 465 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 466 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 467 | void genRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi, |
| 468 | int srcLo, int srcHi) |
| 469 | { |
| 470 | bool destFP = FPREG(destLo) && FPREG(destHi); |
| 471 | bool srcFP = FPREG(srcLo) && FPREG(srcHi); |
| 472 | DCHECK_EQ(FPREG(srcLo), FPREG(srcHi)); |
| 473 | DCHECK_EQ(FPREG(destLo), FPREG(destHi)); |
| 474 | if (destFP) { |
| 475 | if (srcFP) { |
| 476 | genRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 477 | } else { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 478 | newLIR3(cUnit, kThumb2Fmdrr, S2D(destLo, destHi), srcLo, srcHi); |
| 479 | } |
| 480 | } else { |
| 481 | if (srcFP) { |
| 482 | newLIR3(cUnit, kThumb2Fmrrd, destLo, destHi, S2D(srcLo, srcHi)); |
| 483 | } else { |
| 484 | // Handle overlap |
| 485 | if (srcHi == destLo) { |
| 486 | genRegCopy(cUnit, destHi, srcHi); |
| 487 | genRegCopy(cUnit, destLo, srcLo); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 488 | } else { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 489 | genRegCopy(cUnit, destLo, srcLo); |
| 490 | genRegCopy(cUnit, destHi, srcHi); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 494 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 495 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 496 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 497 | } // namespace art |