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