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