Chris Lattner | 2cfd52c | 2009-07-29 20:31:52 +0000 | [diff] [blame] | 1 | //===- ARMBaseRegisterInfo.cpp - ARM Register Information -------*- C++ -*-===// |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the base ARM implementation of TargetRegisterInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ARM.h" |
| 15 | #include "ARMAddressingModes.h" |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 16 | #include "ARMBaseInstrInfo.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 17 | #include "ARMBaseRegisterInfo.h" |
| 18 | #include "ARMInstrInfo.h" |
| 19 | #include "ARMMachineFunctionInfo.h" |
| 20 | #include "ARMSubtarget.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/DerivedTypes.h" |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/LLVMContext.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 27 | #include "llvm/CodeGen/MachineFunction.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/MachineLocation.h" |
| 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 31 | #include "llvm/CodeGen/RegisterScavenging.h" |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetFrameInfo.h" |
| 36 | #include "llvm/Target/TargetMachine.h" |
| 37 | #include "llvm/Target/TargetOptions.h" |
| 38 | #include "llvm/ADT/BitVector.h" |
| 39 | #include "llvm/ADT/SmallVector.h" |
Jim Grosbach | 18ed9c9 | 2009-10-20 20:19:50 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 41 | |
Dan Gohman | 8c407d4 | 2010-04-15 17:34:58 +0000 | [diff] [blame] | 42 | namespace llvm { |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 44 | ForceAllBaseRegAlloc("arm-force-base-reg-alloc", cl::Hidden, cl::init(false), |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 45 | cl::desc("Force use of virtual base registers for stack load/store")); |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 46 | static cl::opt<bool> |
Jim Grosbach | ae47c6d | 2010-08-26 00:58:06 +0000 | [diff] [blame] | 47 | EnableLocalStackAlloc("enable-local-stack-alloc", cl::init(true), cl::Hidden, |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 48 | cl::desc("Enable pre-regalloc stack frame index allocation")); |
Dan Gohman | 8c407d4 | 2010-04-15 17:34:58 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | using namespace llvm; |
Jim Grosbach | 18ed9c9 | 2009-10-20 20:19:50 +0000 | [diff] [blame] | 52 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 53 | unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum, |
Evan Cheng | 8295d99 | 2009-07-22 05:55:18 +0000 | [diff] [blame] | 54 | bool *isSPVFP) { |
| 55 | if (isSPVFP) |
| 56 | *isSPVFP = false; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 57 | |
| 58 | using namespace ARM; |
| 59 | switch (RegEnum) { |
| 60 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 61 | llvm_unreachable("Unknown ARM register!"); |
Evan Cheng | 8295d99 | 2009-07-22 05:55:18 +0000 | [diff] [blame] | 62 | case R0: case D0: case Q0: return 0; |
| 63 | case R1: case D1: case Q1: return 1; |
| 64 | case R2: case D2: case Q2: return 2; |
| 65 | case R3: case D3: case Q3: return 3; |
| 66 | case R4: case D4: case Q4: return 4; |
| 67 | case R5: case D5: case Q5: return 5; |
| 68 | case R6: case D6: case Q6: return 6; |
| 69 | case R7: case D7: case Q7: return 7; |
| 70 | case R8: case D8: case Q8: return 8; |
| 71 | case R9: case D9: case Q9: return 9; |
| 72 | case R10: case D10: case Q10: return 10; |
| 73 | case R11: case D11: case Q11: return 11; |
| 74 | case R12: case D12: case Q12: return 12; |
| 75 | case SP: case D13: case Q13: return 13; |
| 76 | case LR: case D14: case Q14: return 14; |
| 77 | case PC: case D15: case Q15: return 15; |
| 78 | |
| 79 | case D16: return 16; |
| 80 | case D17: return 17; |
| 81 | case D18: return 18; |
| 82 | case D19: return 19; |
| 83 | case D20: return 20; |
| 84 | case D21: return 21; |
| 85 | case D22: return 22; |
| 86 | case D23: return 23; |
| 87 | case D24: return 24; |
| 88 | case D25: return 25; |
Bob Wilson | 98330ff | 2010-03-20 06:05:13 +0000 | [diff] [blame] | 89 | case D26: return 26; |
Evan Cheng | 8295d99 | 2009-07-22 05:55:18 +0000 | [diff] [blame] | 90 | case D27: return 27; |
| 91 | case D28: return 28; |
| 92 | case D29: return 29; |
| 93 | case D30: return 30; |
| 94 | case D31: return 31; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 95 | |
| 96 | case S0: case S1: case S2: case S3: |
| 97 | case S4: case S5: case S6: case S7: |
| 98 | case S8: case S9: case S10: case S11: |
| 99 | case S12: case S13: case S14: case S15: |
| 100 | case S16: case S17: case S18: case S19: |
| 101 | case S20: case S21: case S22: case S23: |
| 102 | case S24: case S25: case S26: case S27: |
Evan Cheng | 8295d99 | 2009-07-22 05:55:18 +0000 | [diff] [blame] | 103 | case S28: case S29: case S30: case S31: { |
| 104 | if (isSPVFP) |
| 105 | *isSPVFP = true; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 106 | switch (RegEnum) { |
| 107 | default: return 0; // Avoid compile time warning. |
| 108 | case S0: return 0; |
| 109 | case S1: return 1; |
| 110 | case S2: return 2; |
| 111 | case S3: return 3; |
| 112 | case S4: return 4; |
| 113 | case S5: return 5; |
| 114 | case S6: return 6; |
| 115 | case S7: return 7; |
| 116 | case S8: return 8; |
| 117 | case S9: return 9; |
| 118 | case S10: return 10; |
| 119 | case S11: return 11; |
| 120 | case S12: return 12; |
| 121 | case S13: return 13; |
| 122 | case S14: return 14; |
| 123 | case S15: return 15; |
| 124 | case S16: return 16; |
| 125 | case S17: return 17; |
| 126 | case S18: return 18; |
| 127 | case S19: return 19; |
| 128 | case S20: return 20; |
| 129 | case S21: return 21; |
| 130 | case S22: return 22; |
| 131 | case S23: return 23; |
| 132 | case S24: return 24; |
| 133 | case S25: return 25; |
| 134 | case S26: return 26; |
| 135 | case S27: return 27; |
| 136 | case S28: return 28; |
| 137 | case S29: return 29; |
| 138 | case S30: return 30; |
| 139 | case S31: return 31; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 145 | ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii, |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 146 | const ARMSubtarget &sti) |
| 147 | : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP), |
| 148 | TII(tii), STI(sti), |
| 149 | FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) { |
| 150 | } |
| 151 | |
| 152 | const unsigned* |
| 153 | ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { |
| 154 | static const unsigned CalleeSavedRegs[] = { |
| 155 | ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8, |
| 156 | ARM::R7, ARM::R6, ARM::R5, ARM::R4, |
| 157 | |
| 158 | ARM::D15, ARM::D14, ARM::D13, ARM::D12, |
| 159 | ARM::D11, ARM::D10, ARM::D9, ARM::D8, |
| 160 | 0 |
| 161 | }; |
| 162 | |
| 163 | static const unsigned DarwinCalleeSavedRegs[] = { |
| 164 | // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved |
| 165 | // register. |
| 166 | ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4, |
| 167 | ARM::R11, ARM::R10, ARM::R8, |
| 168 | |
| 169 | ARM::D15, ARM::D14, ARM::D13, ARM::D12, |
| 170 | ARM::D11, ARM::D10, ARM::D9, ARM::D8, |
| 171 | 0 |
| 172 | }; |
| 173 | return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs; |
| 174 | } |
| 175 | |
Jim Grosbach | 9631864 | 2010-01-06 23:54:42 +0000 | [diff] [blame] | 176 | BitVector ARMBaseRegisterInfo:: |
| 177 | getReservedRegs(const MachineFunction &MF) const { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 178 | // FIXME: avoid re-calculating this everytime. |
| 179 | BitVector Reserved(getNumRegs()); |
| 180 | Reserved.set(ARM::SP); |
| 181 | Reserved.set(ARM::PC); |
Nate Begeman | d1fb583 | 2010-08-03 21:31:55 +0000 | [diff] [blame] | 182 | Reserved.set(ARM::FPSCR); |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 183 | if (hasFP(MF)) |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 184 | Reserved.set(FramePtr); |
| 185 | // Some targets reserve R9. |
| 186 | if (STI.isR9Reserved()) |
| 187 | Reserved.set(ARM::R9); |
| 188 | return Reserved; |
| 189 | } |
| 190 | |
Chris Lattner | 2cfd52c | 2009-07-29 20:31:52 +0000 | [diff] [blame] | 191 | bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF, |
| 192 | unsigned Reg) const { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 193 | switch (Reg) { |
| 194 | default: break; |
| 195 | case ARM::SP: |
| 196 | case ARM::PC: |
| 197 | return true; |
| 198 | case ARM::R7: |
| 199 | case ARM::R11: |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 200 | if (FramePtr == Reg && hasFP(MF)) |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 201 | return true; |
| 202 | break; |
| 203 | case ARM::R9: |
| 204 | return STI.isR9Reserved(); |
| 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
Chris Lattner | 2cfd52c | 2009-07-29 20:31:52 +0000 | [diff] [blame] | 210 | const TargetRegisterClass * |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 211 | ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A, |
| 212 | const TargetRegisterClass *B, |
| 213 | unsigned SubIdx) const { |
| 214 | switch (SubIdx) { |
| 215 | default: return 0; |
Jakob Stoklund Olesen | e00fa64 | 2010-05-25 00:15:15 +0000 | [diff] [blame] | 216 | case ARM::ssub_0: |
| 217 | case ARM::ssub_1: |
| 218 | case ARM::ssub_2: |
| 219 | case ARM::ssub_3: { |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 220 | // S sub-registers. |
| 221 | if (A->getSize() == 8) { |
Evan Cheng | ba90864 | 2009-11-03 05:52:54 +0000 | [diff] [blame] | 222 | if (B == &ARM::SPR_8RegClass) |
| 223 | return &ARM::DPR_8RegClass; |
| 224 | assert(B == &ARM::SPRRegClass && "Expecting SPR register class!"); |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 225 | if (A == &ARM::DPR_8RegClass) |
| 226 | return A; |
| 227 | return &ARM::DPR_VFP2RegClass; |
| 228 | } |
| 229 | |
Evan Cheng | b63387a | 2010-05-06 06:36:08 +0000 | [diff] [blame] | 230 | if (A->getSize() == 16) { |
| 231 | if (B == &ARM::SPR_8RegClass) |
| 232 | return &ARM::QPR_8RegClass; |
| 233 | return &ARM::QPR_VFP2RegClass; |
| 234 | } |
| 235 | |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 236 | if (A->getSize() == 32) { |
| 237 | if (B == &ARM::SPR_8RegClass) |
| 238 | return 0; // Do not allow coalescing! |
| 239 | return &ARM::QQPR_VFP2RegClass; |
| 240 | } |
| 241 | |
| 242 | assert(A->getSize() == 64 && "Expecting a QQQQ register class!"); |
| 243 | return 0; // Do not allow coalescing! |
Evan Cheng | b63387a | 2010-05-06 06:36:08 +0000 | [diff] [blame] | 244 | } |
Jakob Stoklund Olesen | e00fa64 | 2010-05-25 00:15:15 +0000 | [diff] [blame] | 245 | case ARM::dsub_0: |
| 246 | case ARM::dsub_1: |
| 247 | case ARM::dsub_2: |
| 248 | case ARM::dsub_3: { |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 249 | // D sub-registers. |
Evan Cheng | b63387a | 2010-05-06 06:36:08 +0000 | [diff] [blame] | 250 | if (A->getSize() == 16) { |
| 251 | if (B == &ARM::DPR_VFP2RegClass) |
| 252 | return &ARM::QPR_VFP2RegClass; |
| 253 | if (B == &ARM::DPR_8RegClass) |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 254 | return 0; // Do not allow coalescing! |
Evan Cheng | b63387a | 2010-05-06 06:36:08 +0000 | [diff] [blame] | 255 | return A; |
| 256 | } |
| 257 | |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 258 | if (A->getSize() == 32) { |
| 259 | if (B == &ARM::DPR_VFP2RegClass) |
| 260 | return &ARM::QQPR_VFP2RegClass; |
| 261 | if (B == &ARM::DPR_8RegClass) |
| 262 | return 0; // Do not allow coalescing! |
| 263 | return A; |
| 264 | } |
| 265 | |
| 266 | assert(A->getSize() == 64 && "Expecting a QQQQ register class!"); |
| 267 | if (B != &ARM::DPRRegClass) |
| 268 | return 0; // Do not allow coalescing! |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 269 | return A; |
| 270 | } |
Jakob Stoklund Olesen | e00fa64 | 2010-05-25 00:15:15 +0000 | [diff] [blame] | 271 | case ARM::dsub_4: |
| 272 | case ARM::dsub_5: |
| 273 | case ARM::dsub_6: |
| 274 | case ARM::dsub_7: { |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 275 | // D sub-registers of QQQQ registers. |
| 276 | if (A->getSize() == 64 && B == &ARM::DPRRegClass) |
| 277 | return A; |
| 278 | return 0; // Do not allow coalescing! |
| 279 | } |
| 280 | |
Jakob Stoklund Olesen | e00fa64 | 2010-05-25 00:15:15 +0000 | [diff] [blame] | 281 | case ARM::qsub_0: |
| 282 | case ARM::qsub_1: { |
Evan Cheng | b63387a | 2010-05-06 06:36:08 +0000 | [diff] [blame] | 283 | // Q sub-registers. |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 284 | if (A->getSize() == 32) { |
| 285 | if (B == &ARM::QPR_VFP2RegClass) |
| 286 | return &ARM::QQPR_VFP2RegClass; |
| 287 | if (B == &ARM::QPR_8RegClass) |
| 288 | return 0; // Do not allow coalescing! |
| 289 | return A; |
| 290 | } |
| 291 | |
| 292 | assert(A->getSize() == 64 && "Expecting a QQQQ register class!"); |
| 293 | if (B == &ARM::QPRRegClass) |
| 294 | return A; |
| 295 | return 0; // Do not allow coalescing! |
| 296 | } |
Jakob Stoklund Olesen | e00fa64 | 2010-05-25 00:15:15 +0000 | [diff] [blame] | 297 | case ARM::qsub_2: |
| 298 | case ARM::qsub_3: { |
Evan Cheng | 22c687b | 2010-05-14 02:13:41 +0000 | [diff] [blame] | 299 | // Q sub-registers of QQQQ registers. |
| 300 | if (A->getSize() == 64 && B == &ARM::QPRRegClass) |
| 301 | return A; |
| 302 | return 0; // Do not allow coalescing! |
Evan Cheng | b63387a | 2010-05-06 06:36:08 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 308 | bool |
Bob Wilson | 91a74da | 2010-06-02 18:54:47 +0000 | [diff] [blame] | 309 | ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC, |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 310 | SmallVectorImpl<unsigned> &SubIndices, |
| 311 | unsigned &NewSubIdx) const { |
| 312 | |
| 313 | unsigned Size = RC->getSize() * 8; |
| 314 | if (Size < 6) |
| 315 | return 0; |
| 316 | |
| 317 | NewSubIdx = 0; // Whole register. |
| 318 | unsigned NumRegs = SubIndices.size(); |
| 319 | if (NumRegs == 8) { |
| 320 | // 8 D registers -> 1 QQQQ register. |
| 321 | return (Size == 512 && |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 322 | SubIndices[0] == ARM::dsub_0 && |
| 323 | SubIndices[1] == ARM::dsub_1 && |
| 324 | SubIndices[2] == ARM::dsub_2 && |
| 325 | SubIndices[3] == ARM::dsub_3 && |
| 326 | SubIndices[4] == ARM::dsub_4 && |
| 327 | SubIndices[5] == ARM::dsub_5 && |
| 328 | SubIndices[6] == ARM::dsub_6 && |
| 329 | SubIndices[7] == ARM::dsub_7); |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 330 | } else if (NumRegs == 4) { |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 331 | if (SubIndices[0] == ARM::qsub_0) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 332 | // 4 Q registers -> 1 QQQQ register. |
| 333 | return (Size == 512 && |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 334 | SubIndices[1] == ARM::qsub_1 && |
| 335 | SubIndices[2] == ARM::qsub_2 && |
| 336 | SubIndices[3] == ARM::qsub_3); |
| 337 | } else if (SubIndices[0] == ARM::dsub_0) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 338 | // 4 D registers -> 1 QQ register. |
| 339 | if (Size >= 256 && |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 340 | SubIndices[1] == ARM::dsub_1 && |
| 341 | SubIndices[2] == ARM::dsub_2 && |
| 342 | SubIndices[3] == ARM::dsub_3) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 343 | if (Size == 512) |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 344 | NewSubIdx = ARM::qqsub_0; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 347 | } else if (SubIndices[0] == ARM::dsub_4) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 348 | // 4 D registers -> 1 QQ register (2nd). |
| 349 | if (Size == 512 && |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 350 | SubIndices[1] == ARM::dsub_5 && |
| 351 | SubIndices[2] == ARM::dsub_6 && |
| 352 | SubIndices[3] == ARM::dsub_7) { |
| 353 | NewSubIdx = ARM::qqsub_1; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 354 | return true; |
| 355 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 356 | } else if (SubIndices[0] == ARM::ssub_0) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 357 | // 4 S registers -> 1 Q register. |
| 358 | if (Size >= 128 && |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 359 | SubIndices[1] == ARM::ssub_1 && |
| 360 | SubIndices[2] == ARM::ssub_2 && |
| 361 | SubIndices[3] == ARM::ssub_3) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 362 | if (Size >= 256) |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 363 | NewSubIdx = ARM::qsub_0; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | } |
| 367 | } else if (NumRegs == 2) { |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 368 | if (SubIndices[0] == ARM::qsub_0) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 369 | // 2 Q registers -> 1 QQ register. |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 370 | if (Size >= 256 && SubIndices[1] == ARM::qsub_1) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 371 | if (Size == 512) |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 372 | NewSubIdx = ARM::qqsub_0; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 373 | return true; |
| 374 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 375 | } else if (SubIndices[0] == ARM::qsub_2) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 376 | // 2 Q registers -> 1 QQ register (2nd). |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 377 | if (Size == 512 && SubIndices[1] == ARM::qsub_3) { |
| 378 | NewSubIdx = ARM::qqsub_1; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 379 | return true; |
| 380 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 381 | } else if (SubIndices[0] == ARM::dsub_0) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 382 | // 2 D registers -> 1 Q register. |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 383 | if (Size >= 128 && SubIndices[1] == ARM::dsub_1) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 384 | if (Size >= 256) |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 385 | NewSubIdx = ARM::qsub_0; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 386 | return true; |
| 387 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 388 | } else if (SubIndices[0] == ARM::dsub_2) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 389 | // 2 D registers -> 1 Q register (2nd). |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 390 | if (Size >= 256 && SubIndices[1] == ARM::dsub_3) { |
| 391 | NewSubIdx = ARM::qsub_1; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 392 | return true; |
| 393 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 394 | } else if (SubIndices[0] == ARM::dsub_4) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 395 | // 2 D registers -> 1 Q register (3rd). |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 396 | if (Size == 512 && SubIndices[1] == ARM::dsub_5) { |
| 397 | NewSubIdx = ARM::qsub_2; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 398 | return true; |
| 399 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 400 | } else if (SubIndices[0] == ARM::dsub_6) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 401 | // 2 D registers -> 1 Q register (3rd). |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 402 | if (Size == 512 && SubIndices[1] == ARM::dsub_7) { |
| 403 | NewSubIdx = ARM::qsub_3; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 404 | return true; |
| 405 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 406 | } else if (SubIndices[0] == ARM::ssub_0) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 407 | // 2 S registers -> 1 D register. |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 408 | if (SubIndices[1] == ARM::ssub_1) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 409 | if (Size >= 128) |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 410 | NewSubIdx = ARM::dsub_0; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 411 | return true; |
| 412 | } |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 413 | } else if (SubIndices[0] == ARM::ssub_2) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 414 | // 2 S registers -> 1 D register (2nd). |
Jakob Stoklund Olesen | 558661d | 2010-05-24 16:54:32 +0000 | [diff] [blame] | 415 | if (Size >= 128 && SubIndices[1] == ARM::ssub_3) { |
| 416 | NewSubIdx = ARM::dsub_1; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 417 | return true; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 425 | const TargetRegisterClass * |
Chris Lattner | 2cfd52c | 2009-07-29 20:31:52 +0000 | [diff] [blame] | 426 | ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const { |
Jim Grosbach | e11a8f5 | 2009-09-11 19:49:06 +0000 | [diff] [blame] | 427 | return ARM::GPRRegisterClass; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /// getAllocationOrder - Returns the register allocation order for a specified |
| 431 | /// register class in the form of a pair of TargetRegisterClass iterators. |
| 432 | std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator> |
| 433 | ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC, |
| 434 | unsigned HintType, unsigned HintReg, |
| 435 | const MachineFunction &MF) const { |
| 436 | // Alternative register allocation orders when favoring even / odd registers |
| 437 | // of register pairs. |
| 438 | |
| 439 | // No FP, R9 is available. |
| 440 | static const unsigned GPREven1[] = { |
| 441 | ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10, |
| 442 | ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, |
| 443 | ARM::R9, ARM::R11 |
| 444 | }; |
| 445 | static const unsigned GPROdd1[] = { |
| 446 | ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11, |
| 447 | ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, |
| 448 | ARM::R8, ARM::R10 |
| 449 | }; |
| 450 | |
| 451 | // FP is R7, R9 is available. |
| 452 | static const unsigned GPREven2[] = { |
| 453 | ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10, |
| 454 | ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, |
| 455 | ARM::R9, ARM::R11 |
| 456 | }; |
| 457 | static const unsigned GPROdd2[] = { |
| 458 | ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11, |
| 459 | ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, |
| 460 | ARM::R8, ARM::R10 |
| 461 | }; |
| 462 | |
| 463 | // FP is R11, R9 is available. |
| 464 | static const unsigned GPREven3[] = { |
| 465 | ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, |
| 466 | ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, |
| 467 | ARM::R9 |
| 468 | }; |
| 469 | static const unsigned GPROdd3[] = { |
| 470 | ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9, |
| 471 | ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7, |
| 472 | ARM::R8 |
| 473 | }; |
| 474 | |
| 475 | // No FP, R9 is not available. |
| 476 | static const unsigned GPREven4[] = { |
| 477 | ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10, |
| 478 | ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8, |
| 479 | ARM::R11 |
| 480 | }; |
| 481 | static const unsigned GPROdd4[] = { |
| 482 | ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11, |
| 483 | ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8, |
| 484 | ARM::R10 |
| 485 | }; |
| 486 | |
| 487 | // FP is R7, R9 is not available. |
| 488 | static const unsigned GPREven5[] = { |
| 489 | ARM::R0, ARM::R2, ARM::R4, ARM::R10, |
| 490 | ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8, |
| 491 | ARM::R11 |
| 492 | }; |
| 493 | static const unsigned GPROdd5[] = { |
| 494 | ARM::R1, ARM::R3, ARM::R5, ARM::R11, |
| 495 | ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8, |
| 496 | ARM::R10 |
| 497 | }; |
| 498 | |
| 499 | // FP is R11, R9 is not available. |
| 500 | static const unsigned GPREven6[] = { |
| 501 | ARM::R0, ARM::R2, ARM::R4, ARM::R6, |
| 502 | ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8 |
| 503 | }; |
| 504 | static const unsigned GPROdd6[] = { |
| 505 | ARM::R1, ARM::R3, ARM::R5, ARM::R7, |
| 506 | ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8 |
| 507 | }; |
| 508 | |
| 509 | |
| 510 | if (HintType == ARMRI::RegPairEven) { |
| 511 | if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0) |
| 512 | // It's no longer possible to fulfill this hint. Return the default |
| 513 | // allocation order. |
| 514 | return std::make_pair(RC->allocation_order_begin(MF), |
| 515 | RC->allocation_order_end(MF)); |
| 516 | |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 517 | if (!hasFP(MF)) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 518 | if (!STI.isR9Reserved()) |
| 519 | return std::make_pair(GPREven1, |
| 520 | GPREven1 + (sizeof(GPREven1)/sizeof(unsigned))); |
| 521 | else |
| 522 | return std::make_pair(GPREven4, |
| 523 | GPREven4 + (sizeof(GPREven4)/sizeof(unsigned))); |
| 524 | } else if (FramePtr == ARM::R7) { |
| 525 | if (!STI.isR9Reserved()) |
| 526 | return std::make_pair(GPREven2, |
| 527 | GPREven2 + (sizeof(GPREven2)/sizeof(unsigned))); |
| 528 | else |
| 529 | return std::make_pair(GPREven5, |
| 530 | GPREven5 + (sizeof(GPREven5)/sizeof(unsigned))); |
| 531 | } else { // FramePtr == ARM::R11 |
| 532 | if (!STI.isR9Reserved()) |
| 533 | return std::make_pair(GPREven3, |
| 534 | GPREven3 + (sizeof(GPREven3)/sizeof(unsigned))); |
| 535 | else |
| 536 | return std::make_pair(GPREven6, |
| 537 | GPREven6 + (sizeof(GPREven6)/sizeof(unsigned))); |
| 538 | } |
| 539 | } else if (HintType == ARMRI::RegPairOdd) { |
| 540 | if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0) |
| 541 | // It's no longer possible to fulfill this hint. Return the default |
| 542 | // allocation order. |
| 543 | return std::make_pair(RC->allocation_order_begin(MF), |
| 544 | RC->allocation_order_end(MF)); |
| 545 | |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 546 | if (!hasFP(MF)) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 547 | if (!STI.isR9Reserved()) |
| 548 | return std::make_pair(GPROdd1, |
| 549 | GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned))); |
| 550 | else |
| 551 | return std::make_pair(GPROdd4, |
| 552 | GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned))); |
| 553 | } else if (FramePtr == ARM::R7) { |
| 554 | if (!STI.isR9Reserved()) |
| 555 | return std::make_pair(GPROdd2, |
| 556 | GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned))); |
| 557 | else |
| 558 | return std::make_pair(GPROdd5, |
| 559 | GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned))); |
| 560 | } else { // FramePtr == ARM::R11 |
| 561 | if (!STI.isR9Reserved()) |
| 562 | return std::make_pair(GPROdd3, |
| 563 | GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned))); |
| 564 | else |
| 565 | return std::make_pair(GPROdd6, |
| 566 | GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned))); |
| 567 | } |
| 568 | } |
| 569 | return std::make_pair(RC->allocation_order_begin(MF), |
| 570 | RC->allocation_order_end(MF)); |
| 571 | } |
| 572 | |
| 573 | /// ResolveRegAllocHint - Resolves the specified register allocation hint |
| 574 | /// to a physical register. Returns the physical register if it is successful. |
| 575 | unsigned |
| 576 | ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg, |
| 577 | const MachineFunction &MF) const { |
| 578 | if (Reg == 0 || !isPhysicalRegister(Reg)) |
| 579 | return 0; |
| 580 | if (Type == 0) |
| 581 | return Reg; |
| 582 | else if (Type == (unsigned)ARMRI::RegPairOdd) |
| 583 | // Odd register. |
| 584 | return getRegisterPairOdd(Reg, MF); |
| 585 | else if (Type == (unsigned)ARMRI::RegPairEven) |
| 586 | // Even register. |
| 587 | return getRegisterPairEven(Reg, MF); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg, |
| 593 | MachineFunction &MF) const { |
| 594 | MachineRegisterInfo *MRI = &MF.getRegInfo(); |
| 595 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg); |
| 596 | if ((Hint.first == (unsigned)ARMRI::RegPairOdd || |
| 597 | Hint.first == (unsigned)ARMRI::RegPairEven) && |
| 598 | Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) { |
| 599 | // If 'Reg' is one of the even / odd register pair and it's now changed |
| 600 | // (e.g. coalesced) into a different register. The other register of the |
| 601 | // pair allocation hint must be updated to reflect the relationship |
| 602 | // change. |
| 603 | unsigned OtherReg = Hint.second; |
| 604 | Hint = MRI->getRegAllocationHint(OtherReg); |
| 605 | if (Hint.second == Reg) |
| 606 | // Make sure the pair has not already divorced. |
| 607 | MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /// hasFP - Return true if the specified function should have a dedicated frame |
| 612 | /// pointer register. This is true if the function has variable sized allocas |
| 613 | /// or if frame pointer elimination is disabled. |
| 614 | /// |
| 615 | bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const { |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 616 | // Mac OS X requires FP not to be clobbered for backtracing purpose. |
| 617 | if (STI.isTargetDarwin()) |
| 618 | return true; |
| 619 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 620 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | b000d68 | 2010-08-09 22:32:45 +0000 | [diff] [blame] | 621 | // Always eliminate non-leaf frame pointers. |
| 622 | return ((DisableFramePointerElim(MF) && MFI->hasCalls()) || |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 623 | needsStackRealignment(MF) || |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 624 | MFI->hasVarSizedObjects() || |
| 625 | MFI->isFrameAddressTaken()); |
| 626 | } |
| 627 | |
Jim Grosbach | e45ab8a | 2010-01-19 18:31:11 +0000 | [diff] [blame] | 628 | bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const { |
| 629 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 630 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 631 | return (RealignStack && |
| 632 | !AFI->isThumb1OnlyFunction() && |
| 633 | !MFI->hasVarSizedObjects()); |
| 634 | } |
| 635 | |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 636 | bool ARMBaseRegisterInfo:: |
| 637 | needsStackRealignment(const MachineFunction &MF) const { |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 638 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Eric Christopher | d4c36ce | 2010-07-17 00:27:24 +0000 | [diff] [blame] | 639 | const Function *F = MF.getFunction(); |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 640 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jim Grosbach | ad353c7 | 2009-11-09 22:32:03 +0000 | [diff] [blame] | 641 | unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
Eric Christopher | 697cba8 | 2010-07-17 00:33:04 +0000 | [diff] [blame] | 642 | bool requiresRealignment = ((MFI->getMaxAlignment() > StackAlign) || |
| 643 | F->hasFnAttr(Attribute::StackAlignment)); |
Eric Christopher | d4c36ce | 2010-07-17 00:27:24 +0000 | [diff] [blame] | 644 | |
| 645 | // FIXME: Currently we don't support stack realignment for functions with |
| 646 | // variable-sized allocas. |
| 647 | // FIXME: It's more complicated than this... |
| 648 | if (0 && requiresRealignment && MFI->hasVarSizedObjects()) |
| 649 | report_fatal_error( |
| 650 | "Stack realignment in presense of dynamic allocas is not supported"); |
| 651 | |
| 652 | // FIXME: This probably isn't the right place for this. |
| 653 | if (0 && requiresRealignment && AFI->isThumb1OnlyFunction()) |
| 654 | report_fatal_error( |
| 655 | "Stack realignment in thumb1 functions is not supported"); |
| 656 | |
| 657 | return requiresRealignment && canRealignStack(MF); |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Jim Grosbach | 9631864 | 2010-01-06 23:54:42 +0000 | [diff] [blame] | 660 | bool ARMBaseRegisterInfo:: |
| 661 | cannotEliminateFrame(const MachineFunction &MF) const { |
Evan Cheng | 98a0104 | 2009-08-14 20:48:13 +0000 | [diff] [blame] | 662 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bill Wendling | b92187a | 2010-05-14 21:14:32 +0000 | [diff] [blame] | 663 | if (DisableFramePointerElim(MF) && MFI->adjustsStack()) |
Evan Cheng | 98a0104 | 2009-08-14 20:48:13 +0000 | [diff] [blame] | 664 | return true; |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 665 | return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() |
| 666 | || needsStackRealignment(MF); |
Evan Cheng | 98a0104 | 2009-08-14 20:48:13 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Evan Cheng | 542383d | 2009-07-28 06:24:12 +0000 | [diff] [blame] | 669 | /// estimateStackSize - Estimate and return the size of the frame. |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 670 | static unsigned estimateStackSize(MachineFunction &MF) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 671 | const MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 672 | int Offset = 0; |
| 673 | for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) { |
| 674 | int FixedOff = -FFI->getObjectOffset(i); |
| 675 | if (FixedOff > Offset) Offset = FixedOff; |
| 676 | } |
| 677 | for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) { |
| 678 | if (FFI->isDeadObjectIndex(i)) |
| 679 | continue; |
| 680 | Offset += FFI->getObjectSize(i); |
| 681 | unsigned Align = FFI->getObjectAlignment(i); |
| 682 | // Adjust to alignment boundary |
| 683 | Offset = (Offset+Align-1)/Align*Align; |
| 684 | } |
| 685 | return (unsigned)Offset; |
| 686 | } |
| 687 | |
Evan Cheng | 542383d | 2009-07-28 06:24:12 +0000 | [diff] [blame] | 688 | /// estimateRSStackSizeLimit - Look at each instruction that references stack |
| 689 | /// frames and return the stack size limit beyond which some of these |
Jim Grosbach | ce3e769 | 2010-01-06 23:45:18 +0000 | [diff] [blame] | 690 | /// instructions will require a scratch register during their expansion later. |
Evan Cheng | ee42fd3 | 2009-07-30 23:29:25 +0000 | [diff] [blame] | 691 | unsigned |
| 692 | ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const { |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 693 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 542383d | 2009-07-28 06:24:12 +0000 | [diff] [blame] | 694 | unsigned Limit = (1 << 12) - 1; |
Chris Lattner | b180d99 | 2009-07-28 18:48:43 +0000 | [diff] [blame] | 695 | for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { |
| 696 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); |
| 697 | I != E; ++I) { |
| 698 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 699 | if (!I->getOperand(i).isFI()) continue; |
Jakob Stoklund Olesen | 52c61ec | 2010-06-18 20:59:25 +0000 | [diff] [blame] | 700 | |
| 701 | // When using ADDri to get the address of a stack object, 255 is the |
| 702 | // largest offset guaranteed to fit in the immediate offset. |
| 703 | if (I->getOpcode() == ARM::ADDri) { |
| 704 | Limit = std::min(Limit, (1U << 8) - 1); |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | // Otherwise check the addressing mode. |
Jakob Stoklund Olesen | 535af4a | 2010-05-17 23:29:23 +0000 | [diff] [blame] | 709 | switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { |
| 710 | case ARMII::AddrMode3: |
| 711 | case ARMII::AddrModeT2_i8: |
| 712 | Limit = std::min(Limit, (1U << 8) - 1); |
| 713 | break; |
| 714 | case ARMII::AddrMode5: |
| 715 | case ARMII::AddrModeT2_i8s4: |
Chris Lattner | b180d99 | 2009-07-28 18:48:43 +0000 | [diff] [blame] | 716 | Limit = std::min(Limit, ((1U << 8) - 1) * 4); |
Jakob Stoklund Olesen | 535af4a | 2010-05-17 23:29:23 +0000 | [diff] [blame] | 717 | break; |
| 718 | case ARMII::AddrModeT2_i12: |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 719 | // i12 supports only positive offset so these will be converted to |
| 720 | // i8 opcodes. See llvm::rewriteT2FrameIndex. |
| 721 | if (hasFP(MF) && AFI->hasStackFrame()) |
| 722 | Limit = std::min(Limit, (1U << 8) - 1); |
Jakob Stoklund Olesen | 535af4a | 2010-05-17 23:29:23 +0000 | [diff] [blame] | 723 | break; |
| 724 | case ARMII::AddrMode6: |
| 725 | // Addressing mode 6 (load/store) instructions can't encode an |
| 726 | // immediate offset for stack references. |
Jim Grosbach | ce3e769 | 2010-01-06 23:45:18 +0000 | [diff] [blame] | 727 | return 0; |
Jakob Stoklund Olesen | 535af4a | 2010-05-17 23:29:23 +0000 | [diff] [blame] | 728 | default: |
| 729 | break; |
| 730 | } |
Chris Lattner | b180d99 | 2009-07-28 18:48:43 +0000 | [diff] [blame] | 731 | break; // At most one FI per instruction |
| 732 | } |
Evan Cheng | 542383d | 2009-07-28 06:24:12 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | return Limit; |
| 737 | } |
| 738 | |
Chris Lattner | 1c55386 | 2010-07-22 21:14:33 +0000 | [diff] [blame] | 739 | static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, |
| 740 | const ARMBaseInstrInfo &TII) { |
| 741 | unsigned FnSize = 0; |
| 742 | for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); |
| 743 | MBBI != E; ++MBBI) { |
| 744 | const MachineBasicBlock &MBB = *MBBI; |
| 745 | for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); |
| 746 | I != E; ++I) |
| 747 | FnSize += TII.GetInstSizeInBytes(I); |
| 748 | } |
| 749 | return FnSize; |
| 750 | } |
| 751 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 752 | void |
| 753 | ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
Jim Grosbach | 9631864 | 2010-01-06 23:54:42 +0000 | [diff] [blame] | 754 | RegScavenger *RS) const { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 755 | // This tells PEI to spill the FP as if it is any other callee-save register |
| 756 | // to take advantage the eliminateFrameIndex machinery. This also ensures it |
| 757 | // is spilled in the order specified by getCalleeSavedRegs() to make it easier |
| 758 | // to combine multiple loads / stores. |
| 759 | bool CanEliminateFrame = true; |
| 760 | bool CS1Spilled = false; |
| 761 | bool LRSpilled = false; |
| 762 | unsigned NumGPRSpills = 0; |
| 763 | SmallVector<unsigned, 4> UnspilledCS1GPRs; |
| 764 | SmallVector<unsigned, 4> UnspilledCS2GPRs; |
| 765 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jim Grosbach | 6c7d3a1 | 2010-07-09 20:27:06 +0000 | [diff] [blame] | 766 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 767 | |
Anton Korobeynikov | 7cca606 | 2009-12-06 22:39:50 +0000 | [diff] [blame] | 768 | // Spill R4 if Thumb2 function requires stack realignment - it will be used as |
| 769 | // scratch register. |
| 770 | // FIXME: It will be better just to find spare register here. |
| 771 | if (needsStackRealignment(MF) && |
| 772 | AFI->isThumb2Function()) |
| 773 | MF.getRegInfo().setPhysRegUsed(ARM::R4); |
| 774 | |
Jim Grosbach | f49be7c | 2010-03-10 20:01:30 +0000 | [diff] [blame] | 775 | // Spill LR if Thumb1 function uses variable length argument lists. |
| 776 | if (AFI->isThumb1OnlyFunction() && AFI->getVarArgsRegSaveSize() > 0) |
| 777 | MF.getRegInfo().setPhysRegUsed(ARM::LR); |
| 778 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 779 | // Don't spill FP if the frame can be eliminated. This is determined |
| 780 | // by scanning the callee-save registers to see if any is used. |
| 781 | const unsigned *CSRegs = getCalleeSavedRegs(); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 782 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 783 | unsigned Reg = CSRegs[i]; |
| 784 | bool Spilled = false; |
| 785 | if (MF.getRegInfo().isPhysRegUsed(Reg)) { |
| 786 | AFI->setCSRegisterIsSpilled(Reg); |
| 787 | Spilled = true; |
| 788 | CanEliminateFrame = false; |
| 789 | } else { |
| 790 | // Check alias registers too. |
| 791 | for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) { |
| 792 | if (MF.getRegInfo().isPhysRegUsed(*Aliases)) { |
| 793 | Spilled = true; |
| 794 | CanEliminateFrame = false; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
Rafael Espindola | 20fae65 | 2010-06-02 17:54:50 +0000 | [diff] [blame] | 799 | if (!ARM::GPRRegisterClass->contains(Reg)) |
| 800 | continue; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 801 | |
Rafael Espindola | 20fae65 | 2010-06-02 17:54:50 +0000 | [diff] [blame] | 802 | if (Spilled) { |
| 803 | NumGPRSpills++; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 804 | |
Rafael Espindola | 20fae65 | 2010-06-02 17:54:50 +0000 | [diff] [blame] | 805 | if (!STI.isTargetDarwin()) { |
| 806 | if (Reg == ARM::LR) |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 807 | LRSpilled = true; |
Rafael Espindola | 20fae65 | 2010-06-02 17:54:50 +0000 | [diff] [blame] | 808 | CS1Spilled = true; |
| 809 | continue; |
| 810 | } |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 811 | |
Rafael Espindola | 20fae65 | 2010-06-02 17:54:50 +0000 | [diff] [blame] | 812 | // Keep track if LR and any of R4, R5, R6, and R7 is spilled. |
| 813 | switch (Reg) { |
| 814 | case ARM::LR: |
| 815 | LRSpilled = true; |
| 816 | // Fallthrough |
| 817 | case ARM::R4: |
| 818 | case ARM::R5: |
| 819 | case ARM::R6: |
| 820 | case ARM::R7: |
| 821 | CS1Spilled = true; |
| 822 | break; |
| 823 | default: |
| 824 | break; |
| 825 | } |
| 826 | } else { |
| 827 | if (!STI.isTargetDarwin()) { |
| 828 | UnspilledCS1GPRs.push_back(Reg); |
| 829 | continue; |
| 830 | } |
| 831 | |
| 832 | switch (Reg) { |
| 833 | case ARM::R4: |
| 834 | case ARM::R5: |
| 835 | case ARM::R6: |
| 836 | case ARM::R7: |
| 837 | case ARM::LR: |
| 838 | UnspilledCS1GPRs.push_back(Reg); |
| 839 | break; |
| 840 | default: |
| 841 | UnspilledCS2GPRs.push_back(Reg); |
| 842 | break; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | bool ForceLRSpill = false; |
David Goodwin | f1daf7d | 2009-07-08 23:10:31 +0000 | [diff] [blame] | 848 | if (!LRSpilled && AFI->isThumb1OnlyFunction()) { |
Chris Lattner | 1c55386 | 2010-07-22 21:14:33 +0000 | [diff] [blame] | 849 | unsigned FnSize = GetFunctionSizeInBytes(MF, TII); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 850 | // Force LR to be spilled if the Thumb function size is > 2048. This enables |
| 851 | // use of BL to implement far jump. If it turns out that it's not needed |
| 852 | // then the branch fix up path will undo it. |
| 853 | if (FnSize >= (1 << 11)) { |
| 854 | CanEliminateFrame = false; |
| 855 | ForceLRSpill = true; |
| 856 | } |
| 857 | } |
| 858 | |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 859 | // If any of the stack slot references may be out of range of an immediate |
| 860 | // offset, make sure a register (or a spill slot) is available for the |
| 861 | // register scavenger. Note that if we're indexing off the frame pointer, the |
| 862 | // effective stack size is 4 bytes larger since the FP points to the stack |
Jim Grosbach | 6c7d3a1 | 2010-07-09 20:27:06 +0000 | [diff] [blame] | 863 | // slot of the previous FP. Also, if we have variable sized objects in the |
| 864 | // function, stack slot references will often be negative, and some of |
| 865 | // our instructions are positive-offset only, so conservatively consider |
Jim Grosbach | abf7bdf | 2010-08-04 22:46:09 +0000 | [diff] [blame] | 866 | // that case to want a spill slot (or register) as well. Similarly, if |
| 867 | // the function adjusts the stack pointer during execution and the |
| 868 | // adjustments aren't already part of our stack size estimate, our offset |
| 869 | // calculations may be off, so be conservative. |
Jim Grosbach | 6c7d3a1 | 2010-07-09 20:27:06 +0000 | [diff] [blame] | 870 | // FIXME: We could add logic to be more precise about negative offsets |
| 871 | // and which instructions will need a scratch register for them. Is it |
| 872 | // worth the effort and added fragility? |
Chandler Carruth | 68eec39 | 2010-07-10 12:06:22 +0000 | [diff] [blame] | 873 | bool BigStack = |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 874 | (RS && |
| 875 | (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= |
| 876 | estimateRSStackSizeLimit(MF))) |
Chandler Carruth | a7da3ac | 2010-08-05 03:04:21 +0000 | [diff] [blame] | 877 | || MFI->hasVarSizedObjects() |
| 878 | || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 879 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 880 | bool ExtraCSSpill = false; |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 881 | if (BigStack || !CanEliminateFrame || cannotEliminateFrame(MF)) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 882 | AFI->setHasStackFrame(true); |
| 883 | |
| 884 | // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. |
| 885 | // Spill LR as well so we can fold BX_RET to the registers restore (LDM). |
| 886 | if (!LRSpilled && CS1Spilled) { |
| 887 | MF.getRegInfo().setPhysRegUsed(ARM::LR); |
| 888 | AFI->setCSRegisterIsSpilled(ARM::LR); |
| 889 | NumGPRSpills++; |
| 890 | UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(), |
| 891 | UnspilledCS1GPRs.end(), (unsigned)ARM::LR)); |
| 892 | ForceLRSpill = false; |
| 893 | ExtraCSSpill = true; |
| 894 | } |
| 895 | |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 896 | if (hasFP(MF)) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 897 | MF.getRegInfo().setPhysRegUsed(FramePtr); |
| 898 | NumGPRSpills++; |
| 899 | } |
| 900 | |
| 901 | // If stack and double are 8-byte aligned and we are spilling an odd number |
| 902 | // of GPRs. Spill one extra callee save GPR so we won't have to pad between |
| 903 | // the integer and double callee save areas. |
| 904 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 905 | if (TargetAlign == 8 && (NumGPRSpills & 1)) { |
| 906 | if (CS1Spilled && !UnspilledCS1GPRs.empty()) { |
| 907 | for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { |
| 908 | unsigned Reg = UnspilledCS1GPRs[i]; |
David Goodwin | f1daf7d | 2009-07-08 23:10:31 +0000 | [diff] [blame] | 909 | // Don't spill high register if the function is thumb1 |
| 910 | if (!AFI->isThumb1OnlyFunction() || |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 911 | isARMLowRegister(Reg) || Reg == ARM::LR) { |
| 912 | MF.getRegInfo().setPhysRegUsed(Reg); |
| 913 | AFI->setCSRegisterIsSpilled(Reg); |
| 914 | if (!isReservedReg(MF, Reg)) |
| 915 | ExtraCSSpill = true; |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | } else if (!UnspilledCS2GPRs.empty() && |
David Goodwin | f1daf7d | 2009-07-08 23:10:31 +0000 | [diff] [blame] | 920 | !AFI->isThumb1OnlyFunction()) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 921 | unsigned Reg = UnspilledCS2GPRs.front(); |
| 922 | MF.getRegInfo().setPhysRegUsed(Reg); |
| 923 | AFI->setCSRegisterIsSpilled(Reg); |
| 924 | if (!isReservedReg(MF, Reg)) |
| 925 | ExtraCSSpill = true; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | // Estimate if we might need to scavenge a register at some point in order |
| 930 | // to materialize a stack offset. If so, either spill one additional |
| 931 | // callee-saved register or reserve a special spill slot to facilitate |
Jim Grosbach | 3d6cb88 | 2009-09-24 23:52:18 +0000 | [diff] [blame] | 932 | // register scavenging. Thumb1 needs a spill slot for stack pointer |
| 933 | // adjustments also, even when the frame itself is small. |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 934 | if (BigStack && !ExtraCSSpill) { |
| 935 | // If any non-reserved CS register isn't spilled, just spill one or two |
| 936 | // extra. That should take care of it! |
| 937 | unsigned NumExtras = TargetAlign / 4; |
| 938 | SmallVector<unsigned, 2> Extras; |
| 939 | while (NumExtras && !UnspilledCS1GPRs.empty()) { |
| 940 | unsigned Reg = UnspilledCS1GPRs.back(); |
| 941 | UnspilledCS1GPRs.pop_back(); |
Bob Wilson | 1190c14 | 2010-05-13 19:58:24 +0000 | [diff] [blame] | 942 | if (!isReservedReg(MF, Reg) && |
| 943 | (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || |
| 944 | Reg == ARM::LR)) { |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 945 | Extras.push_back(Reg); |
| 946 | NumExtras--; |
| 947 | } |
| 948 | } |
| 949 | // For non-Thumb1 functions, also check for hi-reg CS registers |
| 950 | if (!AFI->isThumb1OnlyFunction()) { |
| 951 | while (NumExtras && !UnspilledCS2GPRs.empty()) { |
| 952 | unsigned Reg = UnspilledCS2GPRs.back(); |
| 953 | UnspilledCS2GPRs.pop_back(); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 954 | if (!isReservedReg(MF, Reg)) { |
| 955 | Extras.push_back(Reg); |
| 956 | NumExtras--; |
| 957 | } |
| 958 | } |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 959 | } |
| 960 | if (Extras.size() && NumExtras == 0) { |
| 961 | for (unsigned i = 0, e = Extras.size(); i != e; ++i) { |
| 962 | MF.getRegInfo().setPhysRegUsed(Extras[i]); |
| 963 | AFI->setCSRegisterIsSpilled(Extras[i]); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 964 | } |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 965 | } else if (!AFI->isThumb1OnlyFunction()) { |
| 966 | // note: Thumb1 functions spill to R12, not the stack. Reserve a slot |
| 967 | // closest to SP or frame pointer. |
| 968 | const TargetRegisterClass *RC = ARM::GPRRegisterClass; |
Jakob Stoklund Olesen | 657baec | 2010-02-24 22:43:17 +0000 | [diff] [blame] | 969 | RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), |
| 970 | RC->getAlignment(), |
| 971 | false)); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | if (ForceLRSpill) { |
| 977 | MF.getRegInfo().setPhysRegUsed(ARM::LR); |
| 978 | AFI->setCSRegisterIsSpilled(ARM::LR); |
| 979 | AFI->setLRIsSpilledForFarJump(true); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | unsigned ARMBaseRegisterInfo::getRARegister() const { |
| 984 | return ARM::LR; |
| 985 | } |
| 986 | |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 987 | unsigned |
| 988 | ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const { |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 989 | if (hasFP(MF)) |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 990 | return FramePtr; |
| 991 | return ARM::SP; |
| 992 | } |
| 993 | |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 994 | // Provide a base+offset reference to an FI slot for debug info. It's the |
| 995 | // same as what we use for resolving the code-gen references for now. |
| 996 | // FIXME: This can go wrong when references are SP-relative and simple call |
| 997 | // frames aren't used. |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 998 | int |
Chris Lattner | 30c6b75 | 2010-01-26 23:15:09 +0000 | [diff] [blame] | 999 | ARMBaseRegisterInfo::getFrameIndexReference(const MachineFunction &MF, int FI, |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1000 | unsigned &FrameReg) const { |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1001 | return ResolveFrameIndexReference(MF, FI, FrameReg, 0); |
| 1002 | } |
| 1003 | |
| 1004 | int |
| 1005 | ARMBaseRegisterInfo::ResolveFrameIndexReference(const MachineFunction &MF, |
| 1006 | int FI, |
| 1007 | unsigned &FrameReg, |
| 1008 | int SPAdj) const { |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1009 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Chris Lattner | 30c6b75 | 2010-01-26 23:15:09 +0000 | [diff] [blame] | 1010 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1011 | int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1012 | int FPOffset = Offset - AFI->getFramePtrSpillOffset(); |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1013 | bool isFixed = MFI->isFixedObjectIndex(FI); |
| 1014 | |
Jim Grosbach | a37aa54 | 2009-11-22 20:05:32 +0000 | [diff] [blame] | 1015 | FrameReg = ARM::SP; |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1016 | Offset += SPAdj; |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1017 | if (AFI->isGPRCalleeSavedArea1Frame(FI)) |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1018 | return Offset - AFI->getGPRCalleeSavedArea1Offset(); |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1019 | else if (AFI->isGPRCalleeSavedArea2Frame(FI)) |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1020 | return Offset - AFI->getGPRCalleeSavedArea2Offset(); |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1021 | else if (AFI->isDPRCalleeSavedAreaFrame(FI)) |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1022 | return Offset - AFI->getDPRCalleeSavedAreaOffset(); |
| 1023 | |
| 1024 | // When dynamically realigning the stack, use the frame pointer for |
| 1025 | // parameters, and the stack pointer for locals. |
| 1026 | if (needsStackRealignment(MF)) { |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1027 | assert (hasFP(MF) && "dynamic stack realignment without a FP!"); |
| 1028 | if (isFixed) { |
| 1029 | FrameReg = getFrameRegister(MF); |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1030 | Offset = FPOffset; |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1031 | } |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1032 | return Offset; |
| 1033 | } |
| 1034 | |
| 1035 | // If there is a frame pointer, use it when we can. |
| 1036 | if (hasFP(MF) && AFI->hasStackFrame()) { |
| 1037 | // Use frame pointer to reference fixed objects. Use it for locals if |
| 1038 | // there are VLAs (and thus the SP isn't reliable as a base). |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1039 | if (isFixed || MFI->hasVarSizedObjects()) { |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1040 | FrameReg = getFrameRegister(MF); |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1041 | Offset = FPOffset; |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1042 | } else if (AFI->isThumb2Function()) { |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1043 | // In Thumb2 mode, the negative offset is very limited. Try to avoid |
| 1044 | // out of range references. |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1045 | if (FPOffset >= -255 && FPOffset < 0) { |
| 1046 | FrameReg = getFrameRegister(MF); |
| 1047 | Offset = FPOffset; |
| 1048 | } |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1049 | } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { |
| 1050 | // Otherwise, use SP or FP, whichever is closer to the stack slot. |
| 1051 | FrameReg = getFrameRegister(MF); |
| 1052 | Offset = FPOffset; |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | return Offset; |
| 1056 | } |
| 1057 | |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1058 | int |
Chris Lattner | 30c6b75 | 2010-01-26 23:15:09 +0000 | [diff] [blame] | 1059 | ARMBaseRegisterInfo::getFrameIndexOffset(const MachineFunction &MF, |
| 1060 | int FI) const { |
Jim Grosbach | 50f8516 | 2009-11-22 02:32:29 +0000 | [diff] [blame] | 1061 | unsigned FrameReg; |
| 1062 | return getFrameIndexReference(MF, FI, FrameReg); |
| 1063 | } |
| 1064 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1065 | unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1066 | llvm_unreachable("What is the exception register"); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1071 | llvm_unreachable("What is the exception handler register"); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const { |
| 1076 | return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0); |
| 1077 | } |
| 1078 | |
| 1079 | unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg, |
Jim Grosbach | 9631864 | 2010-01-06 23:54:42 +0000 | [diff] [blame] | 1080 | const MachineFunction &MF) const { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1081 | switch (Reg) { |
| 1082 | default: break; |
| 1083 | // Return 0 if either register of the pair is a special register. |
| 1084 | // So no R12, etc. |
| 1085 | case ARM::R1: |
| 1086 | return ARM::R0; |
| 1087 | case ARM::R3: |
Jim Grosbach | 6009751 | 2009-10-19 22:57:03 +0000 | [diff] [blame] | 1088 | return ARM::R2; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1089 | case ARM::R5: |
| 1090 | return ARM::R4; |
| 1091 | case ARM::R7: |
| 1092 | return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6; |
| 1093 | case ARM::R9: |
| 1094 | return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8; |
| 1095 | case ARM::R11: |
| 1096 | return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10; |
| 1097 | |
| 1098 | case ARM::S1: |
| 1099 | return ARM::S0; |
| 1100 | case ARM::S3: |
| 1101 | return ARM::S2; |
| 1102 | case ARM::S5: |
| 1103 | return ARM::S4; |
| 1104 | case ARM::S7: |
| 1105 | return ARM::S6; |
| 1106 | case ARM::S9: |
| 1107 | return ARM::S8; |
| 1108 | case ARM::S11: |
| 1109 | return ARM::S10; |
| 1110 | case ARM::S13: |
| 1111 | return ARM::S12; |
| 1112 | case ARM::S15: |
| 1113 | return ARM::S14; |
| 1114 | case ARM::S17: |
| 1115 | return ARM::S16; |
| 1116 | case ARM::S19: |
| 1117 | return ARM::S18; |
| 1118 | case ARM::S21: |
| 1119 | return ARM::S20; |
| 1120 | case ARM::S23: |
| 1121 | return ARM::S22; |
| 1122 | case ARM::S25: |
| 1123 | return ARM::S24; |
| 1124 | case ARM::S27: |
| 1125 | return ARM::S26; |
| 1126 | case ARM::S29: |
| 1127 | return ARM::S28; |
| 1128 | case ARM::S31: |
| 1129 | return ARM::S30; |
| 1130 | |
| 1131 | case ARM::D1: |
| 1132 | return ARM::D0; |
| 1133 | case ARM::D3: |
| 1134 | return ARM::D2; |
| 1135 | case ARM::D5: |
| 1136 | return ARM::D4; |
| 1137 | case ARM::D7: |
| 1138 | return ARM::D6; |
| 1139 | case ARM::D9: |
| 1140 | return ARM::D8; |
| 1141 | case ARM::D11: |
| 1142 | return ARM::D10; |
| 1143 | case ARM::D13: |
| 1144 | return ARM::D12; |
| 1145 | case ARM::D15: |
| 1146 | return ARM::D14; |
Evan Cheng | 8295d99 | 2009-07-22 05:55:18 +0000 | [diff] [blame] | 1147 | case ARM::D17: |
| 1148 | return ARM::D16; |
| 1149 | case ARM::D19: |
| 1150 | return ARM::D18; |
| 1151 | case ARM::D21: |
| 1152 | return ARM::D20; |
| 1153 | case ARM::D23: |
| 1154 | return ARM::D22; |
| 1155 | case ARM::D25: |
| 1156 | return ARM::D24; |
| 1157 | case ARM::D27: |
| 1158 | return ARM::D26; |
| 1159 | case ARM::D29: |
| 1160 | return ARM::D28; |
| 1161 | case ARM::D31: |
| 1162 | return ARM::D30; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg, |
| 1169 | const MachineFunction &MF) const { |
| 1170 | switch (Reg) { |
| 1171 | default: break; |
| 1172 | // Return 0 if either register of the pair is a special register. |
| 1173 | // So no R12, etc. |
| 1174 | case ARM::R0: |
| 1175 | return ARM::R1; |
| 1176 | case ARM::R2: |
Jim Grosbach | 6009751 | 2009-10-19 22:57:03 +0000 | [diff] [blame] | 1177 | return ARM::R3; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1178 | case ARM::R4: |
| 1179 | return ARM::R5; |
| 1180 | case ARM::R6: |
| 1181 | return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7; |
| 1182 | case ARM::R8: |
| 1183 | return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9; |
| 1184 | case ARM::R10: |
| 1185 | return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11; |
| 1186 | |
| 1187 | case ARM::S0: |
| 1188 | return ARM::S1; |
| 1189 | case ARM::S2: |
| 1190 | return ARM::S3; |
| 1191 | case ARM::S4: |
| 1192 | return ARM::S5; |
| 1193 | case ARM::S6: |
| 1194 | return ARM::S7; |
| 1195 | case ARM::S8: |
| 1196 | return ARM::S9; |
| 1197 | case ARM::S10: |
| 1198 | return ARM::S11; |
| 1199 | case ARM::S12: |
| 1200 | return ARM::S13; |
| 1201 | case ARM::S14: |
| 1202 | return ARM::S15; |
| 1203 | case ARM::S16: |
| 1204 | return ARM::S17; |
| 1205 | case ARM::S18: |
| 1206 | return ARM::S19; |
| 1207 | case ARM::S20: |
| 1208 | return ARM::S21; |
| 1209 | case ARM::S22: |
| 1210 | return ARM::S23; |
| 1211 | case ARM::S24: |
| 1212 | return ARM::S25; |
| 1213 | case ARM::S26: |
| 1214 | return ARM::S27; |
| 1215 | case ARM::S28: |
| 1216 | return ARM::S29; |
| 1217 | case ARM::S30: |
| 1218 | return ARM::S31; |
| 1219 | |
| 1220 | case ARM::D0: |
| 1221 | return ARM::D1; |
| 1222 | case ARM::D2: |
| 1223 | return ARM::D3; |
| 1224 | case ARM::D4: |
| 1225 | return ARM::D5; |
| 1226 | case ARM::D6: |
| 1227 | return ARM::D7; |
| 1228 | case ARM::D8: |
| 1229 | return ARM::D9; |
| 1230 | case ARM::D10: |
| 1231 | return ARM::D11; |
| 1232 | case ARM::D12: |
| 1233 | return ARM::D13; |
| 1234 | case ARM::D14: |
| 1235 | return ARM::D15; |
Evan Cheng | 8295d99 | 2009-07-22 05:55:18 +0000 | [diff] [blame] | 1236 | case ARM::D16: |
| 1237 | return ARM::D17; |
| 1238 | case ARM::D18: |
| 1239 | return ARM::D19; |
| 1240 | case ARM::D20: |
| 1241 | return ARM::D21; |
| 1242 | case ARM::D22: |
| 1243 | return ARM::D23; |
| 1244 | case ARM::D24: |
| 1245 | return ARM::D25; |
| 1246 | case ARM::D26: |
| 1247 | return ARM::D27; |
| 1248 | case ARM::D28: |
| 1249 | return ARM::D29; |
| 1250 | case ARM::D30: |
| 1251 | return ARM::D31; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1257 | /// emitLoadConstPool - Emits a load from constpool to materialize the |
| 1258 | /// specified immediate. |
| 1259 | void ARMBaseRegisterInfo:: |
| 1260 | emitLoadConstPool(MachineBasicBlock &MBB, |
| 1261 | MachineBasicBlock::iterator &MBBI, |
David Goodwin | 77521f5 | 2009-07-08 20:28:28 +0000 | [diff] [blame] | 1262 | DebugLoc dl, |
Evan Cheng | 3784453 | 2009-07-16 09:20:10 +0000 | [diff] [blame] | 1263 | unsigned DestReg, unsigned SubIdx, int Val, |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1264 | ARMCC::CondCodes Pred, |
| 1265 | unsigned PredReg) const { |
| 1266 | MachineFunction &MF = *MBB.getParent(); |
| 1267 | MachineConstantPool *ConstantPool = MF.getConstantPool(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1268 | const Constant *C = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1269 | ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1270 | unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4); |
| 1271 | |
Evan Cheng | 3784453 | 2009-07-16 09:20:10 +0000 | [diff] [blame] | 1272 | BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp)) |
| 1273 | .addReg(DestReg, getDefRegState(true), SubIdx) |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1274 | .addConstantPoolIndex(Idx) |
| 1275 | .addReg(0).addImm(0).addImm(Pred).addReg(PredReg); |
| 1276 | } |
| 1277 | |
| 1278 | bool ARMBaseRegisterInfo:: |
| 1279 | requiresRegisterScavenging(const MachineFunction &MF) const { |
| 1280 | return true; |
| 1281 | } |
Jim Grosbach | 41fff8c | 2009-10-21 23:40:56 +0000 | [diff] [blame] | 1282 | |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 1283 | bool ARMBaseRegisterInfo:: |
| 1284 | requiresFrameIndexScavenging(const MachineFunction &MF) const { |
Jim Grosbach | ca5dfb7 | 2009-10-28 17:33:28 +0000 | [diff] [blame] | 1285 | return true; |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 1286 | } |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1287 | |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 1288 | bool ARMBaseRegisterInfo:: |
| 1289 | requiresVirtualBaseRegisters(const MachineFunction &MF) const { |
| 1290 | return EnableLocalStackAlloc; |
| 1291 | } |
| 1292 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1293 | // hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 1294 | // not required, we reserve argument space for call sites in the function |
| 1295 | // immediately on entry to the current function. This eliminates the need for |
| 1296 | // add/sub sp brackets around call sites. Returns true if the call frame is |
| 1297 | // included as part of the stack frame. |
| 1298 | bool ARMBaseRegisterInfo:: |
Eric Christopher | 72852a8 | 2010-07-20 06:52:21 +0000 | [diff] [blame] | 1299 | hasReservedCallFrame(const MachineFunction &MF) const { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1300 | const MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 1301 | unsigned CFSize = FFI->getMaxCallFrameSize(); |
| 1302 | // It's not always a good idea to include the call frame as part of the |
| 1303 | // stack frame. ARM (especially Thumb) has small immediate offset to |
| 1304 | // address the stack frame. So a large call frame can cause poor codegen |
| 1305 | // and may even makes it impossible to scavenge a register. |
| 1306 | if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 |
| 1307 | return false; |
| 1308 | |
| 1309 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 1310 | } |
| 1311 | |
Jim Grosbach | 4642ad3 | 2010-02-22 23:10:38 +0000 | [diff] [blame] | 1312 | // canSimplifyCallFramePseudos - If there is a reserved call frame, the |
| 1313 | // call frame pseudos can be simplified. Unlike most targets, having a FP |
| 1314 | // is not sufficient here since we still may reference some objects via SP |
| 1315 | // even when FP is available in Thumb2 mode. |
| 1316 | bool ARMBaseRegisterInfo:: |
Eric Christopher | 72852a8 | 2010-07-20 06:52:21 +0000 | [diff] [blame] | 1317 | canSimplifyCallFramePseudos(const MachineFunction &MF) const { |
Jim Grosbach | 5f366af | 2010-02-24 02:15:43 +0000 | [diff] [blame] | 1318 | return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); |
Jim Grosbach | 4642ad3 | 2010-02-22 23:10:38 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1321 | static void |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1322 | emitSPUpdate(bool isARM, |
| 1323 | MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
| 1324 | DebugLoc dl, const ARMBaseInstrInfo &TII, |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1325 | int NumBytes, |
| 1326 | ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) { |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1327 | if (isARM) |
| 1328 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, |
| 1329 | Pred, PredReg, TII); |
| 1330 | else |
| 1331 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, |
| 1332 | Pred, PredReg, TII); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1335 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1336 | void ARMBaseRegisterInfo:: |
| 1337 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 1338 | MachineBasicBlock::iterator I) const { |
| 1339 | if (!hasReservedCallFrame(MF)) { |
| 1340 | // If we have alloca, convert as follows: |
| 1341 | // ADJCALLSTACKDOWN -> sub, sp, sp, amount |
| 1342 | // ADJCALLSTACKUP -> add, sp, sp, amount |
| 1343 | MachineInstr *Old = I; |
| 1344 | DebugLoc dl = Old->getDebugLoc(); |
| 1345 | unsigned Amount = Old->getOperand(0).getImm(); |
| 1346 | if (Amount != 0) { |
| 1347 | // We need to keep the stack aligned properly. To do this, we round the |
| 1348 | // amount of space needed for the outgoing arguments up to the next |
| 1349 | // alignment boundary. |
| 1350 | unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 1351 | Amount = (Amount+Align-1)/Align*Align; |
| 1352 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1353 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1354 | assert(!AFI->isThumb1OnlyFunction() && |
Jim Grosbach | cf453ee | 2010-02-23 17:16:27 +0000 | [diff] [blame] | 1355 | "This eliminateCallFramePseudoInstr does not support Thumb1!"); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1356 | bool isARM = !AFI->isThumbFunction(); |
| 1357 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1358 | // Replace the pseudo instruction with a new instruction... |
| 1359 | unsigned Opc = Old->getOpcode(); |
Jim Grosbach | 4c7628e | 2010-02-22 22:47:46 +0000 | [diff] [blame] | 1360 | int PIdx = Old->findFirstPredOperandIdx(); |
| 1361 | ARMCC::CondCodes Pred = (PIdx == -1) |
| 1362 | ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1363 | if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { |
| 1364 | // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. |
| 1365 | unsigned PredReg = Old->getOperand(2).getReg(); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1366 | emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | // Note: PredReg is operand 3 for ADJCALLSTACKUP. |
| 1369 | unsigned PredReg = Old->getOperand(3).getReg(); |
| 1370 | assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1371 | emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | MBB.erase(I); |
| 1376 | } |
| 1377 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1378 | int64_t ARMBaseRegisterInfo:: |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1379 | getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const { |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1380 | const TargetInstrDesc &Desc = MI->getDesc(); |
| 1381 | unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); |
| 1382 | int64_t InstrOffs = 0;; |
| 1383 | int Scale = 1; |
| 1384 | unsigned ImmIdx = 0; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1385 | switch (AddrMode) { |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1386 | case ARMII::AddrModeT2_i8: |
| 1387 | case ARMII::AddrModeT2_i12: |
| 1388 | // i8 supports only negative, and i12 supports only positive, so |
| 1389 | // based on Offset sign, consider the appropriate instruction |
| 1390 | InstrOffs = MI->getOperand(Idx+1).getImm(); |
| 1391 | Scale = 1; |
| 1392 | break; |
| 1393 | case ARMII::AddrMode5: { |
| 1394 | // VFP address mode. |
| 1395 | const MachineOperand &OffOp = MI->getOperand(Idx+1); |
Jim Grosbach | f78ee63 | 2010-08-25 19:11:34 +0000 | [diff] [blame] | 1396 | InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm()); |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1397 | if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub) |
| 1398 | InstrOffs = -InstrOffs; |
| 1399 | Scale = 4; |
| 1400 | break; |
| 1401 | } |
| 1402 | case ARMII::AddrMode2: { |
| 1403 | ImmIdx = Idx+2; |
| 1404 | InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm()); |
| 1405 | if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 1406 | InstrOffs = -InstrOffs; |
| 1407 | break; |
| 1408 | } |
| 1409 | case ARMII::AddrMode3: { |
| 1410 | ImmIdx = Idx+2; |
| 1411 | InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm()); |
| 1412 | if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 1413 | InstrOffs = -InstrOffs; |
| 1414 | break; |
| 1415 | } |
| 1416 | case ARMII::AddrModeT1_s: { |
| 1417 | ImmIdx = Idx+1; |
| 1418 | InstrOffs = MI->getOperand(ImmIdx).getImm(); |
| 1419 | Scale = 4; |
| 1420 | break; |
| 1421 | } |
| 1422 | default: |
| 1423 | llvm_unreachable("Unsupported addressing mode!"); |
| 1424 | break; |
| 1425 | } |
| 1426 | |
| 1427 | return InstrOffs * Scale; |
| 1428 | } |
| 1429 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1430 | /// needsFrameBaseReg - Returns true if the instruction's frame index |
| 1431 | /// reference would be better served by a base register other than FP |
| 1432 | /// or SP. Used by LocalStackFrameAllocation to determine which frame index |
| 1433 | /// references it should create new base registers for. |
| 1434 | bool ARMBaseRegisterInfo:: |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 1435 | needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { |
| 1436 | for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) { |
| 1437 | assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!"); |
| 1438 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1439 | |
| 1440 | // It's the load/store FI references that cause issues, as it can be difficult |
| 1441 | // to materialize the offset if it won't fit in the literal field. Estimate |
| 1442 | // based on the size of the local frame and some conservative assumptions |
| 1443 | // about the rest of the stack frame (note, this is pre-regalloc, so |
| 1444 | // we don't know everything for certain yet) whether this offset is likely |
| 1445 | // to be out of range of the immediate. Return true if so. |
| 1446 | |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 1447 | // We only generate virtual base registers for loads and stores, so |
| 1448 | // return false for everything else. |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1449 | unsigned Opc = MI->getOpcode(); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1450 | switch (Opc) { |
| 1451 | case ARM::LDR: case ARM::LDRH: case ARM::LDRB: |
| 1452 | case ARM::STR: case ARM::STRH: case ARM::STRB: |
| 1453 | case ARM::t2LDRi12: case ARM::t2LDRi8: |
| 1454 | case ARM::t2STRi12: case ARM::t2STRi8: |
| 1455 | case ARM::VLDRS: case ARM::VLDRD: |
| 1456 | case ARM::VSTRS: case ARM::VSTRD: |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 1457 | case ARM::tSTRspi: case ARM::tLDRspi: |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 1458 | if (ForceAllBaseRegAlloc) |
| 1459 | return true; |
| 1460 | break; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1461 | default: |
| 1462 | return false; |
| 1463 | } |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 1464 | |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 1465 | // Without a virtual base register, if the function has variable sized |
| 1466 | // objects, all fixed-size local references will be via the frame pointer, |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 1467 | // Approximate the offset and see if it's legal for the instruction. |
| 1468 | // Note that the incoming offset is based on the SP value at function entry, |
| 1469 | // so it'll be negative. |
| 1470 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 1471 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1472 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 1473 | |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 1474 | // Estimate an offset from the frame pointer. |
| 1475 | // Conservatively assume all callee-saved registers get pushed. R4-R6 |
| 1476 | // will be earlier than the FP, so we ignore those. |
| 1477 | // R7, LR |
| 1478 | int64_t FPOffset = Offset - 8; |
| 1479 | // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15 |
| 1480 | if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction()) |
| 1481 | FPOffset -= 80; |
| 1482 | // Estimate an offset from the stack pointer. |
| 1483 | Offset = -Offset; |
| 1484 | // Assume that we'll have at least some spill slots allocated. |
| 1485 | // FIXME: This is a total SWAG number. We should run some statistics |
| 1486 | // and pick a real one. |
| 1487 | Offset += 128; // 128 bytes of spill slots |
| 1488 | |
| 1489 | // If there is a frame pointer, try using it. |
| 1490 | // The FP is only available if there is no dynamic realignment. We |
| 1491 | // don't know for sure yet whether we'll need that, so we guess based |
| 1492 | // on whether there are any local variables that would trigger it. |
| 1493 | unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 1494 | if (hasFP(MF) && |
| 1495 | !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) { |
| 1496 | if (isFrameOffsetLegal(MI, FPOffset)) |
| 1497 | return false; |
| 1498 | } |
| 1499 | // If we can reference via the stack pointer, try that. |
| 1500 | // FIXME: This (and the code that resolves the references) can be improved |
| 1501 | // to only disallow SP relative references in the live range of |
| 1502 | // the VLA(s). In practice, it's unclear how much difference that |
| 1503 | // would make, but it may be worth doing. |
| 1504 | if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset)) |
| 1505 | return false; |
| 1506 | |
| 1507 | // The offset likely isn't legal, we want to allocate a virtual base register. |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 1508 | return true; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 1511 | /// materializeFrameBaseRegister - Insert defining instruction(s) for |
| 1512 | /// BaseReg to be a pointer to FrameIdx before insertion point I. |
| 1513 | void ARMBaseRegisterInfo:: |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1514 | materializeFrameBaseRegister(MachineBasicBlock::iterator I, unsigned BaseReg, |
| 1515 | int FrameIdx, int64_t Offset) const { |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 1516 | ARMFunctionInfo *AFI = |
| 1517 | I->getParent()->getParent()->getInfo<ARMFunctionInfo>(); |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 1518 | unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : |
| 1519 | (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 1520 | |
| 1521 | MachineInstrBuilder MIB = |
| 1522 | BuildMI(*I->getParent(), I, I->getDebugLoc(), TII.get(ADDriOpc), BaseReg) |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1523 | .addFrameIndex(FrameIdx).addImm(Offset); |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 1524 | if (!AFI->isThumb1OnlyFunction()) |
| 1525 | AddDefaultCC(AddDefaultPred(MIB)); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | void |
| 1529 | ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I, |
| 1530 | unsigned BaseReg, int64_t Offset) const { |
| 1531 | MachineInstr &MI = *I; |
| 1532 | MachineBasicBlock &MBB = *MI.getParent(); |
| 1533 | MachineFunction &MF = *MBB.getParent(); |
| 1534 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1535 | int Off = Offset; // ARM doesn't need the general 64-bit offsets |
| 1536 | unsigned i = 0; |
| 1537 | |
| 1538 | assert(!AFI->isThumb1OnlyFunction() && |
| 1539 | "This resolveFrameIndex does not support Thumb1!"); |
| 1540 | |
| 1541 | while (!MI.getOperand(i).isFI()) { |
| 1542 | ++i; |
| 1543 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 1544 | } |
| 1545 | bool Done = false; |
| 1546 | if (!AFI->isThumbFunction()) |
| 1547 | Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII); |
| 1548 | else { |
| 1549 | assert(AFI->isThumb2Function()); |
| 1550 | Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII); |
| 1551 | } |
| 1552 | assert (Done && "Unable to resolve frame index!"); |
| 1553 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 1554 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1555 | bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, |
| 1556 | int64_t Offset) const { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1557 | const TargetInstrDesc &Desc = MI->getDesc(); |
| 1558 | unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); |
| 1559 | unsigned i = 0; |
| 1560 | |
| 1561 | while (!MI->getOperand(i).isFI()) { |
| 1562 | ++i; |
| 1563 | assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!"); |
| 1564 | } |
| 1565 | |
| 1566 | // AddrMode4 and AddrMode6 cannot handle any offset. |
| 1567 | if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6) |
| 1568 | return Offset == 0; |
| 1569 | |
| 1570 | unsigned NumBits = 0; |
| 1571 | unsigned Scale = 1; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1572 | bool isSigned = true; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1573 | switch (AddrMode) { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1574 | case ARMII::AddrModeT2_i8: |
| 1575 | case ARMII::AddrModeT2_i12: |
| 1576 | // i8 supports only negative, and i12 supports only positive, so |
| 1577 | // based on Offset sign, consider the appropriate instruction |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 1578 | Scale = 1; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1579 | if (Offset < 0) { |
| 1580 | NumBits = 8; |
| 1581 | Offset = -Offset; |
| 1582 | } else { |
| 1583 | NumBits = 12; |
| 1584 | } |
| 1585 | break; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1586 | case ARMII::AddrMode5: |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1587 | // VFP address mode. |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1588 | NumBits = 8; |
| 1589 | Scale = 4; |
| 1590 | break; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1591 | case ARMII::AddrMode2: |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1592 | NumBits = 12; |
| 1593 | break; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1594 | case ARMII::AddrMode3: |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1595 | NumBits = 8; |
| 1596 | break; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1597 | case ARMII::AddrModeT1_s: |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 1598 | NumBits = 5; |
| 1599 | Scale = 4; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1600 | isSigned = false; |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 1601 | break; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1602 | default: |
| 1603 | llvm_unreachable("Unsupported addressing mode!"); |
| 1604 | break; |
| 1605 | } |
| 1606 | |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 1607 | Offset += getFrameIndexInstrOffset(MI, i); |
Jim Grosbach | d4511e9 | 2010-08-31 18:49:31 +0000 | [diff] [blame] | 1608 | // Make sure the offset is encodable for instructions that scale the |
| 1609 | // immediate. |
| 1610 | if ((Offset & (Scale-1)) != 0) |
| 1611 | return false; |
| 1612 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 1613 | if (isSigned && Offset < 0) |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 1614 | Offset = -Offset; |
| 1615 | |
| 1616 | unsigned Mask = (1 << NumBits) - 1; |
| 1617 | if ((unsigned)Offset <= Mask * Scale) |
| 1618 | return true; |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 1619 | |
| 1620 | return false; |
| 1621 | } |
| 1622 | |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 1623 | void |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1624 | ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 1625 | int SPAdj, RegScavenger *RS) const { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1626 | unsigned i = 0; |
| 1627 | MachineInstr &MI = *II; |
| 1628 | MachineBasicBlock &MBB = *MI.getParent(); |
| 1629 | MachineFunction &MF = *MBB.getParent(); |
| 1630 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1631 | assert(!AFI->isThumb1OnlyFunction() && |
Bob Wilson | a15de00 | 2009-09-18 21:42:44 +0000 | [diff] [blame] | 1632 | "This eliminateFrameIndex does not support Thumb1!"); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1633 | |
| 1634 | while (!MI.getOperand(i).isFI()) { |
| 1635 | ++i; |
| 1636 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 1637 | } |
| 1638 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1639 | int FrameIndex = MI.getOperand(i).getIndex(); |
Jim Grosbach | a37aa54 | 2009-11-22 20:05:32 +0000 | [diff] [blame] | 1640 | unsigned FrameReg; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1641 | |
Jim Grosbach | e3ede5e | 2010-08-05 19:27:37 +0000 | [diff] [blame] | 1642 | int Offset = ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1643 | |
Evan Cheng | 62b5065 | 2010-04-26 07:39:25 +0000 | [diff] [blame] | 1644 | // Special handling of dbg_value instructions. |
| 1645 | if (MI.isDebugValue()) { |
| 1646 | MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/); |
| 1647 | MI.getOperand(i+1).ChangeToImmediate(Offset); |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 1648 | return; |
Evan Cheng | 62b5065 | 2010-04-26 07:39:25 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Evan Cheng | 48d8afa | 2009-11-01 21:12:51 +0000 | [diff] [blame] | 1651 | // Modify MI as necessary to handle as much of 'Offset' as possible |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1652 | bool Done = false; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1653 | if (!AFI->isThumbFunction()) |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1654 | Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1655 | else { |
| 1656 | assert(AFI->isThumb2Function()); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1657 | Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1658 | } |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1659 | if (Done) |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 1660 | return; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1661 | |
| 1662 | // If we get here, the immediate doesn't fit into the instruction. We folded |
| 1663 | // as much as possible above, handle the rest, providing a register that is |
| 1664 | // SP+LargeImm. |
Daniel Dunbar | 19bb87d | 2009-08-28 08:08:22 +0000 | [diff] [blame] | 1665 | assert((Offset || |
Jim Grosbach | a443217 | 2009-11-15 21:45:34 +0000 | [diff] [blame] | 1666 | (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 || |
| 1667 | (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) && |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1668 | "This code isn't needed if offset already handled!"); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1669 | |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 1670 | unsigned ScratchReg = 0; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1671 | int PIdx = MI.findFirstPredOperandIdx(); |
| 1672 | ARMCC::CondCodes Pred = (PIdx == -1) |
| 1673 | ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm(); |
| 1674 | unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg(); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1675 | if (Offset == 0) |
Jim Grosbach | a443217 | 2009-11-15 21:45:34 +0000 | [diff] [blame] | 1676 | // Must be addrmode4/6. |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1677 | MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1678 | else { |
Jim Grosbach | ca5dfb7 | 2009-10-28 17:33:28 +0000 | [diff] [blame] | 1679 | ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 1680 | if (!AFI->isThumbFunction()) |
| 1681 | emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, |
| 1682 | Offset, Pred, PredReg, TII); |
| 1683 | else { |
| 1684 | assert(AFI->isThumb2Function()); |
| 1685 | emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, |
| 1686 | Offset, Pred, PredReg, TII); |
| 1687 | } |
| 1688 | MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1689 | } |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Jim Grosbach | 4371cda | 2009-11-04 23:20:40 +0000 | [diff] [blame] | 1692 | /// Move iterator past the next bunch of callee save load / store ops for |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1693 | /// the particular spill area (1: integer area 1, 2: integer area 2, |
| 1694 | /// 3: fp area, 0: don't care). |
| 1695 | static void movePastCSLoadStoreOps(MachineBasicBlock &MBB, |
| 1696 | MachineBasicBlock::iterator &MBBI, |
David Goodwin | 5ff58b5 | 2009-07-24 00:16:18 +0000 | [diff] [blame] | 1697 | int Opc1, int Opc2, unsigned Area, |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1698 | const ARMSubtarget &STI) { |
| 1699 | while (MBBI != MBB.end() && |
David Goodwin | 5ff58b5 | 2009-07-24 00:16:18 +0000 | [diff] [blame] | 1700 | ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) && |
| 1701 | MBBI->getOperand(1).isFI()) { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1702 | if (Area != 0) { |
| 1703 | bool Done = false; |
| 1704 | unsigned Category = 0; |
| 1705 | switch (MBBI->getOperand(0).getReg()) { |
| 1706 | case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7: |
| 1707 | case ARM::LR: |
| 1708 | Category = 1; |
| 1709 | break; |
| 1710 | case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11: |
| 1711 | Category = STI.isTargetDarwin() ? 2 : 1; |
| 1712 | break; |
| 1713 | case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11: |
| 1714 | case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15: |
| 1715 | Category = 3; |
| 1716 | break; |
| 1717 | default: |
| 1718 | Done = true; |
| 1719 | break; |
| 1720 | } |
| 1721 | if (Done || Category != Area) |
| 1722 | break; |
| 1723 | } |
| 1724 | |
| 1725 | ++MBBI; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | void ARMBaseRegisterInfo:: |
| 1730 | emitPrologue(MachineFunction &MF) const { |
| 1731 | MachineBasicBlock &MBB = MF.front(); |
| 1732 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 1733 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1734 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1735 | assert(!AFI->isThumb1OnlyFunction() && |
Jim Grosbach | cf453ee | 2010-02-23 17:16:27 +0000 | [diff] [blame] | 1736 | "This emitPrologue does not support Thumb1!"); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1737 | bool isARM = !AFI->isThumbFunction(); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1738 | unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); |
| 1739 | unsigned NumBytes = MFI->getStackSize(); |
| 1740 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1741 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Determine the sizes of each callee-save spill areas and record which frame |
| 1744 | // belongs to which callee-save spill areas. |
| 1745 | unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; |
| 1746 | int FramePtrSpillFI = 0; |
| 1747 | |
Bob Wilson | c8ce2d4 | 2009-09-25 16:34:46 +0000 | [diff] [blame] | 1748 | // Allocate the vararg register save area. This is not counted in NumBytes. |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1749 | if (VARegSaveSize) |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1750 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (!AFI->hasStackFrame()) { |
| 1753 | if (NumBytes != 0) |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1754 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
| 1758 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1759 | unsigned Reg = CSI[i].getReg(); |
| 1760 | int FI = CSI[i].getFrameIdx(); |
| 1761 | switch (Reg) { |
| 1762 | case ARM::R4: |
| 1763 | case ARM::R5: |
| 1764 | case ARM::R6: |
| 1765 | case ARM::R7: |
| 1766 | case ARM::LR: |
| 1767 | if (Reg == FramePtr) |
| 1768 | FramePtrSpillFI = FI; |
| 1769 | AFI->addGPRCalleeSavedArea1Frame(FI); |
| 1770 | GPRCS1Size += 4; |
| 1771 | break; |
| 1772 | case ARM::R8: |
| 1773 | case ARM::R9: |
| 1774 | case ARM::R10: |
| 1775 | case ARM::R11: |
| 1776 | if (Reg == FramePtr) |
| 1777 | FramePtrSpillFI = FI; |
| 1778 | if (STI.isTargetDarwin()) { |
| 1779 | AFI->addGPRCalleeSavedArea2Frame(FI); |
| 1780 | GPRCS2Size += 4; |
| 1781 | } else { |
| 1782 | AFI->addGPRCalleeSavedArea1Frame(FI); |
| 1783 | GPRCS1Size += 4; |
| 1784 | } |
| 1785 | break; |
| 1786 | default: |
| 1787 | AFI->addDPRCalleeSavedAreaFrame(FI); |
| 1788 | DPRCSSize += 8; |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | // Build the new SUBri to adjust SP for integer callee-save spill area 1. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1793 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size); |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 1794 | movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1795 | |
Bob Wilson | c8ce2d4 | 2009-09-25 16:34:46 +0000 | [diff] [blame] | 1796 | // Set FP to point to the stack slot that contains the previous FP. |
| 1797 | // For Darwin, FP is R7, which has now been stored in spill area 1. |
| 1798 | // Otherwise, if this is not Darwin, all the callee-saved registers go |
| 1799 | // into spill area 1, including the FP in R11. In either case, it is |
| 1800 | // now safe to emit this assignment. |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1801 | bool HasFP = hasFP(MF); |
| 1802 | if (HasFP) { |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1803 | unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1804 | MachineInstrBuilder MIB = |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1805 | BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr) |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1806 | .addFrameIndex(FramePtrSpillFI).addImm(0); |
| 1807 | AddDefaultCC(AddDefaultPred(MIB)); |
| 1808 | } |
| 1809 | |
| 1810 | // Build the new SUBri to adjust SP for integer callee-save spill area 2. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1811 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1812 | |
| 1813 | // Build the new SUBri to adjust SP for FP callee-save spill area. |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 1814 | movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1815 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1816 | |
| 1817 | // Determine starting offsets of spill areas. |
| 1818 | unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); |
| 1819 | unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; |
| 1820 | unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1821 | if (HasFP) |
Bob Wilson | 436e6e7 | 2010-03-04 21:42:36 +0000 | [diff] [blame] | 1822 | AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + |
| 1823 | NumBytes); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1824 | AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); |
| 1825 | AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); |
| 1826 | AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); |
| 1827 | |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 1828 | movePastCSLoadStoreOps(MBB, MBBI, ARM::VSTRD, 0, 3, STI); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1829 | NumBytes = DPRCSOffset; |
| 1830 | if (NumBytes) { |
Jim Grosbach | c5848f4 | 2009-11-04 22:41:00 +0000 | [diff] [blame] | 1831 | // Adjust SP after all the callee-save spills. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1832 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes); |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1833 | if (HasFP) |
| 1834 | AFI->setShouldRestoreSPFromFP(true); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | if (STI.isTargetELF() && hasFP(MF)) { |
| 1838 | MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - |
| 1839 | AFI->getFramePtrSpillOffset()); |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1840 | AFI->setShouldRestoreSPFromFP(true); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); |
| 1844 | AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); |
| 1845 | AFI->setDPRCalleeSavedAreaSize(DPRCSSize); |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 1846 | |
| 1847 | // If we need dynamic stack realignment, do it here. |
| 1848 | if (needsStackRealignment(MF)) { |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 1849 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 1850 | assert (!AFI->isThumb1OnlyFunction()); |
Anton Korobeynikov | 7cca606 | 2009-12-06 22:39:50 +0000 | [diff] [blame] | 1851 | if (!AFI->isThumbFunction()) { |
| 1852 | // Emit bic sp, sp, MaxAlign |
| 1853 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, |
| 1854 | TII.get(ARM::BICri), ARM::SP) |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 1855 | .addReg(ARM::SP, RegState::Kill) |
| 1856 | .addImm(MaxAlign-1))); |
Anton Korobeynikov | 7cca606 | 2009-12-06 22:39:50 +0000 | [diff] [blame] | 1857 | } else { |
| 1858 | // We cannot use sp as source/dest register here, thus we're emitting the |
| 1859 | // following sequence: |
| 1860 | // mov r4, sp |
| 1861 | // bic r4, r4, MaxAlign |
| 1862 | // mov sp, r4 |
| 1863 | // FIXME: It will be better just to find spare register here. |
Jakob Stoklund Olesen | e9912dc | 2009-12-22 18:49:55 +0000 | [diff] [blame] | 1864 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4) |
Anton Korobeynikov | 7cca606 | 2009-12-06 22:39:50 +0000 | [diff] [blame] | 1865 | .addReg(ARM::SP, RegState::Kill); |
| 1866 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, |
| 1867 | TII.get(ARM::t2BICri), ARM::R4) |
| 1868 | .addReg(ARM::R4, RegState::Kill) |
| 1869 | .addImm(MaxAlign-1))); |
| 1870 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP) |
| 1871 | .addReg(ARM::R4, RegState::Kill); |
| 1872 | } |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1873 | |
| 1874 | AFI->setShouldRestoreSPFromFP(true); |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 1875 | } |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1876 | |
| 1877 | // If the frame has variable sized objects then the epilogue must restore |
| 1878 | // the sp from fp. |
| 1879 | if (!AFI->shouldRestoreSPFromFP() && MFI->hasVarSizedObjects()) |
| 1880 | AFI->setShouldRestoreSPFromFP(true); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) { |
| 1884 | for (unsigned i = 0; CSRegs[i]; ++i) |
| 1885 | if (Reg == CSRegs[i]) |
| 1886 | return true; |
| 1887 | return false; |
| 1888 | } |
| 1889 | |
David Goodwin | 77521f5 | 2009-07-08 20:28:28 +0000 | [diff] [blame] | 1890 | static bool isCSRestore(MachineInstr *MI, |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1891 | const ARMBaseInstrInfo &TII, |
David Goodwin | 77521f5 | 2009-07-08 20:28:28 +0000 | [diff] [blame] | 1892 | const unsigned *CSRegs) { |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 1893 | return ((MI->getOpcode() == (int)ARM::VLDRD || |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 1894 | MI->getOpcode() == (int)ARM::LDR || |
| 1895 | MI->getOpcode() == (int)ARM::t2LDRi12) && |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1896 | MI->getOperand(1).isFI() && |
| 1897 | isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs)); |
| 1898 | } |
| 1899 | |
| 1900 | void ARMBaseRegisterInfo:: |
Evan Cheng | 293f8d9 | 2009-07-27 18:31:40 +0000 | [diff] [blame] | 1901 | emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1902 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
Evan Cheng | 5ca53a7 | 2009-07-27 18:20:05 +0000 | [diff] [blame] | 1903 | assert(MBBI->getDesc().isReturn() && |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1904 | "Can only insert epilog into returning blocks"); |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1905 | unsigned RetOpcode = MBBI->getOpcode(); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1906 | DebugLoc dl = MBBI->getDebugLoc(); |
| 1907 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1908 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1909 | assert(!AFI->isThumb1OnlyFunction() && |
Jim Grosbach | cf453ee | 2010-02-23 17:16:27 +0000 | [diff] [blame] | 1910 | "This emitEpilogue does not support Thumb1!"); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1911 | bool isARM = !AFI->isThumbFunction(); |
| 1912 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1913 | unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); |
| 1914 | int NumBytes = (int)MFI->getStackSize(); |
| 1915 | |
| 1916 | if (!AFI->hasStackFrame()) { |
| 1917 | if (NumBytes != 0) |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1918 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1919 | } else { |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 1920 | // Unwind MBBI to point to first LDR / VLDRD. |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1921 | const unsigned *CSRegs = getCalleeSavedRegs(); |
| 1922 | if (MBBI != MBB.begin()) { |
| 1923 | do |
| 1924 | --MBBI; |
David Goodwin | 77521f5 | 2009-07-08 20:28:28 +0000 | [diff] [blame] | 1925 | while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); |
| 1926 | if (!isCSRestore(MBBI, TII, CSRegs)) |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1927 | ++MBBI; |
| 1928 | } |
| 1929 | |
| 1930 | // Move SP to start of FP callee save spill area. |
| 1931 | NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + |
| 1932 | AFI->getGPRCalleeSavedArea2Size() + |
| 1933 | AFI->getDPRCalleeSavedAreaSize()); |
| 1934 | |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1935 | // Reset SP based on frame pointer only if the stack frame extends beyond |
| 1936 | // frame pointer stack slot or target is ELF and the function has FP. |
| 1937 | if (AFI->shouldRestoreSPFromFP()) { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1938 | NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; |
Evan Cheng | ac09680 | 2010-08-10 19:30:19 +0000 | [diff] [blame] | 1939 | if (NumBytes) { |
| 1940 | if (isARM) |
| 1941 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, |
| 1942 | ARMCC::AL, 0, TII); |
| 1943 | else |
| 1944 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, |
| 1945 | ARMCC::AL, 0, TII); |
| 1946 | } else { |
| 1947 | // Thumb2 or ARM. |
| 1948 | if (isARM) |
| 1949 | BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) |
| 1950 | .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 1951 | else |
| 1952 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP) |
| 1953 | .addReg(FramePtr); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1954 | } |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1955 | } else if (NumBytes) |
| 1956 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1957 | |
| 1958 | // Move SP to start of integer callee save spill area 2. |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 1959 | movePastCSLoadStoreOps(MBB, MBBI, ARM::VLDRD, 0, 3, STI); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1960 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize()); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1961 | |
| 1962 | // Move SP to start of integer callee save spill area 1. |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 1963 | movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1964 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size()); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Move SP to SP upon entry to the function. |
Evan Cheng | 5732ca0 | 2009-07-27 03:14:20 +0000 | [diff] [blame] | 1967 | movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 1968 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size()); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1971 | if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND || |
| 1972 | RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) { |
| 1973 | // Tail call return: adjust the stack pointer and jump to callee. |
| 1974 | MBBI = prior(MBB.end()); |
| 1975 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 1976 | |
| 1977 | // Jump to label or value in register. |
| 1978 | if (RetOpcode == ARM::TCRETURNdi) { |
Dale Johannesen | 7835f1f | 2010-07-08 01:18:23 +0000 | [diff] [blame] | 1979 | BuildMI(MBB, MBBI, dl, |
| 1980 | TII.get(STI.isThumb() ? ARM::TAILJMPdt : ARM::TAILJMPd)). |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1981 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), |
| 1982 | JumpTarget.getTargetFlags()); |
| 1983 | } else if (RetOpcode == ARM::TCRETURNdiND) { |
Dale Johannesen | 1041680 | 2010-06-18 20:44:28 +0000 | [diff] [blame] | 1984 | BuildMI(MBB, MBBI, dl, |
| 1985 | TII.get(STI.isThumb() ? ARM::TAILJMPdNDt : ARM::TAILJMPdND)). |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1986 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), |
| 1987 | JumpTarget.getTargetFlags()); |
| 1988 | } else if (RetOpcode == ARM::TCRETURNri) { |
Dale Johannesen | 6470a11 | 2010-06-15 22:08:33 +0000 | [diff] [blame] | 1989 | BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPr)). |
| 1990 | addReg(JumpTarget.getReg(), RegState::Kill); |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1991 | } else if (RetOpcode == ARM::TCRETURNriND) { |
Dale Johannesen | 6470a11 | 2010-06-15 22:08:33 +0000 | [diff] [blame] | 1992 | BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPrND)). |
| 1993 | addReg(JumpTarget.getReg(), RegState::Kill); |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | MachineInstr *NewMI = prior(MBBI); |
Dale Johannesen | 6470a11 | 2010-06-15 22:08:33 +0000 | [diff] [blame] | 1997 | for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1998 | NewMI->addOperand(MBBI->getOperand(i)); |
| 1999 | |
| 2000 | // Delete the pseudo instruction TCRETURN. |
| 2001 | MBB.erase(MBBI); |
| 2002 | } |
| 2003 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 2004 | if (VARegSaveSize) |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 2005 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 2008 | #include "ARMGenRegisterInfo.inc" |