blob: e2fe8b968a7f3d82fa47361724869d455fc10cd1 [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
Tim Northoverc9432eb2013-11-04 23:04:15 +0000123static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB,
124 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
125 const ARMBaseInstrInfo &TII, unsigned DestReg,
126 unsigned SrcReg, int NumBytes,
127 unsigned MIFlags = MachineInstr::NoFlags,
128 ARMCC::CondCodes Pred = ARMCC::AL,
129 unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000130 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000131 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000132 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000133 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000134 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000135 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000136}
137
Tim Northoverc9432eb2013-11-04 23:04:15 +0000138static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
139 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
140 const ARMBaseInstrInfo &TII, int NumBytes,
141 unsigned MIFlags = MachineInstr::NoFlags,
142 ARMCC::CondCodes Pred = ARMCC::AL,
143 unsigned PredReg = 0) {
144 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
145 MIFlags, Pred, PredReg);
146}
147
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000148static int sizeOfSPAdjustment(const MachineInstr *MI) {
Tim Northover603d3162014-11-14 22:45:33 +0000149 int RegSize;
150 switch (MI->getOpcode()) {
151 case ARM::VSTMDDB_UPD:
152 RegSize = 8;
153 break;
154 case ARM::STMDB_UPD:
155 case ARM::t2STMDB_UPD:
156 RegSize = 4;
157 break;
158 case ARM::t2STR_PRE:
159 case ARM::STR_PRE_IMM:
160 return 4;
161 default:
162 llvm_unreachable("Unknown push or pop like instruction");
163 }
164
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000165 int count = 0;
166 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
167 // pred) so the list starts at 4.
168 for (int i = MI->getNumOperands() - 1; i >= 4; --i)
Tim Northover603d3162014-11-14 22:45:33 +0000169 count += RegSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000170 return count;
171}
172
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000173static bool WindowsRequiresStackProbe(const MachineFunction &MF,
174 size_t StackSizeInBytes) {
175 const MachineFrameInfo *MFI = MF.getFrameInfo();
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000176 const Function *F = MF.getFunction();
177 unsigned StackProbeSize = (MFI->getStackProtectorIndex() > 0) ? 4080 : 4096;
178 if (F->hasFnAttribute("stack-probe-size"))
179 F->getFnAttribute("stack-probe-size")
180 .getValueAsString()
181 .getAsInteger(0, StackProbeSize);
182 return StackSizeInBytes >= StackProbeSize;
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000183}
184
Tim Northover603d3162014-11-14 22:45:33 +0000185namespace {
186struct StackAdjustingInsts {
187 struct InstInfo {
188 MachineBasicBlock::iterator I;
189 unsigned SPAdjust;
190 bool BeforeFPSet;
191 };
192
193 SmallVector<InstInfo, 4> Insts;
194
195 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
196 bool BeforeFPSet = false) {
197 InstInfo Info = {I, SPAdjust, BeforeFPSet};
198 Insts.push_back(Info);
199 }
200
201 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
202 auto Info = std::find_if(Insts.begin(), Insts.end(),
203 [&](InstInfo &Info) { return Info.I == I; });
204 assert(Info != Insts.end() && "invalid sp adjusting instruction");
205 Info->SPAdjust += ExtraBytes;
206 }
207
208 void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
209 DebugLoc dl, const ARMBaseInstrInfo &TII, bool HasFP) {
210 unsigned CFAOffset = 0;
211 for (auto &Info : Insts) {
212 if (HasFP && !Info.BeforeFPSet)
213 return;
214
215 CFAOffset -= Info.SPAdjust;
216 unsigned CFIIndex = MMI.addFrameInst(
217 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
218 BuildMI(MBB, std::next(Info.I), dl,
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000219 TII.get(TargetOpcode::CFI_INSTRUCTION))
220 .addCFIIndex(CFIIndex)
221 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000222 }
223 }
224};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000225}
Tim Northover603d3162014-11-14 22:45:33 +0000226
Kristof Beyls933de7a2015-01-08 15:09:14 +0000227/// Emit an instruction sequence that will align the address in
228/// register Reg by zero-ing out the lower bits. For versions of the
229/// architecture that support Neon, this must be done in a single
230/// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
231/// single instruction. That function only gets called when optimizing
232/// spilling of D registers on a core with the Neon instruction set
233/// present.
234static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
235 const TargetInstrInfo &TII,
236 MachineBasicBlock &MBB,
237 MachineBasicBlock::iterator MBBI,
238 DebugLoc DL, const unsigned Reg,
239 const unsigned Alignment,
240 const bool MustBeSingleInstruction) {
Eric Christopher1b21f002015-01-29 00:19:33 +0000241 const ARMSubtarget &AST =
242 static_cast<const ARMSubtarget &>(MF.getSubtarget());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000243 const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
244 const unsigned AlignMask = Alignment - 1;
245 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
246 assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
247 if (!AFI->isThumbFunction()) {
248 // if the BFC instruction is available, use that to zero the lower
249 // bits:
250 // bfc Reg, #0, log2(Alignment)
251 // otherwise use BIC, if the mask to zero the required number of bits
252 // can be encoded in the bic immediate field
253 // bic Reg, Reg, Alignment-1
254 // otherwise, emit
255 // lsr Reg, Reg, log2(Alignment)
256 // lsl Reg, Reg, log2(Alignment)
257 if (CanUseBFC) {
258 AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
259 .addReg(Reg, RegState::Kill)
260 .addImm(~AlignMask));
261 } else if (AlignMask <= 255) {
262 AddDefaultCC(
263 AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
264 .addReg(Reg, RegState::Kill)
265 .addImm(AlignMask)));
266 } else {
267 assert(!MustBeSingleInstruction &&
268 "Shouldn't call emitAligningInstructions demanding a single "
269 "instruction to be emitted for large stack alignment for a target "
270 "without BFC.");
271 AddDefaultCC(AddDefaultPred(
272 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
273 .addReg(Reg, RegState::Kill)
274 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))));
275 AddDefaultCC(AddDefaultPred(
276 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
277 .addReg(Reg, RegState::Kill)
278 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))));
279 }
280 } else {
281 // Since this is only reached for Thumb-2 targets, the BFC instruction
282 // should always be available.
283 assert(CanUseBFC);
284 AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
285 .addReg(Reg, RegState::Kill)
286 .addImm(~AlignMask));
287 }
288}
289
Quentin Colombet61b305e2015-05-05 17:38:16 +0000290void ARMFrameLowering::emitPrologue(MachineFunction &MF,
291 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000292 MachineBasicBlock::iterator MBBI = MBB.begin();
293 MachineFrameInfo *MFI = MF.getFrameInfo();
294 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000295 MachineModuleInfo &MMI = MF.getMMI();
296 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000297 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000298 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopher1b21f002015-01-29 00:19:33 +0000299 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
300 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000301 assert(!AFI->isThumb1OnlyFunction() &&
302 "This emitPrologue does not support Thumb1!");
303 bool isARM = !AFI->isThumbFunction();
Eric Christopher1b21f002015-01-29 00:19:33 +0000304 unsigned Align = STI.getFrameLowering()->getStackAlignment();
Tim Northover775aaeb2015-11-05 21:54:58 +0000305 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
306 unsigned NumBytes = MFI->getStackSize();
307 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
308
309 // Debug location must be unknown since the first debug location is used
310 // to determine the end of the prologue.
311 DebugLoc dl;
312
313 unsigned FramePtr = RegInfo->getFrameRegister(MF);
314
315 // Determine the sizes of each callee-save spill areas and record which frame
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000316 // belongs to which callee-save spill areas.
317 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
318 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000319 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000320
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000321 // All calls are tail calls in GHC calling conv, and functions have no
322 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000323 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
324 return;
325
Tim Northover603d3162014-11-14 22:45:33 +0000326 StackAdjustingInsts DefCFAOffsetCandidates;
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000327 bool HasFP = hasFP(MF);
Tim Northover603d3162014-11-14 22:45:33 +0000328
Oliver Stannardd55e1152014-03-05 15:25:27 +0000329 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000330 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000331 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000332 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000333 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000334 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000335
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000336 if (!AFI->hasStackFrame() &&
337 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000338 if (NumBytes - ArgRegsSaveSize != 0) {
339 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000340 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000341 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
342 NumBytes - ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000343 }
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000344 DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000345 return;
346 }
347
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000348 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000349 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
350 unsigned Reg = CSI[i].getReg();
351 int FI = CSI[i].getFrameIdx();
352 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000353 case ARM::R8:
354 case ARM::R9:
355 case ARM::R10:
356 case ARM::R11:
357 case ARM::R12:
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000358 if (STI.splitFramePushPop()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000359 GPRCS2Size += 4;
360 break;
361 }
362 // fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +0000363 case ARM::R0:
364 case ARM::R1:
365 case ARM::R2:
366 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000367 case ARM::R4:
368 case ARM::R5:
369 case ARM::R6:
370 case ARM::R7:
371 case ARM::LR:
372 if (Reg == FramePtr)
373 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000374 GPRCS1Size += 4;
375 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000376 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000377 // This is a DPR. Exclude the aligned DPRCS2 spills.
378 if (Reg == ARM::D8)
379 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000380 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000381 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000382 }
383 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000384
Eric Christopherb006fc92010-11-18 19:40:05 +0000385 // Move past area 1.
Tim Northover603d3162014-11-14 22:45:33 +0000386 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
387 if (GPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000388 GPRCS1Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000389 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
390 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000391
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000392 // Determine starting offsets of spill areas.
Tim Northover228c9432014-11-05 00:27:13 +0000393 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
394 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
395 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
396 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
397 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000398 int FramePtrOffsetInPush = 0;
399 if (HasFP) {
Tim Northover603d3162014-11-14 22:45:33 +0000400 FramePtrOffsetInPush =
401 MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000402 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
403 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000404 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000405 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
406 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
407 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
408
Tim Northoverc9432eb2013-11-04 23:04:15 +0000409 // Move past area 2.
Tim Northover603d3162014-11-14 22:45:33 +0000410 if (GPRCS2Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000411 GPRCS2Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000412 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
413 }
Tim Northoverc9432eb2013-11-04 23:04:15 +0000414
Tim Northover228c9432014-11-05 00:27:13 +0000415 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
416 // .cfi_offset operations will reflect that.
417 if (DPRGapSize) {
418 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
Tim Northover603d3162014-11-14 22:45:33 +0000419 if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize))
420 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
421 else {
Tim Northover228c9432014-11-05 00:27:13 +0000422 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
423 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000424 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
425 }
Tim Northover228c9432014-11-05 00:27:13 +0000426 }
427
Eric Christopherb006fc92010-11-18 19:40:05 +0000428 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000429 if (DPRCSSize > 0) {
Evan Cheng70d29632011-02-25 00:24:46 +0000430 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000431 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000432 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
433 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI));
Tim Northover93bcc662013-11-08 17:18:07 +0000434 LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000435 }
Evan Cheng70d29632011-02-25 00:24:46 +0000436 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000437
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000438 // Move past the aligned DPRCS2 area.
439 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
440 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
441 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
442 // leaves the stack pointer pointing to the DPRCS2 area.
443 //
444 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
445 NumBytes += MFI->getObjectOffset(D8SpillFI);
446 } else
447 NumBytes = DPRCSOffset;
448
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000449 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
450 uint32_t NumWords = NumBytes >> 2;
451
452 if (NumWords < 65536)
453 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000454 .addImm(NumWords)
455 .setMIFlags(MachineInstr::FrameSetup));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000456 else
457 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000458 .addImm(NumWords)
459 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000460
461 switch (TM.getCodeModel()) {
462 case CodeModel::Small:
463 case CodeModel::Medium:
464 case CodeModel::Default:
465 case CodeModel::Kernel:
466 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
467 .addImm((unsigned)ARMCC::AL).addReg(0)
468 .addExternalSymbol("__chkstk")
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000469 .addReg(ARM::R4, RegState::Implicit)
470 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000471 break;
472 case CodeModel::Large:
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000473 case CodeModel::JITDefault:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000474 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000475 .addExternalSymbol("__chkstk")
476 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000477
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000478 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
479 .addImm((unsigned)ARMCC::AL).addReg(0)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000480 .addReg(ARM::R12, RegState::Kill)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000481 .addReg(ARM::R4, RegState::Implicit)
482 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000483 break;
484 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000485
486 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr),
487 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +0000488 .addReg(ARM::SP, RegState::Kill)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000489 .addReg(ARM::R4, RegState::Kill)
490 .setMIFlags(MachineInstr::FrameSetup)));
491 NumBytes = 0;
492 }
493
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000494 if (NumBytes) {
495 // Adjust SP after all the callee-save spills.
Tim Northoverbeb5bcc2015-09-23 22:21:09 +0000496 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
497 tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes))
Tim Northover603d3162014-11-14 22:45:33 +0000498 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
499 else {
Tim Northover93bcc662013-11-08 17:18:07 +0000500 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
501 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000502 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
503 }
Tim Northover93bcc662013-11-08 17:18:07 +0000504
Evan Chengeb56dca2010-11-22 18:12:04 +0000505 if (HasFP && isARM)
506 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
507 // Note it's not safe to do this in Thumb2 mode because it would have
508 // taken two instructions:
509 // mov sp, r7
510 // sub sp, #24
511 // If an interrupt is taken between the two instructions, then sp is in
512 // an inconsistent state (pointing to the middle of callee-saved area).
513 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000514 AFI->setShouldRestoreSPFromFP(true);
515 }
516
Tim Northover603d3162014-11-14 22:45:33 +0000517 // Set FP to point to the stack slot that contains the previous FP.
518 // For iOS, FP is R7, which has now been stored in spill area 1.
519 // Otherwise, if this is not iOS, all the callee-saved registers go
520 // into spill area 1, including the FP in R11. In either case, it
521 // is in area one and the adjustment needs to take place just after
522 // that push.
523 if (HasFP) {
524 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
525 unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push);
526 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
527 dl, TII, FramePtr, ARM::SP,
528 PushSize + FramePtrOffsetInPush,
529 MachineInstr::FrameSetup);
530 if (FramePtrOffsetInPush + PushSize != 0) {
531 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
532 nullptr, MRI->getDwarfRegNum(FramePtr, true),
533 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
534 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000535 .addCFIIndex(CFIIndex)
536 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000537 } else {
538 unsigned CFIIndex =
539 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
540 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
541 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000542 .addCFIIndex(CFIIndex)
543 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000544 }
545 }
546
547 // Now that the prologue's actual instructions are finalised, we can insert
548 // the necessary DWARF cf instructions to describe the situation. Start by
549 // recording where each register ended up:
550 if (GPRCS1Size > 0) {
551 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
552 int CFIIndex;
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000553 for (const auto &Entry : CSI) {
554 unsigned Reg = Entry.getReg();
555 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000556 switch (Reg) {
557 case ARM::R8:
558 case ARM::R9:
559 case ARM::R10:
560 case ARM::R11:
561 case ARM::R12:
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000562 if (STI.splitFramePushPop())
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000563 break;
564 // fallthrough
565 case ARM::R0:
566 case ARM::R1:
567 case ARM::R2:
568 case ARM::R3:
569 case ARM::R4:
570 case ARM::R5:
571 case ARM::R6:
572 case ARM::R7:
573 case ARM::LR:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000574 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
575 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
576 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000577 .addCFIIndex(CFIIndex)
578 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000579 break;
580 }
581 }
582 }
583
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000584 if (GPRCS2Size > 0) {
Tim Northover603d3162014-11-14 22:45:33 +0000585 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000586 for (const auto &Entry : CSI) {
587 unsigned Reg = Entry.getReg();
588 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000589 switch (Reg) {
590 case ARM::R8:
591 case ARM::R9:
592 case ARM::R10:
593 case ARM::R11:
594 case ARM::R12:
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000595 if (STI.splitFramePushPop()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000596 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000597 unsigned Offset = MFI->getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000598 unsigned CFIIndex = MMI.addFrameInst(
599 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
600 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000601 .addCFIIndex(CFIIndex)
602 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000603 }
604 break;
605 }
606 }
607 }
608
609 if (DPRCSSize > 0) {
610 // Since vpush register list cannot have gaps, there may be multiple vpush
611 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000612 MachineBasicBlock::iterator Pos = std::next(LastPush);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000613 for (const auto &Entry : CSI) {
614 unsigned Reg = Entry.getReg();
615 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000616 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
617 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
618 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
619 unsigned Offset = MFI->getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000620 unsigned CFIIndex = MMI.addFrameInst(
621 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover603d3162014-11-14 22:45:33 +0000622 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000623 .addCFIIndex(CFIIndex)
624 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000625 }
626 }
627 }
628
Tim Northover603d3162014-11-14 22:45:33 +0000629 // Now we can emit descriptions of where the canonical frame address was
630 // throughout the process. If we have a frame pointer, it takes over the job
631 // half-way through, so only the first few .cfi_def_cfa_offset instructions
632 // actually get emitted.
633 DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
Tim Northover93bcc662013-11-08 17:18:07 +0000634
Evan Chengeb56dca2010-11-22 18:12:04 +0000635 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000636 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
637 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000638
639 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
640 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000641 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000642 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
643
644 // If we need dynamic stack realignment, do it here. Be paranoid and make
645 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000646 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000647 // realigned.
648 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000649 unsigned MaxAlign = MFI->getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +0000650 assert(!AFI->isThumb1OnlyFunction());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000651 if (!AFI->isThumbFunction()) {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000652 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
653 false);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000654 } else {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000655 // We cannot use sp as source/dest register here, thus we're using r4 to
656 // perform the calculations. We're emitting the following sequence:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000657 // mov r4, sp
Kristof Beyls933de7a2015-01-08 15:09:14 +0000658 // -- use emitAligningInstructions to produce best sequence to zero
659 // -- out lower bits in r4
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000660 // mov sp, r4
661 // FIXME: It will be better just to find spare register here.
Jim Grosbache9cc9012011-06-30 23:38:17 +0000662 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Kristof Beyls933de7a2015-01-08 15:09:14 +0000663 .addReg(ARM::SP, RegState::Kill));
664 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
665 false);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000666 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Kristof Beyls933de7a2015-01-08 15:09:14 +0000667 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000668 }
669
670 AFI->setShouldRestoreSPFromFP(true);
671 }
672
673 // If we need a base pointer, set it up here. It's whatever the value
674 // of the stack pointer is at this point. Any variable size objects
675 // will be allocated after this, so we can still use the base pointer
676 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000677 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000678 if (RegInfo->hasBasePointer(MF)) {
679 if (isARM)
680 BuildMI(MBB, MBBI, dl,
681 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
682 .addReg(ARM::SP)
683 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
684 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000685 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000686 RegInfo->getBaseRegister())
687 .addReg(ARM::SP));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000688 }
689
690 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000691 // the sp from fp. We can assume there's an FP here since hasFP already
692 // checks for hasVarSizedObjects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000693 if (MFI->hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000694 AFI->setShouldRestoreSPFromFP(true);
695}
696
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000697void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000698 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000699 MachineFrameInfo *MFI = MF.getFrameInfo();
700 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000701 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000702 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000703 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000704 assert(!AFI->isThumb1OnlyFunction() &&
705 "This emitEpilogue does not support Thumb1!");
706 bool isARM = !AFI->isThumbFunction();
707
Tim Northover8cda34f2015-03-11 18:54:22 +0000708 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000709 int NumBytes = (int)MFI->getStackSize();
710 unsigned FramePtr = RegInfo->getFrameRegister(MF);
711
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000712 // All calls are tail calls in GHC calling conv, and functions have no
713 // prologue/epilogue.
Quentin Colombet71a71482015-07-20 21:42:14 +0000714 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000715 return;
Quentin Colombet71a71482015-07-20 21:42:14 +0000716
717 // First put ourselves on the first (from top) terminator instructions.
718 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
719 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Eric Christopherb3322362012-08-03 00:05:53 +0000720
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000721 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000722 if (NumBytes - ArgRegsSaveSize != 0)
723 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000724 } else {
725 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000726 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000727 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000728 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000729 --MBBI;
Tim Northover93bcc662013-11-08 17:18:07 +0000730 } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000731 if (!isCSRestore(MBBI, TII, CSRegs))
732 ++MBBI;
733 }
734
735 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000736 NumBytes -= (ArgRegsSaveSize +
737 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000738 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000739 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000740 AFI->getDPRCalleeSavedAreaSize());
741
742 // Reset SP based on frame pointer only if the stack frame extends beyond
743 // frame pointer stack slot or target is ELF and the function has FP.
744 if (AFI->shouldRestoreSPFromFP()) {
745 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
746 if (NumBytes) {
747 if (isARM)
748 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
749 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000750 else {
751 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000752 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000753 // mov sp, r7
754 // sub sp, #24
755 // This is bad, if an interrupt is taken after the mov, sp is in an
756 // inconsistent state.
757 // Use the first callee-saved register as a scratch register.
Matthias Braun02564862015-07-14 17:17:13 +0000758 assert(!MFI->getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000759 "No scratch register to restore SP from FP!");
760 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000761 ARMCC::AL, 0, TII);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000762 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000763 ARM::SP)
764 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000765 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000766 } else {
767 // Thumb2 or ARM.
768 if (isARM)
769 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
770 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
771 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000772 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000773 ARM::SP)
774 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000775 }
Tim Northoverdee86042013-12-02 14:46:26 +0000776 } else if (NumBytes &&
Tim Northovere4def5e2013-12-05 11:02:02 +0000777 !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000778 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000779
Eric Christopherb006fc92010-11-18 19:40:05 +0000780 // Increment past our save areas.
Evan Cheng70d29632011-02-25 00:24:46 +0000781 if (AFI->getDPRCalleeSavedAreaSize()) {
782 MBBI++;
783 // Since vpop register list cannot have gaps, there may be multiple vpop
784 // instructions in the epilogue.
785 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
786 MBBI++;
787 }
Tim Northover228c9432014-11-05 00:27:13 +0000788 if (AFI->getDPRCalleeSavedGapSize()) {
789 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
790 "unexpected DPR alignment gap");
791 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
792 }
793
Eric Christopherb006fc92010-11-18 19:40:05 +0000794 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
795 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000796 }
797
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000798 if (ArgRegsSaveSize)
799 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000800}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000801
Bob Wilson657f2272011-01-13 21:10:12 +0000802/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
803/// debug info. It's the same as what we use for resolving the code-gen
804/// references for now. FIXME: This can go wrong when references are
805/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000806int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000807ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000808 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000809 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
810}
811
812int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000813ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000814 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000815 int SPAdj) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000816 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000817 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000818 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000819 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
820 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
821 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
822 bool isFixed = MFI->isFixedObjectIndex(FI);
823
824 FrameReg = ARM::SP;
825 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000826
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000827 // SP can move around if there are allocas. We may also lose track of SP
828 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000829 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000830
Anton Korobeynikov46877782010-11-20 15:59:32 +0000831 // When dynamically realigning the stack, use the frame pointer for
832 // parameters, and the stack/base pointer for locals.
833 if (RegInfo->needsStackRealignment(MF)) {
834 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
835 if (isFixed) {
836 FrameReg = RegInfo->getFrameRegister(MF);
837 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000838 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000839 assert(RegInfo->hasBasePointer(MF) &&
840 "VLAs and dynamic stack alignment, but missing base pointer!");
841 FrameReg = RegInfo->getBaseRegister();
842 }
843 return Offset;
844 }
845
846 // If there is a frame pointer, use it when we can.
847 if (hasFP(MF) && AFI->hasStackFrame()) {
848 // Use frame pointer to reference fixed objects. Use it for locals if
849 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000850 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000851 FrameReg = RegInfo->getFrameRegister(MF);
852 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000853 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000854 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000855 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000856 // Try to use the frame pointer if we can, else use the base pointer
857 // since it's available. This is handy for the emergency spill slot, in
858 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000859 if (FPOffset >= -255 && FPOffset < 0) {
860 FrameReg = RegInfo->getFrameRegister(MF);
861 return FPOffset;
862 }
Evan Chengc0d20042011-04-22 01:42:52 +0000863 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000864 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000865 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000866 // ldr <rd>, [sp, #<imm8>]
867 // if at all possible to save space.
868 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
869 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000870 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000871 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000872 if (FPOffset >= -255 && FPOffset < 0) {
873 FrameReg = RegInfo->getFrameRegister(MF);
874 return FPOffset;
875 }
876 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
877 // Otherwise, use SP or FP, whichever is closer to the stack slot.
878 FrameReg = RegInfo->getFrameRegister(MF);
879 return FPOffset;
880 }
881 }
882 // Use the base pointer if we have one.
883 if (RegInfo->hasBasePointer(MF))
884 FrameReg = RegInfo->getBaseRegister();
885 return Offset;
886}
887
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000888void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000889 MachineBasicBlock::iterator MI,
890 const std::vector<CalleeSavedInfo> &CSI,
891 unsigned StmOpc, unsigned StrOpc,
892 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000893 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000894 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000895 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000896 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000897 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
898
899 DebugLoc DL;
900
901 SmallVector<std::pair<unsigned,bool>, 4> Regs;
902 unsigned i = CSI.size();
Evan Cheng775ead32010-12-07 23:08:38 +0000903 while (i != 0) {
904 unsigned LastReg = 0;
905 for (; i != 0; --i) {
906 unsigned Reg = CSI[i-1].getReg();
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000907 if (!(Func)(Reg, STI.splitFramePushPop())) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000908
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000909 // D-registers in the aligned area DPRCS2 are NOT spilled here.
910 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
911 continue;
912
Matthias Braun707e02c2016-04-13 21:43:25 +0000913 bool isLiveIn = MF.getRegInfo().isLiveIn(Reg);
914 if (!isLiveIn)
Evan Cheng775ead32010-12-07 23:08:38 +0000915 MBB.addLiveIn(Reg);
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000916 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000917 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000918 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000919 if (NoGap && LastReg && LastReg != Reg-1)
920 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000921 LastReg = Reg;
Matthias Braun707e02c2016-04-13 21:43:25 +0000922 // Do not set a kill flag on values that are also marked as live-in. This
923 // happens with the @llvm-returnaddress intrinsic and with arguments
924 // passed in callee saved registers.
925 // Omitting the kill flags is conservatively correct even if the live-in
926 // is not used after all.
927 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000928 }
929
Jim Grosbach5fccad82010-12-09 18:31:13 +0000930 if (Regs.empty())
931 continue;
932 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000933 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000934 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000935 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng775ead32010-12-07 23:08:38 +0000936 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
937 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +0000938 } else if (Regs.size() == 1) {
939 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
940 ARM::SP)
941 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000942 .addReg(ARM::SP).setMIFlags(MIFlags)
943 .addImm(-4);
Jim Grosbach5fccad82010-12-09 18:31:13 +0000944 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000945 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000946 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +0000947
948 // Put any subsequent vpush instructions before this one: they will refer to
949 // higher register numbers so need to be pushed first in order to preserve
950 // monotonicity.
Quentin Colombet71a71482015-07-20 21:42:14 +0000951 if (MI != MBB.begin())
952 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000953 }
Evan Cheng775ead32010-12-07 23:08:38 +0000954}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000955
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000956void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000957 MachineBasicBlock::iterator MI,
958 const std::vector<CalleeSavedInfo> &CSI,
959 unsigned LdmOpc, unsigned LdrOpc,
960 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000961 bool(*Func)(unsigned, bool),
962 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +0000963 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000964 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Evan Cheng775ead32010-12-07 23:08:38 +0000965 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet71a71482015-07-20 21:42:14 +0000966 DebugLoc DL;
967 bool isTailCall = false;
968 bool isInterrupt = false;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000969 bool isTrap = false;
Quentin Colombet71a71482015-07-20 21:42:14 +0000970 if (MBB.end() != MI) {
971 DL = MI->getDebugLoc();
972 unsigned RetOpcode = MI->getOpcode();
973 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
974 isInterrupt =
975 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000976 isTrap =
977 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
978 RetOpcode == ARM::tTRAP;
Quentin Colombet71a71482015-07-20 21:42:14 +0000979 }
Evan Cheng775ead32010-12-07 23:08:38 +0000980
981 SmallVector<unsigned, 4> Regs;
982 unsigned i = CSI.size();
983 while (i != 0) {
984 unsigned LastReg = 0;
985 bool DeleteRet = false;
986 for (; i != 0; --i) {
987 unsigned Reg = CSI[i-1].getReg();
Tim Northoverf8b0a7a2016-05-13 19:16:14 +0000988 if (!(Func)(Reg, STI.splitFramePushPop())) continue;
Evan Cheng775ead32010-12-07 23:08:38 +0000989
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000990 // The aligned reloads from area DPRCS2 are not inserted here.
991 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
992 continue;
993
Tim Northoverd8407452013-10-01 14:33:28 +0000994 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000995 !isTrap && STI.hasV5TOps()) {
Quentin Colombet71a71482015-07-20 21:42:14 +0000996 if (MBB.succ_empty()) {
997 Reg = ARM::PC;
998 DeleteRet = true;
999 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
1000 } else
1001 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Evan Cheng775ead32010-12-07 23:08:38 +00001002 // Fold the return instruction into the LDM.
Evan Cheng775ead32010-12-07 23:08:38 +00001003 }
1004
Evan Cheng9d54ae62010-12-08 06:29:02 +00001005 // If NoGap is true, pop consecutive registers and then leave the rest
1006 // for other instructions. e.g.
1007 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1008 if (NoGap && LastReg && LastReg != Reg-1)
1009 break;
1010
Evan Cheng775ead32010-12-07 23:08:38 +00001011 LastReg = Reg;
1012 Regs.push_back(Reg);
1013 }
1014
Jim Grosbach5fccad82010-12-09 18:31:13 +00001015 if (Regs.empty())
1016 continue;
1017 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng775ead32010-12-07 23:08:38 +00001018 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +00001019 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng775ead32010-12-07 23:08:38 +00001020 .addReg(ARM::SP));
1021 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1022 MIB.addReg(Regs[i], getDefRegState(true));
Quentin Colombet71a71482015-07-20 21:42:14 +00001023 if (DeleteRet && MI != MBB.end()) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001024 MIB.copyImplicitOps(*MI);
Evan Cheng775ead32010-12-07 23:08:38 +00001025 MI->eraseFromParent();
Andrew Trick6446bf72011-08-25 17:50:53 +00001026 }
Evan Cheng775ead32010-12-07 23:08:38 +00001027 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +00001028 } else if (Regs.size() == 1) {
1029 // If we adjusted the reg to PC from LR above, switch it back here. We
1030 // only do that for LDM.
1031 if (Regs[0] == ARM::PC)
1032 Regs[0] = ARM::LR;
1033 MachineInstrBuilder MIB =
1034 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1035 .addReg(ARM::SP, RegState::Define)
1036 .addReg(ARM::SP);
1037 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1038 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +00001039 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +00001040 MIB.addReg(0);
1041 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1042 } else
1043 MIB.addImm(4);
1044 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +00001045 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001046 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001047
1048 // Put any subsequent vpop instructions after this one: they will refer to
1049 // higher register numbers so need to be popped afterwards.
Quentin Colombet71a71482015-07-20 21:42:14 +00001050 if (MI != MBB.end())
1051 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +00001052 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001053}
1054
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001055/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +00001056/// starting from d8. Also insert stack realignment code and leave the stack
1057/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001058static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1059 MachineBasicBlock::iterator MI,
1060 unsigned NumAlignedDPRCS2Regs,
1061 const std::vector<CalleeSavedInfo> &CSI,
1062 const TargetRegisterInfo *TRI) {
1063 MachineFunction &MF = *MBB.getParent();
1064 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001065 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001066 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001067 MachineFrameInfo &MFI = *MF.getFrameInfo();
1068
1069 // Mark the D-register spill slots as properly aligned. Since MFI computes
1070 // stack slot layout backwards, this can actually mean that the d-reg stack
1071 // slot offsets can be wrong. The offset for d8 will always be correct.
1072 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1073 unsigned DNum = CSI[i].getReg() - ARM::D8;
Tim Northovere0ccdc62015-10-28 22:46:43 +00001074 if (DNum > NumAlignedDPRCS2Regs - 1)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001075 continue;
1076 int FI = CSI[i].getFrameIdx();
1077 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1078 // registers will be 8-byte aligned.
1079 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1080
1081 // The stack slot for D8 needs to be maximally aligned because this is
1082 // actually the point where we align the stack pointer. MachineFrameInfo
1083 // computes all offsets relative to the incoming stack pointer which is a
1084 // bit weird when realigning the stack. Any extra padding for this
1085 // over-alignment is not realized because the code inserted below adjusts
1086 // the stack pointer by numregs * 8 before aligning the stack pointer.
1087 if (DNum == 0)
1088 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1089 }
1090
1091 // Move the stack pointer to the d8 spill slot, and align it at the same
1092 // time. Leave the stack slot address in the scratch register r4.
1093 //
1094 // sub r4, sp, #numregs * 8
1095 // bic r4, r4, #align - 1
1096 // mov sp, r4
1097 //
1098 bool isThumb = AFI->isThumbFunction();
1099 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1100 AFI->setShouldRestoreSPFromFP(true);
1101
1102 // sub r4, sp, #numregs * 8
1103 // The immediate is <= 64, so it doesn't need any special encoding.
1104 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
1105 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
Kristof Beyls933de7a2015-01-08 15:09:14 +00001106 .addReg(ARM::SP)
1107 .addImm(8 * NumAlignedDPRCS2Regs)));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001108
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001109 unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +00001110 // We must set parameter MustBeSingleInstruction to true, since
1111 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1112 // stack alignment. Luckily, this can always be done since all ARM
1113 // architecture versions that support Neon also support the BFC
1114 // instruction.
1115 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001116
1117 // mov sp, r4
1118 // The stack pointer must be adjusted before spilling anything, otherwise
1119 // the stack slots could be clobbered by an interrupt handler.
1120 // Leave r4 live, it is used below.
1121 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1122 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
1123 .addReg(ARM::R4);
1124 MIB = AddDefaultPred(MIB);
1125 if (!isThumb)
1126 AddDefaultCC(MIB);
1127
1128 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1129 // r4 holds the stack slot address.
1130 unsigned NextReg = ARM::D8;
1131
1132 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1133 // The writeback is only needed when emitting two vst1.64 instructions.
1134 if (NumAlignedDPRCS2Regs >= 6) {
1135 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001136 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001137 MBB.addLiveIn(SupReg);
1138 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
1139 ARM::R4)
1140 .addReg(ARM::R4, RegState::Kill).addImm(16)
1141 .addReg(NextReg)
1142 .addReg(SupReg, RegState::ImplicitKill));
1143 NextReg += 4;
1144 NumAlignedDPRCS2Regs -= 4;
1145 }
1146
1147 // We won't modify r4 beyond this point. It currently points to the next
1148 // register to be spilled.
1149 unsigned R4BaseReg = NextReg;
1150
1151 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1152 if (NumAlignedDPRCS2Regs >= 4) {
1153 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001154 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001155 MBB.addLiveIn(SupReg);
1156 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1157 .addReg(ARM::R4).addImm(16).addReg(NextReg)
1158 .addReg(SupReg, RegState::ImplicitKill));
1159 NextReg += 4;
1160 NumAlignedDPRCS2Regs -= 4;
1161 }
1162
1163 // 16-byte aligned vst1.64 with 2 d-regs.
1164 if (NumAlignedDPRCS2Regs >= 2) {
1165 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001166 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001167 MBB.addLiveIn(SupReg);
1168 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001169 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001170 NextReg += 2;
1171 NumAlignedDPRCS2Regs -= 2;
1172 }
1173
1174 // Finally, use a vanilla vstr.64 for the odd last register.
1175 if (NumAlignedDPRCS2Regs) {
1176 MBB.addLiveIn(NextReg);
1177 // vstr.64 uses addrmode5 which has an offset scale of 4.
1178 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1179 .addReg(NextReg)
1180 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
1181 }
1182
1183 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001184 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001185}
1186
1187/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1188/// iterator to the following instruction.
1189static MachineBasicBlock::iterator
1190skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1191 unsigned NumAlignedDPRCS2Regs) {
1192 // sub r4, sp, #numregs * 8
1193 // bic r4, r4, #align - 1
1194 // mov sp, r4
1195 ++MI; ++MI; ++MI;
1196 assert(MI->mayStore() && "Expecting spill instruction");
1197
1198 // These switches all fall through.
1199 switch(NumAlignedDPRCS2Regs) {
1200 case 7:
1201 ++MI;
1202 assert(MI->mayStore() && "Expecting spill instruction");
1203 default:
1204 ++MI;
1205 assert(MI->mayStore() && "Expecting spill instruction");
1206 case 1:
1207 case 2:
1208 case 4:
1209 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1210 ++MI;
1211 }
1212 return MI;
1213}
1214
1215/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1216/// starting from d8. These instructions are assumed to execute while the
1217/// stack is still aligned, unlike the code inserted by emitPopInst.
1218static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1219 MachineBasicBlock::iterator MI,
1220 unsigned NumAlignedDPRCS2Regs,
1221 const std::vector<CalleeSavedInfo> &CSI,
1222 const TargetRegisterInfo *TRI) {
1223 MachineFunction &MF = *MBB.getParent();
1224 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001225 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001226 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001227
1228 // Find the frame index assigned to d8.
1229 int D8SpillFI = 0;
1230 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1231 if (CSI[i].getReg() == ARM::D8) {
1232 D8SpillFI = CSI[i].getFrameIdx();
1233 break;
1234 }
1235
1236 // Materialize the address of the d8 spill slot into the scratch register r4.
1237 // This can be fairly complicated if the stack frame is large, so just use
1238 // the normal frame index elimination mechanism to do it. This code runs as
1239 // the initial part of the epilog where the stack and base pointers haven't
1240 // been changed yet.
1241 bool isThumb = AFI->isThumbFunction();
1242 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1243
1244 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1245 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1246 .addFrameIndex(D8SpillFI).addImm(0)));
1247
1248 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1249 unsigned NextReg = ARM::D8;
1250
1251 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1252 if (NumAlignedDPRCS2Regs >= 6) {
1253 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001254 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001255 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1256 .addReg(ARM::R4, RegState::Define)
1257 .addReg(ARM::R4, RegState::Kill).addImm(16)
1258 .addReg(SupReg, RegState::ImplicitDefine));
1259 NextReg += 4;
1260 NumAlignedDPRCS2Regs -= 4;
1261 }
1262
1263 // We won't modify r4 beyond this point. It currently points to the next
1264 // register to be spilled.
1265 unsigned R4BaseReg = NextReg;
1266
1267 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1268 if (NumAlignedDPRCS2Regs >= 4) {
1269 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001270 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001271 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1272 .addReg(ARM::R4).addImm(16)
1273 .addReg(SupReg, RegState::ImplicitDefine));
1274 NextReg += 4;
1275 NumAlignedDPRCS2Regs -= 4;
1276 }
1277
1278 // 16-byte aligned vld1.64 with 2 d-regs.
1279 if (NumAlignedDPRCS2Regs >= 2) {
1280 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001281 &ARM::QPRRegClass);
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001282 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1283 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001284 NextReg += 2;
1285 NumAlignedDPRCS2Regs -= 2;
1286 }
1287
1288 // Finally, use a vanilla vldr.64 for the remaining odd register.
1289 if (NumAlignedDPRCS2Regs)
1290 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1291 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
1292
1293 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001294 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001295}
1296
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001297bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001298 MachineBasicBlock::iterator MI,
1299 const std::vector<CalleeSavedInfo> &CSI,
1300 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001301 if (CSI.empty())
1302 return false;
1303
1304 MachineFunction &MF = *MBB.getParent();
1305 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001306
1307 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001308 unsigned PushOneOpc = AFI->isThumbFunction() ?
1309 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001310 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001311 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1312 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001313 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001314 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001315 MachineInstr::FrameSetup);
1316 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001317 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1318
1319 // The code above does not insert spill code for the aligned DPRCS2 registers.
1320 // The stack realignment code will be inserted between the push instructions
1321 // and these spills.
1322 if (NumAlignedDPRCS2Regs)
1323 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001324
1325 return true;
1326}
1327
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001328bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001329 MachineBasicBlock::iterator MI,
1330 const std::vector<CalleeSavedInfo> &CSI,
1331 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001332 if (CSI.empty())
1333 return false;
1334
1335 MachineFunction &MF = *MBB.getParent();
1336 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001337 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001338 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1339
1340 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1341 // registers. Do that here instead.
1342 if (NumAlignedDPRCS2Regs)
1343 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001344
1345 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001346 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001347 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001348 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1349 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001350 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001351 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001352 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001353 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001354
1355 return true;
1356}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001357
1358// FIXME: Make generic?
1359static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1360 const ARMBaseInstrInfo &TII) {
1361 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001362 for (auto &MBB : MF) {
1363 for (auto &MI : MBB)
1364 FnSize += TII.GetInstSizeInBytes(&MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001365 }
1366 return FnSize;
1367}
1368
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001369/// estimateRSStackSizeLimit - Look at each instruction that references stack
1370/// frames and return the stack size limit beyond which some of these
1371/// instructions will require a scratch register during their expansion later.
1372// FIXME: Move to TII?
1373static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001374 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001375 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1376 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001377 for (auto &MBB : MF) {
1378 for (auto &MI : MBB) {
1379 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1380 if (!MI.getOperand(i).isFI())
1381 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001382
1383 // When using ADDri to get the address of a stack object, 255 is the
1384 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001385 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001386 Limit = std::min(Limit, (1U << 8) - 1);
1387 break;
1388 }
1389
1390 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001391 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001392 case ARMII::AddrMode3:
1393 case ARMII::AddrModeT2_i8:
1394 Limit = std::min(Limit, (1U << 8) - 1);
1395 break;
1396 case ARMII::AddrMode5:
1397 case ARMII::AddrModeT2_i8s4:
1398 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1399 break;
1400 case ARMII::AddrModeT2_i12:
1401 // i12 supports only positive offset so these will be converted to
1402 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1403 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1404 Limit = std::min(Limit, (1U << 8) - 1);
1405 break;
1406 case ARMII::AddrMode4:
1407 case ARMII::AddrMode6:
1408 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1409 // immediate offset for stack references.
1410 return 0;
1411 default:
1412 break;
1413 }
1414 break; // At most one FI per instruction
1415 }
1416 }
1417 }
1418
1419 return Limit;
1420}
1421
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001422// In functions that realign the stack, it can be an advantage to spill the
1423// callee-saved vector registers after realigning the stack. The vst1 and vld1
1424// instructions take alignment hints that can improve performance.
1425//
Matthias Braun02564862015-07-14 17:17:13 +00001426static void
1427checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001428 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1429 if (!SpillAlignedNEONRegs)
1430 return;
1431
1432 // Naked functions don't spill callee-saved registers.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001433 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001434 return;
1435
1436 // We are planning to use NEON instructions vst1 / vld1.
Eric Christopher1b21f002015-01-29 00:19:33 +00001437 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001438 return;
1439
1440 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopher1b21f002015-01-29 00:19:33 +00001441 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001442 return;
1443
1444 // Aligned spills require stack realignment.
Eric Christopher1b21f002015-01-29 00:19:33 +00001445 if (!static_cast<const ARMBaseRegisterInfo *>(
1446 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001447 return;
1448
1449 // We always spill contiguous d-registers starting from d8. Count how many
1450 // needs spilling. The register allocator will almost always use the
1451 // callee-saved registers in order, but it can happen that there are holes in
1452 // the range. Registers above the hole will be spilled to the standard DPRCS
1453 // area.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001454 unsigned NumSpills = 0;
1455 for (; NumSpills < 8; ++NumSpills)
Matthias Braun02564862015-07-14 17:17:13 +00001456 if (!SavedRegs.test(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001457 break;
1458
1459 // Don't do this for just one d-register. It's not worth it.
1460 if (NumSpills < 2)
1461 return;
1462
1463 // Spill the first NumSpills D-registers after realigning the stack.
1464 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1465
1466 // A scratch register is required for the vst1 / vld1 instructions.
Matthias Braun02564862015-07-14 17:17:13 +00001467 SavedRegs.set(ARM::R4);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001468}
1469
Matthias Braun02564862015-07-14 17:17:13 +00001470void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1471 BitVector &SavedRegs,
1472 RegScavenger *RS) const {
1473 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001474 // This tells PEI to spill the FP as if it is any other callee-save register
1475 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1476 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1477 // to combine multiple loads / stores.
1478 bool CanEliminateFrame = true;
1479 bool CS1Spilled = false;
1480 bool LRSpilled = false;
1481 unsigned NumGPRSpills = 0;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001482 unsigned NumFPRSpills = 0;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001483 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1484 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001485 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001486 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001487 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001488 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001489 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1490 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001491 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001492 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1493
1494 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1495 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001496 // since it's not always possible to restore sp from fp in a single
1497 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001498 // FIXME: It will be better just to find spare register here.
1499 if (AFI->isThumb2Function() &&
1500 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Matthias Braun02564862015-07-14 17:17:13 +00001501 SavedRegs.set(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001502
Evan Cheng572756a2011-01-16 05:14:33 +00001503 if (AFI->isThumb1OnlyFunction()) {
1504 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001505 if (AFI->getArgRegsSaveSize() > 0)
Matthias Braun02564862015-07-14 17:17:13 +00001506 SavedRegs.set(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001507
Jim Grosbachdca85312011-06-13 21:18:25 +00001508 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1509 // for sure what the stack size will be, but for this, an estimate is good
1510 // enough. If there anything changes it, it'll be a spill, which implies
1511 // we've used all the registers and so R4 is already used, so not marking
Chad Rosieradd38c12011-10-20 00:07:12 +00001512 // it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001513 // FIXME: It will be better just to find spare register here.
Hal Finkel628ba122013-03-14 21:15:20 +00001514 unsigned StackSize = MFI->estimateStackSize(MF);
Chad Rosieradd38c12011-10-20 00:07:12 +00001515 if (MFI->hasVarSizedObjects() || StackSize > 508)
Matthias Braun02564862015-07-14 17:17:13 +00001516 SavedRegs.set(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001517 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001518
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001519 // See if we can spill vector registers to aligned stack.
Matthias Braun02564862015-07-14 17:17:13 +00001520 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001521
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001522 // Spill the BasePtr if it's used.
1523 if (RegInfo->hasBasePointer(MF))
Matthias Braun02564862015-07-14 17:17:13 +00001524 SavedRegs.set(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001525
1526 // Don't spill FP if the frame can be eliminated. This is determined
Matthias Braun02564862015-07-14 17:17:13 +00001527 // by scanning the callee-save registers to see if any is modified.
Craig Topper840beec2014-04-04 05:16:06 +00001528 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001529 for (unsigned i = 0; CSRegs[i]; ++i) {
1530 unsigned Reg = CSRegs[i];
1531 bool Spilled = false;
Matthias Braun02564862015-07-14 17:17:13 +00001532 if (SavedRegs.test(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001533 Spilled = true;
1534 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001535 }
1536
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001537 if (!ARM::GPRRegClass.contains(Reg)) {
1538 if (Spilled) {
1539 if (ARM::SPRRegClass.contains(Reg))
1540 NumFPRSpills++;
1541 else if (ARM::DPRRegClass.contains(Reg))
1542 NumFPRSpills += 2;
1543 else if (ARM::QPRRegClass.contains(Reg))
1544 NumFPRSpills += 4;
1545 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001546 continue;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001547 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001548
1549 if (Spilled) {
1550 NumGPRSpills++;
1551
Tim Northoverf8b0a7a2016-05-13 19:16:14 +00001552 if (!STI.splitFramePushPop()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001553 if (Reg == ARM::LR)
1554 LRSpilled = true;
1555 CS1Spilled = true;
1556 continue;
1557 }
1558
1559 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1560 switch (Reg) {
1561 case ARM::LR:
1562 LRSpilled = true;
1563 // Fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +00001564 case ARM::R0: case ARM::R1:
1565 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001566 case ARM::R4: case ARM::R5:
1567 case ARM::R6: case ARM::R7:
1568 CS1Spilled = true;
1569 break;
1570 default:
1571 break;
1572 }
1573 } else {
Tim Northoverf8b0a7a2016-05-13 19:16:14 +00001574 if (!STI.splitFramePushPop()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001575 UnspilledCS1GPRs.push_back(Reg);
1576 continue;
1577 }
1578
1579 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001580 case ARM::R0: case ARM::R1:
1581 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001582 case ARM::R4: case ARM::R5:
1583 case ARM::R6: case ARM::R7:
1584 case ARM::LR:
1585 UnspilledCS1GPRs.push_back(Reg);
1586 break;
1587 default:
1588 UnspilledCS2GPRs.push_back(Reg);
1589 break;
1590 }
1591 }
1592 }
1593
1594 bool ForceLRSpill = false;
1595 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1596 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1597 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1598 // use of BL to implement far jump. If it turns out that it's not needed
1599 // then the branch fix up path will undo it.
1600 if (FnSize >= (1 << 11)) {
1601 CanEliminateFrame = false;
1602 ForceLRSpill = true;
1603 }
1604 }
1605
1606 // If any of the stack slot references may be out of range of an immediate
1607 // offset, make sure a register (or a spill slot) is available for the
1608 // register scavenger. Note that if we're indexing off the frame pointer, the
1609 // effective stack size is 4 bytes larger since the FP points to the stack
1610 // slot of the previous FP. Also, if we have variable sized objects in the
1611 // function, stack slot references will often be negative, and some of
1612 // our instructions are positive-offset only, so conservatively consider
1613 // that case to want a spill slot (or register) as well. Similarly, if
1614 // the function adjusts the stack pointer during execution and the
1615 // adjustments aren't already part of our stack size estimate, our offset
1616 // calculations may be off, so be conservative.
1617 // FIXME: We could add logic to be more precise about negative offsets
1618 // and which instructions will need a scratch register for them. Is it
1619 // worth the effort and added fragility?
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001620 unsigned EstimatedStackSize =
1621 MFI->estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
1622 if (hasFP(MF)) {
1623 if (AFI->hasStackFrame())
1624 EstimatedStackSize += 4;
1625 } else {
1626 // If FP is not used, SP will be used to access arguments, so count the
1627 // size of arguments into the estimation.
1628 EstimatedStackSize += MF.getInfo<ARMFunctionInfo>()->getArgumentStackSize();
1629 }
1630 EstimatedStackSize += 16; // For possible paddings.
1631
1632 bool BigStack = EstimatedStackSize >= estimateRSStackSizeLimit(MF, this) ||
Vitaly Buka6b5c8922016-05-04 19:44:11 +00001633 MFI->hasVarSizedObjects() ||
1634 (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001635 bool ExtraCSSpill = false;
1636 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1637 AFI->setHasStackFrame(true);
1638
1639 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1640 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1641 if (!LRSpilled && CS1Spilled) {
Matthias Braun02564862015-07-14 17:17:13 +00001642 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001643 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001644 SmallVectorImpl<unsigned>::iterator LRPos;
1645 LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1646 (unsigned)ARM::LR);
1647 if (LRPos != UnspilledCS1GPRs.end())
1648 UnspilledCS1GPRs.erase(LRPos);
1649
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001650 ForceLRSpill = false;
1651 ExtraCSSpill = true;
1652 }
1653
1654 if (hasFP(MF)) {
Matthias Braun02564862015-07-14 17:17:13 +00001655 SavedRegs.set(FramePtr);
Joerg Sonnenberger818e7252014-05-06 20:43:01 +00001656 auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1657 FramePtr);
1658 if (FPPos != UnspilledCS1GPRs.end())
1659 UnspilledCS1GPRs.erase(FPPos);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001660 NumGPRSpills++;
1661 }
1662
1663 // If stack and double are 8-byte aligned and we are spilling an odd number
1664 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1665 // the integer and double callee save areas.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001666 unsigned TargetAlign = getStackAlignment();
Tim Northoverdc0d9e42014-11-05 00:27:20 +00001667 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001668 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1669 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1670 unsigned Reg = UnspilledCS1GPRs[i];
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001671 // Don't spill high register if the function is thumb. In the case of
1672 // Windows on ARM, accept R11 (frame pointer)
Peter Collingbourne78f1ecc2015-04-23 20:31:26 +00001673 if (!AFI->isThumbFunction() ||
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001674 (STI.isTargetWindows() && Reg == ARM::R11) ||
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001675 isARMLowRegister(Reg) || Reg == ARM::LR) {
Matthias Braun02564862015-07-14 17:17:13 +00001676 SavedRegs.set(Reg);
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001677 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001678 ExtraCSSpill = true;
1679 break;
1680 }
1681 }
1682 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1683 unsigned Reg = UnspilledCS2GPRs.front();
Matthias Braun02564862015-07-14 17:17:13 +00001684 SavedRegs.set(Reg);
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001685 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001686 ExtraCSSpill = true;
1687 }
1688 }
1689
1690 // Estimate if we might need to scavenge a register at some point in order
1691 // to materialize a stack offset. If so, either spill one additional
1692 // callee-saved register or reserve a special spill slot to facilitate
1693 // register scavenging. Thumb1 needs a spill slot for stack pointer
1694 // adjustments also, even when the frame itself is small.
1695 if (BigStack && !ExtraCSSpill) {
1696 // If any non-reserved CS register isn't spilled, just spill one or two
1697 // extra. That should take care of it!
1698 unsigned NumExtras = TargetAlign / 4;
1699 SmallVector<unsigned, 2> Extras;
1700 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1701 unsigned Reg = UnspilledCS1GPRs.back();
1702 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001703 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001704 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1705 Reg == ARM::LR)) {
1706 Extras.push_back(Reg);
1707 NumExtras--;
1708 }
1709 }
1710 // For non-Thumb1 functions, also check for hi-reg CS registers
1711 if (!AFI->isThumb1OnlyFunction()) {
1712 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1713 unsigned Reg = UnspilledCS2GPRs.back();
1714 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001715 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001716 Extras.push_back(Reg);
1717 NumExtras--;
1718 }
1719 }
1720 }
1721 if (Extras.size() && NumExtras == 0) {
1722 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Matthias Braun02564862015-07-14 17:17:13 +00001723 SavedRegs.set(Extras[i]);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001724 }
1725 } else if (!AFI->isThumb1OnlyFunction()) {
1726 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1727 // closest to SP or frame pointer.
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001728 assert(RS && "Register scavenging not provided");
Craig Topperc7242e02012-04-20 07:30:17 +00001729 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Hal Finkel9e331c22013-03-22 23:32:27 +00001730 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001731 RC->getAlignment(),
1732 false));
1733 }
1734 }
1735 }
1736
1737 if (ForceLRSpill) {
Matthias Braun02564862015-07-14 17:17:13 +00001738 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001739 AFI->setLRIsSpilledForFarJump(true);
1740 }
1741}
Eli Bendersky8da87162013-02-21 20:05:00 +00001742
Hans Wennborge1a2e902016-03-31 18:33:38 +00001743MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
1744 MachineFunction &MF, MachineBasicBlock &MBB,
1745 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001746 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001747 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001748 if (!hasReservedCallFrame(MF)) {
1749 // If we have alloca, convert as follows:
1750 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1751 // ADJCALLSTACKUP -> add, sp, sp, amount
1752 MachineInstr *Old = I;
1753 DebugLoc dl = Old->getDebugLoc();
1754 unsigned Amount = Old->getOperand(0).getImm();
1755 if (Amount != 0) {
1756 // We need to keep the stack aligned properly. To do this, we round the
1757 // amount of space needed for the outgoing arguments up to the next
1758 // alignment boundary.
Guozhi Weif66d3842015-08-17 22:36:27 +00001759 Amount = alignSPAdjust(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +00001760
1761 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1762 assert(!AFI->isThumb1OnlyFunction() &&
1763 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1764 bool isARM = !AFI->isThumbFunction();
1765
1766 // Replace the pseudo instruction with a new instruction...
1767 unsigned Opc = Old->getOpcode();
1768 int PIdx = Old->findFirstPredOperandIdx();
1769 ARMCC::CondCodes Pred = (PIdx == -1)
1770 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1771 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1772 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1773 unsigned PredReg = Old->getOperand(2).getReg();
1774 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1775 Pred, PredReg);
1776 } else {
1777 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1778 unsigned PredReg = Old->getOperand(3).getReg();
1779 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1780 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1781 Pred, PredReg);
1782 }
1783 }
1784 }
Hans Wennborge1a2e902016-03-31 18:33:38 +00001785 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00001786}
1787
Oliver Stannardb14c6252014-04-02 16:10:33 +00001788/// Get the minimum constant for ARM that is greater than or equal to the
1789/// argument. In ARM, constants can have any value that can be produced by
1790/// rotating an 8-bit value to the right by an even number of bits within a
1791/// 32-bit word.
1792static uint32_t alignToARMConstant(uint32_t Value) {
1793 unsigned Shifted = 0;
1794
1795 if (Value == 0)
1796 return 0;
1797
1798 while (!(Value & 0xC0000000)) {
1799 Value = Value << 2;
1800 Shifted += 2;
1801 }
1802
1803 bool Carry = (Value & 0x00FFFFFF);
1804 Value = ((Value & 0xFF000000) >> 24) + Carry;
1805
1806 if (Value & 0x0000100)
1807 Value = Value & 0x000001FC;
1808
1809 if (Shifted > 24)
1810 Value = Value >> (Shifted - 24);
1811 else
1812 Value = Value << (24 - Shifted);
1813
1814 return Value;
1815}
1816
1817// The stack limit in the TCB is set to this many bytes above the actual
1818// stack limit.
1819static const uint64_t kSplitStackAvailable = 256;
1820
1821// Adjust the function prologue to enable split stacks. This currently only
1822// supports android and linux.
1823//
1824// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
1825// must be well defined in order to allow for consistent implementations of the
1826// __morestack helper function. The ABI is also not a normal ABI in that it
1827// doesn't follow the normal calling conventions because this allows the
1828// prologue of each function to be optimized further.
1829//
1830// Currently, the ABI looks like (when calling __morestack)
1831//
1832// * r4 holds the minimum stack size requested for this function call
1833// * r5 holds the stack size of the arguments to the function
1834// * the beginning of the function is 3 instructions after the call to
1835// __morestack
1836//
1837// Implementations of __morestack should use r4 to allocate a new stack, r5 to
1838// place the arguments on to the new stack, and the 3-instruction knowledge to
1839// jump directly to the body of the function when working on the new stack.
1840//
1841// An old (and possibly no longer compatible) implementation of __morestack for
1842// ARM can be found at [1].
1843//
1844// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
Quentin Colombet61b305e2015-05-05 17:38:16 +00001845void ARMFrameLowering::adjustForSegmentedStacks(
1846 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
Oliver Stannardb14c6252014-04-02 16:10:33 +00001847 unsigned Opcode;
1848 unsigned CFIIndex;
Eric Christopher22b2ad22015-02-20 08:24:37 +00001849 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
Oliver Stannardb14c6252014-04-02 16:10:33 +00001850 bool Thumb = ST->isThumb();
1851
1852 // Sadly, this currently doesn't support varargs, platforms other than
1853 // android/linux. Note that thumb1/thumb2 are support for android/linux.
1854 if (MF.getFunction()->isVarArg())
1855 report_fatal_error("Segmented stacks do not support vararg functions.");
1856 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00001857 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00001858
Oliver Stannardb14c6252014-04-02 16:10:33 +00001859 MachineFrameInfo *MFI = MF.getFrameInfo();
1860 MachineModuleInfo &MMI = MF.getMMI();
1861 MCContext &Context = MMI.getContext();
1862 const MCRegisterInfo *MRI = Context.getRegisterInfo();
1863 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001864 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00001865 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
1866 DebugLoc DL;
1867
Tim Northoverf9e798b2014-05-22 13:03:43 +00001868 uint64_t StackSize = MFI->getStackSize();
1869
1870 // Do not generate a prologue for functions with a stack of size zero
1871 if (StackSize == 0)
1872 return;
1873
Oliver Stannardb14c6252014-04-02 16:10:33 +00001874 // Use R4 and R5 as scratch registers.
1875 // We save R4 and R5 before use and restore them before leaving the function.
1876 unsigned ScratchReg0 = ARM::R4;
1877 unsigned ScratchReg1 = ARM::R5;
1878 uint64_t AlignedStackSize;
1879
1880 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
1881 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
1882 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
1883 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
1884 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
1885
Quentin Colombet71a71482015-07-20 21:42:14 +00001886 // Grab everything that reaches PrologueMBB to update there liveness as well.
1887 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
1888 SmallVector<MachineBasicBlock *, 2> WalkList;
1889 WalkList.push_back(&PrologueMBB);
1890
1891 do {
1892 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
1893 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
1894 if (BeforePrologueRegion.insert(PredBB).second)
1895 WalkList.push_back(PredBB);
1896 }
1897 } while (!WalkList.empty());
1898
1899 // The order in that list is important.
1900 // The blocks will all be inserted before PrologueMBB using that order.
1901 // Therefore the block that should appear first in the CFG should appear
1902 // first in the list.
1903 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
1904 PostStackMBB};
Quentin Colombet71a71482015-07-20 21:42:14 +00001905
Craig Topper80720812015-12-01 06:13:01 +00001906 for (MachineBasicBlock *B : AddedBlocks)
1907 BeforePrologueRegion.insert(B);
Quentin Colombet71a71482015-07-20 21:42:14 +00001908
Matthias Braund9da1622015-09-09 18:08:03 +00001909 for (const auto &LI : PrologueMBB.liveins()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00001910 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
Matthias Braunb2b7ef12015-08-24 22:59:52 +00001911 PredBB->addLiveIn(LI);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001912 }
1913
Quentin Colombet71a71482015-07-20 21:42:14 +00001914 // Remove the newly added blocks from the list, since we know
1915 // we do not have to do the following updates for them.
Craig Topper80720812015-12-01 06:13:01 +00001916 for (MachineBasicBlock *B : AddedBlocks) {
1917 BeforePrologueRegion.erase(B);
1918 MF.insert(PrologueMBB.getIterator(), B);
Quentin Colombet71a71482015-07-20 21:42:14 +00001919 }
1920
1921 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
1922 // Make sure the LiveIns are still sorted and unique.
1923 MBB->sortUniqueLiveIns();
1924 // Replace the edges to PrologueMBB by edges to the sequences
1925 // we are about to add.
1926 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
1927 }
Oliver Stannardb14c6252014-04-02 16:10:33 +00001928
1929 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00001930 AlignedStackSize = alignToARMConstant(StackSize);
1931
1932 // When the frame size is less than 256 we just compare the stack
1933 // boundary directly to the value of the stack pointer, per gcc.
1934 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
1935
1936 // We will use two of the callee save registers as scratch registers so we
1937 // need to save those registers onto the stack.
1938 // We will use SR0 to hold stack limit and SR1 to hold the stack size
1939 // requested and arguments for __morestack().
1940 // SR0: Scratch Register #0
1941 // SR1: Scratch Register #1
1942 // push {SR0, SR1}
1943 if (Thumb) {
1944 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH)))
1945 .addReg(ScratchReg0).addReg(ScratchReg1);
1946 } else {
1947 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
1948 .addReg(ARM::SP, RegState::Define).addReg(ARM::SP))
1949 .addReg(ScratchReg0).addReg(ScratchReg1);
1950 }
1951
1952 // Emit the relevant DWARF information about the change in stack pointer as
1953 // well as where to find both r4 and r5 (the callee-save registers)
1954 CFIIndex =
1955 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
1956 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1957 .addCFIIndex(CFIIndex);
1958 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1959 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
1960 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1961 .addCFIIndex(CFIIndex);
1962 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1963 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
1964 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1965 .addCFIIndex(CFIIndex);
1966
1967 // mov SR1, sp
1968 if (Thumb) {
1969 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
1970 .addReg(ARM::SP));
1971 } else if (CompareStackPointer) {
1972 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
1973 .addReg(ARM::SP)).addReg(0);
1974 }
1975
1976 // sub SR1, sp, #StackSize
1977 if (!CompareStackPointer && Thumb) {
1978 AddDefaultPred(
1979 AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1))
1980 .addReg(ScratchReg1).addImm(AlignedStackSize));
1981 } else if (!CompareStackPointer) {
1982 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
1983 .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0);
1984 }
1985
1986 if (Thumb && ST->isThumb1Only()) {
1987 unsigned PCLabelId = ARMFI->createPICLabelUId();
1988 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Oliver Stannard92e0fc02014-04-03 08:45:16 +00001989 MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001990 MachineConstantPool *MCP = MF.getConstantPool();
Tim Northover956b0082015-10-02 18:07:13 +00001991 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001992
1993 // ldr SR0, [pc, offset(STACK_LIMIT)]
1994 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
1995 .addConstantPoolIndex(CPI));
1996
1997 // ldr SR0, [SR0]
1998 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
1999 .addReg(ScratchReg0).addImm(0));
2000 } else {
2001 // Get TLS base address from the coprocessor
2002 // mrc p15, #0, SR0, c13, c0, #3
2003 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2004 .addImm(15)
2005 .addImm(0)
2006 .addImm(13)
2007 .addImm(0)
2008 .addImm(3));
2009
2010 // Use the last tls slot on android and a private field of the TCP on linux.
2011 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2012 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2013
2014 // Get the stack limit from the right offset
2015 // ldr SR0, [sr0, #4 * TlsOffset]
2016 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2017 .addReg(ScratchReg0).addImm(4 * TlsOffset));
2018 }
2019
2020 // Compare stack limit with stack size requested.
2021 // cmp SR0, SR1
2022 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
2023 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode))
2024 .addReg(ScratchReg0)
2025 .addReg(ScratchReg1));
2026
2027 // This jump is taken if StackLimit < SP - stack required.
2028 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2029 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2030 .addImm(ARMCC::LO)
2031 .addReg(ARM::CPSR);
2032
2033
2034 // Calling __morestack(StackSize, Size of stack arguments).
2035 // __morestack knows that the stack size requested is in SR0(r4)
2036 // and amount size of stack arguments is in SR1(r5).
2037
2038 // Pass first argument for the __morestack by Scratch Register #0.
2039 // The amount size of stack required
2040 if (Thumb) {
2041 AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8),
2042 ScratchReg0)).addImm(AlignedStackSize));
2043 } else {
2044 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2045 .addImm(AlignedStackSize)).addReg(0);
2046 }
2047 // Pass second argument for the __morestack by Scratch Register #1.
2048 // The amount size of stack consumed to save function arguments.
2049 if (Thumb) {
2050 AddDefaultPred(
2051 AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1))
2052 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())));
2053 } else {
2054 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2055 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())))
2056 .addReg(0);
2057 }
2058
2059 // push {lr} - Save return address of this function.
2060 if (Thumb) {
2061 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH)))
2062 .addReg(ARM::LR);
2063 } else {
2064 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2065 .addReg(ARM::SP, RegState::Define)
2066 .addReg(ARM::SP))
2067 .addReg(ARM::LR);
2068 }
2069
2070 // Emit the DWARF info about the change in stack as well as where to find the
2071 // previous link register
2072 CFIIndex =
2073 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
2074 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2075 .addCFIIndex(CFIIndex);
2076 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
2077 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2078 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2079 .addCFIIndex(CFIIndex);
2080
2081 // Call __morestack().
2082 if (Thumb) {
2083 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL)))
2084 .addExternalSymbol("__morestack");
2085 } else {
2086 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2087 .addExternalSymbol("__morestack");
2088 }
2089
2090 // pop {lr} - Restore return address of this original function.
2091 if (Thumb) {
2092 if (ST->isThumb1Only()) {
2093 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2094 .addReg(ScratchReg0);
2095 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2096 .addReg(ScratchReg0));
2097 } else {
2098 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2099 .addReg(ARM::LR, RegState::Define)
2100 .addReg(ARM::SP, RegState::Define)
2101 .addReg(ARM::SP)
2102 .addImm(4));
2103 }
2104 } else {
2105 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2106 .addReg(ARM::SP, RegState::Define)
2107 .addReg(ARM::SP))
2108 .addReg(ARM::LR);
2109 }
2110
2111 // Restore SR0 and SR1 in case of __morestack() was called.
2112 // __morestack() will skip PostStackMBB block so we need to restore
2113 // scratch registers from here.
2114 // pop {SR0, SR1}
2115 if (Thumb) {
2116 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2117 .addReg(ScratchReg0)
2118 .addReg(ScratchReg1);
2119 } else {
2120 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2121 .addReg(ARM::SP, RegState::Define)
2122 .addReg(ARM::SP))
2123 .addReg(ScratchReg0)
2124 .addReg(ScratchReg1);
2125 }
2126
2127 // Update the CFA offset now that we've popped
2128 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2129 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2130 .addCFIIndex(CFIIndex);
2131
2132 // bx lr - Return from this function.
2133 Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
2134 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode)));
2135
2136 // Restore SR0 and SR1 in case of __morestack() was not called.
2137 // pop {SR0, SR1}
2138 if (Thumb) {
2139 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP)))
2140 .addReg(ScratchReg0)
2141 .addReg(ScratchReg1);
2142 } else {
2143 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2144 .addReg(ARM::SP, RegState::Define)
2145 .addReg(ARM::SP))
2146 .addReg(ScratchReg0)
2147 .addReg(ScratchReg1);
2148 }
2149
2150 // Update the CFA offset now that we've popped
2151 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2152 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2153 .addCFIIndex(CFIIndex);
2154
2155 // Tell debuggers that r4 and r5 are now the same as they were in the
2156 // previous function, that they're the "Same Value".
2157 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2158 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2159 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2160 .addCFIIndex(CFIIndex);
2161 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2162 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2163 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2164 .addCFIIndex(CFIIndex);
2165
2166 // Organizing MBB lists
Quentin Colombet61b305e2015-05-05 17:38:16 +00002167 PostStackMBB->addSuccessor(&PrologueMBB);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002168
2169 AllocMBB->addSuccessor(PostStackMBB);
2170
2171 GetMBB->addSuccessor(PostStackMBB);
2172 GetMBB->addSuccessor(AllocMBB);
2173
2174 McrMBB->addSuccessor(GetMBB);
2175
2176 PrevStackMBB->addSuccessor(McrMBB);
2177
Filipe Cabecinhas0da99372016-04-29 15:22:48 +00002178#ifdef EXPENSIVE_CHECKS
Oliver Stannardb14c6252014-04-02 16:10:33 +00002179 MF.verify();
2180#endif
2181}