blob: 12e6e329a9234abb84afb459fddc13c490ef8651 [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"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000019#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000020#include "MCTargetDesc/ARMAddressingModes.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000021#include "MCTargetDesc/ARMBaseInfo.h"
22#include "llvm/ADT/BitVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000024#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000026#include "llvm/CodeGen/MachineBasicBlock.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000030#include "llvm/CodeGen/MachineInstr.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000033#include "llvm/CodeGen/MachineOperand.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000035#include "llvm/CodeGen/RegisterScavenging.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000036#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000038#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/Function.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000040#include "llvm/MC/MCContext.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000041#include "llvm/MC/MCDwarf.h"
42#include "llvm/MC/MCRegisterInfo.h"
43#include "llvm/Support/CodeGen.h"
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000044#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000045#include "llvm/Support/Compiler.h"
46#include "llvm/Support/Debug.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/MathExtras.h"
49#include "llvm/Support/raw_ostream.h"
50#include "llvm/Target/TargetInstrInfo.h"
51#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000052#include "llvm/Target/TargetOptions.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000053#include "llvm/Target/TargetRegisterInfo.h"
54#include "llvm/Target/TargetSubtargetInfo.h"
55#include <algorithm>
56#include <cassert>
57#include <cstddef>
58#include <cstdint>
59#include <iterator>
60#include <utility>
61#include <vector>
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000062
Reid Klecknerbdfc05f2016-10-11 21:14:03 +000063#define DEBUG_TYPE "arm-frame-lowering"
64
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000065using namespace llvm;
66
Benjamin Kramer9fceb902012-02-24 22:09:25 +000067static cl::opt<bool>
Jakob Stoklund Olesen68a922c2012-01-06 22:19:37 +000068SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000069 cl::desc("Align ARM NEON spills in prolog and epilog"));
70
71static MachineBasicBlock::iterator
72skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
73 unsigned NumAlignedDPRCS2Regs);
74
Eric Christopher45fb7b62014-06-26 19:29:59 +000075ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
76 : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
77 STI(sti) {}
78
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000079bool ARMFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
80 // iOS always has a FP for backtracking, force other targets to keep their FP
81 // when doing FastISel. The emitted code is currently superior, and in cases
82 // like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
83 return TargetFrameLowering::noFramePointerElim(MF) ||
84 MF.getSubtarget<ARMSubtarget>().useFastISel();
85}
86
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000087/// hasFP - Return true if the specified function should have a dedicated frame
88/// pointer register. This is true if the function has variable sized allocas
89/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000090bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +000091 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Oliver Stannard9aa6f012016-08-23 09:19:22 +000092 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000093
Oliver Stannard9aa6f012016-08-23 09:19:22 +000094 // ABI-required frame pointer.
95 if (MF.getTarget().Options.DisableFramePointerElim(MF))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000096 return true;
97
Oliver Stannard9aa6f012016-08-23 09:19:22 +000098 // Frame pointer required for use within this function.
99 return (RegInfo->needsStackRealignment(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000100 MFI.hasVarSizedObjects() ||
101 MFI.isFrameAddressTaken());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000102}
103
Bob Wilson657f2272011-01-13 21:10:12 +0000104/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
105/// not required, we reserve argument space for call sites in the function
106/// immediately on entry to the current function. This eliminates the need for
107/// add/sub sp brackets around call sites. Returns true if the call frame is
108/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000109bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000110 const MachineFrameInfo &MFI = MF.getFrameInfo();
111 unsigned CFSize = MFI.getMaxCallFrameSize();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000112 // It's not always a good idea to include the call frame as part of the
113 // stack frame. ARM (especially Thumb) has small immediate offset to
114 // address the stack frame. So a large call frame can cause poor codegen
115 // and may even makes it impossible to scavenge a register.
116 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
117 return false;
118
Matthias Braun941a7052016-07-28 18:40:00 +0000119 return !MFI.hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000120}
121
Bob Wilson657f2272011-01-13 21:10:12 +0000122/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
123/// call frame pseudos can be simplified. Unlike most targets, having a FP
124/// is not sufficient here since we still may reference some objects via SP
125/// even when FP is available in Thumb2 mode.
126bool
127ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000128 return hasReservedCallFrame(MF) || MF.getFrameInfo().hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000129}
130
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000131static bool isCSRestore(MachineInstr &MI, const ARMBaseInstrInfo &TII,
Craig Topper840beec2014-04-04 05:16:06 +0000132 const MCPhysReg *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000133 // Integer spill area is handled with "pop".
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000134 if (isPopOpcode(MI.getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000135 // The first two operands are predicates. The last two are
136 // imp-def and imp-use of SP. Check everything in between.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000137 for (int i = 5, e = MI.getNumOperands(); i != e; ++i)
138 if (!isCalleeSavedRegister(MI.getOperand(i).getReg(), CSRegs))
Eric Christopherb006fc92010-11-18 19:40:05 +0000139 return false;
140 return true;
141 }
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000142 if ((MI.getOpcode() == ARM::LDR_POST_IMM ||
143 MI.getOpcode() == ARM::LDR_POST_REG ||
144 MI.getOpcode() == ARM::t2LDR_POST) &&
145 isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs) &&
146 MI.getOperand(1).getReg() == ARM::SP)
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000147 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000148
149 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000150}
151
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000152static void emitRegPlusImmediate(
153 bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
154 const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
155 unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
156 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000157 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000158 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000159 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000160 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000161 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000162 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000163}
164
Tim Northoverc9432eb2013-11-04 23:04:15 +0000165static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000166 MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
Tim Northoverc9432eb2013-11-04 23:04:15 +0000167 const ARMBaseInstrInfo &TII, int NumBytes,
168 unsigned MIFlags = MachineInstr::NoFlags,
169 ARMCC::CondCodes Pred = ARMCC::AL,
170 unsigned PredReg = 0) {
171 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
172 MIFlags, Pred, PredReg);
173}
174
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000175static int sizeOfSPAdjustment(const MachineInstr &MI) {
Tim Northover603d3162014-11-14 22:45:33 +0000176 int RegSize;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000177 switch (MI.getOpcode()) {
Tim Northover603d3162014-11-14 22:45:33 +0000178 case ARM::VSTMDDB_UPD:
179 RegSize = 8;
180 break;
181 case ARM::STMDB_UPD:
182 case ARM::t2STMDB_UPD:
183 RegSize = 4;
184 break;
185 case ARM::t2STR_PRE:
186 case ARM::STR_PRE_IMM:
187 return 4;
188 default:
189 llvm_unreachable("Unknown push or pop like instruction");
190 }
191
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000192 int count = 0;
193 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
194 // pred) so the list starts at 4.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000195 for (int i = MI.getNumOperands() - 1; i >= 4; --i)
Tim Northover603d3162014-11-14 22:45:33 +0000196 count += RegSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000197 return count;
198}
199
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000200static bool WindowsRequiresStackProbe(const MachineFunction &MF,
201 size_t StackSizeInBytes) {
Matthias Braun941a7052016-07-28 18:40:00 +0000202 const MachineFrameInfo &MFI = MF.getFrameInfo();
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000203 const Function *F = MF.getFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000204 unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000205 if (F->hasFnAttribute("stack-probe-size"))
206 F->getFnAttribute("stack-probe-size")
207 .getValueAsString()
208 .getAsInteger(0, StackProbeSize);
209 return StackSizeInBytes >= StackProbeSize;
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000210}
211
Tim Northover603d3162014-11-14 22:45:33 +0000212namespace {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000213
Tim Northover603d3162014-11-14 22:45:33 +0000214struct StackAdjustingInsts {
215 struct InstInfo {
216 MachineBasicBlock::iterator I;
217 unsigned SPAdjust;
218 bool BeforeFPSet;
219 };
220
221 SmallVector<InstInfo, 4> Insts;
222
223 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
224 bool BeforeFPSet = false) {
225 InstInfo Info = {I, SPAdjust, BeforeFPSet};
226 Insts.push_back(Info);
227 }
228
229 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000230 auto Info =
231 llvm::find_if(Insts, [&](InstInfo &Info) { return Info.I == I; });
Tim Northover603d3162014-11-14 22:45:33 +0000232 assert(Info != Insts.end() && "invalid sp adjusting instruction");
233 Info->SPAdjust += ExtraBytes;
234 }
235
Matthias Braunf23ef432016-11-30 23:48:42 +0000236 void emitDefCFAOffsets(MachineBasicBlock &MBB, const DebugLoc &dl,
237 const ARMBaseInstrInfo &TII, bool HasFP) {
238 MachineFunction &MF = *MBB.getParent();
Tim Northover603d3162014-11-14 22:45:33 +0000239 unsigned CFAOffset = 0;
240 for (auto &Info : Insts) {
241 if (HasFP && !Info.BeforeFPSet)
242 return;
243
244 CFAOffset -= Info.SPAdjust;
Matthias Braunf23ef432016-11-30 23:48:42 +0000245 unsigned CFIIndex = MF.addFrameInst(
Tim Northover603d3162014-11-14 22:45:33 +0000246 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
247 BuildMI(MBB, std::next(Info.I), dl,
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000248 TII.get(TargetOpcode::CFI_INSTRUCTION))
249 .addCFIIndex(CFIIndex)
250 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000251 }
252 }
253};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000254
255} // end anonymous namespace
Tim Northover603d3162014-11-14 22:45:33 +0000256
Kristof Beyls933de7a2015-01-08 15:09:14 +0000257/// Emit an instruction sequence that will align the address in
258/// register Reg by zero-ing out the lower bits. For versions of the
259/// architecture that support Neon, this must be done in a single
260/// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
261/// single instruction. That function only gets called when optimizing
262/// spilling of D registers on a core with the Neon instruction set
263/// present.
264static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
265 const TargetInstrInfo &TII,
266 MachineBasicBlock &MBB,
267 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000268 const DebugLoc &DL, const unsigned Reg,
Kristof Beyls933de7a2015-01-08 15:09:14 +0000269 const unsigned Alignment,
270 const bool MustBeSingleInstruction) {
Eric Christopher1b21f002015-01-29 00:19:33 +0000271 const ARMSubtarget &AST =
272 static_cast<const ARMSubtarget &>(MF.getSubtarget());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000273 const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
274 const unsigned AlignMask = Alignment - 1;
275 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
276 assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
277 if (!AFI->isThumbFunction()) {
278 // if the BFC instruction is available, use that to zero the lower
279 // bits:
280 // bfc Reg, #0, log2(Alignment)
281 // otherwise use BIC, if the mask to zero the required number of bits
282 // can be encoded in the bic immediate field
283 // bic Reg, Reg, Alignment-1
284 // otherwise, emit
285 // lsr Reg, Reg, log2(Alignment)
286 // lsl Reg, Reg, log2(Alignment)
287 if (CanUseBFC) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000288 BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
289 .addReg(Reg, RegState::Kill)
290 .addImm(~AlignMask)
291 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000292 } else if (AlignMask <= 255) {
Diana Picus8a73f552017-01-13 10:18:01 +0000293 BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
294 .addReg(Reg, RegState::Kill)
295 .addImm(AlignMask)
296 .add(predOps(ARMCC::AL))
297 .add(condCodeOp());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000298 } else {
299 assert(!MustBeSingleInstruction &&
300 "Shouldn't call emitAligningInstructions demanding a single "
301 "instruction to be emitted for large stack alignment for a target "
302 "without BFC.");
Diana Picus8a73f552017-01-13 10:18:01 +0000303 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
304 .addReg(Reg, RegState::Kill)
305 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))
306 .add(predOps(ARMCC::AL))
307 .add(condCodeOp());
308 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
309 .addReg(Reg, RegState::Kill)
310 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))
311 .add(predOps(ARMCC::AL))
312 .add(condCodeOp());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000313 }
314 } else {
315 // Since this is only reached for Thumb-2 targets, the BFC instruction
316 // should always be available.
317 assert(CanUseBFC);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000318 BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
319 .addReg(Reg, RegState::Kill)
320 .addImm(~AlignMask)
321 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000322 }
323}
324
Matthias Braun8aaa3682017-04-19 21:11:44 +0000325/// We need the offset of the frame pointer relative to other MachineFrameInfo
326/// offsets which are encoded relative to SP at function begin.
327/// See also emitPrologue() for how the FP is set up.
328/// Unfortunately we cannot determine this value in determineCalleeSaves() yet
329/// as assignCalleeSavedSpillSlots() hasn't run at this point. Instead we use
330/// this to produce a conservative estimate that we check in an assert() later.
331static int getMaxFPOffset(const Function &F, const ARMFunctionInfo &AFI) {
332 // This is a conservative estimation: Assume the frame pointer being r7 and
333 // pc("r15") up to r8 getting spilled before (= 8 registers).
334 return -AFI.getArgRegsSaveSize() - (8 * 4);
335}
336
Quentin Colombet61b305e2015-05-05 17:38:16 +0000337void ARMFrameLowering::emitPrologue(MachineFunction &MF,
338 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000339 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000340 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000341 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000342 MachineModuleInfo &MMI = MF.getMMI();
343 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000344 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000345 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopher1b21f002015-01-29 00:19:33 +0000346 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
347 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000348 assert(!AFI->isThumb1OnlyFunction() &&
349 "This emitPrologue does not support Thumb1!");
350 bool isARM = !AFI->isThumbFunction();
Eric Christopher1b21f002015-01-29 00:19:33 +0000351 unsigned Align = STI.getFrameLowering()->getStackAlignment();
Tim Northover775aaeb2015-11-05 21:54:58 +0000352 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000353 unsigned NumBytes = MFI.getStackSize();
354 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000355
356 // Debug location must be unknown since the first debug location is used
357 // to determine the end of the prologue.
358 DebugLoc dl;
359
360 unsigned FramePtr = RegInfo->getFrameRegister(MF);
361
362 // Determine the sizes of each callee-save spill areas and record which frame
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000363 // belongs to which callee-save spill areas.
364 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
365 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000366 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000367
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000368 // All calls are tail calls in GHC calling conv, and functions have no
369 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000370 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
371 return;
372
Tim Northover603d3162014-11-14 22:45:33 +0000373 StackAdjustingInsts DefCFAOffsetCandidates;
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000374 bool HasFP = hasFP(MF);
Tim Northover603d3162014-11-14 22:45:33 +0000375
Oliver Stannardd55e1152014-03-05 15:25:27 +0000376 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000377 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000378 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000379 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000380 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000381 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000382
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000383 if (!AFI->hasStackFrame() &&
384 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000385 if (NumBytes - ArgRegsSaveSize != 0) {
386 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000387 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000388 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
389 NumBytes - ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000390 }
Matthias Braunf23ef432016-11-30 23:48:42 +0000391 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000392 return;
393 }
394
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000395 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000396 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
397 unsigned Reg = CSI[i].getReg();
398 int FI = CSI[i].getFrameIdx();
399 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000400 case ARM::R8:
401 case ARM::R9:
402 case ARM::R10:
403 case ARM::R11:
404 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000405 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000406 GPRCS2Size += 4;
407 break;
408 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000409 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +0000410 case ARM::R0:
411 case ARM::R1:
412 case ARM::R2:
413 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000414 case ARM::R4:
415 case ARM::R5:
416 case ARM::R6:
417 case ARM::R7:
418 case ARM::LR:
419 if (Reg == FramePtr)
420 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000421 GPRCS1Size += 4;
422 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000423 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000424 // This is a DPR. Exclude the aligned DPRCS2 spills.
425 if (Reg == ARM::D8)
426 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000427 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000428 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000429 }
430 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000431
Eric Christopherb006fc92010-11-18 19:40:05 +0000432 // Move past area 1.
Tim Northover603d3162014-11-14 22:45:33 +0000433 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
434 if (GPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000435 GPRCS1Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000436 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
437 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000438
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000439 // Determine starting offsets of spill areas.
Tim Northover228c9432014-11-05 00:27:13 +0000440 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
441 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
442 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
443 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
444 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000445 int FramePtrOffsetInPush = 0;
446 if (HasFP) {
Matthias Braun8aaa3682017-04-19 21:11:44 +0000447 int FPOffset = MFI.getObjectOffset(FramePtrSpillFI);
448 assert(getMaxFPOffset(*MF.getFunction(), *AFI) <= FPOffset &&
449 "Max FP estimation is wrong");
450 FramePtrOffsetInPush = FPOffset + ArgRegsSaveSize;
Matthias Braun941a7052016-07-28 18:40:00 +0000451 AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000452 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000453 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000454 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
455 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
456 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
457
Tim Northoverc9432eb2013-11-04 23:04:15 +0000458 // Move past area 2.
Tim Northover603d3162014-11-14 22:45:33 +0000459 if (GPRCS2Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000460 GPRCS2Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000461 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
462 }
Tim Northoverc9432eb2013-11-04 23:04:15 +0000463
Tim Northover228c9432014-11-05 00:27:13 +0000464 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
465 // .cfi_offset operations will reflect that.
466 if (DPRGapSize) {
467 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
Duncan P. N. Exon Smithec083b52016-08-17 00:53:04 +0000468 if (LastPush != MBB.end() &&
469 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, DPRGapSize))
Tim Northover603d3162014-11-14 22:45:33 +0000470 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
471 else {
Tim Northover228c9432014-11-05 00:27:13 +0000472 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
473 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000474 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
475 }
Tim Northover228c9432014-11-05 00:27:13 +0000476 }
477
Eric Christopherb006fc92010-11-18 19:40:05 +0000478 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000479 if (DPRCSSize > 0) {
Evan Cheng70d29632011-02-25 00:24:46 +0000480 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000481 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000482 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000483 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(*MBBI));
Tim Northover93bcc662013-11-08 17:18:07 +0000484 LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000485 }
Evan Cheng70d29632011-02-25 00:24:46 +0000486 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000487
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000488 // Move past the aligned DPRCS2 area.
489 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
490 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
491 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
492 // leaves the stack pointer pointing to the DPRCS2 area.
493 //
494 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
Matthias Braun941a7052016-07-28 18:40:00 +0000495 NumBytes += MFI.getObjectOffset(D8SpillFI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000496 } else
497 NumBytes = DPRCSOffset;
498
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000499 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
500 uint32_t NumWords = NumBytes >> 2;
501
502 if (NumWords < 65536)
Diana Picus4f8c3e12017-01-13 09:37:56 +0000503 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
504 .addImm(NumWords)
505 .setMIFlags(MachineInstr::FrameSetup)
506 .add(predOps(ARMCC::AL));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000507 else
508 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000509 .addImm(NumWords)
510 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000511
512 switch (TM.getCodeModel()) {
513 case CodeModel::Small:
514 case CodeModel::Medium:
515 case CodeModel::Default:
516 case CodeModel::Kernel:
517 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
Diana Picusbd66b7d2017-01-20 08:15:24 +0000518 .add(predOps(ARMCC::AL))
519 .addExternalSymbol("__chkstk")
520 .addReg(ARM::R4, RegState::Implicit)
521 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000522 break;
523 case CodeModel::Large:
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000524 case CodeModel::JITDefault:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000525 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000526 .addExternalSymbol("__chkstk")
527 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000528
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000529 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
Diana Picusbd66b7d2017-01-20 08:15:24 +0000530 .add(predOps(ARMCC::AL))
531 .addReg(ARM::R12, RegState::Kill)
532 .addReg(ARM::R4, RegState::Implicit)
533 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000534 break;
535 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000536
Diana Picus8a73f552017-01-13 10:18:01 +0000537 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), ARM::SP)
538 .addReg(ARM::SP, RegState::Kill)
539 .addReg(ARM::R4, RegState::Kill)
540 .setMIFlags(MachineInstr::FrameSetup)
541 .add(predOps(ARMCC::AL))
542 .add(condCodeOp());
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000543 NumBytes = 0;
544 }
545
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000546 if (NumBytes) {
547 // Adjust SP after all the callee-save spills.
Tim Northoverbeb5bcc2015-09-23 22:21:09 +0000548 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000549 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, NumBytes))
Tim Northover603d3162014-11-14 22:45:33 +0000550 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
551 else {
Tim Northover93bcc662013-11-08 17:18:07 +0000552 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
553 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000554 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
555 }
Tim Northover93bcc662013-11-08 17:18:07 +0000556
Evan Chengeb56dca2010-11-22 18:12:04 +0000557 if (HasFP && isARM)
558 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
559 // Note it's not safe to do this in Thumb2 mode because it would have
560 // taken two instructions:
561 // mov sp, r7
562 // sub sp, #24
563 // If an interrupt is taken between the two instructions, then sp is in
564 // an inconsistent state (pointing to the middle of callee-saved area).
565 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000566 AFI->setShouldRestoreSPFromFP(true);
567 }
568
Tim Northover603d3162014-11-14 22:45:33 +0000569 // Set FP to point to the stack slot that contains the previous FP.
570 // For iOS, FP is R7, which has now been stored in spill area 1.
571 // Otherwise, if this is not iOS, all the callee-saved registers go
572 // into spill area 1, including the FP in R11. In either case, it
573 // is in area one and the adjustment needs to take place just after
574 // that push.
575 if (HasFP) {
576 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000577 unsigned PushSize = sizeOfSPAdjustment(*GPRCS1Push);
Tim Northover603d3162014-11-14 22:45:33 +0000578 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
579 dl, TII, FramePtr, ARM::SP,
580 PushSize + FramePtrOffsetInPush,
581 MachineInstr::FrameSetup);
582 if (FramePtrOffsetInPush + PushSize != 0) {
Matthias Braunf23ef432016-11-30 23:48:42 +0000583 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
Tim Northover603d3162014-11-14 22:45:33 +0000584 nullptr, MRI->getDwarfRegNum(FramePtr, true),
585 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
586 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000587 .addCFIIndex(CFIIndex)
588 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000589 } else {
590 unsigned CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +0000591 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
Tim Northover603d3162014-11-14 22:45:33 +0000592 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
593 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000594 .addCFIIndex(CFIIndex)
595 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000596 }
597 }
598
599 // Now that the prologue's actual instructions are finalised, we can insert
600 // the necessary DWARF cf instructions to describe the situation. Start by
601 // recording where each register ended up:
602 if (GPRCS1Size > 0) {
603 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
604 int CFIIndex;
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000605 for (const auto &Entry : CSI) {
606 unsigned Reg = Entry.getReg();
607 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000608 switch (Reg) {
609 case ARM::R8:
610 case ARM::R9:
611 case ARM::R10:
612 case ARM::R11:
613 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000614 if (STI.splitFramePushPop(MF))
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000615 break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000616 LLVM_FALLTHROUGH;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000617 case ARM::R0:
618 case ARM::R1:
619 case ARM::R2:
620 case ARM::R3:
621 case ARM::R4:
622 case ARM::R5:
623 case ARM::R6:
624 case ARM::R7:
625 case ARM::LR:
Matthias Braunf23ef432016-11-30 23:48:42 +0000626 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Matthias Braun941a7052016-07-28 18:40:00 +0000627 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000628 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000629 .addCFIIndex(CFIIndex)
630 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000631 break;
632 }
633 }
634 }
635
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000636 if (GPRCS2Size > 0) {
Tim Northover603d3162014-11-14 22:45:33 +0000637 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000638 for (const auto &Entry : CSI) {
639 unsigned Reg = Entry.getReg();
640 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000641 switch (Reg) {
642 case ARM::R8:
643 case ARM::R9:
644 case ARM::R10:
645 case ARM::R11:
646 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000647 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000648 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000649 unsigned Offset = MFI.getObjectOffset(FI);
Matthias Braunf23ef432016-11-30 23:48:42 +0000650 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000651 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
652 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000653 .addCFIIndex(CFIIndex)
654 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000655 }
656 break;
657 }
658 }
659 }
660
661 if (DPRCSSize > 0) {
662 // Since vpush register list cannot have gaps, there may be multiple vpush
663 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000664 MachineBasicBlock::iterator Pos = std::next(LastPush);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000665 for (const auto &Entry : CSI) {
666 unsigned Reg = Entry.getReg();
667 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000668 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
669 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
670 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000671 unsigned Offset = MFI.getObjectOffset(FI);
Matthias Braunf23ef432016-11-30 23:48:42 +0000672 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000673 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover603d3162014-11-14 22:45:33 +0000674 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000675 .addCFIIndex(CFIIndex)
676 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000677 }
678 }
679 }
680
Tim Northover603d3162014-11-14 22:45:33 +0000681 // Now we can emit descriptions of where the canonical frame address was
682 // throughout the process. If we have a frame pointer, it takes over the job
683 // half-way through, so only the first few .cfi_def_cfa_offset instructions
684 // actually get emitted.
Matthias Braunf23ef432016-11-30 23:48:42 +0000685 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
Tim Northover93bcc662013-11-08 17:18:07 +0000686
Evan Chengeb56dca2010-11-22 18:12:04 +0000687 if (STI.isTargetELF() && hasFP(MF))
Matthias Braun941a7052016-07-28 18:40:00 +0000688 MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
689 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000690
691 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
692 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000693 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000694 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
695
696 // If we need dynamic stack realignment, do it here. Be paranoid and make
697 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000698 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000699 // realigned.
700 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000701 unsigned MaxAlign = MFI.getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +0000702 assert(!AFI->isThumb1OnlyFunction());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000703 if (!AFI->isThumbFunction()) {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000704 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
705 false);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000706 } else {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000707 // We cannot use sp as source/dest register here, thus we're using r4 to
708 // perform the calculations. We're emitting the following sequence:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000709 // mov r4, sp
Kristof Beyls933de7a2015-01-08 15:09:14 +0000710 // -- use emitAligningInstructions to produce best sequence to zero
711 // -- out lower bits in r4
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000712 // mov sp, r4
713 // FIXME: It will be better just to find spare register here.
Diana Picus4f8c3e12017-01-13 09:37:56 +0000714 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
715 .addReg(ARM::SP, RegState::Kill)
716 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000717 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
718 false);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000719 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
720 .addReg(ARM::R4, RegState::Kill)
721 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000722 }
723
724 AFI->setShouldRestoreSPFromFP(true);
725 }
726
727 // If we need a base pointer, set it up here. It's whatever the value
728 // of the stack pointer is at this point. Any variable size objects
729 // will be allocated after this, so we can still use the base pointer
730 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000731 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000732 if (RegInfo->hasBasePointer(MF)) {
733 if (isARM)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000734 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), RegInfo->getBaseRegister())
735 .addReg(ARM::SP)
736 .add(predOps(ARMCC::AL))
737 .add(condCodeOp());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000738 else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000739 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), RegInfo->getBaseRegister())
740 .addReg(ARM::SP)
741 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000742 }
743
744 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000745 // the sp from fp. We can assume there's an FP here since hasFP already
746 // checks for hasVarSizedObjects.
Matthias Braun941a7052016-07-28 18:40:00 +0000747 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000748 AFI->setShouldRestoreSPFromFP(true);
749}
750
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000751void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000752 MachineBasicBlock &MBB) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000753 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000754 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000755 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000756 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000757 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000758 assert(!AFI->isThumb1OnlyFunction() &&
759 "This emitEpilogue does not support Thumb1!");
760 bool isARM = !AFI->isThumbFunction();
761
Tim Northover8cda34f2015-03-11 18:54:22 +0000762 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000763 int NumBytes = (int)MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000764 unsigned FramePtr = RegInfo->getFrameRegister(MF);
765
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000766 // All calls are tail calls in GHC calling conv, and functions have no
767 // prologue/epilogue.
Quentin Colombet71a71482015-07-20 21:42:14 +0000768 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000769 return;
Quentin Colombet71a71482015-07-20 21:42:14 +0000770
771 // First put ourselves on the first (from top) terminator instructions.
772 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
773 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Eric Christopherb3322362012-08-03 00:05:53 +0000774
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000775 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000776 if (NumBytes - ArgRegsSaveSize != 0)
777 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000778 } else {
779 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000780 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000781 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000782 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000783 --MBBI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000784 } while (MBBI != MBB.begin() && isCSRestore(*MBBI, TII, CSRegs));
785 if (!isCSRestore(*MBBI, TII, CSRegs))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000786 ++MBBI;
787 }
788
789 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000790 NumBytes -= (ArgRegsSaveSize +
791 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000792 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000793 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000794 AFI->getDPRCalleeSavedAreaSize());
795
796 // Reset SP based on frame pointer only if the stack frame extends beyond
797 // frame pointer stack slot or target is ELF and the function has FP.
798 if (AFI->shouldRestoreSPFromFP()) {
799 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
800 if (NumBytes) {
801 if (isARM)
802 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
803 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000804 else {
805 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000806 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000807 // mov sp, r7
808 // sub sp, #24
809 // This is bad, if an interrupt is taken after the mov, sp is in an
810 // inconsistent state.
811 // Use the first callee-saved register as a scratch register.
Matthias Braun941a7052016-07-28 18:40:00 +0000812 assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000813 "No scratch register to restore SP from FP!");
814 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000815 ARMCC::AL, 0, TII);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000816 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
817 .addReg(ARM::R4)
818 .add(predOps(ARMCC::AL));
Evan Chengeb56dca2010-11-22 18:12:04 +0000819 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000820 } else {
821 // Thumb2 or ARM.
822 if (isARM)
823 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000824 .addReg(FramePtr)
825 .add(predOps(ARMCC::AL))
826 .add(condCodeOp());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000827 else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000828 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
829 .addReg(FramePtr)
830 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000831 }
Tim Northoverdee86042013-12-02 14:46:26 +0000832 } else if (NumBytes &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000833 !tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
834 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000835
Eric Christopherb006fc92010-11-18 19:40:05 +0000836 // Increment past our save areas.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000837 if (MBBI != MBB.end() && AFI->getDPRCalleeSavedAreaSize()) {
Evan Cheng70d29632011-02-25 00:24:46 +0000838 MBBI++;
839 // Since vpop register list cannot have gaps, there may be multiple vpop
840 // instructions in the epilogue.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000841 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VLDMDIA_UPD)
Evan Cheng70d29632011-02-25 00:24:46 +0000842 MBBI++;
843 }
Tim Northover228c9432014-11-05 00:27:13 +0000844 if (AFI->getDPRCalleeSavedGapSize()) {
845 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
846 "unexpected DPR alignment gap");
847 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
848 }
849
Eric Christopherb006fc92010-11-18 19:40:05 +0000850 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
851 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000852 }
853
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000854 if (ArgRegsSaveSize)
855 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000856}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000857
Bob Wilson657f2272011-01-13 21:10:12 +0000858/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
859/// debug info. It's the same as what we use for resolving the code-gen
860/// references for now. FIXME: This can go wrong when references are
861/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000862int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000863ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000864 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000865 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
866}
867
868int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000869ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000870 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000871 int SPAdj) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000872 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000873 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000874 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000875 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000876 int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +0000877 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000878 bool isFixed = MFI.isFixedObjectIndex(FI);
Anton Korobeynikov46877782010-11-20 15:59:32 +0000879
880 FrameReg = ARM::SP;
881 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000882
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000883 // SP can move around if there are allocas. We may also lose track of SP
884 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000885 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000886
Anton Korobeynikov46877782010-11-20 15:59:32 +0000887 // When dynamically realigning the stack, use the frame pointer for
888 // parameters, and the stack/base pointer for locals.
889 if (RegInfo->needsStackRealignment(MF)) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000890 assert(hasFP(MF) && "dynamic stack realignment without a FP!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000891 if (isFixed) {
892 FrameReg = RegInfo->getFrameRegister(MF);
893 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000894 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000895 assert(RegInfo->hasBasePointer(MF) &&
896 "VLAs and dynamic stack alignment, but missing base pointer!");
897 FrameReg = RegInfo->getBaseRegister();
898 }
899 return Offset;
900 }
901
902 // If there is a frame pointer, use it when we can.
903 if (hasFP(MF) && AFI->hasStackFrame()) {
904 // Use frame pointer to reference fixed objects. Use it for locals if
905 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000906 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000907 FrameReg = RegInfo->getFrameRegister(MF);
908 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000909 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000910 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000911 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000912 // Try to use the frame pointer if we can, else use the base pointer
913 // since it's available. This is handy for the emergency spill slot, in
914 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000915 if (FPOffset >= -255 && FPOffset < 0) {
916 FrameReg = RegInfo->getFrameRegister(MF);
917 return FPOffset;
918 }
Evan Chengc0d20042011-04-22 01:42:52 +0000919 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000920 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000921 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000922 // ldr <rd>, [sp, #<imm8>]
923 // if at all possible to save space.
924 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
925 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000926 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000927 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000928 if (FPOffset >= -255 && FPOffset < 0) {
929 FrameReg = RegInfo->getFrameRegister(MF);
930 return FPOffset;
931 }
932 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
933 // Otherwise, use SP or FP, whichever is closer to the stack slot.
934 FrameReg = RegInfo->getFrameRegister(MF);
935 return FPOffset;
936 }
937 }
938 // Use the base pointer if we have one.
939 if (RegInfo->hasBasePointer(MF))
940 FrameReg = RegInfo->getBaseRegister();
941 return Offset;
942}
943
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000944void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000945 MachineBasicBlock::iterator MI,
946 const std::vector<CalleeSavedInfo> &CSI,
947 unsigned StmOpc, unsigned StrOpc,
948 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000949 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000950 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000951 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000952 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000953 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover46a6f0f2016-11-14 20:28:24 +0000954 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000955
956 DebugLoc DL;
957
Tim Northover46a6f0f2016-11-14 20:28:24 +0000958 typedef std::pair<unsigned, bool> RegAndKill;
959 SmallVector<RegAndKill, 4> Regs;
Tim Northover775aaeb2015-11-05 21:54:58 +0000960 unsigned i = CSI.size();
Evan Cheng775ead32010-12-07 23:08:38 +0000961 while (i != 0) {
962 unsigned LastReg = 0;
963 for (; i != 0; --i) {
964 unsigned Reg = CSI[i-1].getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000965 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000966
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000967 // D-registers in the aligned area DPRCS2 are NOT spilled here.
968 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
969 continue;
970
Matthias Braun0dba4e32017-05-31 01:21:30 +0000971 const MachineRegisterInfo &MRI = MF.getRegInfo();
972 bool isLiveIn = MRI.isLiveIn(Reg);
973 if (!isLiveIn && !MRI.isReserved(Reg))
Evan Cheng775ead32010-12-07 23:08:38 +0000974 MBB.addLiveIn(Reg);
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000975 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000976 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000977 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000978 if (NoGap && LastReg && LastReg != Reg-1)
979 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000980 LastReg = Reg;
Matthias Braun707e02c2016-04-13 21:43:25 +0000981 // Do not set a kill flag on values that are also marked as live-in. This
982 // happens with the @llvm-returnaddress intrinsic and with arguments
983 // passed in callee saved registers.
984 // Omitting the kill flags is conservatively correct even if the live-in
985 // is not used after all.
986 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000987 }
988
Jim Grosbach5fccad82010-12-09 18:31:13 +0000989 if (Regs.empty())
990 continue;
Tim Northover46a6f0f2016-11-14 20:28:24 +0000991
Tim Northover3d38c382016-11-14 20:31:53 +0000992 std::sort(Regs.begin(), Regs.end(), [&](const RegAndKill &LHS,
993 const RegAndKill &RHS) {
Tim Northover46a6f0f2016-11-14 20:28:24 +0000994 return TRI.getEncodingValue(LHS.first) < TRI.getEncodingValue(RHS.first);
995 });
996
Jim Grosbach5fccad82010-12-09 18:31:13 +0000997 if (Regs.size() > 1 || StrOpc== 0) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000998 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
999 .addReg(ARM::SP)
1000 .setMIFlags(MIFlags)
1001 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001002 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1003 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +00001004 } else if (Regs.size() == 1) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001005 BuildMI(MBB, MI, DL, TII.get(StrOpc), ARM::SP)
1006 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
1007 .addReg(ARM::SP)
1008 .setMIFlags(MIFlags)
1009 .addImm(-4)
1010 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001011 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001012 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001013
1014 // Put any subsequent vpush instructions before this one: they will refer to
1015 // higher register numbers so need to be pushed first in order to preserve
1016 // monotonicity.
Quentin Colombet71a71482015-07-20 21:42:14 +00001017 if (MI != MBB.begin())
1018 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001019 }
Evan Cheng775ead32010-12-07 23:08:38 +00001020}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001021
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001022void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001023 MachineBasicBlock::iterator MI,
1024 const std::vector<CalleeSavedInfo> &CSI,
1025 unsigned LdmOpc, unsigned LdrOpc,
1026 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001027 bool(*Func)(unsigned, bool),
1028 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +00001029 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001030 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover46a6f0f2016-11-14 20:28:24 +00001031 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Evan Cheng775ead32010-12-07 23:08:38 +00001032 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet71a71482015-07-20 21:42:14 +00001033 DebugLoc DL;
1034 bool isTailCall = false;
1035 bool isInterrupt = false;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001036 bool isTrap = false;
Quentin Colombet71a71482015-07-20 21:42:14 +00001037 if (MBB.end() != MI) {
1038 DL = MI->getDebugLoc();
1039 unsigned RetOpcode = MI->getOpcode();
1040 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
1041 isInterrupt =
1042 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001043 isTrap =
1044 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
1045 RetOpcode == ARM::tTRAP;
Quentin Colombet71a71482015-07-20 21:42:14 +00001046 }
Evan Cheng775ead32010-12-07 23:08:38 +00001047
1048 SmallVector<unsigned, 4> Regs;
1049 unsigned i = CSI.size();
1050 while (i != 0) {
1051 unsigned LastReg = 0;
1052 bool DeleteRet = false;
1053 for (; i != 0; --i) {
1054 unsigned Reg = CSI[i-1].getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001055 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Evan Cheng775ead32010-12-07 23:08:38 +00001056
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001057 // The aligned reloads from area DPRCS2 are not inserted here.
1058 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
1059 continue;
1060
Tim Northoverd8407452013-10-01 14:33:28 +00001061 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001062 !isTrap && STI.hasV5TOps()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00001063 if (MBB.succ_empty()) {
1064 Reg = ARM::PC;
1065 DeleteRet = true;
1066 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
1067 } else
1068 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Evan Cheng775ead32010-12-07 23:08:38 +00001069 // Fold the return instruction into the LDM.
Evan Cheng775ead32010-12-07 23:08:38 +00001070 }
1071
Evan Cheng9d54ae62010-12-08 06:29:02 +00001072 // If NoGap is true, pop consecutive registers and then leave the rest
1073 // for other instructions. e.g.
1074 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1075 if (NoGap && LastReg && LastReg != Reg-1)
1076 break;
1077
Evan Cheng775ead32010-12-07 23:08:38 +00001078 LastReg = Reg;
1079 Regs.push_back(Reg);
1080 }
1081
Jim Grosbach5fccad82010-12-09 18:31:13 +00001082 if (Regs.empty())
1083 continue;
Tim Northover46a6f0f2016-11-14 20:28:24 +00001084
1085 std::sort(Regs.begin(), Regs.end(), [&](unsigned LHS, unsigned RHS) {
1086 return TRI.getEncodingValue(LHS) < TRI.getEncodingValue(RHS);
1087 });
1088
Jim Grosbach5fccad82010-12-09 18:31:13 +00001089 if (Regs.size() > 1 || LdrOpc == 0) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001090 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
1091 .addReg(ARM::SP)
1092 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001093 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1094 MIB.addReg(Regs[i], getDefRegState(true));
Quentin Colombet71a71482015-07-20 21:42:14 +00001095 if (DeleteRet && MI != MBB.end()) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001096 MIB.copyImplicitOps(*MI);
Evan Cheng775ead32010-12-07 23:08:38 +00001097 MI->eraseFromParent();
Andrew Trick6446bf72011-08-25 17:50:53 +00001098 }
Evan Cheng775ead32010-12-07 23:08:38 +00001099 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +00001100 } else if (Regs.size() == 1) {
1101 // If we adjusted the reg to PC from LR above, switch it back here. We
1102 // only do that for LDM.
1103 if (Regs[0] == ARM::PC)
1104 Regs[0] = ARM::LR;
1105 MachineInstrBuilder MIB =
1106 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1107 .addReg(ARM::SP, RegState::Define)
1108 .addReg(ARM::SP);
1109 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1110 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +00001111 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +00001112 MIB.addReg(0);
1113 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1114 } else
1115 MIB.addImm(4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001116 MIB.add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001117 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001118 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001119
1120 // Put any subsequent vpop instructions after this one: they will refer to
1121 // higher register numbers so need to be popped afterwards.
Quentin Colombet71a71482015-07-20 21:42:14 +00001122 if (MI != MBB.end())
1123 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +00001124 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001125}
1126
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001127/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +00001128/// starting from d8. Also insert stack realignment code and leave the stack
1129/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001130static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1131 MachineBasicBlock::iterator MI,
1132 unsigned NumAlignedDPRCS2Regs,
1133 const std::vector<CalleeSavedInfo> &CSI,
1134 const TargetRegisterInfo *TRI) {
1135 MachineFunction &MF = *MBB.getParent();
1136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001137 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001138 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001139 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001140
1141 // Mark the D-register spill slots as properly aligned. Since MFI computes
1142 // stack slot layout backwards, this can actually mean that the d-reg stack
1143 // slot offsets can be wrong. The offset for d8 will always be correct.
1144 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1145 unsigned DNum = CSI[i].getReg() - ARM::D8;
Tim Northovere0ccdc62015-10-28 22:46:43 +00001146 if (DNum > NumAlignedDPRCS2Regs - 1)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001147 continue;
1148 int FI = CSI[i].getFrameIdx();
1149 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1150 // registers will be 8-byte aligned.
1151 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1152
1153 // The stack slot for D8 needs to be maximally aligned because this is
1154 // actually the point where we align the stack pointer. MachineFrameInfo
1155 // computes all offsets relative to the incoming stack pointer which is a
1156 // bit weird when realigning the stack. Any extra padding for this
1157 // over-alignment is not realized because the code inserted below adjusts
1158 // the stack pointer by numregs * 8 before aligning the stack pointer.
1159 if (DNum == 0)
1160 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1161 }
1162
1163 // Move the stack pointer to the d8 spill slot, and align it at the same
1164 // time. Leave the stack slot address in the scratch register r4.
1165 //
1166 // sub r4, sp, #numregs * 8
1167 // bic r4, r4, #align - 1
1168 // mov sp, r4
1169 //
1170 bool isThumb = AFI->isThumbFunction();
1171 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1172 AFI->setShouldRestoreSPFromFP(true);
1173
1174 // sub r4, sp, #numregs * 8
1175 // The immediate is <= 64, so it doesn't need any special encoding.
1176 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
Diana Picus8a73f552017-01-13 10:18:01 +00001177 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1178 .addReg(ARM::SP)
1179 .addImm(8 * NumAlignedDPRCS2Regs)
1180 .add(predOps(ARMCC::AL))
1181 .add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001182
Matthias Braun941a7052016-07-28 18:40:00 +00001183 unsigned MaxAlign = MF.getFrameInfo().getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +00001184 // We must set parameter MustBeSingleInstruction to true, since
1185 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1186 // stack alignment. Luckily, this can always be done since all ARM
1187 // architecture versions that support Neon also support the BFC
1188 // instruction.
1189 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001190
1191 // mov sp, r4
1192 // The stack pointer must be adjusted before spilling anything, otherwise
1193 // the stack slots could be clobbered by an interrupt handler.
1194 // Leave r4 live, it is used below.
1195 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1196 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
Diana Picus4f8c3e12017-01-13 09:37:56 +00001197 .addReg(ARM::R4)
1198 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001199 if (!isThumb)
Diana Picus8a73f552017-01-13 10:18:01 +00001200 MIB.add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001201
1202 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1203 // r4 holds the stack slot address.
1204 unsigned NextReg = ARM::D8;
1205
1206 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1207 // The writeback is only needed when emitting two vst1.64 instructions.
1208 if (NumAlignedDPRCS2Regs >= 6) {
1209 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001210 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001211 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001212 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), ARM::R4)
1213 .addReg(ARM::R4, RegState::Kill)
1214 .addImm(16)
1215 .addReg(NextReg)
1216 .addReg(SupReg, RegState::ImplicitKill)
1217 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001218 NextReg += 4;
1219 NumAlignedDPRCS2Regs -= 4;
1220 }
1221
1222 // We won't modify r4 beyond this point. It currently points to the next
1223 // register to be spilled.
1224 unsigned R4BaseReg = NextReg;
1225
1226 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1227 if (NumAlignedDPRCS2Regs >= 4) {
1228 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001229 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001230 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001231 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1232 .addReg(ARM::R4)
1233 .addImm(16)
1234 .addReg(NextReg)
1235 .addReg(SupReg, RegState::ImplicitKill)
1236 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001237 NextReg += 4;
1238 NumAlignedDPRCS2Regs -= 4;
1239 }
1240
1241 // 16-byte aligned vst1.64 with 2 d-regs.
1242 if (NumAlignedDPRCS2Regs >= 2) {
1243 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001244 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001245 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001246 BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
1247 .addReg(ARM::R4)
1248 .addImm(16)
1249 .addReg(SupReg)
1250 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001251 NextReg += 2;
1252 NumAlignedDPRCS2Regs -= 2;
1253 }
1254
1255 // Finally, use a vanilla vstr.64 for the odd last register.
1256 if (NumAlignedDPRCS2Regs) {
1257 MBB.addLiveIn(NextReg);
1258 // vstr.64 uses addrmode5 which has an offset scale of 4.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001259 BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1260 .addReg(NextReg)
1261 .addReg(ARM::R4)
1262 .addImm((NextReg - R4BaseReg) * 2)
1263 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001264 }
1265
1266 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001267 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001268}
1269
1270/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1271/// iterator to the following instruction.
1272static MachineBasicBlock::iterator
1273skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1274 unsigned NumAlignedDPRCS2Regs) {
1275 // sub r4, sp, #numregs * 8
1276 // bic r4, r4, #align - 1
1277 // mov sp, r4
1278 ++MI; ++MI; ++MI;
1279 assert(MI->mayStore() && "Expecting spill instruction");
1280
1281 // These switches all fall through.
1282 switch(NumAlignedDPRCS2Regs) {
1283 case 7:
1284 ++MI;
1285 assert(MI->mayStore() && "Expecting spill instruction");
Florian Hahndb479522017-07-27 14:37:17 +00001286 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001287 default:
1288 ++MI;
1289 assert(MI->mayStore() && "Expecting spill instruction");
Florian Hahndb479522017-07-27 14:37:17 +00001290 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001291 case 1:
1292 case 2:
1293 case 4:
1294 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1295 ++MI;
1296 }
1297 return MI;
1298}
1299
1300/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1301/// starting from d8. These instructions are assumed to execute while the
1302/// stack is still aligned, unlike the code inserted by emitPopInst.
1303static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1304 MachineBasicBlock::iterator MI,
1305 unsigned NumAlignedDPRCS2Regs,
1306 const std::vector<CalleeSavedInfo> &CSI,
1307 const TargetRegisterInfo *TRI) {
1308 MachineFunction &MF = *MBB.getParent();
1309 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001310 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001311 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001312
1313 // Find the frame index assigned to d8.
1314 int D8SpillFI = 0;
1315 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1316 if (CSI[i].getReg() == ARM::D8) {
1317 D8SpillFI = CSI[i].getFrameIdx();
1318 break;
1319 }
1320
1321 // Materialize the address of the d8 spill slot into the scratch register r4.
1322 // This can be fairly complicated if the stack frame is large, so just use
1323 // the normal frame index elimination mechanism to do it. This code runs as
1324 // the initial part of the epilog where the stack and base pointers haven't
1325 // been changed yet.
1326 bool isThumb = AFI->isThumbFunction();
1327 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1328
1329 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
Diana Picus8a73f552017-01-13 10:18:01 +00001330 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1331 .addFrameIndex(D8SpillFI)
1332 .addImm(0)
1333 .add(predOps(ARMCC::AL))
1334 .add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001335
1336 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1337 unsigned NextReg = ARM::D8;
1338
1339 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1340 if (NumAlignedDPRCS2Regs >= 6) {
1341 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001342 &ARM::QQPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001343 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1344 .addReg(ARM::R4, RegState::Define)
1345 .addReg(ARM::R4, RegState::Kill)
1346 .addImm(16)
1347 .addReg(SupReg, RegState::ImplicitDefine)
1348 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001349 NextReg += 4;
1350 NumAlignedDPRCS2Regs -= 4;
1351 }
1352
1353 // We won't modify r4 beyond this point. It currently points to the next
1354 // register to be spilled.
1355 unsigned R4BaseReg = NextReg;
1356
1357 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1358 if (NumAlignedDPRCS2Regs >= 4) {
1359 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001360 &ARM::QQPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001361 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1362 .addReg(ARM::R4)
1363 .addImm(16)
1364 .addReg(SupReg, RegState::ImplicitDefine)
1365 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001366 NextReg += 4;
1367 NumAlignedDPRCS2Regs -= 4;
1368 }
1369
1370 // 16-byte aligned vld1.64 with 2 d-regs.
1371 if (NumAlignedDPRCS2Regs >= 2) {
1372 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001373 &ARM::QPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001374 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1375 .addReg(ARM::R4)
1376 .addImm(16)
1377 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001378 NextReg += 2;
1379 NumAlignedDPRCS2Regs -= 2;
1380 }
1381
1382 // Finally, use a vanilla vldr.64 for the remaining odd register.
1383 if (NumAlignedDPRCS2Regs)
Diana Picus4f8c3e12017-01-13 09:37:56 +00001384 BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1385 .addReg(ARM::R4)
1386 .addImm(2 * (NextReg - R4BaseReg))
1387 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001388
1389 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001390 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001391}
1392
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001393bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001394 MachineBasicBlock::iterator MI,
1395 const std::vector<CalleeSavedInfo> &CSI,
1396 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001397 if (CSI.empty())
1398 return false;
1399
1400 MachineFunction &MF = *MBB.getParent();
1401 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001402
1403 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001404 unsigned PushOneOpc = AFI->isThumbFunction() ?
1405 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001406 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001407 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1408 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001409 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001410 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001411 MachineInstr::FrameSetup);
1412 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001413 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1414
1415 // The code above does not insert spill code for the aligned DPRCS2 registers.
1416 // The stack realignment code will be inserted between the push instructions
1417 // and these spills.
1418 if (NumAlignedDPRCS2Regs)
1419 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001420
1421 return true;
1422}
1423
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001424bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001425 MachineBasicBlock::iterator MI,
1426 const std::vector<CalleeSavedInfo> &CSI,
1427 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001428 if (CSI.empty())
1429 return false;
1430
1431 MachineFunction &MF = *MBB.getParent();
1432 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001433 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001434 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1435
1436 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1437 // registers. Do that here instead.
1438 if (NumAlignedDPRCS2Regs)
1439 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001440
1441 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001442 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001443 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001444 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1445 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001446 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001447 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001448 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001449 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001450
1451 return true;
1452}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001453
1454// FIXME: Make generic?
1455static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1456 const ARMBaseInstrInfo &TII) {
1457 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001458 for (auto &MBB : MF) {
1459 for (auto &MI : MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001460 FnSize += TII.getInstSizeInBytes(MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001461 }
1462 return FnSize;
1463}
1464
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001465/// estimateRSStackSizeLimit - Look at each instruction that references stack
1466/// frames and return the stack size limit beyond which some of these
1467/// instructions will require a scratch register during their expansion later.
1468// FIXME: Move to TII?
1469static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001470 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001471 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1472 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001473 for (auto &MBB : MF) {
1474 for (auto &MI : MBB) {
1475 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1476 if (!MI.getOperand(i).isFI())
1477 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001478
1479 // When using ADDri to get the address of a stack object, 255 is the
1480 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001481 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001482 Limit = std::min(Limit, (1U << 8) - 1);
1483 break;
1484 }
1485
1486 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001487 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001488 case ARMII::AddrMode3:
1489 case ARMII::AddrModeT2_i8:
1490 Limit = std::min(Limit, (1U << 8) - 1);
1491 break;
1492 case ARMII::AddrMode5:
1493 case ARMII::AddrModeT2_i8s4:
1494 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1495 break;
1496 case ARMII::AddrModeT2_i12:
1497 // i12 supports only positive offset so these will be converted to
1498 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1499 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1500 Limit = std::min(Limit, (1U << 8) - 1);
1501 break;
1502 case ARMII::AddrMode4:
1503 case ARMII::AddrMode6:
1504 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1505 // immediate offset for stack references.
1506 return 0;
1507 default:
1508 break;
1509 }
1510 break; // At most one FI per instruction
1511 }
1512 }
1513 }
1514
1515 return Limit;
1516}
1517
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001518// In functions that realign the stack, it can be an advantage to spill the
1519// callee-saved vector registers after realigning the stack. The vst1 and vld1
1520// instructions take alignment hints that can improve performance.
1521//
Matthias Braun02564862015-07-14 17:17:13 +00001522static void
1523checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001524 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1525 if (!SpillAlignedNEONRegs)
1526 return;
1527
1528 // Naked functions don't spill callee-saved registers.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001529 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001530 return;
1531
1532 // We are planning to use NEON instructions vst1 / vld1.
Eric Christopher1b21f002015-01-29 00:19:33 +00001533 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001534 return;
1535
1536 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopher1b21f002015-01-29 00:19:33 +00001537 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001538 return;
1539
1540 // Aligned spills require stack realignment.
Eric Christopher1b21f002015-01-29 00:19:33 +00001541 if (!static_cast<const ARMBaseRegisterInfo *>(
1542 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001543 return;
1544
1545 // We always spill contiguous d-registers starting from d8. Count how many
1546 // needs spilling. The register allocator will almost always use the
1547 // callee-saved registers in order, but it can happen that there are holes in
1548 // the range. Registers above the hole will be spilled to the standard DPRCS
1549 // area.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001550 unsigned NumSpills = 0;
1551 for (; NumSpills < 8; ++NumSpills)
Matthias Braun02564862015-07-14 17:17:13 +00001552 if (!SavedRegs.test(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001553 break;
1554
1555 // Don't do this for just one d-register. It's not worth it.
1556 if (NumSpills < 2)
1557 return;
1558
1559 // Spill the first NumSpills D-registers after realigning the stack.
1560 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1561
1562 // A scratch register is required for the vst1 / vld1 instructions.
Matthias Braun02564862015-07-14 17:17:13 +00001563 SavedRegs.set(ARM::R4);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001564}
1565
Matthias Braun02564862015-07-14 17:17:13 +00001566void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1567 BitVector &SavedRegs,
1568 RegScavenger *RS) const {
1569 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001570 // This tells PEI to spill the FP as if it is any other callee-save register
1571 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1572 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1573 // to combine multiple loads / stores.
1574 bool CanEliminateFrame = true;
1575 bool CS1Spilled = false;
1576 bool LRSpilled = false;
1577 unsigned NumGPRSpills = 0;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001578 unsigned NumFPRSpills = 0;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001579 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1580 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001581 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001582 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001583 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001584 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001585 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +00001586 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001587 MachineRegisterInfo &MRI = MF.getRegInfo();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001588 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1589 (void)TRI; // Silence unused warning in non-assert builds.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001590 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1591
1592 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1593 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001594 // since it's not always possible to restore sp from fp in a single
1595 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001596 // FIXME: It will be better just to find spare register here.
1597 if (AFI->isThumb2Function() &&
Matthias Braun941a7052016-07-28 18:40:00 +00001598 (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Matthias Braun02564862015-07-14 17:17:13 +00001599 SavedRegs.set(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001600
Evan Cheng572756a2011-01-16 05:14:33 +00001601 if (AFI->isThumb1OnlyFunction()) {
1602 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001603 if (AFI->getArgRegsSaveSize() > 0)
Matthias Braun02564862015-07-14 17:17:13 +00001604 SavedRegs.set(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001605
Jim Grosbachdca85312011-06-13 21:18:25 +00001606 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1607 // for sure what the stack size will be, but for this, an estimate is good
1608 // enough. If there anything changes it, it'll be a spill, which implies
1609 // we've used all the registers and so R4 is already used, so not marking
Chad Rosieradd38c12011-10-20 00:07:12 +00001610 // it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001611 // FIXME: It will be better just to find spare register here.
Matthias Braun941a7052016-07-28 18:40:00 +00001612 unsigned StackSize = MFI.estimateStackSize(MF);
1613 if (MFI.hasVarSizedObjects() || StackSize > 508)
Matthias Braun02564862015-07-14 17:17:13 +00001614 SavedRegs.set(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001615 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001616
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001617 // See if we can spill vector registers to aligned stack.
Matthias Braun02564862015-07-14 17:17:13 +00001618 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001619
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001620 // Spill the BasePtr if it's used.
1621 if (RegInfo->hasBasePointer(MF))
Matthias Braun02564862015-07-14 17:17:13 +00001622 SavedRegs.set(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001623
1624 // Don't spill FP if the frame can be eliminated. This is determined
Matthias Braun02564862015-07-14 17:17:13 +00001625 // by scanning the callee-save registers to see if any is modified.
Craig Topper840beec2014-04-04 05:16:06 +00001626 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001627 for (unsigned i = 0; CSRegs[i]; ++i) {
1628 unsigned Reg = CSRegs[i];
1629 bool Spilled = false;
Matthias Braun02564862015-07-14 17:17:13 +00001630 if (SavedRegs.test(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001631 Spilled = true;
1632 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001633 }
1634
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001635 if (!ARM::GPRRegClass.contains(Reg)) {
1636 if (Spilled) {
1637 if (ARM::SPRRegClass.contains(Reg))
1638 NumFPRSpills++;
1639 else if (ARM::DPRRegClass.contains(Reg))
1640 NumFPRSpills += 2;
1641 else if (ARM::QPRRegClass.contains(Reg))
1642 NumFPRSpills += 4;
1643 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001644 continue;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001645 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001646
1647 if (Spilled) {
1648 NumGPRSpills++;
1649
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001650 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001651 if (Reg == ARM::LR)
1652 LRSpilled = true;
1653 CS1Spilled = true;
1654 continue;
1655 }
1656
1657 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1658 switch (Reg) {
1659 case ARM::LR:
1660 LRSpilled = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00001661 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +00001662 case ARM::R0: case ARM::R1:
1663 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001664 case ARM::R4: case ARM::R5:
1665 case ARM::R6: case ARM::R7:
1666 CS1Spilled = true;
1667 break;
1668 default:
1669 break;
1670 }
1671 } else {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001672 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001673 UnspilledCS1GPRs.push_back(Reg);
1674 continue;
1675 }
1676
1677 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001678 case ARM::R0: case ARM::R1:
1679 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001680 case ARM::R4: case ARM::R5:
1681 case ARM::R6: case ARM::R7:
1682 case ARM::LR:
1683 UnspilledCS1GPRs.push_back(Reg);
1684 break;
1685 default:
1686 UnspilledCS2GPRs.push_back(Reg);
1687 break;
1688 }
1689 }
1690 }
1691
1692 bool ForceLRSpill = false;
1693 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1694 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1695 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1696 // use of BL to implement far jump. If it turns out that it's not needed
1697 // then the branch fix up path will undo it.
1698 if (FnSize >= (1 << 11)) {
1699 CanEliminateFrame = false;
1700 ForceLRSpill = true;
1701 }
1702 }
1703
1704 // If any of the stack slot references may be out of range of an immediate
1705 // offset, make sure a register (or a spill slot) is available for the
1706 // register scavenger. Note that if we're indexing off the frame pointer, the
1707 // effective stack size is 4 bytes larger since the FP points to the stack
1708 // slot of the previous FP. Also, if we have variable sized objects in the
1709 // function, stack slot references will often be negative, and some of
1710 // our instructions are positive-offset only, so conservatively consider
1711 // that case to want a spill slot (or register) as well. Similarly, if
1712 // the function adjusts the stack pointer during execution and the
1713 // adjustments aren't already part of our stack size estimate, our offset
1714 // calculations may be off, so be conservative.
1715 // FIXME: We could add logic to be more precise about negative offsets
1716 // and which instructions will need a scratch register for them. Is it
1717 // worth the effort and added fragility?
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001718 unsigned EstimatedStackSize =
Matthias Braun941a7052016-07-28 18:40:00 +00001719 MFI.estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001720
1721 // Determine biggest (positive) SP offset in MachineFrameInfo.
1722 int MaxFixedOffset = 0;
1723 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
1724 int MaxObjectOffset = MFI.getObjectOffset(I) + MFI.getObjectSize(I);
1725 MaxFixedOffset = std::max(MaxFixedOffset, MaxObjectOffset);
1726 }
1727
Matthias Braun44047422017-04-05 16:58:41 +00001728 bool HasFP = hasFP(MF);
1729 if (HasFP) {
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001730 if (AFI->hasStackFrame())
1731 EstimatedStackSize += 4;
1732 } else {
1733 // If FP is not used, SP will be used to access arguments, so count the
1734 // size of arguments into the estimation.
Matthias Braun8aaa3682017-04-19 21:11:44 +00001735 EstimatedStackSize += MaxFixedOffset;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001736 }
1737 EstimatedStackSize += 16; // For possible paddings.
1738
Matthias Braun8aaa3682017-04-19 21:11:44 +00001739 unsigned EstimatedRSStackSizeLimit = estimateRSStackSizeLimit(MF, this);
1740 int MaxFPOffset = getMaxFPOffset(*MF.getFunction(), *AFI);
1741 bool BigFrameOffsets = EstimatedStackSize >= EstimatedRSStackSizeLimit ||
1742 MFI.hasVarSizedObjects() ||
1743 (MFI.adjustsStack() && !canSimplifyCallFramePseudos(MF)) ||
1744 // For large argument stacks fp relative addressed may overflow.
1745 (HasFP && (MaxFixedOffset - MaxFPOffset) >= (int)EstimatedRSStackSizeLimit);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001746 if (BigFrameOffsets ||
1747 !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001748 AFI->setHasStackFrame(true);
1749
Matthias Braun44047422017-04-05 16:58:41 +00001750 if (HasFP) {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001751 SavedRegs.set(FramePtr);
1752 // If the frame pointer is required by the ABI, also spill LR so that we
1753 // emit a complete frame record.
1754 if (MF.getTarget().Options.DisableFramePointerElim(MF) && !LRSpilled) {
1755 SavedRegs.set(ARM::LR);
1756 LRSpilled = true;
1757 NumGPRSpills++;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001758 auto LRPos = llvm::find(UnspilledCS1GPRs, ARM::LR);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001759 if (LRPos != UnspilledCS1GPRs.end())
1760 UnspilledCS1GPRs.erase(LRPos);
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001761 }
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001762 auto FPPos = llvm::find(UnspilledCS1GPRs, FramePtr);
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001763 if (FPPos != UnspilledCS1GPRs.end())
1764 UnspilledCS1GPRs.erase(FPPos);
1765 NumGPRSpills++;
1766 if (FramePtr == ARM::R7)
1767 CS1Spilled = true;
1768 }
1769
Matthias Braunc618a462017-07-28 01:36:32 +00001770 // This is true when we inserted a spill for an unused register that can now
1771 // be used for register scavenging.
1772 bool ExtraCSSpill = false;
1773
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001774 if (AFI->isThumb1OnlyFunction()) {
1775 // For Thumb1-only targets, we need some low registers when we save and
1776 // restore the high registers (which aren't allocatable, but could be
1777 // used by inline assembly) because the push/pop instructions can not
1778 // access high registers. If necessary, we might need to push more low
1779 // registers to ensure that there is at least one free that can be used
1780 // for the saving & restoring, and preferably we should ensure that as
1781 // many as are needed are available so that fewer push/pop instructions
1782 // are required.
1783
1784 // Low registers which are not currently pushed, but could be (r4-r7).
1785 SmallVector<unsigned, 4> AvailableRegs;
1786
1787 // Unused argument registers (r0-r3) can be clobbered in the prologue for
1788 // free.
1789 int EntryRegDeficit = 0;
1790 for (unsigned Reg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3}) {
1791 if (!MF.getRegInfo().isLiveIn(Reg)) {
1792 --EntryRegDeficit;
1793 DEBUG(dbgs() << PrintReg(Reg, TRI)
1794 << " is unused argument register, EntryRegDeficit = "
1795 << EntryRegDeficit << "\n");
1796 }
1797 }
1798
1799 // Unused return registers can be clobbered in the epilogue for free.
1800 int ExitRegDeficit = AFI->getReturnRegsCount() - 4;
1801 DEBUG(dbgs() << AFI->getReturnRegsCount()
1802 << " return regs used, ExitRegDeficit = " << ExitRegDeficit
1803 << "\n");
1804
1805 int RegDeficit = std::max(EntryRegDeficit, ExitRegDeficit);
1806 DEBUG(dbgs() << "RegDeficit = " << RegDeficit << "\n");
1807
1808 // r4-r6 can be used in the prologue if they are pushed by the first push
1809 // instruction.
1810 for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6}) {
1811 if (SavedRegs.test(Reg)) {
1812 --RegDeficit;
1813 DEBUG(dbgs() << PrintReg(Reg, TRI)
1814 << " is saved low register, RegDeficit = " << RegDeficit
1815 << "\n");
1816 } else {
1817 AvailableRegs.push_back(Reg);
1818 DEBUG(dbgs()
1819 << PrintReg(Reg, TRI)
1820 << " is non-saved low register, adding to AvailableRegs\n");
1821 }
1822 }
1823
1824 // r7 can be used if it is not being used as the frame pointer.
Matthias Braun44047422017-04-05 16:58:41 +00001825 if (!HasFP) {
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001826 if (SavedRegs.test(ARM::R7)) {
1827 --RegDeficit;
1828 DEBUG(dbgs() << "%R7 is saved low register, RegDeficit = "
1829 << RegDeficit << "\n");
1830 } else {
1831 AvailableRegs.push_back(ARM::R7);
1832 DEBUG(dbgs()
1833 << "%R7 is non-saved low register, adding to AvailableRegs\n");
1834 }
1835 }
1836
1837 // Each of r8-r11 needs to be copied to a low register, then pushed.
1838 for (unsigned Reg : {ARM::R8, ARM::R9, ARM::R10, ARM::R11}) {
1839 if (SavedRegs.test(Reg)) {
1840 ++RegDeficit;
1841 DEBUG(dbgs() << PrintReg(Reg, TRI)
1842 << " is saved high register, RegDeficit = " << RegDeficit
1843 << "\n");
1844 }
1845 }
1846
1847 // LR can only be used by PUSH, not POP, and can't be used at all if the
1848 // llvm.returnaddress intrinsic is used. This is only worth doing if we
1849 // are more limited at function entry than exit.
1850 if ((EntryRegDeficit > ExitRegDeficit) &&
1851 !(MF.getRegInfo().isLiveIn(ARM::LR) &&
1852 MF.getFrameInfo().isReturnAddressTaken())) {
1853 if (SavedRegs.test(ARM::LR)) {
1854 --RegDeficit;
1855 DEBUG(dbgs() << "%LR is saved register, RegDeficit = " << RegDeficit
1856 << "\n");
1857 } else {
1858 AvailableRegs.push_back(ARM::LR);
1859 DEBUG(dbgs() << "%LR is not saved, adding to AvailableRegs\n");
1860 }
1861 }
1862
1863 // If there are more high registers that need pushing than low registers
1864 // available, push some more low registers so that we can use fewer push
1865 // instructions. This might not reduce RegDeficit all the way to zero,
1866 // because we can only guarantee that r4-r6 are available, but r8-r11 may
1867 // need saving.
1868 DEBUG(dbgs() << "Final RegDeficit = " << RegDeficit << "\n");
1869 for (; RegDeficit > 0 && !AvailableRegs.empty(); --RegDeficit) {
1870 unsigned Reg = AvailableRegs.pop_back_val();
1871 DEBUG(dbgs() << "Spilling " << PrintReg(Reg, TRI)
1872 << " to make up reg deficit\n");
1873 SavedRegs.set(Reg);
1874 NumGPRSpills++;
1875 CS1Spilled = true;
Matthias Braunc618a462017-07-28 01:36:32 +00001876 assert(!MRI.isReserved(Reg) && "Should not be reserved");
1877 if (!MRI.isPhysRegUsed(Reg))
1878 ExtraCSSpill = true;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001879 UnspilledCS1GPRs.erase(llvm::find(UnspilledCS1GPRs, Reg));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001880 if (Reg == ARM::LR)
1881 LRSpilled = true;
1882 }
1883 DEBUG(dbgs() << "After adding spills, RegDeficit = " << RegDeficit << "\n");
1884 }
1885
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001886 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1887 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1888 if (!LRSpilled && CS1Spilled) {
Matthias Braun02564862015-07-14 17:17:13 +00001889 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001890 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001891 SmallVectorImpl<unsigned>::iterator LRPos;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001892 LRPos = llvm::find(UnspilledCS1GPRs, (unsigned)ARM::LR);
Tim Northoverd8407452013-10-01 14:33:28 +00001893 if (LRPos != UnspilledCS1GPRs.end())
1894 UnspilledCS1GPRs.erase(LRPos);
1895
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001896 ForceLRSpill = false;
Matthias Braunc618a462017-07-28 01:36:32 +00001897 if (!MRI.isReserved(ARM::LR) && !MRI.isPhysRegUsed(ARM::LR))
1898 ExtraCSSpill = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001899 }
1900
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001901 // If stack and double are 8-byte aligned and we are spilling an odd number
1902 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1903 // the integer and double callee save areas.
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001904 DEBUG(dbgs() << "NumGPRSpills = " << NumGPRSpills << "\n");
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001905 unsigned TargetAlign = getStackAlignment();
Tim Northoverdc0d9e42014-11-05 00:27:20 +00001906 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001907 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1908 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1909 unsigned Reg = UnspilledCS1GPRs[i];
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001910 // Don't spill high register if the function is thumb. In the case of
1911 // Windows on ARM, accept R11 (frame pointer)
Peter Collingbourne78f1ecc2015-04-23 20:31:26 +00001912 if (!AFI->isThumbFunction() ||
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001913 (STI.isTargetWindows() && Reg == ARM::R11) ||
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001914 isARMLowRegister(Reg) || Reg == ARM::LR) {
Matthias Braun02564862015-07-14 17:17:13 +00001915 SavedRegs.set(Reg);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001916 DEBUG(dbgs() << "Spilling " << PrintReg(Reg, TRI)
1917 << " to make up alignment\n");
Matthias Braunc618a462017-07-28 01:36:32 +00001918 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001919 ExtraCSSpill = true;
1920 break;
1921 }
1922 }
1923 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1924 unsigned Reg = UnspilledCS2GPRs.front();
Matthias Braun02564862015-07-14 17:17:13 +00001925 SavedRegs.set(Reg);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001926 DEBUG(dbgs() << "Spilling " << PrintReg(Reg, TRI)
1927 << " to make up alignment\n");
Matthias Braunc618a462017-07-28 01:36:32 +00001928 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001929 ExtraCSSpill = true;
1930 }
1931 }
1932
1933 // Estimate if we might need to scavenge a register at some point in order
1934 // to materialize a stack offset. If so, either spill one additional
1935 // callee-saved register or reserve a special spill slot to facilitate
1936 // register scavenging. Thumb1 needs a spill slot for stack pointer
1937 // adjustments also, even when the frame itself is small.
Matthias Braun8aaa3682017-04-19 21:11:44 +00001938 if (BigFrameOffsets && !ExtraCSSpill) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001939 // If any non-reserved CS register isn't spilled, just spill one or two
1940 // extra. That should take care of it!
1941 unsigned NumExtras = TargetAlign / 4;
1942 SmallVector<unsigned, 2> Extras;
1943 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1944 unsigned Reg = UnspilledCS1GPRs.back();
1945 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001946 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001947 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1948 Reg == ARM::LR)) {
1949 Extras.push_back(Reg);
1950 NumExtras--;
1951 }
1952 }
1953 // For non-Thumb1 functions, also check for hi-reg CS registers
1954 if (!AFI->isThumb1OnlyFunction()) {
1955 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1956 unsigned Reg = UnspilledCS2GPRs.back();
1957 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001958 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001959 Extras.push_back(Reg);
1960 NumExtras--;
1961 }
1962 }
1963 }
Matthias Braunc618a462017-07-28 01:36:32 +00001964 if (NumExtras == 0) {
1965 for (unsigned Reg : Extras) {
1966 SavedRegs.set(Reg);
1967 if (!MRI.isPhysRegUsed(Reg))
1968 ExtraCSSpill = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001969 }
Matthias Braunc618a462017-07-28 01:36:32 +00001970 }
1971 if (!ExtraCSSpill && !AFI->isThumb1OnlyFunction()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001972 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1973 // closest to SP or frame pointer.
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001974 assert(RS && "Register scavenging not provided");
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001975 const TargetRegisterClass &RC = ARM::GPRRegClass;
1976 unsigned Size = TRI->getSpillSize(RC);
1977 unsigned Align = TRI->getSpillAlignment(RC);
1978 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001979 }
1980 }
1981 }
1982
1983 if (ForceLRSpill) {
Matthias Braun02564862015-07-14 17:17:13 +00001984 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001985 AFI->setLRIsSpilledForFarJump(true);
1986 }
1987}
Eli Bendersky8da87162013-02-21 20:05:00 +00001988
Hans Wennborge1a2e902016-03-31 18:33:38 +00001989MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
1990 MachineFunction &MF, MachineBasicBlock &MBB,
1991 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001992 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001993 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001994 if (!hasReservedCallFrame(MF)) {
1995 // If we have alloca, convert as follows:
1996 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1997 // ADJCALLSTACKUP -> add, sp, sp, amount
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001998 MachineInstr &Old = *I;
1999 DebugLoc dl = Old.getDebugLoc();
Serge Pavlov5943a962017-04-19 03:12:05 +00002000 unsigned Amount = TII.getFrameSize(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +00002001 if (Amount != 0) {
2002 // We need to keep the stack aligned properly. To do this, we round the
2003 // amount of space needed for the outgoing arguments up to the next
2004 // alignment boundary.
Guozhi Weif66d3842015-08-17 22:36:27 +00002005 Amount = alignSPAdjust(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +00002006
2007 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2008 assert(!AFI->isThumb1OnlyFunction() &&
2009 "This eliminateCallFramePseudoInstr does not support Thumb1!");
2010 bool isARM = !AFI->isThumbFunction();
2011
2012 // Replace the pseudo instruction with a new instruction...
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00002013 unsigned Opc = Old.getOpcode();
2014 int PIdx = Old.findFirstPredOperandIdx();
2015 ARMCC::CondCodes Pred =
2016 (PIdx == -1) ? ARMCC::AL
2017 : (ARMCC::CondCodes)Old.getOperand(PIdx).getImm();
Serge Pavlov5943a962017-04-19 03:12:05 +00002018 unsigned PredReg = TII.getFramePred(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +00002019 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
Eli Bendersky8da87162013-02-21 20:05:00 +00002020 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
2021 Pred, PredReg);
2022 } else {
Eli Bendersky8da87162013-02-21 20:05:00 +00002023 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
2024 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
2025 Pred, PredReg);
2026 }
2027 }
2028 }
Hans Wennborge1a2e902016-03-31 18:33:38 +00002029 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00002030}
2031
Oliver Stannardb14c6252014-04-02 16:10:33 +00002032/// Get the minimum constant for ARM that is greater than or equal to the
2033/// argument. In ARM, constants can have any value that can be produced by
2034/// rotating an 8-bit value to the right by an even number of bits within a
2035/// 32-bit word.
2036static uint32_t alignToARMConstant(uint32_t Value) {
2037 unsigned Shifted = 0;
2038
2039 if (Value == 0)
2040 return 0;
2041
2042 while (!(Value & 0xC0000000)) {
2043 Value = Value << 2;
2044 Shifted += 2;
2045 }
2046
2047 bool Carry = (Value & 0x00FFFFFF);
2048 Value = ((Value & 0xFF000000) >> 24) + Carry;
2049
2050 if (Value & 0x0000100)
2051 Value = Value & 0x000001FC;
2052
2053 if (Shifted > 24)
2054 Value = Value >> (Shifted - 24);
2055 else
2056 Value = Value << (24 - Shifted);
2057
2058 return Value;
2059}
2060
2061// The stack limit in the TCB is set to this many bytes above the actual
2062// stack limit.
2063static const uint64_t kSplitStackAvailable = 256;
2064
2065// Adjust the function prologue to enable split stacks. This currently only
2066// supports android and linux.
2067//
2068// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
2069// must be well defined in order to allow for consistent implementations of the
2070// __morestack helper function. The ABI is also not a normal ABI in that it
2071// doesn't follow the normal calling conventions because this allows the
2072// prologue of each function to be optimized further.
2073//
2074// Currently, the ABI looks like (when calling __morestack)
2075//
2076// * r4 holds the minimum stack size requested for this function call
2077// * r5 holds the stack size of the arguments to the function
2078// * the beginning of the function is 3 instructions after the call to
2079// __morestack
2080//
2081// Implementations of __morestack should use r4 to allocate a new stack, r5 to
2082// place the arguments on to the new stack, and the 3-instruction knowledge to
2083// jump directly to the body of the function when working on the new stack.
2084//
2085// An old (and possibly no longer compatible) implementation of __morestack for
2086// ARM can be found at [1].
2087//
2088// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
Quentin Colombet61b305e2015-05-05 17:38:16 +00002089void ARMFrameLowering::adjustForSegmentedStacks(
2090 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
Oliver Stannardb14c6252014-04-02 16:10:33 +00002091 unsigned Opcode;
2092 unsigned CFIIndex;
Eric Christopher22b2ad22015-02-20 08:24:37 +00002093 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
Oliver Stannardb14c6252014-04-02 16:10:33 +00002094 bool Thumb = ST->isThumb();
2095
2096 // Sadly, this currently doesn't support varargs, platforms other than
2097 // android/linux. Note that thumb1/thumb2 are support for android/linux.
2098 if (MF.getFunction()->isVarArg())
2099 report_fatal_error("Segmented stacks do not support vararg functions.");
2100 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00002101 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00002102
Matthias Braun941a7052016-07-28 18:40:00 +00002103 MachineFrameInfo &MFI = MF.getFrameInfo();
Oliver Stannardb14c6252014-04-02 16:10:33 +00002104 MachineModuleInfo &MMI = MF.getMMI();
2105 MCContext &Context = MMI.getContext();
2106 const MCRegisterInfo *MRI = Context.getRegisterInfo();
2107 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00002108 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002109 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
2110 DebugLoc DL;
2111
Matthias Braun941a7052016-07-28 18:40:00 +00002112 uint64_t StackSize = MFI.getStackSize();
Tim Northoverf9e798b2014-05-22 13:03:43 +00002113
2114 // Do not generate a prologue for functions with a stack of size zero
2115 if (StackSize == 0)
2116 return;
2117
Oliver Stannardb14c6252014-04-02 16:10:33 +00002118 // Use R4 and R5 as scratch registers.
2119 // We save R4 and R5 before use and restore them before leaving the function.
2120 unsigned ScratchReg0 = ARM::R4;
2121 unsigned ScratchReg1 = ARM::R5;
2122 uint64_t AlignedStackSize;
2123
2124 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
2125 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
2126 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
2127 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
2128 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
2129
Quentin Colombet71a71482015-07-20 21:42:14 +00002130 // Grab everything that reaches PrologueMBB to update there liveness as well.
2131 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
2132 SmallVector<MachineBasicBlock *, 2> WalkList;
2133 WalkList.push_back(&PrologueMBB);
2134
2135 do {
2136 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
2137 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
2138 if (BeforePrologueRegion.insert(PredBB).second)
2139 WalkList.push_back(PredBB);
2140 }
2141 } while (!WalkList.empty());
2142
2143 // The order in that list is important.
2144 // The blocks will all be inserted before PrologueMBB using that order.
2145 // Therefore the block that should appear first in the CFG should appear
2146 // first in the list.
2147 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
2148 PostStackMBB};
Quentin Colombet71a71482015-07-20 21:42:14 +00002149
Craig Topper80720812015-12-01 06:13:01 +00002150 for (MachineBasicBlock *B : AddedBlocks)
2151 BeforePrologueRegion.insert(B);
Quentin Colombet71a71482015-07-20 21:42:14 +00002152
Matthias Braund9da1622015-09-09 18:08:03 +00002153 for (const auto &LI : PrologueMBB.liveins()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00002154 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
Matthias Braunb2b7ef12015-08-24 22:59:52 +00002155 PredBB->addLiveIn(LI);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002156 }
2157
Quentin Colombet71a71482015-07-20 21:42:14 +00002158 // Remove the newly added blocks from the list, since we know
2159 // we do not have to do the following updates for them.
Craig Topper80720812015-12-01 06:13:01 +00002160 for (MachineBasicBlock *B : AddedBlocks) {
2161 BeforePrologueRegion.erase(B);
2162 MF.insert(PrologueMBB.getIterator(), B);
Quentin Colombet71a71482015-07-20 21:42:14 +00002163 }
2164
2165 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
2166 // Make sure the LiveIns are still sorted and unique.
2167 MBB->sortUniqueLiveIns();
2168 // Replace the edges to PrologueMBB by edges to the sequences
2169 // we are about to add.
2170 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
2171 }
Oliver Stannardb14c6252014-04-02 16:10:33 +00002172
2173 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00002174 AlignedStackSize = alignToARMConstant(StackSize);
2175
2176 // When the frame size is less than 256 we just compare the stack
2177 // boundary directly to the value of the stack pointer, per gcc.
2178 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
2179
2180 // We will use two of the callee save registers as scratch registers so we
2181 // need to save those registers onto the stack.
2182 // We will use SR0 to hold stack limit and SR1 to hold the stack size
2183 // requested and arguments for __morestack().
2184 // SR0: Scratch Register #0
2185 // SR1: Scratch Register #1
2186 // push {SR0, SR1}
2187 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002188 BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))
2189 .add(predOps(ARMCC::AL))
2190 .addReg(ScratchReg0)
2191 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002192 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002193 BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
2194 .addReg(ARM::SP, RegState::Define)
2195 .addReg(ARM::SP)
2196 .add(predOps(ARMCC::AL))
2197 .addReg(ScratchReg0)
2198 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002199 }
2200
2201 // Emit the relevant DWARF information about the change in stack pointer as
2202 // well as where to find both r4 and r5 (the callee-save registers)
2203 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00002204 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002205 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2206 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002207 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002208 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
2209 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2210 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002211 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002212 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
2213 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2214 .addCFIIndex(CFIIndex);
2215
2216 // mov SR1, sp
2217 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002218 BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
2219 .addReg(ARM::SP)
2220 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002221 } else if (CompareStackPointer) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002222 BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
2223 .addReg(ARM::SP)
2224 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002225 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002226 }
2227
2228 // sub SR1, sp, #StackSize
2229 if (!CompareStackPointer && Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002230 BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)
2231 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002232 .addReg(ScratchReg1)
2233 .addImm(AlignedStackSize)
2234 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002235 } else if (!CompareStackPointer) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002236 BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
2237 .addReg(ARM::SP)
2238 .addImm(AlignedStackSize)
2239 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002240 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002241 }
2242
2243 if (Thumb && ST->isThumb1Only()) {
2244 unsigned PCLabelId = ARMFI->createPICLabelUId();
2245 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Oliver Stannard92e0fc02014-04-03 08:45:16 +00002246 MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002247 MachineConstantPool *MCP = MF.getConstantPool();
Tim Northover956b0082015-10-02 18:07:13 +00002248 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002249
2250 // ldr SR0, [pc, offset(STACK_LIMIT)]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002251 BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
2252 .addConstantPoolIndex(CPI)
2253 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002254
2255 // ldr SR0, [SR0]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002256 BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
2257 .addReg(ScratchReg0)
2258 .addImm(0)
2259 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002260 } else {
2261 // Get TLS base address from the coprocessor
2262 // mrc p15, #0, SR0, c13, c0, #3
Diana Picus4f8c3e12017-01-13 09:37:56 +00002263 BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2264 .addImm(15)
2265 .addImm(0)
2266 .addImm(13)
2267 .addImm(0)
2268 .addImm(3)
2269 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002270
2271 // Use the last tls slot on android and a private field of the TCP on linux.
2272 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2273 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2274
2275 // Get the stack limit from the right offset
2276 // ldr SR0, [sr0, #4 * TlsOffset]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002277 BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2278 .addReg(ScratchReg0)
2279 .addImm(4 * TlsOffset)
2280 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002281 }
2282
2283 // Compare stack limit with stack size requested.
2284 // cmp SR0, SR1
2285 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
Diana Picus4f8c3e12017-01-13 09:37:56 +00002286 BuildMI(GetMBB, DL, TII.get(Opcode))
2287 .addReg(ScratchReg0)
2288 .addReg(ScratchReg1)
2289 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002290
2291 // This jump is taken if StackLimit < SP - stack required.
2292 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2293 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2294 .addImm(ARMCC::LO)
2295 .addReg(ARM::CPSR);
2296
2297
2298 // Calling __morestack(StackSize, Size of stack arguments).
2299 // __morestack knows that the stack size requested is in SR0(r4)
2300 // and amount size of stack arguments is in SR1(r5).
2301
2302 // Pass first argument for the __morestack by Scratch Register #0.
2303 // The amount size of stack required
2304 if (Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002305 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg0)
2306 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002307 .addImm(AlignedStackSize)
2308 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002309 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002310 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2311 .addImm(AlignedStackSize)
2312 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002313 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002314 }
2315 // Pass second argument for the __morestack by Scratch Register #1.
2316 // The amount size of stack consumed to save function arguments.
2317 if (Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002318 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)
2319 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002320 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2321 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002322 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002323 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2324 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2325 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002326 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002327 }
2328
2329 // push {lr} - Save return address of this function.
2330 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002331 BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))
2332 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002333 .addReg(ARM::LR);
2334 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002335 BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2336 .addReg(ARM::SP, RegState::Define)
2337 .addReg(ARM::SP)
2338 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002339 .addReg(ARM::LR);
2340 }
2341
2342 // Emit the DWARF info about the change in stack as well as where to find the
2343 // previous link register
2344 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00002345 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002346 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2347 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002348 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002349 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2350 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2351 .addCFIIndex(CFIIndex);
2352
2353 // Call __morestack().
2354 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002355 BuildMI(AllocMBB, DL, TII.get(ARM::tBL))
2356 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002357 .addExternalSymbol("__morestack");
2358 } else {
2359 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2360 .addExternalSymbol("__morestack");
2361 }
2362
2363 // pop {lr} - Restore return address of this original function.
2364 if (Thumb) {
2365 if (ST->isThumb1Only()) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002366 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2367 .add(predOps(ARMCC::AL))
2368 .addReg(ScratchReg0);
2369 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2370 .addReg(ScratchReg0)
2371 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002372 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002373 BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2374 .addReg(ARM::LR, RegState::Define)
2375 .addReg(ARM::SP, RegState::Define)
2376 .addReg(ARM::SP)
2377 .addImm(4)
2378 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002379 }
2380 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002381 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2382 .addReg(ARM::SP, RegState::Define)
2383 .addReg(ARM::SP)
2384 .add(predOps(ARMCC::AL))
2385 .addReg(ARM::LR);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002386 }
2387
2388 // Restore SR0 and SR1 in case of __morestack() was called.
2389 // __morestack() will skip PostStackMBB block so we need to restore
2390 // scratch registers from here.
2391 // pop {SR0, SR1}
2392 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002393 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2394 .add(predOps(ARMCC::AL))
2395 .addReg(ScratchReg0)
2396 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002397 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002398 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2399 .addReg(ARM::SP, RegState::Define)
2400 .addReg(ARM::SP)
2401 .add(predOps(ARMCC::AL))
2402 .addReg(ScratchReg0)
2403 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002404 }
2405
2406 // Update the CFA offset now that we've popped
Matthias Braunf23ef432016-11-30 23:48:42 +00002407 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002408 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2409 .addCFIIndex(CFIIndex);
2410
2411 // bx lr - Return from this function.
2412 Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
Diana Picus4f8c3e12017-01-13 09:37:56 +00002413 BuildMI(AllocMBB, DL, TII.get(Opcode)).add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002414
2415 // Restore SR0 and SR1 in case of __morestack() was not called.
2416 // pop {SR0, SR1}
2417 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002418 BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))
2419 .add(predOps(ARMCC::AL))
2420 .addReg(ScratchReg0)
2421 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002422 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002423 BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2424 .addReg(ARM::SP, RegState::Define)
2425 .addReg(ARM::SP)
2426 .add(predOps(ARMCC::AL))
2427 .addReg(ScratchReg0)
2428 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002429 }
2430
2431 // Update the CFA offset now that we've popped
Matthias Braunf23ef432016-11-30 23:48:42 +00002432 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002433 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2434 .addCFIIndex(CFIIndex);
2435
2436 // Tell debuggers that r4 and r5 are now the same as they were in the
2437 // previous function, that they're the "Same Value".
Matthias Braunf23ef432016-11-30 23:48:42 +00002438 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002439 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2440 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2441 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002442 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002443 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2444 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2445 .addCFIIndex(CFIIndex);
2446
2447 // Organizing MBB lists
Quentin Colombet61b305e2015-05-05 17:38:16 +00002448 PostStackMBB->addSuccessor(&PrologueMBB);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002449
2450 AllocMBB->addSuccessor(PostStackMBB);
2451
2452 GetMBB->addSuccessor(PostStackMBB);
2453 GetMBB->addSuccessor(AllocMBB);
2454
2455 McrMBB->addSuccessor(GetMBB);
2456
2457 PrevStackMBB->addSuccessor(McrMBB);
2458
Filipe Cabecinhas0da99372016-04-29 15:22:48 +00002459#ifdef EXPENSIVE_CHECKS
Oliver Stannardb14c6252014-04-02 16:10:33 +00002460 MF.verify();
2461#endif
2462}