blob: 6ce33733a63ac399eb8aeaf70cc35c768a34d38e [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
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 Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the ARM implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "ARMFrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "ARMBaseInstrInfo.h"
Evan Chenge45d6852011-01-11 21:46:47 +000016#include "ARMBaseRegisterInfo.h"
Oliver Stannardb14c6252014-04-02 16:10:33 +000017#include "ARMConstantPoolValue.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000018#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000019#include "MCTargetDesc/ARMAddressingModes.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000025#include "llvm/CodeGen/RegisterScavenging.h"
Tim Northovere0ccdc62015-10-28 22:46:43 +000026#include "llvm/MC/MCAsmInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Function.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000029#include "llvm/MC/MCContext.h"
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000030#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000032
33using namespace llvm;
34
Benjamin Kramer9fceb902012-02-24 22:09:25 +000035static cl::opt<bool>
Jakob Stoklund Olesen68a922c2012-01-06 22:19:37 +000036SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000037 cl::desc("Align ARM NEON spills in prolog and epilog"));
38
39static MachineBasicBlock::iterator
40skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
41 unsigned NumAlignedDPRCS2Regs);
42
Eric Christopher45fb7b62014-06-26 19:29:59 +000043ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
44 : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
45 STI(sti) {}
46
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000047bool ARMFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
48 // iOS always has a FP for backtracking, force other targets to keep their FP
49 // when doing FastISel. The emitted code is currently superior, and in cases
50 // like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
51 return TargetFrameLowering::noFramePointerElim(MF) ||
52 MF.getSubtarget<ARMSubtarget>().useFastISel();
53}
54
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000055/// hasFP - Return true if the specified function should have a dedicated frame
56/// pointer register. This is true if the function has variable sized allocas
57/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000058bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +000059 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000060
Evan Cheng801d98b2012-01-04 01:55:04 +000061 // iOS requires FP not to be clobbered for backtracing purpose.
Tim Northovere0ccdc62015-10-28 22:46:43 +000062 if (STI.isTargetIOS() || STI.isTargetWatchOS())
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000063 return true;
64
65 const MachineFrameInfo *MFI = MF.getFrameInfo();
66 // Always eliminate non-leaf frame pointers.
Nick Lewycky50f02cb2011-12-02 22:16:29 +000067 return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
68 MFI->hasCalls()) ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000069 RegInfo->needsStackRealignment(MF) ||
70 MFI->hasVarSizedObjects() ||
71 MFI->isFrameAddressTaken());
72}
73
Bob Wilson657f2272011-01-13 21:10:12 +000074/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
75/// not required, we reserve argument space for call sites in the function
76/// immediately on entry to the current function. This eliminates the need for
77/// add/sub sp brackets around call sites. Returns true if the call frame is
78/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000079bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000080 const MachineFrameInfo *FFI = MF.getFrameInfo();
81 unsigned CFSize = FFI->getMaxCallFrameSize();
82 // It's not always a good idea to include the call frame as part of the
83 // stack frame. ARM (especially Thumb) has small immediate offset to
84 // address the stack frame. So a large call frame can cause poor codegen
85 // and may even makes it impossible to scavenge a register.
86 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
87 return false;
88
89 return !MF.getFrameInfo()->hasVarSizedObjects();
90}
91
Bob Wilson657f2272011-01-13 21:10:12 +000092/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
93/// call frame pseudos can be simplified. Unlike most targets, having a FP
94/// is not sufficient here since we still may reference some objects via SP
95/// even when FP is available in Thumb2 mode.
96bool
97ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000098 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
99}
100
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000101static bool isCSRestore(MachineInstr *MI,
102 const ARMBaseInstrInfo &TII,
Craig Topper840beec2014-04-04 05:16:06 +0000103 const MCPhysReg *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000104 // Integer spill area is handled with "pop".
Tim Northover93bcc662013-11-08 17:18:07 +0000105 if (isPopOpcode(MI->getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000106 // The first two operands are predicates. The last two are
107 // imp-def and imp-use of SP. Check everything in between.
108 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
109 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
110 return false;
111 return true;
112 }
Owen Anderson2aedba62011-07-26 20:54:26 +0000113 if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
114 MI->getOpcode() == ARM::LDR_POST_REG ||
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000115 MI->getOpcode() == ARM::t2LDR_POST) &&
116 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
117 MI->getOperand(1).getReg() == ARM::SP)
118 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000119
120 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000121}
122
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000123static void emitRegPlusImmediate(
124 bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
125 const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
126 unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
127 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000128 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000129 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000130 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000131 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000132 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000133 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000134}
135
Tim Northoverc9432eb2013-11-04 23:04:15 +0000136static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000137 MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
Tim Northoverc9432eb2013-11-04 23:04:15 +0000138 const ARMBaseInstrInfo &TII, int NumBytes,
139 unsigned MIFlags = MachineInstr::NoFlags,
140 ARMCC::CondCodes Pred = ARMCC::AL,
141 unsigned PredReg = 0) {
142 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
143 MIFlags, Pred, PredReg);
144}
145
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000146static int sizeOfSPAdjustment(const MachineInstr *MI) {
Tim Northover603d3162014-11-14 22:45:33 +0000147 int RegSize;
148 switch (MI->getOpcode()) {
149 case ARM::VSTMDDB_UPD:
150 RegSize = 8;
151 break;
152 case ARM::STMDB_UPD:
153 case ARM::t2STMDB_UPD:
154 RegSize = 4;
155 break;
156 case ARM::t2STR_PRE:
157 case ARM::STR_PRE_IMM:
158 return 4;
159 default:
160 llvm_unreachable("Unknown push or pop like instruction");
161 }
162
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000163 int count = 0;
164 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
165 // pred) so the list starts at 4.
166 for (int i = MI->getNumOperands() - 1; i >= 4; --i)
Tim Northover603d3162014-11-14 22:45:33 +0000167 count += RegSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000168 return count;
169}
170
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000171static bool WindowsRequiresStackProbe(const MachineFunction &MF,
172 size_t StackSizeInBytes) {
173 const MachineFrameInfo *MFI = MF.getFrameInfo();
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000174 const Function *F = MF.getFunction();
175 unsigned StackProbeSize = (MFI->getStackProtectorIndex() > 0) ? 4080 : 4096;
176 if (F->hasFnAttribute("stack-probe-size"))
177 F->getFnAttribute("stack-probe-size")
178 .getValueAsString()
179 .getAsInteger(0, StackProbeSize);
180 return StackSizeInBytes >= StackProbeSize;
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000181}
182
Tim Northover603d3162014-11-14 22:45:33 +0000183namespace {
184struct StackAdjustingInsts {
185 struct InstInfo {
186 MachineBasicBlock::iterator I;
187 unsigned SPAdjust;
188 bool BeforeFPSet;
189 };
190
191 SmallVector<InstInfo, 4> Insts;
192
193 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
194 bool BeforeFPSet = false) {
195 InstInfo Info = {I, SPAdjust, BeforeFPSet};
196 Insts.push_back(Info);
197 }
198
199 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
200 auto Info = std::find_if(Insts.begin(), Insts.end(),
201 [&](InstInfo &Info) { return Info.I == I; });
202 assert(Info != Insts.end() && "invalid sp adjusting instruction");
203 Info->SPAdjust += ExtraBytes;
204 }
205
206 void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000207 const DebugLoc &dl, const ARMBaseInstrInfo &TII,
208 bool HasFP) {
Tim Northover603d3162014-11-14 22:45:33 +0000209 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 Prantlb9fa9452014-12-16 00:20:49 +0000218 TII.get(TargetOpcode::CFI_INSTRUCTION))
219 .addCFIIndex(CFIIndex)
220 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000221 }
222 }
223};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000224}
Tim Northover603d3162014-11-14 22:45:33 +0000225
Kristof Beyls933de7a2015-01-08 15:09:14 +0000226/// 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.
233static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
234 const TargetInstrInfo &TII,
235 MachineBasicBlock &MBB,
236 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000237 const DebugLoc &DL, const unsigned Reg,
Kristof Beyls933de7a2015-01-08 15:09:14 +0000238 const unsigned Alignment,
239 const bool MustBeSingleInstruction) {
Eric Christopher1b21f002015-01-29 00:19:33 +0000240 const ARMSubtarget &AST =
241 static_cast<const ARMSubtarget &>(MF.getSubtarget());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000242 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 Colombet61b305e2015-05-05 17:38:16 +0000289void ARMFrameLowering::emitPrologue(MachineFunction &MF,
290 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000291 MachineBasicBlock::iterator MBBI = MBB.begin();
292 MachineFrameInfo *MFI = MF.getFrameInfo();
293 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000294 MachineModuleInfo &MMI = MF.getMMI();
295 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000296 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000297 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopher1b21f002015-01-29 00:19:33 +0000298 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
299 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000300 assert(!AFI->isThumb1OnlyFunction() &&
301 "This emitPrologue does not support Thumb1!");
302 bool isARM = !AFI->isThumbFunction();
Eric Christopher1b21f002015-01-29 00:19:33 +0000303 unsigned Align = STI.getFrameLowering()->getStackAlignment();
Tim Northover775aaeb2015-11-05 21:54:58 +0000304 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
305 unsigned NumBytes = MFI->getStackSize();
306 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
307
308 // Debug location must be unknown since the first debug location is used
309 // to determine the end of the prologue.
310 DebugLoc dl;
311
312 unsigned FramePtr = RegInfo->getFrameRegister(MF);
313
314 // Determine the sizes of each callee-save spill areas and record which frame
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000315 // belongs to which callee-save spill areas.
316 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
317 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000318 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000319
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000320 // All calls are tail calls in GHC calling conv, and functions have no
321 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000322 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
323 return;
324
Tim Northover603d3162014-11-14 22:45:33 +0000325 StackAdjustingInsts DefCFAOffsetCandidates;
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000326 bool HasFP = hasFP(MF);
Tim Northover603d3162014-11-14 22:45:33 +0000327
Oliver Stannardd55e1152014-03-05 15:25:27 +0000328 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000329 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000330 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000331 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000332 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000333 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000334
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000335 if (!AFI->hasStackFrame() &&
336 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000337 if (NumBytes - ArgRegsSaveSize != 0) {
338 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000339 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000340 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
341 NumBytes - ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000342 }
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000343 DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000344 return;
345 }
346
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000347 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000348 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
349 unsigned Reg = CSI[i].getReg();
350 int FI = CSI[i].getFrameIdx();
351 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000352 case ARM::R8:
353 case ARM::R9:
354 case ARM::R10:
355 case ARM::R11:
356 case ARM::R12:
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000357 if (STI.splitFramePushPop()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000358 GPRCS2Size += 4;
359 break;
360 }
361 // fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +0000362 case ARM::R0:
363 case ARM::R1:
364 case ARM::R2:
365 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000366 case ARM::R4:
367 case ARM::R5:
368 case ARM::R6:
369 case ARM::R7:
370 case ARM::LR:
371 if (Reg == FramePtr)
372 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000373 GPRCS1Size += 4;
374 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000375 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000376 // This is a DPR. Exclude the aligned DPRCS2 spills.
377 if (Reg == ARM::D8)
378 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000379 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000380 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000381 }
382 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000383
Eric Christopherb006fc92010-11-18 19:40:05 +0000384 // Move past area 1.
Tim Northover603d3162014-11-14 22:45:33 +0000385 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
386 if (GPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000387 GPRCS1Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000388 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
389 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000390
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000391 // Determine starting offsets of spill areas.
Tim Northover228c9432014-11-05 00:27:13 +0000392 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
393 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
394 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
395 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
396 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000397 int FramePtrOffsetInPush = 0;
398 if (HasFP) {
Tim Northover603d3162014-11-14 22:45:33 +0000399 FramePtrOffsetInPush =
400 MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000401 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
402 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000403 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000404 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
405 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
406 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
407
Tim Northoverc9432eb2013-11-04 23:04:15 +0000408 // Move past area 2.
Tim Northover603d3162014-11-14 22:45:33 +0000409 if (GPRCS2Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000410 GPRCS2Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000411 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
412 }
Tim Northoverc9432eb2013-11-04 23:04:15 +0000413
Tim Northover228c9432014-11-05 00:27:13 +0000414 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
415 // .cfi_offset operations will reflect that.
416 if (DPRGapSize) {
417 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
Tim Northover603d3162014-11-14 22:45:33 +0000418 if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize))
419 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
420 else {
Tim Northover228c9432014-11-05 00:27:13 +0000421 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
422 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000423 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
424 }
Tim Northover228c9432014-11-05 00:27:13 +0000425 }
426
Eric Christopherb006fc92010-11-18 19:40:05 +0000427 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000428 if (DPRCSSize > 0) {
Evan Cheng70d29632011-02-25 00:24:46 +0000429 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000430 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000431 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
432 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI));
Tim Northover93bcc662013-11-08 17:18:07 +0000433 LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000434 }
Evan Cheng70d29632011-02-25 00:24:46 +0000435 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000436
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000437 // Move past the aligned DPRCS2 area.
438 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
439 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
440 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
441 // leaves the stack pointer pointing to the DPRCS2 area.
442 //
443 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
444 NumBytes += MFI->getObjectOffset(D8SpillFI);
445 } else
446 NumBytes = DPRCSOffset;
447
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000448 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
449 uint32_t NumWords = NumBytes >> 2;
450
451 if (NumWords < 65536)
452 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000453 .addImm(NumWords)
454 .setMIFlags(MachineInstr::FrameSetup));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000455 else
456 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000457 .addImm(NumWords)
458 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000459
460 switch (TM.getCodeModel()) {
461 case CodeModel::Small:
462 case CodeModel::Medium:
463 case CodeModel::Default:
464 case CodeModel::Kernel:
465 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
466 .addImm((unsigned)ARMCC::AL).addReg(0)
467 .addExternalSymbol("__chkstk")
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000468 .addReg(ARM::R4, RegState::Implicit)
469 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000470 break;
471 case CodeModel::Large:
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000472 case CodeModel::JITDefault:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000473 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000474 .addExternalSymbol("__chkstk")
475 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000476
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000477 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
478 .addImm((unsigned)ARMCC::AL).addReg(0)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000479 .addReg(ARM::R12, RegState::Kill)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000480 .addReg(ARM::R4, RegState::Implicit)
481 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000482 break;
483 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000484
485 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr),
486 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +0000487 .addReg(ARM::SP, RegState::Kill)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000488 .addReg(ARM::R4, RegState::Kill)
489 .setMIFlags(MachineInstr::FrameSetup)));
490 NumBytes = 0;
491 }
492
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000493 if (NumBytes) {
494 // Adjust SP after all the callee-save spills.
Tim Northoverbeb5bcc2015-09-23 22:21:09 +0000495 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
496 tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes))
Tim Northover603d3162014-11-14 22:45:33 +0000497 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
498 else {
Tim Northover93bcc662013-11-08 17:18:07 +0000499 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
500 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000501 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
502 }
Tim Northover93bcc662013-11-08 17:18:07 +0000503
Evan Chengeb56dca2010-11-22 18:12:04 +0000504 if (HasFP && isARM)
505 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
506 // Note it's not safe to do this in Thumb2 mode because it would have
507 // taken two instructions:
508 // mov sp, r7
509 // sub sp, #24
510 // If an interrupt is taken between the two instructions, then sp is in
511 // an inconsistent state (pointing to the middle of callee-saved area).
512 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000513 AFI->setShouldRestoreSPFromFP(true);
514 }
515
Tim Northover603d3162014-11-14 22:45:33 +0000516 // Set FP to point to the stack slot that contains the previous FP.
517 // For iOS, FP is R7, which has now been stored in spill area 1.
518 // Otherwise, if this is not iOS, all the callee-saved registers go
519 // into spill area 1, including the FP in R11. In either case, it
520 // is in area one and the adjustment needs to take place just after
521 // that push.
522 if (HasFP) {
523 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
524 unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push);
525 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
526 dl, TII, FramePtr, ARM::SP,
527 PushSize + FramePtrOffsetInPush,
528 MachineInstr::FrameSetup);
529 if (FramePtrOffsetInPush + PushSize != 0) {
530 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
531 nullptr, MRI->getDwarfRegNum(FramePtr, true),
532 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
533 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000534 .addCFIIndex(CFIIndex)
535 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000536 } else {
537 unsigned CFIIndex =
538 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
539 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
540 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000541 .addCFIIndex(CFIIndex)
542 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000543 }
544 }
545
546 // Now that the prologue's actual instructions are finalised, we can insert
547 // the necessary DWARF cf instructions to describe the situation. Start by
548 // recording where each register ended up:
549 if (GPRCS1Size > 0) {
550 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
551 int CFIIndex;
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000552 for (const auto &Entry : CSI) {
553 unsigned Reg = Entry.getReg();
554 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000555 switch (Reg) {
556 case ARM::R8:
557 case ARM::R9:
558 case ARM::R10:
559 case ARM::R11:
560 case ARM::R12:
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000561 if (STI.splitFramePushPop())
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000562 break;
563 // fallthrough
564 case ARM::R0:
565 case ARM::R1:
566 case ARM::R2:
567 case ARM::R3:
568 case ARM::R4:
569 case ARM::R5:
570 case ARM::R6:
571 case ARM::R7:
572 case ARM::LR:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000573 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
574 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
575 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000576 .addCFIIndex(CFIIndex)
577 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000578 break;
579 }
580 }
581 }
582
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000583 if (GPRCS2Size > 0) {
Tim Northover603d3162014-11-14 22:45:33 +0000584 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000585 for (const auto &Entry : CSI) {
586 unsigned Reg = Entry.getReg();
587 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000588 switch (Reg) {
589 case ARM::R8:
590 case ARM::R9:
591 case ARM::R10:
592 case ARM::R11:
593 case ARM::R12:
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000594 if (STI.splitFramePushPop()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000595 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000596 unsigned Offset = MFI->getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000597 unsigned CFIIndex = MMI.addFrameInst(
598 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
599 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000600 .addCFIIndex(CFIIndex)
601 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000602 }
603 break;
604 }
605 }
606 }
607
608 if (DPRCSSize > 0) {
609 // Since vpush register list cannot have gaps, there may be multiple vpush
610 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000611 MachineBasicBlock::iterator Pos = std::next(LastPush);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000612 for (const auto &Entry : CSI) {
613 unsigned Reg = Entry.getReg();
614 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000615 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
616 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
617 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
618 unsigned Offset = MFI->getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000619 unsigned CFIIndex = MMI.addFrameInst(
620 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover603d3162014-11-14 22:45:33 +0000621 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000622 .addCFIIndex(CFIIndex)
623 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000624 }
625 }
626 }
627
Tim Northover603d3162014-11-14 22:45:33 +0000628 // Now we can emit descriptions of where the canonical frame address was
629 // throughout the process. If we have a frame pointer, it takes over the job
630 // half-way through, so only the first few .cfi_def_cfa_offset instructions
631 // actually get emitted.
632 DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
Tim Northover93bcc662013-11-08 17:18:07 +0000633
Evan Chengeb56dca2010-11-22 18:12:04 +0000634 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000635 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
636 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000637
638 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
639 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000640 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000641 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
642
643 // If we need dynamic stack realignment, do it here. Be paranoid and make
644 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000645 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000646 // realigned.
647 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000648 unsigned MaxAlign = MFI->getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +0000649 assert(!AFI->isThumb1OnlyFunction());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000650 if (!AFI->isThumbFunction()) {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000651 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
652 false);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000653 } else {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000654 // We cannot use sp as source/dest register here, thus we're using r4 to
655 // perform the calculations. We're emitting the following sequence:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000656 // mov r4, sp
Kristof Beyls933de7a2015-01-08 15:09:14 +0000657 // -- use emitAligningInstructions to produce best sequence to zero
658 // -- out lower bits in r4
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000659 // mov sp, r4
660 // FIXME: It will be better just to find spare register here.
Jim Grosbache9cc9012011-06-30 23:38:17 +0000661 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Kristof Beyls933de7a2015-01-08 15:09:14 +0000662 .addReg(ARM::SP, RegState::Kill));
663 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
664 false);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000665 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Kristof Beyls933de7a2015-01-08 15:09:14 +0000666 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000667 }
668
669 AFI->setShouldRestoreSPFromFP(true);
670 }
671
672 // If we need a base pointer, set it up here. It's whatever the value
673 // of the stack pointer is at this point. Any variable size objects
674 // will be allocated after this, so we can still use the base pointer
675 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000676 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000677 if (RegInfo->hasBasePointer(MF)) {
678 if (isARM)
679 BuildMI(MBB, MBBI, dl,
680 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
681 .addReg(ARM::SP)
682 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
683 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000684 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000685 RegInfo->getBaseRegister())
686 .addReg(ARM::SP));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000687 }
688
689 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000690 // the sp from fp. We can assume there's an FP here since hasFP already
691 // checks for hasVarSizedObjects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000692 if (MFI->hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000693 AFI->setShouldRestoreSPFromFP(true);
694}
695
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000696void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000697 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000698 MachineFrameInfo *MFI = MF.getFrameInfo();
699 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000700 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000701 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000702 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000703 assert(!AFI->isThumb1OnlyFunction() &&
704 "This emitEpilogue does not support Thumb1!");
705 bool isARM = !AFI->isThumbFunction();
706
Tim Northover8cda34f2015-03-11 18:54:22 +0000707 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000708 int NumBytes = (int)MFI->getStackSize();
709 unsigned FramePtr = RegInfo->getFrameRegister(MF);
710
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000711 // All calls are tail calls in GHC calling conv, and functions have no
712 // prologue/epilogue.
Quentin Colombet71a71482015-07-20 21:42:14 +0000713 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000714 return;
Quentin Colombet71a71482015-07-20 21:42:14 +0000715
716 // First put ourselves on the first (from top) terminator instructions.
717 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
718 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Eric Christopherb3322362012-08-03 00:05:53 +0000719
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000720 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000721 if (NumBytes - ArgRegsSaveSize != 0)
722 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000723 } else {
724 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000725 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000726 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000727 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000728 --MBBI;
Tim Northover93bcc662013-11-08 17:18:07 +0000729 } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000730 if (!isCSRestore(MBBI, TII, CSRegs))
731 ++MBBI;
732 }
733
734 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000735 NumBytes -= (ArgRegsSaveSize +
736 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000737 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000738 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000739 AFI->getDPRCalleeSavedAreaSize());
740
741 // Reset SP based on frame pointer only if the stack frame extends beyond
742 // frame pointer stack slot or target is ELF and the function has FP.
743 if (AFI->shouldRestoreSPFromFP()) {
744 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
745 if (NumBytes) {
746 if (isARM)
747 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
748 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000749 else {
750 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000751 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000752 // mov sp, r7
753 // sub sp, #24
754 // This is bad, if an interrupt is taken after the mov, sp is in an
755 // inconsistent state.
756 // Use the first callee-saved register as a scratch register.
Matthias Braun02564862015-07-14 17:17:13 +0000757 assert(!MFI->getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000758 "No scratch register to restore SP from FP!");
759 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000760 ARMCC::AL, 0, TII);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000761 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000762 ARM::SP)
763 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000764 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000765 } else {
766 // Thumb2 or ARM.
767 if (isARM)
768 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
769 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
770 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000771 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000772 ARM::SP)
773 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000774 }
Tim Northoverdee86042013-12-02 14:46:26 +0000775 } else if (NumBytes &&
Tim Northovere4def5e2013-12-05 11:02:02 +0000776 !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000777 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000778
Eric Christopherb006fc92010-11-18 19:40:05 +0000779 // Increment past our save areas.
Evan Cheng70d29632011-02-25 00:24:46 +0000780 if (AFI->getDPRCalleeSavedAreaSize()) {
781 MBBI++;
782 // Since vpop register list cannot have gaps, there may be multiple vpop
783 // instructions in the epilogue.
784 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
785 MBBI++;
786 }
Tim Northover228c9432014-11-05 00:27:13 +0000787 if (AFI->getDPRCalleeSavedGapSize()) {
788 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
789 "unexpected DPR alignment gap");
790 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
791 }
792
Eric Christopherb006fc92010-11-18 19:40:05 +0000793 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
794 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000795 }
796
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000797 if (ArgRegsSaveSize)
798 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000799}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000800
Bob Wilson657f2272011-01-13 21:10:12 +0000801/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
802/// debug info. It's the same as what we use for resolving the code-gen
803/// references for now. FIXME: This can go wrong when references are
804/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000805int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000806ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000807 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000808 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
809}
810
811int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000812ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000813 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000814 int SPAdj) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000815 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000816 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000817 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000818 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
819 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
820 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
821 bool isFixed = MFI->isFixedObjectIndex(FI);
822
823 FrameReg = ARM::SP;
824 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000825
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000826 // SP can move around if there are allocas. We may also lose track of SP
827 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000828 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000829
Anton Korobeynikov46877782010-11-20 15:59:32 +0000830 // When dynamically realigning the stack, use the frame pointer for
831 // parameters, and the stack/base pointer for locals.
832 if (RegInfo->needsStackRealignment(MF)) {
833 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
834 if (isFixed) {
835 FrameReg = RegInfo->getFrameRegister(MF);
836 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000837 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000838 assert(RegInfo->hasBasePointer(MF) &&
839 "VLAs and dynamic stack alignment, but missing base pointer!");
840 FrameReg = RegInfo->getBaseRegister();
841 }
842 return Offset;
843 }
844
845 // If there is a frame pointer, use it when we can.
846 if (hasFP(MF) && AFI->hasStackFrame()) {
847 // Use frame pointer to reference fixed objects. Use it for locals if
848 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000849 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000850 FrameReg = RegInfo->getFrameRegister(MF);
851 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000852 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000853 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000854 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000855 // Try to use the frame pointer if we can, else use the base pointer
856 // since it's available. This is handy for the emergency spill slot, in
857 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000858 if (FPOffset >= -255 && FPOffset < 0) {
859 FrameReg = RegInfo->getFrameRegister(MF);
860 return FPOffset;
861 }
Evan Chengc0d20042011-04-22 01:42:52 +0000862 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000863 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000864 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000865 // ldr <rd>, [sp, #<imm8>]
866 // if at all possible to save space.
867 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
868 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000869 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000870 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000871 if (FPOffset >= -255 && FPOffset < 0) {
872 FrameReg = RegInfo->getFrameRegister(MF);
873 return FPOffset;
874 }
875 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
876 // Otherwise, use SP or FP, whichever is closer to the stack slot.
877 FrameReg = RegInfo->getFrameRegister(MF);
878 return FPOffset;
879 }
880 }
881 // Use the base pointer if we have one.
882 if (RegInfo->hasBasePointer(MF))
883 FrameReg = RegInfo->getBaseRegister();
884 return Offset;
885}
886
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000887void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000888 MachineBasicBlock::iterator MI,
889 const std::vector<CalleeSavedInfo> &CSI,
890 unsigned StmOpc, unsigned StrOpc,
891 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000892 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000893 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000894 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000895 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000896 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
897
898 DebugLoc DL;
899
900 SmallVector<std::pair<unsigned,bool>, 4> Regs;
901 unsigned i = CSI.size();
Evan Cheng775ead32010-12-07 23:08:38 +0000902 while (i != 0) {
903 unsigned LastReg = 0;
904 for (; i != 0; --i) {
905 unsigned Reg = CSI[i-1].getReg();
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000906 if (!(Func)(Reg, STI.splitFramePushPop())) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000907
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000908 // D-registers in the aligned area DPRCS2 are NOT spilled here.
909 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
910 continue;
911
Matthias Braun707e02c2016-04-13 21:43:25 +0000912 bool isLiveIn = MF.getRegInfo().isLiveIn(Reg);
913 if (!isLiveIn)
Evan Cheng775ead32010-12-07 23:08:38 +0000914 MBB.addLiveIn(Reg);
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000915 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000916 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000917 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000918 if (NoGap && LastReg && LastReg != Reg-1)
919 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000920 LastReg = Reg;
Matthias Braun707e02c2016-04-13 21:43:25 +0000921 // Do not set a kill flag on values that are also marked as live-in. This
922 // happens with the @llvm-returnaddress intrinsic and with arguments
923 // passed in callee saved registers.
924 // Omitting the kill flags is conservatively correct even if the live-in
925 // is not used after all.
926 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000927 }
928
Jim Grosbach5fccad82010-12-09 18:31:13 +0000929 if (Regs.empty())
930 continue;
931 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000932 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000933 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000934 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng775ead32010-12-07 23:08:38 +0000935 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
936 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +0000937 } else if (Regs.size() == 1) {
938 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
939 ARM::SP)
940 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000941 .addReg(ARM::SP).setMIFlags(MIFlags)
942 .addImm(-4);
Jim Grosbach5fccad82010-12-09 18:31:13 +0000943 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000944 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000945 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +0000946
947 // Put any subsequent vpush instructions before this one: they will refer to
948 // higher register numbers so need to be pushed first in order to preserve
949 // monotonicity.
Quentin Colombet71a71482015-07-20 21:42:14 +0000950 if (MI != MBB.begin())
951 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000952 }
Evan Cheng775ead32010-12-07 23:08:38 +0000953}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000954
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000955void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000956 MachineBasicBlock::iterator MI,
957 const std::vector<CalleeSavedInfo> &CSI,
958 unsigned LdmOpc, unsigned LdrOpc,
959 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000960 bool(*Func)(unsigned, bool),
961 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +0000962 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000963 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Evan Cheng775ead32010-12-07 23:08:38 +0000964 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet71a71482015-07-20 21:42:14 +0000965 DebugLoc DL;
966 bool isTailCall = false;
967 bool isInterrupt = false;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000968 bool isTrap = false;
Quentin Colombet71a71482015-07-20 21:42:14 +0000969 if (MBB.end() != MI) {
970 DL = MI->getDebugLoc();
971 unsigned RetOpcode = MI->getOpcode();
972 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
973 isInterrupt =
974 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000975 isTrap =
976 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
977 RetOpcode == ARM::tTRAP;
Quentin Colombet71a71482015-07-20 21:42:14 +0000978 }
Evan Cheng775ead32010-12-07 23:08:38 +0000979
980 SmallVector<unsigned, 4> Regs;
981 unsigned i = CSI.size();
982 while (i != 0) {
983 unsigned LastReg = 0;
984 bool DeleteRet = false;
985 for (; i != 0; --i) {
986 unsigned Reg = CSI[i-1].getReg();
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000987 if (!(Func)(Reg, STI.splitFramePushPop())) continue;
Evan Cheng775ead32010-12-07 23:08:38 +0000988
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000989 // The aligned reloads from area DPRCS2 are not inserted here.
990 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
991 continue;
992
Tim Northoverd8407452013-10-01 14:33:28 +0000993 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000994 !isTrap && STI.hasV5TOps()) {
Quentin Colombet71a71482015-07-20 21:42:14 +0000995 if (MBB.succ_empty()) {
996 Reg = ARM::PC;
997 DeleteRet = true;
998 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
999 } else
1000 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Evan Cheng775ead32010-12-07 23:08:38 +00001001 // Fold the return instruction into the LDM.
Evan Cheng775ead32010-12-07 23:08:38 +00001002 }
1003
Evan Cheng9d54ae62010-12-08 06:29:02 +00001004 // If NoGap is true, pop consecutive registers and then leave the rest
1005 // for other instructions. e.g.
1006 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1007 if (NoGap && LastReg && LastReg != Reg-1)
1008 break;
1009
Evan Cheng775ead32010-12-07 23:08:38 +00001010 LastReg = Reg;
1011 Regs.push_back(Reg);
1012 }
1013
Jim Grosbach5fccad82010-12-09 18:31:13 +00001014 if (Regs.empty())
1015 continue;
1016 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng775ead32010-12-07 23:08:38 +00001017 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +00001018 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng775ead32010-12-07 23:08:38 +00001019 .addReg(ARM::SP));
1020 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1021 MIB.addReg(Regs[i], getDefRegState(true));
Quentin Colombet71a71482015-07-20 21:42:14 +00001022 if (DeleteRet && MI != MBB.end()) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001023 MIB.copyImplicitOps(*MI);
Evan Cheng775ead32010-12-07 23:08:38 +00001024 MI->eraseFromParent();
Andrew Trick6446bf72011-08-25 17:50:53 +00001025 }
Evan Cheng775ead32010-12-07 23:08:38 +00001026 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +00001027 } else if (Regs.size() == 1) {
1028 // If we adjusted the reg to PC from LR above, switch it back here. We
1029 // only do that for LDM.
1030 if (Regs[0] == ARM::PC)
1031 Regs[0] = ARM::LR;
1032 MachineInstrBuilder MIB =
1033 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1034 .addReg(ARM::SP, RegState::Define)
1035 .addReg(ARM::SP);
1036 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1037 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +00001038 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +00001039 MIB.addReg(0);
1040 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1041 } else
1042 MIB.addImm(4);
1043 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +00001044 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001045 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001046
1047 // Put any subsequent vpop instructions after this one: they will refer to
1048 // higher register numbers so need to be popped afterwards.
Quentin Colombet71a71482015-07-20 21:42:14 +00001049 if (MI != MBB.end())
1050 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +00001051 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001052}
1053
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001054/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +00001055/// starting from d8. Also insert stack realignment code and leave the stack
1056/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001057static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1058 MachineBasicBlock::iterator MI,
1059 unsigned NumAlignedDPRCS2Regs,
1060 const std::vector<CalleeSavedInfo> &CSI,
1061 const TargetRegisterInfo *TRI) {
1062 MachineFunction &MF = *MBB.getParent();
1063 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001064 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001065 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001066 MachineFrameInfo &MFI = *MF.getFrameInfo();
1067
1068 // Mark the D-register spill slots as properly aligned. Since MFI computes
1069 // stack slot layout backwards, this can actually mean that the d-reg stack
1070 // slot offsets can be wrong. The offset for d8 will always be correct.
1071 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1072 unsigned DNum = CSI[i].getReg() - ARM::D8;
Tim Northovere0ccdc62015-10-28 22:46:43 +00001073 if (DNum > NumAlignedDPRCS2Regs - 1)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001074 continue;
1075 int FI = CSI[i].getFrameIdx();
1076 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1077 // registers will be 8-byte aligned.
1078 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1079
1080 // The stack slot for D8 needs to be maximally aligned because this is
1081 // actually the point where we align the stack pointer. MachineFrameInfo
1082 // computes all offsets relative to the incoming stack pointer which is a
1083 // bit weird when realigning the stack. Any extra padding for this
1084 // over-alignment is not realized because the code inserted below adjusts
1085 // the stack pointer by numregs * 8 before aligning the stack pointer.
1086 if (DNum == 0)
1087 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1088 }
1089
1090 // Move the stack pointer to the d8 spill slot, and align it at the same
1091 // time. Leave the stack slot address in the scratch register r4.
1092 //
1093 // sub r4, sp, #numregs * 8
1094 // bic r4, r4, #align - 1
1095 // mov sp, r4
1096 //
1097 bool isThumb = AFI->isThumbFunction();
1098 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1099 AFI->setShouldRestoreSPFromFP(true);
1100
1101 // sub r4, sp, #numregs * 8
1102 // The immediate is <= 64, so it doesn't need any special encoding.
1103 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
1104 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
Kristof Beyls933de7a2015-01-08 15:09:14 +00001105 .addReg(ARM::SP)
1106 .addImm(8 * NumAlignedDPRCS2Regs)));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001107
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001108 unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +00001109 // We must set parameter MustBeSingleInstruction to true, since
1110 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1111 // stack alignment. Luckily, this can always be done since all ARM
1112 // architecture versions that support Neon also support the BFC
1113 // instruction.
1114 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001115
1116 // mov sp, r4
1117 // The stack pointer must be adjusted before spilling anything, otherwise
1118 // the stack slots could be clobbered by an interrupt handler.
1119 // Leave r4 live, it is used below.
1120 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1121 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
1122 .addReg(ARM::R4);
1123 MIB = AddDefaultPred(MIB);
1124 if (!isThumb)
1125 AddDefaultCC(MIB);
1126
1127 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1128 // r4 holds the stack slot address.
1129 unsigned NextReg = ARM::D8;
1130
1131 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1132 // The writeback is only needed when emitting two vst1.64 instructions.
1133 if (NumAlignedDPRCS2Regs >= 6) {
1134 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001135 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001136 MBB.addLiveIn(SupReg);
1137 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
1138 ARM::R4)
1139 .addReg(ARM::R4, RegState::Kill).addImm(16)
1140 .addReg(NextReg)
1141 .addReg(SupReg, RegState::ImplicitKill));
1142 NextReg += 4;
1143 NumAlignedDPRCS2Regs -= 4;
1144 }
1145
1146 // We won't modify r4 beyond this point. It currently points to the next
1147 // register to be spilled.
1148 unsigned R4BaseReg = NextReg;
1149
1150 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1151 if (NumAlignedDPRCS2Regs >= 4) {
1152 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001153 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001154 MBB.addLiveIn(SupReg);
1155 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1156 .addReg(ARM::R4).addImm(16).addReg(NextReg)
1157 .addReg(SupReg, RegState::ImplicitKill));
1158 NextReg += 4;
1159 NumAlignedDPRCS2Regs -= 4;
1160 }
1161
1162 // 16-byte aligned vst1.64 with 2 d-regs.
1163 if (NumAlignedDPRCS2Regs >= 2) {
1164 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001165 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001166 MBB.addLiveIn(SupReg);
1167 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001168 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001169 NextReg += 2;
1170 NumAlignedDPRCS2Regs -= 2;
1171 }
1172
1173 // Finally, use a vanilla vstr.64 for the odd last register.
1174 if (NumAlignedDPRCS2Regs) {
1175 MBB.addLiveIn(NextReg);
1176 // vstr.64 uses addrmode5 which has an offset scale of 4.
1177 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1178 .addReg(NextReg)
1179 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
1180 }
1181
1182 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001183 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001184}
1185
1186/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1187/// iterator to the following instruction.
1188static MachineBasicBlock::iterator
1189skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1190 unsigned NumAlignedDPRCS2Regs) {
1191 // sub r4, sp, #numregs * 8
1192 // bic r4, r4, #align - 1
1193 // mov sp, r4
1194 ++MI; ++MI; ++MI;
1195 assert(MI->mayStore() && "Expecting spill instruction");
1196
1197 // These switches all fall through.
1198 switch(NumAlignedDPRCS2Regs) {
1199 case 7:
1200 ++MI;
1201 assert(MI->mayStore() && "Expecting spill instruction");
1202 default:
1203 ++MI;
1204 assert(MI->mayStore() && "Expecting spill instruction");
1205 case 1:
1206 case 2:
1207 case 4:
1208 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1209 ++MI;
1210 }
1211 return MI;
1212}
1213
1214/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1215/// starting from d8. These instructions are assumed to execute while the
1216/// stack is still aligned, unlike the code inserted by emitPopInst.
1217static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1218 MachineBasicBlock::iterator MI,
1219 unsigned NumAlignedDPRCS2Regs,
1220 const std::vector<CalleeSavedInfo> &CSI,
1221 const TargetRegisterInfo *TRI) {
1222 MachineFunction &MF = *MBB.getParent();
1223 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001224 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001225 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001226
1227 // Find the frame index assigned to d8.
1228 int D8SpillFI = 0;
1229 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1230 if (CSI[i].getReg() == ARM::D8) {
1231 D8SpillFI = CSI[i].getFrameIdx();
1232 break;
1233 }
1234
1235 // Materialize the address of the d8 spill slot into the scratch register r4.
1236 // This can be fairly complicated if the stack frame is large, so just use
1237 // the normal frame index elimination mechanism to do it. This code runs as
1238 // the initial part of the epilog where the stack and base pointers haven't
1239 // been changed yet.
1240 bool isThumb = AFI->isThumbFunction();
1241 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1242
1243 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1244 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1245 .addFrameIndex(D8SpillFI).addImm(0)));
1246
1247 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1248 unsigned NextReg = ARM::D8;
1249
1250 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1251 if (NumAlignedDPRCS2Regs >= 6) {
1252 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001253 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001254 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1255 .addReg(ARM::R4, RegState::Define)
1256 .addReg(ARM::R4, RegState::Kill).addImm(16)
1257 .addReg(SupReg, RegState::ImplicitDefine));
1258 NextReg += 4;
1259 NumAlignedDPRCS2Regs -= 4;
1260 }
1261
1262 // We won't modify r4 beyond this point. It currently points to the next
1263 // register to be spilled.
1264 unsigned R4BaseReg = NextReg;
1265
1266 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1267 if (NumAlignedDPRCS2Regs >= 4) {
1268 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001269 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001270 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1271 .addReg(ARM::R4).addImm(16)
1272 .addReg(SupReg, RegState::ImplicitDefine));
1273 NextReg += 4;
1274 NumAlignedDPRCS2Regs -= 4;
1275 }
1276
1277 // 16-byte aligned vld1.64 with 2 d-regs.
1278 if (NumAlignedDPRCS2Regs >= 2) {
1279 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001280 &ARM::QPRRegClass);
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001281 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1282 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001283 NextReg += 2;
1284 NumAlignedDPRCS2Regs -= 2;
1285 }
1286
1287 // Finally, use a vanilla vldr.64 for the remaining odd register.
1288 if (NumAlignedDPRCS2Regs)
1289 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1290 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
1291
1292 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001293 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001294}
1295
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001296bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001297 MachineBasicBlock::iterator MI,
1298 const std::vector<CalleeSavedInfo> &CSI,
1299 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001300 if (CSI.empty())
1301 return false;
1302
1303 MachineFunction &MF = *MBB.getParent();
1304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001305
1306 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001307 unsigned PushOneOpc = AFI->isThumbFunction() ?
1308 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001309 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001310 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1311 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001312 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001313 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001314 MachineInstr::FrameSetup);
1315 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001316 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1317
1318 // The code above does not insert spill code for the aligned DPRCS2 registers.
1319 // The stack realignment code will be inserted between the push instructions
1320 // and these spills.
1321 if (NumAlignedDPRCS2Regs)
1322 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001323
1324 return true;
1325}
1326
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001327bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001328 MachineBasicBlock::iterator MI,
1329 const std::vector<CalleeSavedInfo> &CSI,
1330 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001331 if (CSI.empty())
1332 return false;
1333
1334 MachineFunction &MF = *MBB.getParent();
1335 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001336 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001337 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1338
1339 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1340 // registers. Do that here instead.
1341 if (NumAlignedDPRCS2Regs)
1342 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001343
1344 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001345 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001346 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001347 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1348 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001349 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001350 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001351 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001352 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001353
1354 return true;
1355}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001356
1357// FIXME: Make generic?
1358static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1359 const ARMBaseInstrInfo &TII) {
1360 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001361 for (auto &MBB : MF) {
1362 for (auto &MI : MBB)
1363 FnSize += TII.GetInstSizeInBytes(&MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001364 }
1365 return FnSize;
1366}
1367
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001368/// estimateRSStackSizeLimit - Look at each instruction that references stack
1369/// frames and return the stack size limit beyond which some of these
1370/// instructions will require a scratch register during their expansion later.
1371// FIXME: Move to TII?
1372static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001373 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001374 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1375 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001376 for (auto &MBB : MF) {
1377 for (auto &MI : MBB) {
1378 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1379 if (!MI.getOperand(i).isFI())
1380 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001381
1382 // When using ADDri to get the address of a stack object, 255 is the
1383 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001384 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001385 Limit = std::min(Limit, (1U << 8) - 1);
1386 break;
1387 }
1388
1389 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001390 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001391 case ARMII::AddrMode3:
1392 case ARMII::AddrModeT2_i8:
1393 Limit = std::min(Limit, (1U << 8) - 1);
1394 break;
1395 case ARMII::AddrMode5:
1396 case ARMII::AddrModeT2_i8s4:
1397 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1398 break;
1399 case ARMII::AddrModeT2_i12:
1400 // i12 supports only positive offset so these will be converted to
1401 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1402 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1403 Limit = std::min(Limit, (1U << 8) - 1);
1404 break;
1405 case ARMII::AddrMode4:
1406 case ARMII::AddrMode6:
1407 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1408 // immediate offset for stack references.
1409 return 0;
1410 default:
1411 break;
1412 }
1413 break; // At most one FI per instruction
1414 }
1415 }
1416 }
1417
1418 return Limit;
1419}
1420
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001421// In functions that realign the stack, it can be an advantage to spill the
1422// callee-saved vector registers after realigning the stack. The vst1 and vld1
1423// instructions take alignment hints that can improve performance.
1424//
Matthias Braun02564862015-07-14 17:17:13 +00001425static void
1426checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001427 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1428 if (!SpillAlignedNEONRegs)
1429 return;
1430
1431 // Naked functions don't spill callee-saved registers.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001432 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001433 return;
1434
1435 // We are planning to use NEON instructions vst1 / vld1.
Eric Christopher1b21f002015-01-29 00:19:33 +00001436 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001437 return;
1438
1439 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopher1b21f002015-01-29 00:19:33 +00001440 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001441 return;
1442
1443 // Aligned spills require stack realignment.
Eric Christopher1b21f002015-01-29 00:19:33 +00001444 if (!static_cast<const ARMBaseRegisterInfo *>(
1445 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001446 return;
1447
1448 // We always spill contiguous d-registers starting from d8. Count how many
1449 // needs spilling. The register allocator will almost always use the
1450 // callee-saved registers in order, but it can happen that there are holes in
1451 // the range. Registers above the hole will be spilled to the standard DPRCS
1452 // area.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001453 unsigned NumSpills = 0;
1454 for (; NumSpills < 8; ++NumSpills)
Matthias Braun02564862015-07-14 17:17:13 +00001455 if (!SavedRegs.test(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001456 break;
1457
1458 // Don't do this for just one d-register. It's not worth it.
1459 if (NumSpills < 2)
1460 return;
1461
1462 // Spill the first NumSpills D-registers after realigning the stack.
1463 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1464
1465 // A scratch register is required for the vst1 / vld1 instructions.
Matthias Braun02564862015-07-14 17:17:13 +00001466 SavedRegs.set(ARM::R4);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001467}
1468
Matthias Braun02564862015-07-14 17:17:13 +00001469void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1470 BitVector &SavedRegs,
1471 RegScavenger *RS) const {
1472 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001473 // This tells PEI to spill the FP as if it is any other callee-save register
1474 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1475 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1476 // to combine multiple loads / stores.
1477 bool CanEliminateFrame = true;
1478 bool CS1Spilled = false;
1479 bool LRSpilled = false;
1480 unsigned NumGPRSpills = 0;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001481 unsigned NumFPRSpills = 0;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001482 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1483 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001484 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001485 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001486 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001487 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001488 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1489 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001490 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001491 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1492
1493 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1494 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001495 // since it's not always possible to restore sp from fp in a single
1496 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001497 // FIXME: It will be better just to find spare register here.
1498 if (AFI->isThumb2Function() &&
1499 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Matthias Braun02564862015-07-14 17:17:13 +00001500 SavedRegs.set(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001501
Evan Cheng572756a2011-01-16 05:14:33 +00001502 if (AFI->isThumb1OnlyFunction()) {
1503 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001504 if (AFI->getArgRegsSaveSize() > 0)
Matthias Braun02564862015-07-14 17:17:13 +00001505 SavedRegs.set(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001506
Jim Grosbachdca85312011-06-13 21:18:25 +00001507 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1508 // for sure what the stack size will be, but for this, an estimate is good
1509 // enough. If there anything changes it, it'll be a spill, which implies
1510 // we've used all the registers and so R4 is already used, so not marking
Chad Rosieradd38c12011-10-20 00:07:12 +00001511 // it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001512 // FIXME: It will be better just to find spare register here.
Hal Finkel628ba122013-03-14 21:15:20 +00001513 unsigned StackSize = MFI->estimateStackSize(MF);
Chad Rosieradd38c12011-10-20 00:07:12 +00001514 if (MFI->hasVarSizedObjects() || StackSize > 508)
Matthias Braun02564862015-07-14 17:17:13 +00001515 SavedRegs.set(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001516 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001517
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001518 // See if we can spill vector registers to aligned stack.
Matthias Braun02564862015-07-14 17:17:13 +00001519 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001520
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001521 // Spill the BasePtr if it's used.
1522 if (RegInfo->hasBasePointer(MF))
Matthias Braun02564862015-07-14 17:17:13 +00001523 SavedRegs.set(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001524
1525 // Don't spill FP if the frame can be eliminated. This is determined
Matthias Braun02564862015-07-14 17:17:13 +00001526 // by scanning the callee-save registers to see if any is modified.
Craig Topper840beec2014-04-04 05:16:06 +00001527 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001528 for (unsigned i = 0; CSRegs[i]; ++i) {
1529 unsigned Reg = CSRegs[i];
1530 bool Spilled = false;
Matthias Braun02564862015-07-14 17:17:13 +00001531 if (SavedRegs.test(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001532 Spilled = true;
1533 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001534 }
1535
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001536 if (!ARM::GPRRegClass.contains(Reg)) {
1537 if (Spilled) {
1538 if (ARM::SPRRegClass.contains(Reg))
1539 NumFPRSpills++;
1540 else if (ARM::DPRRegClass.contains(Reg))
1541 NumFPRSpills += 2;
1542 else if (ARM::QPRRegClass.contains(Reg))
1543 NumFPRSpills += 4;
1544 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001545 continue;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001546 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001547
1548 if (Spilled) {
1549 NumGPRSpills++;
1550
Tim Northoverf8b0a7a2016-05-13 19:16:14 +00001551 if (!STI.splitFramePushPop()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001552 if (Reg == ARM::LR)
1553 LRSpilled = true;
1554 CS1Spilled = true;
1555 continue;
1556 }
1557
1558 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1559 switch (Reg) {
1560 case ARM::LR:
1561 LRSpilled = true;
1562 // Fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +00001563 case ARM::R0: case ARM::R1:
1564 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001565 case ARM::R4: case ARM::R5:
1566 case ARM::R6: case ARM::R7:
1567 CS1Spilled = true;
1568 break;
1569 default:
1570 break;
1571 }
1572 } else {
Tim Northoverf8b0a7a2016-05-13 19:16:14 +00001573 if (!STI.splitFramePushPop()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001574 UnspilledCS1GPRs.push_back(Reg);
1575 continue;
1576 }
1577
1578 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001579 case ARM::R0: case ARM::R1:
1580 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001581 case ARM::R4: case ARM::R5:
1582 case ARM::R6: case ARM::R7:
1583 case ARM::LR:
1584 UnspilledCS1GPRs.push_back(Reg);
1585 break;
1586 default:
1587 UnspilledCS2GPRs.push_back(Reg);
1588 break;
1589 }
1590 }
1591 }
1592
1593 bool ForceLRSpill = false;
1594 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1595 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1596 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1597 // use of BL to implement far jump. If it turns out that it's not needed
1598 // then the branch fix up path will undo it.
1599 if (FnSize >= (1 << 11)) {
1600 CanEliminateFrame = false;
1601 ForceLRSpill = true;
1602 }
1603 }
1604
1605 // If any of the stack slot references may be out of range of an immediate
1606 // offset, make sure a register (or a spill slot) is available for the
1607 // register scavenger. Note that if we're indexing off the frame pointer, the
1608 // effective stack size is 4 bytes larger since the FP points to the stack
1609 // slot of the previous FP. Also, if we have variable sized objects in the
1610 // function, stack slot references will often be negative, and some of
1611 // our instructions are positive-offset only, so conservatively consider
1612 // that case to want a spill slot (or register) as well. Similarly, if
1613 // the function adjusts the stack pointer during execution and the
1614 // adjustments aren't already part of our stack size estimate, our offset
1615 // calculations may be off, so be conservative.
1616 // FIXME: We could add logic to be more precise about negative offsets
1617 // and which instructions will need a scratch register for them. Is it
1618 // worth the effort and added fragility?
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001619 unsigned EstimatedStackSize =
1620 MFI->estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
1621 if (hasFP(MF)) {
1622 if (AFI->hasStackFrame())
1623 EstimatedStackSize += 4;
1624 } else {
1625 // If FP is not used, SP will be used to access arguments, so count the
1626 // size of arguments into the estimation.
1627 EstimatedStackSize += MF.getInfo<ARMFunctionInfo>()->getArgumentStackSize();
1628 }
1629 EstimatedStackSize += 16; // For possible paddings.
1630
1631 bool BigStack = EstimatedStackSize >= estimateRSStackSizeLimit(MF, this) ||
Vitaly Buka6b5c8922016-05-04 19:44:11 +00001632 MFI->hasVarSizedObjects() ||
1633 (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001634 bool ExtraCSSpill = false;
1635 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1636 AFI->setHasStackFrame(true);
1637
1638 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1639 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1640 if (!LRSpilled && CS1Spilled) {
Matthias Braun02564862015-07-14 17:17:13 +00001641 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001642 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001643 SmallVectorImpl<unsigned>::iterator LRPos;
1644 LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1645 (unsigned)ARM::LR);
1646 if (LRPos != UnspilledCS1GPRs.end())
1647 UnspilledCS1GPRs.erase(LRPos);
1648
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001649 ForceLRSpill = false;
1650 ExtraCSSpill = true;
1651 }
1652
1653 if (hasFP(MF)) {
Matthias Braun02564862015-07-14 17:17:13 +00001654 SavedRegs.set(FramePtr);
Joerg Sonnenberger818e7252014-05-06 20:43:01 +00001655 auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1656 FramePtr);
1657 if (FPPos != UnspilledCS1GPRs.end())
1658 UnspilledCS1GPRs.erase(FPPos);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001659 NumGPRSpills++;
1660 }
1661
1662 // If stack and double are 8-byte aligned and we are spilling an odd number
1663 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1664 // the integer and double callee save areas.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001665 unsigned TargetAlign = getStackAlignment();
Tim Northoverdc0d9e42014-11-05 00:27:20 +00001666 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001667 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1668 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1669 unsigned Reg = UnspilledCS1GPRs[i];
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001670 // Don't spill high register if the function is thumb. In the case of
1671 // Windows on ARM, accept R11 (frame pointer)
Peter Collingbourne78f1ecc2015-04-23 20:31:26 +00001672 if (!AFI->isThumbFunction() ||
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001673 (STI.isTargetWindows() && Reg == ARM::R11) ||
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001674 isARMLowRegister(Reg) || Reg == ARM::LR) {
Matthias Braun02564862015-07-14 17:17:13 +00001675 SavedRegs.set(Reg);
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001676 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001677 ExtraCSSpill = true;
1678 break;
1679 }
1680 }
1681 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1682 unsigned Reg = UnspilledCS2GPRs.front();
Matthias Braun02564862015-07-14 17:17:13 +00001683 SavedRegs.set(Reg);
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001684 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001685 ExtraCSSpill = true;
1686 }
1687 }
1688
1689 // Estimate if we might need to scavenge a register at some point in order
1690 // to materialize a stack offset. If so, either spill one additional
1691 // callee-saved register or reserve a special spill slot to facilitate
1692 // register scavenging. Thumb1 needs a spill slot for stack pointer
1693 // adjustments also, even when the frame itself is small.
1694 if (BigStack && !ExtraCSSpill) {
1695 // If any non-reserved CS register isn't spilled, just spill one or two
1696 // extra. That should take care of it!
1697 unsigned NumExtras = TargetAlign / 4;
1698 SmallVector<unsigned, 2> Extras;
1699 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1700 unsigned Reg = UnspilledCS1GPRs.back();
1701 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001702 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001703 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1704 Reg == ARM::LR)) {
1705 Extras.push_back(Reg);
1706 NumExtras--;
1707 }
1708 }
1709 // For non-Thumb1 functions, also check for hi-reg CS registers
1710 if (!AFI->isThumb1OnlyFunction()) {
1711 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1712 unsigned Reg = UnspilledCS2GPRs.back();
1713 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001714 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001715 Extras.push_back(Reg);
1716 NumExtras--;
1717 }
1718 }
1719 }
1720 if (Extras.size() && NumExtras == 0) {
1721 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Matthias Braun02564862015-07-14 17:17:13 +00001722 SavedRegs.set(Extras[i]);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001723 }
1724 } else if (!AFI->isThumb1OnlyFunction()) {
1725 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1726 // closest to SP or frame pointer.
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001727 assert(RS && "Register scavenging not provided");
Craig Topperc7242e02012-04-20 07:30:17 +00001728 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Hal Finkel9e331c22013-03-22 23:32:27 +00001729 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001730 RC->getAlignment(),
1731 false));
1732 }
1733 }
1734 }
1735
1736 if (ForceLRSpill) {
Matthias Braun02564862015-07-14 17:17:13 +00001737 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001738 AFI->setLRIsSpilledForFarJump(true);
1739 }
1740}
Eli Bendersky8da87162013-02-21 20:05:00 +00001741
Hans Wennborge1a2e902016-03-31 18:33:38 +00001742MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
1743 MachineFunction &MF, MachineBasicBlock &MBB,
1744 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001745 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001746 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001747 if (!hasReservedCallFrame(MF)) {
1748 // If we have alloca, convert as follows:
1749 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1750 // ADJCALLSTACKUP -> add, sp, sp, amount
1751 MachineInstr *Old = I;
1752 DebugLoc dl = Old->getDebugLoc();
1753 unsigned Amount = Old->getOperand(0).getImm();
1754 if (Amount != 0) {
1755 // We need to keep the stack aligned properly. To do this, we round the
1756 // amount of space needed for the outgoing arguments up to the next
1757 // alignment boundary.
Guozhi Weif66d3842015-08-17 22:36:27 +00001758 Amount = alignSPAdjust(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +00001759
1760 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1761 assert(!AFI->isThumb1OnlyFunction() &&
1762 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1763 bool isARM = !AFI->isThumbFunction();
1764
1765 // Replace the pseudo instruction with a new instruction...
1766 unsigned Opc = Old->getOpcode();
1767 int PIdx = Old->findFirstPredOperandIdx();
1768 ARMCC::CondCodes Pred = (PIdx == -1)
1769 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1770 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1771 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1772 unsigned PredReg = Old->getOperand(2).getReg();
1773 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1774 Pred, PredReg);
1775 } else {
1776 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1777 unsigned PredReg = Old->getOperand(3).getReg();
1778 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1779 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1780 Pred, PredReg);
1781 }
1782 }
1783 }
Hans Wennborge1a2e902016-03-31 18:33:38 +00001784 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00001785}
1786
Oliver Stannardb14c6252014-04-02 16:10:33 +00001787/// Get the minimum constant for ARM that is greater than or equal to the
1788/// argument. In ARM, constants can have any value that can be produced by
1789/// rotating an 8-bit value to the right by an even number of bits within a
1790/// 32-bit word.
1791static uint32_t alignToARMConstant(uint32_t Value) {
1792 unsigned Shifted = 0;
1793
1794 if (Value == 0)
1795 return 0;
1796
1797 while (!(Value & 0xC0000000)) {
1798 Value = Value << 2;
1799 Shifted += 2;
1800 }
1801
1802 bool Carry = (Value & 0x00FFFFFF);
1803 Value = ((Value & 0xFF000000) >> 24) + Carry;
1804
1805 if (Value & 0x0000100)
1806 Value = Value & 0x000001FC;
1807
1808 if (Shifted > 24)
1809 Value = Value >> (Shifted - 24);
1810 else
1811 Value = Value << (24 - Shifted);
1812
1813 return Value;
1814}
1815
1816// The stack limit in the TCB is set to this many bytes above the actual
1817// stack limit.
1818static const uint64_t kSplitStackAvailable = 256;
1819
1820// Adjust the function prologue to enable split stacks. This currently only
1821// supports android and linux.
1822//
1823// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
1824// must be well defined in order to allow for consistent implementations of the
1825// __morestack helper function. The ABI is also not a normal ABI in that it
1826// doesn't follow the normal calling conventions because this allows the
1827// prologue of each function to be optimized further.
1828//
1829// Currently, the ABI looks like (when calling __morestack)
1830//
1831// * r4 holds the minimum stack size requested for this function call
1832// * r5 holds the stack size of the arguments to the function
1833// * the beginning of the function is 3 instructions after the call to
1834// __morestack
1835//
1836// Implementations of __morestack should use r4 to allocate a new stack, r5 to
1837// place the arguments on to the new stack, and the 3-instruction knowledge to
1838// jump directly to the body of the function when working on the new stack.
1839//
1840// An old (and possibly no longer compatible) implementation of __morestack for
1841// ARM can be found at [1].
1842//
1843// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
Quentin Colombet61b305e2015-05-05 17:38:16 +00001844void ARMFrameLowering::adjustForSegmentedStacks(
1845 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
Oliver Stannardb14c6252014-04-02 16:10:33 +00001846 unsigned Opcode;
1847 unsigned CFIIndex;
Eric Christopher22b2ad22015-02-20 08:24:37 +00001848 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
Oliver Stannardb14c6252014-04-02 16:10:33 +00001849 bool Thumb = ST->isThumb();
1850
1851 // Sadly, this currently doesn't support varargs, platforms other than
1852 // android/linux. Note that thumb1/thumb2 are support for android/linux.
1853 if (MF.getFunction()->isVarArg())
1854 report_fatal_error("Segmented stacks do not support vararg functions.");
1855 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00001856 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00001857
Oliver Stannardb14c6252014-04-02 16:10:33 +00001858 MachineFrameInfo *MFI = MF.getFrameInfo();
1859 MachineModuleInfo &MMI = MF.getMMI();
1860 MCContext &Context = MMI.getContext();
1861 const MCRegisterInfo *MRI = Context.getRegisterInfo();
1862 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001863 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00001864 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
1865 DebugLoc DL;
1866
Tim Northoverf9e798b2014-05-22 13:03:43 +00001867 uint64_t StackSize = MFI->getStackSize();
1868
1869 // Do not generate a prologue for functions with a stack of size zero
1870 if (StackSize == 0)
1871 return;
1872
Oliver Stannardb14c6252014-04-02 16:10:33 +00001873 // Use R4 and R5 as scratch registers.
1874 // We save R4 and R5 before use and restore them before leaving the function.
1875 unsigned ScratchReg0 = ARM::R4;
1876 unsigned ScratchReg1 = ARM::R5;
1877 uint64_t AlignedStackSize;
1878
1879 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
1880 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
1881 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
1882 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
1883 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
1884
Quentin Colombet71a71482015-07-20 21:42:14 +00001885 // Grab everything that reaches PrologueMBB to update there liveness as well.
1886 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
1887 SmallVector<MachineBasicBlock *, 2> WalkList;
1888 WalkList.push_back(&PrologueMBB);
1889
1890 do {
1891 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
1892 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
1893 if (BeforePrologueRegion.insert(PredBB).second)
1894 WalkList.push_back(PredBB);
1895 }
1896 } while (!WalkList.empty());
1897
1898 // The order in that list is important.
1899 // The blocks will all be inserted before PrologueMBB using that order.
1900 // Therefore the block that should appear first in the CFG should appear
1901 // first in the list.
1902 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
1903 PostStackMBB};
Quentin Colombet71a71482015-07-20 21:42:14 +00001904
Craig Topper80720812015-12-01 06:13:01 +00001905 for (MachineBasicBlock *B : AddedBlocks)
1906 BeforePrologueRegion.insert(B);
Quentin Colombet71a71482015-07-20 21:42:14 +00001907
Matthias Braund9da1622015-09-09 18:08:03 +00001908 for (const auto &LI : PrologueMBB.liveins()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00001909 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
Matthias Braunb2b7ef12015-08-24 22:59:52 +00001910 PredBB->addLiveIn(LI);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001911 }
1912
Quentin Colombet71a71482015-07-20 21:42:14 +00001913 // Remove the newly added blocks from the list, since we know
1914 // we do not have to do the following updates for them.
Craig Topper80720812015-12-01 06:13:01 +00001915 for (MachineBasicBlock *B : AddedBlocks) {
1916 BeforePrologueRegion.erase(B);
1917 MF.insert(PrologueMBB.getIterator(), B);
Quentin Colombet71a71482015-07-20 21:42:14 +00001918 }
1919
1920 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
1921 // Make sure the LiveIns are still sorted and unique.
1922 MBB->sortUniqueLiveIns();
1923 // Replace the edges to PrologueMBB by edges to the sequences
1924 // we are about to add.
1925 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
1926 }
Oliver Stannardb14c6252014-04-02 16:10:33 +00001927
1928 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00001929 AlignedStackSize = alignToARMConstant(StackSize);
1930
1931 // When the frame size is less than 256 we just compare the stack
1932 // boundary directly to the value of the stack pointer, per gcc.
1933 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
1934
1935 // We will use two of the callee save registers as scratch registers so we
1936 // need to save those registers onto the stack.
1937 // We will use SR0 to hold stack limit and SR1 to hold the stack size
1938 // requested and arguments for __morestack().
1939 // SR0: Scratch Register #0
1940 // SR1: Scratch Register #1
1941 // push {SR0, SR1}
1942 if (Thumb) {
1943 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH)))
1944 .addReg(ScratchReg0).addReg(ScratchReg1);
1945 } else {
1946 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
1947 .addReg(ARM::SP, RegState::Define).addReg(ARM::SP))
1948 .addReg(ScratchReg0).addReg(ScratchReg1);
1949 }
1950
1951 // Emit the relevant DWARF information about the change in stack pointer as
1952 // well as where to find both r4 and r5 (the callee-save registers)
1953 CFIIndex =
1954 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
1955 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1956 .addCFIIndex(CFIIndex);
1957 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1958 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
1959 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1960 .addCFIIndex(CFIIndex);
1961 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1962 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
1963 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1964 .addCFIIndex(CFIIndex);
1965
1966 // mov SR1, sp
1967 if (Thumb) {
1968 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
1969 .addReg(ARM::SP));
1970 } else if (CompareStackPointer) {
1971 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
1972 .addReg(ARM::SP)).addReg(0);
1973 }
1974
1975 // sub SR1, sp, #StackSize
1976 if (!CompareStackPointer && Thumb) {
1977 AddDefaultPred(
1978 AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1))
1979 .addReg(ScratchReg1).addImm(AlignedStackSize));
1980 } else if (!CompareStackPointer) {
1981 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
1982 .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0);
1983 }
1984
1985 if (Thumb && ST->isThumb1Only()) {
1986 unsigned PCLabelId = ARMFI->createPICLabelUId();
1987 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Oliver Stannard92e0fc02014-04-03 08:45:16 +00001988 MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001989 MachineConstantPool *MCP = MF.getConstantPool();
Tim Northover956b0082015-10-02 18:07:13 +00001990 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001991
1992 // ldr SR0, [pc, offset(STACK_LIMIT)]
1993 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
1994 .addConstantPoolIndex(CPI));
1995
1996 // ldr SR0, [SR0]
1997 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
1998 .addReg(ScratchReg0).addImm(0));
1999 } else {
2000 // Get TLS base address from the coprocessor
2001 // mrc p15, #0, SR0, c13, c0, #3
2002 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2003 .addImm(15)
2004 .addImm(0)
2005 .addImm(13)
2006 .addImm(0)
2007 .addImm(3));
2008
2009 // Use the last tls slot on android and a private field of the TCP on linux.
2010 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2011 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2012
2013 // Get the stack limit from the right offset
2014 // ldr SR0, [sr0, #4 * TlsOffset]
2015 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2016 .addReg(ScratchReg0).addImm(4 * TlsOffset));
2017 }
2018
2019 // Compare stack limit with stack size requested.
2020 // cmp SR0, SR1
2021 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
2022 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode))
2023 .addReg(ScratchReg0)
2024 .addReg(ScratchReg1));
2025
2026 // This jump is taken if StackLimit < SP - stack required.
2027 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2028 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2029 .addImm(ARMCC::LO)
2030 .addReg(ARM::CPSR);
2031
2032
2033 // Calling __morestack(StackSize, Size of stack arguments).
2034 // __morestack knows that the stack size requested is in SR0(r4)
2035 // and amount size of stack arguments is in SR1(r5).
2036
2037 // Pass first argument for the __morestack by Scratch Register #0.
2038 // The amount size of stack required
2039 if (Thumb) {
2040 AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8),
2041 ScratchReg0)).addImm(AlignedStackSize));
2042 } else {
2043 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2044 .addImm(AlignedStackSize)).addReg(0);
2045 }
2046 // Pass second argument for the __morestack by Scratch Register #1.
2047 // The amount size of stack consumed to save function arguments.
2048 if (Thumb) {
2049 AddDefaultPred(
2050 AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1))
2051 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())));
2052 } else {
2053 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2054 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())))
2055 .addReg(0);
2056 }
2057
2058 // push {lr} - Save return address of this function.
2059 if (Thumb) {
2060 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH)))
2061 .addReg(ARM::LR);
2062 } else {
2063 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2064 .addReg(ARM::SP, RegState::Define)
2065 .addReg(ARM::SP))
2066 .addReg(ARM::LR);
2067 }
2068
2069 // Emit the DWARF info about the change in stack as well as where to find the
2070 // previous link register
2071 CFIIndex =
2072 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
2073 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2074 .addCFIIndex(CFIIndex);
2075 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
2076 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2077 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2078 .addCFIIndex(CFIIndex);
2079
2080 // Call __morestack().
2081 if (Thumb) {
2082 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL)))
2083 .addExternalSymbol("__morestack");
2084 } else {
2085 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2086 .addExternalSymbol("__morestack");
2087 }
2088
2089 // pop {lr} - Restore return address of this original function.
2090 if (Thumb) {
2091 if (ST->isThumb1Only()) {
2092 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2093 .addReg(ScratchReg0);
2094 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2095 .addReg(ScratchReg0));
2096 } else {
2097 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2098 .addReg(ARM::LR, RegState::Define)
2099 .addReg(ARM::SP, RegState::Define)
2100 .addReg(ARM::SP)
2101 .addImm(4));
2102 }
2103 } else {
2104 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2105 .addReg(ARM::SP, RegState::Define)
2106 .addReg(ARM::SP))
2107 .addReg(ARM::LR);
2108 }
2109
2110 // Restore SR0 and SR1 in case of __morestack() was called.
2111 // __morestack() will skip PostStackMBB block so we need to restore
2112 // scratch registers from here.
2113 // pop {SR0, SR1}
2114 if (Thumb) {
2115 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2116 .addReg(ScratchReg0)
2117 .addReg(ScratchReg1);
2118 } else {
2119 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2120 .addReg(ARM::SP, RegState::Define)
2121 .addReg(ARM::SP))
2122 .addReg(ScratchReg0)
2123 .addReg(ScratchReg1);
2124 }
2125
2126 // Update the CFA offset now that we've popped
2127 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2128 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2129 .addCFIIndex(CFIIndex);
2130
2131 // bx lr - Return from this function.
2132 Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
2133 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode)));
2134
2135 // Restore SR0 and SR1 in case of __morestack() was not called.
2136 // pop {SR0, SR1}
2137 if (Thumb) {
2138 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP)))
2139 .addReg(ScratchReg0)
2140 .addReg(ScratchReg1);
2141 } else {
2142 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2143 .addReg(ARM::SP, RegState::Define)
2144 .addReg(ARM::SP))
2145 .addReg(ScratchReg0)
2146 .addReg(ScratchReg1);
2147 }
2148
2149 // Update the CFA offset now that we've popped
2150 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2151 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2152 .addCFIIndex(CFIIndex);
2153
2154 // Tell debuggers that r4 and r5 are now the same as they were in the
2155 // previous function, that they're the "Same Value".
2156 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2157 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2158 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2159 .addCFIIndex(CFIIndex);
2160 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2161 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2162 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2163 .addCFIIndex(CFIIndex);
2164
2165 // Organizing MBB lists
Quentin Colombet61b305e2015-05-05 17:38:16 +00002166 PostStackMBB->addSuccessor(&PrologueMBB);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002167
2168 AllocMBB->addSuccessor(PostStackMBB);
2169
2170 GetMBB->addSuccessor(PostStackMBB);
2171 GetMBB->addSuccessor(AllocMBB);
2172
2173 McrMBB->addSuccessor(GetMBB);
2174
2175 PrevStackMBB->addSuccessor(McrMBB);
2176
Filipe Cabecinhas0da99372016-04-29 15:22:48 +00002177#ifdef EXPENSIVE_CHECKS
Oliver Stannardb14c6252014-04-02 16:10:33 +00002178 MF.verify();
2179#endif
2180}