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