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" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 26 | #include "oat/runtime/oat_support_entrypoints.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 27 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 28 | namespace art { |
| 29 | |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 30 | |
| 31 | /* Return the position of an ssa name within the argument list */ |
| 32 | int inPosition(CompilationUnit* cUnit, int sReg) |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 33 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 34 | int vReg = SRegToVReg(cUnit, sReg); |
| 35 | return vReg - cUnit->numRegs; |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Describe an argument. If it's already in an arg register, just leave it |
| 40 | * there. NOTE: all live arg registers must be locked prior to this call |
| 41 | * to avoid having them allocated as a temp by downstream utilities. |
| 42 | */ |
| 43 | RegLocation argLoc(CompilationUnit* cUnit, RegLocation loc) |
| 44 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 45 | int argNum = inPosition(cUnit, loc.sRegLow); |
| 46 | if (loc.wide) { |
| 47 | if (argNum == 2) { |
| 48 | // Bad case - half in register, half in frame. Just punt |
| 49 | loc.location = kLocInvalid; |
| 50 | } else if (argNum < 2) { |
| 51 | loc.lowReg = rARG1 + argNum; |
| 52 | loc.highReg = loc.lowReg + 1; |
| 53 | loc.location = kLocPhysReg; |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 54 | } else { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 55 | loc.location = kLocDalvikFrame; |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 56 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 57 | } else { |
| 58 | if (argNum < 3) { |
| 59 | loc.lowReg = rARG1 + argNum; |
| 60 | loc.location = kLocPhysReg; |
| 61 | } else { |
| 62 | loc.location = kLocDalvikFrame; |
| 63 | } |
| 64 | } |
| 65 | return loc; |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Load an argument. If already in a register, just return. If in |
| 70 | * the frame, we can't use the normal loadValue() because it assumed |
| 71 | * a proper frame - and we're frameless. |
| 72 | */ |
| 73 | RegLocation loadArg(CompilationUnit* cUnit, RegLocation loc) |
| 74 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 75 | if (loc.location == kLocDalvikFrame) { |
| 76 | int start = (inPosition(cUnit, loc.sRegLow) + 1) * sizeof(uint32_t); |
| 77 | loc.lowReg = oatAllocTemp(cUnit); |
| 78 | loadWordDisp(cUnit, rSP, start, loc.lowReg); |
| 79 | if (loc.wide) { |
| 80 | loc.highReg = oatAllocTemp(cUnit); |
| 81 | loadWordDisp(cUnit, rSP, start + sizeof(uint32_t), loc.highReg); |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 82 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 83 | loc.location = kLocPhysReg; |
| 84 | } |
| 85 | return loc; |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Lock any referenced arguments that arrive in registers */ |
| 89 | void lockLiveArgs(CompilationUnit* cUnit, MIR* mir) |
| 90 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 91 | int firstIn = cUnit->numRegs; |
| 92 | const int numArgRegs = 3; // TODO: generalize & move to RegUtil.cc |
| 93 | for (int i = 0; i < mir->ssaRep->numUses; i++) { |
| 94 | int vReg = SRegToVReg(cUnit, mir->ssaRep->uses[i]); |
| 95 | int inPosition = vReg - firstIn; |
| 96 | if (inPosition < numArgRegs) { |
| 97 | oatLockTemp(cUnit, rARG1 + inPosition); |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 98 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 99 | } |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 100 | } |
| 101 | |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 102 | /* Find the next MIR, which may be in a following basic block */ |
| 103 | MIR* getNextMir(CompilationUnit* cUnit, BasicBlock** pBb, MIR* mir) |
| 104 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 105 | BasicBlock* bb = *pBb; |
| 106 | MIR* origMir = mir; |
| 107 | while (bb != NULL) { |
| 108 | if (mir != NULL) { |
| 109 | mir = mir->next; |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 110 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 111 | if (mir != NULL) { |
| 112 | return mir; |
| 113 | } else { |
| 114 | bb = bb->fallThrough; |
| 115 | *pBb = bb; |
| 116 | if (bb) { |
| 117 | mir = bb->firstMIRInsn; |
| 118 | if (mir != NULL) { |
| 119 | return mir; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return origMir; |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* Used for the "printMe" listing */ |
| 128 | void genPrintLabel(CompilationUnit *cUnit, MIR* mir) |
| 129 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 130 | LIR* boundaryLIR; |
| 131 | /* Mark the beginning of a Dalvik instruction for line tracking */ |
| 132 | char* instStr = cUnit->printMe ? |
| 133 | oatGetDalvikDisassembly(cUnit, mir->dalvikInsn, "") : NULL; |
| 134 | boundaryLIR = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary, |
| 135 | (intptr_t) instStr); |
| 136 | cUnit->boundaryMap.Put(mir->offset, boundaryLIR); |
| 137 | /* Don't generate the SSA annotation unless verbose mode is on */ |
| 138 | if (cUnit->printMe && mir->ssaRep) { |
| 139 | char* ssaString = oatGetSSAString(cUnit, mir->ssaRep); |
| 140 | newLIR1(cUnit, kPseudoSSARep, (int) ssaString); |
| 141 | } |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | MIR* specialIGet(CompilationUnit* cUnit, BasicBlock** bb, MIR* mir, |
| 145 | OpSize size, bool longOrDouble, bool isObject) |
| 146 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 147 | int fieldOffset; |
| 148 | bool isVolatile; |
| 149 | uint32_t fieldIdx = mir->dalvikInsn.vC; |
| 150 | bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false); |
| 151 | if (!fastPath || !(mir->optimizationFlags & MIR_IGNORE_NULL_CHECK)) { |
| 152 | return NULL; |
| 153 | } |
| 154 | RegLocation rlObj = oatGetSrc(cUnit, mir, 0); |
| 155 | lockLiveArgs(cUnit, mir); |
| 156 | rlObj = argLoc(cUnit, rlObj); |
| 157 | RegLocation rlDest; |
| 158 | if (longOrDouble) { |
| 159 | rlDest = oatGetReturnWide(cUnit, false); |
| 160 | } else { |
| 161 | rlDest = oatGetReturn(cUnit, false); |
| 162 | } |
| 163 | // Point of no return - no aborts after this |
| 164 | genPrintLabel(cUnit, mir); |
| 165 | rlObj = loadArg(cUnit, rlObj); |
| 166 | genIGet(cUnit, mir, size, rlDest, rlObj, longOrDouble, isObject); |
| 167 | return getNextMir(cUnit, bb, mir); |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | MIR* specialIPut(CompilationUnit* cUnit, BasicBlock** bb, MIR* mir, |
| 171 | OpSize size, bool longOrDouble, bool isObject) |
| 172 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 173 | int fieldOffset; |
| 174 | bool isVolatile; |
| 175 | uint32_t fieldIdx = mir->dalvikInsn.vC; |
| 176 | bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile, false); |
| 177 | if (!fastPath || !(mir->optimizationFlags & MIR_IGNORE_NULL_CHECK)) { |
| 178 | return NULL; |
| 179 | } |
| 180 | RegLocation rlSrc; |
| 181 | RegLocation rlObj; |
| 182 | lockLiveArgs(cUnit, mir); |
| 183 | if (longOrDouble) { |
| 184 | rlSrc = oatGetSrcWide(cUnit, mir, 0, 1); |
| 185 | rlObj = oatGetSrc(cUnit, mir, 2); |
| 186 | } else { |
| 187 | rlSrc = oatGetSrc(cUnit, mir, 0); |
| 188 | rlObj = oatGetSrc(cUnit, mir, 1); |
| 189 | } |
| 190 | rlSrc = argLoc(cUnit, rlSrc); |
| 191 | rlObj = argLoc(cUnit, rlObj); |
| 192 | // Reject if source is split across registers & frame |
| 193 | if (rlObj.location == kLocInvalid) { |
| 194 | oatResetRegPool(cUnit); |
| 195 | return NULL; |
| 196 | } |
| 197 | // Point of no return - no aborts after this |
| 198 | genPrintLabel(cUnit, mir); |
| 199 | rlObj = loadArg(cUnit, rlObj); |
| 200 | rlSrc = loadArg(cUnit, rlSrc); |
| 201 | genIPut(cUnit, mir, size, rlSrc, rlObj, longOrDouble, isObject); |
| 202 | return getNextMir(cUnit, bb, mir); |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 203 | } |
| 204 | |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 205 | MIR* specialIdentity(CompilationUnit* cUnit, MIR* mir) |
| 206 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 207 | RegLocation rlSrc; |
| 208 | RegLocation rlDest; |
| 209 | bool wide = (mir->ssaRep->numUses == 2); |
| 210 | if (wide) { |
| 211 | rlSrc = oatGetSrcWide(cUnit, mir, 0, 1); |
| 212 | rlDest = oatGetReturnWide(cUnit, false); |
| 213 | } else { |
| 214 | rlSrc = oatGetSrc(cUnit, mir, 0); |
| 215 | rlDest = oatGetReturn(cUnit, false); |
| 216 | } |
| 217 | lockLiveArgs(cUnit, mir); |
| 218 | rlSrc = argLoc(cUnit, rlSrc); |
| 219 | if (rlSrc.location == kLocInvalid) { |
| 220 | oatResetRegPool(cUnit); |
| 221 | return NULL; |
| 222 | } |
| 223 | // Point of no return - no aborts after this |
| 224 | genPrintLabel(cUnit, mir); |
| 225 | rlSrc = loadArg(cUnit, rlSrc); |
| 226 | if (wide) { |
| 227 | storeValueWide(cUnit, rlDest, rlSrc); |
| 228 | } else { |
| 229 | storeValue(cUnit, rlDest, rlSrc); |
| 230 | } |
| 231 | return mir; |
buzbee | e62076c | 2012-03-21 14:26:16 -0700 | [diff] [blame] | 232 | } |
| 233 | |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 234 | /* |
| 235 | * Special-case code genration for simple non-throwing leaf methods. |
| 236 | */ |
| 237 | void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 238 | SpecialCaseHandler specialCase) |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 239 | { |
| 240 | cUnit->currentDalvikOffset = mir->offset; |
| 241 | MIR* nextMir = NULL; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 242 | switch (specialCase) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 243 | case kNullMethod: |
| 244 | DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID); |
| 245 | nextMir = mir; |
| 246 | break; |
| 247 | case kConstFunction: |
| 248 | genPrintLabel(cUnit, mir); |
| 249 | loadConstant(cUnit, rRET0, mir->dalvikInsn.vB); |
| 250 | nextMir = getNextMir(cUnit, &bb, mir); |
| 251 | break; |
| 252 | case kIGet: |
| 253 | nextMir = specialIGet(cUnit, &bb, mir, kWord, false, false); |
| 254 | break; |
| 255 | case kIGetBoolean: |
| 256 | case kIGetByte: |
| 257 | nextMir = specialIGet(cUnit, &bb, mir, kUnsignedByte, false, false); |
| 258 | break; |
| 259 | case kIGetObject: |
| 260 | nextMir = specialIGet(cUnit, &bb, mir, kWord, false, true); |
| 261 | break; |
| 262 | case kIGetChar: |
| 263 | nextMir = specialIGet(cUnit, &bb, mir, kUnsignedHalf, false, false); |
| 264 | break; |
| 265 | case kIGetShort: |
| 266 | nextMir = specialIGet(cUnit, &bb, mir, kSignedHalf, false, false); |
| 267 | break; |
| 268 | case kIGetWide: |
| 269 | nextMir = specialIGet(cUnit, &bb, mir, kLong, true, false); |
| 270 | break; |
| 271 | case kIPut: |
| 272 | nextMir = specialIPut(cUnit, &bb, mir, kWord, false, false); |
| 273 | break; |
| 274 | case kIPutBoolean: |
| 275 | case kIPutByte: |
| 276 | nextMir = specialIPut(cUnit, &bb, mir, kUnsignedByte, false, false); |
| 277 | break; |
| 278 | case kIPutObject: |
| 279 | nextMir = specialIPut(cUnit, &bb, mir, kWord, false, true); |
| 280 | break; |
| 281 | case kIPutChar: |
| 282 | nextMir = specialIPut(cUnit, &bb, mir, kUnsignedHalf, false, false); |
| 283 | break; |
| 284 | case kIPutShort: |
| 285 | nextMir = specialIPut(cUnit, &bb, mir, kSignedHalf, false, false); |
| 286 | break; |
| 287 | case kIPutWide: |
| 288 | nextMir = specialIPut(cUnit, &bb, mir, kLong, true, false); |
| 289 | break; |
| 290 | case kIdentity: |
| 291 | nextMir = specialIdentity(cUnit, mir); |
| 292 | break; |
| 293 | default: |
| 294 | return; |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 295 | } |
| 296 | if (nextMir != NULL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 297 | cUnit->currentDalvikOffset = nextMir->offset; |
| 298 | if (specialCase != kIdentity) { |
| 299 | genPrintLabel(cUnit, nextMir); |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 300 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 301 | newLIR1(cUnit, kThumbBx, rLR); |
| 302 | cUnit->coreSpillMask = 0; |
| 303 | cUnit->numCoreSpills = 0; |
| 304 | cUnit->fpSpillMask = 0; |
| 305 | cUnit->numFPSpills = 0; |
| 306 | cUnit->frameSize = 0; |
| 307 | cUnit->coreVmapTable.clear(); |
| 308 | cUnit->fpVmapTable.clear(); |
| 309 | } |
buzbee | 16da88c | 2012-03-20 10:38:17 -0700 | [diff] [blame] | 310 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * Generate a Thumb2 IT instruction, which can nullify up to |
| 314 | * four subsequent instructions based on a condition and its |
| 315 | * inverse. The condition applies to the first instruction, which |
| 316 | * is executed if the condition is met. The string "guide" consists |
| 317 | * of 0 to 3 chars, and applies to the 2nd through 4th instruction. |
| 318 | * A "T" means the instruction is executed if the condition is |
| 319 | * met, and an "E" means the instruction is executed if the condition |
| 320 | * is not met. |
| 321 | */ |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 322 | LIR* opIT(CompilationUnit* cUnit, ArmConditionCode code, const char* guide) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 323 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 324 | int mask; |
| 325 | int condBit = code & 1; |
| 326 | int altBit = condBit ^ 1; |
| 327 | int mask3 = 0; |
| 328 | int mask2 = 0; |
| 329 | int mask1 = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 330 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 331 | //Note: case fallthroughs intentional |
| 332 | switch (strlen(guide)) { |
| 333 | case 3: |
| 334 | mask1 = (guide[2] == 'T') ? condBit : altBit; |
| 335 | case 2: |
| 336 | mask2 = (guide[1] == 'T') ? condBit : altBit; |
| 337 | case 1: |
| 338 | mask3 = (guide[0] == 'T') ? condBit : altBit; |
| 339 | break; |
| 340 | case 0: |
| 341 | break; |
| 342 | default: |
| 343 | LOG(FATAL) << "OAT: bad case in opIT"; |
| 344 | } |
| 345 | mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) | |
| 346 | (1 << (3 - strlen(guide))); |
| 347 | return newLIR2(cUnit, kThumb2It, code, mask); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 351 | * The sparse table in the literal pool is an array of <key,displacement> |
| 352 | * pairs. For each set, we'll load them as a pair using ldmia. |
| 353 | * This means that the register number of the temp we use for the key |
| 354 | * must be lower than the reg for the displacement. |
| 355 | * |
| 356 | * The test loop will look something like: |
| 357 | * |
| 358 | * adr rBase, <table> |
| 359 | * ldr rVal, [rSP, vRegOff] |
| 360 | * mov rIdx, #tableSize |
| 361 | * lp: |
| 362 | * ldmia rBase!, {rKey, rDisp} |
| 363 | * sub rIdx, #1 |
| 364 | * cmp rVal, rKey |
| 365 | * ifeq |
| 366 | * add rPC, rDisp ; This is the branch from which we compute displacement |
| 367 | * cbnz rIdx, lp |
| 368 | */ |
Ian Rogers | 55bd45f | 2012-04-04 17:31:20 -0700 | [diff] [blame] | 369 | void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc, |
| 370 | LIR* labelList) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 371 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 372 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 373 | if (cUnit->printMe) { |
| 374 | dumpSparseSwitchTable(table); |
| 375 | } |
| 376 | // Add the table to the list - we'll process it later |
| 377 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 378 | true, kAllocData); |
| 379 | tabRec->table = table; |
| 380 | tabRec->vaddr = mir->offset; |
| 381 | int size = table[1]; |
| 382 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, kAllocLIR); |
| 383 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 384 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 385 | // Get the switch value |
| 386 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 387 | int rBase = oatAllocTemp(cUnit); |
| 388 | /* Allocate key and disp temps */ |
| 389 | int rKey = oatAllocTemp(cUnit); |
| 390 | int rDisp = oatAllocTemp(cUnit); |
| 391 | // Make sure rKey's register number is less than rDisp's number for ldmia |
| 392 | if (rKey > rDisp) { |
| 393 | int tmp = rDisp; |
| 394 | rDisp = rKey; |
| 395 | rKey = tmp; |
| 396 | } |
| 397 | // Materialize a pointer to the switch table |
| 398 | newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec); |
| 399 | // Set up rIdx |
| 400 | int rIdx = oatAllocTemp(cUnit); |
| 401 | loadConstant(cUnit, rIdx, size); |
| 402 | // Establish loop branch target |
| 403 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
| 404 | // Load next key/disp |
| 405 | newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp)); |
| 406 | opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg); |
| 407 | // Go if match. NOTE: No instruction set switch here - must stay Thumb2 |
| 408 | opIT(cUnit, kArmCondEq, ""); |
| 409 | LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp); |
| 410 | tabRec->anchor = switchBranch; |
| 411 | // Needs to use setflags encoding here |
| 412 | newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1); |
| 413 | opCondBranch(cUnit, kCondNe, target); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 417 | void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 418 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 419 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 420 | if (cUnit->printMe) { |
| 421 | dumpPackedSwitchTable(table); |
| 422 | } |
| 423 | // Add the table to the list - we'll process it later |
| 424 | SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable), |
| 425 | true, kAllocData); |
| 426 | tabRec->table = table; |
| 427 | tabRec->vaddr = mir->offset; |
| 428 | int size = table[1]; |
| 429 | tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true, kAllocLIR); |
| 430 | oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 431 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 432 | // Get the switch value |
| 433 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 434 | int tableBase = oatAllocTemp(cUnit); |
| 435 | // Materialize a pointer to the switch table |
| 436 | newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec); |
| 437 | int lowKey = s4FromSwitchData(&table[2]); |
| 438 | int keyReg; |
| 439 | // Remove the bias, if necessary |
| 440 | if (lowKey == 0) { |
| 441 | keyReg = rlSrc.lowReg; |
| 442 | } else { |
| 443 | keyReg = oatAllocTemp(cUnit); |
| 444 | opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey); |
| 445 | } |
| 446 | // Bounds check - if < 0 or >= size continue following switch |
| 447 | opRegImm(cUnit, kOpCmp, keyReg, size-1); |
| 448 | LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 449 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 450 | // Load the displacement from the switch table |
| 451 | int dispReg = oatAllocTemp(cUnit); |
| 452 | loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 453 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 454 | // ..and go! NOTE: No instruction set switch here - must stay Thumb2 |
| 455 | LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg); |
| 456 | tabRec->anchor = switchBranch; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 457 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 458 | /* branchOver target here */ |
| 459 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
| 460 | branchOver->target = (LIR*)target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Array data table format: |
| 465 | * ushort ident = 0x0300 magic value |
| 466 | * ushort width width of each element in the table |
| 467 | * uint size number of elements in the table |
| 468 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 469 | * padding at the end) |
| 470 | * |
| 471 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 472 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 473 | void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 474 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 475 | const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB; |
| 476 | // Add the table to the list - we'll process it later |
| 477 | FillArrayData *tabRec = (FillArrayData *) |
| 478 | oatNew(cUnit, sizeof(FillArrayData), true, kAllocData); |
| 479 | tabRec->table = table; |
| 480 | tabRec->vaddr = mir->offset; |
| 481 | u2 width = tabRec->table[1]; |
| 482 | u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16); |
| 483 | tabRec->size = (size * width) + 8; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 484 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 485 | oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 486 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 487 | // Making a call - use explicit registers |
| 488 | oatFlushAllRegs(cUnit); /* Everything to home location */ |
| 489 | loadValueDirectFixed(cUnit, rlSrc, r0); |
| 490 | loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), |
| 491 | rLR); |
| 492 | // Materialize a pointer to the fill data image |
| 493 | newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec); |
| 494 | oatClobberCalleeSave(cUnit); |
| 495 | opReg(cUnit, kOpBlx, rLR); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 496 | } |
| 497 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 498 | void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 499 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 500 | RegLocation rlResult; |
| 501 | rlSrc = loadValue(cUnit, rlSrc, kFPReg); |
| 502 | rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true); |
| 503 | newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg); |
| 504 | storeValue(cUnit, rlDest, rlResult); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 505 | } |
| 506 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 507 | void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 508 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 509 | RegLocation rlResult; |
| 510 | rlSrc = loadValueWide(cUnit, rlSrc, kFPReg); |
| 511 | rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true); |
| 512 | newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg), |
| 513 | S2D(rlSrc.lowReg, rlSrc.highReg)); |
| 514 | storeValueWide(cUnit, rlDest, rlResult); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 515 | } |
| 516 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 517 | /* |
| 518 | * Handle simple case (thin lock) inline. If it's complicated, bail |
| 519 | * out to the heavyweight lock/unlock routines. We'll use dedicated |
| 520 | * registers here in order to be in the right position in case we |
| 521 | * to bail to dvm[Lock/Unlock]Object(self, object) |
| 522 | * |
| 523 | * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object |
| 524 | * r1 -> object [arg1 for dvm[Lock/Unlock]Object |
| 525 | * r2 -> intial contents of object->lock, later result of strex |
| 526 | * r3 -> self->threadId |
| 527 | * r12 -> allow to be used by utilities as general temp |
| 528 | * |
| 529 | * The result of the strex is 0 if we acquire the lock. |
| 530 | * |
| 531 | * See comments in Sync.c for the layout of the lock word. |
| 532 | * Of particular interest to this code is the test for the |
| 533 | * simple case - which we handle inline. For monitor enter, the |
| 534 | * simple case is thin lock, held by no-one. For monitor exit, |
| 535 | * the simple case is thin lock, held by the unlocking thread with |
| 536 | * a recurse count of 0. |
| 537 | * |
| 538 | * A minor complication is that there is a field in the lock word |
| 539 | * unrelated to locking: the hash state. This field must be ignored, but |
| 540 | * preserved. |
| 541 | * |
| 542 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 543 | void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 544 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 545 | oatFlushAllRegs(cUnit); |
| 546 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
| 547 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj |
| 548 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 549 | genNullCheck(cUnit, rlSrc.sRegLow, r0, mir); |
| 550 | loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 551 | newLIR3(cUnit, kThumb2Ldrex, r1, r0, |
| 552 | Object::MonitorOffset().Int32Value() >> 2); // Get object->lock |
| 553 | // Align owner |
| 554 | opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
| 555 | // Is lock unheld on lock or held by us (==threadId) on unlock? |
| 556 | newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1); |
| 557 | newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 558 | opRegImm(cUnit, kOpCmp, r1, 0); |
| 559 | opIT(cUnit, kArmCondEq, ""); |
| 560 | newLIR4(cUnit, kThumb2Strex, r1, r2, r0, |
| 561 | Object::MonitorOffset().Int32Value() >> 2); |
| 562 | opRegImm(cUnit, kOpCmp, r1, 0); |
| 563 | opIT(cUnit, kArmCondNe, "T"); |
| 564 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 565 | loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pLockObjectFromCode), rLR); |
| 566 | oatClobberCalleeSave(cUnit); |
| 567 | opReg(cUnit, kOpBlx, rLR); |
| 568 | oatGenMemBarrier(cUnit, kSY); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
| 572 | * For monitor unlock, we don't have to use ldrex/strex. Once |
| 573 | * we've determined that the lock is thin and that we own it with |
| 574 | * a zero recursion count, it's safe to punch it back to the |
| 575 | * initial, unlock thin state with a store word. |
| 576 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 577 | void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 578 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 579 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
| 580 | oatFlushAllRegs(cUnit); |
| 581 | loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj |
| 582 | oatLockCallTemps(cUnit); // Prepare for explicit register usage |
| 583 | genNullCheck(cUnit, rlSrc.sRegLow, r0, mir); |
| 584 | loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock |
| 585 | loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 586 | // Is lock unheld on lock or held by us (==threadId) on unlock? |
| 587 | opRegRegImm(cUnit, kOpAnd, r3, r1, |
| 588 | (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT)); |
| 589 | // Align owner |
| 590 | opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
| 591 | newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 592 | opRegReg(cUnit, kOpSub, r1, r2); |
| 593 | opIT(cUnit, kArmCondEq, "EE"); |
| 594 | storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3); |
| 595 | // Go expensive route - UnlockObjectFromCode(obj); |
| 596 | loadWordDisp(cUnit, rSELF, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rLR); |
| 597 | oatClobberCalleeSave(cUnit); |
| 598 | opReg(cUnit, kOpBlx, rLR); |
| 599 | oatGenMemBarrier(cUnit, kSY); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
| 603 | * 64-bit 3way compare function. |
| 604 | * mov rX, #-1 |
| 605 | * cmp op1hi, op2hi |
| 606 | * blt done |
| 607 | * bgt flip |
| 608 | * sub rX, op1lo, op2lo (treat as unsigned) |
| 609 | * beq done |
| 610 | * ite hi |
| 611 | * mov(hi) rX, #-1 |
| 612 | * mov(!hi) rX, #1 |
| 613 | * flip: |
| 614 | * neg rX |
| 615 | * done: |
| 616 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 617 | void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 618 | RegLocation rlSrc1, RegLocation rlSrc2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 619 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 620 | LIR* target1; |
| 621 | LIR* target2; |
| 622 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 623 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 624 | int tReg = oatAllocTemp(cUnit); |
| 625 | loadConstant(cUnit, tReg, -1); |
| 626 | opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg); |
| 627 | LIR* branch1 = opCondBranch(cUnit, kCondLt, NULL); |
| 628 | LIR* branch2 = opCondBranch(cUnit, kCondGt, NULL); |
| 629 | opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg); |
| 630 | LIR* branch3 = opCondBranch(cUnit, kCondEq, NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 631 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 632 | opIT(cUnit, kArmCondHi, "E"); |
| 633 | newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1)); |
| 634 | loadConstant(cUnit, tReg, 1); |
| 635 | genBarrier(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 636 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 637 | target2 = newLIR0(cUnit, kPseudoTargetLabel); |
| 638 | opRegReg(cUnit, kOpNeg, tReg, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 639 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 640 | target1 = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 641 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 642 | RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change |
| 643 | rlTemp.lowReg = tReg; |
| 644 | storeValue(cUnit, rlDest, rlTemp); |
| 645 | oatFreeTemp(cUnit, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 646 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 647 | branch1->target = (LIR*)target1; |
| 648 | branch2->target = (LIR*)target2; |
| 649 | branch3->target = branch1->target; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 650 | } |
| 651 | |
buzbee | 84fd693 | 2012-03-29 16:44:16 -0700 | [diff] [blame] | 652 | void genFusedLongCmpBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir) |
| 653 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 654 | LIR* labelList = (LIR*)cUnit->blockLabelList; |
| 655 | LIR* taken = &labelList[bb->taken->id]; |
| 656 | LIR* notTaken = &labelList[bb->fallThrough->id]; |
| 657 | RegLocation rlSrc1 = oatGetSrcWide(cUnit, mir, 0, 1); |
| 658 | RegLocation rlSrc2 = oatGetSrcWide(cUnit, mir, 2, 3); |
| 659 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 660 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 661 | ConditionCode ccode = static_cast<ConditionCode>(mir->dalvikInsn.arg[0]); |
| 662 | opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg); |
| 663 | switch(ccode) { |
| 664 | case kCondEq: |
| 665 | opCondBranch(cUnit, kCondNe, notTaken); |
| 666 | break; |
| 667 | case kCondNe: |
| 668 | opCondBranch(cUnit, kCondNe, taken); |
| 669 | break; |
| 670 | case kCondLt: |
| 671 | opCondBranch(cUnit, kCondLt, taken); |
| 672 | opCondBranch(cUnit, kCondGt, notTaken); |
| 673 | ccode = kCondCc; |
| 674 | break; |
| 675 | case kCondLe: |
| 676 | opCondBranch(cUnit, kCondLt, taken); |
| 677 | opCondBranch(cUnit, kCondGt, notTaken); |
| 678 | ccode = kCondLs; |
| 679 | break; |
| 680 | case kCondGt: |
| 681 | opCondBranch(cUnit, kCondGt, taken); |
| 682 | opCondBranch(cUnit, kCondLt, notTaken); |
| 683 | ccode = kCondHi; |
| 684 | break; |
| 685 | case kCondGe: |
| 686 | opCondBranch(cUnit, kCondGt, taken); |
| 687 | opCondBranch(cUnit, kCondLt, notTaken); |
| 688 | ccode = kCondCs; |
| 689 | break; |
| 690 | default: |
| 691 | LOG(FATAL) << "Unexpected ccode: " << (int)ccode; |
| 692 | } |
| 693 | opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg); |
| 694 | opCondBranch(cUnit, ccode, taken); |
buzbee | 84fd693 | 2012-03-29 16:44:16 -0700 | [diff] [blame] | 695 | } |
| 696 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 697 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 698 | * Generate a register comparison to an immediate and branch. Caller |
| 699 | * is responsible for setting branch target field. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 700 | */ |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 701 | LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 702 | int checkValue, LIR* target) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 703 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 704 | LIR* branch; |
| 705 | int modImm; |
| 706 | ArmConditionCode armCond = oatArmConditionEncoding(cond); |
| 707 | if ((LOWREG(reg)) && (checkValue == 0) && |
| 708 | ((armCond == kArmCondEq) || (armCond == kArmCondNe))) { |
| 709 | branch = newLIR2(cUnit, (armCond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz, |
| 710 | reg, 0); |
| 711 | } else { |
| 712 | modImm = modifiedImmediate(checkValue); |
| 713 | if (LOWREG(reg) && ((checkValue & 0xff) == checkValue)) { |
| 714 | newLIR2(cUnit, kThumbCmpRI8, reg, checkValue); |
| 715 | } else if (modImm >= 0) { |
| 716 | newLIR2(cUnit, kThumb2CmpRI8, reg, modImm); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 717 | } else { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 718 | int tReg = oatAllocTemp(cUnit); |
| 719 | loadConstant(cUnit, tReg, checkValue); |
| 720 | opRegReg(cUnit, kOpCmp, reg, tReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 721 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 722 | branch = newLIR2(cUnit, kThumbBCond, 0, armCond); |
| 723 | } |
| 724 | branch->target = target; |
| 725 | return branch; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 726 | } |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 727 | LIR* opRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 728 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 729 | LIR* res; |
| 730 | ArmOpcode opcode; |
| 731 | if (FPREG(rDest) || FPREG(rSrc)) |
| 732 | return fpRegCopy(cUnit, rDest, rSrc); |
| 733 | if (LOWREG(rDest) && LOWREG(rSrc)) |
| 734 | opcode = kThumbMovRR; |
| 735 | else if (!LOWREG(rDest) && !LOWREG(rSrc)) |
| 736 | opcode = kThumbMovRR_H2H; |
| 737 | else if (LOWREG(rDest)) |
| 738 | opcode = kThumbMovRR_H2L; |
| 739 | else |
| 740 | opcode = kThumbMovRR_L2H; |
| 741 | res = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, rDest, rSrc); |
| 742 | if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && rDest == rSrc) { |
| 743 | res->flags.isNop = true; |
| 744 | } |
| 745 | return res; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 746 | } |
| 747 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 748 | LIR* opRegCopy(CompilationUnit* cUnit, int rDest, int rSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 749 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 750 | LIR* res = opRegCopyNoInsert(cUnit, rDest, rSrc); |
| 751 | oatAppendLIR(cUnit, (LIR*)res); |
| 752 | return res; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 753 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 754 | |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 755 | void opRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 756 | int srcLo, int srcHi) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 757 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 758 | bool destFP = FPREG(destLo) && FPREG(destHi); |
| 759 | bool srcFP = FPREG(srcLo) && FPREG(srcHi); |
| 760 | DCHECK_EQ(FPREG(srcLo), FPREG(srcHi)); |
| 761 | DCHECK_EQ(FPREG(destLo), FPREG(destHi)); |
| 762 | if (destFP) { |
| 763 | if (srcFP) { |
| 764 | opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi)); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 765 | } else { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 766 | newLIR3(cUnit, kThumb2Fmdrr, S2D(destLo, destHi), srcLo, srcHi); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 767 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 768 | } else { |
| 769 | if (srcFP) { |
| 770 | newLIR3(cUnit, kThumb2Fmrrd, destLo, destHi, S2D(srcLo, srcHi)); |
| 771 | } else { |
| 772 | // Handle overlap |
| 773 | if (srcHi == destLo) { |
| 774 | opRegCopy(cUnit, destHi, srcHi); |
| 775 | opRegCopy(cUnit, destLo, srcLo); |
| 776 | } else { |
| 777 | opRegCopy(cUnit, destLo, srcLo); |
| 778 | opRegCopy(cUnit, destHi, srcHi); |
| 779 | } |
| 780 | } |
| 781 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 782 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 783 | |
buzbee | f3aac97 | 2012-04-11 16:33:36 -0700 | [diff] [blame] | 784 | // Table of magic divisors |
| 785 | enum DividePattern { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 786 | DivideNone, |
| 787 | Divide3, |
| 788 | Divide5, |
| 789 | Divide7, |
buzbee | f3aac97 | 2012-04-11 16:33:36 -0700 | [diff] [blame] | 790 | }; |
| 791 | |
| 792 | struct MagicTable { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 793 | uint32_t magic; |
| 794 | uint32_t shift; |
| 795 | DividePattern pattern; |
buzbee | f3aac97 | 2012-04-11 16:33:36 -0700 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | static const MagicTable magicTable[] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 799 | {0, 0, DivideNone}, // 0 |
| 800 | {0, 0, DivideNone}, // 1 |
| 801 | {0, 0, DivideNone}, // 2 |
| 802 | {0x55555556, 0, Divide3}, // 3 |
| 803 | {0, 0, DivideNone}, // 4 |
| 804 | {0x66666667, 1, Divide5}, // 5 |
| 805 | {0x2AAAAAAB, 0, Divide3}, // 6 |
| 806 | {0x92492493, 2, Divide7}, // 7 |
| 807 | {0, 0, DivideNone}, // 8 |
| 808 | {0x38E38E39, 1, Divide5}, // 9 |
| 809 | {0x66666667, 2, Divide5}, // 10 |
| 810 | {0x2E8BA2E9, 1, Divide5}, // 11 |
| 811 | {0x2AAAAAAB, 1, Divide5}, // 12 |
| 812 | {0x4EC4EC4F, 2, Divide5}, // 13 |
| 813 | {0x92492493, 3, Divide7}, // 14 |
| 814 | {0x88888889, 3, Divide7}, // 15 |
buzbee | f3aac97 | 2012-04-11 16:33:36 -0700 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | // Integer division by constant via reciprocal multiply (Hacker's Delight, 10-4) |
| 818 | bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode, |
| 819 | RegLocation rlSrc, RegLocation rlDest, int lit) |
| 820 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 821 | if ((lit < 0) || (lit >= (int)(sizeof(magicTable)/sizeof(magicTable[0])))) { |
| 822 | return false; |
| 823 | } |
| 824 | DividePattern pattern = magicTable[lit].pattern; |
| 825 | if (pattern == DivideNone) { |
| 826 | return false; |
| 827 | } |
| 828 | // Tuning: add rem patterns |
| 829 | if (dalvikOpcode != Instruction::DIV_INT_LIT8) { |
| 830 | return false; |
| 831 | } |
buzbee | f3aac97 | 2012-04-11 16:33:36 -0700 | [diff] [blame] | 832 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 833 | int rMagic = oatAllocTemp(cUnit); |
| 834 | loadConstant(cUnit, rMagic, magicTable[lit].magic); |
| 835 | rlSrc = loadValue(cUnit, rlSrc, kCoreReg); |
| 836 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 837 | int rHi = oatAllocTemp(cUnit); |
| 838 | int rLo = oatAllocTemp(cUnit); |
| 839 | newLIR4(cUnit, kThumb2Smull, rLo, rHi, rMagic, rlSrc.lowReg); |
| 840 | switch(pattern) { |
| 841 | case Divide3: |
| 842 | opRegRegRegShift(cUnit, kOpSub, rlResult.lowReg, rHi, |
| 843 | rlSrc.lowReg, encodeShift(kArmAsr, 31)); |
| 844 | break; |
| 845 | case Divide5: |
| 846 | opRegRegImm(cUnit, kOpAsr, rLo, rlSrc.lowReg, 31); |
| 847 | opRegRegRegShift(cUnit, kOpRsub, rlResult.lowReg, rLo, rHi, |
| 848 | encodeShift(kArmAsr, magicTable[lit].shift)); |
| 849 | break; |
| 850 | case Divide7: |
| 851 | opRegReg(cUnit, kOpAdd, rHi, rlSrc.lowReg); |
| 852 | opRegRegImm(cUnit, kOpAsr, rLo, rlSrc.lowReg, 31); |
| 853 | opRegRegRegShift(cUnit, kOpRsub, rlResult.lowReg, rLo, rHi, |
| 854 | encodeShift(kArmAsr, magicTable[lit].shift)); |
| 855 | break; |
| 856 | default: |
| 857 | LOG(FATAL) << "Unexpected pattern: " << (int)pattern; |
| 858 | } |
| 859 | storeValue(cUnit, rlDest, rlResult); |
| 860 | return true; |
buzbee | f3aac97 | 2012-04-11 16:33:36 -0700 | [diff] [blame] | 861 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 862 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 863 | } // namespace art |