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