blob: 61be18c2b54d850e2775db8db7311b015dec35ef [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();
Oliver Stannard9aa6f012016-08-23 09:19:22 +000060 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000061
Oliver Stannard9aa6f012016-08-23 09:19:22 +000062 // ABI-required frame pointer.
63 if (MF.getTarget().Options.DisableFramePointerElim(MF))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000064 return true;
65
Oliver Stannard9aa6f012016-08-23 09:19:22 +000066 // Frame pointer required for use within this function.
67 return (RegInfo->needsStackRealignment(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +000068 MFI.hasVarSizedObjects() ||
69 MFI.isFrameAddressTaken());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000070}
71
Bob Wilson657f2272011-01-13 21:10:12 +000072/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
73/// not required, we reserve argument space for call sites in the function
74/// immediately on entry to the current function. This eliminates the need for
75/// add/sub sp brackets around call sites. Returns true if the call frame is
76/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000077bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000078 const MachineFrameInfo &MFI = MF.getFrameInfo();
79 unsigned CFSize = MFI.getMaxCallFrameSize();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000080 // It's not always a good idea to include the call frame as part of the
81 // stack frame. ARM (especially Thumb) has small immediate offset to
82 // address the stack frame. So a large call frame can cause poor codegen
83 // and may even makes it impossible to scavenge a register.
84 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
85 return false;
86
Matthias Braun941a7052016-07-28 18:40:00 +000087 return !MFI.hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000088}
89
Bob Wilson657f2272011-01-13 21:10:12 +000090/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
91/// call frame pseudos can be simplified. Unlike most targets, having a FP
92/// is not sufficient here since we still may reference some objects via SP
93/// even when FP is available in Thumb2 mode.
94bool
95ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000096 return hasReservedCallFrame(MF) || MF.getFrameInfo().hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000097}
98
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +000099static bool isCSRestore(MachineInstr &MI, const ARMBaseInstrInfo &TII,
Craig Topper840beec2014-04-04 05:16:06 +0000100 const MCPhysReg *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000101 // Integer spill area is handled with "pop".
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000102 if (isPopOpcode(MI.getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000103 // The first two operands are predicates. The last two are
104 // imp-def and imp-use of SP. Check everything in between.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000105 for (int i = 5, e = MI.getNumOperands(); i != e; ++i)
106 if (!isCalleeSavedRegister(MI.getOperand(i).getReg(), CSRegs))
Eric Christopherb006fc92010-11-18 19:40:05 +0000107 return false;
108 return true;
109 }
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000110 if ((MI.getOpcode() == ARM::LDR_POST_IMM ||
111 MI.getOpcode() == ARM::LDR_POST_REG ||
112 MI.getOpcode() == ARM::t2LDR_POST) &&
113 isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs) &&
114 MI.getOperand(1).getReg() == ARM::SP)
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000115 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000116
117 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000118}
119
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000120static void emitRegPlusImmediate(
121 bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
122 const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
123 unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
124 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000125 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000126 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000127 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000128 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000129 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000130 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000131}
132
Tim Northoverc9432eb2013-11-04 23:04:15 +0000133static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000134 MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
Tim Northoverc9432eb2013-11-04 23:04:15 +0000135 const ARMBaseInstrInfo &TII, int NumBytes,
136 unsigned MIFlags = MachineInstr::NoFlags,
137 ARMCC::CondCodes Pred = ARMCC::AL,
138 unsigned PredReg = 0) {
139 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
140 MIFlags, Pred, PredReg);
141}
142
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000143static int sizeOfSPAdjustment(const MachineInstr &MI) {
Tim Northover603d3162014-11-14 22:45:33 +0000144 int RegSize;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000145 switch (MI.getOpcode()) {
Tim Northover603d3162014-11-14 22:45:33 +0000146 case ARM::VSTMDDB_UPD:
147 RegSize = 8;
148 break;
149 case ARM::STMDB_UPD:
150 case ARM::t2STMDB_UPD:
151 RegSize = 4;
152 break;
153 case ARM::t2STR_PRE:
154 case ARM::STR_PRE_IMM:
155 return 4;
156 default:
157 llvm_unreachable("Unknown push or pop like instruction");
158 }
159
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000160 int count = 0;
161 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
162 // pred) so the list starts at 4.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000163 for (int i = MI.getNumOperands() - 1; i >= 4; --i)
Tim Northover603d3162014-11-14 22:45:33 +0000164 count += RegSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000165 return count;
166}
167
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000168static bool WindowsRequiresStackProbe(const MachineFunction &MF,
169 size_t StackSizeInBytes) {
Matthias Braun941a7052016-07-28 18:40:00 +0000170 const MachineFrameInfo &MFI = MF.getFrameInfo();
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000171 const Function *F = MF.getFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000172 unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000173 if (F->hasFnAttribute("stack-probe-size"))
174 F->getFnAttribute("stack-probe-size")
175 .getValueAsString()
176 .getAsInteger(0, StackProbeSize);
177 return StackSizeInBytes >= StackProbeSize;
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000178}
179
Tim Northover603d3162014-11-14 22:45:33 +0000180namespace {
181struct StackAdjustingInsts {
182 struct InstInfo {
183 MachineBasicBlock::iterator I;
184 unsigned SPAdjust;
185 bool BeforeFPSet;
186 };
187
188 SmallVector<InstInfo, 4> Insts;
189
190 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
191 bool BeforeFPSet = false) {
192 InstInfo Info = {I, SPAdjust, BeforeFPSet};
193 Insts.push_back(Info);
194 }
195
196 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
David Majnemer562e8292016-08-12 00:18:03 +0000197 auto Info = find_if(Insts, [&](InstInfo &Info) { return Info.I == I; });
Tim Northover603d3162014-11-14 22:45:33 +0000198 assert(Info != Insts.end() && "invalid sp adjusting instruction");
199 Info->SPAdjust += ExtraBytes;
200 }
201
202 void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000203 const DebugLoc &dl, const ARMBaseInstrInfo &TII,
204 bool HasFP) {
Tim Northover603d3162014-11-14 22:45:33 +0000205 unsigned CFAOffset = 0;
206 for (auto &Info : Insts) {
207 if (HasFP && !Info.BeforeFPSet)
208 return;
209
210 CFAOffset -= Info.SPAdjust;
211 unsigned CFIIndex = MMI.addFrameInst(
212 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
213 BuildMI(MBB, std::next(Info.I), dl,
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000214 TII.get(TargetOpcode::CFI_INSTRUCTION))
215 .addCFIIndex(CFIIndex)
216 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000217 }
218 }
219};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000220}
Tim Northover603d3162014-11-14 22:45:33 +0000221
Kristof Beyls933de7a2015-01-08 15:09:14 +0000222/// Emit an instruction sequence that will align the address in
223/// register Reg by zero-ing out the lower bits. For versions of the
224/// architecture that support Neon, this must be done in a single
225/// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
226/// single instruction. That function only gets called when optimizing
227/// spilling of D registers on a core with the Neon instruction set
228/// present.
229static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
230 const TargetInstrInfo &TII,
231 MachineBasicBlock &MBB,
232 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000233 const DebugLoc &DL, const unsigned Reg,
Kristof Beyls933de7a2015-01-08 15:09:14 +0000234 const unsigned Alignment,
235 const bool MustBeSingleInstruction) {
Eric Christopher1b21f002015-01-29 00:19:33 +0000236 const ARMSubtarget &AST =
237 static_cast<const ARMSubtarget &>(MF.getSubtarget());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000238 const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
239 const unsigned AlignMask = Alignment - 1;
240 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
241 assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
242 if (!AFI->isThumbFunction()) {
243 // if the BFC instruction is available, use that to zero the lower
244 // bits:
245 // bfc Reg, #0, log2(Alignment)
246 // otherwise use BIC, if the mask to zero the required number of bits
247 // can be encoded in the bic immediate field
248 // bic Reg, Reg, Alignment-1
249 // otherwise, emit
250 // lsr Reg, Reg, log2(Alignment)
251 // lsl Reg, Reg, log2(Alignment)
252 if (CanUseBFC) {
253 AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
254 .addReg(Reg, RegState::Kill)
255 .addImm(~AlignMask));
256 } else if (AlignMask <= 255) {
257 AddDefaultCC(
258 AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
259 .addReg(Reg, RegState::Kill)
260 .addImm(AlignMask)));
261 } else {
262 assert(!MustBeSingleInstruction &&
263 "Shouldn't call emitAligningInstructions demanding a single "
264 "instruction to be emitted for large stack alignment for a target "
265 "without BFC.");
266 AddDefaultCC(AddDefaultPred(
267 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
268 .addReg(Reg, RegState::Kill)
269 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))));
270 AddDefaultCC(AddDefaultPred(
271 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
272 .addReg(Reg, RegState::Kill)
273 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))));
274 }
275 } else {
276 // Since this is only reached for Thumb-2 targets, the BFC instruction
277 // should always be available.
278 assert(CanUseBFC);
279 AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
280 .addReg(Reg, RegState::Kill)
281 .addImm(~AlignMask));
282 }
283}
284
Quentin Colombet61b305e2015-05-05 17:38:16 +0000285void ARMFrameLowering::emitPrologue(MachineFunction &MF,
286 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000287 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000288 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000289 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000290 MachineModuleInfo &MMI = MF.getMMI();
291 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000292 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000293 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopher1b21f002015-01-29 00:19:33 +0000294 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
295 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000296 assert(!AFI->isThumb1OnlyFunction() &&
297 "This emitPrologue does not support Thumb1!");
298 bool isARM = !AFI->isThumbFunction();
Eric Christopher1b21f002015-01-29 00:19:33 +0000299 unsigned Align = STI.getFrameLowering()->getStackAlignment();
Tim Northover775aaeb2015-11-05 21:54:58 +0000300 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000301 unsigned NumBytes = MFI.getStackSize();
302 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000303
304 // Debug location must be unknown since the first debug location is used
305 // to determine the end of the prologue.
306 DebugLoc dl;
307
308 unsigned FramePtr = RegInfo->getFrameRegister(MF);
309
310 // Determine the sizes of each callee-save spill areas and record which frame
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000311 // belongs to which callee-save spill areas.
312 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
313 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000314 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000315
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000316 // All calls are tail calls in GHC calling conv, and functions have no
317 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000318 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
319 return;
320
Tim Northover603d3162014-11-14 22:45:33 +0000321 StackAdjustingInsts DefCFAOffsetCandidates;
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000322 bool HasFP = hasFP(MF);
Tim Northover603d3162014-11-14 22:45:33 +0000323
Oliver Stannardd55e1152014-03-05 15:25:27 +0000324 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000325 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000326 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000327 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000328 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000329 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000330
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000331 if (!AFI->hasStackFrame() &&
332 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000333 if (NumBytes - ArgRegsSaveSize != 0) {
334 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000335 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000336 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
337 NumBytes - ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000338 }
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000339 DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000340 return;
341 }
342
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000343 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000344 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
345 unsigned Reg = CSI[i].getReg();
346 int FI = CSI[i].getFrameIdx();
347 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000348 case ARM::R8:
349 case ARM::R9:
350 case ARM::R10:
351 case ARM::R11:
352 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000353 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000354 GPRCS2Size += 4;
355 break;
356 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000357 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +0000358 case ARM::R0:
359 case ARM::R1:
360 case ARM::R2:
361 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000362 case ARM::R4:
363 case ARM::R5:
364 case ARM::R6:
365 case ARM::R7:
366 case ARM::LR:
367 if (Reg == FramePtr)
368 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000369 GPRCS1Size += 4;
370 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000371 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000372 // This is a DPR. Exclude the aligned DPRCS2 spills.
373 if (Reg == ARM::D8)
374 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000375 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000376 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000377 }
378 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000379
Eric Christopherb006fc92010-11-18 19:40:05 +0000380 // Move past area 1.
Tim Northover603d3162014-11-14 22:45:33 +0000381 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
382 if (GPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000383 GPRCS1Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000384 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
385 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000386
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000387 // Determine starting offsets of spill areas.
Tim Northover228c9432014-11-05 00:27:13 +0000388 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
389 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
390 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
391 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
392 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000393 int FramePtrOffsetInPush = 0;
394 if (HasFP) {
Tim Northover603d3162014-11-14 22:45:33 +0000395 FramePtrOffsetInPush =
Matthias Braun941a7052016-07-28 18:40:00 +0000396 MFI.getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize;
397 AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000398 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000399 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000400 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
401 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
402 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
403
Tim Northoverc9432eb2013-11-04 23:04:15 +0000404 // Move past area 2.
Tim Northover603d3162014-11-14 22:45:33 +0000405 if (GPRCS2Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000406 GPRCS2Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000407 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
408 }
Tim Northoverc9432eb2013-11-04 23:04:15 +0000409
Tim Northover228c9432014-11-05 00:27:13 +0000410 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
411 // .cfi_offset operations will reflect that.
412 if (DPRGapSize) {
413 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
Duncan P. N. Exon Smithec083b52016-08-17 00:53:04 +0000414 if (LastPush != MBB.end() &&
415 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, DPRGapSize))
Tim Northover603d3162014-11-14 22:45:33 +0000416 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
417 else {
Tim Northover228c9432014-11-05 00:27:13 +0000418 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
419 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000420 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
421 }
Tim Northover228c9432014-11-05 00:27:13 +0000422 }
423
Eric Christopherb006fc92010-11-18 19:40:05 +0000424 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000425 if (DPRCSSize > 0) {
Evan Cheng70d29632011-02-25 00:24:46 +0000426 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000427 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000428 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000429 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(*MBBI));
Tim Northover93bcc662013-11-08 17:18:07 +0000430 LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000431 }
Evan Cheng70d29632011-02-25 00:24:46 +0000432 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000433
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000434 // Move past the aligned DPRCS2 area.
435 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
436 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
437 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
438 // leaves the stack pointer pointing to the DPRCS2 area.
439 //
440 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
Matthias Braun941a7052016-07-28 18:40:00 +0000441 NumBytes += MFI.getObjectOffset(D8SpillFI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000442 } else
443 NumBytes = DPRCSOffset;
444
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000445 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
446 uint32_t NumWords = NumBytes >> 2;
447
448 if (NumWords < 65536)
449 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000450 .addImm(NumWords)
451 .setMIFlags(MachineInstr::FrameSetup));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000452 else
453 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000454 .addImm(NumWords)
455 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000456
457 switch (TM.getCodeModel()) {
458 case CodeModel::Small:
459 case CodeModel::Medium:
460 case CodeModel::Default:
461 case CodeModel::Kernel:
462 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
463 .addImm((unsigned)ARMCC::AL).addReg(0)
464 .addExternalSymbol("__chkstk")
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000465 .addReg(ARM::R4, RegState::Implicit)
466 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000467 break;
468 case CodeModel::Large:
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000469 case CodeModel::JITDefault:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000470 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000471 .addExternalSymbol("__chkstk")
472 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000473
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000474 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
475 .addImm((unsigned)ARMCC::AL).addReg(0)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000476 .addReg(ARM::R12, RegState::Kill)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000477 .addReg(ARM::R4, RegState::Implicit)
478 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000479 break;
480 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000481
482 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr),
483 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +0000484 .addReg(ARM::SP, RegState::Kill)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000485 .addReg(ARM::R4, RegState::Kill)
486 .setMIFlags(MachineInstr::FrameSetup)));
487 NumBytes = 0;
488 }
489
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000490 if (NumBytes) {
491 // Adjust SP after all the callee-save spills.
Tim Northoverbeb5bcc2015-09-23 22:21:09 +0000492 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000493 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, NumBytes))
Tim Northover603d3162014-11-14 22:45:33 +0000494 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
495 else {
Tim Northover93bcc662013-11-08 17:18:07 +0000496 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
497 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000498 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
499 }
Tim Northover93bcc662013-11-08 17:18:07 +0000500
Evan Chengeb56dca2010-11-22 18:12:04 +0000501 if (HasFP && isARM)
502 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
503 // Note it's not safe to do this in Thumb2 mode because it would have
504 // taken two instructions:
505 // mov sp, r7
506 // sub sp, #24
507 // If an interrupt is taken between the two instructions, then sp is in
508 // an inconsistent state (pointing to the middle of callee-saved area).
509 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000510 AFI->setShouldRestoreSPFromFP(true);
511 }
512
Tim Northover603d3162014-11-14 22:45:33 +0000513 // Set FP to point to the stack slot that contains the previous FP.
514 // For iOS, FP is R7, which has now been stored in spill area 1.
515 // Otherwise, if this is not iOS, all the callee-saved registers go
516 // into spill area 1, including the FP in R11. In either case, it
517 // is in area one and the adjustment needs to take place just after
518 // that push.
519 if (HasFP) {
520 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000521 unsigned PushSize = sizeOfSPAdjustment(*GPRCS1Push);
Tim Northover603d3162014-11-14 22:45:33 +0000522 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
523 dl, TII, FramePtr, ARM::SP,
524 PushSize + FramePtrOffsetInPush,
525 MachineInstr::FrameSetup);
526 if (FramePtrOffsetInPush + PushSize != 0) {
527 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
528 nullptr, MRI->getDwarfRegNum(FramePtr, true),
529 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
530 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000531 .addCFIIndex(CFIIndex)
532 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000533 } else {
534 unsigned CFIIndex =
535 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
536 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
537 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000538 .addCFIIndex(CFIIndex)
539 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000540 }
541 }
542
543 // Now that the prologue's actual instructions are finalised, we can insert
544 // the necessary DWARF cf instructions to describe the situation. Start by
545 // recording where each register ended up:
546 if (GPRCS1Size > 0) {
547 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
548 int CFIIndex;
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000549 for (const auto &Entry : CSI) {
550 unsigned Reg = Entry.getReg();
551 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000552 switch (Reg) {
553 case ARM::R8:
554 case ARM::R9:
555 case ARM::R10:
556 case ARM::R11:
557 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000558 if (STI.splitFramePushPop(MF))
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000559 break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000560 LLVM_FALLTHROUGH;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000561 case ARM::R0:
562 case ARM::R1:
563 case ARM::R2:
564 case ARM::R3:
565 case ARM::R4:
566 case ARM::R5:
567 case ARM::R6:
568 case ARM::R7:
569 case ARM::LR:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000570 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
Matthias Braun941a7052016-07-28 18:40:00 +0000571 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000572 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000573 .addCFIIndex(CFIIndex)
574 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000575 break;
576 }
577 }
578 }
579
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000580 if (GPRCS2Size > 0) {
Tim Northover603d3162014-11-14 22:45:33 +0000581 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000582 for (const auto &Entry : CSI) {
583 unsigned Reg = Entry.getReg();
584 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000585 switch (Reg) {
586 case ARM::R8:
587 case ARM::R9:
588 case ARM::R10:
589 case ARM::R11:
590 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000591 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000592 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000593 unsigned Offset = MFI.getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000594 unsigned CFIIndex = MMI.addFrameInst(
595 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
596 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000597 .addCFIIndex(CFIIndex)
598 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000599 }
600 break;
601 }
602 }
603 }
604
605 if (DPRCSSize > 0) {
606 // Since vpush register list cannot have gaps, there may be multiple vpush
607 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000608 MachineBasicBlock::iterator Pos = std::next(LastPush);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000609 for (const auto &Entry : CSI) {
610 unsigned Reg = Entry.getReg();
611 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000612 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
613 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
614 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000615 unsigned Offset = MFI.getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000616 unsigned CFIIndex = MMI.addFrameInst(
617 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover603d3162014-11-14 22:45:33 +0000618 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000619 .addCFIIndex(CFIIndex)
620 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000621 }
622 }
623 }
624
Tim Northover603d3162014-11-14 22:45:33 +0000625 // Now we can emit descriptions of where the canonical frame address was
626 // throughout the process. If we have a frame pointer, it takes over the job
627 // half-way through, so only the first few .cfi_def_cfa_offset instructions
628 // actually get emitted.
629 DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
Tim Northover93bcc662013-11-08 17:18:07 +0000630
Evan Chengeb56dca2010-11-22 18:12:04 +0000631 if (STI.isTargetELF() && hasFP(MF))
Matthias Braun941a7052016-07-28 18:40:00 +0000632 MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
633 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000634
635 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
636 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000637 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000638 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
639
640 // If we need dynamic stack realignment, do it here. Be paranoid and make
641 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000642 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000643 // realigned.
644 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000645 unsigned MaxAlign = MFI.getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +0000646 assert(!AFI->isThumb1OnlyFunction());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000647 if (!AFI->isThumbFunction()) {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000648 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
649 false);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000650 } else {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000651 // We cannot use sp as source/dest register here, thus we're using r4 to
652 // perform the calculations. We're emitting the following sequence:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000653 // mov r4, sp
Kristof Beyls933de7a2015-01-08 15:09:14 +0000654 // -- use emitAligningInstructions to produce best sequence to zero
655 // -- out lower bits in r4
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000656 // mov sp, r4
657 // FIXME: It will be better just to find spare register here.
Jim Grosbache9cc9012011-06-30 23:38:17 +0000658 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Kristof Beyls933de7a2015-01-08 15:09:14 +0000659 .addReg(ARM::SP, RegState::Kill));
660 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
661 false);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000662 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Kristof Beyls933de7a2015-01-08 15:09:14 +0000663 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000664 }
665
666 AFI->setShouldRestoreSPFromFP(true);
667 }
668
669 // If we need a base pointer, set it up here. It's whatever the value
670 // of the stack pointer is at this point. Any variable size objects
671 // will be allocated after this, so we can still use the base pointer
672 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000673 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000674 if (RegInfo->hasBasePointer(MF)) {
675 if (isARM)
676 BuildMI(MBB, MBBI, dl,
677 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
678 .addReg(ARM::SP)
679 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
680 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000681 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000682 RegInfo->getBaseRegister())
683 .addReg(ARM::SP));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000684 }
685
686 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000687 // the sp from fp. We can assume there's an FP here since hasFP already
688 // checks for hasVarSizedObjects.
Matthias Braun941a7052016-07-28 18:40:00 +0000689 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000690 AFI->setShouldRestoreSPFromFP(true);
691}
692
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000693void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000694 MachineBasicBlock &MBB) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000695 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000696 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000697 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000698 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000699 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000700 assert(!AFI->isThumb1OnlyFunction() &&
701 "This emitEpilogue does not support Thumb1!");
702 bool isARM = !AFI->isThumbFunction();
703
Tim Northover8cda34f2015-03-11 18:54:22 +0000704 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000705 int NumBytes = (int)MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000706 unsigned FramePtr = RegInfo->getFrameRegister(MF);
707
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000708 // All calls are tail calls in GHC calling conv, and functions have no
709 // prologue/epilogue.
Quentin Colombet71a71482015-07-20 21:42:14 +0000710 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000711 return;
Quentin Colombet71a71482015-07-20 21:42:14 +0000712
713 // First put ourselves on the first (from top) terminator instructions.
714 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
715 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Eric Christopherb3322362012-08-03 00:05:53 +0000716
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000717 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000718 if (NumBytes - ArgRegsSaveSize != 0)
719 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000720 } else {
721 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000722 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000723 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000724 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000725 --MBBI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000726 } while (MBBI != MBB.begin() && isCSRestore(*MBBI, TII, CSRegs));
727 if (!isCSRestore(*MBBI, TII, CSRegs))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000728 ++MBBI;
729 }
730
731 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000732 NumBytes -= (ArgRegsSaveSize +
733 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000734 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000735 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000736 AFI->getDPRCalleeSavedAreaSize());
737
738 // Reset SP based on frame pointer only if the stack frame extends beyond
739 // frame pointer stack slot or target is ELF and the function has FP.
740 if (AFI->shouldRestoreSPFromFP()) {
741 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
742 if (NumBytes) {
743 if (isARM)
744 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
745 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000746 else {
747 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000748 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000749 // mov sp, r7
750 // sub sp, #24
751 // This is bad, if an interrupt is taken after the mov, sp is in an
752 // inconsistent state.
753 // Use the first callee-saved register as a scratch register.
Matthias Braun941a7052016-07-28 18:40:00 +0000754 assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000755 "No scratch register to restore SP from FP!");
756 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000757 ARMCC::AL, 0, TII);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000758 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000759 ARM::SP)
760 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000761 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000762 } else {
763 // Thumb2 or ARM.
764 if (isARM)
765 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
766 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
767 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000768 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000769 ARM::SP)
770 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000771 }
Tim Northoverdee86042013-12-02 14:46:26 +0000772 } else if (NumBytes &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000773 !tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
774 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000775
Eric Christopherb006fc92010-11-18 19:40:05 +0000776 // Increment past our save areas.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000777 if (MBBI != MBB.end() && AFI->getDPRCalleeSavedAreaSize()) {
Evan Cheng70d29632011-02-25 00:24:46 +0000778 MBBI++;
779 // Since vpop register list cannot have gaps, there may be multiple vpop
780 // instructions in the epilogue.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000781 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VLDMDIA_UPD)
Evan Cheng70d29632011-02-25 00:24:46 +0000782 MBBI++;
783 }
Tim Northover228c9432014-11-05 00:27:13 +0000784 if (AFI->getDPRCalleeSavedGapSize()) {
785 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
786 "unexpected DPR alignment gap");
787 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
788 }
789
Eric Christopherb006fc92010-11-18 19:40:05 +0000790 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
791 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000792 }
793
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000794 if (ArgRegsSaveSize)
795 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000796}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000797
Bob Wilson657f2272011-01-13 21:10:12 +0000798/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
799/// debug info. It's the same as what we use for resolving the code-gen
800/// references for now. FIXME: This can go wrong when references are
801/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000802int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000803ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000804 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000805 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
806}
807
808int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000809ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000810 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000811 int SPAdj) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000812 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000813 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000814 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000815 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000816 int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +0000817 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000818 bool isFixed = MFI.isFixedObjectIndex(FI);
Anton Korobeynikov46877782010-11-20 15:59:32 +0000819
820 FrameReg = ARM::SP;
821 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000822
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000823 // SP can move around if there are allocas. We may also lose track of SP
824 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000825 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000826
Anton Korobeynikov46877782010-11-20 15:59:32 +0000827 // When dynamically realigning the stack, use the frame pointer for
828 // parameters, and the stack/base pointer for locals.
829 if (RegInfo->needsStackRealignment(MF)) {
830 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
831 if (isFixed) {
832 FrameReg = RegInfo->getFrameRegister(MF);
833 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000834 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000835 assert(RegInfo->hasBasePointer(MF) &&
836 "VLAs and dynamic stack alignment, but missing base pointer!");
837 FrameReg = RegInfo->getBaseRegister();
838 }
839 return Offset;
840 }
841
842 // If there is a frame pointer, use it when we can.
843 if (hasFP(MF) && AFI->hasStackFrame()) {
844 // Use frame pointer to reference fixed objects. Use it for locals if
845 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000846 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000847 FrameReg = RegInfo->getFrameRegister(MF);
848 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000849 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000850 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000851 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000852 // Try to use the frame pointer if we can, else use the base pointer
853 // since it's available. This is handy for the emergency spill slot, in
854 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000855 if (FPOffset >= -255 && FPOffset < 0) {
856 FrameReg = RegInfo->getFrameRegister(MF);
857 return FPOffset;
858 }
Evan Chengc0d20042011-04-22 01:42:52 +0000859 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000860 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000861 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000862 // ldr <rd>, [sp, #<imm8>]
863 // if at all possible to save space.
864 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
865 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000866 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000867 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000868 if (FPOffset >= -255 && FPOffset < 0) {
869 FrameReg = RegInfo->getFrameRegister(MF);
870 return FPOffset;
871 }
872 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
873 // Otherwise, use SP or FP, whichever is closer to the stack slot.
874 FrameReg = RegInfo->getFrameRegister(MF);
875 return FPOffset;
876 }
877 }
878 // Use the base pointer if we have one.
879 if (RegInfo->hasBasePointer(MF))
880 FrameReg = RegInfo->getBaseRegister();
881 return Offset;
882}
883
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000884void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000885 MachineBasicBlock::iterator MI,
886 const std::vector<CalleeSavedInfo> &CSI,
887 unsigned StmOpc, unsigned StrOpc,
888 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000889 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000890 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000891 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000892 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000893 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
894
895 DebugLoc DL;
896
897 SmallVector<std::pair<unsigned,bool>, 4> Regs;
898 unsigned i = CSI.size();
Evan Cheng775ead32010-12-07 23:08:38 +0000899 while (i != 0) {
900 unsigned LastReg = 0;
901 for (; i != 0; --i) {
902 unsigned Reg = CSI[i-1].getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000903 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000904
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000905 // D-registers in the aligned area DPRCS2 are NOT spilled here.
906 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
907 continue;
908
Matthias Braun707e02c2016-04-13 21:43:25 +0000909 bool isLiveIn = MF.getRegInfo().isLiveIn(Reg);
910 if (!isLiveIn)
Evan Cheng775ead32010-12-07 23:08:38 +0000911 MBB.addLiveIn(Reg);
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000912 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000913 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000914 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000915 if (NoGap && LastReg && LastReg != Reg-1)
916 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000917 LastReg = Reg;
Matthias Braun707e02c2016-04-13 21:43:25 +0000918 // Do not set a kill flag on values that are also marked as live-in. This
919 // happens with the @llvm-returnaddress intrinsic and with arguments
920 // passed in callee saved registers.
921 // Omitting the kill flags is conservatively correct even if the live-in
922 // is not used after all.
923 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000924 }
925
Jim Grosbach5fccad82010-12-09 18:31:13 +0000926 if (Regs.empty())
927 continue;
928 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000929 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000930 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000931 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng775ead32010-12-07 23:08:38 +0000932 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
933 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +0000934 } else if (Regs.size() == 1) {
935 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
936 ARM::SP)
937 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000938 .addReg(ARM::SP).setMIFlags(MIFlags)
939 .addImm(-4);
Jim Grosbach5fccad82010-12-09 18:31:13 +0000940 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000941 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000942 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +0000943
944 // Put any subsequent vpush instructions before this one: they will refer to
945 // higher register numbers so need to be pushed first in order to preserve
946 // monotonicity.
Quentin Colombet71a71482015-07-20 21:42:14 +0000947 if (MI != MBB.begin())
948 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000949 }
Evan Cheng775ead32010-12-07 23:08:38 +0000950}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000951
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000952void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000953 MachineBasicBlock::iterator MI,
954 const std::vector<CalleeSavedInfo> &CSI,
955 unsigned LdmOpc, unsigned LdrOpc,
956 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000957 bool(*Func)(unsigned, bool),
958 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +0000959 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000960 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Evan Cheng775ead32010-12-07 23:08:38 +0000961 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet71a71482015-07-20 21:42:14 +0000962 DebugLoc DL;
963 bool isTailCall = false;
964 bool isInterrupt = false;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000965 bool isTrap = false;
Quentin Colombet71a71482015-07-20 21:42:14 +0000966 if (MBB.end() != MI) {
967 DL = MI->getDebugLoc();
968 unsigned RetOpcode = MI->getOpcode();
969 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
970 isInterrupt =
971 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000972 isTrap =
973 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
974 RetOpcode == ARM::tTRAP;
Quentin Colombet71a71482015-07-20 21:42:14 +0000975 }
Evan Cheng775ead32010-12-07 23:08:38 +0000976
977 SmallVector<unsigned, 4> Regs;
978 unsigned i = CSI.size();
979 while (i != 0) {
980 unsigned LastReg = 0;
981 bool DeleteRet = false;
982 for (; i != 0; --i) {
983 unsigned Reg = CSI[i-1].getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000984 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Evan Cheng775ead32010-12-07 23:08:38 +0000985
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000986 // The aligned reloads from area DPRCS2 are not inserted here.
987 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
988 continue;
989
Tim Northoverd8407452013-10-01 14:33:28 +0000990 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +0000991 !isTrap && STI.hasV5TOps()) {
Quentin Colombet71a71482015-07-20 21:42:14 +0000992 if (MBB.succ_empty()) {
993 Reg = ARM::PC;
994 DeleteRet = true;
995 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
996 } else
997 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Evan Cheng775ead32010-12-07 23:08:38 +0000998 // Fold the return instruction into the LDM.
Evan Cheng775ead32010-12-07 23:08:38 +0000999 }
1000
Evan Cheng9d54ae62010-12-08 06:29:02 +00001001 // If NoGap is true, pop consecutive registers and then leave the rest
1002 // for other instructions. e.g.
1003 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1004 if (NoGap && LastReg && LastReg != Reg-1)
1005 break;
1006
Evan Cheng775ead32010-12-07 23:08:38 +00001007 LastReg = Reg;
1008 Regs.push_back(Reg);
1009 }
1010
Jim Grosbach5fccad82010-12-09 18:31:13 +00001011 if (Regs.empty())
1012 continue;
1013 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng775ead32010-12-07 23:08:38 +00001014 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +00001015 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng775ead32010-12-07 23:08:38 +00001016 .addReg(ARM::SP));
1017 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1018 MIB.addReg(Regs[i], getDefRegState(true));
Quentin Colombet71a71482015-07-20 21:42:14 +00001019 if (DeleteRet && MI != MBB.end()) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001020 MIB.copyImplicitOps(*MI);
Evan Cheng775ead32010-12-07 23:08:38 +00001021 MI->eraseFromParent();
Andrew Trick6446bf72011-08-25 17:50:53 +00001022 }
Evan Cheng775ead32010-12-07 23:08:38 +00001023 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +00001024 } else if (Regs.size() == 1) {
1025 // If we adjusted the reg to PC from LR above, switch it back here. We
1026 // only do that for LDM.
1027 if (Regs[0] == ARM::PC)
1028 Regs[0] = ARM::LR;
1029 MachineInstrBuilder MIB =
1030 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1031 .addReg(ARM::SP, RegState::Define)
1032 .addReg(ARM::SP);
1033 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1034 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +00001035 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +00001036 MIB.addReg(0);
1037 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1038 } else
1039 MIB.addImm(4);
1040 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +00001041 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001042 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001043
1044 // Put any subsequent vpop instructions after this one: they will refer to
1045 // higher register numbers so need to be popped afterwards.
Quentin Colombet71a71482015-07-20 21:42:14 +00001046 if (MI != MBB.end())
1047 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +00001048 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001049}
1050
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001051/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +00001052/// starting from d8. Also insert stack realignment code and leave the stack
1053/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001054static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1055 MachineBasicBlock::iterator MI,
1056 unsigned NumAlignedDPRCS2Regs,
1057 const std::vector<CalleeSavedInfo> &CSI,
1058 const TargetRegisterInfo *TRI) {
1059 MachineFunction &MF = *MBB.getParent();
1060 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001061 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001062 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001063 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001064
1065 // Mark the D-register spill slots as properly aligned. Since MFI computes
1066 // stack slot layout backwards, this can actually mean that the d-reg stack
1067 // slot offsets can be wrong. The offset for d8 will always be correct.
1068 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1069 unsigned DNum = CSI[i].getReg() - ARM::D8;
Tim Northovere0ccdc62015-10-28 22:46:43 +00001070 if (DNum > NumAlignedDPRCS2Regs - 1)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001071 continue;
1072 int FI = CSI[i].getFrameIdx();
1073 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1074 // registers will be 8-byte aligned.
1075 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1076
1077 // The stack slot for D8 needs to be maximally aligned because this is
1078 // actually the point where we align the stack pointer. MachineFrameInfo
1079 // computes all offsets relative to the incoming stack pointer which is a
1080 // bit weird when realigning the stack. Any extra padding for this
1081 // over-alignment is not realized because the code inserted below adjusts
1082 // the stack pointer by numregs * 8 before aligning the stack pointer.
1083 if (DNum == 0)
1084 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1085 }
1086
1087 // Move the stack pointer to the d8 spill slot, and align it at the same
1088 // time. Leave the stack slot address in the scratch register r4.
1089 //
1090 // sub r4, sp, #numregs * 8
1091 // bic r4, r4, #align - 1
1092 // mov sp, r4
1093 //
1094 bool isThumb = AFI->isThumbFunction();
1095 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1096 AFI->setShouldRestoreSPFromFP(true);
1097
1098 // sub r4, sp, #numregs * 8
1099 // The immediate is <= 64, so it doesn't need any special encoding.
1100 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
1101 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
Kristof Beyls933de7a2015-01-08 15:09:14 +00001102 .addReg(ARM::SP)
1103 .addImm(8 * NumAlignedDPRCS2Regs)));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001104
Matthias Braun941a7052016-07-28 18:40:00 +00001105 unsigned MaxAlign = MF.getFrameInfo().getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +00001106 // We must set parameter MustBeSingleInstruction to true, since
1107 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1108 // stack alignment. Luckily, this can always be done since all ARM
1109 // architecture versions that support Neon also support the BFC
1110 // instruction.
1111 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001112
1113 // mov sp, r4
1114 // The stack pointer must be adjusted before spilling anything, otherwise
1115 // the stack slots could be clobbered by an interrupt handler.
1116 // Leave r4 live, it is used below.
1117 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1118 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
1119 .addReg(ARM::R4);
1120 MIB = AddDefaultPred(MIB);
1121 if (!isThumb)
1122 AddDefaultCC(MIB);
1123
1124 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1125 // r4 holds the stack slot address.
1126 unsigned NextReg = ARM::D8;
1127
1128 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1129 // The writeback is only needed when emitting two vst1.64 instructions.
1130 if (NumAlignedDPRCS2Regs >= 6) {
1131 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001132 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001133 MBB.addLiveIn(SupReg);
1134 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
1135 ARM::R4)
1136 .addReg(ARM::R4, RegState::Kill).addImm(16)
1137 .addReg(NextReg)
1138 .addReg(SupReg, RegState::ImplicitKill));
1139 NextReg += 4;
1140 NumAlignedDPRCS2Regs -= 4;
1141 }
1142
1143 // We won't modify r4 beyond this point. It currently points to the next
1144 // register to be spilled.
1145 unsigned R4BaseReg = NextReg;
1146
1147 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1148 if (NumAlignedDPRCS2Regs >= 4) {
1149 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001150 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001151 MBB.addLiveIn(SupReg);
1152 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1153 .addReg(ARM::R4).addImm(16).addReg(NextReg)
1154 .addReg(SupReg, RegState::ImplicitKill));
1155 NextReg += 4;
1156 NumAlignedDPRCS2Regs -= 4;
1157 }
1158
1159 // 16-byte aligned vst1.64 with 2 d-regs.
1160 if (NumAlignedDPRCS2Regs >= 2) {
1161 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001162 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001163 MBB.addLiveIn(SupReg);
1164 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001165 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001166 NextReg += 2;
1167 NumAlignedDPRCS2Regs -= 2;
1168 }
1169
1170 // Finally, use a vanilla vstr.64 for the odd last register.
1171 if (NumAlignedDPRCS2Regs) {
1172 MBB.addLiveIn(NextReg);
1173 // vstr.64 uses addrmode5 which has an offset scale of 4.
1174 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1175 .addReg(NextReg)
1176 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
1177 }
1178
1179 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001180 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001181}
1182
1183/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1184/// iterator to the following instruction.
1185static MachineBasicBlock::iterator
1186skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1187 unsigned NumAlignedDPRCS2Regs) {
1188 // sub r4, sp, #numregs * 8
1189 // bic r4, r4, #align - 1
1190 // mov sp, r4
1191 ++MI; ++MI; ++MI;
1192 assert(MI->mayStore() && "Expecting spill instruction");
1193
1194 // These switches all fall through.
1195 switch(NumAlignedDPRCS2Regs) {
1196 case 7:
1197 ++MI;
1198 assert(MI->mayStore() && "Expecting spill instruction");
1199 default:
1200 ++MI;
1201 assert(MI->mayStore() && "Expecting spill instruction");
1202 case 1:
1203 case 2:
1204 case 4:
1205 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1206 ++MI;
1207 }
1208 return MI;
1209}
1210
1211/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1212/// starting from d8. These instructions are assumed to execute while the
1213/// stack is still aligned, unlike the code inserted by emitPopInst.
1214static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1215 MachineBasicBlock::iterator MI,
1216 unsigned NumAlignedDPRCS2Regs,
1217 const std::vector<CalleeSavedInfo> &CSI,
1218 const TargetRegisterInfo *TRI) {
1219 MachineFunction &MF = *MBB.getParent();
1220 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001221 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001222 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001223
1224 // Find the frame index assigned to d8.
1225 int D8SpillFI = 0;
1226 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1227 if (CSI[i].getReg() == ARM::D8) {
1228 D8SpillFI = CSI[i].getFrameIdx();
1229 break;
1230 }
1231
1232 // Materialize the address of the d8 spill slot into the scratch register r4.
1233 // This can be fairly complicated if the stack frame is large, so just use
1234 // the normal frame index elimination mechanism to do it. This code runs as
1235 // the initial part of the epilog where the stack and base pointers haven't
1236 // been changed yet.
1237 bool isThumb = AFI->isThumbFunction();
1238 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1239
1240 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1241 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1242 .addFrameIndex(D8SpillFI).addImm(0)));
1243
1244 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1245 unsigned NextReg = ARM::D8;
1246
1247 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1248 if (NumAlignedDPRCS2Regs >= 6) {
1249 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001250 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001251 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1252 .addReg(ARM::R4, RegState::Define)
1253 .addReg(ARM::R4, RegState::Kill).addImm(16)
1254 .addReg(SupReg, RegState::ImplicitDefine));
1255 NextReg += 4;
1256 NumAlignedDPRCS2Regs -= 4;
1257 }
1258
1259 // We won't modify r4 beyond this point. It currently points to the next
1260 // register to be spilled.
1261 unsigned R4BaseReg = NextReg;
1262
1263 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1264 if (NumAlignedDPRCS2Regs >= 4) {
1265 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001266 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001267 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1268 .addReg(ARM::R4).addImm(16)
1269 .addReg(SupReg, RegState::ImplicitDefine));
1270 NextReg += 4;
1271 NumAlignedDPRCS2Regs -= 4;
1272 }
1273
1274 // 16-byte aligned vld1.64 with 2 d-regs.
1275 if (NumAlignedDPRCS2Regs >= 2) {
1276 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001277 &ARM::QPRRegClass);
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001278 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1279 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001280 NextReg += 2;
1281 NumAlignedDPRCS2Regs -= 2;
1282 }
1283
1284 // Finally, use a vanilla vldr.64 for the remaining odd register.
1285 if (NumAlignedDPRCS2Regs)
1286 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1287 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
1288
1289 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001290 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001291}
1292
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001293bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001294 MachineBasicBlock::iterator MI,
1295 const std::vector<CalleeSavedInfo> &CSI,
1296 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001297 if (CSI.empty())
1298 return false;
1299
1300 MachineFunction &MF = *MBB.getParent();
1301 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001302
1303 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001304 unsigned PushOneOpc = AFI->isThumbFunction() ?
1305 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001306 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001307 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1308 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001309 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001310 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001311 MachineInstr::FrameSetup);
1312 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001313 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1314
1315 // The code above does not insert spill code for the aligned DPRCS2 registers.
1316 // The stack realignment code will be inserted between the push instructions
1317 // and these spills.
1318 if (NumAlignedDPRCS2Regs)
1319 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001320
1321 return true;
1322}
1323
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001324bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001325 MachineBasicBlock::iterator MI,
1326 const std::vector<CalleeSavedInfo> &CSI,
1327 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001328 if (CSI.empty())
1329 return false;
1330
1331 MachineFunction &MF = *MBB.getParent();
1332 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001333 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001334 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1335
1336 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1337 // registers. Do that here instead.
1338 if (NumAlignedDPRCS2Regs)
1339 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001340
1341 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001342 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001343 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001344 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1345 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001346 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001347 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001348 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001349 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001350
1351 return true;
1352}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001353
1354// FIXME: Make generic?
1355static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1356 const ARMBaseInstrInfo &TII) {
1357 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001358 for (auto &MBB : MF) {
1359 for (auto &MI : MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001360 FnSize += TII.getInstSizeInBytes(MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001361 }
1362 return FnSize;
1363}
1364
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001365/// estimateRSStackSizeLimit - Look at each instruction that references stack
1366/// frames and return the stack size limit beyond which some of these
1367/// instructions will require a scratch register during their expansion later.
1368// FIXME: Move to TII?
1369static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001370 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001371 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1372 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001373 for (auto &MBB : MF) {
1374 for (auto &MI : MBB) {
1375 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1376 if (!MI.getOperand(i).isFI())
1377 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001378
1379 // When using ADDri to get the address of a stack object, 255 is the
1380 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001381 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001382 Limit = std::min(Limit, (1U << 8) - 1);
1383 break;
1384 }
1385
1386 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001387 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001388 case ARMII::AddrMode3:
1389 case ARMII::AddrModeT2_i8:
1390 Limit = std::min(Limit, (1U << 8) - 1);
1391 break;
1392 case ARMII::AddrMode5:
1393 case ARMII::AddrModeT2_i8s4:
1394 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1395 break;
1396 case ARMII::AddrModeT2_i12:
1397 // i12 supports only positive offset so these will be converted to
1398 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1399 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1400 Limit = std::min(Limit, (1U << 8) - 1);
1401 break;
1402 case ARMII::AddrMode4:
1403 case ARMII::AddrMode6:
1404 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1405 // immediate offset for stack references.
1406 return 0;
1407 default:
1408 break;
1409 }
1410 break; // At most one FI per instruction
1411 }
1412 }
1413 }
1414
1415 return Limit;
1416}
1417
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001418// In functions that realign the stack, it can be an advantage to spill the
1419// callee-saved vector registers after realigning the stack. The vst1 and vld1
1420// instructions take alignment hints that can improve performance.
1421//
Matthias Braun02564862015-07-14 17:17:13 +00001422static void
1423checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001424 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1425 if (!SpillAlignedNEONRegs)
1426 return;
1427
1428 // Naked functions don't spill callee-saved registers.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001429 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001430 return;
1431
1432 // We are planning to use NEON instructions vst1 / vld1.
Eric Christopher1b21f002015-01-29 00:19:33 +00001433 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001434 return;
1435
1436 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopher1b21f002015-01-29 00:19:33 +00001437 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001438 return;
1439
1440 // Aligned spills require stack realignment.
Eric Christopher1b21f002015-01-29 00:19:33 +00001441 if (!static_cast<const ARMBaseRegisterInfo *>(
1442 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001443 return;
1444
1445 // We always spill contiguous d-registers starting from d8. Count how many
1446 // needs spilling. The register allocator will almost always use the
1447 // callee-saved registers in order, but it can happen that there are holes in
1448 // the range. Registers above the hole will be spilled to the standard DPRCS
1449 // area.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001450 unsigned NumSpills = 0;
1451 for (; NumSpills < 8; ++NumSpills)
Matthias Braun02564862015-07-14 17:17:13 +00001452 if (!SavedRegs.test(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001453 break;
1454
1455 // Don't do this for just one d-register. It's not worth it.
1456 if (NumSpills < 2)
1457 return;
1458
1459 // Spill the first NumSpills D-registers after realigning the stack.
1460 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1461
1462 // A scratch register is required for the vst1 / vld1 instructions.
Matthias Braun02564862015-07-14 17:17:13 +00001463 SavedRegs.set(ARM::R4);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001464}
1465
Matthias Braun02564862015-07-14 17:17:13 +00001466void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1467 BitVector &SavedRegs,
1468 RegScavenger *RS) const {
1469 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001470 // This tells PEI to spill the FP as if it is any other callee-save register
1471 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1472 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1473 // to combine multiple loads / stores.
1474 bool CanEliminateFrame = true;
1475 bool CS1Spilled = false;
1476 bool LRSpilled = false;
1477 unsigned NumGPRSpills = 0;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001478 unsigned NumFPRSpills = 0;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001479 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1480 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001481 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001482 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001483 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001484 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001485 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +00001486 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001487 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001488 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1489
1490 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1491 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001492 // since it's not always possible to restore sp from fp in a single
1493 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001494 // FIXME: It will be better just to find spare register here.
1495 if (AFI->isThumb2Function() &&
Matthias Braun941a7052016-07-28 18:40:00 +00001496 (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Matthias Braun02564862015-07-14 17:17:13 +00001497 SavedRegs.set(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001498
Evan Cheng572756a2011-01-16 05:14:33 +00001499 if (AFI->isThumb1OnlyFunction()) {
1500 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001501 if (AFI->getArgRegsSaveSize() > 0)
Matthias Braun02564862015-07-14 17:17:13 +00001502 SavedRegs.set(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001503
Jim Grosbachdca85312011-06-13 21:18:25 +00001504 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1505 // for sure what the stack size will be, but for this, an estimate is good
1506 // enough. If there anything changes it, it'll be a spill, which implies
1507 // we've used all the registers and so R4 is already used, so not marking
Chad Rosieradd38c12011-10-20 00:07:12 +00001508 // it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001509 // FIXME: It will be better just to find spare register here.
Matthias Braun941a7052016-07-28 18:40:00 +00001510 unsigned StackSize = MFI.estimateStackSize(MF);
1511 if (MFI.hasVarSizedObjects() || StackSize > 508)
Matthias Braun02564862015-07-14 17:17:13 +00001512 SavedRegs.set(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001513 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001514
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001515 // See if we can spill vector registers to aligned stack.
Matthias Braun02564862015-07-14 17:17:13 +00001516 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001517
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001518 // Spill the BasePtr if it's used.
1519 if (RegInfo->hasBasePointer(MF))
Matthias Braun02564862015-07-14 17:17:13 +00001520 SavedRegs.set(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001521
1522 // Don't spill FP if the frame can be eliminated. This is determined
Matthias Braun02564862015-07-14 17:17:13 +00001523 // by scanning the callee-save registers to see if any is modified.
Craig Topper840beec2014-04-04 05:16:06 +00001524 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001525 for (unsigned i = 0; CSRegs[i]; ++i) {
1526 unsigned Reg = CSRegs[i];
1527 bool Spilled = false;
Matthias Braun02564862015-07-14 17:17:13 +00001528 if (SavedRegs.test(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001529 Spilled = true;
1530 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001531 }
1532
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001533 if (!ARM::GPRRegClass.contains(Reg)) {
1534 if (Spilled) {
1535 if (ARM::SPRRegClass.contains(Reg))
1536 NumFPRSpills++;
1537 else if (ARM::DPRRegClass.contains(Reg))
1538 NumFPRSpills += 2;
1539 else if (ARM::QPRRegClass.contains(Reg))
1540 NumFPRSpills += 4;
1541 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001542 continue;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001543 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001544
1545 if (Spilled) {
1546 NumGPRSpills++;
1547
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001548 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001549 if (Reg == ARM::LR)
1550 LRSpilled = true;
1551 CS1Spilled = true;
1552 continue;
1553 }
1554
1555 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1556 switch (Reg) {
1557 case ARM::LR:
1558 LRSpilled = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00001559 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +00001560 case ARM::R0: case ARM::R1:
1561 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001562 case ARM::R4: case ARM::R5:
1563 case ARM::R6: case ARM::R7:
1564 CS1Spilled = true;
1565 break;
1566 default:
1567 break;
1568 }
1569 } else {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001570 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001571 UnspilledCS1GPRs.push_back(Reg);
1572 continue;
1573 }
1574
1575 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001576 case ARM::R0: case ARM::R1:
1577 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001578 case ARM::R4: case ARM::R5:
1579 case ARM::R6: case ARM::R7:
1580 case ARM::LR:
1581 UnspilledCS1GPRs.push_back(Reg);
1582 break;
1583 default:
1584 UnspilledCS2GPRs.push_back(Reg);
1585 break;
1586 }
1587 }
1588 }
1589
1590 bool ForceLRSpill = false;
1591 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1592 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1593 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1594 // use of BL to implement far jump. If it turns out that it's not needed
1595 // then the branch fix up path will undo it.
1596 if (FnSize >= (1 << 11)) {
1597 CanEliminateFrame = false;
1598 ForceLRSpill = true;
1599 }
1600 }
1601
1602 // If any of the stack slot references may be out of range of an immediate
1603 // offset, make sure a register (or a spill slot) is available for the
1604 // register scavenger. Note that if we're indexing off the frame pointer, the
1605 // effective stack size is 4 bytes larger since the FP points to the stack
1606 // slot of the previous FP. Also, if we have variable sized objects in the
1607 // function, stack slot references will often be negative, and some of
1608 // our instructions are positive-offset only, so conservatively consider
1609 // that case to want a spill slot (or register) as well. Similarly, if
1610 // the function adjusts the stack pointer during execution and the
1611 // adjustments aren't already part of our stack size estimate, our offset
1612 // calculations may be off, so be conservative.
1613 // FIXME: We could add logic to be more precise about negative offsets
1614 // and which instructions will need a scratch register for them. Is it
1615 // worth the effort and added fragility?
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001616 unsigned EstimatedStackSize =
Matthias Braun941a7052016-07-28 18:40:00 +00001617 MFI.estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001618 if (hasFP(MF)) {
1619 if (AFI->hasStackFrame())
1620 EstimatedStackSize += 4;
1621 } else {
1622 // If FP is not used, SP will be used to access arguments, so count the
1623 // size of arguments into the estimation.
1624 EstimatedStackSize += MF.getInfo<ARMFunctionInfo>()->getArgumentStackSize();
1625 }
1626 EstimatedStackSize += 16; // For possible paddings.
1627
1628 bool BigStack = EstimatedStackSize >= estimateRSStackSizeLimit(MF, this) ||
Matthias Braun941a7052016-07-28 18:40:00 +00001629 MFI.hasVarSizedObjects() ||
1630 (MFI.adjustsStack() && !canSimplifyCallFramePseudos(MF));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001631 bool ExtraCSSpill = false;
1632 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1633 AFI->setHasStackFrame(true);
1634
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001635 if (hasFP(MF)) {
1636 SavedRegs.set(FramePtr);
1637 // If the frame pointer is required by the ABI, also spill LR so that we
1638 // emit a complete frame record.
1639 if (MF.getTarget().Options.DisableFramePointerElim(MF) && !LRSpilled) {
1640 SavedRegs.set(ARM::LR);
1641 LRSpilled = true;
1642 NumGPRSpills++;
1643 }
1644 auto FPPos = find(UnspilledCS1GPRs, FramePtr);
1645 if (FPPos != UnspilledCS1GPRs.end())
1646 UnspilledCS1GPRs.erase(FPPos);
1647 NumGPRSpills++;
1648 if (FramePtr == ARM::R7)
1649 CS1Spilled = true;
1650 }
1651
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001652 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1653 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1654 if (!LRSpilled && CS1Spilled) {
Matthias Braun02564862015-07-14 17:17:13 +00001655 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001656 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001657 SmallVectorImpl<unsigned>::iterator LRPos;
David Majnemer0d955d02016-08-11 22:21:41 +00001658 LRPos = find(UnspilledCS1GPRs, (unsigned)ARM::LR);
Tim Northoverd8407452013-10-01 14:33:28 +00001659 if (LRPos != UnspilledCS1GPRs.end())
1660 UnspilledCS1GPRs.erase(LRPos);
1661
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001662 ForceLRSpill = false;
1663 ExtraCSSpill = true;
1664 }
1665
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001666 // If stack and double are 8-byte aligned and we are spilling an odd number
1667 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1668 // the integer and double callee save areas.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001669 unsigned TargetAlign = getStackAlignment();
Tim Northoverdc0d9e42014-11-05 00:27:20 +00001670 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001671 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1672 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1673 unsigned Reg = UnspilledCS1GPRs[i];
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001674 // Don't spill high register if the function is thumb. In the case of
1675 // Windows on ARM, accept R11 (frame pointer)
Peter Collingbourne78f1ecc2015-04-23 20:31:26 +00001676 if (!AFI->isThumbFunction() ||
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001677 (STI.isTargetWindows() && Reg == ARM::R11) ||
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001678 isARMLowRegister(Reg) || Reg == ARM::LR) {
Matthias Braun02564862015-07-14 17:17:13 +00001679 SavedRegs.set(Reg);
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001680 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001681 ExtraCSSpill = true;
1682 break;
1683 }
1684 }
1685 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1686 unsigned Reg = UnspilledCS2GPRs.front();
Matthias Braun02564862015-07-14 17:17:13 +00001687 SavedRegs.set(Reg);
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001688 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001689 ExtraCSSpill = true;
1690 }
1691 }
1692
1693 // Estimate if we might need to scavenge a register at some point in order
1694 // to materialize a stack offset. If so, either spill one additional
1695 // callee-saved register or reserve a special spill slot to facilitate
1696 // register scavenging. Thumb1 needs a spill slot for stack pointer
1697 // adjustments also, even when the frame itself is small.
1698 if (BigStack && !ExtraCSSpill) {
1699 // If any non-reserved CS register isn't spilled, just spill one or two
1700 // extra. That should take care of it!
1701 unsigned NumExtras = TargetAlign / 4;
1702 SmallVector<unsigned, 2> Extras;
1703 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1704 unsigned Reg = UnspilledCS1GPRs.back();
1705 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001706 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001707 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1708 Reg == ARM::LR)) {
1709 Extras.push_back(Reg);
1710 NumExtras--;
1711 }
1712 }
1713 // For non-Thumb1 functions, also check for hi-reg CS registers
1714 if (!AFI->isThumb1OnlyFunction()) {
1715 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1716 unsigned Reg = UnspilledCS2GPRs.back();
1717 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001718 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001719 Extras.push_back(Reg);
1720 NumExtras--;
1721 }
1722 }
1723 }
1724 if (Extras.size() && NumExtras == 0) {
1725 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Matthias Braun02564862015-07-14 17:17:13 +00001726 SavedRegs.set(Extras[i]);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001727 }
1728 } else if (!AFI->isThumb1OnlyFunction()) {
1729 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1730 // closest to SP or frame pointer.
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001731 assert(RS && "Register scavenging not provided");
Craig Topperc7242e02012-04-20 07:30:17 +00001732 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Matthias Braun941a7052016-07-28 18:40:00 +00001733 RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1734 RC->getAlignment(),
1735 false));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001736 }
1737 }
1738 }
1739
1740 if (ForceLRSpill) {
Matthias Braun02564862015-07-14 17:17:13 +00001741 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001742 AFI->setLRIsSpilledForFarJump(true);
1743 }
1744}
Eli Bendersky8da87162013-02-21 20:05:00 +00001745
Hans Wennborge1a2e902016-03-31 18:33:38 +00001746MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
1747 MachineFunction &MF, MachineBasicBlock &MBB,
1748 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001749 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001750 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001751 if (!hasReservedCallFrame(MF)) {
1752 // If we have alloca, convert as follows:
1753 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1754 // ADJCALLSTACKUP -> add, sp, sp, amount
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001755 MachineInstr &Old = *I;
1756 DebugLoc dl = Old.getDebugLoc();
1757 unsigned Amount = Old.getOperand(0).getImm();
Eli Bendersky8da87162013-02-21 20:05:00 +00001758 if (Amount != 0) {
1759 // We need to keep the stack aligned properly. To do this, we round the
1760 // amount of space needed for the outgoing arguments up to the next
1761 // alignment boundary.
Guozhi Weif66d3842015-08-17 22:36:27 +00001762 Amount = alignSPAdjust(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +00001763
1764 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1765 assert(!AFI->isThumb1OnlyFunction() &&
1766 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1767 bool isARM = !AFI->isThumbFunction();
1768
1769 // Replace the pseudo instruction with a new instruction...
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001770 unsigned Opc = Old.getOpcode();
1771 int PIdx = Old.findFirstPredOperandIdx();
1772 ARMCC::CondCodes Pred =
1773 (PIdx == -1) ? ARMCC::AL
1774 : (ARMCC::CondCodes)Old.getOperand(PIdx).getImm();
Eli Bendersky8da87162013-02-21 20:05:00 +00001775 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1776 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001777 unsigned PredReg = Old.getOperand(2).getReg();
Eli Bendersky8da87162013-02-21 20:05:00 +00001778 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1779 Pred, PredReg);
1780 } else {
1781 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001782 unsigned PredReg = Old.getOperand(3).getReg();
Eli Bendersky8da87162013-02-21 20:05:00 +00001783 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1784 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1785 Pred, PredReg);
1786 }
1787 }
1788 }
Hans Wennborge1a2e902016-03-31 18:33:38 +00001789 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00001790}
1791
Oliver Stannardb14c6252014-04-02 16:10:33 +00001792/// Get the minimum constant for ARM that is greater than or equal to the
1793/// argument. In ARM, constants can have any value that can be produced by
1794/// rotating an 8-bit value to the right by an even number of bits within a
1795/// 32-bit word.
1796static uint32_t alignToARMConstant(uint32_t Value) {
1797 unsigned Shifted = 0;
1798
1799 if (Value == 0)
1800 return 0;
1801
1802 while (!(Value & 0xC0000000)) {
1803 Value = Value << 2;
1804 Shifted += 2;
1805 }
1806
1807 bool Carry = (Value & 0x00FFFFFF);
1808 Value = ((Value & 0xFF000000) >> 24) + Carry;
1809
1810 if (Value & 0x0000100)
1811 Value = Value & 0x000001FC;
1812
1813 if (Shifted > 24)
1814 Value = Value >> (Shifted - 24);
1815 else
1816 Value = Value << (24 - Shifted);
1817
1818 return Value;
1819}
1820
1821// The stack limit in the TCB is set to this many bytes above the actual
1822// stack limit.
1823static const uint64_t kSplitStackAvailable = 256;
1824
1825// Adjust the function prologue to enable split stacks. This currently only
1826// supports android and linux.
1827//
1828// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
1829// must be well defined in order to allow for consistent implementations of the
1830// __morestack helper function. The ABI is also not a normal ABI in that it
1831// doesn't follow the normal calling conventions because this allows the
1832// prologue of each function to be optimized further.
1833//
1834// Currently, the ABI looks like (when calling __morestack)
1835//
1836// * r4 holds the minimum stack size requested for this function call
1837// * r5 holds the stack size of the arguments to the function
1838// * the beginning of the function is 3 instructions after the call to
1839// __morestack
1840//
1841// Implementations of __morestack should use r4 to allocate a new stack, r5 to
1842// place the arguments on to the new stack, and the 3-instruction knowledge to
1843// jump directly to the body of the function when working on the new stack.
1844//
1845// An old (and possibly no longer compatible) implementation of __morestack for
1846// ARM can be found at [1].
1847//
1848// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
Quentin Colombet61b305e2015-05-05 17:38:16 +00001849void ARMFrameLowering::adjustForSegmentedStacks(
1850 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
Oliver Stannardb14c6252014-04-02 16:10:33 +00001851 unsigned Opcode;
1852 unsigned CFIIndex;
Eric Christopher22b2ad22015-02-20 08:24:37 +00001853 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
Oliver Stannardb14c6252014-04-02 16:10:33 +00001854 bool Thumb = ST->isThumb();
1855
1856 // Sadly, this currently doesn't support varargs, platforms other than
1857 // android/linux. Note that thumb1/thumb2 are support for android/linux.
1858 if (MF.getFunction()->isVarArg())
1859 report_fatal_error("Segmented stacks do not support vararg functions.");
1860 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00001861 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00001862
Matthias Braun941a7052016-07-28 18:40:00 +00001863 MachineFrameInfo &MFI = MF.getFrameInfo();
Oliver Stannardb14c6252014-04-02 16:10:33 +00001864 MachineModuleInfo &MMI = MF.getMMI();
1865 MCContext &Context = MMI.getContext();
1866 const MCRegisterInfo *MRI = Context.getRegisterInfo();
1867 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001868 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00001869 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
1870 DebugLoc DL;
1871
Matthias Braun941a7052016-07-28 18:40:00 +00001872 uint64_t StackSize = MFI.getStackSize();
Tim Northoverf9e798b2014-05-22 13:03:43 +00001873
1874 // Do not generate a prologue for functions with a stack of size zero
1875 if (StackSize == 0)
1876 return;
1877
Oliver Stannardb14c6252014-04-02 16:10:33 +00001878 // Use R4 and R5 as scratch registers.
1879 // We save R4 and R5 before use and restore them before leaving the function.
1880 unsigned ScratchReg0 = ARM::R4;
1881 unsigned ScratchReg1 = ARM::R5;
1882 uint64_t AlignedStackSize;
1883
1884 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
1885 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
1886 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
1887 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
1888 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
1889
Quentin Colombet71a71482015-07-20 21:42:14 +00001890 // Grab everything that reaches PrologueMBB to update there liveness as well.
1891 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
1892 SmallVector<MachineBasicBlock *, 2> WalkList;
1893 WalkList.push_back(&PrologueMBB);
1894
1895 do {
1896 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
1897 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
1898 if (BeforePrologueRegion.insert(PredBB).second)
1899 WalkList.push_back(PredBB);
1900 }
1901 } while (!WalkList.empty());
1902
1903 // The order in that list is important.
1904 // The blocks will all be inserted before PrologueMBB using that order.
1905 // Therefore the block that should appear first in the CFG should appear
1906 // first in the list.
1907 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
1908 PostStackMBB};
Quentin Colombet71a71482015-07-20 21:42:14 +00001909
Craig Topper80720812015-12-01 06:13:01 +00001910 for (MachineBasicBlock *B : AddedBlocks)
1911 BeforePrologueRegion.insert(B);
Quentin Colombet71a71482015-07-20 21:42:14 +00001912
Matthias Braund9da1622015-09-09 18:08:03 +00001913 for (const auto &LI : PrologueMBB.liveins()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00001914 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
Matthias Braunb2b7ef12015-08-24 22:59:52 +00001915 PredBB->addLiveIn(LI);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001916 }
1917
Quentin Colombet71a71482015-07-20 21:42:14 +00001918 // Remove the newly added blocks from the list, since we know
1919 // we do not have to do the following updates for them.
Craig Topper80720812015-12-01 06:13:01 +00001920 for (MachineBasicBlock *B : AddedBlocks) {
1921 BeforePrologueRegion.erase(B);
1922 MF.insert(PrologueMBB.getIterator(), B);
Quentin Colombet71a71482015-07-20 21:42:14 +00001923 }
1924
1925 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
1926 // Make sure the LiveIns are still sorted and unique.
1927 MBB->sortUniqueLiveIns();
1928 // Replace the edges to PrologueMBB by edges to the sequences
1929 // we are about to add.
1930 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
1931 }
Oliver Stannardb14c6252014-04-02 16:10:33 +00001932
1933 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00001934 AlignedStackSize = alignToARMConstant(StackSize);
1935
1936 // When the frame size is less than 256 we just compare the stack
1937 // boundary directly to the value of the stack pointer, per gcc.
1938 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
1939
1940 // We will use two of the callee save registers as scratch registers so we
1941 // need to save those registers onto the stack.
1942 // We will use SR0 to hold stack limit and SR1 to hold the stack size
1943 // requested and arguments for __morestack().
1944 // SR0: Scratch Register #0
1945 // SR1: Scratch Register #1
1946 // push {SR0, SR1}
1947 if (Thumb) {
1948 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH)))
1949 .addReg(ScratchReg0).addReg(ScratchReg1);
1950 } else {
1951 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
1952 .addReg(ARM::SP, RegState::Define).addReg(ARM::SP))
1953 .addReg(ScratchReg0).addReg(ScratchReg1);
1954 }
1955
1956 // Emit the relevant DWARF information about the change in stack pointer as
1957 // well as where to find both r4 and r5 (the callee-save registers)
1958 CFIIndex =
1959 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
1960 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1961 .addCFIIndex(CFIIndex);
1962 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1963 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
1964 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1965 .addCFIIndex(CFIIndex);
1966 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1967 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
1968 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1969 .addCFIIndex(CFIIndex);
1970
1971 // mov SR1, sp
1972 if (Thumb) {
1973 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
1974 .addReg(ARM::SP));
1975 } else if (CompareStackPointer) {
1976 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
1977 .addReg(ARM::SP)).addReg(0);
1978 }
1979
1980 // sub SR1, sp, #StackSize
1981 if (!CompareStackPointer && Thumb) {
1982 AddDefaultPred(
1983 AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1))
1984 .addReg(ScratchReg1).addImm(AlignedStackSize));
1985 } else if (!CompareStackPointer) {
1986 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
1987 .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0);
1988 }
1989
1990 if (Thumb && ST->isThumb1Only()) {
1991 unsigned PCLabelId = ARMFI->createPICLabelUId();
1992 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Oliver Stannard92e0fc02014-04-03 08:45:16 +00001993 MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001994 MachineConstantPool *MCP = MF.getConstantPool();
Tim Northover956b0082015-10-02 18:07:13 +00001995 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001996
1997 // ldr SR0, [pc, offset(STACK_LIMIT)]
1998 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
1999 .addConstantPoolIndex(CPI));
2000
2001 // ldr SR0, [SR0]
2002 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
2003 .addReg(ScratchReg0).addImm(0));
2004 } else {
2005 // Get TLS base address from the coprocessor
2006 // mrc p15, #0, SR0, c13, c0, #3
2007 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2008 .addImm(15)
2009 .addImm(0)
2010 .addImm(13)
2011 .addImm(0)
2012 .addImm(3));
2013
2014 // Use the last tls slot on android and a private field of the TCP on linux.
2015 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2016 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2017
2018 // Get the stack limit from the right offset
2019 // ldr SR0, [sr0, #4 * TlsOffset]
2020 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2021 .addReg(ScratchReg0).addImm(4 * TlsOffset));
2022 }
2023
2024 // Compare stack limit with stack size requested.
2025 // cmp SR0, SR1
2026 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
2027 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode))
2028 .addReg(ScratchReg0)
2029 .addReg(ScratchReg1));
2030
2031 // This jump is taken if StackLimit < SP - stack required.
2032 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2033 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2034 .addImm(ARMCC::LO)
2035 .addReg(ARM::CPSR);
2036
2037
2038 // Calling __morestack(StackSize, Size of stack arguments).
2039 // __morestack knows that the stack size requested is in SR0(r4)
2040 // and amount size of stack arguments is in SR1(r5).
2041
2042 // Pass first argument for the __morestack by Scratch Register #0.
2043 // The amount size of stack required
2044 if (Thumb) {
2045 AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8),
2046 ScratchReg0)).addImm(AlignedStackSize));
2047 } else {
2048 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2049 .addImm(AlignedStackSize)).addReg(0);
2050 }
2051 // Pass second argument for the __morestack by Scratch Register #1.
2052 // The amount size of stack consumed to save function arguments.
2053 if (Thumb) {
2054 AddDefaultPred(
2055 AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1))
2056 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())));
2057 } else {
2058 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2059 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())))
2060 .addReg(0);
2061 }
2062
2063 // push {lr} - Save return address of this function.
2064 if (Thumb) {
2065 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH)))
2066 .addReg(ARM::LR);
2067 } else {
2068 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2069 .addReg(ARM::SP, RegState::Define)
2070 .addReg(ARM::SP))
2071 .addReg(ARM::LR);
2072 }
2073
2074 // Emit the DWARF info about the change in stack as well as where to find the
2075 // previous link register
2076 CFIIndex =
2077 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
2078 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2079 .addCFIIndex(CFIIndex);
2080 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
2081 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2082 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2083 .addCFIIndex(CFIIndex);
2084
2085 // Call __morestack().
2086 if (Thumb) {
2087 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL)))
2088 .addExternalSymbol("__morestack");
2089 } else {
2090 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2091 .addExternalSymbol("__morestack");
2092 }
2093
2094 // pop {lr} - Restore return address of this original function.
2095 if (Thumb) {
2096 if (ST->isThumb1Only()) {
2097 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2098 .addReg(ScratchReg0);
2099 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2100 .addReg(ScratchReg0));
2101 } else {
2102 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2103 .addReg(ARM::LR, RegState::Define)
2104 .addReg(ARM::SP, RegState::Define)
2105 .addReg(ARM::SP)
2106 .addImm(4));
2107 }
2108 } else {
2109 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2110 .addReg(ARM::SP, RegState::Define)
2111 .addReg(ARM::SP))
2112 .addReg(ARM::LR);
2113 }
2114
2115 // Restore SR0 and SR1 in case of __morestack() was called.
2116 // __morestack() will skip PostStackMBB block so we need to restore
2117 // scratch registers from here.
2118 // pop {SR0, SR1}
2119 if (Thumb) {
2120 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2121 .addReg(ScratchReg0)
2122 .addReg(ScratchReg1);
2123 } else {
2124 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2125 .addReg(ARM::SP, RegState::Define)
2126 .addReg(ARM::SP))
2127 .addReg(ScratchReg0)
2128 .addReg(ScratchReg1);
2129 }
2130
2131 // Update the CFA offset now that we've popped
2132 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2133 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2134 .addCFIIndex(CFIIndex);
2135
2136 // bx lr - Return from this function.
2137 Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
2138 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode)));
2139
2140 // Restore SR0 and SR1 in case of __morestack() was not called.
2141 // pop {SR0, SR1}
2142 if (Thumb) {
2143 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP)))
2144 .addReg(ScratchReg0)
2145 .addReg(ScratchReg1);
2146 } else {
2147 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2148 .addReg(ARM::SP, RegState::Define)
2149 .addReg(ARM::SP))
2150 .addReg(ScratchReg0)
2151 .addReg(ScratchReg1);
2152 }
2153
2154 // Update the CFA offset now that we've popped
2155 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2156 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2157 .addCFIIndex(CFIIndex);
2158
2159 // Tell debuggers that r4 and r5 are now the same as they were in the
2160 // previous function, that they're the "Same Value".
2161 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2162 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2163 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2164 .addCFIIndex(CFIIndex);
2165 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2166 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2167 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2168 .addCFIIndex(CFIIndex);
2169
2170 // Organizing MBB lists
Quentin Colombet61b305e2015-05-05 17:38:16 +00002171 PostStackMBB->addSuccessor(&PrologueMBB);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002172
2173 AllocMBB->addSuccessor(PostStackMBB);
2174
2175 GetMBB->addSuccessor(PostStackMBB);
2176 GetMBB->addSuccessor(AllocMBB);
2177
2178 McrMBB->addSuccessor(GetMBB);
2179
2180 PrevStackMBB->addSuccessor(McrMBB);
2181
Filipe Cabecinhas0da99372016-04-29 15:22:48 +00002182#ifdef EXPENSIVE_CHECKS
Oliver Stannardb14c6252014-04-02 16:10:33 +00002183 MF.verify();
2184#endif
2185}