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 arm-specific codegen factory support. |
| 19 | * It is included by |
| 20 | * |
| 21 | * Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | */ |
| 24 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 25 | namespace art { |
| 26 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 27 | void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 28 | |
buzbee | c5159d5 | 2012-03-03 11:48:39 -0800 | [diff] [blame] | 29 | bool genNegLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest, |
| 30 | RegLocation rlSrc) |
| 31 | { |
| 32 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 33 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 34 | int zReg = oatAllocTemp(cUnit); |
| 35 | loadConstantNoClobber(cUnit, zReg, 0); |
| 36 | // Check for destructive overlap |
| 37 | if (rlResult.lowReg == rlSrc.highReg) { |
| 38 | int tReg = oatAllocTemp(cUnit); |
| 39 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, |
| 40 | zReg, rlSrc.lowReg); |
| 41 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, |
| 42 | zReg, tReg); |
| 43 | oatFreeTemp(cUnit, tReg); |
| 44 | } else { |
| 45 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, |
| 46 | zReg, rlSrc.lowReg); |
| 47 | opRegRegReg(cUnit, kOpSbc, rlResult.highReg, |
| 48 | zReg, rlSrc.highReg); |
| 49 | } |
| 50 | oatFreeTemp(cUnit, zReg); |
| 51 | storeValueWide(cUnit, rlDest, rlResult); |
| 52 | return false; |
| 53 | } |
| 54 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 55 | int loadHelper(CompilationUnit* cUnit, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 56 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 57 | loadWordDisp(cUnit, rSELF, offset, rLR); |
| 58 | return rLR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 59 | } |
| 60 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 61 | void genEntrySequence(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 62 | { |
| 63 | int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills; |
| 64 | /* |
| 65 | * On entry, r0, r1, r2 & r3 are live. Let the register allocation |
| 66 | * mechanism know so it doesn't try to use any of them when |
| 67 | * expanding the frame or flushing. This leaves the utility |
| 68 | * code with a single temp: r12. This should be enough. |
| 69 | */ |
| 70 | oatLockTemp(cUnit, r0); |
| 71 | oatLockTemp(cUnit, r1); |
| 72 | oatLockTemp(cUnit, r2); |
| 73 | oatLockTemp(cUnit, r3); |
| 74 | |
| 75 | /* |
| 76 | * We can safely skip the stack overflow check if we're |
| 77 | * a leaf *and* our frame size < fudge factor. |
| 78 | */ |
| 79 | bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) && |
| 80 | ((size_t)cUnit->frameSize < |
| 81 | Thread::kStackOverflowReservedBytes)); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 82 | newLIR0(cUnit, kPseudoMethodEntry); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 83 | if (!skipOverflowCheck) { |
| 84 | /* Load stack limit */ |
| 85 | loadWordDisp(cUnit, rSELF, |
| 86 | Thread::StackEndOffset().Int32Value(), r12); |
| 87 | } |
| 88 | /* Spill core callee saves */ |
| 89 | newLIR1(cUnit, kThumb2Push, cUnit->coreSpillMask); |
| 90 | /* Need to spill any FP regs? */ |
| 91 | if (cUnit->numFPSpills) { |
| 92 | /* |
| 93 | * NOTE: fp spills are a little different from core spills in that |
| 94 | * they are pushed as a contiguous block. When promoting from |
| 95 | * the fp set, we must allocate all singles from s16..highest-promoted |
| 96 | */ |
| 97 | newLIR1(cUnit, kThumb2VPushCS, cUnit->numFPSpills); |
| 98 | } |
| 99 | if (!skipOverflowCheck) { |
| 100 | opRegRegImm(cUnit, kOpSub, rLR, rSP, |
| 101 | cUnit->frameSize - (spillCount * 4)); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 102 | genRegRegCheck(cUnit, kCondCc, rLR, r12, NULL, |
| 103 | kThrowStackOverflow); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 104 | opRegCopy(cUnit, rSP, rLR); // Establish stack |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 105 | } else { |
| 106 | opRegImm(cUnit, kOpSub, rSP, |
| 107 | cUnit->frameSize - (spillCount * 4)); |
| 108 | } |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 109 | |
| 110 | /* |
| 111 | * Dummy up a RegLocation for the incoming Method* |
| 112 | * It will attempt to keep r0 live (or copy it to home location |
| 113 | * if promoted). |
| 114 | */ |
| 115 | RegLocation rlSrc = cUnit->regLocation[cUnit->methodSReg]; |
| 116 | RegLocation rlMethod = cUnit->regLocation[cUnit->methodSReg]; |
| 117 | rlSrc.location = kLocPhysReg; |
| 118 | rlSrc.lowReg = r0; |
| 119 | rlSrc.home = false; |
| 120 | oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow); |
| 121 | storeValue(cUnit, rlMethod, rlSrc); |
| 122 | |
| 123 | /* Flush the rest of the ins */ |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 124 | flushIns(cUnit); |
| 125 | |
| 126 | if (cUnit->genDebugger) { |
| 127 | // Refresh update debugger callout |
| 128 | loadWordDisp(cUnit, rSELF, |
| 129 | OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND); |
| 130 | genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY); |
| 131 | } |
| 132 | |
| 133 | oatFreeTemp(cUnit, r0); |
| 134 | oatFreeTemp(cUnit, r1); |
| 135 | oatFreeTemp(cUnit, r2); |
| 136 | oatFreeTemp(cUnit, r3); |
| 137 | } |
| 138 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 139 | void genExitSequence(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 140 | { |
| 141 | int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills; |
| 142 | /* |
| 143 | * In the exit path, r0/r1 are live - make sure they aren't |
| 144 | * allocated by the register utilities as temps. |
| 145 | */ |
| 146 | oatLockTemp(cUnit, r0); |
| 147 | oatLockTemp(cUnit, r1); |
| 148 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 149 | newLIR0(cUnit, kPseudoMethodExit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 150 | /* If we're compiling for the debugger, generate an update callout */ |
| 151 | if (cUnit->genDebugger) { |
| 152 | genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT); |
| 153 | } |
| 154 | opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - (spillCount * 4)); |
| 155 | /* Need to restore any FP callee saves? */ |
| 156 | if (cUnit->numFPSpills) { |
| 157 | newLIR1(cUnit, kThumb2VPopCS, cUnit->numFPSpills); |
| 158 | } |
| 159 | if (cUnit->coreSpillMask & (1 << rLR)) { |
| 160 | /* Unspill rLR to rPC */ |
| 161 | cUnit->coreSpillMask &= ~(1 << rLR); |
| 162 | cUnit->coreSpillMask |= (1 << rPC); |
| 163 | } |
| 164 | newLIR1(cUnit, kThumb2Pop, cUnit->coreSpillMask); |
| 165 | if (!(cUnit->coreSpillMask & (1 << rPC))) { |
| 166 | /* We didn't pop to rPC, so must do a bv rLR */ |
| 167 | newLIR1(cUnit, kThumbBx, rLR); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Nop any unconditional branches that go to the next instruction. |
| 173 | * Note: new redundant branches may be inserted later, and we'll |
| 174 | * use a check in final instruction assembly to nop those out. |
| 175 | */ |
| 176 | void removeRedundantBranches(CompilationUnit* cUnit) |
| 177 | { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 178 | LIR* thisLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 179 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 180 | for (thisLIR = (LIR*) cUnit->firstLIRInsn; |
| 181 | thisLIR != (LIR*) cUnit->lastLIRInsn; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 182 | thisLIR = NEXT_LIR(thisLIR)) { |
| 183 | |
| 184 | /* Branch to the next instruction */ |
| 185 | if ((thisLIR->opcode == kThumbBUncond) || |
| 186 | (thisLIR->opcode == kThumb2BUncond)) { |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 187 | LIR* nextLIR = thisLIR; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 188 | |
| 189 | while (true) { |
| 190 | nextLIR = NEXT_LIR(nextLIR); |
| 191 | |
| 192 | /* |
| 193 | * Is the branch target the next instruction? |
| 194 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 195 | if (nextLIR == (LIR*) thisLIR->target) { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 196 | thisLIR->flags.isNop = true; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Found real useful stuff between the branch and the target. |
| 202 | * Need to explicitly check the lastLIRInsn here because it |
| 203 | * might be the last real instruction. |
| 204 | */ |
| 205 | if (!isPseudoOpcode(nextLIR->opcode) || |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 206 | (nextLIR = (LIR*) cUnit->lastLIRInsn)) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 213 | |
| 214 | /* Common initialization routine for an architecture family */ |
| 215 | bool oatArchInit() |
| 216 | { |
| 217 | int i; |
| 218 | |
| 219 | for (i = 0; i < kArmLast; i++) { |
| 220 | if (EncodingMap[i].opcode != i) { |
| 221 | LOG(FATAL) << "Encoding order for " << EncodingMap[i].name << |
| 222 | " is wrong: expecting " << i << ", seeing " << |
| 223 | (int)EncodingMap[i].opcode; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return oatArchVariantInit(); |
| 228 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 229 | } // namespace art |