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