Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +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 | // |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the ARM implementation of TargetFrameLowering class. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "ARMFrameLowering.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 15 | #include "ARMBaseInstrInfo.h" |
Evan Cheng | e45d685 | 2011-01-11 21:46:47 +0000 | [diff] [blame] | 16 | #include "ARMBaseRegisterInfo.h" |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 17 | #include "ARMConstantPoolValue.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | a20cde3 | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/ARMAddressingModes.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/RegisterScavenging.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CallingConv.h" |
| 27 | #include "llvm/IR/Function.h" |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Benjamin Kramer | 9fceb90 | 2012-02-24 22:09:25 +0000 | [diff] [blame] | 34 | static cl::opt<bool> |
Jakob Stoklund Olesen | 68a922c | 2012-01-06 22:19:37 +0000 | [diff] [blame] | 35 | SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true), |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 36 | cl::desc("Align ARM NEON spills in prolog and epilog")); |
| 37 | |
| 38 | static MachineBasicBlock::iterator |
| 39 | skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, |
| 40 | unsigned NumAlignedDPRCS2Regs); |
| 41 | |
Eric Christopher | 45fb7b6 | 2014-06-26 19:29:59 +0000 | [diff] [blame] | 42 | ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti) |
| 43 | : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4), |
| 44 | STI(sti) {} |
| 45 | |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 46 | /// hasFP - Return true if the specified function should have a dedicated frame |
| 47 | /// pointer register. This is true if the function has variable sized allocas |
| 48 | /// or if frame pointer elimination is disabled. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 49 | bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 50 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 51 | |
Evan Cheng | 801d98b | 2012-01-04 01:55:04 +0000 | [diff] [blame] | 52 | // iOS requires FP not to be clobbered for backtracing purpose. |
| 53 | if (STI.isTargetIOS()) |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 54 | return true; |
| 55 | |
| 56 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 57 | // Always eliminate non-leaf frame pointers. |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 58 | return ((MF.getTarget().Options.DisableFramePointerElim(MF) && |
| 59 | MFI->hasCalls()) || |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 60 | RegInfo->needsStackRealignment(MF) || |
| 61 | MFI->hasVarSizedObjects() || |
| 62 | MFI->isFrameAddressTaken()); |
| 63 | } |
| 64 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 65 | /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 66 | /// not required, we reserve argument space for call sites in the function |
| 67 | /// immediately on entry to the current function. This eliminates the need for |
| 68 | /// add/sub sp brackets around call sites. Returns true if the call frame is |
| 69 | /// included as part of the stack frame. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 70 | bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 71 | const MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 72 | unsigned CFSize = FFI->getMaxCallFrameSize(); |
| 73 | // It's not always a good idea to include the call frame as part of the |
| 74 | // stack frame. ARM (especially Thumb) has small immediate offset to |
| 75 | // address the stack frame. So a large call frame can cause poor codegen |
| 76 | // and may even makes it impossible to scavenge a register. |
| 77 | if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 |
| 78 | return false; |
| 79 | |
| 80 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 81 | } |
| 82 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 83 | /// canSimplifyCallFramePseudos - If there is a reserved call frame, the |
| 84 | /// call frame pseudos can be simplified. Unlike most targets, having a FP |
| 85 | /// is not sufficient here since we still may reference some objects via SP |
| 86 | /// even when FP is available in Thumb2 mode. |
| 87 | bool |
| 88 | ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 89 | return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); |
| 90 | } |
| 91 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 92 | static bool isCSRestore(MachineInstr *MI, |
| 93 | const ARMBaseInstrInfo &TII, |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 94 | const MCPhysReg *CSRegs) { |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 95 | // Integer spill area is handled with "pop". |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 96 | if (isPopOpcode(MI->getOpcode())) { |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 97 | // The first two operands are predicates. The last two are |
| 98 | // imp-def and imp-use of SP. Check everything in between. |
| 99 | for (int i = 5, e = MI->getNumOperands(); i != e; ++i) |
| 100 | if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) |
| 101 | return false; |
| 102 | return true; |
| 103 | } |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 104 | if ((MI->getOpcode() == ARM::LDR_POST_IMM || |
| 105 | MI->getOpcode() == ARM::LDR_POST_REG || |
Jim Grosbach | bdb7ed1 | 2010-12-10 18:41:15 +0000 | [diff] [blame] | 106 | MI->getOpcode() == ARM::t2LDR_POST) && |
| 107 | isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && |
| 108 | MI->getOperand(1).getReg() == ARM::SP) |
| 109 | return true; |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 110 | |
| 111 | return false; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 114 | static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB, |
| 115 | MachineBasicBlock::iterator &MBBI, DebugLoc dl, |
| 116 | const ARMBaseInstrInfo &TII, unsigned DestReg, |
| 117 | unsigned SrcReg, int NumBytes, |
| 118 | unsigned MIFlags = MachineInstr::NoFlags, |
| 119 | ARMCC::CondCodes Pred = ARMCC::AL, |
| 120 | unsigned PredReg = 0) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 121 | if (isARM) |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 122 | emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 123 | Pred, PredReg, TII, MIFlags); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 124 | else |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 125 | emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 126 | Pred, PredReg, TII, MIFlags); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 129 | static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB, |
| 130 | MachineBasicBlock::iterator &MBBI, DebugLoc dl, |
| 131 | const ARMBaseInstrInfo &TII, int NumBytes, |
| 132 | unsigned MIFlags = MachineInstr::NoFlags, |
| 133 | ARMCC::CondCodes Pred = ARMCC::AL, |
| 134 | unsigned PredReg = 0) { |
| 135 | emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes, |
| 136 | MIFlags, Pred, PredReg); |
| 137 | } |
| 138 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 139 | static int sizeOfSPAdjustment(const MachineInstr *MI) { |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 140 | int RegSize; |
| 141 | switch (MI->getOpcode()) { |
| 142 | case ARM::VSTMDDB_UPD: |
| 143 | RegSize = 8; |
| 144 | break; |
| 145 | case ARM::STMDB_UPD: |
| 146 | case ARM::t2STMDB_UPD: |
| 147 | RegSize = 4; |
| 148 | break; |
| 149 | case ARM::t2STR_PRE: |
| 150 | case ARM::STR_PRE_IMM: |
| 151 | return 4; |
| 152 | default: |
| 153 | llvm_unreachable("Unknown push or pop like instruction"); |
| 154 | } |
| 155 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 156 | int count = 0; |
| 157 | // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ |
| 158 | // pred) so the list starts at 4. |
| 159 | for (int i = MI->getNumOperands() - 1; i >= 4; --i) |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 160 | count += RegSize; |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 161 | return count; |
| 162 | } |
| 163 | |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 164 | static bool WindowsRequiresStackProbe(const MachineFunction &MF, |
| 165 | size_t StackSizeInBytes) { |
| 166 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 167 | if (MFI->getStackProtectorIndex() > 0) |
| 168 | return StackSizeInBytes >= 4080; |
| 169 | return StackSizeInBytes >= 4096; |
| 170 | } |
| 171 | |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 172 | namespace { |
| 173 | struct StackAdjustingInsts { |
| 174 | struct InstInfo { |
| 175 | MachineBasicBlock::iterator I; |
| 176 | unsigned SPAdjust; |
| 177 | bool BeforeFPSet; |
| 178 | }; |
| 179 | |
| 180 | SmallVector<InstInfo, 4> Insts; |
| 181 | |
| 182 | void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust, |
| 183 | bool BeforeFPSet = false) { |
| 184 | InstInfo Info = {I, SPAdjust, BeforeFPSet}; |
| 185 | Insts.push_back(Info); |
| 186 | } |
| 187 | |
| 188 | void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) { |
| 189 | auto Info = std::find_if(Insts.begin(), Insts.end(), |
| 190 | [&](InstInfo &Info) { return Info.I == I; }); |
| 191 | assert(Info != Insts.end() && "invalid sp adjusting instruction"); |
| 192 | Info->SPAdjust += ExtraBytes; |
| 193 | } |
| 194 | |
| 195 | void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB, |
| 196 | DebugLoc dl, const ARMBaseInstrInfo &TII, bool HasFP) { |
| 197 | unsigned CFAOffset = 0; |
| 198 | for (auto &Info : Insts) { |
| 199 | if (HasFP && !Info.BeforeFPSet) |
| 200 | return; |
| 201 | |
| 202 | CFAOffset -= Info.SPAdjust; |
| 203 | unsigned CFIIndex = MMI.addFrameInst( |
| 204 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 205 | BuildMI(MBB, std::next(Info.I), dl, |
Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 206 | TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 207 | .addCFIIndex(CFIIndex) |
| 208 | .setMIFlags(MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | }; |
| 212 | } |
| 213 | |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 214 | /// Emit an instruction sequence that will align the address in |
| 215 | /// register Reg by zero-ing out the lower bits. For versions of the |
| 216 | /// architecture that support Neon, this must be done in a single |
| 217 | /// instruction, since skipAlignedDPRCS2Spills assumes it is done in a |
| 218 | /// single instruction. That function only gets called when optimizing |
| 219 | /// spilling of D registers on a core with the Neon instruction set |
| 220 | /// present. |
| 221 | static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI, |
| 222 | const TargetInstrInfo &TII, |
| 223 | MachineBasicBlock &MBB, |
| 224 | MachineBasicBlock::iterator MBBI, |
| 225 | DebugLoc DL, const unsigned Reg, |
| 226 | const unsigned Alignment, |
| 227 | const bool MustBeSingleInstruction) { |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 228 | const ARMSubtarget &AST = |
| 229 | static_cast<const ARMSubtarget &>(MF.getSubtarget()); |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 230 | const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops(); |
| 231 | const unsigned AlignMask = Alignment - 1; |
| 232 | const unsigned NrBitsToZero = countTrailingZeros(Alignment); |
| 233 | assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported"); |
| 234 | if (!AFI->isThumbFunction()) { |
| 235 | // if the BFC instruction is available, use that to zero the lower |
| 236 | // bits: |
| 237 | // bfc Reg, #0, log2(Alignment) |
| 238 | // otherwise use BIC, if the mask to zero the required number of bits |
| 239 | // can be encoded in the bic immediate field |
| 240 | // bic Reg, Reg, Alignment-1 |
| 241 | // otherwise, emit |
| 242 | // lsr Reg, Reg, log2(Alignment) |
| 243 | // lsl Reg, Reg, log2(Alignment) |
| 244 | if (CanUseBFC) { |
| 245 | AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg) |
| 246 | .addReg(Reg, RegState::Kill) |
| 247 | .addImm(~AlignMask)); |
| 248 | } else if (AlignMask <= 255) { |
| 249 | AddDefaultCC( |
| 250 | AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg) |
| 251 | .addReg(Reg, RegState::Kill) |
| 252 | .addImm(AlignMask))); |
| 253 | } else { |
| 254 | assert(!MustBeSingleInstruction && |
| 255 | "Shouldn't call emitAligningInstructions demanding a single " |
| 256 | "instruction to be emitted for large stack alignment for a target " |
| 257 | "without BFC."); |
| 258 | AddDefaultCC(AddDefaultPred( |
| 259 | BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg) |
| 260 | .addReg(Reg, RegState::Kill) |
| 261 | .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero)))); |
| 262 | AddDefaultCC(AddDefaultPred( |
| 263 | BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg) |
| 264 | .addReg(Reg, RegState::Kill) |
| 265 | .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero)))); |
| 266 | } |
| 267 | } else { |
| 268 | // Since this is only reached for Thumb-2 targets, the BFC instruction |
| 269 | // should always be available. |
| 270 | assert(CanUseBFC); |
| 271 | AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg) |
| 272 | .addReg(Reg, RegState::Kill) |
| 273 | .addImm(~AlignMask)); |
| 274 | } |
| 275 | } |
| 276 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 277 | void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 278 | MachineBasicBlock &MBB = MF.front(); |
| 279 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 280 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 281 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 282 | MachineModuleInfo &MMI = MF.getMMI(); |
| 283 | MCContext &Context = MMI.getContext(); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 284 | const TargetMachine &TM = MF.getTarget(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 285 | const MCRegisterInfo *MRI = Context.getRegisterInfo(); |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 286 | const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo(); |
| 287 | const ARMBaseInstrInfo &TII = *STI.getInstrInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 288 | assert(!AFI->isThumb1OnlyFunction() && |
| 289 | "This emitPrologue does not support Thumb1!"); |
| 290 | bool isARM = !AFI->isThumbFunction(); |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 291 | unsigned Align = STI.getFrameLowering()->getStackAlignment(); |
Stepan Dyatkovskiy | d0e34a2 | 2013-05-20 08:01:34 +0000 | [diff] [blame] | 292 | unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 293 | unsigned NumBytes = MFI->getStackSize(); |
| 294 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 295 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 296 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 297 | |
| 298 | // Determine the sizes of each callee-save spill areas and record which frame |
| 299 | // belongs to which callee-save spill areas. |
| 300 | unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; |
| 301 | int FramePtrSpillFI = 0; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 302 | int D8SpillFI = 0; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 303 | |
Jakob Stoklund Olesen | e380183 | 2012-10-26 21:46:57 +0000 | [diff] [blame] | 304 | // All calls are tail calls in GHC calling conv, and functions have no |
| 305 | // prologue/epilogue. |
Eric Christopher | b332236 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 306 | if (MF.getFunction()->getCallingConv() == CallingConv::GHC) |
| 307 | return; |
| 308 | |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 309 | StackAdjustingInsts DefCFAOffsetCandidates; |
| 310 | |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 311 | // Allocate the vararg register save area. |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 312 | if (ArgRegsSaveSize) { |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 313 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 314 | MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 315 | DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 316 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 317 | |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 318 | if (!AFI->hasStackFrame() && |
| 319 | (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) { |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 320 | if (NumBytes - ArgRegsSaveSize != 0) { |
| 321 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize), |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 322 | MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 323 | DefCFAOffsetCandidates.addInst(std::prev(MBBI), |
| 324 | NumBytes - ArgRegsSaveSize, true); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 325 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 329 | // Determine spill area sizes. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 330 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 331 | unsigned Reg = CSI[i].getReg(); |
| 332 | int FI = CSI[i].getFrameIdx(); |
| 333 | switch (Reg) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 334 | case ARM::R8: |
| 335 | case ARM::R9: |
| 336 | case ARM::R10: |
| 337 | case ARM::R11: |
| 338 | case ARM::R12: |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 339 | if (STI.isTargetDarwin()) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 340 | GPRCS2Size += 4; |
| 341 | break; |
| 342 | } |
| 343 | // fallthrough |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 344 | case ARM::R0: |
| 345 | case ARM::R1: |
| 346 | case ARM::R2: |
| 347 | case ARM::R3: |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 348 | case ARM::R4: |
| 349 | case ARM::R5: |
| 350 | case ARM::R6: |
| 351 | case ARM::R7: |
| 352 | case ARM::LR: |
| 353 | if (Reg == FramePtr) |
| 354 | FramePtrSpillFI = FI; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 355 | GPRCS1Size += 4; |
| 356 | break; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 357 | default: |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 358 | // This is a DPR. Exclude the aligned DPRCS2 spills. |
| 359 | if (Reg == ARM::D8) |
| 360 | D8SpillFI = FI; |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 361 | if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 362 | DPRCSSize += 8; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 365 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 366 | // Move past area 1. |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 367 | MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push; |
| 368 | if (GPRCS1Size > 0) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 369 | GPRCS1Push = LastPush = MBBI++; |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 370 | DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true); |
| 371 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 372 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 373 | // Determine starting offsets of spill areas. |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 374 | bool HasFP = hasFP(MF); |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 375 | unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size; |
| 376 | unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size; |
| 377 | unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U; |
| 378 | unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign; |
| 379 | unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize; |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 380 | int FramePtrOffsetInPush = 0; |
| 381 | if (HasFP) { |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 382 | FramePtrOffsetInPush = |
| 383 | MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 384 | AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + |
| 385 | NumBytes); |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 386 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 387 | AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); |
| 388 | AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); |
| 389 | AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); |
| 390 | |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 391 | // Move past area 2. |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 392 | if (GPRCS2Size > 0) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 393 | GPRCS2Push = LastPush = MBBI++; |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 394 | DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size); |
| 395 | } |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 396 | |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 397 | // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our |
| 398 | // .cfi_offset operations will reflect that. |
| 399 | if (DPRGapSize) { |
| 400 | assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs"); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 401 | if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize)) |
| 402 | DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize); |
| 403 | else { |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 404 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize, |
| 405 | MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 406 | DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize); |
| 407 | } |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 410 | // Move past area 3. |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 411 | if (DPRCSSize > 0) { |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 412 | // Since vpush register list cannot have gaps, there may be multiple vpush |
Evan Cheng | a921dc5 | 2011-02-25 01:29:29 +0000 | [diff] [blame] | 413 | // instructions in the prologue. |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 414 | while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) { |
| 415 | DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI)); |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 416 | LastPush = MBBI++; |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 417 | } |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 418 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 419 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 420 | // Move past the aligned DPRCS2 area. |
| 421 | if (AFI->getNumAlignedDPRCS2Regs() > 0) { |
| 422 | MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs()); |
| 423 | // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and |
| 424 | // leaves the stack pointer pointing to the DPRCS2 area. |
| 425 | // |
| 426 | // Adjust NumBytes to represent the stack slots below the DPRCS2 area. |
| 427 | NumBytes += MFI->getObjectOffset(D8SpillFI); |
| 428 | } else |
| 429 | NumBytes = DPRCSOffset; |
| 430 | |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 431 | if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) { |
| 432 | uint32_t NumWords = NumBytes >> 2; |
| 433 | |
| 434 | if (NumWords < 65536) |
| 435 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 436 | .addImm(NumWords) |
| 437 | .setMIFlags(MachineInstr::FrameSetup)); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 438 | else |
| 439 | BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 440 | .addImm(NumWords) |
| 441 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 442 | |
| 443 | switch (TM.getCodeModel()) { |
| 444 | case CodeModel::Small: |
| 445 | case CodeModel::Medium: |
| 446 | case CodeModel::Default: |
| 447 | case CodeModel::Kernel: |
| 448 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL)) |
| 449 | .addImm((unsigned)ARMCC::AL).addReg(0) |
| 450 | .addExternalSymbol("__chkstk") |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 451 | .addReg(ARM::R4, RegState::Implicit) |
| 452 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 453 | break; |
| 454 | case CodeModel::Large: |
Saleem Abdulrasool | 7158303 | 2014-05-01 04:19:59 +0000 | [diff] [blame] | 455 | case CodeModel::JITDefault: |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 456 | BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 457 | .addExternalSymbol("__chkstk") |
| 458 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 7158303 | 2014-05-01 04:19:59 +0000 | [diff] [blame] | 459 | |
Saleem Abdulrasool | acd0338 | 2014-05-07 03:03:27 +0000 | [diff] [blame] | 460 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr)) |
| 461 | .addImm((unsigned)ARMCC::AL).addReg(0) |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 462 | .addReg(ARM::R12, RegState::Kill) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 463 | .addReg(ARM::R4, RegState::Implicit) |
| 464 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 467 | |
| 468 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), |
| 469 | ARM::SP) |
| 470 | .addReg(ARM::SP, RegState::Define) |
| 471 | .addReg(ARM::R4, RegState::Kill) |
| 472 | .setMIFlags(MachineInstr::FrameSetup))); |
| 473 | NumBytes = 0; |
| 474 | } |
| 475 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 476 | if (NumBytes) { |
| 477 | // Adjust SP after all the callee-save spills. |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 478 | if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes)) |
| 479 | DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes); |
| 480 | else { |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 481 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, |
| 482 | MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 483 | DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes); |
| 484 | } |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 485 | |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 486 | if (HasFP && isARM) |
| 487 | // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 |
| 488 | // Note it's not safe to do this in Thumb2 mode because it would have |
| 489 | // taken two instructions: |
| 490 | // mov sp, r7 |
| 491 | // sub sp, #24 |
| 492 | // If an interrupt is taken between the two instructions, then sp is in |
| 493 | // an inconsistent state (pointing to the middle of callee-saved area). |
| 494 | // The interrupt handler can end up clobbering the registers. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 495 | AFI->setShouldRestoreSPFromFP(true); |
| 496 | } |
| 497 | |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 498 | // Set FP to point to the stack slot that contains the previous FP. |
| 499 | // For iOS, FP is R7, which has now been stored in spill area 1. |
| 500 | // Otherwise, if this is not iOS, all the callee-saved registers go |
| 501 | // into spill area 1, including the FP in R11. In either case, it |
| 502 | // is in area one and the adjustment needs to take place just after |
| 503 | // that push. |
| 504 | if (HasFP) { |
| 505 | MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push); |
| 506 | unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push); |
| 507 | emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush, |
| 508 | dl, TII, FramePtr, ARM::SP, |
| 509 | PushSize + FramePtrOffsetInPush, |
| 510 | MachineInstr::FrameSetup); |
| 511 | if (FramePtrOffsetInPush + PushSize != 0) { |
| 512 | unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa( |
| 513 | nullptr, MRI->getDwarfRegNum(FramePtr, true), |
| 514 | -(ArgRegsSaveSize - FramePtrOffsetInPush))); |
| 515 | BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 516 | .addCFIIndex(CFIIndex) |
| 517 | .setMIFlags(MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 518 | } else { |
| 519 | unsigned CFIIndex = |
| 520 | MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister( |
| 521 | nullptr, MRI->getDwarfRegNum(FramePtr, true))); |
| 522 | BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 523 | .addCFIIndex(CFIIndex) |
| 524 | .setMIFlags(MachineInstr::FrameSetup); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | // Now that the prologue's actual instructions are finalised, we can insert |
| 529 | // the necessary DWARF cf instructions to describe the situation. Start by |
| 530 | // recording where each register ended up: |
| 531 | if (GPRCS1Size > 0) { |
| 532 | MachineBasicBlock::iterator Pos = std::next(GPRCS1Push); |
| 533 | int CFIIndex; |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 534 | for (const auto &Entry : CSI) { |
| 535 | unsigned Reg = Entry.getReg(); |
| 536 | int FI = Entry.getFrameIdx(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 537 | switch (Reg) { |
| 538 | case ARM::R8: |
| 539 | case ARM::R9: |
| 540 | case ARM::R10: |
| 541 | case ARM::R11: |
| 542 | case ARM::R12: |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 543 | if (STI.isTargetDarwin()) |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 544 | break; |
| 545 | // fallthrough |
| 546 | case ARM::R0: |
| 547 | case ARM::R1: |
| 548 | case ARM::R2: |
| 549 | case ARM::R3: |
| 550 | case ARM::R4: |
| 551 | case ARM::R5: |
| 552 | case ARM::R6: |
| 553 | case ARM::R7: |
| 554 | case ARM::LR: |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 555 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 556 | nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI))); |
| 557 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 558 | .addCFIIndex(CFIIndex) |
| 559 | .setMIFlags(MachineInstr::FrameSetup); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 565 | if (GPRCS2Size > 0) { |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 566 | MachineBasicBlock::iterator Pos = std::next(GPRCS2Push); |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 567 | for (const auto &Entry : CSI) { |
| 568 | unsigned Reg = Entry.getReg(); |
| 569 | int FI = Entry.getFrameIdx(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 570 | switch (Reg) { |
| 571 | case ARM::R8: |
| 572 | case ARM::R9: |
| 573 | case ARM::R10: |
| 574 | case ARM::R11: |
| 575 | case ARM::R12: |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 576 | if (STI.isTargetDarwin()) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 577 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 578 | unsigned Offset = MFI->getObjectOffset(FI); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 579 | unsigned CFIIndex = MMI.addFrameInst( |
| 580 | MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); |
| 581 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 582 | .addCFIIndex(CFIIndex) |
| 583 | .setMIFlags(MachineInstr::FrameSetup); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 584 | } |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (DPRCSSize > 0) { |
| 591 | // Since vpush register list cannot have gaps, there may be multiple vpush |
| 592 | // instructions in the prologue. |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 593 | MachineBasicBlock::iterator Pos = std::next(LastPush); |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 594 | for (const auto &Entry : CSI) { |
| 595 | unsigned Reg = Entry.getReg(); |
| 596 | int FI = Entry.getFrameIdx(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 597 | if ((Reg >= ARM::D0 && Reg <= ARM::D31) && |
| 598 | (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) { |
| 599 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
| 600 | unsigned Offset = MFI->getObjectOffset(FI); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 601 | unsigned CFIIndex = MMI.addFrameInst( |
| 602 | MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 603 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 604 | .addCFIIndex(CFIIndex) |
| 605 | .setMIFlags(MachineInstr::FrameSetup); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
Tim Northover | 603d316 | 2014-11-14 22:45:33 +0000 | [diff] [blame] | 610 | // Now we can emit descriptions of where the canonical frame address was |
| 611 | // throughout the process. If we have a frame pointer, it takes over the job |
| 612 | // half-way through, so only the first few .cfi_def_cfa_offset instructions |
| 613 | // actually get emitted. |
| 614 | DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP); |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 615 | |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 616 | if (STI.isTargetELF() && hasFP(MF)) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 617 | MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - |
| 618 | AFI->getFramePtrSpillOffset()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 619 | |
| 620 | AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); |
| 621 | AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 622 | AFI->setDPRCalleeSavedGapSize(DPRGapSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 623 | AFI->setDPRCalleeSavedAreaSize(DPRCSSize); |
| 624 | |
| 625 | // If we need dynamic stack realignment, do it here. Be paranoid and make |
| 626 | // sure if we also have VLAs, we have a base pointer for frame access. |
Jakob Stoklund Olesen | 103318e | 2011-12-24 04:17:01 +0000 | [diff] [blame] | 627 | // If aligned NEON registers were spilled, the stack has already been |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 628 | // realigned. |
| 629 | if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 630 | unsigned MaxAlign = MFI->getMaxAlignment(); |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 631 | assert(!AFI->isThumb1OnlyFunction()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 632 | if (!AFI->isThumbFunction()) { |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 633 | emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign, |
| 634 | false); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 635 | } else { |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 636 | // We cannot use sp as source/dest register here, thus we're using r4 to |
| 637 | // perform the calculations. We're emitting the following sequence: |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 638 | // mov r4, sp |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 639 | // -- use emitAligningInstructions to produce best sequence to zero |
| 640 | // -- out lower bits in r4 |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 641 | // mov sp, r4 |
| 642 | // FIXME: It will be better just to find spare register here. |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 643 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 644 | .addReg(ARM::SP, RegState::Kill)); |
| 645 | emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign, |
| 646 | false); |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 647 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 648 | .addReg(ARM::R4, RegState::Kill)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | AFI->setShouldRestoreSPFromFP(true); |
| 652 | } |
| 653 | |
| 654 | // If we need a base pointer, set it up here. It's whatever the value |
| 655 | // of the stack pointer is at this point. Any variable size objects |
| 656 | // will be allocated after this, so we can still use the base pointer |
| 657 | // to reference locals. |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 658 | // FIXME: Clarify FrameSetup flags here. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 659 | if (RegInfo->hasBasePointer(MF)) { |
| 660 | if (isARM) |
| 661 | BuildMI(MBB, MBBI, dl, |
| 662 | TII.get(ARM::MOVr), RegInfo->getBaseRegister()) |
| 663 | .addReg(ARM::SP) |
| 664 | .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 665 | else |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 666 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 667 | RegInfo->getBaseRegister()) |
| 668 | .addReg(ARM::SP)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | // If the frame has variable sized objects then the epilogue must restore |
Eric Christopher | d5bbeba | 2011-01-10 23:10:59 +0000 | [diff] [blame] | 672 | // the sp from fp. We can assume there's an FP here since hasFP already |
| 673 | // checks for hasVarSizedObjects. |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 674 | if (MFI->hasVarSizedObjects()) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 675 | AFI->setShouldRestoreSPFromFP(true); |
| 676 | } |
| 677 | |
Tim Northover | 3024b55 | 2014-12-01 17:46:39 +0000 | [diff] [blame] | 678 | // Resolve TCReturn pseudo-instruction |
| 679 | void ARMFrameLowering::fixTCReturn(MachineFunction &MF, |
| 680 | MachineBasicBlock &MBB) const { |
| 681 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| 682 | assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); |
| 683 | unsigned RetOpcode = MBBI->getOpcode(); |
| 684 | DebugLoc dl = MBBI->getDebugLoc(); |
| 685 | const ARMBaseInstrInfo &TII = |
| 686 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
| 687 | |
| 688 | if (!(RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri)) |
| 689 | return; |
| 690 | |
| 691 | // Tail call return: adjust the stack pointer and jump to callee. |
| 692 | MBBI = MBB.getLastNonDebugInstr(); |
| 693 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 694 | |
| 695 | // Jump to label or value in register. |
| 696 | if (RetOpcode == ARM::TCRETURNdi) { |
| 697 | unsigned TCOpcode = STI.isThumb() ? |
| 698 | (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) : |
| 699 | ARM::TAILJMPd; |
| 700 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); |
| 701 | if (JumpTarget.isGlobal()) |
| 702 | MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), |
| 703 | JumpTarget.getTargetFlags()); |
| 704 | else { |
| 705 | assert(JumpTarget.isSymbol()); |
| 706 | MIB.addExternalSymbol(JumpTarget.getSymbolName(), |
| 707 | JumpTarget.getTargetFlags()); |
| 708 | } |
| 709 | |
| 710 | // Add the default predicate in Thumb mode. |
| 711 | if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); |
| 712 | } else if (RetOpcode == ARM::TCRETURNri) { |
| 713 | BuildMI(MBB, MBBI, dl, |
| 714 | TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). |
| 715 | addReg(JumpTarget.getReg(), RegState::Kill); |
| 716 | } |
| 717 | |
| 718 | MachineInstr *NewMI = std::prev(MBBI); |
| 719 | for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) |
| 720 | NewMI->addOperand(MBBI->getOperand(i)); |
| 721 | |
| 722 | // Delete the pseudo instruction TCRETURN. |
| 723 | MBB.erase(MBBI); |
| 724 | MBBI = NewMI; |
| 725 | } |
| 726 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 727 | void ARMFrameLowering::emitEpilogue(MachineFunction &MF, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 728 | MachineBasicBlock &MBB) const { |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 729 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 730 | assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 731 | DebugLoc dl = MBBI->getDebugLoc(); |
| 732 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 733 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 734 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 735 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 736 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 737 | assert(!AFI->isThumb1OnlyFunction() && |
| 738 | "This emitEpilogue does not support Thumb1!"); |
| 739 | bool isARM = !AFI->isThumbFunction(); |
| 740 | |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 741 | unsigned Align = STI.getFrameLowering()->getStackAlignment(); |
Stepan Dyatkovskiy | d0e34a2 | 2013-05-20 08:01:34 +0000 | [diff] [blame] | 742 | unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 743 | int NumBytes = (int)MFI->getStackSize(); |
| 744 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 745 | |
Jakob Stoklund Olesen | e380183 | 2012-10-26 21:46:57 +0000 | [diff] [blame] | 746 | // All calls are tail calls in GHC calling conv, and functions have no |
| 747 | // prologue/epilogue. |
Tim Northover | 3024b55 | 2014-12-01 17:46:39 +0000 | [diff] [blame] | 748 | if (MF.getFunction()->getCallingConv() == CallingConv::GHC) { |
| 749 | fixTCReturn(MF, MBB); |
Eric Christopher | b332236 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 750 | return; |
Tim Northover | 3024b55 | 2014-12-01 17:46:39 +0000 | [diff] [blame] | 751 | } |
Eric Christopher | b332236 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 752 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 753 | if (!AFI->hasStackFrame()) { |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 754 | if (NumBytes - ArgRegsSaveSize != 0) |
| 755 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 756 | } else { |
| 757 | // Unwind MBBI to point to first LDR / VLDRD. |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 758 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 759 | if (MBBI != MBB.begin()) { |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 760 | do { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 761 | --MBBI; |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 762 | } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 763 | if (!isCSRestore(MBBI, TII, CSRegs)) |
| 764 | ++MBBI; |
| 765 | } |
| 766 | |
| 767 | // Move SP to start of FP callee save spill area. |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 768 | NumBytes -= (ArgRegsSaveSize + |
| 769 | AFI->getGPRCalleeSavedArea1Size() + |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 770 | AFI->getGPRCalleeSavedArea2Size() + |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 771 | AFI->getDPRCalleeSavedGapSize() + |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 772 | AFI->getDPRCalleeSavedAreaSize()); |
| 773 | |
| 774 | // Reset SP based on frame pointer only if the stack frame extends beyond |
| 775 | // frame pointer stack slot or target is ELF and the function has FP. |
| 776 | if (AFI->shouldRestoreSPFromFP()) { |
| 777 | NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; |
| 778 | if (NumBytes) { |
| 779 | if (isARM) |
| 780 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, |
| 781 | ARMCC::AL, 0, TII); |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 782 | else { |
| 783 | // It's not possible to restore SP from FP in a single instruction. |
Evan Cheng | 801d98b | 2012-01-04 01:55:04 +0000 | [diff] [blame] | 784 | // For iOS, this looks like: |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 785 | // mov sp, r7 |
| 786 | // sub sp, #24 |
| 787 | // This is bad, if an interrupt is taken after the mov, sp is in an |
| 788 | // inconsistent state. |
| 789 | // Use the first callee-saved register as a scratch register. |
Kaelyn Uhrain | 271fbb6 | 2012-10-26 23:28:41 +0000 | [diff] [blame] | 790 | assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 791 | "No scratch register to restore SP from FP!"); |
| 792 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 793 | ARMCC::AL, 0, TII); |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 794 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 795 | ARM::SP) |
| 796 | .addReg(ARM::R4)); |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 797 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 798 | } else { |
| 799 | // Thumb2 or ARM. |
| 800 | if (isARM) |
| 801 | BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) |
| 802 | .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 803 | else |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 804 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 805 | ARM::SP) |
| 806 | .addReg(FramePtr)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 807 | } |
Tim Northover | dee8604 | 2013-12-02 14:46:26 +0000 | [diff] [blame] | 808 | } else if (NumBytes && |
Tim Northover | e4def5e | 2013-12-05 11:02:02 +0000 | [diff] [blame] | 809 | !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes)) |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 810 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 811 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 812 | // Increment past our save areas. |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 813 | if (AFI->getDPRCalleeSavedAreaSize()) { |
| 814 | MBBI++; |
| 815 | // Since vpop register list cannot have gaps, there may be multiple vpop |
| 816 | // instructions in the epilogue. |
| 817 | while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) |
| 818 | MBBI++; |
| 819 | } |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame] | 820 | if (AFI->getDPRCalleeSavedGapSize()) { |
| 821 | assert(AFI->getDPRCalleeSavedGapSize() == 4 && |
| 822 | "unexpected DPR alignment gap"); |
| 823 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize()); |
| 824 | } |
| 825 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 826 | if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; |
| 827 | if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Tim Northover | 3024b55 | 2014-12-01 17:46:39 +0000 | [diff] [blame] | 830 | fixTCReturn(MF, MBB); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 831 | |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 832 | if (ArgRegsSaveSize) |
| 833 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 834 | } |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 835 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 836 | /// getFrameIndexReference - Provide a base+offset reference to an FI slot for |
| 837 | /// debug info. It's the same as what we use for resolving the code-gen |
| 838 | /// references for now. FIXME: This can go wrong when references are |
| 839 | /// SP-relative and simple call frames aren't used. |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 840 | int |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 841 | ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 842 | unsigned &FrameReg) const { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 843 | return ResolveFrameIndexReference(MF, FI, FrameReg, 0); |
| 844 | } |
| 845 | |
| 846 | int |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 847 | ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 848 | int FI, unsigned &FrameReg, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 849 | int SPAdj) const { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 850 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 851 | const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 852 | MF.getSubtarget().getRegisterInfo()); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 853 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 854 | int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); |
| 855 | int FPOffset = Offset - AFI->getFramePtrSpillOffset(); |
| 856 | bool isFixed = MFI->isFixedObjectIndex(FI); |
| 857 | |
| 858 | FrameReg = ARM::SP; |
| 859 | Offset += SPAdj; |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 860 | |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 861 | // SP can move around if there are allocas. We may also lose track of SP |
| 862 | // when emergency spilling inside a non-reserved call frame setup. |
Bob Wilson | ca69032 | 2012-03-20 19:28:22 +0000 | [diff] [blame] | 863 | bool hasMovingSP = !hasReservedCallFrame(MF); |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 864 | |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 865 | // When dynamically realigning the stack, use the frame pointer for |
| 866 | // parameters, and the stack/base pointer for locals. |
| 867 | if (RegInfo->needsStackRealignment(MF)) { |
| 868 | assert (hasFP(MF) && "dynamic stack realignment without a FP!"); |
| 869 | if (isFixed) { |
| 870 | FrameReg = RegInfo->getFrameRegister(MF); |
| 871 | Offset = FPOffset; |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 872 | } else if (hasMovingSP) { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 873 | assert(RegInfo->hasBasePointer(MF) && |
| 874 | "VLAs and dynamic stack alignment, but missing base pointer!"); |
| 875 | FrameReg = RegInfo->getBaseRegister(); |
| 876 | } |
| 877 | return Offset; |
| 878 | } |
| 879 | |
| 880 | // If there is a frame pointer, use it when we can. |
| 881 | if (hasFP(MF) && AFI->hasStackFrame()) { |
| 882 | // Use frame pointer to reference fixed objects. Use it for locals if |
| 883 | // there are VLAs (and thus the SP isn't reliable as a base). |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 884 | if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 885 | FrameReg = RegInfo->getFrameRegister(MF); |
| 886 | return FPOffset; |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 887 | } else if (hasMovingSP) { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 888 | assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 889 | if (AFI->isThumb2Function()) { |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 890 | // Try to use the frame pointer if we can, else use the base pointer |
| 891 | // since it's available. This is handy for the emergency spill slot, in |
| 892 | // particular. |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 893 | if (FPOffset >= -255 && FPOffset < 0) { |
| 894 | FrameReg = RegInfo->getFrameRegister(MF); |
| 895 | return FPOffset; |
| 896 | } |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 897 | } |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 898 | } else if (AFI->isThumb2Function()) { |
Andrew Trick | f7ecc16 | 2011-08-25 17:40:54 +0000 | [diff] [blame] | 899 | // Use add <rd>, sp, #<imm8> |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 900 | // ldr <rd>, [sp, #<imm8>] |
| 901 | // if at all possible to save space. |
| 902 | if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) |
| 903 | return Offset; |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 904 | // In Thumb2 mode, the negative offset is very limited. Try to avoid |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 905 | // out of range references. ldr <rt>,[<rn>, #-<imm8>] |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 906 | if (FPOffset >= -255 && FPOffset < 0) { |
| 907 | FrameReg = RegInfo->getFrameRegister(MF); |
| 908 | return FPOffset; |
| 909 | } |
| 910 | } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { |
| 911 | // Otherwise, use SP or FP, whichever is closer to the stack slot. |
| 912 | FrameReg = RegInfo->getFrameRegister(MF); |
| 913 | return FPOffset; |
| 914 | } |
| 915 | } |
| 916 | // Use the base pointer if we have one. |
| 917 | if (RegInfo->hasBasePointer(MF)) |
| 918 | FrameReg = RegInfo->getBaseRegister(); |
| 919 | return Offset; |
| 920 | } |
| 921 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 922 | int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, |
| 923 | int FI) const { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 924 | unsigned FrameReg; |
| 925 | return getFrameIndexReference(MF, FI, FrameReg); |
| 926 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 927 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 928 | void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 929 | MachineBasicBlock::iterator MI, |
| 930 | const std::vector<CalleeSavedInfo> &CSI, |
| 931 | unsigned StmOpc, unsigned StrOpc, |
| 932 | bool NoGap, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 933 | bool(*Func)(unsigned, bool), |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 934 | unsigned NumAlignedDPRCS2Regs, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 935 | unsigned MIFlags) const { |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 936 | MachineFunction &MF = *MBB.getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 937 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 938 | |
| 939 | DebugLoc DL; |
| 940 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 941 | |
Evan Cheng | c27c956 | 2010-12-07 19:59:34 +0000 | [diff] [blame] | 942 | SmallVector<std::pair<unsigned,bool>, 4> Regs; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 943 | unsigned i = CSI.size(); |
| 944 | while (i != 0) { |
| 945 | unsigned LastReg = 0; |
| 946 | for (; i != 0; --i) { |
| 947 | unsigned Reg = CSI[i-1].getReg(); |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 948 | if (!(Func)(Reg, STI.isTargetDarwin())) continue; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 949 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 950 | // D-registers in the aligned area DPRCS2 are NOT spilled here. |
| 951 | if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) |
| 952 | continue; |
| 953 | |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 954 | // Add the callee-saved register as live-in unless it's LR and |
Jim Grosbach | c0b669f | 2010-12-09 16:14:46 +0000 | [diff] [blame] | 955 | // @llvm.returnaddress is called. If LR is returned for |
| 956 | // @llvm.returnaddress then it's already added to the function and |
| 957 | // entry block live-in sets. |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 958 | bool isKill = true; |
| 959 | if (Reg == ARM::LR) { |
| 960 | if (MF.getFrameInfo()->isReturnAddressTaken() && |
| 961 | MF.getRegInfo().isLiveIn(Reg)) |
| 962 | isKill = false; |
| 963 | } |
| 964 | |
| 965 | if (isKill) |
| 966 | MBB.addLiveIn(Reg); |
| 967 | |
Eric Christopher | 2a2e65c | 2010-12-09 01:57:45 +0000 | [diff] [blame] | 968 | // If NoGap is true, push consecutive registers and then leave the rest |
Evan Cheng | 9d54ae6 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 969 | // for other instructions. e.g. |
Eric Christopher | 2a2e65c | 2010-12-09 01:57:45 +0000 | [diff] [blame] | 970 | // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} |
Evan Cheng | 9d54ae6 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 971 | if (NoGap && LastReg && LastReg != Reg-1) |
| 972 | break; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 973 | LastReg = Reg; |
| 974 | Regs.push_back(std::make_pair(Reg, isKill)); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 977 | if (Regs.empty()) |
| 978 | continue; |
| 979 | if (Regs.size() > 1 || StrOpc== 0) { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 980 | MachineInstrBuilder MIB = |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 981 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 982 | .addReg(ARM::SP).setMIFlags(MIFlags)); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 983 | for (unsigned i = 0, e = Regs.size(); i < e; ++i) |
| 984 | MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 985 | } else if (Regs.size() == 1) { |
| 986 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), |
| 987 | ARM::SP) |
| 988 | .addReg(Regs[0].first, getKillRegState(Regs[0].second)) |
Jim Grosbach | f0c95ca | 2011-08-05 20:35:44 +0000 | [diff] [blame] | 989 | .addReg(ARM::SP).setMIFlags(MIFlags) |
| 990 | .addImm(-4); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 991 | AddDefaultPred(MIB); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 992 | } |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 993 | Regs.clear(); |
Tim Northover | 3cccc45 | 2014-03-12 11:29:23 +0000 | [diff] [blame] | 994 | |
| 995 | // Put any subsequent vpush instructions before this one: they will refer to |
| 996 | // higher register numbers so need to be pushed first in order to preserve |
| 997 | // monotonicity. |
| 998 | --MI; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 999 | } |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1000 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1001 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1002 | void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1003 | MachineBasicBlock::iterator MI, |
| 1004 | const std::vector<CalleeSavedInfo> &CSI, |
| 1005 | unsigned LdmOpc, unsigned LdrOpc, |
| 1006 | bool isVarArg, bool NoGap, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1007 | bool(*Func)(unsigned, bool), |
| 1008 | unsigned NumAlignedDPRCS2Regs) const { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1009 | MachineFunction &MF = *MBB.getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1010 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1011 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1012 | DebugLoc DL = MI->getDebugLoc(); |
Evan Cheng | d6093ff | 2011-01-25 01:28:33 +0000 | [diff] [blame] | 1013 | unsigned RetOpcode = MI->getOpcode(); |
| 1014 | bool isTailCall = (RetOpcode == ARM::TCRETURNdi || |
Jakob Stoklund Olesen | b4bd388 | 2012-04-06 21:17:42 +0000 | [diff] [blame] | 1015 | RetOpcode == ARM::TCRETURNri); |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1016 | bool isInterrupt = |
| 1017 | RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1018 | |
| 1019 | SmallVector<unsigned, 4> Regs; |
| 1020 | unsigned i = CSI.size(); |
| 1021 | while (i != 0) { |
| 1022 | unsigned LastReg = 0; |
| 1023 | bool DeleteRet = false; |
| 1024 | for (; i != 0; --i) { |
| 1025 | unsigned Reg = CSI[i-1].getReg(); |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 1026 | if (!(Func)(Reg, STI.isTargetDarwin())) continue; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1027 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1028 | // The aligned reloads from area DPRCS2 are not inserted here. |
| 1029 | if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) |
| 1030 | continue; |
| 1031 | |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1032 | if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt && |
| 1033 | STI.hasV5TOps()) { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1034 | Reg = ARM::PC; |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1035 | LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1036 | // Fold the return instruction into the LDM. |
| 1037 | DeleteRet = true; |
| 1038 | } |
| 1039 | |
Evan Cheng | 9d54ae6 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 1040 | // If NoGap is true, pop consecutive registers and then leave the rest |
| 1041 | // for other instructions. e.g. |
| 1042 | // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} |
| 1043 | if (NoGap && LastReg && LastReg != Reg-1) |
| 1044 | break; |
| 1045 | |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1046 | LastReg = Reg; |
| 1047 | Regs.push_back(Reg); |
| 1048 | } |
| 1049 | |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1050 | if (Regs.empty()) |
| 1051 | continue; |
| 1052 | if (Regs.size() > 1 || LdrOpc == 0) { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1053 | MachineInstrBuilder MIB = |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1054 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1055 | .addReg(ARM::SP)); |
| 1056 | for (unsigned i = 0, e = Regs.size(); i < e; ++i) |
| 1057 | MIB.addReg(Regs[i], getDefRegState(true)); |
Andrew Trick | 6446bf7 | 2011-08-25 17:50:53 +0000 | [diff] [blame] | 1058 | if (DeleteRet) { |
Jakob Stoklund Olesen | 33f5d14 | 2012-12-20 22:54:02 +0000 | [diff] [blame] | 1059 | MIB.copyImplicitOps(&*MI); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1060 | MI->eraseFromParent(); |
Andrew Trick | 6446bf7 | 2011-08-25 17:50:53 +0000 | [diff] [blame] | 1061 | } |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1062 | MI = MIB; |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1063 | } else if (Regs.size() == 1) { |
| 1064 | // If we adjusted the reg to PC from LR above, switch it back here. We |
| 1065 | // only do that for LDM. |
| 1066 | if (Regs[0] == ARM::PC) |
| 1067 | Regs[0] = ARM::LR; |
| 1068 | MachineInstrBuilder MIB = |
| 1069 | BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) |
| 1070 | .addReg(ARM::SP, RegState::Define) |
| 1071 | .addReg(ARM::SP); |
| 1072 | // ARM mode needs an extra reg0 here due to addrmode2. Will go away once |
| 1073 | // that refactoring is complete (eventually). |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 1074 | if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1075 | MIB.addReg(0); |
| 1076 | MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); |
| 1077 | } else |
| 1078 | MIB.addImm(4); |
| 1079 | AddDefaultPred(MIB); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 1080 | } |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1081 | Regs.clear(); |
Tim Northover | 3cccc45 | 2014-03-12 11:29:23 +0000 | [diff] [blame] | 1082 | |
| 1083 | // Put any subsequent vpop instructions after this one: they will refer to |
| 1084 | // higher register numbers so need to be popped afterwards. |
| 1085 | ++MI; |
Evan Cheng | c27c956 | 2010-12-07 19:59:34 +0000 | [diff] [blame] | 1086 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1089 | /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers |
Jakob Stoklund Olesen | 103318e | 2011-12-24 04:17:01 +0000 | [diff] [blame] | 1090 | /// starting from d8. Also insert stack realignment code and leave the stack |
| 1091 | /// pointer pointing to the d8 spill slot. |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1092 | static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB, |
| 1093 | MachineBasicBlock::iterator MI, |
| 1094 | unsigned NumAlignedDPRCS2Regs, |
| 1095 | const std::vector<CalleeSavedInfo> &CSI, |
| 1096 | const TargetRegisterInfo *TRI) { |
| 1097 | MachineFunction &MF = *MBB.getParent(); |
| 1098 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1099 | DebugLoc DL = MI->getDebugLoc(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1100 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1101 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 1102 | |
| 1103 | // Mark the D-register spill slots as properly aligned. Since MFI computes |
| 1104 | // stack slot layout backwards, this can actually mean that the d-reg stack |
| 1105 | // slot offsets can be wrong. The offset for d8 will always be correct. |
| 1106 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1107 | unsigned DNum = CSI[i].getReg() - ARM::D8; |
| 1108 | if (DNum >= 8) |
| 1109 | continue; |
| 1110 | int FI = CSI[i].getFrameIdx(); |
| 1111 | // The even-numbered registers will be 16-byte aligned, the odd-numbered |
| 1112 | // registers will be 8-byte aligned. |
| 1113 | MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16); |
| 1114 | |
| 1115 | // The stack slot for D8 needs to be maximally aligned because this is |
| 1116 | // actually the point where we align the stack pointer. MachineFrameInfo |
| 1117 | // computes all offsets relative to the incoming stack pointer which is a |
| 1118 | // bit weird when realigning the stack. Any extra padding for this |
| 1119 | // over-alignment is not realized because the code inserted below adjusts |
| 1120 | // the stack pointer by numregs * 8 before aligning the stack pointer. |
| 1121 | if (DNum == 0) |
| 1122 | MFI.setObjectAlignment(FI, MFI.getMaxAlignment()); |
| 1123 | } |
| 1124 | |
| 1125 | // Move the stack pointer to the d8 spill slot, and align it at the same |
| 1126 | // time. Leave the stack slot address in the scratch register r4. |
| 1127 | // |
| 1128 | // sub r4, sp, #numregs * 8 |
| 1129 | // bic r4, r4, #align - 1 |
| 1130 | // mov sp, r4 |
| 1131 | // |
| 1132 | bool isThumb = AFI->isThumbFunction(); |
| 1133 | assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); |
| 1134 | AFI->setShouldRestoreSPFromFP(true); |
| 1135 | |
| 1136 | // sub r4, sp, #numregs * 8 |
| 1137 | // The immediate is <= 64, so it doesn't need any special encoding. |
| 1138 | unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri; |
| 1139 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 1140 | .addReg(ARM::SP) |
| 1141 | .addImm(8 * NumAlignedDPRCS2Regs))); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1142 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1143 | unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment(); |
Kristof Beyls | 933de7a | 2015-01-08 15:09:14 +0000 | [diff] [blame] | 1144 | // We must set parameter MustBeSingleInstruction to true, since |
| 1145 | // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform |
| 1146 | // stack alignment. Luckily, this can always be done since all ARM |
| 1147 | // architecture versions that support Neon also support the BFC |
| 1148 | // instruction. |
| 1149 | emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1150 | |
| 1151 | // mov sp, r4 |
| 1152 | // The stack pointer must be adjusted before spilling anything, otherwise |
| 1153 | // the stack slots could be clobbered by an interrupt handler. |
| 1154 | // Leave r4 live, it is used below. |
| 1155 | Opc = isThumb ? ARM::tMOVr : ARM::MOVr; |
| 1156 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP) |
| 1157 | .addReg(ARM::R4); |
| 1158 | MIB = AddDefaultPred(MIB); |
| 1159 | if (!isThumb) |
| 1160 | AddDefaultCC(MIB); |
| 1161 | |
| 1162 | // Now spill NumAlignedDPRCS2Regs registers starting from d8. |
| 1163 | // r4 holds the stack slot address. |
| 1164 | unsigned NextReg = ARM::D8; |
| 1165 | |
| 1166 | // 16-byte aligned vst1.64 with 4 d-regs and address writeback. |
| 1167 | // The writeback is only needed when emitting two vst1.64 instructions. |
| 1168 | if (NumAlignedDPRCS2Regs >= 6) { |
| 1169 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1170 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1171 | MBB.addLiveIn(SupReg); |
| 1172 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), |
| 1173 | ARM::R4) |
| 1174 | .addReg(ARM::R4, RegState::Kill).addImm(16) |
| 1175 | .addReg(NextReg) |
| 1176 | .addReg(SupReg, RegState::ImplicitKill)); |
| 1177 | NextReg += 4; |
| 1178 | NumAlignedDPRCS2Regs -= 4; |
| 1179 | } |
| 1180 | |
| 1181 | // We won't modify r4 beyond this point. It currently points to the next |
| 1182 | // register to be spilled. |
| 1183 | unsigned R4BaseReg = NextReg; |
| 1184 | |
| 1185 | // 16-byte aligned vst1.64 with 4 d-regs, no writeback. |
| 1186 | if (NumAlignedDPRCS2Regs >= 4) { |
| 1187 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1188 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1189 | MBB.addLiveIn(SupReg); |
| 1190 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q)) |
| 1191 | .addReg(ARM::R4).addImm(16).addReg(NextReg) |
| 1192 | .addReg(SupReg, RegState::ImplicitKill)); |
| 1193 | NextReg += 4; |
| 1194 | NumAlignedDPRCS2Regs -= 4; |
| 1195 | } |
| 1196 | |
| 1197 | // 16-byte aligned vst1.64 with 2 d-regs. |
| 1198 | if (NumAlignedDPRCS2Regs >= 2) { |
| 1199 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1200 | &ARM::QPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1201 | MBB.addLiveIn(SupReg); |
| 1202 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64)) |
Jim Grosbach | c988e0c | 2012-03-05 19:33:30 +0000 | [diff] [blame] | 1203 | .addReg(ARM::R4).addImm(16).addReg(SupReg)); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1204 | NextReg += 2; |
| 1205 | NumAlignedDPRCS2Regs -= 2; |
| 1206 | } |
| 1207 | |
| 1208 | // Finally, use a vanilla vstr.64 for the odd last register. |
| 1209 | if (NumAlignedDPRCS2Regs) { |
| 1210 | MBB.addLiveIn(NextReg); |
| 1211 | // vstr.64 uses addrmode5 which has an offset scale of 4. |
| 1212 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD)) |
| 1213 | .addReg(NextReg) |
| 1214 | .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2)); |
| 1215 | } |
| 1216 | |
| 1217 | // The last spill instruction inserted should kill the scratch register r4. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1218 | std::prev(MI)->addRegisterKilled(ARM::R4, TRI); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an |
| 1222 | /// iterator to the following instruction. |
| 1223 | static MachineBasicBlock::iterator |
| 1224 | skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, |
| 1225 | unsigned NumAlignedDPRCS2Regs) { |
| 1226 | // sub r4, sp, #numregs * 8 |
| 1227 | // bic r4, r4, #align - 1 |
| 1228 | // mov sp, r4 |
| 1229 | ++MI; ++MI; ++MI; |
| 1230 | assert(MI->mayStore() && "Expecting spill instruction"); |
| 1231 | |
| 1232 | // These switches all fall through. |
| 1233 | switch(NumAlignedDPRCS2Regs) { |
| 1234 | case 7: |
| 1235 | ++MI; |
| 1236 | assert(MI->mayStore() && "Expecting spill instruction"); |
| 1237 | default: |
| 1238 | ++MI; |
| 1239 | assert(MI->mayStore() && "Expecting spill instruction"); |
| 1240 | case 1: |
| 1241 | case 2: |
| 1242 | case 4: |
| 1243 | assert(MI->killsRegister(ARM::R4) && "Missed kill flag"); |
| 1244 | ++MI; |
| 1245 | } |
| 1246 | return MI; |
| 1247 | } |
| 1248 | |
| 1249 | /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers |
| 1250 | /// starting from d8. These instructions are assumed to execute while the |
| 1251 | /// stack is still aligned, unlike the code inserted by emitPopInst. |
| 1252 | static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB, |
| 1253 | MachineBasicBlock::iterator MI, |
| 1254 | unsigned NumAlignedDPRCS2Regs, |
| 1255 | const std::vector<CalleeSavedInfo> &CSI, |
| 1256 | const TargetRegisterInfo *TRI) { |
| 1257 | MachineFunction &MF = *MBB.getParent(); |
| 1258 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1259 | DebugLoc DL = MI->getDebugLoc(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1260 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Find the frame index assigned to d8. |
| 1263 | int D8SpillFI = 0; |
| 1264 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) |
| 1265 | if (CSI[i].getReg() == ARM::D8) { |
| 1266 | D8SpillFI = CSI[i].getFrameIdx(); |
| 1267 | break; |
| 1268 | } |
| 1269 | |
| 1270 | // Materialize the address of the d8 spill slot into the scratch register r4. |
| 1271 | // This can be fairly complicated if the stack frame is large, so just use |
| 1272 | // the normal frame index elimination mechanism to do it. This code runs as |
| 1273 | // the initial part of the epilog where the stack and base pointers haven't |
| 1274 | // been changed yet. |
| 1275 | bool isThumb = AFI->isThumbFunction(); |
| 1276 | assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); |
| 1277 | |
| 1278 | unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; |
| 1279 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) |
| 1280 | .addFrameIndex(D8SpillFI).addImm(0))); |
| 1281 | |
| 1282 | // Now restore NumAlignedDPRCS2Regs registers starting from d8. |
| 1283 | unsigned NextReg = ARM::D8; |
| 1284 | |
| 1285 | // 16-byte aligned vld1.64 with 4 d-regs and writeback. |
| 1286 | if (NumAlignedDPRCS2Regs >= 6) { |
| 1287 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1288 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1289 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg) |
| 1290 | .addReg(ARM::R4, RegState::Define) |
| 1291 | .addReg(ARM::R4, RegState::Kill).addImm(16) |
| 1292 | .addReg(SupReg, RegState::ImplicitDefine)); |
| 1293 | NextReg += 4; |
| 1294 | NumAlignedDPRCS2Regs -= 4; |
| 1295 | } |
| 1296 | |
| 1297 | // We won't modify r4 beyond this point. It currently points to the next |
| 1298 | // register to be spilled. |
| 1299 | unsigned R4BaseReg = NextReg; |
| 1300 | |
| 1301 | // 16-byte aligned vld1.64 with 4 d-regs, no writeback. |
| 1302 | if (NumAlignedDPRCS2Regs >= 4) { |
| 1303 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1304 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1305 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg) |
| 1306 | .addReg(ARM::R4).addImm(16) |
| 1307 | .addReg(SupReg, RegState::ImplicitDefine)); |
| 1308 | NextReg += 4; |
| 1309 | NumAlignedDPRCS2Regs -= 4; |
| 1310 | } |
| 1311 | |
| 1312 | // 16-byte aligned vld1.64 with 2 d-regs. |
| 1313 | if (NumAlignedDPRCS2Regs >= 2) { |
| 1314 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1315 | &ARM::QPRRegClass); |
Jim Grosbach | c988e0c | 2012-03-05 19:33:30 +0000 | [diff] [blame] | 1316 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg) |
| 1317 | .addReg(ARM::R4).addImm(16)); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1318 | NextReg += 2; |
| 1319 | NumAlignedDPRCS2Regs -= 2; |
| 1320 | } |
| 1321 | |
| 1322 | // Finally, use a vanilla vldr.64 for the remaining odd register. |
| 1323 | if (NumAlignedDPRCS2Regs) |
| 1324 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg) |
| 1325 | .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg))); |
| 1326 | |
| 1327 | // Last store kills r4. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1328 | std::prev(MI)->addRegisterKilled(ARM::R4, TRI); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1331 | bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1332 | MachineBasicBlock::iterator MI, |
| 1333 | const std::vector<CalleeSavedInfo> &CSI, |
| 1334 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1335 | if (CSI.empty()) |
| 1336 | return false; |
| 1337 | |
| 1338 | MachineFunction &MF = *MBB.getParent(); |
| 1339 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1340 | |
| 1341 | unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; |
Jim Grosbach | 05dec8b1 | 2011-09-02 18:46:15 +0000 | [diff] [blame] | 1342 | unsigned PushOneOpc = AFI->isThumbFunction() ? |
| 1343 | ARM::t2STR_PRE : ARM::STR_PRE_IMM; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1344 | unsigned FltOpc = ARM::VSTMDDB_UPD; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1345 | unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); |
| 1346 | emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 1347 | MachineInstr::FrameSetup); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1348 | emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 1349 | MachineInstr::FrameSetup); |
| 1350 | emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1351 | NumAlignedDPRCS2Regs, MachineInstr::FrameSetup); |
| 1352 | |
| 1353 | // The code above does not insert spill code for the aligned DPRCS2 registers. |
| 1354 | // The stack realignment code will be inserted between the push instructions |
| 1355 | // and these spills. |
| 1356 | if (NumAlignedDPRCS2Regs) |
| 1357 | emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1358 | |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1362 | bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1363 | MachineBasicBlock::iterator MI, |
| 1364 | const std::vector<CalleeSavedInfo> &CSI, |
| 1365 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1366 | if (CSI.empty()) |
| 1367 | return false; |
| 1368 | |
| 1369 | MachineFunction &MF = *MBB.getParent(); |
| 1370 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 1371 | bool isVarArg = AFI->getArgRegsSaveSize() > 0; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1372 | unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); |
| 1373 | |
| 1374 | // The emitPopInst calls below do not insert reloads for the aligned DPRCS2 |
| 1375 | // registers. Do that here instead. |
| 1376 | if (NumAlignedDPRCS2Regs) |
| 1377 | emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1378 | |
| 1379 | unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; |
Jim Grosbach | 05dec8b1 | 2011-09-02 18:46:15 +0000 | [diff] [blame] | 1380 | unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1381 | unsigned FltOpc = ARM::VLDMDIA_UPD; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1382 | emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register, |
| 1383 | NumAlignedDPRCS2Regs); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1384 | emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1385 | &isARMArea2Register, 0); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1386 | emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1387 | &isARMArea1Register, 0); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1388 | |
| 1389 | return true; |
| 1390 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1391 | |
| 1392 | // FIXME: Make generic? |
| 1393 | static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, |
| 1394 | const ARMBaseInstrInfo &TII) { |
| 1395 | unsigned FnSize = 0; |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1396 | for (auto &MBB : MF) { |
| 1397 | for (auto &MI : MBB) |
| 1398 | FnSize += TII.GetInstSizeInBytes(&MI); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1399 | } |
| 1400 | return FnSize; |
| 1401 | } |
| 1402 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1403 | /// estimateRSStackSizeLimit - Look at each instruction that references stack |
| 1404 | /// frames and return the stack size limit beyond which some of these |
| 1405 | /// instructions will require a scratch register during their expansion later. |
| 1406 | // FIXME: Move to TII? |
| 1407 | static unsigned estimateRSStackSizeLimit(MachineFunction &MF, |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1408 | const TargetFrameLowering *TFI) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1409 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1410 | unsigned Limit = (1 << 12) - 1; |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1411 | for (auto &MBB : MF) { |
| 1412 | for (auto &MI : MBB) { |
| 1413 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1414 | if (!MI.getOperand(i).isFI()) |
| 1415 | continue; |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1416 | |
| 1417 | // When using ADDri to get the address of a stack object, 255 is the |
| 1418 | // largest offset guaranteed to fit in the immediate offset. |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1419 | if (MI.getOpcode() == ARM::ADDri) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1420 | Limit = std::min(Limit, (1U << 8) - 1); |
| 1421 | break; |
| 1422 | } |
| 1423 | |
| 1424 | // Otherwise check the addressing mode. |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1425 | switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1426 | case ARMII::AddrMode3: |
| 1427 | case ARMII::AddrModeT2_i8: |
| 1428 | Limit = std::min(Limit, (1U << 8) - 1); |
| 1429 | break; |
| 1430 | case ARMII::AddrMode5: |
| 1431 | case ARMII::AddrModeT2_i8s4: |
| 1432 | Limit = std::min(Limit, ((1U << 8) - 1) * 4); |
| 1433 | break; |
| 1434 | case ARMII::AddrModeT2_i12: |
| 1435 | // i12 supports only positive offset so these will be converted to |
| 1436 | // i8 opcodes. See llvm::rewriteT2FrameIndex. |
| 1437 | if (TFI->hasFP(MF) && AFI->hasStackFrame()) |
| 1438 | Limit = std::min(Limit, (1U << 8) - 1); |
| 1439 | break; |
| 1440 | case ARMII::AddrMode4: |
| 1441 | case ARMII::AddrMode6: |
| 1442 | // Addressing modes 4 & 6 (load/store) instructions can't encode an |
| 1443 | // immediate offset for stack references. |
| 1444 | return 0; |
| 1445 | default: |
| 1446 | break; |
| 1447 | } |
| 1448 | break; // At most one FI per instruction |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | return Limit; |
| 1454 | } |
| 1455 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1456 | // In functions that realign the stack, it can be an advantage to spill the |
| 1457 | // callee-saved vector registers after realigning the stack. The vst1 and vld1 |
| 1458 | // instructions take alignment hints that can improve performance. |
| 1459 | // |
| 1460 | static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) { |
| 1461 | MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0); |
| 1462 | if (!SpillAlignedNEONRegs) |
| 1463 | return; |
| 1464 | |
| 1465 | // Naked functions don't spill callee-saved registers. |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 1466 | if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 1467 | Attribute::Naked)) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1468 | return; |
| 1469 | |
| 1470 | // We are planning to use NEON instructions vst1 / vld1. |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 1471 | if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON()) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1472 | return; |
| 1473 | |
| 1474 | // Don't bother if the default stack alignment is sufficiently high. |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 1475 | if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | |
| 1478 | // Aligned spills require stack realignment. |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame^] | 1479 | if (!static_cast<const ARMBaseRegisterInfo *>( |
| 1480 | MF.getSubtarget().getRegisterInfo())->canRealignStack(MF)) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1481 | return; |
| 1482 | |
| 1483 | // We always spill contiguous d-registers starting from d8. Count how many |
| 1484 | // needs spilling. The register allocator will almost always use the |
| 1485 | // callee-saved registers in order, but it can happen that there are holes in |
| 1486 | // the range. Registers above the hole will be spilled to the standard DPRCS |
| 1487 | // area. |
| 1488 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 1489 | unsigned NumSpills = 0; |
| 1490 | for (; NumSpills < 8; ++NumSpills) |
Jakob Stoklund Olesen | 0736442 | 2012-10-17 18:44:18 +0000 | [diff] [blame] | 1491 | if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills)) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1492 | break; |
| 1493 | |
| 1494 | // Don't do this for just one d-register. It's not worth it. |
| 1495 | if (NumSpills < 2) |
| 1496 | return; |
| 1497 | |
| 1498 | // Spill the first NumSpills D-registers after realigning the stack. |
| 1499 | MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills); |
| 1500 | |
| 1501 | // A scratch register is required for the vst1 / vld1 instructions. |
| 1502 | MF.getRegInfo().setPhysRegUsed(ARM::R4); |
| 1503 | } |
| 1504 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1505 | void |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1506 | ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1507 | RegScavenger *RS) const { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1508 | // This tells PEI to spill the FP as if it is any other callee-save register |
| 1509 | // to take advantage the eliminateFrameIndex machinery. This also ensures it |
| 1510 | // is spilled in the order specified by getCalleeSavedRegs() to make it easier |
| 1511 | // to combine multiple loads / stores. |
| 1512 | bool CanEliminateFrame = true; |
| 1513 | bool CS1Spilled = false; |
| 1514 | bool LRSpilled = false; |
| 1515 | unsigned NumGPRSpills = 0; |
| 1516 | SmallVector<unsigned, 4> UnspilledCS1GPRs; |
| 1517 | SmallVector<unsigned, 4> UnspilledCS2GPRs; |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1518 | const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1519 | MF.getSubtarget().getRegisterInfo()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1520 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1521 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1522 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1523 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1524 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1525 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 1526 | |
| 1527 | // Spill R4 if Thumb2 function requires stack realignment - it will be used as |
| 1528 | // scratch register. Also spill R4 if Thumb2 function has varsized objects, |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1529 | // since it's not always possible to restore sp from fp in a single |
| 1530 | // instruction. |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1531 | // FIXME: It will be better just to find spare register here. |
| 1532 | if (AFI->isThumb2Function() && |
| 1533 | (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1534 | MRI.setPhysRegUsed(ARM::R4); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1535 | |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1536 | if (AFI->isThumb1OnlyFunction()) { |
| 1537 | // Spill LR if Thumb1 function uses variable length argument lists. |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 1538 | if (AFI->getArgRegsSaveSize() > 0) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1539 | MRI.setPhysRegUsed(ARM::LR); |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1540 | |
Jim Grosbach | dca8531 | 2011-06-13 21:18:25 +0000 | [diff] [blame] | 1541 | // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know |
| 1542 | // for sure what the stack size will be, but for this, an estimate is good |
| 1543 | // enough. If there anything changes it, it'll be a spill, which implies |
| 1544 | // we've used all the registers and so R4 is already used, so not marking |
Chad Rosier | add38c1 | 2011-10-20 00:07:12 +0000 | [diff] [blame] | 1545 | // it here will be OK. |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1546 | // FIXME: It will be better just to find spare register here. |
Hal Finkel | 628ba12 | 2013-03-14 21:15:20 +0000 | [diff] [blame] | 1547 | unsigned StackSize = MFI->estimateStackSize(MF); |
Chad Rosier | add38c1 | 2011-10-20 00:07:12 +0000 | [diff] [blame] | 1548 | if (MFI->hasVarSizedObjects() || StackSize > 508) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1549 | MRI.setPhysRegUsed(ARM::R4); |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1550 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1551 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1552 | // See if we can spill vector registers to aligned stack. |
| 1553 | checkNumAlignedDPRCS2Regs(MF); |
| 1554 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1555 | // Spill the BasePtr if it's used. |
| 1556 | if (RegInfo->hasBasePointer(MF)) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1557 | MRI.setPhysRegUsed(RegInfo->getBaseRegister()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1558 | |
| 1559 | // Don't spill FP if the frame can be eliminated. This is determined |
| 1560 | // by scanning the callee-save registers to see if any is used. |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 1561 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1562 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 1563 | unsigned Reg = CSRegs[i]; |
| 1564 | bool Spilled = false; |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1565 | if (MRI.isPhysRegUsed(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1566 | Spilled = true; |
| 1567 | CanEliminateFrame = false; |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1570 | if (!ARM::GPRRegClass.contains(Reg)) |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1571 | continue; |
| 1572 | |
| 1573 | if (Spilled) { |
| 1574 | NumGPRSpills++; |
| 1575 | |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 1576 | if (!STI.isTargetDarwin()) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1577 | if (Reg == ARM::LR) |
| 1578 | LRSpilled = true; |
| 1579 | CS1Spilled = true; |
| 1580 | continue; |
| 1581 | } |
| 1582 | |
| 1583 | // Keep track if LR and any of R4, R5, R6, and R7 is spilled. |
| 1584 | switch (Reg) { |
| 1585 | case ARM::LR: |
| 1586 | LRSpilled = true; |
| 1587 | // Fallthrough |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1588 | case ARM::R0: case ARM::R1: |
| 1589 | case ARM::R2: case ARM::R3: |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1590 | case ARM::R4: case ARM::R5: |
| 1591 | case ARM::R6: case ARM::R7: |
| 1592 | CS1Spilled = true; |
| 1593 | break; |
| 1594 | default: |
| 1595 | break; |
| 1596 | } |
| 1597 | } else { |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 1598 | if (!STI.isTargetDarwin()) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1599 | UnspilledCS1GPRs.push_back(Reg); |
| 1600 | continue; |
| 1601 | } |
| 1602 | |
| 1603 | switch (Reg) { |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1604 | case ARM::R0: case ARM::R1: |
| 1605 | case ARM::R2: case ARM::R3: |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1606 | case ARM::R4: case ARM::R5: |
| 1607 | case ARM::R6: case ARM::R7: |
| 1608 | case ARM::LR: |
| 1609 | UnspilledCS1GPRs.push_back(Reg); |
| 1610 | break; |
| 1611 | default: |
| 1612 | UnspilledCS2GPRs.push_back(Reg); |
| 1613 | break; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | bool ForceLRSpill = false; |
| 1619 | if (!LRSpilled && AFI->isThumb1OnlyFunction()) { |
| 1620 | unsigned FnSize = GetFunctionSizeInBytes(MF, TII); |
| 1621 | // Force LR to be spilled if the Thumb function size is > 2048. This enables |
| 1622 | // use of BL to implement far jump. If it turns out that it's not needed |
| 1623 | // then the branch fix up path will undo it. |
| 1624 | if (FnSize >= (1 << 11)) { |
| 1625 | CanEliminateFrame = false; |
| 1626 | ForceLRSpill = true; |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | // If any of the stack slot references may be out of range of an immediate |
| 1631 | // offset, make sure a register (or a spill slot) is available for the |
| 1632 | // register scavenger. Note that if we're indexing off the frame pointer, the |
| 1633 | // effective stack size is 4 bytes larger since the FP points to the stack |
| 1634 | // slot of the previous FP. Also, if we have variable sized objects in the |
| 1635 | // function, stack slot references will often be negative, and some of |
| 1636 | // our instructions are positive-offset only, so conservatively consider |
| 1637 | // that case to want a spill slot (or register) as well. Similarly, if |
| 1638 | // the function adjusts the stack pointer during execution and the |
| 1639 | // adjustments aren't already part of our stack size estimate, our offset |
| 1640 | // calculations may be off, so be conservative. |
| 1641 | // FIXME: We could add logic to be more precise about negative offsets |
| 1642 | // and which instructions will need a scratch register for them. Is it |
| 1643 | // worth the effort and added fragility? |
| 1644 | bool BigStack = |
| 1645 | (RS && |
Hal Finkel | 628ba12 | 2013-03-14 21:15:20 +0000 | [diff] [blame] | 1646 | (MFI->estimateStackSize(MF) + |
| 1647 | ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1648 | estimateRSStackSizeLimit(MF, this))) |
| 1649 | || MFI->hasVarSizedObjects() |
| 1650 | || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); |
| 1651 | |
| 1652 | bool ExtraCSSpill = false; |
| 1653 | if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { |
| 1654 | AFI->setHasStackFrame(true); |
| 1655 | |
| 1656 | // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. |
| 1657 | // Spill LR as well so we can fold BX_RET to the registers restore (LDM). |
| 1658 | if (!LRSpilled && CS1Spilled) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1659 | MRI.setPhysRegUsed(ARM::LR); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1660 | NumGPRSpills++; |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1661 | SmallVectorImpl<unsigned>::iterator LRPos; |
| 1662 | LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), |
| 1663 | (unsigned)ARM::LR); |
| 1664 | if (LRPos != UnspilledCS1GPRs.end()) |
| 1665 | UnspilledCS1GPRs.erase(LRPos); |
| 1666 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1667 | ForceLRSpill = false; |
| 1668 | ExtraCSSpill = true; |
| 1669 | } |
| 1670 | |
| 1671 | if (hasFP(MF)) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1672 | MRI.setPhysRegUsed(FramePtr); |
Joerg Sonnenberger | 818e725 | 2014-05-06 20:43:01 +0000 | [diff] [blame] | 1673 | auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), |
| 1674 | FramePtr); |
| 1675 | if (FPPos != UnspilledCS1GPRs.end()) |
| 1676 | UnspilledCS1GPRs.erase(FPPos); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1677 | NumGPRSpills++; |
| 1678 | } |
| 1679 | |
| 1680 | // If stack and double are 8-byte aligned and we are spilling an odd number |
| 1681 | // of GPRs, spill one extra callee save GPR so we won't have to pad between |
| 1682 | // the integer and double callee save areas. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1683 | unsigned TargetAlign = getStackAlignment(); |
Tim Northover | dc0d9e4 | 2014-11-05 00:27:20 +0000 | [diff] [blame] | 1684 | if (TargetAlign >= 8 && (NumGPRSpills & 1)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1685 | if (CS1Spilled && !UnspilledCS1GPRs.empty()) { |
| 1686 | for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { |
| 1687 | unsigned Reg = UnspilledCS1GPRs[i]; |
| 1688 | // Don't spill high register if the function is thumb1 |
| 1689 | if (!AFI->isThumb1OnlyFunction() || |
| 1690 | isARMLowRegister(Reg) || Reg == ARM::LR) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1691 | MRI.setPhysRegUsed(Reg); |
| 1692 | if (!MRI.isReserved(Reg)) |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1693 | ExtraCSSpill = true; |
| 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { |
| 1698 | unsigned Reg = UnspilledCS2GPRs.front(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1699 | MRI.setPhysRegUsed(Reg); |
| 1700 | if (!MRI.isReserved(Reg)) |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1701 | ExtraCSSpill = true; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | // Estimate if we might need to scavenge a register at some point in order |
| 1706 | // to materialize a stack offset. If so, either spill one additional |
| 1707 | // callee-saved register or reserve a special spill slot to facilitate |
| 1708 | // register scavenging. Thumb1 needs a spill slot for stack pointer |
| 1709 | // adjustments also, even when the frame itself is small. |
| 1710 | if (BigStack && !ExtraCSSpill) { |
| 1711 | // If any non-reserved CS register isn't spilled, just spill one or two |
| 1712 | // extra. That should take care of it! |
| 1713 | unsigned NumExtras = TargetAlign / 4; |
| 1714 | SmallVector<unsigned, 2> Extras; |
| 1715 | while (NumExtras && !UnspilledCS1GPRs.empty()) { |
| 1716 | unsigned Reg = UnspilledCS1GPRs.back(); |
| 1717 | UnspilledCS1GPRs.pop_back(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1718 | if (!MRI.isReserved(Reg) && |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1719 | (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || |
| 1720 | Reg == ARM::LR)) { |
| 1721 | Extras.push_back(Reg); |
| 1722 | NumExtras--; |
| 1723 | } |
| 1724 | } |
| 1725 | // For non-Thumb1 functions, also check for hi-reg CS registers |
| 1726 | if (!AFI->isThumb1OnlyFunction()) { |
| 1727 | while (NumExtras && !UnspilledCS2GPRs.empty()) { |
| 1728 | unsigned Reg = UnspilledCS2GPRs.back(); |
| 1729 | UnspilledCS2GPRs.pop_back(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1730 | if (!MRI.isReserved(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1731 | Extras.push_back(Reg); |
| 1732 | NumExtras--; |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | if (Extras.size() && NumExtras == 0) { |
| 1737 | for (unsigned i = 0, e = Extras.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1738 | MRI.setPhysRegUsed(Extras[i]); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } else if (!AFI->isThumb1OnlyFunction()) { |
| 1741 | // note: Thumb1 functions spill to R12, not the stack. Reserve a slot |
| 1742 | // closest to SP or frame pointer. |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1743 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
Hal Finkel | 9e331c2 | 2013-03-22 23:32:27 +0000 | [diff] [blame] | 1744 | RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1745 | RC->getAlignment(), |
| 1746 | false)); |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | if (ForceLRSpill) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1752 | MRI.setPhysRegUsed(ARM::LR); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1753 | AFI->setLRIsSpilledForFarJump(true); |
| 1754 | } |
| 1755 | } |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 1756 | |
| 1757 | |
| 1758 | void ARMFrameLowering:: |
| 1759 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 1760 | MachineBasicBlock::iterator I) const { |
| 1761 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1762 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 1763 | if (!hasReservedCallFrame(MF)) { |
| 1764 | // If we have alloca, convert as follows: |
| 1765 | // ADJCALLSTACKDOWN -> sub, sp, sp, amount |
| 1766 | // ADJCALLSTACKUP -> add, sp, sp, amount |
| 1767 | MachineInstr *Old = I; |
| 1768 | DebugLoc dl = Old->getDebugLoc(); |
| 1769 | unsigned Amount = Old->getOperand(0).getImm(); |
| 1770 | if (Amount != 0) { |
| 1771 | // We need to keep the stack aligned properly. To do this, we round the |
| 1772 | // amount of space needed for the outgoing arguments up to the next |
| 1773 | // alignment boundary. |
| 1774 | unsigned Align = getStackAlignment(); |
| 1775 | Amount = (Amount+Align-1)/Align*Align; |
| 1776 | |
| 1777 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1778 | assert(!AFI->isThumb1OnlyFunction() && |
| 1779 | "This eliminateCallFramePseudoInstr does not support Thumb1!"); |
| 1780 | bool isARM = !AFI->isThumbFunction(); |
| 1781 | |
| 1782 | // Replace the pseudo instruction with a new instruction... |
| 1783 | unsigned Opc = Old->getOpcode(); |
| 1784 | int PIdx = Old->findFirstPredOperandIdx(); |
| 1785 | ARMCC::CondCodes Pred = (PIdx == -1) |
| 1786 | ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); |
| 1787 | if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { |
| 1788 | // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. |
| 1789 | unsigned PredReg = Old->getOperand(2).getReg(); |
| 1790 | emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags, |
| 1791 | Pred, PredReg); |
| 1792 | } else { |
| 1793 | // Note: PredReg is operand 3 for ADJCALLSTACKUP. |
| 1794 | unsigned PredReg = Old->getOperand(3).getReg(); |
| 1795 | assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); |
| 1796 | emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags, |
| 1797 | Pred, PredReg); |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | MBB.erase(I); |
| 1802 | } |
| 1803 | |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1804 | /// Get the minimum constant for ARM that is greater than or equal to the |
| 1805 | /// argument. In ARM, constants can have any value that can be produced by |
| 1806 | /// rotating an 8-bit value to the right by an even number of bits within a |
| 1807 | /// 32-bit word. |
| 1808 | static uint32_t alignToARMConstant(uint32_t Value) { |
| 1809 | unsigned Shifted = 0; |
| 1810 | |
| 1811 | if (Value == 0) |
| 1812 | return 0; |
| 1813 | |
| 1814 | while (!(Value & 0xC0000000)) { |
| 1815 | Value = Value << 2; |
| 1816 | Shifted += 2; |
| 1817 | } |
| 1818 | |
| 1819 | bool Carry = (Value & 0x00FFFFFF); |
| 1820 | Value = ((Value & 0xFF000000) >> 24) + Carry; |
| 1821 | |
| 1822 | if (Value & 0x0000100) |
| 1823 | Value = Value & 0x000001FC; |
| 1824 | |
| 1825 | if (Shifted > 24) |
| 1826 | Value = Value >> (Shifted - 24); |
| 1827 | else |
| 1828 | Value = Value << (24 - Shifted); |
| 1829 | |
| 1830 | return Value; |
| 1831 | } |
| 1832 | |
| 1833 | // The stack limit in the TCB is set to this many bytes above the actual |
| 1834 | // stack limit. |
| 1835 | static const uint64_t kSplitStackAvailable = 256; |
| 1836 | |
| 1837 | // Adjust the function prologue to enable split stacks. This currently only |
| 1838 | // supports android and linux. |
| 1839 | // |
| 1840 | // The ABI of the segmented stack prologue is a little arbitrarily chosen, but |
| 1841 | // must be well defined in order to allow for consistent implementations of the |
| 1842 | // __morestack helper function. The ABI is also not a normal ABI in that it |
| 1843 | // doesn't follow the normal calling conventions because this allows the |
| 1844 | // prologue of each function to be optimized further. |
| 1845 | // |
| 1846 | // Currently, the ABI looks like (when calling __morestack) |
| 1847 | // |
| 1848 | // * r4 holds the minimum stack size requested for this function call |
| 1849 | // * r5 holds the stack size of the arguments to the function |
| 1850 | // * the beginning of the function is 3 instructions after the call to |
| 1851 | // __morestack |
| 1852 | // |
| 1853 | // Implementations of __morestack should use r4 to allocate a new stack, r5 to |
| 1854 | // place the arguments on to the new stack, and the 3-instruction knowledge to |
| 1855 | // jump directly to the body of the function when working on the new stack. |
| 1856 | // |
| 1857 | // An old (and possibly no longer compatible) implementation of __morestack for |
| 1858 | // ARM can be found at [1]. |
| 1859 | // |
| 1860 | // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S |
| 1861 | void ARMFrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const { |
| 1862 | unsigned Opcode; |
| 1863 | unsigned CFIIndex; |
| 1864 | const ARMSubtarget *ST = &MF.getTarget().getSubtarget<ARMSubtarget>(); |
| 1865 | bool Thumb = ST->isThumb(); |
| 1866 | |
| 1867 | // Sadly, this currently doesn't support varargs, platforms other than |
| 1868 | // android/linux. Note that thumb1/thumb2 are support for android/linux. |
| 1869 | if (MF.getFunction()->isVarArg()) |
| 1870 | report_fatal_error("Segmented stacks do not support vararg functions."); |
| 1871 | if (!ST->isTargetAndroid() && !ST->isTargetLinux()) |
Alp Toker | 16f98b2 | 2014-04-09 14:47:27 +0000 | [diff] [blame] | 1872 | report_fatal_error("Segmented stacks not supported on this platform."); |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1873 | |
| 1874 | MachineBasicBlock &prologueMBB = MF.front(); |
| 1875 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1876 | MachineModuleInfo &MMI = MF.getMMI(); |
| 1877 | MCContext &Context = MMI.getContext(); |
| 1878 | const MCRegisterInfo *MRI = Context.getRegisterInfo(); |
| 1879 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1880 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1881 | ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>(); |
| 1882 | DebugLoc DL; |
| 1883 | |
Tim Northover | f9e798b | 2014-05-22 13:03:43 +0000 | [diff] [blame] | 1884 | uint64_t StackSize = MFI->getStackSize(); |
| 1885 | |
| 1886 | // Do not generate a prologue for functions with a stack of size zero |
| 1887 | if (StackSize == 0) |
| 1888 | return; |
| 1889 | |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1890 | // Use R4 and R5 as scratch registers. |
| 1891 | // We save R4 and R5 before use and restore them before leaving the function. |
| 1892 | unsigned ScratchReg0 = ARM::R4; |
| 1893 | unsigned ScratchReg1 = ARM::R5; |
| 1894 | uint64_t AlignedStackSize; |
| 1895 | |
| 1896 | MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock(); |
| 1897 | MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock(); |
| 1898 | MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock(); |
| 1899 | MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock(); |
| 1900 | MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock(); |
| 1901 | |
| 1902 | for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(), |
| 1903 | e = prologueMBB.livein_end(); |
| 1904 | i != e; ++i) { |
| 1905 | AllocMBB->addLiveIn(*i); |
| 1906 | GetMBB->addLiveIn(*i); |
| 1907 | McrMBB->addLiveIn(*i); |
| 1908 | PrevStackMBB->addLiveIn(*i); |
| 1909 | PostStackMBB->addLiveIn(*i); |
| 1910 | } |
| 1911 | |
| 1912 | MF.push_front(PostStackMBB); |
| 1913 | MF.push_front(AllocMBB); |
| 1914 | MF.push_front(GetMBB); |
| 1915 | MF.push_front(McrMBB); |
| 1916 | MF.push_front(PrevStackMBB); |
| 1917 | |
| 1918 | // The required stack size that is aligned to ARM constant criterion. |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1919 | AlignedStackSize = alignToARMConstant(StackSize); |
| 1920 | |
| 1921 | // When the frame size is less than 256 we just compare the stack |
| 1922 | // boundary directly to the value of the stack pointer, per gcc. |
| 1923 | bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable; |
| 1924 | |
| 1925 | // We will use two of the callee save registers as scratch registers so we |
| 1926 | // need to save those registers onto the stack. |
| 1927 | // We will use SR0 to hold stack limit and SR1 to hold the stack size |
| 1928 | // requested and arguments for __morestack(). |
| 1929 | // SR0: Scratch Register #0 |
| 1930 | // SR1: Scratch Register #1 |
| 1931 | // push {SR0, SR1} |
| 1932 | if (Thumb) { |
| 1933 | AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))) |
| 1934 | .addReg(ScratchReg0).addReg(ScratchReg1); |
| 1935 | } else { |
| 1936 | AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD)) |
| 1937 | .addReg(ARM::SP, RegState::Define).addReg(ARM::SP)) |
| 1938 | .addReg(ScratchReg0).addReg(ScratchReg1); |
| 1939 | } |
| 1940 | |
| 1941 | // Emit the relevant DWARF information about the change in stack pointer as |
| 1942 | // well as where to find both r4 and r5 (the callee-save registers) |
| 1943 | CFIIndex = |
| 1944 | MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8)); |
| 1945 | BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1946 | .addCFIIndex(CFIIndex); |
| 1947 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 1948 | nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4)); |
| 1949 | BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1950 | .addCFIIndex(CFIIndex); |
| 1951 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 1952 | nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8)); |
| 1953 | BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1954 | .addCFIIndex(CFIIndex); |
| 1955 | |
| 1956 | // mov SR1, sp |
| 1957 | if (Thumb) { |
| 1958 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1) |
| 1959 | .addReg(ARM::SP)); |
| 1960 | } else if (CompareStackPointer) { |
| 1961 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1) |
| 1962 | .addReg(ARM::SP)).addReg(0); |
| 1963 | } |
| 1964 | |
| 1965 | // sub SR1, sp, #StackSize |
| 1966 | if (!CompareStackPointer && Thumb) { |
| 1967 | AddDefaultPred( |
| 1968 | AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)) |
| 1969 | .addReg(ScratchReg1).addImm(AlignedStackSize)); |
| 1970 | } else if (!CompareStackPointer) { |
| 1971 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1) |
| 1972 | .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0); |
| 1973 | } |
| 1974 | |
| 1975 | if (Thumb && ST->isThumb1Only()) { |
| 1976 | unsigned PCLabelId = ARMFI->createPICLabelUId(); |
| 1977 | ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create( |
Oliver Stannard | 92e0fc0 | 2014-04-03 08:45:16 +0000 | [diff] [blame] | 1978 | MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0); |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1979 | MachineConstantPool *MCP = MF.getConstantPool(); |
| 1980 | unsigned CPI = MCP->getConstantPoolIndex(NewCPV, MF.getAlignment()); |
| 1981 | |
| 1982 | // ldr SR0, [pc, offset(STACK_LIMIT)] |
| 1983 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0) |
| 1984 | .addConstantPoolIndex(CPI)); |
| 1985 | |
| 1986 | // ldr SR0, [SR0] |
| 1987 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0) |
| 1988 | .addReg(ScratchReg0).addImm(0)); |
| 1989 | } else { |
| 1990 | // Get TLS base address from the coprocessor |
| 1991 | // mrc p15, #0, SR0, c13, c0, #3 |
| 1992 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0) |
| 1993 | .addImm(15) |
| 1994 | .addImm(0) |
| 1995 | .addImm(13) |
| 1996 | .addImm(0) |
| 1997 | .addImm(3)); |
| 1998 | |
| 1999 | // Use the last tls slot on android and a private field of the TCP on linux. |
| 2000 | assert(ST->isTargetAndroid() || ST->isTargetLinux()); |
| 2001 | unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1; |
| 2002 | |
| 2003 | // Get the stack limit from the right offset |
| 2004 | // ldr SR0, [sr0, #4 * TlsOffset] |
| 2005 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0) |
| 2006 | .addReg(ScratchReg0).addImm(4 * TlsOffset)); |
| 2007 | } |
| 2008 | |
| 2009 | // Compare stack limit with stack size requested. |
| 2010 | // cmp SR0, SR1 |
| 2011 | Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr; |
| 2012 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode)) |
| 2013 | .addReg(ScratchReg0) |
| 2014 | .addReg(ScratchReg1)); |
| 2015 | |
| 2016 | // This jump is taken if StackLimit < SP - stack required. |
| 2017 | Opcode = Thumb ? ARM::tBcc : ARM::Bcc; |
| 2018 | BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB) |
| 2019 | .addImm(ARMCC::LO) |
| 2020 | .addReg(ARM::CPSR); |
| 2021 | |
| 2022 | |
| 2023 | // Calling __morestack(StackSize, Size of stack arguments). |
| 2024 | // __morestack knows that the stack size requested is in SR0(r4) |
| 2025 | // and amount size of stack arguments is in SR1(r5). |
| 2026 | |
| 2027 | // Pass first argument for the __morestack by Scratch Register #0. |
| 2028 | // The amount size of stack required |
| 2029 | if (Thumb) { |
| 2030 | AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), |
| 2031 | ScratchReg0)).addImm(AlignedStackSize)); |
| 2032 | } else { |
| 2033 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0) |
| 2034 | .addImm(AlignedStackSize)).addReg(0); |
| 2035 | } |
| 2036 | // Pass second argument for the __morestack by Scratch Register #1. |
| 2037 | // The amount size of stack consumed to save function arguments. |
| 2038 | if (Thumb) { |
| 2039 | AddDefaultPred( |
| 2040 | AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)) |
| 2041 | .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))); |
| 2042 | } else { |
| 2043 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1) |
| 2044 | .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))) |
| 2045 | .addReg(0); |
| 2046 | } |
| 2047 | |
| 2048 | // push {lr} - Save return address of this function. |
| 2049 | if (Thumb) { |
| 2050 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))) |
| 2051 | .addReg(ARM::LR); |
| 2052 | } else { |
| 2053 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD)) |
| 2054 | .addReg(ARM::SP, RegState::Define) |
| 2055 | .addReg(ARM::SP)) |
| 2056 | .addReg(ARM::LR); |
| 2057 | } |
| 2058 | |
| 2059 | // Emit the DWARF info about the change in stack as well as where to find the |
| 2060 | // previous link register |
| 2061 | CFIIndex = |
| 2062 | MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12)); |
| 2063 | BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2064 | .addCFIIndex(CFIIndex); |
| 2065 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 2066 | nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12)); |
| 2067 | BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2068 | .addCFIIndex(CFIIndex); |
| 2069 | |
| 2070 | // Call __morestack(). |
| 2071 | if (Thumb) { |
| 2072 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL))) |
| 2073 | .addExternalSymbol("__morestack"); |
| 2074 | } else { |
| 2075 | BuildMI(AllocMBB, DL, TII.get(ARM::BL)) |
| 2076 | .addExternalSymbol("__morestack"); |
| 2077 | } |
| 2078 | |
| 2079 | // pop {lr} - Restore return address of this original function. |
| 2080 | if (Thumb) { |
| 2081 | if (ST->isThumb1Only()) { |
| 2082 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) |
| 2083 | .addReg(ScratchReg0); |
| 2084 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR) |
| 2085 | .addReg(ScratchReg0)); |
| 2086 | } else { |
| 2087 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST)) |
| 2088 | .addReg(ARM::LR, RegState::Define) |
| 2089 | .addReg(ARM::SP, RegState::Define) |
| 2090 | .addReg(ARM::SP) |
| 2091 | .addImm(4)); |
| 2092 | } |
| 2093 | } else { |
| 2094 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) |
| 2095 | .addReg(ARM::SP, RegState::Define) |
| 2096 | .addReg(ARM::SP)) |
| 2097 | .addReg(ARM::LR); |
| 2098 | } |
| 2099 | |
| 2100 | // Restore SR0 and SR1 in case of __morestack() was called. |
| 2101 | // __morestack() will skip PostStackMBB block so we need to restore |
| 2102 | // scratch registers from here. |
| 2103 | // pop {SR0, SR1} |
| 2104 | if (Thumb) { |
| 2105 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) |
| 2106 | .addReg(ScratchReg0) |
| 2107 | .addReg(ScratchReg1); |
| 2108 | } else { |
| 2109 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) |
| 2110 | .addReg(ARM::SP, RegState::Define) |
| 2111 | .addReg(ARM::SP)) |
| 2112 | .addReg(ScratchReg0) |
| 2113 | .addReg(ScratchReg1); |
| 2114 | } |
| 2115 | |
| 2116 | // Update the CFA offset now that we've popped |
| 2117 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); |
| 2118 | BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2119 | .addCFIIndex(CFIIndex); |
| 2120 | |
| 2121 | // bx lr - Return from this function. |
| 2122 | Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET; |
| 2123 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode))); |
| 2124 | |
| 2125 | // Restore SR0 and SR1 in case of __morestack() was not called. |
| 2126 | // pop {SR0, SR1} |
| 2127 | if (Thumb) { |
| 2128 | AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))) |
| 2129 | .addReg(ScratchReg0) |
| 2130 | .addReg(ScratchReg1); |
| 2131 | } else { |
| 2132 | AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD)) |
| 2133 | .addReg(ARM::SP, RegState::Define) |
| 2134 | .addReg(ARM::SP)) |
| 2135 | .addReg(ScratchReg0) |
| 2136 | .addReg(ScratchReg1); |
| 2137 | } |
| 2138 | |
| 2139 | // Update the CFA offset now that we've popped |
| 2140 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); |
| 2141 | BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2142 | .addCFIIndex(CFIIndex); |
| 2143 | |
| 2144 | // Tell debuggers that r4 and r5 are now the same as they were in the |
| 2145 | // previous function, that they're the "Same Value". |
| 2146 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( |
| 2147 | nullptr, MRI->getDwarfRegNum(ScratchReg0, true))); |
| 2148 | BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2149 | .addCFIIndex(CFIIndex); |
| 2150 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( |
| 2151 | nullptr, MRI->getDwarfRegNum(ScratchReg1, true))); |
| 2152 | BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2153 | .addCFIIndex(CFIIndex); |
| 2154 | |
| 2155 | // Organizing MBB lists |
| 2156 | PostStackMBB->addSuccessor(&prologueMBB); |
| 2157 | |
| 2158 | AllocMBB->addSuccessor(PostStackMBB); |
| 2159 | |
| 2160 | GetMBB->addSuccessor(PostStackMBB); |
| 2161 | GetMBB->addSuccessor(AllocMBB); |
| 2162 | |
| 2163 | McrMBB->addSuccessor(GetMBB); |
| 2164 | |
| 2165 | PrevStackMBB->addSuccessor(McrMBB); |
| 2166 | |
| 2167 | #ifdef XDEBUG |
| 2168 | MF.verify(); |
| 2169 | #endif |
| 2170 | } |