Jia Liu | 31d157a | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===// |
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 | |
Craig Topper | c1f6f42 | 2012-03-17 07:33:42 +0000 | [diff] [blame] | 14 | #include "ARMBaseRegisterInfo.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 15 | #include "ARM.h" |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 16 | #include "ARMBaseInstrInfo.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 17 | #include "ARMFrameLowering.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
| 19 | #include "ARMSubtarget.h" |
Evan Cheng | ee04a6d | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 20 | #include "MCTargetDesc/ARMAddressingModes.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/BitVector.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 28 | #include "llvm/CodeGen/RegisterScavenging.h" |
Jakob Stoklund Olesen | 303da1b | 2012-12-03 22:35:35 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/VirtRegMap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Constants.h" |
| 31 | #include "llvm/DerivedTypes.h" |
| 32 | #include "llvm/Function.h" |
| 33 | #include "llvm/LLVMContext.h" |
| 34 | #include "llvm/Support/CommandLine.h" |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetFrameLowering.h" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetMachine.h" |
| 40 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | 73f50d9 | 2011-06-27 18:32:37 +0000 | [diff] [blame] | 41 | |
Evan Cheng | 73f50d9 | 2011-06-27 18:32:37 +0000 | [diff] [blame] | 42 | #define GET_REGINFO_TARGET_DESC |
Evan Cheng | a347f85 | 2011-06-24 01:44:41 +0000 | [diff] [blame] | 43 | #include "ARMGenRegisterInfo.inc" |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 44 | |
Evan Cheng | 1b4886d | 2010-11-18 01:28:51 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 47 | ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii, |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 48 | const ARMSubtarget &sti) |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 49 | : ARMGenRegisterInfo(ARM::LR), TII(tii), STI(sti), |
Jim Grosbach | 65482b1 | 2010-09-03 18:37:12 +0000 | [diff] [blame] | 50 | FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11), |
| 51 | BasePtr(ARM::R6) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Craig Topper | 015f228 | 2012-03-04 03:33:22 +0000 | [diff] [blame] | 54 | const uint16_t* |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 55 | ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { |
Eric Christopher | e94ac88 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 56 | bool ghcCall = false; |
| 57 | |
| 58 | if (MF) { |
| 59 | const Function *F = MF->getFunction(); |
| 60 | ghcCall = (F ? F->getCallingConv() == CallingConv::GHC : false); |
| 61 | } |
| 62 | |
| 63 | if (ghcCall) { |
| 64 | return CSR_GHC_SaveList; |
| 65 | } |
| 66 | else { |
Evan Cheng | afb3b5e | 2012-04-27 02:11:10 +0000 | [diff] [blame] | 67 | return (STI.isTargetIOS() && !STI.isAAPCS_ABI()) |
| 68 | ? CSR_iOS_SaveList : CSR_AAPCS_SaveList; |
Eric Christopher | e94ac88 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 69 | } |
Jakob Stoklund Olesen | 3ee7d15 | 2012-01-17 23:09:00 +0000 | [diff] [blame] | 70 | } |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 71 | |
Jakob Stoklund Olesen | 3ee7d15 | 2012-01-17 23:09:00 +0000 | [diff] [blame] | 72 | const uint32_t* |
| 73 | ARMBaseRegisterInfo::getCallPreservedMask(CallingConv::ID) const { |
Evan Cheng | afb3b5e | 2012-04-27 02:11:10 +0000 | [diff] [blame] | 74 | return (STI.isTargetIOS() && !STI.isAAPCS_ABI()) |
| 75 | ? CSR_iOS_RegMask : CSR_AAPCS_RegMask; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chad Rosier | e7bd519 | 2012-11-06 23:05:24 +0000 | [diff] [blame] | 78 | const uint32_t* |
| 79 | ARMBaseRegisterInfo::getNoPreservedMask() const { |
| 80 | return CSR_NoRegs_RegMask; |
| 81 | } |
| 82 | |
Jim Grosbach | 9631864 | 2010-01-06 23:54:42 +0000 | [diff] [blame] | 83 | BitVector ARMBaseRegisterInfo:: |
| 84 | getReservedRegs(const MachineFunction &MF) const { |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 85 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 87 | // FIXME: avoid re-calculating this every time. |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 88 | BitVector Reserved(getNumRegs()); |
| 89 | Reserved.set(ARM::SP); |
| 90 | Reserved.set(ARM::PC); |
Lang Hames | 4f92b5e | 2012-03-06 00:19:55 +0000 | [diff] [blame] | 91 | Reserved.set(ARM::FPSCR); |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 92 | if (TFI->hasFP(MF)) |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 93 | Reserved.set(FramePtr); |
Jim Grosbach | 65482b1 | 2010-09-03 18:37:12 +0000 | [diff] [blame] | 94 | if (hasBasePointer(MF)) |
| 95 | Reserved.set(BasePtr); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 96 | // Some targets reserve R9. |
| 97 | if (STI.isR9Reserved()) |
| 98 | Reserved.set(ARM::R9); |
Jakob Stoklund Olesen | 3b6434e | 2011-06-18 00:53:27 +0000 | [diff] [blame] | 99 | // Reserve D16-D31 if the subtarget doesn't support them. |
| 100 | if (!STI.hasVFP3() || STI.hasD16()) { |
| 101 | assert(ARM::D31 == ARM::D16 + 15); |
| 102 | for (unsigned i = 0; i != 16; ++i) |
| 103 | Reserved.set(ARM::D16 + i); |
| 104 | } |
Jakob Stoklund Olesen | cd275f5 | 2012-10-26 21:29:15 +0000 | [diff] [blame] | 105 | const TargetRegisterClass *RC = &ARM::GPRPairRegClass; |
| 106 | for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I) |
| 107 | for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI) |
| 108 | if (Reserved.test(*SI)) Reserved.set(*I); |
| 109 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 110 | return Reserved; |
| 111 | } |
| 112 | |
Jakob Stoklund Olesen | c9e5015 | 2011-04-26 18:52:33 +0000 | [diff] [blame] | 113 | const TargetRegisterClass* |
| 114 | ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC) |
| 115 | const { |
| 116 | const TargetRegisterClass *Super = RC; |
Jakob Stoklund Olesen | c8e2bb6 | 2011-09-30 22:19:07 +0000 | [diff] [blame] | 117 | TargetRegisterClass::sc_iterator I = RC->getSuperClasses(); |
Jakob Stoklund Olesen | c9e5015 | 2011-04-26 18:52:33 +0000 | [diff] [blame] | 118 | do { |
| 119 | switch (Super->getID()) { |
| 120 | case ARM::GPRRegClassID: |
| 121 | case ARM::SPRRegClassID: |
| 122 | case ARM::DPRRegClassID: |
| 123 | case ARM::QPRRegClassID: |
| 124 | case ARM::QQPRRegClassID: |
| 125 | case ARM::QQQQPRRegClassID: |
Jakob Stoklund Olesen | cd275f5 | 2012-10-26 21:29:15 +0000 | [diff] [blame] | 126 | case ARM::GPRPairRegClassID: |
Jakob Stoklund Olesen | c9e5015 | 2011-04-26 18:52:33 +0000 | [diff] [blame] | 127 | return Super; |
| 128 | } |
| 129 | Super = *I++; |
| 130 | } while (Super); |
| 131 | return RC; |
| 132 | } |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 133 | |
Evan Cheng | 4f54c12 | 2009-10-25 07:53:28 +0000 | [diff] [blame] | 134 | const TargetRegisterClass * |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 135 | ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) |
| 136 | const { |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 137 | return &ARM::GPRRegClass; |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Evan Cheng | 342e316 | 2011-08-30 01:34:54 +0000 | [diff] [blame] | 140 | const TargetRegisterClass * |
| 141 | ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { |
| 142 | if (RC == &ARM::CCRRegClass) |
| 143 | return 0; // Can't copy CCR registers. |
| 144 | return RC; |
| 145 | } |
| 146 | |
Cameron Zwarich | be2119e | 2011-03-07 21:56:36 +0000 | [diff] [blame] | 147 | unsigned |
| 148 | ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, |
| 149 | MachineFunction &MF) const { |
| 150 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
| 151 | |
| 152 | switch (RC->getID()) { |
| 153 | default: |
| 154 | return 0; |
| 155 | case ARM::tGPRRegClassID: |
| 156 | return TFI->hasFP(MF) ? 4 : 5; |
| 157 | case ARM::GPRRegClassID: { |
| 158 | unsigned FP = TFI->hasFP(MF) ? 1 : 0; |
| 159 | return 10 - FP - (STI.isR9Reserved() ? 1 : 0); |
| 160 | } |
| 161 | case ARM::SPRRegClassID: // Currently not used as 'rep' register class. |
| 162 | case ARM::DPRRegClassID: |
| 163 | return 32 - 10; |
| 164 | } |
| 165 | } |
| 166 | |
Jakob Stoklund Olesen | 303da1b | 2012-12-03 22:35:35 +0000 | [diff] [blame] | 167 | // Get the other register in a GPRPair. |
| 168 | static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) { |
| 169 | for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers) |
| 170 | if (ARM::GPRPairRegClass.contains(*Supers)) |
| 171 | return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | // Resolve the RegPairEven / RegPairOdd register allocator hints. |
| 176 | void |
| 177 | ARMBaseRegisterInfo::getRegAllocationHints(unsigned VirtReg, |
| 178 | ArrayRef<MCPhysReg> Order, |
| 179 | SmallVectorImpl<MCPhysReg> &Hints, |
| 180 | const MachineFunction &MF, |
| 181 | const VirtRegMap *VRM) const { |
| 182 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 183 | std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg); |
| 184 | |
| 185 | unsigned Odd; |
| 186 | switch (Hint.first) { |
| 187 | case ARMRI::RegPairEven: |
| 188 | Odd = 0; |
| 189 | break; |
| 190 | case ARMRI::RegPairOdd: |
| 191 | Odd = 1; |
| 192 | break; |
| 193 | default: |
| 194 | TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // This register should preferably be even (Odd == 0) or odd (Odd == 1). |
| 199 | // Check if the other part of the pair has already been assigned, and provide |
| 200 | // the paired register as the first hint. |
| 201 | unsigned PairedPhys = 0; |
| 202 | if (VRM && VRM->hasPhys(Hint.second)) { |
| 203 | PairedPhys = getPairedGPR(VRM->getPhys(Hint.second), Odd, this); |
| 204 | if (PairedPhys && MRI.isReserved(PairedPhys)) |
| 205 | PairedPhys = 0; |
| 206 | } |
| 207 | |
| 208 | // First prefer the paired physreg. |
| 209 | if (PairedPhys) |
| 210 | Hints.push_back(PairedPhys); |
| 211 | |
| 212 | // Then prefer even or odd registers. |
| 213 | for (unsigned I = 0, E = Order.size(); I != E; ++I) { |
| 214 | unsigned Reg = Order[I]; |
| 215 | if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd) |
| 216 | continue; |
| 217 | // Don't provide hints that are paired to a reserved register. |
| 218 | unsigned Paired = getPairedGPR(Reg, !Odd, this); |
| 219 | if (!Paired || MRI.isReserved(Paired)) |
| 220 | continue; |
| 221 | Hints.push_back(Reg); |
| 222 | } |
| 223 | } |
| 224 | |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 225 | void |
| 226 | ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg, |
| 227 | MachineFunction &MF) const { |
| 228 | MachineRegisterInfo *MRI = &MF.getRegInfo(); |
| 229 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg); |
| 230 | if ((Hint.first == (unsigned)ARMRI::RegPairOdd || |
| 231 | Hint.first == (unsigned)ARMRI::RegPairEven) && |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 232 | TargetRegisterInfo::isVirtualRegister(Hint.second)) { |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 233 | // If 'Reg' is one of the even / odd register pair and it's now changed |
| 234 | // (e.g. coalesced) into a different register. The other register of the |
| 235 | // pair allocation hint must be updated to reflect the relationship |
| 236 | // change. |
| 237 | unsigned OtherReg = Hint.second; |
| 238 | Hint = MRI->getRegAllocationHint(OtherReg); |
| 239 | if (Hint.second == Reg) |
| 240 | // Make sure the pair has not already divorced. |
| 241 | MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg); |
| 242 | } |
| 243 | } |
| 244 | |
Bob Wilson | f6a4d3c | 2011-04-19 18:11:45 +0000 | [diff] [blame] | 245 | bool |
| 246 | ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const { |
| 247 | // CortexA9 has a Write-after-write hazard for NEON registers. |
Silviu Baranga | 616471d | 2012-09-13 15:05:10 +0000 | [diff] [blame] | 248 | if (!STI.isLikeA9()) |
Bob Wilson | f6a4d3c | 2011-04-19 18:11:45 +0000 | [diff] [blame] | 249 | return false; |
| 250 | |
| 251 | switch (RC->getID()) { |
| 252 | case ARM::DPRRegClassID: |
| 253 | case ARM::DPR_8RegClassID: |
| 254 | case ARM::DPR_VFP2RegClassID: |
| 255 | case ARM::QPRRegClassID: |
| 256 | case ARM::QPR_8RegClassID: |
| 257 | case ARM::QPR_VFP2RegClassID: |
| 258 | case ARM::SPRRegClassID: |
| 259 | case ARM::SPR_8RegClassID: |
| 260 | // Avoid reusing S, D, and Q registers. |
| 261 | // Don't increase register pressure for QQ and QQQQ. |
| 262 | return true; |
| 263 | default: |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | |
Jim Grosbach | 65482b1 | 2010-09-03 18:37:12 +0000 | [diff] [blame] | 268 | bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const { |
Jim Grosbach | e45ab8a | 2010-01-19 18:31:11 +0000 | [diff] [blame] | 269 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 270 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jakob Stoklund Olesen | 0f9d07f | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 271 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
Jim Grosbach | 65482b1 | 2010-09-03 18:37:12 +0000 | [diff] [blame] | 272 | |
Jakob Stoklund Olesen | 0f9d07f | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 273 | // When outgoing call frames are so large that we adjust the stack pointer |
| 274 | // around the call, we can no longer use the stack pointer to reach the |
| 275 | // emergency spill slot. |
Bob Wilson | 055a812 | 2012-03-20 19:28:22 +0000 | [diff] [blame] | 276 | if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF)) |
Jim Grosbach | 65482b1 | 2010-09-03 18:37:12 +0000 | [diff] [blame] | 277 | return true; |
| 278 | |
| 279 | // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited |
| 280 | // negative range for ldr/str (255), and thumb1 is positive offsets only. |
| 281 | // It's going to be better to use the SP or Base Pointer instead. When there |
| 282 | // are variable sized objects, we can't reference off of the SP, so we |
| 283 | // reserve a Base Pointer. |
| 284 | if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) { |
| 285 | // Conservatively estimate whether the negative offset from the frame |
| 286 | // pointer will be sufficient to reach. If a function has a smallish |
| 287 | // frame, it's less likely to have lots of spills and callee saved |
| 288 | // space, so it's all more likely to be within range of the frame pointer. |
| 289 | // If it's wrong, the scavenger will still enable access to work, it just |
| 290 | // won't be optimal. |
| 291 | if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128) |
| 292 | return false; |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const { |
Jakob Stoklund Olesen | 54f3b7a | 2012-01-05 00:26:52 +0000 | [diff] [blame] | 300 | const MachineRegisterInfo *MRI = &MF.getRegInfo(); |
Chad Rosier | 6690bca | 2011-10-20 00:07:12 +0000 | [diff] [blame] | 301 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jim Grosbach | 30c93e1 | 2010-09-08 17:22:12 +0000 | [diff] [blame] | 302 | // We can't realign the stack if: |
| 303 | // 1. Dynamic stack realignment is explicitly disabled, |
Chad Rosier | 6690bca | 2011-10-20 00:07:12 +0000 | [diff] [blame] | 304 | // 2. This is a Thumb1 function (it's not useful, so we don't bother), or |
| 305 | // 3. There are VLAs in the function and the base pointer is disabled. |
Jakob Stoklund Olesen | 54f3b7a | 2012-01-05 00:26:52 +0000 | [diff] [blame] | 306 | if (!MF.getTarget().Options.RealignStack) |
| 307 | return false; |
| 308 | if (AFI->isThumb1OnlyFunction()) |
| 309 | return false; |
| 310 | // Stack realignment requires a frame pointer. If we already started |
| 311 | // register allocation with frame pointer elimination, it is too late now. |
| 312 | if (!MRI->canReserveReg(FramePtr)) |
| 313 | return false; |
Bob Wilson | aaa1e2f | 2012-03-20 19:28:25 +0000 | [diff] [blame] | 314 | // We may also need a base pointer if there are dynamic allocas or stack |
| 315 | // pointer adjustments around calls. |
| 316 | if (MF.getTarget().getFrameLowering()->hasReservedCallFrame(MF)) |
Jakob Stoklund Olesen | 54f3b7a | 2012-01-05 00:26:52 +0000 | [diff] [blame] | 317 | return true; |
Jakob Stoklund Olesen | 54f3b7a | 2012-01-05 00:26:52 +0000 | [diff] [blame] | 318 | // A base pointer is required and allowed. Check that it isn't too late to |
| 319 | // reserve it. |
| 320 | return MRI->canReserveReg(BasePtr); |
Jim Grosbach | e45ab8a | 2010-01-19 18:31:11 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 323 | bool ARMBaseRegisterInfo:: |
| 324 | needsStackRealignment(const MachineFunction &MF) const { |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 325 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Eric Christopher | d4c36ce | 2010-07-17 00:27:24 +0000 | [diff] [blame] | 326 | const Function *F = MF.getFunction(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 327 | unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment(); |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 328 | bool requiresRealignment = |
| 329 | ((MFI->getMaxAlignment() > StackAlign) || |
| 330 | F->getFnAttributes().hasAttribute(Attributes::StackAlignment)); |
Jim Grosbach | 5c33f5b | 2010-09-02 19:52:39 +0000 | [diff] [blame] | 331 | |
Eric Christopher | d4c36ce | 2010-07-17 00:27:24 +0000 | [diff] [blame] | 332 | return requiresRealignment && canRealignStack(MF); |
Jim Grosbach | 3dab277 | 2009-10-27 22:45:39 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Jim Grosbach | 9631864 | 2010-01-06 23:54:42 +0000 | [diff] [blame] | 335 | bool ARMBaseRegisterInfo:: |
| 336 | cannotEliminateFrame(const MachineFunction &MF) const { |
Evan Cheng | 98a0104 | 2009-08-14 20:48:13 +0000 | [diff] [blame] | 337 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 338 | if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack()) |
Evan Cheng | 98a0104 | 2009-08-14 20:48:13 +0000 | [diff] [blame] | 339 | return true; |
Jim Grosbach | 31bc849 | 2009-11-08 00:27:19 +0000 | [diff] [blame] | 340 | return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() |
| 341 | || needsStackRealignment(MF); |
Evan Cheng | 98a0104 | 2009-08-14 20:48:13 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Jim Grosbach | 5c33f5b | 2010-09-02 19:52:39 +0000 | [diff] [blame] | 344 | unsigned |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 345 | ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const { |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 346 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 347 | |
| 348 | if (TFI->hasFP(MF)) |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 349 | return FramePtr; |
| 350 | return ARM::SP; |
| 351 | } |
| 352 | |
| 353 | unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 354 | llvm_unreachable("What is the exception register"); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 358 | llvm_unreachable("What is the exception handler register"); |
David Goodwin | c140c48 | 2009-07-08 17:28:55 +0000 | [diff] [blame] | 359 | } |
| 360 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 361 | /// emitLoadConstPool - Emits a load from constpool to materialize the |
| 362 | /// specified immediate. |
| 363 | void ARMBaseRegisterInfo:: |
| 364 | emitLoadConstPool(MachineBasicBlock &MBB, |
| 365 | MachineBasicBlock::iterator &MBBI, |
David Goodwin | 77521f5 | 2009-07-08 20:28:28 +0000 | [diff] [blame] | 366 | DebugLoc dl, |
Evan Cheng | 3784453 | 2009-07-16 09:20:10 +0000 | [diff] [blame] | 367 | unsigned DestReg, unsigned SubIdx, int Val, |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 368 | ARMCC::CondCodes Pred, |
Anton Korobeynikov | 3daccd8 | 2011-03-05 18:43:50 +0000 | [diff] [blame] | 369 | unsigned PredReg, unsigned MIFlags) const { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 370 | MachineFunction &MF = *MBB.getParent(); |
| 371 | MachineConstantPool *ConstantPool = MF.getConstantPool(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 372 | const Constant *C = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 373 | ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 374 | unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4); |
| 375 | |
Evan Cheng | 3784453 | 2009-07-16 09:20:10 +0000 | [diff] [blame] | 376 | BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp)) |
| 377 | .addReg(DestReg, getDefRegState(true), SubIdx) |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 378 | .addConstantPoolIndex(Idx) |
Anton Korobeynikov | 3daccd8 | 2011-03-05 18:43:50 +0000 | [diff] [blame] | 379 | .addImm(0).addImm(Pred).addReg(PredReg) |
| 380 | .setMIFlags(MIFlags); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | bool ARMBaseRegisterInfo:: |
| 384 | requiresRegisterScavenging(const MachineFunction &MF) const { |
| 385 | return true; |
| 386 | } |
Jim Grosbach | 41fff8c | 2009-10-21 23:40:56 +0000 | [diff] [blame] | 387 | |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 388 | bool ARMBaseRegisterInfo:: |
Preston Gurd | 6a8c7bf | 2012-04-23 21:39:35 +0000 | [diff] [blame] | 389 | trackLivenessAfterRegAlloc(const MachineFunction &MF) const { |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | bool ARMBaseRegisterInfo:: |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 394 | requiresFrameIndexScavenging(const MachineFunction &MF) const { |
Jim Grosbach | ca5dfb7 | 2009-10-28 17:33:28 +0000 | [diff] [blame] | 395 | return true; |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 396 | } |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 397 | |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 398 | bool ARMBaseRegisterInfo:: |
| 399 | requiresVirtualBaseRegisters(const MachineFunction &MF) const { |
Jim Grosbach | c8cd8aa | 2012-12-11 23:31:12 +0000 | [diff] [blame^] | 400 | return true; |
Jim Grosbach | a273442 | 2010-08-24 19:05:43 +0000 | [diff] [blame] | 401 | } |
| 402 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 403 | static void |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 404 | emitSPUpdate(bool isARM, |
| 405 | MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
| 406 | DebugLoc dl, const ARMBaseInstrInfo &TII, |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 407 | int NumBytes, |
| 408 | ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) { |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 409 | if (isARM) |
| 410 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, |
| 411 | Pred, PredReg, TII); |
| 412 | else |
| 413 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, |
| 414 | Pred, PredReg, TII); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 417 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 418 | void ARMBaseRegisterInfo:: |
| 419 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 420 | MachineBasicBlock::iterator I) const { |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 421 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 422 | if (!TFI->hasReservedCallFrame(MF)) { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 423 | // If we have alloca, convert as follows: |
| 424 | // ADJCALLSTACKDOWN -> sub, sp, sp, amount |
| 425 | // ADJCALLSTACKUP -> add, sp, sp, amount |
| 426 | MachineInstr *Old = I; |
| 427 | DebugLoc dl = Old->getDebugLoc(); |
| 428 | unsigned Amount = Old->getOperand(0).getImm(); |
| 429 | if (Amount != 0) { |
| 430 | // We need to keep the stack aligned properly. To do this, we round the |
| 431 | // amount of space needed for the outgoing arguments up to the next |
| 432 | // alignment boundary. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 433 | unsigned Align = TFI->getStackAlignment(); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 434 | Amount = (Amount+Align-1)/Align*Align; |
| 435 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 436 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 437 | assert(!AFI->isThumb1OnlyFunction() && |
Jim Grosbach | cf453ee | 2010-02-23 17:16:27 +0000 | [diff] [blame] | 438 | "This eliminateCallFramePseudoInstr does not support Thumb1!"); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 439 | bool isARM = !AFI->isThumbFunction(); |
| 440 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 441 | // Replace the pseudo instruction with a new instruction... |
| 442 | unsigned Opc = Old->getOpcode(); |
Jim Grosbach | 4c7628e | 2010-02-22 22:47:46 +0000 | [diff] [blame] | 443 | int PIdx = Old->findFirstPredOperandIdx(); |
| 444 | ARMCC::CondCodes Pred = (PIdx == -1) |
| 445 | ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 446 | if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { |
| 447 | // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. |
| 448 | unsigned PredReg = Old->getOperand(2).getReg(); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 449 | emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 450 | } else { |
| 451 | // Note: PredReg is operand 3 for ADJCALLSTACKUP. |
| 452 | unsigned PredReg = Old->getOperand(3).getReg(); |
| 453 | assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 454 | emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
| 458 | MBB.erase(I); |
| 459 | } |
| 460 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 461 | int64_t ARMBaseRegisterInfo:: |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 462 | getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 463 | const MCInstrDesc &Desc = MI->getDesc(); |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 464 | unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); |
Chad Rosier | 90f2004 | 2012-02-22 17:25:00 +0000 | [diff] [blame] | 465 | int64_t InstrOffs = 0; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 466 | int Scale = 1; |
| 467 | unsigned ImmIdx = 0; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 468 | switch (AddrMode) { |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 469 | case ARMII::AddrModeT2_i8: |
| 470 | case ARMII::AddrModeT2_i12: |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 471 | case ARMII::AddrMode_i12: |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 472 | InstrOffs = MI->getOperand(Idx+1).getImm(); |
| 473 | Scale = 1; |
| 474 | break; |
| 475 | case ARMII::AddrMode5: { |
| 476 | // VFP address mode. |
| 477 | const MachineOperand &OffOp = MI->getOperand(Idx+1); |
Jim Grosbach | f78ee63 | 2010-08-25 19:11:34 +0000 | [diff] [blame] | 478 | InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm()); |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 479 | if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub) |
| 480 | InstrOffs = -InstrOffs; |
| 481 | Scale = 4; |
| 482 | break; |
| 483 | } |
| 484 | case ARMII::AddrMode2: { |
| 485 | ImmIdx = Idx+2; |
| 486 | InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm()); |
| 487 | if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 488 | InstrOffs = -InstrOffs; |
| 489 | break; |
| 490 | } |
| 491 | case ARMII::AddrMode3: { |
| 492 | ImmIdx = Idx+2; |
| 493 | InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm()); |
| 494 | if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub) |
| 495 | InstrOffs = -InstrOffs; |
| 496 | break; |
| 497 | } |
| 498 | case ARMII::AddrModeT1_s: { |
| 499 | ImmIdx = Idx+1; |
| 500 | InstrOffs = MI->getOperand(ImmIdx).getImm(); |
| 501 | Scale = 4; |
| 502 | break; |
| 503 | } |
| 504 | default: |
| 505 | llvm_unreachable("Unsupported addressing mode!"); |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | return InstrOffs * Scale; |
| 509 | } |
| 510 | |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 511 | /// needsFrameBaseReg - Returns true if the instruction's frame index |
| 512 | /// reference would be better served by a base register other than FP |
| 513 | /// or SP. Used by LocalStackFrameAllocation to determine which frame index |
| 514 | /// references it should create new base registers for. |
| 515 | bool ARMBaseRegisterInfo:: |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 516 | needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { |
| 517 | for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) { |
| 518 | assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!"); |
| 519 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 520 | |
| 521 | // It's the load/store FI references that cause issues, as it can be difficult |
| 522 | // to materialize the offset if it won't fit in the literal field. Estimate |
| 523 | // based on the size of the local frame and some conservative assumptions |
| 524 | // about the rest of the stack frame (note, this is pre-regalloc, so |
| 525 | // we don't know everything for certain yet) whether this offset is likely |
| 526 | // to be out of range of the immediate. Return true if so. |
| 527 | |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 528 | // We only generate virtual base registers for loads and stores, so |
| 529 | // return false for everything else. |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 530 | unsigned Opc = MI->getOpcode(); |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 531 | switch (Opc) { |
Jakob Stoklund Olesen | cff9baa | 2012-08-28 03:11:27 +0000 | [diff] [blame] | 532 | case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12: |
Jim Grosbach | 7e3383c | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 533 | case ARM::STRi12: case ARM::STRH: case ARM::STRBi12: |
Jakob Stoklund Olesen | cff9baa | 2012-08-28 03:11:27 +0000 | [diff] [blame] | 534 | case ARM::t2LDRi12: case ARM::t2LDRi8: |
| 535 | case ARM::t2STRi12: case ARM::t2STRi8: |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 536 | case ARM::VLDRS: case ARM::VLDRD: |
| 537 | case ARM::VSTRS: case ARM::VSTRD: |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 538 | case ARM::tSTRspi: case ARM::tLDRspi: |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 539 | break; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 540 | default: |
| 541 | return false; |
| 542 | } |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 543 | |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 544 | // Without a virtual base register, if the function has variable sized |
| 545 | // objects, all fixed-size local references will be via the frame pointer, |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 546 | // Approximate the offset and see if it's legal for the instruction. |
| 547 | // Note that the incoming offset is based on the SP value at function entry, |
| 548 | // so it'll be negative. |
| 549 | MachineFunction &MF = *MI->getParent()->getParent(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 550 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 551 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 552 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Jim Grosbach | cd59dc5 | 2010-08-24 18:04:52 +0000 | [diff] [blame] | 553 | |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 554 | // Estimate an offset from the frame pointer. |
| 555 | // Conservatively assume all callee-saved registers get pushed. R4-R6 |
| 556 | // will be earlier than the FP, so we ignore those. |
| 557 | // R7, LR |
| 558 | int64_t FPOffset = Offset - 8; |
| 559 | // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15 |
| 560 | if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction()) |
| 561 | FPOffset -= 80; |
| 562 | // Estimate an offset from the stack pointer. |
Jim Grosbach | c1dc78d | 2010-08-31 18:52:31 +0000 | [diff] [blame] | 563 | // The incoming offset is relating to the SP at the start of the function, |
| 564 | // but when we access the local it'll be relative to the SP after local |
| 565 | // allocation, so adjust our SP-relative offset by that allocation size. |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 566 | Offset = -Offset; |
Jim Grosbach | c1dc78d | 2010-08-31 18:52:31 +0000 | [diff] [blame] | 567 | Offset += MFI->getLocalFrameSize(); |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 568 | // Assume that we'll have at least some spill slots allocated. |
| 569 | // FIXME: This is a total SWAG number. We should run some statistics |
| 570 | // and pick a real one. |
| 571 | Offset += 128; // 128 bytes of spill slots |
| 572 | |
| 573 | // If there is a frame pointer, try using it. |
| 574 | // The FP is only available if there is no dynamic realignment. We |
| 575 | // don't know for sure yet whether we'll need that, so we guess based |
| 576 | // on whether there are any local variables that would trigger it. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 577 | unsigned StackAlign = TFI->getStackAlignment(); |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 578 | if (TFI->hasFP(MF) && |
Jim Grosbach | 3197380 | 2010-08-24 21:19:33 +0000 | [diff] [blame] | 579 | !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) { |
| 580 | if (isFrameOffsetLegal(MI, FPOffset)) |
| 581 | return false; |
| 582 | } |
| 583 | // If we can reference via the stack pointer, try that. |
| 584 | // FIXME: This (and the code that resolves the references) can be improved |
| 585 | // to only disallow SP relative references in the live range of |
| 586 | // the VLA(s). In practice, it's unclear how much difference that |
| 587 | // would make, but it may be worth doing. |
| 588 | if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset)) |
| 589 | return false; |
| 590 | |
| 591 | // 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] | 592 | return true; |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 595 | /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to |
| 596 | /// be a pointer to FrameIdx at the beginning of the basic block. |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 597 | void ARMBaseRegisterInfo:: |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 598 | materializeFrameBaseRegister(MachineBasicBlock *MBB, |
| 599 | unsigned BaseReg, int FrameIdx, |
| 600 | int64_t Offset) const { |
| 601 | ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>(); |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 602 | unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : |
| 603 | (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 604 | |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 605 | MachineBasicBlock::iterator Ins = MBB->begin(); |
| 606 | DebugLoc DL; // Defaults to "unknown" |
| 607 | if (Ins != MBB->end()) |
| 608 | DL = Ins->getDebugLoc(); |
| 609 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 610 | const MCInstrDesc &MCID = TII.get(ADDriOpc); |
Cameron Zwarich | 2180372 | 2011-05-19 02:18:27 +0000 | [diff] [blame] | 611 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 612 | const MachineFunction &MF = *MBB->getParent(); |
| 613 | MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); |
Cameron Zwarich | 2180372 | 2011-05-19 02:18:27 +0000 | [diff] [blame] | 614 | |
Jim Grosbach | 5b81584 | 2011-08-24 17:46:13 +0000 | [diff] [blame] | 615 | MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg) |
| 616 | .addFrameIndex(FrameIdx).addImm(Offset)); |
Bill Wendling | 976ef86 | 2010-12-17 23:09:14 +0000 | [diff] [blame] | 617 | |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 618 | if (!AFI->isThumb1OnlyFunction()) |
Jim Grosbach | 5b81584 | 2011-08-24 17:46:13 +0000 | [diff] [blame] | 619 | AddDefaultCC(MIB); |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void |
| 623 | ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I, |
| 624 | unsigned BaseReg, int64_t Offset) const { |
| 625 | MachineInstr &MI = *I; |
| 626 | MachineBasicBlock &MBB = *MI.getParent(); |
| 627 | MachineFunction &MF = *MBB.getParent(); |
| 628 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 629 | int Off = Offset; // ARM doesn't need the general 64-bit offsets |
| 630 | unsigned i = 0; |
| 631 | |
| 632 | assert(!AFI->isThumb1OnlyFunction() && |
| 633 | "This resolveFrameIndex does not support Thumb1!"); |
| 634 | |
| 635 | while (!MI.getOperand(i).isFI()) { |
| 636 | ++i; |
| 637 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 638 | } |
| 639 | bool Done = false; |
| 640 | if (!AFI->isThumbFunction()) |
| 641 | Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII); |
| 642 | else { |
| 643 | assert(AFI->isThumb2Function()); |
| 644 | Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII); |
| 645 | } |
| 646 | assert (Done && "Unable to resolve frame index!"); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 647 | (void)Done; |
Jim Grosbach | dc140c6 | 2010-08-17 22:41:55 +0000 | [diff] [blame] | 648 | } |
Jim Grosbach | 8708ead | 2010-08-17 18:13:53 +0000 | [diff] [blame] | 649 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 650 | bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, |
| 651 | int64_t Offset) const { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 652 | const MCInstrDesc &Desc = MI->getDesc(); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 653 | unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); |
| 654 | unsigned i = 0; |
| 655 | |
| 656 | while (!MI->getOperand(i).isFI()) { |
| 657 | ++i; |
| 658 | assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!"); |
| 659 | } |
| 660 | |
| 661 | // AddrMode4 and AddrMode6 cannot handle any offset. |
| 662 | if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6) |
| 663 | return Offset == 0; |
| 664 | |
| 665 | unsigned NumBits = 0; |
| 666 | unsigned Scale = 1; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 667 | bool isSigned = true; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 668 | switch (AddrMode) { |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 669 | case ARMII::AddrModeT2_i8: |
| 670 | case ARMII::AddrModeT2_i12: |
| 671 | // i8 supports only negative, and i12 supports only positive, so |
| 672 | // based on Offset sign, consider the appropriate instruction |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 673 | Scale = 1; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 674 | if (Offset < 0) { |
| 675 | NumBits = 8; |
| 676 | Offset = -Offset; |
| 677 | } else { |
| 678 | NumBits = 12; |
| 679 | } |
| 680 | break; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 681 | case ARMII::AddrMode5: |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 682 | // VFP address mode. |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 683 | NumBits = 8; |
| 684 | Scale = 4; |
| 685 | break; |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 686 | case ARMII::AddrMode_i12: |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 687 | case ARMII::AddrMode2: |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 688 | NumBits = 12; |
| 689 | break; |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 690 | case ARMII::AddrMode3: |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 691 | NumBits = 8; |
| 692 | break; |
Bill Wendling | e575499 | 2011-10-11 21:40:47 +0000 | [diff] [blame] | 693 | case ARMII::AddrModeT1_s: |
| 694 | NumBits = 5; |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 695 | Scale = 4; |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 696 | isSigned = false; |
Jim Grosbach | 74d7b0a | 2010-08-19 17:52:13 +0000 | [diff] [blame] | 697 | break; |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 698 | default: |
| 699 | llvm_unreachable("Unsupported addressing mode!"); |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Jim Grosbach | 1ab3f16 | 2010-08-26 21:56:30 +0000 | [diff] [blame] | 702 | Offset += getFrameIndexInstrOffset(MI, i); |
Jim Grosbach | d4511e9 | 2010-08-31 18:49:31 +0000 | [diff] [blame] | 703 | // Make sure the offset is encodable for instructions that scale the |
| 704 | // immediate. |
| 705 | if ((Offset & (Scale-1)) != 0) |
| 706 | return false; |
| 707 | |
Jim Grosbach | e2f5569 | 2010-08-19 23:52:25 +0000 | [diff] [blame] | 708 | if (isSigned && Offset < 0) |
Jim Grosbach | 2b1e202 | 2010-08-18 22:44:49 +0000 | [diff] [blame] | 709 | Offset = -Offset; |
| 710 | |
| 711 | unsigned Mask = (1 << NumBits) - 1; |
| 712 | if ((unsigned)Offset <= Mask * Scale) |
| 713 | return true; |
Jim Grosbach | 74d803a | 2010-08-18 17:57:37 +0000 | [diff] [blame] | 714 | |
| 715 | return false; |
| 716 | } |
| 717 | |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 718 | void |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 719 | ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 720 | int SPAdj, RegScavenger *RS) const { |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 721 | unsigned i = 0; |
| 722 | MachineInstr &MI = *II; |
| 723 | MachineBasicBlock &MBB = *MI.getParent(); |
| 724 | MachineFunction &MF = *MBB.getParent(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 725 | const ARMFrameLowering *TFI = |
| 726 | static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering()); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 727 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 728 | assert(!AFI->isThumb1OnlyFunction() && |
Bob Wilson | a15de00 | 2009-09-18 21:42:44 +0000 | [diff] [blame] | 729 | "This eliminateFrameIndex does not support Thumb1!"); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 730 | |
| 731 | while (!MI.getOperand(i).isFI()) { |
| 732 | ++i; |
| 733 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 734 | } |
| 735 | |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 736 | int FrameIndex = MI.getOperand(i).getIndex(); |
Jim Grosbach | a37aa54 | 2009-11-22 20:05:32 +0000 | [diff] [blame] | 737 | unsigned FrameReg; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 738 | |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 739 | int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 740 | |
Jakob Stoklund Olesen | 0f9d07f | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 741 | // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the |
| 742 | // call frame setup/destroy instructions have already been eliminated. That |
| 743 | // means the stack pointer cannot be used to access the emergency spill slot |
| 744 | // when !hasReservedCallFrame(). |
| 745 | #ifndef NDEBUG |
| 746 | if (RS && FrameReg == ARM::SP && FrameIndex == RS->getScavengingFrameIndex()){ |
| 747 | assert(TFI->hasReservedCallFrame(MF) && |
| 748 | "Cannot use SP to access the emergency spill slot in " |
| 749 | "functions without a reserved call frame"); |
| 750 | assert(!MF.getFrameInfo()->hasVarSizedObjects() && |
| 751 | "Cannot use SP to access the emergency spill slot in " |
| 752 | "functions with variable sized frame objects"); |
| 753 | } |
| 754 | #endif // NDEBUG |
| 755 | |
Evan Cheng | 62b5065 | 2010-04-26 07:39:25 +0000 | [diff] [blame] | 756 | // Special handling of dbg_value instructions. |
| 757 | if (MI.isDebugValue()) { |
| 758 | MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/); |
| 759 | MI.getOperand(i+1).ChangeToImmediate(Offset); |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 760 | return; |
Evan Cheng | 62b5065 | 2010-04-26 07:39:25 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Evan Cheng | 48d8afa | 2009-11-01 21:12:51 +0000 | [diff] [blame] | 763 | // Modify MI as necessary to handle as much of 'Offset' as possible |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 764 | bool Done = false; |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 765 | if (!AFI->isThumbFunction()) |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 766 | Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 767 | else { |
| 768 | assert(AFI->isThumb2Function()); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 769 | Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 770 | } |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 771 | if (Done) |
Jim Grosbach | fcb4a8e | 2010-08-26 23:32:16 +0000 | [diff] [blame] | 772 | return; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 773 | |
| 774 | // If we get here, the immediate doesn't fit into the instruction. We folded |
| 775 | // as much as possible above, handle the rest, providing a register that is |
| 776 | // SP+LargeImm. |
Daniel Dunbar | 19bb87d | 2009-08-28 08:08:22 +0000 | [diff] [blame] | 777 | assert((Offset || |
Jim Grosbach | a443217 | 2009-11-15 21:45:34 +0000 | [diff] [blame] | 778 | (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 || |
| 779 | (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) && |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 780 | "This code isn't needed if offset already handled!"); |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 781 | |
Jim Grosbach | 7e831db | 2009-10-20 01:26:58 +0000 | [diff] [blame] | 782 | unsigned ScratchReg = 0; |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 783 | int PIdx = MI.findFirstPredOperandIdx(); |
| 784 | ARMCC::CondCodes Pred = (PIdx == -1) |
| 785 | ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm(); |
| 786 | unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg(); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 787 | if (Offset == 0) |
Jim Grosbach | a443217 | 2009-11-15 21:45:34 +0000 | [diff] [blame] | 788 | // Must be addrmode4/6. |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 789 | MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 790 | else { |
Craig Topper | 420761a | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 791 | ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass); |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 792 | if (!AFI->isThumbFunction()) |
| 793 | emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, |
| 794 | Offset, Pred, PredReg, TII); |
| 795 | else { |
| 796 | assert(AFI->isThumb2Function()); |
| 797 | emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, |
| 798 | Offset, Pred, PredReg, TII); |
| 799 | } |
Jim Grosbach | cde3129 | 2010-12-09 01:22:13 +0000 | [diff] [blame] | 800 | // Update the original instruction to use the scratch register. |
Evan Cheng | cdbb3f5 | 2009-08-27 01:23:50 +0000 | [diff] [blame] | 801 | MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true); |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 802 | } |
David Goodwin | db5a71a | 2009-07-08 18:31:39 +0000 | [diff] [blame] | 803 | } |