blob: f9d2b606987077d70f532dbe5a8f2504892666bb [file] [log] [blame]
Eugene Zelenko076468c2017-09-20 21:35:51 +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"
Eugene Zelenko076468c2017-09-20 21:35:51 +000022#include "Utils/ARMBaseInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000023#include "llvm/ADT/BitVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000031#include "llvm/CodeGen/MachineInstr.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000034#include "llvm/CodeGen/MachineOperand.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000036#include "llvm/CodeGen/RegisterScavenging.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetOpcodes.h"
39#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000041#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/CallingConv.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000043#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/Function.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000045#include "llvm/MC/MCContext.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000046#include "llvm/MC/MCDwarf.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000047#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000048#include "llvm/MC/MCRegisterInfo.h"
49#include "llvm/Support/CodeGen.h"
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000050#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000051#include "llvm/Support/Compiler.h"
52#include "llvm/Support/Debug.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/MathExtras.h"
55#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000056#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000057#include "llvm/Target/TargetOptions.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000058#include <algorithm>
59#include <cassert>
60#include <cstddef>
61#include <cstdint>
62#include <iterator>
63#include <utility>
64#include <vector>
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000065
Reid Klecknerbdfc05f2016-10-11 21:14:03 +000066#define DEBUG_TYPE "arm-frame-lowering"
67
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000068using namespace llvm;
69
Benjamin Kramer9fceb902012-02-24 22:09:25 +000070static cl::opt<bool>
Jakob Stoklund Olesen68a922c2012-01-06 22:19:37 +000071SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000072 cl::desc("Align ARM NEON spills in prolog and epilog"));
73
74static MachineBasicBlock::iterator
75skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
76 unsigned NumAlignedDPRCS2Regs);
77
Eric Christopher45fb7b62014-06-26 19:29:59 +000078ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
79 : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
80 STI(sti) {}
81
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000082bool ARMFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
83 // iOS always has a FP for backtracking, force other targets to keep their FP
84 // when doing FastISel. The emitted code is currently superior, and in cases
85 // like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
86 return TargetFrameLowering::noFramePointerElim(MF) ||
87 MF.getSubtarget<ARMSubtarget>().useFastISel();
88}
89
Tim Northoverb30388b2018-04-05 14:26:06 +000090/// Returns true if the target can safely skip saving callee-saved registers
91/// for noreturn nounwind functions.
92bool ARMFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
93 assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
94 MF.getFunction().hasFnAttribute(Attribute::NoUnwind));
95
96 // Frame pointer and link register are not treated as normal CSR, thus we
97 // can always skip CSR saves for nonreturning functions.
98 return true;
99}
100
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000101/// hasFP - Return true if the specified function should have a dedicated frame
102/// pointer register. This is true if the function has variable sized allocas
103/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000104bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +0000105 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000106 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000107
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000108 // ABI-required frame pointer.
109 if (MF.getTarget().Options.DisableFramePointerElim(MF))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000110 return true;
111
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000112 // Frame pointer required for use within this function.
113 return (RegInfo->needsStackRealignment(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000114 MFI.hasVarSizedObjects() ||
115 MFI.isFrameAddressTaken());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000116}
117
Bob Wilson657f2272011-01-13 21:10:12 +0000118/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
119/// not required, we reserve argument space for call sites in the function
120/// immediately on entry to the current function. This eliminates the need for
121/// add/sub sp brackets around call sites. Returns true if the call frame is
122/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000123bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000124 const MachineFrameInfo &MFI = MF.getFrameInfo();
125 unsigned CFSize = MFI.getMaxCallFrameSize();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000126 // It's not always a good idea to include the call frame as part of the
127 // stack frame. ARM (especially Thumb) has small immediate offset to
128 // address the stack frame. So a large call frame can cause poor codegen
129 // and may even makes it impossible to scavenge a register.
130 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
131 return false;
132
Matthias Braun941a7052016-07-28 18:40:00 +0000133 return !MFI.hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000134}
135
Bob Wilson657f2272011-01-13 21:10:12 +0000136/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
137/// call frame pseudos can be simplified. Unlike most targets, having a FP
138/// is not sufficient here since we still may reference some objects via SP
139/// even when FP is available in Thumb2 mode.
140bool
141ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000142 return hasReservedCallFrame(MF) || MF.getFrameInfo().hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000143}
144
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000145static bool isCSRestore(MachineInstr &MI, const ARMBaseInstrInfo &TII,
Craig Topper840beec2014-04-04 05:16:06 +0000146 const MCPhysReg *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000147 // Integer spill area is handled with "pop".
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000148 if (isPopOpcode(MI.getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000149 // The first two operands are predicates. The last two are
150 // imp-def and imp-use of SP. Check everything in between.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000151 for (int i = 5, e = MI.getNumOperands(); i != e; ++i)
152 if (!isCalleeSavedRegister(MI.getOperand(i).getReg(), CSRegs))
Eric Christopherb006fc92010-11-18 19:40:05 +0000153 return false;
154 return true;
155 }
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000156 if ((MI.getOpcode() == ARM::LDR_POST_IMM ||
157 MI.getOpcode() == ARM::LDR_POST_REG ||
158 MI.getOpcode() == ARM::t2LDR_POST) &&
159 isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs) &&
160 MI.getOperand(1).getReg() == ARM::SP)
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000161 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000162
163 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000164}
165
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000166static void emitRegPlusImmediate(
167 bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
168 const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
169 unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
170 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000171 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000172 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000173 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000174 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000175 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000176 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000177}
178
Tim Northoverc9432eb2013-11-04 23:04:15 +0000179static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000180 MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
Tim Northoverc9432eb2013-11-04 23:04:15 +0000181 const ARMBaseInstrInfo &TII, int NumBytes,
182 unsigned MIFlags = MachineInstr::NoFlags,
183 ARMCC::CondCodes Pred = ARMCC::AL,
184 unsigned PredReg = 0) {
185 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
186 MIFlags, Pred, PredReg);
187}
188
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000189static int sizeOfSPAdjustment(const MachineInstr &MI) {
Tim Northover603d3162014-11-14 22:45:33 +0000190 int RegSize;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000191 switch (MI.getOpcode()) {
Tim Northover603d3162014-11-14 22:45:33 +0000192 case ARM::VSTMDDB_UPD:
193 RegSize = 8;
194 break;
195 case ARM::STMDB_UPD:
196 case ARM::t2STMDB_UPD:
197 RegSize = 4;
198 break;
199 case ARM::t2STR_PRE:
200 case ARM::STR_PRE_IMM:
201 return 4;
202 default:
203 llvm_unreachable("Unknown push or pop like instruction");
204 }
205
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000206 int count = 0;
207 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
208 // pred) so the list starts at 4.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000209 for (int i = MI.getNumOperands() - 1; i >= 4; --i)
Tim Northover603d3162014-11-14 22:45:33 +0000210 count += RegSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000211 return count;
212}
213
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000214static bool WindowsRequiresStackProbe(const MachineFunction &MF,
215 size_t StackSizeInBytes) {
Matthias Braun941a7052016-07-28 18:40:00 +0000216 const MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000217 const Function &F = MF.getFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000218 unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
Matthias Braunf1caa282017-12-15 22:22:58 +0000219 if (F.hasFnAttribute("stack-probe-size"))
220 F.getFnAttribute("stack-probe-size")
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000221 .getValueAsString()
222 .getAsInteger(0, StackProbeSize);
Hans Wennborg89c35fc2018-02-23 13:46:25 +0000223 return (StackSizeInBytes >= StackProbeSize) &&
224 !F.hasFnAttribute("no-stack-arg-probe");
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000225}
226
Tim Northover603d3162014-11-14 22:45:33 +0000227namespace {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000228
Tim Northover603d3162014-11-14 22:45:33 +0000229struct StackAdjustingInsts {
230 struct InstInfo {
231 MachineBasicBlock::iterator I;
232 unsigned SPAdjust;
233 bool BeforeFPSet;
234 };
235
236 SmallVector<InstInfo, 4> Insts;
237
238 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
239 bool BeforeFPSet = false) {
240 InstInfo Info = {I, SPAdjust, BeforeFPSet};
241 Insts.push_back(Info);
242 }
243
244 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000245 auto Info =
246 llvm::find_if(Insts, [&](InstInfo &Info) { return Info.I == I; });
Tim Northover603d3162014-11-14 22:45:33 +0000247 assert(Info != Insts.end() && "invalid sp adjusting instruction");
248 Info->SPAdjust += ExtraBytes;
249 }
250
Matthias Braunf23ef432016-11-30 23:48:42 +0000251 void emitDefCFAOffsets(MachineBasicBlock &MBB, const DebugLoc &dl,
252 const ARMBaseInstrInfo &TII, bool HasFP) {
253 MachineFunction &MF = *MBB.getParent();
Tim Northover603d3162014-11-14 22:45:33 +0000254 unsigned CFAOffset = 0;
255 for (auto &Info : Insts) {
256 if (HasFP && !Info.BeforeFPSet)
257 return;
258
259 CFAOffset -= Info.SPAdjust;
Matthias Braunf23ef432016-11-30 23:48:42 +0000260 unsigned CFIIndex = MF.addFrameInst(
Tim Northover603d3162014-11-14 22:45:33 +0000261 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
262 BuildMI(MBB, std::next(Info.I), dl,
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000263 TII.get(TargetOpcode::CFI_INSTRUCTION))
264 .addCFIIndex(CFIIndex)
265 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000266 }
267 }
268};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000269
270} // end anonymous namespace
Tim Northover603d3162014-11-14 22:45:33 +0000271
Kristof Beyls933de7a2015-01-08 15:09:14 +0000272/// Emit an instruction sequence that will align the address in
273/// register Reg by zero-ing out the lower bits. For versions of the
274/// architecture that support Neon, this must be done in a single
275/// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
276/// single instruction. That function only gets called when optimizing
277/// spilling of D registers on a core with the Neon instruction set
278/// present.
279static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
280 const TargetInstrInfo &TII,
281 MachineBasicBlock &MBB,
282 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000283 const DebugLoc &DL, const unsigned Reg,
Kristof Beyls933de7a2015-01-08 15:09:14 +0000284 const unsigned Alignment,
285 const bool MustBeSingleInstruction) {
Eric Christopher1b21f002015-01-29 00:19:33 +0000286 const ARMSubtarget &AST =
287 static_cast<const ARMSubtarget &>(MF.getSubtarget());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000288 const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
289 const unsigned AlignMask = Alignment - 1;
290 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
291 assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
292 if (!AFI->isThumbFunction()) {
293 // if the BFC instruction is available, use that to zero the lower
294 // bits:
295 // bfc Reg, #0, log2(Alignment)
296 // otherwise use BIC, if the mask to zero the required number of bits
297 // can be encoded in the bic immediate field
298 // bic Reg, Reg, Alignment-1
299 // otherwise, emit
300 // lsr Reg, Reg, log2(Alignment)
301 // lsl Reg, Reg, log2(Alignment)
302 if (CanUseBFC) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000303 BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
304 .addReg(Reg, RegState::Kill)
305 .addImm(~AlignMask)
306 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000307 } else if (AlignMask <= 255) {
Diana Picus8a73f552017-01-13 10:18:01 +0000308 BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
309 .addReg(Reg, RegState::Kill)
310 .addImm(AlignMask)
311 .add(predOps(ARMCC::AL))
312 .add(condCodeOp());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000313 } else {
314 assert(!MustBeSingleInstruction &&
315 "Shouldn't call emitAligningInstructions demanding a single "
316 "instruction to be emitted for large stack alignment for a target "
317 "without BFC.");
Diana Picus8a73f552017-01-13 10:18:01 +0000318 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
319 .addReg(Reg, RegState::Kill)
320 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))
321 .add(predOps(ARMCC::AL))
322 .add(condCodeOp());
323 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
324 .addReg(Reg, RegState::Kill)
325 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))
326 .add(predOps(ARMCC::AL))
327 .add(condCodeOp());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000328 }
329 } else {
330 // Since this is only reached for Thumb-2 targets, the BFC instruction
331 // should always be available.
332 assert(CanUseBFC);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000333 BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
334 .addReg(Reg, RegState::Kill)
335 .addImm(~AlignMask)
336 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000337 }
338}
339
Matthias Braun8aaa3682017-04-19 21:11:44 +0000340/// We need the offset of the frame pointer relative to other MachineFrameInfo
341/// offsets which are encoded relative to SP at function begin.
342/// See also emitPrologue() for how the FP is set up.
343/// Unfortunately we cannot determine this value in determineCalleeSaves() yet
344/// as assignCalleeSavedSpillSlots() hasn't run at this point. Instead we use
345/// this to produce a conservative estimate that we check in an assert() later.
346static int getMaxFPOffset(const Function &F, const ARMFunctionInfo &AFI) {
347 // This is a conservative estimation: Assume the frame pointer being r7 and
348 // pc("r15") up to r8 getting spilled before (= 8 registers).
349 return -AFI.getArgRegsSaveSize() - (8 * 4);
350}
351
Quentin Colombet61b305e2015-05-05 17:38:16 +0000352void ARMFrameLowering::emitPrologue(MachineFunction &MF,
353 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000354 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000355 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000356 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000357 MachineModuleInfo &MMI = MF.getMMI();
358 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000359 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000360 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopher1b21f002015-01-29 00:19:33 +0000361 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
362 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000363 assert(!AFI->isThumb1OnlyFunction() &&
364 "This emitPrologue does not support Thumb1!");
365 bool isARM = !AFI->isThumbFunction();
Eric Christopher1b21f002015-01-29 00:19:33 +0000366 unsigned Align = STI.getFrameLowering()->getStackAlignment();
Tim Northover775aaeb2015-11-05 21:54:58 +0000367 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000368 unsigned NumBytes = MFI.getStackSize();
369 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000370
371 // Debug location must be unknown since the first debug location is used
372 // to determine the end of the prologue.
373 DebugLoc dl;
374
375 unsigned FramePtr = RegInfo->getFrameRegister(MF);
376
377 // Determine the sizes of each callee-save spill areas and record which frame
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000378 // belongs to which callee-save spill areas.
379 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
380 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000381 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000382
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000383 // All calls are tail calls in GHC calling conv, and functions have no
384 // prologue/epilogue.
Matthias Braunf1caa282017-12-15 22:22:58 +0000385 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000386 return;
387
Tim Northover603d3162014-11-14 22:45:33 +0000388 StackAdjustingInsts DefCFAOffsetCandidates;
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000389 bool HasFP = hasFP(MF);
Tim Northover603d3162014-11-14 22:45:33 +0000390
Oliver Stannardd55e1152014-03-05 15:25:27 +0000391 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000392 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000393 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000394 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000395 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000396 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000397
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000398 if (!AFI->hasStackFrame() &&
399 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000400 if (NumBytes - ArgRegsSaveSize != 0) {
401 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000402 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000403 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
404 NumBytes - ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000405 }
Matthias Braunf23ef432016-11-30 23:48:42 +0000406 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000407 return;
408 }
409
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000410 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000411 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
412 unsigned Reg = CSI[i].getReg();
413 int FI = CSI[i].getFrameIdx();
414 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000415 case ARM::R8:
416 case ARM::R9:
417 case ARM::R10:
418 case ARM::R11:
419 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000420 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000421 GPRCS2Size += 4;
422 break;
423 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000424 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +0000425 case ARM::R0:
426 case ARM::R1:
427 case ARM::R2:
428 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000429 case ARM::R4:
430 case ARM::R5:
431 case ARM::R6:
432 case ARM::R7:
433 case ARM::LR:
434 if (Reg == FramePtr)
435 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000436 GPRCS1Size += 4;
437 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000438 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000439 // This is a DPR. Exclude the aligned DPRCS2 spills.
440 if (Reg == ARM::D8)
441 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000442 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000443 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000444 }
445 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000446
Eric Christopherb006fc92010-11-18 19:40:05 +0000447 // Move past area 1.
Tim Northover603d3162014-11-14 22:45:33 +0000448 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
449 if (GPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000450 GPRCS1Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000451 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
452 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000453
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000454 // Determine starting offsets of spill areas.
Tim Northover228c9432014-11-05 00:27:13 +0000455 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
456 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
457 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
458 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
459 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000460 int FramePtrOffsetInPush = 0;
461 if (HasFP) {
Matthias Braun8aaa3682017-04-19 21:11:44 +0000462 int FPOffset = MFI.getObjectOffset(FramePtrSpillFI);
Matthias Braunf1caa282017-12-15 22:22:58 +0000463 assert(getMaxFPOffset(MF.getFunction(), *AFI) <= FPOffset &&
Matthias Braun8aaa3682017-04-19 21:11:44 +0000464 "Max FP estimation is wrong");
465 FramePtrOffsetInPush = FPOffset + ArgRegsSaveSize;
Matthias Braun941a7052016-07-28 18:40:00 +0000466 AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000467 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000468 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000469 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
470 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
471 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
472
Tim Northoverc9432eb2013-11-04 23:04:15 +0000473 // Move past area 2.
Tim Northover603d3162014-11-14 22:45:33 +0000474 if (GPRCS2Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000475 GPRCS2Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000476 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
477 }
Tim Northoverc9432eb2013-11-04 23:04:15 +0000478
Tim Northover228c9432014-11-05 00:27:13 +0000479 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
480 // .cfi_offset operations will reflect that.
481 if (DPRGapSize) {
482 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
Duncan P. N. Exon Smithec083b52016-08-17 00:53:04 +0000483 if (LastPush != MBB.end() &&
484 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, DPRGapSize))
Tim Northover603d3162014-11-14 22:45:33 +0000485 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
486 else {
Tim Northover228c9432014-11-05 00:27:13 +0000487 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
488 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000489 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
490 }
Tim Northover228c9432014-11-05 00:27:13 +0000491 }
492
Eric Christopherb006fc92010-11-18 19:40:05 +0000493 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000494 if (DPRCSSize > 0) {
Evan Cheng70d29632011-02-25 00:24:46 +0000495 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000496 // instructions in the prologue.
Matthias Braun5d01e702017-11-28 01:17:52 +0000497 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000498 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(*MBBI));
Tim Northover93bcc662013-11-08 17:18:07 +0000499 LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000500 }
Evan Cheng70d29632011-02-25 00:24:46 +0000501 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000502
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000503 // Move past the aligned DPRCS2 area.
504 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
505 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
506 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
507 // leaves the stack pointer pointing to the DPRCS2 area.
508 //
509 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
Matthias Braun941a7052016-07-28 18:40:00 +0000510 NumBytes += MFI.getObjectOffset(D8SpillFI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000511 } else
512 NumBytes = DPRCSOffset;
513
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000514 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
515 uint32_t NumWords = NumBytes >> 2;
516
517 if (NumWords < 65536)
Diana Picus4f8c3e12017-01-13 09:37:56 +0000518 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
519 .addImm(NumWords)
520 .setMIFlags(MachineInstr::FrameSetup)
521 .add(predOps(ARMCC::AL));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000522 else
523 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000524 .addImm(NumWords)
525 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000526
527 switch (TM.getCodeModel()) {
528 case CodeModel::Small:
529 case CodeModel::Medium:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000530 case CodeModel::Kernel:
531 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
Diana Picusbd66b7d2017-01-20 08:15:24 +0000532 .add(predOps(ARMCC::AL))
533 .addExternalSymbol("__chkstk")
534 .addReg(ARM::R4, RegState::Implicit)
535 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000536 break;
537 case CodeModel::Large:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000538 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000539 .addExternalSymbol("__chkstk")
540 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000541
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000542 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
Diana Picusbd66b7d2017-01-20 08:15:24 +0000543 .add(predOps(ARMCC::AL))
544 .addReg(ARM::R12, RegState::Kill)
545 .addReg(ARM::R4, RegState::Implicit)
546 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000547 break;
548 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000549
Diana Picus8a73f552017-01-13 10:18:01 +0000550 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), ARM::SP)
551 .addReg(ARM::SP, RegState::Kill)
552 .addReg(ARM::R4, RegState::Kill)
553 .setMIFlags(MachineInstr::FrameSetup)
554 .add(predOps(ARMCC::AL))
555 .add(condCodeOp());
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000556 NumBytes = 0;
557 }
558
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000559 if (NumBytes) {
560 // Adjust SP after all the callee-save spills.
Tim Northoverbeb5bcc2015-09-23 22:21:09 +0000561 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000562 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, NumBytes))
Tim Northover603d3162014-11-14 22:45:33 +0000563 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
564 else {
Tim Northover93bcc662013-11-08 17:18:07 +0000565 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
566 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000567 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
568 }
Tim Northover93bcc662013-11-08 17:18:07 +0000569
Evan Chengeb56dca2010-11-22 18:12:04 +0000570 if (HasFP && isARM)
571 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
572 // Note it's not safe to do this in Thumb2 mode because it would have
573 // taken two instructions:
574 // mov sp, r7
575 // sub sp, #24
576 // If an interrupt is taken between the two instructions, then sp is in
577 // an inconsistent state (pointing to the middle of callee-saved area).
578 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000579 AFI->setShouldRestoreSPFromFP(true);
580 }
581
Tim Northover603d3162014-11-14 22:45:33 +0000582 // Set FP to point to the stack slot that contains the previous FP.
583 // For iOS, FP is R7, which has now been stored in spill area 1.
584 // Otherwise, if this is not iOS, all the callee-saved registers go
585 // into spill area 1, including the FP in R11. In either case, it
586 // is in area one and the adjustment needs to take place just after
587 // that push.
588 if (HasFP) {
589 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000590 unsigned PushSize = sizeOfSPAdjustment(*GPRCS1Push);
Tim Northover603d3162014-11-14 22:45:33 +0000591 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
592 dl, TII, FramePtr, ARM::SP,
593 PushSize + FramePtrOffsetInPush,
594 MachineInstr::FrameSetup);
595 if (FramePtrOffsetInPush + PushSize != 0) {
Matthias Braunf23ef432016-11-30 23:48:42 +0000596 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
Tim Northover603d3162014-11-14 22:45:33 +0000597 nullptr, MRI->getDwarfRegNum(FramePtr, true),
598 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
599 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000600 .addCFIIndex(CFIIndex)
601 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000602 } else {
603 unsigned CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +0000604 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
Tim Northover603d3162014-11-14 22:45:33 +0000605 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
606 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000607 .addCFIIndex(CFIIndex)
608 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000609 }
610 }
611
612 // Now that the prologue's actual instructions are finalised, we can insert
613 // the necessary DWARF cf instructions to describe the situation. Start by
614 // recording where each register ended up:
615 if (GPRCS1Size > 0) {
616 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
617 int CFIIndex;
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000618 for (const auto &Entry : CSI) {
619 unsigned Reg = Entry.getReg();
620 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000621 switch (Reg) {
622 case ARM::R8:
623 case ARM::R9:
624 case ARM::R10:
625 case ARM::R11:
626 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000627 if (STI.splitFramePushPop(MF))
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000628 break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000629 LLVM_FALLTHROUGH;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000630 case ARM::R0:
631 case ARM::R1:
632 case ARM::R2:
633 case ARM::R3:
634 case ARM::R4:
635 case ARM::R5:
636 case ARM::R6:
637 case ARM::R7:
638 case ARM::LR:
Matthias Braunf23ef432016-11-30 23:48:42 +0000639 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Matthias Braun941a7052016-07-28 18:40:00 +0000640 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000641 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000642 .addCFIIndex(CFIIndex)
643 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000644 break;
645 }
646 }
647 }
648
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000649 if (GPRCS2Size > 0) {
Tim Northover603d3162014-11-14 22:45:33 +0000650 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000651 for (const auto &Entry : CSI) {
652 unsigned Reg = Entry.getReg();
653 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000654 switch (Reg) {
655 case ARM::R8:
656 case ARM::R9:
657 case ARM::R10:
658 case ARM::R11:
659 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000660 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000661 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000662 unsigned Offset = MFI.getObjectOffset(FI);
Matthias Braunf23ef432016-11-30 23:48:42 +0000663 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000664 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
665 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000666 .addCFIIndex(CFIIndex)
667 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000668 }
669 break;
670 }
671 }
672 }
673
674 if (DPRCSSize > 0) {
675 // Since vpush register list cannot have gaps, there may be multiple vpush
676 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000677 MachineBasicBlock::iterator Pos = std::next(LastPush);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000678 for (const auto &Entry : CSI) {
679 unsigned Reg = Entry.getReg();
680 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000681 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
682 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
683 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000684 unsigned Offset = MFI.getObjectOffset(FI);
Matthias Braunf23ef432016-11-30 23:48:42 +0000685 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000686 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover603d3162014-11-14 22:45:33 +0000687 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000688 .addCFIIndex(CFIIndex)
689 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000690 }
691 }
692 }
693
Tim Northover603d3162014-11-14 22:45:33 +0000694 // Now we can emit descriptions of where the canonical frame address was
695 // throughout the process. If we have a frame pointer, it takes over the job
696 // half-way through, so only the first few .cfi_def_cfa_offset instructions
697 // actually get emitted.
Matthias Braunf23ef432016-11-30 23:48:42 +0000698 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
Tim Northover93bcc662013-11-08 17:18:07 +0000699
Evan Chengeb56dca2010-11-22 18:12:04 +0000700 if (STI.isTargetELF() && hasFP(MF))
Matthias Braun941a7052016-07-28 18:40:00 +0000701 MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
702 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000703
704 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
705 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000706 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000707 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
708
709 // If we need dynamic stack realignment, do it here. Be paranoid and make
710 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000711 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000712 // realigned.
713 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000714 unsigned MaxAlign = MFI.getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +0000715 assert(!AFI->isThumb1OnlyFunction());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000716 if (!AFI->isThumbFunction()) {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000717 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
718 false);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000719 } else {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000720 // We cannot use sp as source/dest register here, thus we're using r4 to
721 // perform the calculations. We're emitting the following sequence:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000722 // mov r4, sp
Kristof Beyls933de7a2015-01-08 15:09:14 +0000723 // -- use emitAligningInstructions to produce best sequence to zero
724 // -- out lower bits in r4
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000725 // mov sp, r4
726 // FIXME: It will be better just to find spare register here.
Diana Picus4f8c3e12017-01-13 09:37:56 +0000727 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
728 .addReg(ARM::SP, RegState::Kill)
729 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000730 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
731 false);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000732 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
733 .addReg(ARM::R4, RegState::Kill)
734 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000735 }
736
737 AFI->setShouldRestoreSPFromFP(true);
738 }
739
740 // If we need a base pointer, set it up here. It's whatever the value
741 // of the stack pointer is at this point. Any variable size objects
742 // will be allocated after this, so we can still use the base pointer
743 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000744 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000745 if (RegInfo->hasBasePointer(MF)) {
746 if (isARM)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000747 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), RegInfo->getBaseRegister())
748 .addReg(ARM::SP)
749 .add(predOps(ARMCC::AL))
750 .add(condCodeOp());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000751 else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000752 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), RegInfo->getBaseRegister())
753 .addReg(ARM::SP)
754 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000755 }
756
757 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000758 // the sp from fp. We can assume there's an FP here since hasFP already
759 // checks for hasVarSizedObjects.
Matthias Braun941a7052016-07-28 18:40:00 +0000760 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000761 AFI->setShouldRestoreSPFromFP(true);
762}
763
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000764void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000765 MachineBasicBlock &MBB) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000766 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000767 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000768 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000769 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000770 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000771 assert(!AFI->isThumb1OnlyFunction() &&
772 "This emitEpilogue does not support Thumb1!");
773 bool isARM = !AFI->isThumbFunction();
774
Tim Northover8cda34f2015-03-11 18:54:22 +0000775 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000776 int NumBytes = (int)MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000777 unsigned FramePtr = RegInfo->getFrameRegister(MF);
778
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000779 // All calls are tail calls in GHC calling conv, and functions have no
780 // prologue/epilogue.
Matthias Braunf1caa282017-12-15 22:22:58 +0000781 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000782 return;
Quentin Colombet71a71482015-07-20 21:42:14 +0000783
784 // First put ourselves on the first (from top) terminator instructions.
785 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
786 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Eric Christopherb3322362012-08-03 00:05:53 +0000787
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000788 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000789 if (NumBytes - ArgRegsSaveSize != 0)
790 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000791 } else {
792 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000793 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000794 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000795 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000796 --MBBI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000797 } while (MBBI != MBB.begin() && isCSRestore(*MBBI, TII, CSRegs));
798 if (!isCSRestore(*MBBI, TII, CSRegs))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000799 ++MBBI;
800 }
801
802 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000803 NumBytes -= (ArgRegsSaveSize +
804 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000805 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000806 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000807 AFI->getDPRCalleeSavedAreaSize());
808
809 // Reset SP based on frame pointer only if the stack frame extends beyond
810 // frame pointer stack slot or target is ELF and the function has FP.
811 if (AFI->shouldRestoreSPFromFP()) {
812 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
813 if (NumBytes) {
814 if (isARM)
815 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
816 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000817 else {
818 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000819 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000820 // mov sp, r7
821 // sub sp, #24
822 // This is bad, if an interrupt is taken after the mov, sp is in an
823 // inconsistent state.
824 // Use the first callee-saved register as a scratch register.
Matthias Braun941a7052016-07-28 18:40:00 +0000825 assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000826 "No scratch register to restore SP from FP!");
827 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000828 ARMCC::AL, 0, TII);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000829 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
830 .addReg(ARM::R4)
831 .add(predOps(ARMCC::AL));
Evan Chengeb56dca2010-11-22 18:12:04 +0000832 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000833 } else {
834 // Thumb2 or ARM.
835 if (isARM)
836 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000837 .addReg(FramePtr)
838 .add(predOps(ARMCC::AL))
839 .add(condCodeOp());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000840 else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000841 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
842 .addReg(FramePtr)
843 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000844 }
Tim Northoverdee86042013-12-02 14:46:26 +0000845 } else if (NumBytes &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000846 !tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
847 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000848
Eric Christopherb006fc92010-11-18 19:40:05 +0000849 // Increment past our save areas.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000850 if (MBBI != MBB.end() && AFI->getDPRCalleeSavedAreaSize()) {
Evan Cheng70d29632011-02-25 00:24:46 +0000851 MBBI++;
852 // Since vpop register list cannot have gaps, there may be multiple vpop
853 // instructions in the epilogue.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000854 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VLDMDIA_UPD)
Evan Cheng70d29632011-02-25 00:24:46 +0000855 MBBI++;
856 }
Tim Northover228c9432014-11-05 00:27:13 +0000857 if (AFI->getDPRCalleeSavedGapSize()) {
858 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
859 "unexpected DPR alignment gap");
860 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
861 }
862
Eric Christopherb006fc92010-11-18 19:40:05 +0000863 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
864 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000865 }
866
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000867 if (ArgRegsSaveSize)
868 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000869}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000870
Bob Wilson657f2272011-01-13 21:10:12 +0000871/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
872/// debug info. It's the same as what we use for resolving the code-gen
873/// references for now. FIXME: This can go wrong when references are
874/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000875int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000876ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000877 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000878 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
879}
880
881int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000882ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000883 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000884 int SPAdj) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000885 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000886 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000887 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000888 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000889 int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +0000890 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000891 bool isFixed = MFI.isFixedObjectIndex(FI);
Anton Korobeynikov46877782010-11-20 15:59:32 +0000892
893 FrameReg = ARM::SP;
894 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000895
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000896 // SP can move around if there are allocas. We may also lose track of SP
897 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000898 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000899
Anton Korobeynikov46877782010-11-20 15:59:32 +0000900 // When dynamically realigning the stack, use the frame pointer for
901 // parameters, and the stack/base pointer for locals.
902 if (RegInfo->needsStackRealignment(MF)) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000903 assert(hasFP(MF) && "dynamic stack realignment without a FP!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000904 if (isFixed) {
905 FrameReg = RegInfo->getFrameRegister(MF);
906 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000907 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000908 assert(RegInfo->hasBasePointer(MF) &&
909 "VLAs and dynamic stack alignment, but missing base pointer!");
910 FrameReg = RegInfo->getBaseRegister();
911 }
912 return Offset;
913 }
914
915 // If there is a frame pointer, use it when we can.
916 if (hasFP(MF) && AFI->hasStackFrame()) {
917 // Use frame pointer to reference fixed objects. Use it for locals if
918 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000919 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000920 FrameReg = RegInfo->getFrameRegister(MF);
921 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000922 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000923 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000924 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000925 // Try to use the frame pointer if we can, else use the base pointer
926 // since it's available. This is handy for the emergency spill slot, in
927 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000928 if (FPOffset >= -255 && FPOffset < 0) {
929 FrameReg = RegInfo->getFrameRegister(MF);
930 return FPOffset;
931 }
Evan Chengc0d20042011-04-22 01:42:52 +0000932 }
Momchil Velikov505614b2018-03-02 15:47:14 +0000933 } else if (AFI->isThumbFunction()) {
934 // Prefer SP to base pointer, if the offset is suitably aligned and in
935 // range as the effective range of the immediate offset is bigger when
936 // basing off SP.
Andrew Trickf7ecc162011-08-25 17:40:54 +0000937 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000938 // ldr <rd>, [sp, #<imm8>]
Evan Chengc0d20042011-04-22 01:42:52 +0000939 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
940 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000941 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000942 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Momchil Velikov505614b2018-03-02 15:47:14 +0000943 if (AFI->isThumb2Function() && FPOffset >= -255 && FPOffset < 0) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000944 FrameReg = RegInfo->getFrameRegister(MF);
945 return FPOffset;
946 }
947 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
948 // Otherwise, use SP or FP, whichever is closer to the stack slot.
949 FrameReg = RegInfo->getFrameRegister(MF);
950 return FPOffset;
951 }
952 }
953 // Use the base pointer if we have one.
954 if (RegInfo->hasBasePointer(MF))
955 FrameReg = RegInfo->getBaseRegister();
956 return Offset;
957}
958
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000959void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000960 MachineBasicBlock::iterator MI,
961 const std::vector<CalleeSavedInfo> &CSI,
962 unsigned StmOpc, unsigned StrOpc,
963 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000964 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000965 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000966 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000967 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000968 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover46a6f0f2016-11-14 20:28:24 +0000969 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000970
971 DebugLoc DL;
972
Eugene Zelenko076468c2017-09-20 21:35:51 +0000973 using RegAndKill = std::pair<unsigned, bool>;
974
Tim Northover46a6f0f2016-11-14 20:28:24 +0000975 SmallVector<RegAndKill, 4> Regs;
Tim Northover775aaeb2015-11-05 21:54:58 +0000976 unsigned i = CSI.size();
Evan Cheng775ead32010-12-07 23:08:38 +0000977 while (i != 0) {
978 unsigned LastReg = 0;
979 for (; i != 0; --i) {
980 unsigned Reg = CSI[i-1].getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000981 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000982
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000983 // D-registers in the aligned area DPRCS2 are NOT spilled here.
984 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
985 continue;
986
Matthias Braun0dba4e32017-05-31 01:21:30 +0000987 const MachineRegisterInfo &MRI = MF.getRegInfo();
988 bool isLiveIn = MRI.isLiveIn(Reg);
989 if (!isLiveIn && !MRI.isReserved(Reg))
Evan Cheng775ead32010-12-07 23:08:38 +0000990 MBB.addLiveIn(Reg);
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000991 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000992 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000993 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000994 if (NoGap && LastReg && LastReg != Reg-1)
995 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000996 LastReg = Reg;
Matthias Braun707e02c2016-04-13 21:43:25 +0000997 // Do not set a kill flag on values that are also marked as live-in. This
998 // happens with the @llvm-returnaddress intrinsic and with arguments
999 // passed in callee saved registers.
1000 // Omitting the kill flags is conservatively correct even if the live-in
1001 // is not used after all.
1002 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001003 }
1004
Jim Grosbach5fccad82010-12-09 18:31:13 +00001005 if (Regs.empty())
1006 continue;
Tim Northover46a6f0f2016-11-14 20:28:24 +00001007
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001008 llvm::sort(Regs.begin(), Regs.end(), [&](const RegAndKill &LHS,
1009 const RegAndKill &RHS) {
Tim Northover46a6f0f2016-11-14 20:28:24 +00001010 return TRI.getEncodingValue(LHS.first) < TRI.getEncodingValue(RHS.first);
1011 });
1012
Jim Grosbach5fccad82010-12-09 18:31:13 +00001013 if (Regs.size() > 1 || StrOpc== 0) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001014 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
1015 .addReg(ARM::SP)
1016 .setMIFlags(MIFlags)
1017 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001018 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1019 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +00001020 } else if (Regs.size() == 1) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001021 BuildMI(MBB, MI, DL, TII.get(StrOpc), ARM::SP)
1022 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
1023 .addReg(ARM::SP)
1024 .setMIFlags(MIFlags)
1025 .addImm(-4)
1026 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001027 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001028 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001029
1030 // Put any subsequent vpush instructions before this one: they will refer to
1031 // higher register numbers so need to be pushed first in order to preserve
1032 // monotonicity.
Quentin Colombet71a71482015-07-20 21:42:14 +00001033 if (MI != MBB.begin())
1034 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001035 }
Evan Cheng775ead32010-12-07 23:08:38 +00001036}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001037
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001038void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001039 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001040 std::vector<CalleeSavedInfo> &CSI,
Bob Wilson657f2272011-01-13 21:10:12 +00001041 unsigned LdmOpc, unsigned LdrOpc,
1042 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001043 bool(*Func)(unsigned, bool),
1044 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +00001045 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001046 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover46a6f0f2016-11-14 20:28:24 +00001047 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Evan Cheng775ead32010-12-07 23:08:38 +00001048 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet71a71482015-07-20 21:42:14 +00001049 DebugLoc DL;
1050 bool isTailCall = false;
1051 bool isInterrupt = false;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001052 bool isTrap = false;
Quentin Colombet71a71482015-07-20 21:42:14 +00001053 if (MBB.end() != MI) {
1054 DL = MI->getDebugLoc();
1055 unsigned RetOpcode = MI->getOpcode();
1056 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
1057 isInterrupt =
1058 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001059 isTrap =
1060 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
1061 RetOpcode == ARM::tTRAP;
Quentin Colombet71a71482015-07-20 21:42:14 +00001062 }
Evan Cheng775ead32010-12-07 23:08:38 +00001063
1064 SmallVector<unsigned, 4> Regs;
1065 unsigned i = CSI.size();
1066 while (i != 0) {
1067 unsigned LastReg = 0;
1068 bool DeleteRet = false;
1069 for (; i != 0; --i) {
Matthias Braun51687912017-09-28 23:12:06 +00001070 CalleeSavedInfo &Info = CSI[i-1];
1071 unsigned Reg = Info.getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001072 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Evan Cheng775ead32010-12-07 23:08:38 +00001073
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001074 // The aligned reloads from area DPRCS2 are not inserted here.
1075 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
1076 continue;
1077
Tim Northoverd8407452013-10-01 14:33:28 +00001078 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001079 !isTrap && STI.hasV5TOps()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00001080 if (MBB.succ_empty()) {
1081 Reg = ARM::PC;
Thomas Preud'hommec699eaa2018-03-05 11:49:00 +00001082 // Fold the return instruction into the LDM.
Quentin Colombet71a71482015-07-20 21:42:14 +00001083 DeleteRet = true;
1084 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Matthias Braun51687912017-09-28 23:12:06 +00001085 // We 'restore' LR into PC so it is not live out of the return block:
1086 // Clear Restored bit.
1087 Info.setRestored(false);
Quentin Colombet71a71482015-07-20 21:42:14 +00001088 } else
1089 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Evan Cheng775ead32010-12-07 23:08:38 +00001090 }
1091
Evan Cheng9d54ae62010-12-08 06:29:02 +00001092 // If NoGap is true, pop consecutive registers and then leave the rest
1093 // for other instructions. e.g.
1094 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1095 if (NoGap && LastReg && LastReg != Reg-1)
1096 break;
1097
Evan Cheng775ead32010-12-07 23:08:38 +00001098 LastReg = Reg;
1099 Regs.push_back(Reg);
1100 }
1101
Jim Grosbach5fccad82010-12-09 18:31:13 +00001102 if (Regs.empty())
1103 continue;
Tim Northover46a6f0f2016-11-14 20:28:24 +00001104
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001105 llvm::sort(Regs.begin(), Regs.end(), [&](unsigned LHS, unsigned RHS) {
Tim Northover46a6f0f2016-11-14 20:28:24 +00001106 return TRI.getEncodingValue(LHS) < TRI.getEncodingValue(RHS);
1107 });
1108
Jim Grosbach5fccad82010-12-09 18:31:13 +00001109 if (Regs.size() > 1 || LdrOpc == 0) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001110 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
1111 .addReg(ARM::SP)
1112 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001113 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1114 MIB.addReg(Regs[i], getDefRegState(true));
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001115 if (DeleteRet) {
1116 if (MI != MBB.end()) {
1117 MIB.copyImplicitOps(*MI);
1118 MI->eraseFromParent();
1119 }
Andrew Trick6446bf72011-08-25 17:50:53 +00001120 }
Evan Cheng775ead32010-12-07 23:08:38 +00001121 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +00001122 } else if (Regs.size() == 1) {
1123 // If we adjusted the reg to PC from LR above, switch it back here. We
1124 // only do that for LDM.
1125 if (Regs[0] == ARM::PC)
1126 Regs[0] = ARM::LR;
1127 MachineInstrBuilder MIB =
1128 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1129 .addReg(ARM::SP, RegState::Define)
1130 .addReg(ARM::SP);
1131 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1132 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +00001133 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +00001134 MIB.addReg(0);
1135 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1136 } else
1137 MIB.addImm(4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001138 MIB.add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001139 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001140 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001141
1142 // Put any subsequent vpop instructions after this one: they will refer to
1143 // higher register numbers so need to be popped afterwards.
Quentin Colombet71a71482015-07-20 21:42:14 +00001144 if (MI != MBB.end())
1145 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +00001146 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001147}
1148
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001149/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +00001150/// starting from d8. Also insert stack realignment code and leave the stack
1151/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001152static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1153 MachineBasicBlock::iterator MI,
1154 unsigned NumAlignedDPRCS2Regs,
1155 const std::vector<CalleeSavedInfo> &CSI,
1156 const TargetRegisterInfo *TRI) {
1157 MachineFunction &MF = *MBB.getParent();
1158 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001159 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001160 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001161 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001162
1163 // Mark the D-register spill slots as properly aligned. Since MFI computes
1164 // stack slot layout backwards, this can actually mean that the d-reg stack
1165 // slot offsets can be wrong. The offset for d8 will always be correct.
1166 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1167 unsigned DNum = CSI[i].getReg() - ARM::D8;
Tim Northovere0ccdc62015-10-28 22:46:43 +00001168 if (DNum > NumAlignedDPRCS2Regs - 1)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001169 continue;
1170 int FI = CSI[i].getFrameIdx();
1171 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1172 // registers will be 8-byte aligned.
1173 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1174
1175 // The stack slot for D8 needs to be maximally aligned because this is
1176 // actually the point where we align the stack pointer. MachineFrameInfo
1177 // computes all offsets relative to the incoming stack pointer which is a
1178 // bit weird when realigning the stack. Any extra padding for this
1179 // over-alignment is not realized because the code inserted below adjusts
1180 // the stack pointer by numregs * 8 before aligning the stack pointer.
1181 if (DNum == 0)
1182 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1183 }
1184
1185 // Move the stack pointer to the d8 spill slot, and align it at the same
1186 // time. Leave the stack slot address in the scratch register r4.
1187 //
1188 // sub r4, sp, #numregs * 8
1189 // bic r4, r4, #align - 1
1190 // mov sp, r4
1191 //
1192 bool isThumb = AFI->isThumbFunction();
1193 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1194 AFI->setShouldRestoreSPFromFP(true);
1195
1196 // sub r4, sp, #numregs * 8
1197 // The immediate is <= 64, so it doesn't need any special encoding.
1198 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
Diana Picus8a73f552017-01-13 10:18:01 +00001199 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1200 .addReg(ARM::SP)
1201 .addImm(8 * NumAlignedDPRCS2Regs)
1202 .add(predOps(ARMCC::AL))
1203 .add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001204
Matthias Braun941a7052016-07-28 18:40:00 +00001205 unsigned MaxAlign = MF.getFrameInfo().getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +00001206 // We must set parameter MustBeSingleInstruction to true, since
1207 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1208 // stack alignment. Luckily, this can always be done since all ARM
1209 // architecture versions that support Neon also support the BFC
1210 // instruction.
1211 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001212
1213 // mov sp, r4
1214 // The stack pointer must be adjusted before spilling anything, otherwise
1215 // the stack slots could be clobbered by an interrupt handler.
1216 // Leave r4 live, it is used below.
1217 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1218 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
Diana Picus4f8c3e12017-01-13 09:37:56 +00001219 .addReg(ARM::R4)
1220 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001221 if (!isThumb)
Diana Picus8a73f552017-01-13 10:18:01 +00001222 MIB.add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001223
1224 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1225 // r4 holds the stack slot address.
1226 unsigned NextReg = ARM::D8;
1227
1228 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1229 // The writeback is only needed when emitting two vst1.64 instructions.
1230 if (NumAlignedDPRCS2Regs >= 6) {
1231 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001232 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001233 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001234 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), ARM::R4)
1235 .addReg(ARM::R4, RegState::Kill)
1236 .addImm(16)
1237 .addReg(NextReg)
1238 .addReg(SupReg, RegState::ImplicitKill)
1239 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001240 NextReg += 4;
1241 NumAlignedDPRCS2Regs -= 4;
1242 }
1243
1244 // We won't modify r4 beyond this point. It currently points to the next
1245 // register to be spilled.
1246 unsigned R4BaseReg = NextReg;
1247
1248 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1249 if (NumAlignedDPRCS2Regs >= 4) {
1250 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001251 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001252 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001253 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1254 .addReg(ARM::R4)
1255 .addImm(16)
1256 .addReg(NextReg)
1257 .addReg(SupReg, RegState::ImplicitKill)
1258 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001259 NextReg += 4;
1260 NumAlignedDPRCS2Regs -= 4;
1261 }
1262
1263 // 16-byte aligned vst1.64 with 2 d-regs.
1264 if (NumAlignedDPRCS2Regs >= 2) {
1265 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001266 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001267 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001268 BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
1269 .addReg(ARM::R4)
1270 .addImm(16)
1271 .addReg(SupReg)
1272 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001273 NextReg += 2;
1274 NumAlignedDPRCS2Regs -= 2;
1275 }
1276
1277 // Finally, use a vanilla vstr.64 for the odd last register.
1278 if (NumAlignedDPRCS2Regs) {
1279 MBB.addLiveIn(NextReg);
1280 // vstr.64 uses addrmode5 which has an offset scale of 4.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001281 BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1282 .addReg(NextReg)
1283 .addReg(ARM::R4)
1284 .addImm((NextReg - R4BaseReg) * 2)
1285 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001286 }
1287
1288 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001289 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001290}
1291
1292/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1293/// iterator to the following instruction.
1294static MachineBasicBlock::iterator
1295skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1296 unsigned NumAlignedDPRCS2Regs) {
1297 // sub r4, sp, #numregs * 8
1298 // bic r4, r4, #align - 1
1299 // mov sp, r4
1300 ++MI; ++MI; ++MI;
1301 assert(MI->mayStore() && "Expecting spill instruction");
1302
1303 // These switches all fall through.
1304 switch(NumAlignedDPRCS2Regs) {
1305 case 7:
1306 ++MI;
1307 assert(MI->mayStore() && "Expecting spill instruction");
Florian Hahndb479522017-07-27 14:37:17 +00001308 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001309 default:
1310 ++MI;
1311 assert(MI->mayStore() && "Expecting spill instruction");
Florian Hahndb479522017-07-27 14:37:17 +00001312 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001313 case 1:
1314 case 2:
1315 case 4:
1316 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1317 ++MI;
1318 }
1319 return MI;
1320}
1321
1322/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1323/// starting from d8. These instructions are assumed to execute while the
1324/// stack is still aligned, unlike the code inserted by emitPopInst.
1325static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1326 MachineBasicBlock::iterator MI,
1327 unsigned NumAlignedDPRCS2Regs,
1328 const std::vector<CalleeSavedInfo> &CSI,
1329 const TargetRegisterInfo *TRI) {
1330 MachineFunction &MF = *MBB.getParent();
1331 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001332 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001333 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001334
1335 // Find the frame index assigned to d8.
1336 int D8SpillFI = 0;
1337 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1338 if (CSI[i].getReg() == ARM::D8) {
1339 D8SpillFI = CSI[i].getFrameIdx();
1340 break;
1341 }
1342
1343 // Materialize the address of the d8 spill slot into the scratch register r4.
1344 // This can be fairly complicated if the stack frame is large, so just use
1345 // the normal frame index elimination mechanism to do it. This code runs as
1346 // the initial part of the epilog where the stack and base pointers haven't
1347 // been changed yet.
1348 bool isThumb = AFI->isThumbFunction();
1349 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1350
1351 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
Diana Picus8a73f552017-01-13 10:18:01 +00001352 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1353 .addFrameIndex(D8SpillFI)
1354 .addImm(0)
1355 .add(predOps(ARMCC::AL))
1356 .add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001357
1358 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1359 unsigned NextReg = ARM::D8;
1360
1361 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1362 if (NumAlignedDPRCS2Regs >= 6) {
1363 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001364 &ARM::QQPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001365 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1366 .addReg(ARM::R4, RegState::Define)
1367 .addReg(ARM::R4, RegState::Kill)
1368 .addImm(16)
1369 .addReg(SupReg, RegState::ImplicitDefine)
1370 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001371 NextReg += 4;
1372 NumAlignedDPRCS2Regs -= 4;
1373 }
1374
1375 // We won't modify r4 beyond this point. It currently points to the next
1376 // register to be spilled.
1377 unsigned R4BaseReg = NextReg;
1378
1379 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1380 if (NumAlignedDPRCS2Regs >= 4) {
1381 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001382 &ARM::QQPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001383 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1384 .addReg(ARM::R4)
1385 .addImm(16)
1386 .addReg(SupReg, RegState::ImplicitDefine)
1387 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001388 NextReg += 4;
1389 NumAlignedDPRCS2Regs -= 4;
1390 }
1391
1392 // 16-byte aligned vld1.64 with 2 d-regs.
1393 if (NumAlignedDPRCS2Regs >= 2) {
1394 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001395 &ARM::QPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001396 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1397 .addReg(ARM::R4)
1398 .addImm(16)
1399 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001400 NextReg += 2;
1401 NumAlignedDPRCS2Regs -= 2;
1402 }
1403
1404 // Finally, use a vanilla vldr.64 for the remaining odd register.
1405 if (NumAlignedDPRCS2Regs)
Diana Picus4f8c3e12017-01-13 09:37:56 +00001406 BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1407 .addReg(ARM::R4)
1408 .addImm(2 * (NextReg - R4BaseReg))
1409 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001410
1411 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001412 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001413}
1414
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001415bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001416 MachineBasicBlock::iterator MI,
1417 const std::vector<CalleeSavedInfo> &CSI,
1418 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001419 if (CSI.empty())
1420 return false;
1421
1422 MachineFunction &MF = *MBB.getParent();
1423 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001424
1425 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001426 unsigned PushOneOpc = AFI->isThumbFunction() ?
1427 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001428 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001429 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1430 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001431 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001432 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001433 MachineInstr::FrameSetup);
1434 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001435 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1436
1437 // The code above does not insert spill code for the aligned DPRCS2 registers.
1438 // The stack realignment code will be inserted between the push instructions
1439 // and these spills.
1440 if (NumAlignedDPRCS2Regs)
1441 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001442
1443 return true;
1444}
1445
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001446bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001447 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001448 std::vector<CalleeSavedInfo> &CSI,
Bob Wilson657f2272011-01-13 21:10:12 +00001449 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001450 if (CSI.empty())
1451 return false;
1452
1453 MachineFunction &MF = *MBB.getParent();
1454 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001455 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001456 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1457
1458 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1459 // registers. Do that here instead.
1460 if (NumAlignedDPRCS2Regs)
1461 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001462
1463 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001464 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001465 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001466 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1467 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001468 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001469 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001470 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001471 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001472
1473 return true;
1474}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001475
1476// FIXME: Make generic?
1477static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1478 const ARMBaseInstrInfo &TII) {
1479 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001480 for (auto &MBB : MF) {
1481 for (auto &MI : MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001482 FnSize += TII.getInstSizeInBytes(MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001483 }
1484 return FnSize;
1485}
1486
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001487/// estimateRSStackSizeLimit - Look at each instruction that references stack
1488/// frames and return the stack size limit beyond which some of these
1489/// instructions will require a scratch register during their expansion later.
1490// FIXME: Move to TII?
1491static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001492 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001493 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1494 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001495 for (auto &MBB : MF) {
1496 for (auto &MI : MBB) {
1497 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1498 if (!MI.getOperand(i).isFI())
1499 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001500
1501 // When using ADDri to get the address of a stack object, 255 is the
1502 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001503 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001504 Limit = std::min(Limit, (1U << 8) - 1);
1505 break;
1506 }
1507
1508 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001509 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001510 case ARMII::AddrMode3:
1511 case ARMII::AddrModeT2_i8:
1512 Limit = std::min(Limit, (1U << 8) - 1);
1513 break;
1514 case ARMII::AddrMode5:
1515 case ARMII::AddrModeT2_i8s4:
1516 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1517 break;
1518 case ARMII::AddrModeT2_i12:
1519 // i12 supports only positive offset so these will be converted to
1520 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1521 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1522 Limit = std::min(Limit, (1U << 8) - 1);
1523 break;
1524 case ARMII::AddrMode4:
1525 case ARMII::AddrMode6:
1526 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1527 // immediate offset for stack references.
1528 return 0;
1529 default:
1530 break;
1531 }
1532 break; // At most one FI per instruction
1533 }
1534 }
1535 }
1536
1537 return Limit;
1538}
1539
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001540// In functions that realign the stack, it can be an advantage to spill the
1541// callee-saved vector registers after realigning the stack. The vst1 and vld1
1542// instructions take alignment hints that can improve performance.
Matthias Braun02564862015-07-14 17:17:13 +00001543static void
1544checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001545 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1546 if (!SpillAlignedNEONRegs)
1547 return;
1548
1549 // Naked functions don't spill callee-saved registers.
Matthias Braunf1caa282017-12-15 22:22:58 +00001550 if (MF.getFunction().hasFnAttribute(Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001551 return;
1552
1553 // We are planning to use NEON instructions vst1 / vld1.
Eric Christopher1b21f002015-01-29 00:19:33 +00001554 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001555 return;
1556
1557 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopher1b21f002015-01-29 00:19:33 +00001558 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001559 return;
1560
1561 // Aligned spills require stack realignment.
Eric Christopher1b21f002015-01-29 00:19:33 +00001562 if (!static_cast<const ARMBaseRegisterInfo *>(
1563 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001564 return;
1565
1566 // We always spill contiguous d-registers starting from d8. Count how many
1567 // needs spilling. The register allocator will almost always use the
1568 // callee-saved registers in order, but it can happen that there are holes in
1569 // the range. Registers above the hole will be spilled to the standard DPRCS
1570 // area.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001571 unsigned NumSpills = 0;
1572 for (; NumSpills < 8; ++NumSpills)
Matthias Braun02564862015-07-14 17:17:13 +00001573 if (!SavedRegs.test(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001574 break;
1575
1576 // Don't do this for just one d-register. It's not worth it.
1577 if (NumSpills < 2)
1578 return;
1579
1580 // Spill the first NumSpills D-registers after realigning the stack.
1581 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1582
1583 // A scratch register is required for the vst1 / vld1 instructions.
Matthias Braun02564862015-07-14 17:17:13 +00001584 SavedRegs.set(ARM::R4);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001585}
1586
Matthias Braun02564862015-07-14 17:17:13 +00001587void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1588 BitVector &SavedRegs,
1589 RegScavenger *RS) const {
1590 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001591 // This tells PEI to spill the FP as if it is any other callee-save register
1592 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1593 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1594 // to combine multiple loads / stores.
1595 bool CanEliminateFrame = true;
1596 bool CS1Spilled = false;
1597 bool LRSpilled = false;
1598 unsigned NumGPRSpills = 0;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001599 unsigned NumFPRSpills = 0;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001600 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1601 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001602 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001603 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001604 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001605 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001606 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +00001607 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001608 MachineRegisterInfo &MRI = MF.getRegInfo();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001609 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1610 (void)TRI; // Silence unused warning in non-assert builds.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001611 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1612
1613 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1614 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001615 // since it's not always possible to restore sp from fp in a single
1616 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001617 // FIXME: It will be better just to find spare register here.
1618 if (AFI->isThumb2Function() &&
Matthias Braun941a7052016-07-28 18:40:00 +00001619 (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Matthias Braun02564862015-07-14 17:17:13 +00001620 SavedRegs.set(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001621
Evan Cheng572756a2011-01-16 05:14:33 +00001622 if (AFI->isThumb1OnlyFunction()) {
1623 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001624 if (AFI->getArgRegsSaveSize() > 0)
Matthias Braun02564862015-07-14 17:17:13 +00001625 SavedRegs.set(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001626
Momchil Velikovd6a4ab32017-10-22 11:56:35 +00001627 // Spill R4 if Thumb1 epilogue has to restore SP from FP or the function
1628 // requires stack alignment. We don't know for sure what the stack size
1629 // will be, but for this, an estimate is good enough. If there anything
1630 // changes it, it'll be a spill, which implies we've used all the registers
1631 // and so R4 is already used, so not marking it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001632 // FIXME: It will be better just to find spare register here.
Momchil Velikovd6a4ab32017-10-22 11:56:35 +00001633 if (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF) ||
1634 MFI.estimateStackSize(MF) > 508)
Matthias Braun02564862015-07-14 17:17:13 +00001635 SavedRegs.set(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001636 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001637
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001638 // See if we can spill vector registers to aligned stack.
Matthias Braun02564862015-07-14 17:17:13 +00001639 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001640
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001641 // Spill the BasePtr if it's used.
1642 if (RegInfo->hasBasePointer(MF))
Matthias Braun02564862015-07-14 17:17:13 +00001643 SavedRegs.set(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001644
1645 // Don't spill FP if the frame can be eliminated. This is determined
Matthias Braun02564862015-07-14 17:17:13 +00001646 // by scanning the callee-save registers to see if any is modified.
Craig Topper840beec2014-04-04 05:16:06 +00001647 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001648 for (unsigned i = 0; CSRegs[i]; ++i) {
1649 unsigned Reg = CSRegs[i];
1650 bool Spilled = false;
Matthias Braun02564862015-07-14 17:17:13 +00001651 if (SavedRegs.test(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001652 Spilled = true;
1653 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001654 }
1655
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001656 if (!ARM::GPRRegClass.contains(Reg)) {
1657 if (Spilled) {
1658 if (ARM::SPRRegClass.contains(Reg))
1659 NumFPRSpills++;
1660 else if (ARM::DPRRegClass.contains(Reg))
1661 NumFPRSpills += 2;
1662 else if (ARM::QPRRegClass.contains(Reg))
1663 NumFPRSpills += 4;
1664 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001665 continue;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001666 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001667
1668 if (Spilled) {
1669 NumGPRSpills++;
1670
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001671 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001672 if (Reg == ARM::LR)
1673 LRSpilled = true;
1674 CS1Spilled = true;
1675 continue;
1676 }
1677
1678 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1679 switch (Reg) {
1680 case ARM::LR:
1681 LRSpilled = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00001682 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +00001683 case ARM::R0: case ARM::R1:
1684 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001685 case ARM::R4: case ARM::R5:
1686 case ARM::R6: case ARM::R7:
1687 CS1Spilled = true;
1688 break;
1689 default:
1690 break;
1691 }
1692 } else {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001693 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001694 UnspilledCS1GPRs.push_back(Reg);
1695 continue;
1696 }
1697
1698 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001699 case ARM::R0: case ARM::R1:
1700 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001701 case ARM::R4: case ARM::R5:
1702 case ARM::R6: case ARM::R7:
1703 case ARM::LR:
1704 UnspilledCS1GPRs.push_back(Reg);
1705 break;
1706 default:
1707 UnspilledCS2GPRs.push_back(Reg);
1708 break;
1709 }
1710 }
1711 }
1712
1713 bool ForceLRSpill = false;
1714 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1715 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1716 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1717 // use of BL to implement far jump. If it turns out that it's not needed
1718 // then the branch fix up path will undo it.
1719 if (FnSize >= (1 << 11)) {
1720 CanEliminateFrame = false;
1721 ForceLRSpill = true;
1722 }
1723 }
1724
1725 // If any of the stack slot references may be out of range of an immediate
1726 // offset, make sure a register (or a spill slot) is available for the
1727 // register scavenger. Note that if we're indexing off the frame pointer, the
1728 // effective stack size is 4 bytes larger since the FP points to the stack
1729 // slot of the previous FP. Also, if we have variable sized objects in the
1730 // function, stack slot references will often be negative, and some of
1731 // our instructions are positive-offset only, so conservatively consider
1732 // that case to want a spill slot (or register) as well. Similarly, if
1733 // the function adjusts the stack pointer during execution and the
1734 // adjustments aren't already part of our stack size estimate, our offset
1735 // calculations may be off, so be conservative.
1736 // FIXME: We could add logic to be more precise about negative offsets
1737 // and which instructions will need a scratch register for them. Is it
1738 // worth the effort and added fragility?
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001739 unsigned EstimatedStackSize =
Matthias Braun941a7052016-07-28 18:40:00 +00001740 MFI.estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001741
1742 // Determine biggest (positive) SP offset in MachineFrameInfo.
1743 int MaxFixedOffset = 0;
1744 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
1745 int MaxObjectOffset = MFI.getObjectOffset(I) + MFI.getObjectSize(I);
1746 MaxFixedOffset = std::max(MaxFixedOffset, MaxObjectOffset);
1747 }
1748
Matthias Braun44047422017-04-05 16:58:41 +00001749 bool HasFP = hasFP(MF);
1750 if (HasFP) {
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001751 if (AFI->hasStackFrame())
1752 EstimatedStackSize += 4;
1753 } else {
1754 // If FP is not used, SP will be used to access arguments, so count the
1755 // size of arguments into the estimation.
Matthias Braun8aaa3682017-04-19 21:11:44 +00001756 EstimatedStackSize += MaxFixedOffset;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001757 }
1758 EstimatedStackSize += 16; // For possible paddings.
1759
Matthias Braun8aaa3682017-04-19 21:11:44 +00001760 unsigned EstimatedRSStackSizeLimit = estimateRSStackSizeLimit(MF, this);
Matthias Braunf1caa282017-12-15 22:22:58 +00001761 int MaxFPOffset = getMaxFPOffset(MF.getFunction(), *AFI);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001762 bool BigFrameOffsets = EstimatedStackSize >= EstimatedRSStackSizeLimit ||
1763 MFI.hasVarSizedObjects() ||
1764 (MFI.adjustsStack() && !canSimplifyCallFramePseudos(MF)) ||
1765 // For large argument stacks fp relative addressed may overflow.
1766 (HasFP && (MaxFixedOffset - MaxFPOffset) >= (int)EstimatedRSStackSizeLimit);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001767 if (BigFrameOffsets ||
1768 !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001769 AFI->setHasStackFrame(true);
1770
Matthias Braun44047422017-04-05 16:58:41 +00001771 if (HasFP) {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001772 SavedRegs.set(FramePtr);
1773 // If the frame pointer is required by the ABI, also spill LR so that we
1774 // emit a complete frame record.
1775 if (MF.getTarget().Options.DisableFramePointerElim(MF) && !LRSpilled) {
1776 SavedRegs.set(ARM::LR);
1777 LRSpilled = true;
1778 NumGPRSpills++;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001779 auto LRPos = llvm::find(UnspilledCS1GPRs, ARM::LR);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001780 if (LRPos != UnspilledCS1GPRs.end())
1781 UnspilledCS1GPRs.erase(LRPos);
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001782 }
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001783 auto FPPos = llvm::find(UnspilledCS1GPRs, FramePtr);
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001784 if (FPPos != UnspilledCS1GPRs.end())
1785 UnspilledCS1GPRs.erase(FPPos);
1786 NumGPRSpills++;
1787 if (FramePtr == ARM::R7)
1788 CS1Spilled = true;
1789 }
1790
Matthias Braunc618a462017-07-28 01:36:32 +00001791 // This is true when we inserted a spill for an unused register that can now
1792 // be used for register scavenging.
1793 bool ExtraCSSpill = false;
1794
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001795 if (AFI->isThumb1OnlyFunction()) {
1796 // For Thumb1-only targets, we need some low registers when we save and
1797 // restore the high registers (which aren't allocatable, but could be
1798 // used by inline assembly) because the push/pop instructions can not
1799 // access high registers. If necessary, we might need to push more low
1800 // registers to ensure that there is at least one free that can be used
1801 // for the saving & restoring, and preferably we should ensure that as
1802 // many as are needed are available so that fewer push/pop instructions
1803 // are required.
1804
1805 // Low registers which are not currently pushed, but could be (r4-r7).
1806 SmallVector<unsigned, 4> AvailableRegs;
1807
1808 // Unused argument registers (r0-r3) can be clobbered in the prologue for
1809 // free.
1810 int EntryRegDeficit = 0;
1811 for (unsigned Reg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3}) {
1812 if (!MF.getRegInfo().isLiveIn(Reg)) {
1813 --EntryRegDeficit;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001814 DEBUG(dbgs() << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001815 << " is unused argument register, EntryRegDeficit = "
1816 << EntryRegDeficit << "\n");
1817 }
1818 }
1819
1820 // Unused return registers can be clobbered in the epilogue for free.
1821 int ExitRegDeficit = AFI->getReturnRegsCount() - 4;
1822 DEBUG(dbgs() << AFI->getReturnRegsCount()
1823 << " return regs used, ExitRegDeficit = " << ExitRegDeficit
1824 << "\n");
1825
1826 int RegDeficit = std::max(EntryRegDeficit, ExitRegDeficit);
1827 DEBUG(dbgs() << "RegDeficit = " << RegDeficit << "\n");
1828
1829 // r4-r6 can be used in the prologue if they are pushed by the first push
1830 // instruction.
1831 for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6}) {
1832 if (SavedRegs.test(Reg)) {
1833 --RegDeficit;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001834 DEBUG(dbgs() << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001835 << " is saved low register, RegDeficit = " << RegDeficit
1836 << "\n");
1837 } else {
1838 AvailableRegs.push_back(Reg);
1839 DEBUG(dbgs()
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001840 << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001841 << " is non-saved low register, adding to AvailableRegs\n");
1842 }
1843 }
1844
1845 // r7 can be used if it is not being used as the frame pointer.
Matthias Braun44047422017-04-05 16:58:41 +00001846 if (!HasFP) {
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001847 if (SavedRegs.test(ARM::R7)) {
1848 --RegDeficit;
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001849 DEBUG(dbgs() << "%r7 is saved low register, RegDeficit = "
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001850 << RegDeficit << "\n");
1851 } else {
1852 AvailableRegs.push_back(ARM::R7);
1853 DEBUG(dbgs()
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001854 << "%r7 is non-saved low register, adding to AvailableRegs\n");
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001855 }
1856 }
1857
1858 // Each of r8-r11 needs to be copied to a low register, then pushed.
1859 for (unsigned Reg : {ARM::R8, ARM::R9, ARM::R10, ARM::R11}) {
1860 if (SavedRegs.test(Reg)) {
1861 ++RegDeficit;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001862 DEBUG(dbgs() << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001863 << " is saved high register, RegDeficit = " << RegDeficit
1864 << "\n");
1865 }
1866 }
1867
1868 // LR can only be used by PUSH, not POP, and can't be used at all if the
1869 // llvm.returnaddress intrinsic is used. This is only worth doing if we
1870 // are more limited at function entry than exit.
1871 if ((EntryRegDeficit > ExitRegDeficit) &&
1872 !(MF.getRegInfo().isLiveIn(ARM::LR) &&
1873 MF.getFrameInfo().isReturnAddressTaken())) {
1874 if (SavedRegs.test(ARM::LR)) {
1875 --RegDeficit;
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001876 DEBUG(dbgs() << "%lr is saved register, RegDeficit = " << RegDeficit
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001877 << "\n");
1878 } else {
1879 AvailableRegs.push_back(ARM::LR);
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001880 DEBUG(dbgs() << "%lr is not saved, adding to AvailableRegs\n");
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001881 }
1882 }
1883
1884 // If there are more high registers that need pushing than low registers
1885 // available, push some more low registers so that we can use fewer push
1886 // instructions. This might not reduce RegDeficit all the way to zero,
1887 // because we can only guarantee that r4-r6 are available, but r8-r11 may
1888 // need saving.
1889 DEBUG(dbgs() << "Final RegDeficit = " << RegDeficit << "\n");
1890 for (; RegDeficit > 0 && !AvailableRegs.empty(); --RegDeficit) {
1891 unsigned Reg = AvailableRegs.pop_back_val();
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001892 DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001893 << " to make up reg deficit\n");
1894 SavedRegs.set(Reg);
1895 NumGPRSpills++;
1896 CS1Spilled = true;
Matthias Braunc618a462017-07-28 01:36:32 +00001897 assert(!MRI.isReserved(Reg) && "Should not be reserved");
1898 if (!MRI.isPhysRegUsed(Reg))
1899 ExtraCSSpill = true;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001900 UnspilledCS1GPRs.erase(llvm::find(UnspilledCS1GPRs, Reg));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001901 if (Reg == ARM::LR)
1902 LRSpilled = true;
1903 }
1904 DEBUG(dbgs() << "After adding spills, RegDeficit = " << RegDeficit << "\n");
1905 }
1906
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001907 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1908 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1909 if (!LRSpilled && CS1Spilled) {
Matthias Braun02564862015-07-14 17:17:13 +00001910 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001911 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001912 SmallVectorImpl<unsigned>::iterator LRPos;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001913 LRPos = llvm::find(UnspilledCS1GPRs, (unsigned)ARM::LR);
Tim Northoverd8407452013-10-01 14:33:28 +00001914 if (LRPos != UnspilledCS1GPRs.end())
1915 UnspilledCS1GPRs.erase(LRPos);
1916
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001917 ForceLRSpill = false;
Matthias Braunc618a462017-07-28 01:36:32 +00001918 if (!MRI.isReserved(ARM::LR) && !MRI.isPhysRegUsed(ARM::LR))
1919 ExtraCSSpill = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001920 }
1921
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001922 // If stack and double are 8-byte aligned and we are spilling an odd number
1923 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1924 // the integer and double callee save areas.
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001925 DEBUG(dbgs() << "NumGPRSpills = " << NumGPRSpills << "\n");
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001926 unsigned TargetAlign = getStackAlignment();
Tim Northoverdc0d9e42014-11-05 00:27:20 +00001927 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001928 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1929 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1930 unsigned Reg = UnspilledCS1GPRs[i];
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001931 // Don't spill high register if the function is thumb. In the case of
1932 // Windows on ARM, accept R11 (frame pointer)
Peter Collingbourne78f1ecc2015-04-23 20:31:26 +00001933 if (!AFI->isThumbFunction() ||
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001934 (STI.isTargetWindows() && Reg == ARM::R11) ||
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001935 isARMLowRegister(Reg) || Reg == ARM::LR) {
Matthias Braun02564862015-07-14 17:17:13 +00001936 SavedRegs.set(Reg);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001937 DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001938 << " to make up alignment\n");
Matthias Braunc618a462017-07-28 01:36:32 +00001939 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001940 ExtraCSSpill = true;
1941 break;
1942 }
1943 }
1944 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1945 unsigned Reg = UnspilledCS2GPRs.front();
Matthias Braun02564862015-07-14 17:17:13 +00001946 SavedRegs.set(Reg);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001947 DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001948 << " to make up alignment\n");
Matthias Braunc618a462017-07-28 01:36:32 +00001949 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001950 ExtraCSSpill = true;
1951 }
1952 }
1953
1954 // Estimate if we might need to scavenge a register at some point in order
1955 // to materialize a stack offset. If so, either spill one additional
1956 // callee-saved register or reserve a special spill slot to facilitate
1957 // register scavenging. Thumb1 needs a spill slot for stack pointer
1958 // adjustments also, even when the frame itself is small.
Matthias Braun8aaa3682017-04-19 21:11:44 +00001959 if (BigFrameOffsets && !ExtraCSSpill) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001960 // If any non-reserved CS register isn't spilled, just spill one or two
1961 // extra. That should take care of it!
1962 unsigned NumExtras = TargetAlign / 4;
1963 SmallVector<unsigned, 2> Extras;
1964 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1965 unsigned Reg = UnspilledCS1GPRs.back();
1966 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001967 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001968 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1969 Reg == ARM::LR)) {
1970 Extras.push_back(Reg);
1971 NumExtras--;
1972 }
1973 }
1974 // For non-Thumb1 functions, also check for hi-reg CS registers
1975 if (!AFI->isThumb1OnlyFunction()) {
1976 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1977 unsigned Reg = UnspilledCS2GPRs.back();
1978 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001979 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001980 Extras.push_back(Reg);
1981 NumExtras--;
1982 }
1983 }
1984 }
Matthias Braunc618a462017-07-28 01:36:32 +00001985 if (NumExtras == 0) {
1986 for (unsigned Reg : Extras) {
1987 SavedRegs.set(Reg);
1988 if (!MRI.isPhysRegUsed(Reg))
1989 ExtraCSSpill = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001990 }
Matthias Braunc618a462017-07-28 01:36:32 +00001991 }
1992 if (!ExtraCSSpill && !AFI->isThumb1OnlyFunction()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001993 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1994 // closest to SP or frame pointer.
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001995 assert(RS && "Register scavenging not provided");
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001996 const TargetRegisterClass &RC = ARM::GPRRegClass;
1997 unsigned Size = TRI->getSpillSize(RC);
1998 unsigned Align = TRI->getSpillAlignment(RC);
1999 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00002000 }
2001 }
2002 }
2003
2004 if (ForceLRSpill) {
Matthias Braun02564862015-07-14 17:17:13 +00002005 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00002006 AFI->setLRIsSpilledForFarJump(true);
2007 }
2008}
Eli Bendersky8da87162013-02-21 20:05:00 +00002009
Hans Wennborge1a2e902016-03-31 18:33:38 +00002010MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
2011 MachineFunction &MF, MachineBasicBlock &MBB,
2012 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00002013 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00002014 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00002015 if (!hasReservedCallFrame(MF)) {
2016 // If we have alloca, convert as follows:
2017 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
2018 // ADJCALLSTACKUP -> add, sp, sp, amount
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00002019 MachineInstr &Old = *I;
2020 DebugLoc dl = Old.getDebugLoc();
Serge Pavlov5943a962017-04-19 03:12:05 +00002021 unsigned Amount = TII.getFrameSize(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +00002022 if (Amount != 0) {
2023 // We need to keep the stack aligned properly. To do this, we round the
2024 // amount of space needed for the outgoing arguments up to the next
2025 // alignment boundary.
Guozhi Weif66d3842015-08-17 22:36:27 +00002026 Amount = alignSPAdjust(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +00002027
2028 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2029 assert(!AFI->isThumb1OnlyFunction() &&
2030 "This eliminateCallFramePseudoInstr does not support Thumb1!");
2031 bool isARM = !AFI->isThumbFunction();
2032
2033 // Replace the pseudo instruction with a new instruction...
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00002034 unsigned Opc = Old.getOpcode();
2035 int PIdx = Old.findFirstPredOperandIdx();
2036 ARMCC::CondCodes Pred =
2037 (PIdx == -1) ? ARMCC::AL
2038 : (ARMCC::CondCodes)Old.getOperand(PIdx).getImm();
Serge Pavlov5943a962017-04-19 03:12:05 +00002039 unsigned PredReg = TII.getFramePred(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +00002040 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
Eli Bendersky8da87162013-02-21 20:05:00 +00002041 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
2042 Pred, PredReg);
2043 } else {
Eli Bendersky8da87162013-02-21 20:05:00 +00002044 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
2045 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
2046 Pred, PredReg);
2047 }
2048 }
2049 }
Hans Wennborge1a2e902016-03-31 18:33:38 +00002050 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00002051}
2052
Oliver Stannardb14c6252014-04-02 16:10:33 +00002053/// Get the minimum constant for ARM that is greater than or equal to the
2054/// argument. In ARM, constants can have any value that can be produced by
2055/// rotating an 8-bit value to the right by an even number of bits within a
2056/// 32-bit word.
2057static uint32_t alignToARMConstant(uint32_t Value) {
2058 unsigned Shifted = 0;
2059
2060 if (Value == 0)
2061 return 0;
2062
2063 while (!(Value & 0xC0000000)) {
2064 Value = Value << 2;
2065 Shifted += 2;
2066 }
2067
2068 bool Carry = (Value & 0x00FFFFFF);
2069 Value = ((Value & 0xFF000000) >> 24) + Carry;
2070
2071 if (Value & 0x0000100)
2072 Value = Value & 0x000001FC;
2073
2074 if (Shifted > 24)
2075 Value = Value >> (Shifted - 24);
2076 else
2077 Value = Value << (24 - Shifted);
2078
2079 return Value;
2080}
2081
2082// The stack limit in the TCB is set to this many bytes above the actual
2083// stack limit.
2084static const uint64_t kSplitStackAvailable = 256;
2085
2086// Adjust the function prologue to enable split stacks. This currently only
2087// supports android and linux.
2088//
2089// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
2090// must be well defined in order to allow for consistent implementations of the
2091// __morestack helper function. The ABI is also not a normal ABI in that it
2092// doesn't follow the normal calling conventions because this allows the
2093// prologue of each function to be optimized further.
2094//
2095// Currently, the ABI looks like (when calling __morestack)
2096//
2097// * r4 holds the minimum stack size requested for this function call
2098// * r5 holds the stack size of the arguments to the function
2099// * the beginning of the function is 3 instructions after the call to
2100// __morestack
2101//
2102// Implementations of __morestack should use r4 to allocate a new stack, r5 to
2103// place the arguments on to the new stack, and the 3-instruction knowledge to
2104// jump directly to the body of the function when working on the new stack.
2105//
2106// An old (and possibly no longer compatible) implementation of __morestack for
2107// ARM can be found at [1].
2108//
2109// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
Quentin Colombet61b305e2015-05-05 17:38:16 +00002110void ARMFrameLowering::adjustForSegmentedStacks(
2111 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
Oliver Stannardb14c6252014-04-02 16:10:33 +00002112 unsigned Opcode;
2113 unsigned CFIIndex;
Eric Christopher22b2ad22015-02-20 08:24:37 +00002114 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
Oliver Stannardb14c6252014-04-02 16:10:33 +00002115 bool Thumb = ST->isThumb();
2116
2117 // Sadly, this currently doesn't support varargs, platforms other than
2118 // android/linux. Note that thumb1/thumb2 are support for android/linux.
Matthias Braunf1caa282017-12-15 22:22:58 +00002119 if (MF.getFunction().isVarArg())
Oliver Stannardb14c6252014-04-02 16:10:33 +00002120 report_fatal_error("Segmented stacks do not support vararg functions.");
2121 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00002122 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00002123
Matthias Braun941a7052016-07-28 18:40:00 +00002124 MachineFrameInfo &MFI = MF.getFrameInfo();
Oliver Stannardb14c6252014-04-02 16:10:33 +00002125 MachineModuleInfo &MMI = MF.getMMI();
2126 MCContext &Context = MMI.getContext();
2127 const MCRegisterInfo *MRI = Context.getRegisterInfo();
2128 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00002129 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002130 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
2131 DebugLoc DL;
2132
Matthias Braun941a7052016-07-28 18:40:00 +00002133 uint64_t StackSize = MFI.getStackSize();
Tim Northoverf9e798b2014-05-22 13:03:43 +00002134
2135 // Do not generate a prologue for functions with a stack of size zero
2136 if (StackSize == 0)
2137 return;
2138
Oliver Stannardb14c6252014-04-02 16:10:33 +00002139 // Use R4 and R5 as scratch registers.
2140 // We save R4 and R5 before use and restore them before leaving the function.
2141 unsigned ScratchReg0 = ARM::R4;
2142 unsigned ScratchReg1 = ARM::R5;
2143 uint64_t AlignedStackSize;
2144
2145 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
2146 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
2147 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
2148 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
2149 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
2150
Quentin Colombet71a71482015-07-20 21:42:14 +00002151 // Grab everything that reaches PrologueMBB to update there liveness as well.
2152 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
2153 SmallVector<MachineBasicBlock *, 2> WalkList;
2154 WalkList.push_back(&PrologueMBB);
2155
2156 do {
2157 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
2158 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
2159 if (BeforePrologueRegion.insert(PredBB).second)
2160 WalkList.push_back(PredBB);
2161 }
2162 } while (!WalkList.empty());
2163
2164 // The order in that list is important.
2165 // The blocks will all be inserted before PrologueMBB using that order.
2166 // Therefore the block that should appear first in the CFG should appear
2167 // first in the list.
2168 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
2169 PostStackMBB};
Quentin Colombet71a71482015-07-20 21:42:14 +00002170
Craig Topper80720812015-12-01 06:13:01 +00002171 for (MachineBasicBlock *B : AddedBlocks)
2172 BeforePrologueRegion.insert(B);
Quentin Colombet71a71482015-07-20 21:42:14 +00002173
Matthias Braund9da1622015-09-09 18:08:03 +00002174 for (const auto &LI : PrologueMBB.liveins()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00002175 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
Matthias Braunb2b7ef12015-08-24 22:59:52 +00002176 PredBB->addLiveIn(LI);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002177 }
2178
Quentin Colombet71a71482015-07-20 21:42:14 +00002179 // Remove the newly added blocks from the list, since we know
2180 // we do not have to do the following updates for them.
Craig Topper80720812015-12-01 06:13:01 +00002181 for (MachineBasicBlock *B : AddedBlocks) {
2182 BeforePrologueRegion.erase(B);
2183 MF.insert(PrologueMBB.getIterator(), B);
Quentin Colombet71a71482015-07-20 21:42:14 +00002184 }
2185
2186 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
2187 // Make sure the LiveIns are still sorted and unique.
2188 MBB->sortUniqueLiveIns();
2189 // Replace the edges to PrologueMBB by edges to the sequences
2190 // we are about to add.
2191 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
2192 }
Oliver Stannardb14c6252014-04-02 16:10:33 +00002193
2194 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00002195 AlignedStackSize = alignToARMConstant(StackSize);
2196
2197 // When the frame size is less than 256 we just compare the stack
2198 // boundary directly to the value of the stack pointer, per gcc.
2199 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
2200
2201 // We will use two of the callee save registers as scratch registers so we
2202 // need to save those registers onto the stack.
2203 // We will use SR0 to hold stack limit and SR1 to hold the stack size
2204 // requested and arguments for __morestack().
2205 // SR0: Scratch Register #0
2206 // SR1: Scratch Register #1
2207 // push {SR0, SR1}
2208 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002209 BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))
2210 .add(predOps(ARMCC::AL))
2211 .addReg(ScratchReg0)
2212 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002213 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002214 BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
2215 .addReg(ARM::SP, RegState::Define)
2216 .addReg(ARM::SP)
2217 .add(predOps(ARMCC::AL))
2218 .addReg(ScratchReg0)
2219 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002220 }
2221
2222 // Emit the relevant DWARF information about the change in stack pointer as
2223 // well as where to find both r4 and r5 (the callee-save registers)
2224 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00002225 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002226 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2227 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002228 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002229 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
2230 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2231 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002232 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002233 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
2234 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2235 .addCFIIndex(CFIIndex);
2236
2237 // mov SR1, sp
2238 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002239 BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
2240 .addReg(ARM::SP)
2241 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002242 } else if (CompareStackPointer) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002243 BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
2244 .addReg(ARM::SP)
2245 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002246 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002247 }
2248
2249 // sub SR1, sp, #StackSize
2250 if (!CompareStackPointer && Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002251 BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)
2252 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002253 .addReg(ScratchReg1)
2254 .addImm(AlignedStackSize)
2255 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002256 } else if (!CompareStackPointer) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002257 BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
2258 .addReg(ARM::SP)
2259 .addImm(AlignedStackSize)
2260 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002261 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002262 }
2263
2264 if (Thumb && ST->isThumb1Only()) {
2265 unsigned PCLabelId = ARMFI->createPICLabelUId();
2266 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Matthias Braunf1caa282017-12-15 22:22:58 +00002267 MF.getFunction().getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002268 MachineConstantPool *MCP = MF.getConstantPool();
Tim Northover956b0082015-10-02 18:07:13 +00002269 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002270
2271 // ldr SR0, [pc, offset(STACK_LIMIT)]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002272 BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
2273 .addConstantPoolIndex(CPI)
2274 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002275
2276 // ldr SR0, [SR0]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002277 BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
2278 .addReg(ScratchReg0)
2279 .addImm(0)
2280 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002281 } else {
2282 // Get TLS base address from the coprocessor
2283 // mrc p15, #0, SR0, c13, c0, #3
Diana Picus4f8c3e12017-01-13 09:37:56 +00002284 BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2285 .addImm(15)
2286 .addImm(0)
2287 .addImm(13)
2288 .addImm(0)
2289 .addImm(3)
2290 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002291
2292 // Use the last tls slot on android and a private field of the TCP on linux.
2293 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2294 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2295
2296 // Get the stack limit from the right offset
2297 // ldr SR0, [sr0, #4 * TlsOffset]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002298 BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2299 .addReg(ScratchReg0)
2300 .addImm(4 * TlsOffset)
2301 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002302 }
2303
2304 // Compare stack limit with stack size requested.
2305 // cmp SR0, SR1
2306 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
Diana Picus4f8c3e12017-01-13 09:37:56 +00002307 BuildMI(GetMBB, DL, TII.get(Opcode))
2308 .addReg(ScratchReg0)
2309 .addReg(ScratchReg1)
2310 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002311
2312 // This jump is taken if StackLimit < SP - stack required.
2313 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2314 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2315 .addImm(ARMCC::LO)
2316 .addReg(ARM::CPSR);
2317
2318
2319 // Calling __morestack(StackSize, Size of stack arguments).
2320 // __morestack knows that the stack size requested is in SR0(r4)
2321 // and amount size of stack arguments is in SR1(r5).
2322
2323 // Pass first argument for the __morestack by Scratch Register #0.
2324 // The amount size of stack required
2325 if (Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002326 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg0)
2327 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002328 .addImm(AlignedStackSize)
2329 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002330 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002331 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2332 .addImm(AlignedStackSize)
2333 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002334 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002335 }
2336 // Pass second argument for the __morestack by Scratch Register #1.
2337 // The amount size of stack consumed to save function arguments.
2338 if (Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002339 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)
2340 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002341 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2342 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002343 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002344 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2345 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2346 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002347 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002348 }
2349
2350 // push {lr} - Save return address of this function.
2351 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002352 BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))
2353 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002354 .addReg(ARM::LR);
2355 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002356 BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2357 .addReg(ARM::SP, RegState::Define)
2358 .addReg(ARM::SP)
2359 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002360 .addReg(ARM::LR);
2361 }
2362
2363 // Emit the DWARF info about the change in stack as well as where to find the
2364 // previous link register
2365 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00002366 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002367 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2368 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002369 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002370 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2371 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2372 .addCFIIndex(CFIIndex);
2373
2374 // Call __morestack().
2375 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002376 BuildMI(AllocMBB, DL, TII.get(ARM::tBL))
2377 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002378 .addExternalSymbol("__morestack");
2379 } else {
2380 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2381 .addExternalSymbol("__morestack");
2382 }
2383
2384 // pop {lr} - Restore return address of this original function.
2385 if (Thumb) {
2386 if (ST->isThumb1Only()) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002387 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2388 .add(predOps(ARMCC::AL))
2389 .addReg(ScratchReg0);
2390 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2391 .addReg(ScratchReg0)
2392 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002393 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002394 BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2395 .addReg(ARM::LR, RegState::Define)
2396 .addReg(ARM::SP, RegState::Define)
2397 .addReg(ARM::SP)
2398 .addImm(4)
2399 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002400 }
2401 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002402 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2403 .addReg(ARM::SP, RegState::Define)
2404 .addReg(ARM::SP)
2405 .add(predOps(ARMCC::AL))
2406 .addReg(ARM::LR);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002407 }
2408
2409 // Restore SR0 and SR1 in case of __morestack() was called.
2410 // __morestack() will skip PostStackMBB block so we need to restore
2411 // scratch registers from here.
2412 // pop {SR0, SR1}
2413 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002414 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2415 .add(predOps(ARMCC::AL))
2416 .addReg(ScratchReg0)
2417 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002418 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002419 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2420 .addReg(ARM::SP, RegState::Define)
2421 .addReg(ARM::SP)
2422 .add(predOps(ARMCC::AL))
2423 .addReg(ScratchReg0)
2424 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002425 }
2426
2427 // Update the CFA offset now that we've popped
Matthias Braunf23ef432016-11-30 23:48:42 +00002428 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002429 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2430 .addCFIIndex(CFIIndex);
2431
Joerg Sonnenberger0f76a352017-08-28 20:20:47 +00002432 // Return from this function.
2433 BuildMI(AllocMBB, DL, TII.get(ST->getReturnOpcode())).add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002434
2435 // Restore SR0 and SR1 in case of __morestack() was not called.
2436 // pop {SR0, SR1}
2437 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002438 BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))
2439 .add(predOps(ARMCC::AL))
2440 .addReg(ScratchReg0)
2441 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002442 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002443 BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2444 .addReg(ARM::SP, RegState::Define)
2445 .addReg(ARM::SP)
2446 .add(predOps(ARMCC::AL))
2447 .addReg(ScratchReg0)
2448 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002449 }
2450
2451 // Update the CFA offset now that we've popped
Matthias Braunf23ef432016-11-30 23:48:42 +00002452 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002453 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2454 .addCFIIndex(CFIIndex);
2455
2456 // Tell debuggers that r4 and r5 are now the same as they were in the
2457 // previous function, that they're the "Same Value".
Matthias Braunf23ef432016-11-30 23:48:42 +00002458 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002459 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2460 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2461 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002462 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002463 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2464 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2465 .addCFIIndex(CFIIndex);
2466
2467 // Organizing MBB lists
Quentin Colombet61b305e2015-05-05 17:38:16 +00002468 PostStackMBB->addSuccessor(&PrologueMBB);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002469
2470 AllocMBB->addSuccessor(PostStackMBB);
2471
2472 GetMBB->addSuccessor(PostStackMBB);
2473 GetMBB->addSuccessor(AllocMBB);
2474
2475 McrMBB->addSuccessor(GetMBB);
2476
2477 PrevStackMBB->addSuccessor(McrMBB);
2478
Filipe Cabecinhas0da99372016-04-29 15:22:48 +00002479#ifdef EXPENSIVE_CHECKS
Oliver Stannardb14c6252014-04-02 16:10:33 +00002480 MF.verify();
2481#endif
2482}