blob: ce4add974d6ac1c518108a9feb6f8532aa9934a2 [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"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000037#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/CallingConv.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000039#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Function.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000041#include "llvm/MC/MCContext.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000042#include "llvm/MC/MCDwarf.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000043#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000044#include "llvm/MC/MCRegisterInfo.h"
45#include "llvm/Support/CodeGen.h"
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000046#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000047#include "llvm/Support/Compiler.h"
48#include "llvm/Support/Debug.h"
49#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
51#include "llvm/Support/raw_ostream.h"
52#include "llvm/Target/TargetInstrInfo.h"
53#include "llvm/Target/TargetMachine.h"
Eugene Zelenko076468c2017-09-20 21:35:51 +000054#include "llvm/Target/TargetOpcodes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000055#include "llvm/Target/TargetOptions.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000056#include "llvm/Target/TargetRegisterInfo.h"
57#include "llvm/Target/TargetSubtargetInfo.h"
58#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
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000090/// hasFP - Return true if the specified function should have a dedicated frame
91/// pointer register. This is true if the function has variable sized allocas
92/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000093bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +000094 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Oliver Stannard9aa6f012016-08-23 09:19:22 +000095 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000096
Oliver Stannard9aa6f012016-08-23 09:19:22 +000097 // ABI-required frame pointer.
98 if (MF.getTarget().Options.DisableFramePointerElim(MF))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000099 return true;
100
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000101 // Frame pointer required for use within this function.
102 return (RegInfo->needsStackRealignment(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000103 MFI.hasVarSizedObjects() ||
104 MFI.isFrameAddressTaken());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000105}
106
Bob Wilson657f2272011-01-13 21:10:12 +0000107/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
108/// not required, we reserve argument space for call sites in the function
109/// immediately on entry to the current function. This eliminates the need for
110/// add/sub sp brackets around call sites. Returns true if the call frame is
111/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000112bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000113 const MachineFrameInfo &MFI = MF.getFrameInfo();
114 unsigned CFSize = MFI.getMaxCallFrameSize();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000115 // It's not always a good idea to include the call frame as part of the
116 // stack frame. ARM (especially Thumb) has small immediate offset to
117 // address the stack frame. So a large call frame can cause poor codegen
118 // and may even makes it impossible to scavenge a register.
119 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
120 return false;
121
Matthias Braun941a7052016-07-28 18:40:00 +0000122 return !MFI.hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000123}
124
Bob Wilson657f2272011-01-13 21:10:12 +0000125/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
126/// call frame pseudos can be simplified. Unlike most targets, having a FP
127/// is not sufficient here since we still may reference some objects via SP
128/// even when FP is available in Thumb2 mode.
129bool
130ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000131 return hasReservedCallFrame(MF) || MF.getFrameInfo().hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000132}
133
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000134static bool isCSRestore(MachineInstr &MI, const ARMBaseInstrInfo &TII,
Craig Topper840beec2014-04-04 05:16:06 +0000135 const MCPhysReg *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000136 // Integer spill area is handled with "pop".
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000137 if (isPopOpcode(MI.getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +0000138 // The first two operands are predicates. The last two are
139 // imp-def and imp-use of SP. Check everything in between.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000140 for (int i = 5, e = MI.getNumOperands(); i != e; ++i)
141 if (!isCalleeSavedRegister(MI.getOperand(i).getReg(), CSRegs))
Eric Christopherb006fc92010-11-18 19:40:05 +0000142 return false;
143 return true;
144 }
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000145 if ((MI.getOpcode() == ARM::LDR_POST_IMM ||
146 MI.getOpcode() == ARM::LDR_POST_REG ||
147 MI.getOpcode() == ARM::t2LDR_POST) &&
148 isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs) &&
149 MI.getOperand(1).getReg() == ARM::SP)
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000150 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000151
152 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000153}
154
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000155static void emitRegPlusImmediate(
156 bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
157 const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
158 unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
159 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000160 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000161 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000162 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000163 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000164 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000165 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000166}
167
Tim Northoverc9432eb2013-11-04 23:04:15 +0000168static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000169 MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
Tim Northoverc9432eb2013-11-04 23:04:15 +0000170 const ARMBaseInstrInfo &TII, int NumBytes,
171 unsigned MIFlags = MachineInstr::NoFlags,
172 ARMCC::CondCodes Pred = ARMCC::AL,
173 unsigned PredReg = 0) {
174 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
175 MIFlags, Pred, PredReg);
176}
177
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000178static int sizeOfSPAdjustment(const MachineInstr &MI) {
Tim Northover603d3162014-11-14 22:45:33 +0000179 int RegSize;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000180 switch (MI.getOpcode()) {
Tim Northover603d3162014-11-14 22:45:33 +0000181 case ARM::VSTMDDB_UPD:
182 RegSize = 8;
183 break;
184 case ARM::STMDB_UPD:
185 case ARM::t2STMDB_UPD:
186 RegSize = 4;
187 break;
188 case ARM::t2STR_PRE:
189 case ARM::STR_PRE_IMM:
190 return 4;
191 default:
192 llvm_unreachable("Unknown push or pop like instruction");
193 }
194
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000195 int count = 0;
196 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
197 // pred) so the list starts at 4.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000198 for (int i = MI.getNumOperands() - 1; i >= 4; --i)
Tim Northover603d3162014-11-14 22:45:33 +0000199 count += RegSize;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000200 return count;
201}
202
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000203static bool WindowsRequiresStackProbe(const MachineFunction &MF,
204 size_t StackSizeInBytes) {
Matthias Braun941a7052016-07-28 18:40:00 +0000205 const MachineFrameInfo &MFI = MF.getFrameInfo();
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000206 const Function *F = MF.getFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000207 unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
Saleem Abdulrasoolfb8a66f2015-01-31 02:26:37 +0000208 if (F->hasFnAttribute("stack-probe-size"))
209 F->getFnAttribute("stack-probe-size")
210 .getValueAsString()
211 .getAsInteger(0, StackProbeSize);
212 return StackSizeInBytes >= StackProbeSize;
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000213}
214
Tim Northover603d3162014-11-14 22:45:33 +0000215namespace {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000216
Tim Northover603d3162014-11-14 22:45:33 +0000217struct StackAdjustingInsts {
218 struct InstInfo {
219 MachineBasicBlock::iterator I;
220 unsigned SPAdjust;
221 bool BeforeFPSet;
222 };
223
224 SmallVector<InstInfo, 4> Insts;
225
226 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
227 bool BeforeFPSet = false) {
228 InstInfo Info = {I, SPAdjust, BeforeFPSet};
229 Insts.push_back(Info);
230 }
231
232 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000233 auto Info =
234 llvm::find_if(Insts, [&](InstInfo &Info) { return Info.I == I; });
Tim Northover603d3162014-11-14 22:45:33 +0000235 assert(Info != Insts.end() && "invalid sp adjusting instruction");
236 Info->SPAdjust += ExtraBytes;
237 }
238
Matthias Braunf23ef432016-11-30 23:48:42 +0000239 void emitDefCFAOffsets(MachineBasicBlock &MBB, const DebugLoc &dl,
240 const ARMBaseInstrInfo &TII, bool HasFP) {
241 MachineFunction &MF = *MBB.getParent();
Tim Northover603d3162014-11-14 22:45:33 +0000242 unsigned CFAOffset = 0;
243 for (auto &Info : Insts) {
244 if (HasFP && !Info.BeforeFPSet)
245 return;
246
247 CFAOffset -= Info.SPAdjust;
Matthias Braunf23ef432016-11-30 23:48:42 +0000248 unsigned CFIIndex = MF.addFrameInst(
Tim Northover603d3162014-11-14 22:45:33 +0000249 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
250 BuildMI(MBB, std::next(Info.I), dl,
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000251 TII.get(TargetOpcode::CFI_INSTRUCTION))
252 .addCFIIndex(CFIIndex)
253 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000254 }
255 }
256};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000257
258} // end anonymous namespace
Tim Northover603d3162014-11-14 22:45:33 +0000259
Kristof Beyls933de7a2015-01-08 15:09:14 +0000260/// Emit an instruction sequence that will align the address in
261/// register Reg by zero-ing out the lower bits. For versions of the
262/// architecture that support Neon, this must be done in a single
263/// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
264/// single instruction. That function only gets called when optimizing
265/// spilling of D registers on a core with the Neon instruction set
266/// present.
267static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
268 const TargetInstrInfo &TII,
269 MachineBasicBlock &MBB,
270 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000271 const DebugLoc &DL, const unsigned Reg,
Kristof Beyls933de7a2015-01-08 15:09:14 +0000272 const unsigned Alignment,
273 const bool MustBeSingleInstruction) {
Eric Christopher1b21f002015-01-29 00:19:33 +0000274 const ARMSubtarget &AST =
275 static_cast<const ARMSubtarget &>(MF.getSubtarget());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000276 const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
277 const unsigned AlignMask = Alignment - 1;
278 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
279 assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
280 if (!AFI->isThumbFunction()) {
281 // if the BFC instruction is available, use that to zero the lower
282 // bits:
283 // bfc Reg, #0, log2(Alignment)
284 // otherwise use BIC, if the mask to zero the required number of bits
285 // can be encoded in the bic immediate field
286 // bic Reg, Reg, Alignment-1
287 // otherwise, emit
288 // lsr Reg, Reg, log2(Alignment)
289 // lsl Reg, Reg, log2(Alignment)
290 if (CanUseBFC) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000291 BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
292 .addReg(Reg, RegState::Kill)
293 .addImm(~AlignMask)
294 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000295 } else if (AlignMask <= 255) {
Diana Picus8a73f552017-01-13 10:18:01 +0000296 BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
297 .addReg(Reg, RegState::Kill)
298 .addImm(AlignMask)
299 .add(predOps(ARMCC::AL))
300 .add(condCodeOp());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000301 } else {
302 assert(!MustBeSingleInstruction &&
303 "Shouldn't call emitAligningInstructions demanding a single "
304 "instruction to be emitted for large stack alignment for a target "
305 "without BFC.");
Diana Picus8a73f552017-01-13 10:18:01 +0000306 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
307 .addReg(Reg, RegState::Kill)
308 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))
309 .add(predOps(ARMCC::AL))
310 .add(condCodeOp());
311 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
312 .addReg(Reg, RegState::Kill)
313 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))
314 .add(predOps(ARMCC::AL))
315 .add(condCodeOp());
Kristof Beyls933de7a2015-01-08 15:09:14 +0000316 }
317 } else {
318 // Since this is only reached for Thumb-2 targets, the BFC instruction
319 // should always be available.
320 assert(CanUseBFC);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000321 BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
322 .addReg(Reg, RegState::Kill)
323 .addImm(~AlignMask)
324 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000325 }
326}
327
Matthias Braun8aaa3682017-04-19 21:11:44 +0000328/// We need the offset of the frame pointer relative to other MachineFrameInfo
329/// offsets which are encoded relative to SP at function begin.
330/// See also emitPrologue() for how the FP is set up.
331/// Unfortunately we cannot determine this value in determineCalleeSaves() yet
332/// as assignCalleeSavedSpillSlots() hasn't run at this point. Instead we use
333/// this to produce a conservative estimate that we check in an assert() later.
334static int getMaxFPOffset(const Function &F, const ARMFunctionInfo &AFI) {
335 // This is a conservative estimation: Assume the frame pointer being r7 and
336 // pc("r15") up to r8 getting spilled before (= 8 registers).
337 return -AFI.getArgRegsSaveSize() - (8 * 4);
338}
339
Quentin Colombet61b305e2015-05-05 17:38:16 +0000340void ARMFrameLowering::emitPrologue(MachineFunction &MF,
341 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000342 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000343 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000344 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000345 MachineModuleInfo &MMI = MF.getMMI();
346 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000347 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000348 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopher1b21f002015-01-29 00:19:33 +0000349 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
350 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000351 assert(!AFI->isThumb1OnlyFunction() &&
352 "This emitPrologue does not support Thumb1!");
353 bool isARM = !AFI->isThumbFunction();
Eric Christopher1b21f002015-01-29 00:19:33 +0000354 unsigned Align = STI.getFrameLowering()->getStackAlignment();
Tim Northover775aaeb2015-11-05 21:54:58 +0000355 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000356 unsigned NumBytes = MFI.getStackSize();
357 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000358
359 // Debug location must be unknown since the first debug location is used
360 // to determine the end of the prologue.
361 DebugLoc dl;
362
363 unsigned FramePtr = RegInfo->getFrameRegister(MF);
364
365 // Determine the sizes of each callee-save spill areas and record which frame
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000366 // belongs to which callee-save spill areas.
367 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
368 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000369 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000370
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000371 // All calls are tail calls in GHC calling conv, and functions have no
372 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000373 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
374 return;
375
Tim Northover603d3162014-11-14 22:45:33 +0000376 StackAdjustingInsts DefCFAOffsetCandidates;
Sergey Dmitrouk3cc62b32015-04-08 10:10:12 +0000377 bool HasFP = hasFP(MF);
Tim Northover603d3162014-11-14 22:45:33 +0000378
Oliver Stannardd55e1152014-03-05 15:25:27 +0000379 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000380 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000381 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000382 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000383 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000384 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000385
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000386 if (!AFI->hasStackFrame() &&
387 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000388 if (NumBytes - ArgRegsSaveSize != 0) {
389 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000390 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000391 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
392 NumBytes - ArgRegsSaveSize, true);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000393 }
Matthias Braunf23ef432016-11-30 23:48:42 +0000394 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000395 return;
396 }
397
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000398 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000399 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
400 unsigned Reg = CSI[i].getReg();
401 int FI = CSI[i].getFrameIdx();
402 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000403 case ARM::R8:
404 case ARM::R9:
405 case ARM::R10:
406 case ARM::R11:
407 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000408 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000409 GPRCS2Size += 4;
410 break;
411 }
Justin Bognerb03fd122016-08-17 05:10:15 +0000412 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +0000413 case ARM::R0:
414 case ARM::R1:
415 case ARM::R2:
416 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000417 case ARM::R4:
418 case ARM::R5:
419 case ARM::R6:
420 case ARM::R7:
421 case ARM::LR:
422 if (Reg == FramePtr)
423 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000424 GPRCS1Size += 4;
425 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000426 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000427 // This is a DPR. Exclude the aligned DPRCS2 spills.
428 if (Reg == ARM::D8)
429 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000430 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000431 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000432 }
433 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000434
Eric Christopherb006fc92010-11-18 19:40:05 +0000435 // Move past area 1.
Tim Northover603d3162014-11-14 22:45:33 +0000436 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
437 if (GPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000438 GPRCS1Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000439 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
440 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000441
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000442 // Determine starting offsets of spill areas.
Tim Northover228c9432014-11-05 00:27:13 +0000443 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
444 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
445 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
446 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
447 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000448 int FramePtrOffsetInPush = 0;
449 if (HasFP) {
Matthias Braun8aaa3682017-04-19 21:11:44 +0000450 int FPOffset = MFI.getObjectOffset(FramePtrSpillFI);
451 assert(getMaxFPOffset(*MF.getFunction(), *AFI) <= FPOffset &&
452 "Max FP estimation is wrong");
453 FramePtrOffsetInPush = FPOffset + ArgRegsSaveSize;
Matthias Braun941a7052016-07-28 18:40:00 +0000454 AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000455 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000456 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000457 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
458 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
459 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
460
Tim Northoverc9432eb2013-11-04 23:04:15 +0000461 // Move past area 2.
Tim Northover603d3162014-11-14 22:45:33 +0000462 if (GPRCS2Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000463 GPRCS2Push = LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000464 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
465 }
Tim Northoverc9432eb2013-11-04 23:04:15 +0000466
Tim Northover228c9432014-11-05 00:27:13 +0000467 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
468 // .cfi_offset operations will reflect that.
469 if (DPRGapSize) {
470 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
Duncan P. N. Exon Smithec083b52016-08-17 00:53:04 +0000471 if (LastPush != MBB.end() &&
472 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, DPRGapSize))
Tim Northover603d3162014-11-14 22:45:33 +0000473 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
474 else {
Tim Northover228c9432014-11-05 00:27:13 +0000475 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
476 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000477 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
478 }
Tim Northover228c9432014-11-05 00:27:13 +0000479 }
480
Eric Christopherb006fc92010-11-18 19:40:05 +0000481 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000482 if (DPRCSSize > 0) {
Evan Cheng70d29632011-02-25 00:24:46 +0000483 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000484 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000485 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000486 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(*MBBI));
Tim Northover93bcc662013-11-08 17:18:07 +0000487 LastPush = MBBI++;
Tim Northover603d3162014-11-14 22:45:33 +0000488 }
Evan Cheng70d29632011-02-25 00:24:46 +0000489 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000490
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000491 // Move past the aligned DPRCS2 area.
492 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
493 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
494 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
495 // leaves the stack pointer pointing to the DPRCS2 area.
496 //
497 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
Matthias Braun941a7052016-07-28 18:40:00 +0000498 NumBytes += MFI.getObjectOffset(D8SpillFI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000499 } else
500 NumBytes = DPRCSOffset;
501
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000502 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
503 uint32_t NumWords = NumBytes >> 2;
504
505 if (NumWords < 65536)
Diana Picus4f8c3e12017-01-13 09:37:56 +0000506 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
507 .addImm(NumWords)
508 .setMIFlags(MachineInstr::FrameSetup)
509 .add(predOps(ARMCC::AL));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000510 else
511 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000512 .addImm(NumWords)
513 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000514
515 switch (TM.getCodeModel()) {
516 case CodeModel::Small:
517 case CodeModel::Medium:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000518 case CodeModel::Kernel:
519 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
Diana Picusbd66b7d2017-01-20 08:15:24 +0000520 .add(predOps(ARMCC::AL))
521 .addExternalSymbol("__chkstk")
522 .addReg(ARM::R4, RegState::Implicit)
523 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000524 break;
525 case CodeModel::Large:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000526 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000527 .addExternalSymbol("__chkstk")
528 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000529
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000530 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
Diana Picusbd66b7d2017-01-20 08:15:24 +0000531 .add(predOps(ARMCC::AL))
532 .addReg(ARM::R12, RegState::Kill)
533 .addReg(ARM::R4, RegState::Implicit)
534 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000535 break;
536 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000537
Diana Picus8a73f552017-01-13 10:18:01 +0000538 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), ARM::SP)
539 .addReg(ARM::SP, RegState::Kill)
540 .addReg(ARM::R4, RegState::Kill)
541 .setMIFlags(MachineInstr::FrameSetup)
542 .add(predOps(ARMCC::AL))
543 .add(condCodeOp());
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000544 NumBytes = 0;
545 }
546
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000547 if (NumBytes) {
548 // Adjust SP after all the callee-save spills.
Tim Northoverbeb5bcc2015-09-23 22:21:09 +0000549 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000550 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, NumBytes))
Tim Northover603d3162014-11-14 22:45:33 +0000551 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
552 else {
Tim Northover93bcc662013-11-08 17:18:07 +0000553 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
554 MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000555 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
556 }
Tim Northover93bcc662013-11-08 17:18:07 +0000557
Evan Chengeb56dca2010-11-22 18:12:04 +0000558 if (HasFP && isARM)
559 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
560 // Note it's not safe to do this in Thumb2 mode because it would have
561 // taken two instructions:
562 // mov sp, r7
563 // sub sp, #24
564 // If an interrupt is taken between the two instructions, then sp is in
565 // an inconsistent state (pointing to the middle of callee-saved area).
566 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000567 AFI->setShouldRestoreSPFromFP(true);
568 }
569
Tim Northover603d3162014-11-14 22:45:33 +0000570 // Set FP to point to the stack slot that contains the previous FP.
571 // For iOS, FP is R7, which has now been stored in spill area 1.
572 // Otherwise, if this is not iOS, all the callee-saved registers go
573 // into spill area 1, including the FP in R11. In either case, it
574 // is in area one and the adjustment needs to take place just after
575 // that push.
576 if (HasFP) {
577 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000578 unsigned PushSize = sizeOfSPAdjustment(*GPRCS1Push);
Tim Northover603d3162014-11-14 22:45:33 +0000579 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
580 dl, TII, FramePtr, ARM::SP,
581 PushSize + FramePtrOffsetInPush,
582 MachineInstr::FrameSetup);
583 if (FramePtrOffsetInPush + PushSize != 0) {
Matthias Braunf23ef432016-11-30 23:48:42 +0000584 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
Tim Northover603d3162014-11-14 22:45:33 +0000585 nullptr, MRI->getDwarfRegNum(FramePtr, true),
586 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
587 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000588 .addCFIIndex(CFIIndex)
589 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000590 } else {
591 unsigned CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +0000592 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
Tim Northover603d3162014-11-14 22:45:33 +0000593 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
594 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000595 .addCFIIndex(CFIIndex)
596 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover603d3162014-11-14 22:45:33 +0000597 }
598 }
599
600 // Now that the prologue's actual instructions are finalised, we can insert
601 // the necessary DWARF cf instructions to describe the situation. Start by
602 // recording where each register ended up:
603 if (GPRCS1Size > 0) {
604 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
605 int CFIIndex;
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000606 for (const auto &Entry : CSI) {
607 unsigned Reg = Entry.getReg();
608 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000609 switch (Reg) {
610 case ARM::R8:
611 case ARM::R9:
612 case ARM::R10:
613 case ARM::R11:
614 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000615 if (STI.splitFramePushPop(MF))
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000616 break;
Justin Bognerb03fd122016-08-17 05:10:15 +0000617 LLVM_FALLTHROUGH;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000618 case ARM::R0:
619 case ARM::R1:
620 case ARM::R2:
621 case ARM::R3:
622 case ARM::R4:
623 case ARM::R5:
624 case ARM::R6:
625 case ARM::R7:
626 case ARM::LR:
Matthias Braunf23ef432016-11-30 23:48:42 +0000627 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Matthias Braun941a7052016-07-28 18:40:00 +0000628 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000629 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000630 .addCFIIndex(CFIIndex)
631 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000632 break;
633 }
634 }
635 }
636
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000637 if (GPRCS2Size > 0) {
Tim Northover603d3162014-11-14 22:45:33 +0000638 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000639 for (const auto &Entry : CSI) {
640 unsigned Reg = Entry.getReg();
641 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000642 switch (Reg) {
643 case ARM::R8:
644 case ARM::R9:
645 case ARM::R10:
646 case ARM::R11:
647 case ARM::R12:
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000648 if (STI.splitFramePushPop(MF)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000649 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000650 unsigned Offset = MFI.getObjectOffset(FI);
Matthias Braunf23ef432016-11-30 23:48:42 +0000651 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000652 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
653 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000654 .addCFIIndex(CFIIndex)
655 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000656 }
657 break;
658 }
659 }
660 }
661
662 if (DPRCSSize > 0) {
663 // Since vpush register list cannot have gaps, there may be multiple vpush
664 // instructions in the prologue.
Tim Northover603d3162014-11-14 22:45:33 +0000665 MachineBasicBlock::iterator Pos = std::next(LastPush);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000666 for (const auto &Entry : CSI) {
667 unsigned Reg = Entry.getReg();
668 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000669 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
670 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
671 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braun941a7052016-07-28 18:40:00 +0000672 unsigned Offset = MFI.getObjectOffset(FI);
Matthias Braunf23ef432016-11-30 23:48:42 +0000673 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000674 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover603d3162014-11-14 22:45:33 +0000675 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000676 .addCFIIndex(CFIIndex)
677 .setMIFlags(MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000678 }
679 }
680 }
681
Tim Northover603d3162014-11-14 22:45:33 +0000682 // Now we can emit descriptions of where the canonical frame address was
683 // throughout the process. If we have a frame pointer, it takes over the job
684 // half-way through, so only the first few .cfi_def_cfa_offset instructions
685 // actually get emitted.
Matthias Braunf23ef432016-11-30 23:48:42 +0000686 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
Tim Northover93bcc662013-11-08 17:18:07 +0000687
Evan Chengeb56dca2010-11-22 18:12:04 +0000688 if (STI.isTargetELF() && hasFP(MF))
Matthias Braun941a7052016-07-28 18:40:00 +0000689 MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
690 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000691
692 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
693 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000694 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000695 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
696
697 // If we need dynamic stack realignment, do it here. Be paranoid and make
698 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000699 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000700 // realigned.
701 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000702 unsigned MaxAlign = MFI.getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +0000703 assert(!AFI->isThumb1OnlyFunction());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000704 if (!AFI->isThumbFunction()) {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000705 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
706 false);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000707 } else {
Kristof Beyls933de7a2015-01-08 15:09:14 +0000708 // We cannot use sp as source/dest register here, thus we're using r4 to
709 // perform the calculations. We're emitting the following sequence:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000710 // mov r4, sp
Kristof Beyls933de7a2015-01-08 15:09:14 +0000711 // -- use emitAligningInstructions to produce best sequence to zero
712 // -- out lower bits in r4
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000713 // mov sp, r4
714 // FIXME: It will be better just to find spare register here.
Diana Picus4f8c3e12017-01-13 09:37:56 +0000715 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
716 .addReg(ARM::SP, RegState::Kill)
717 .add(predOps(ARMCC::AL));
Kristof Beyls933de7a2015-01-08 15:09:14 +0000718 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
719 false);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000720 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
721 .addReg(ARM::R4, RegState::Kill)
722 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000723 }
724
725 AFI->setShouldRestoreSPFromFP(true);
726 }
727
728 // If we need a base pointer, set it up here. It's whatever the value
729 // of the stack pointer is at this point. Any variable size objects
730 // will be allocated after this, so we can still use the base pointer
731 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000732 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000733 if (RegInfo->hasBasePointer(MF)) {
734 if (isARM)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000735 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), RegInfo->getBaseRegister())
736 .addReg(ARM::SP)
737 .add(predOps(ARMCC::AL))
738 .add(condCodeOp());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000739 else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000740 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), RegInfo->getBaseRegister())
741 .addReg(ARM::SP)
742 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000743 }
744
745 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000746 // the sp from fp. We can assume there's an FP here since hasFP already
747 // checks for hasVarSizedObjects.
Matthias Braun941a7052016-07-28 18:40:00 +0000748 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000749 AFI->setShouldRestoreSPFromFP(true);
750}
751
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000752void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000753 MachineBasicBlock &MBB) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000754 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000755 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000756 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000757 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000758 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000759 assert(!AFI->isThumb1OnlyFunction() &&
760 "This emitEpilogue does not support Thumb1!");
761 bool isARM = !AFI->isThumbFunction();
762
Tim Northover8cda34f2015-03-11 18:54:22 +0000763 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
Matthias Braun941a7052016-07-28 18:40:00 +0000764 int NumBytes = (int)MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000765 unsigned FramePtr = RegInfo->getFrameRegister(MF);
766
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000767 // All calls are tail calls in GHC calling conv, and functions have no
768 // prologue/epilogue.
Quentin Colombet71a71482015-07-20 21:42:14 +0000769 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
Eric Christopherb3322362012-08-03 00:05:53 +0000770 return;
Quentin Colombet71a71482015-07-20 21:42:14 +0000771
772 // First put ourselves on the first (from top) terminator instructions.
773 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
774 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Eric Christopherb3322362012-08-03 00:05:53 +0000775
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000776 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000777 if (NumBytes - ArgRegsSaveSize != 0)
778 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000779 } else {
780 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000781 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000782 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000783 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000784 --MBBI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000785 } while (MBBI != MBB.begin() && isCSRestore(*MBBI, TII, CSRegs));
786 if (!isCSRestore(*MBBI, TII, CSRegs))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000787 ++MBBI;
788 }
789
790 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000791 NumBytes -= (ArgRegsSaveSize +
792 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000793 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000794 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000795 AFI->getDPRCalleeSavedAreaSize());
796
797 // Reset SP based on frame pointer only if the stack frame extends beyond
798 // frame pointer stack slot or target is ELF and the function has FP.
799 if (AFI->shouldRestoreSPFromFP()) {
800 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
801 if (NumBytes) {
802 if (isARM)
803 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
804 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000805 else {
806 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000807 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000808 // mov sp, r7
809 // sub sp, #24
810 // This is bad, if an interrupt is taken after the mov, sp is in an
811 // inconsistent state.
812 // Use the first callee-saved register as a scratch register.
Matthias Braun941a7052016-07-28 18:40:00 +0000813 assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000814 "No scratch register to restore SP from FP!");
815 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000816 ARMCC::AL, 0, TII);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000817 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
818 .addReg(ARM::R4)
819 .add(predOps(ARMCC::AL));
Evan Chengeb56dca2010-11-22 18:12:04 +0000820 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000821 } else {
822 // Thumb2 or ARM.
823 if (isARM)
824 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000825 .addReg(FramePtr)
826 .add(predOps(ARMCC::AL))
827 .add(condCodeOp());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000828 else
Diana Picus4f8c3e12017-01-13 09:37:56 +0000829 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
830 .addReg(FramePtr)
831 .add(predOps(ARMCC::AL));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000832 }
Tim Northoverdee86042013-12-02 14:46:26 +0000833 } else if (NumBytes &&
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000834 !tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
835 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000836
Eric Christopherb006fc92010-11-18 19:40:05 +0000837 // Increment past our save areas.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000838 if (MBBI != MBB.end() && AFI->getDPRCalleeSavedAreaSize()) {
Evan Cheng70d29632011-02-25 00:24:46 +0000839 MBBI++;
840 // Since vpop register list cannot have gaps, there may be multiple vpop
841 // instructions in the epilogue.
Duncan P. N. Exon Smith8f44c982016-08-21 00:08:10 +0000842 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VLDMDIA_UPD)
Evan Cheng70d29632011-02-25 00:24:46 +0000843 MBBI++;
844 }
Tim Northover228c9432014-11-05 00:27:13 +0000845 if (AFI->getDPRCalleeSavedGapSize()) {
846 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
847 "unexpected DPR alignment gap");
848 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
849 }
850
Eric Christopherb006fc92010-11-18 19:40:05 +0000851 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
852 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000853 }
854
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000855 if (ArgRegsSaveSize)
856 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000857}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000858
Bob Wilson657f2272011-01-13 21:10:12 +0000859/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
860/// debug info. It's the same as what we use for resolving the code-gen
861/// references for now. FIXME: This can go wrong when references are
862/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000863int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000864ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000865 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000866 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
867}
868
869int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000870ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000871 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000872 int SPAdj) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000873 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000874 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000875 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000876 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000877 int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +0000878 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000879 bool isFixed = MFI.isFixedObjectIndex(FI);
Anton Korobeynikov46877782010-11-20 15:59:32 +0000880
881 FrameReg = ARM::SP;
882 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000883
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000884 // SP can move around if there are allocas. We may also lose track of SP
885 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000886 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000887
Anton Korobeynikov46877782010-11-20 15:59:32 +0000888 // When dynamically realigning the stack, use the frame pointer for
889 // parameters, and the stack/base pointer for locals.
890 if (RegInfo->needsStackRealignment(MF)) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000891 assert(hasFP(MF) && "dynamic stack realignment without a FP!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000892 if (isFixed) {
893 FrameReg = RegInfo->getFrameRegister(MF);
894 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000895 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000896 assert(RegInfo->hasBasePointer(MF) &&
897 "VLAs and dynamic stack alignment, but missing base pointer!");
898 FrameReg = RegInfo->getBaseRegister();
899 }
900 return Offset;
901 }
902
903 // If there is a frame pointer, use it when we can.
904 if (hasFP(MF) && AFI->hasStackFrame()) {
905 // Use frame pointer to reference fixed objects. Use it for locals if
906 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000907 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000908 FrameReg = RegInfo->getFrameRegister(MF);
909 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000910 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000911 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000912 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000913 // Try to use the frame pointer if we can, else use the base pointer
914 // since it's available. This is handy for the emergency spill slot, in
915 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000916 if (FPOffset >= -255 && FPOffset < 0) {
917 FrameReg = RegInfo->getFrameRegister(MF);
918 return FPOffset;
919 }
Evan Chengc0d20042011-04-22 01:42:52 +0000920 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000921 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000922 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000923 // ldr <rd>, [sp, #<imm8>]
924 // if at all possible to save space.
925 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
926 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000927 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000928 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000929 if (FPOffset >= -255 && FPOffset < 0) {
930 FrameReg = RegInfo->getFrameRegister(MF);
931 return FPOffset;
932 }
933 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
934 // Otherwise, use SP or FP, whichever is closer to the stack slot.
935 FrameReg = RegInfo->getFrameRegister(MF);
936 return FPOffset;
937 }
938 }
939 // Use the base pointer if we have one.
940 if (RegInfo->hasBasePointer(MF))
941 FrameReg = RegInfo->getBaseRegister();
942 return Offset;
943}
944
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000945void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000946 MachineBasicBlock::iterator MI,
947 const std::vector<CalleeSavedInfo> &CSI,
948 unsigned StmOpc, unsigned StrOpc,
949 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000950 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000951 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000952 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000953 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000954 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover46a6f0f2016-11-14 20:28:24 +0000955 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000956
957 DebugLoc DL;
958
Eugene Zelenko076468c2017-09-20 21:35:51 +0000959 using RegAndKill = std::pair<unsigned, bool>;
960
Tim Northover46a6f0f2016-11-14 20:28:24 +0000961 SmallVector<RegAndKill, 4> Regs;
Tim Northover775aaeb2015-11-05 21:54:58 +0000962 unsigned i = CSI.size();
Evan Cheng775ead32010-12-07 23:08:38 +0000963 while (i != 0) {
964 unsigned LastReg = 0;
965 for (; i != 0; --i) {
966 unsigned Reg = CSI[i-1].getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +0000967 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000968
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000969 // D-registers in the aligned area DPRCS2 are NOT spilled here.
970 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
971 continue;
972
Matthias Braun0dba4e32017-05-31 01:21:30 +0000973 const MachineRegisterInfo &MRI = MF.getRegInfo();
974 bool isLiveIn = MRI.isLiveIn(Reg);
975 if (!isLiveIn && !MRI.isReserved(Reg))
Evan Cheng775ead32010-12-07 23:08:38 +0000976 MBB.addLiveIn(Reg);
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000977 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000978 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000979 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000980 if (NoGap && LastReg && LastReg != Reg-1)
981 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000982 LastReg = Reg;
Matthias Braun707e02c2016-04-13 21:43:25 +0000983 // Do not set a kill flag on values that are also marked as live-in. This
984 // happens with the @llvm-returnaddress intrinsic and with arguments
985 // passed in callee saved registers.
986 // Omitting the kill flags is conservatively correct even if the live-in
987 // is not used after all.
988 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000989 }
990
Jim Grosbach5fccad82010-12-09 18:31:13 +0000991 if (Regs.empty())
992 continue;
Tim Northover46a6f0f2016-11-14 20:28:24 +0000993
Tim Northover3d38c382016-11-14 20:31:53 +0000994 std::sort(Regs.begin(), Regs.end(), [&](const RegAndKill &LHS,
995 const RegAndKill &RHS) {
Tim Northover46a6f0f2016-11-14 20:28:24 +0000996 return TRI.getEncodingValue(LHS.first) < TRI.getEncodingValue(RHS.first);
997 });
998
Jim Grosbach5fccad82010-12-09 18:31:13 +0000999 if (Regs.size() > 1 || StrOpc== 0) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001000 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
1001 .addReg(ARM::SP)
1002 .setMIFlags(MIFlags)
1003 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001004 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1005 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +00001006 } else if (Regs.size() == 1) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001007 BuildMI(MBB, MI, DL, TII.get(StrOpc), ARM::SP)
1008 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
1009 .addReg(ARM::SP)
1010 .setMIFlags(MIFlags)
1011 .addImm(-4)
1012 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001013 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001014 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001015
1016 // Put any subsequent vpush instructions before this one: they will refer to
1017 // higher register numbers so need to be pushed first in order to preserve
1018 // monotonicity.
Quentin Colombet71a71482015-07-20 21:42:14 +00001019 if (MI != MBB.begin())
1020 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001021 }
Evan Cheng775ead32010-12-07 23:08:38 +00001022}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001023
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001024void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001025 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001026 std::vector<CalleeSavedInfo> &CSI,
Bob Wilson657f2272011-01-13 21:10:12 +00001027 unsigned LdmOpc, unsigned LdrOpc,
1028 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001029 bool(*Func)(unsigned, bool),
1030 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +00001031 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001032 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover46a6f0f2016-11-14 20:28:24 +00001033 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Evan Cheng775ead32010-12-07 23:08:38 +00001034 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet71a71482015-07-20 21:42:14 +00001035 DebugLoc DL;
1036 bool isTailCall = false;
1037 bool isInterrupt = false;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001038 bool isTrap = false;
Quentin Colombet71a71482015-07-20 21:42:14 +00001039 if (MBB.end() != MI) {
1040 DL = MI->getDebugLoc();
1041 unsigned RetOpcode = MI->getOpcode();
1042 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
1043 isInterrupt =
1044 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001045 isTrap =
1046 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
1047 RetOpcode == ARM::tTRAP;
Quentin Colombet71a71482015-07-20 21:42:14 +00001048 }
Evan Cheng775ead32010-12-07 23:08:38 +00001049
1050 SmallVector<unsigned, 4> Regs;
1051 unsigned i = CSI.size();
1052 while (i != 0) {
1053 unsigned LastReg = 0;
1054 bool DeleteRet = false;
1055 for (; i != 0; --i) {
Matthias Braun51687912017-09-28 23:12:06 +00001056 CalleeSavedInfo &Info = CSI[i-1];
1057 unsigned Reg = Info.getReg();
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001058 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
Evan Cheng775ead32010-12-07 23:08:38 +00001059
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001060 // The aligned reloads from area DPRCS2 are not inserted here.
1061 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
1062 continue;
1063
Tim Northoverd8407452013-10-01 14:33:28 +00001064 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
Oleg Ranevskyy6389dd92015-10-23 17:17:59 +00001065 !isTrap && STI.hasV5TOps()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00001066 if (MBB.succ_empty()) {
1067 Reg = ARM::PC;
1068 DeleteRet = true;
1069 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Matthias Braun51687912017-09-28 23:12:06 +00001070 // We 'restore' LR into PC so it is not live out of the return block:
1071 // Clear Restored bit.
1072 Info.setRestored(false);
Quentin Colombet71a71482015-07-20 21:42:14 +00001073 } else
1074 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Evan Cheng775ead32010-12-07 23:08:38 +00001075 // Fold the return instruction into the LDM.
Evan Cheng775ead32010-12-07 23:08:38 +00001076 }
1077
Evan Cheng9d54ae62010-12-08 06:29:02 +00001078 // If NoGap is true, pop consecutive registers and then leave the rest
1079 // for other instructions. e.g.
1080 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1081 if (NoGap && LastReg && LastReg != Reg-1)
1082 break;
1083
Evan Cheng775ead32010-12-07 23:08:38 +00001084 LastReg = Reg;
1085 Regs.push_back(Reg);
1086 }
1087
Jim Grosbach5fccad82010-12-09 18:31:13 +00001088 if (Regs.empty())
1089 continue;
Tim Northover46a6f0f2016-11-14 20:28:24 +00001090
1091 std::sort(Regs.begin(), Regs.end(), [&](unsigned LHS, unsigned RHS) {
1092 return TRI.getEncodingValue(LHS) < TRI.getEncodingValue(RHS);
1093 });
1094
Jim Grosbach5fccad82010-12-09 18:31:13 +00001095 if (Regs.size() > 1 || LdrOpc == 0) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001096 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
1097 .addReg(ARM::SP)
1098 .add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001099 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1100 MIB.addReg(Regs[i], getDefRegState(true));
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001101 if (DeleteRet) {
1102 if (MI != MBB.end()) {
1103 MIB.copyImplicitOps(*MI);
1104 MI->eraseFromParent();
1105 }
Andrew Trick6446bf72011-08-25 17:50:53 +00001106 }
Evan Cheng775ead32010-12-07 23:08:38 +00001107 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +00001108 } else if (Regs.size() == 1) {
1109 // If we adjusted the reg to PC from LR above, switch it back here. We
1110 // only do that for LDM.
1111 if (Regs[0] == ARM::PC)
1112 Regs[0] = ARM::LR;
1113 MachineInstrBuilder MIB =
1114 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1115 .addReg(ARM::SP, RegState::Define)
1116 .addReg(ARM::SP);
1117 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1118 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +00001119 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +00001120 MIB.addReg(0);
1121 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1122 } else
1123 MIB.addImm(4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001124 MIB.add(predOps(ARMCC::AL));
Evan Cheng775ead32010-12-07 23:08:38 +00001125 }
Jim Grosbach5fccad82010-12-09 18:31:13 +00001126 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +00001127
1128 // Put any subsequent vpop instructions after this one: they will refer to
1129 // higher register numbers so need to be popped afterwards.
Quentin Colombet71a71482015-07-20 21:42:14 +00001130 if (MI != MBB.end())
1131 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +00001132 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001133}
1134
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001135/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +00001136/// starting from d8. Also insert stack realignment code and leave the stack
1137/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001138static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1139 MachineBasicBlock::iterator MI,
1140 unsigned NumAlignedDPRCS2Regs,
1141 const std::vector<CalleeSavedInfo> &CSI,
1142 const TargetRegisterInfo *TRI) {
1143 MachineFunction &MF = *MBB.getParent();
1144 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001145 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001146 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001147 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001148
1149 // Mark the D-register spill slots as properly aligned. Since MFI computes
1150 // stack slot layout backwards, this can actually mean that the d-reg stack
1151 // slot offsets can be wrong. The offset for d8 will always be correct.
1152 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1153 unsigned DNum = CSI[i].getReg() - ARM::D8;
Tim Northovere0ccdc62015-10-28 22:46:43 +00001154 if (DNum > NumAlignedDPRCS2Regs - 1)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001155 continue;
1156 int FI = CSI[i].getFrameIdx();
1157 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1158 // registers will be 8-byte aligned.
1159 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1160
1161 // The stack slot for D8 needs to be maximally aligned because this is
1162 // actually the point where we align the stack pointer. MachineFrameInfo
1163 // computes all offsets relative to the incoming stack pointer which is a
1164 // bit weird when realigning the stack. Any extra padding for this
1165 // over-alignment is not realized because the code inserted below adjusts
1166 // the stack pointer by numregs * 8 before aligning the stack pointer.
1167 if (DNum == 0)
1168 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1169 }
1170
1171 // Move the stack pointer to the d8 spill slot, and align it at the same
1172 // time. Leave the stack slot address in the scratch register r4.
1173 //
1174 // sub r4, sp, #numregs * 8
1175 // bic r4, r4, #align - 1
1176 // mov sp, r4
1177 //
1178 bool isThumb = AFI->isThumbFunction();
1179 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1180 AFI->setShouldRestoreSPFromFP(true);
1181
1182 // sub r4, sp, #numregs * 8
1183 // The immediate is <= 64, so it doesn't need any special encoding.
1184 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
Diana Picus8a73f552017-01-13 10:18:01 +00001185 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1186 .addReg(ARM::SP)
1187 .addImm(8 * NumAlignedDPRCS2Regs)
1188 .add(predOps(ARMCC::AL))
1189 .add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001190
Matthias Braun941a7052016-07-28 18:40:00 +00001191 unsigned MaxAlign = MF.getFrameInfo().getMaxAlignment();
Kristof Beyls933de7a2015-01-08 15:09:14 +00001192 // We must set parameter MustBeSingleInstruction to true, since
1193 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1194 // stack alignment. Luckily, this can always be done since all ARM
1195 // architecture versions that support Neon also support the BFC
1196 // instruction.
1197 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001198
1199 // mov sp, r4
1200 // The stack pointer must be adjusted before spilling anything, otherwise
1201 // the stack slots could be clobbered by an interrupt handler.
1202 // Leave r4 live, it is used below.
1203 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1204 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
Diana Picus4f8c3e12017-01-13 09:37:56 +00001205 .addReg(ARM::R4)
1206 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001207 if (!isThumb)
Diana Picus8a73f552017-01-13 10:18:01 +00001208 MIB.add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001209
1210 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1211 // r4 holds the stack slot address.
1212 unsigned NextReg = ARM::D8;
1213
1214 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1215 // The writeback is only needed when emitting two vst1.64 instructions.
1216 if (NumAlignedDPRCS2Regs >= 6) {
1217 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001218 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001219 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001220 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), ARM::R4)
1221 .addReg(ARM::R4, RegState::Kill)
1222 .addImm(16)
1223 .addReg(NextReg)
1224 .addReg(SupReg, RegState::ImplicitKill)
1225 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001226 NextReg += 4;
1227 NumAlignedDPRCS2Regs -= 4;
1228 }
1229
1230 // We won't modify r4 beyond this point. It currently points to the next
1231 // register to be spilled.
1232 unsigned R4BaseReg = NextReg;
1233
1234 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1235 if (NumAlignedDPRCS2Regs >= 4) {
1236 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001237 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001238 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001239 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1240 .addReg(ARM::R4)
1241 .addImm(16)
1242 .addReg(NextReg)
1243 .addReg(SupReg, RegState::ImplicitKill)
1244 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001245 NextReg += 4;
1246 NumAlignedDPRCS2Regs -= 4;
1247 }
1248
1249 // 16-byte aligned vst1.64 with 2 d-regs.
1250 if (NumAlignedDPRCS2Regs >= 2) {
1251 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001252 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001253 MBB.addLiveIn(SupReg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001254 BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
1255 .addReg(ARM::R4)
1256 .addImm(16)
1257 .addReg(SupReg)
1258 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001259 NextReg += 2;
1260 NumAlignedDPRCS2Regs -= 2;
1261 }
1262
1263 // Finally, use a vanilla vstr.64 for the odd last register.
1264 if (NumAlignedDPRCS2Regs) {
1265 MBB.addLiveIn(NextReg);
1266 // vstr.64 uses addrmode5 which has an offset scale of 4.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001267 BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1268 .addReg(NextReg)
1269 .addReg(ARM::R4)
1270 .addImm((NextReg - R4BaseReg) * 2)
1271 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001272 }
1273
1274 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001275 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001276}
1277
1278/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1279/// iterator to the following instruction.
1280static MachineBasicBlock::iterator
1281skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1282 unsigned NumAlignedDPRCS2Regs) {
1283 // sub r4, sp, #numregs * 8
1284 // bic r4, r4, #align - 1
1285 // mov sp, r4
1286 ++MI; ++MI; ++MI;
1287 assert(MI->mayStore() && "Expecting spill instruction");
1288
1289 // These switches all fall through.
1290 switch(NumAlignedDPRCS2Regs) {
1291 case 7:
1292 ++MI;
1293 assert(MI->mayStore() && "Expecting spill instruction");
Florian Hahndb479522017-07-27 14:37:17 +00001294 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001295 default:
1296 ++MI;
1297 assert(MI->mayStore() && "Expecting spill instruction");
Florian Hahndb479522017-07-27 14:37:17 +00001298 LLVM_FALLTHROUGH;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001299 case 1:
1300 case 2:
1301 case 4:
1302 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1303 ++MI;
1304 }
1305 return MI;
1306}
1307
1308/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1309/// starting from d8. These instructions are assumed to execute while the
1310/// stack is still aligned, unlike the code inserted by emitPopInst.
1311static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1312 MachineBasicBlock::iterator MI,
1313 unsigned NumAlignedDPRCS2Regs,
1314 const std::vector<CalleeSavedInfo> &CSI,
1315 const TargetRegisterInfo *TRI) {
1316 MachineFunction &MF = *MBB.getParent();
1317 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Quentin Colombet5084e442015-10-15 00:41:26 +00001318 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001319 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001320
1321 // Find the frame index assigned to d8.
1322 int D8SpillFI = 0;
1323 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1324 if (CSI[i].getReg() == ARM::D8) {
1325 D8SpillFI = CSI[i].getFrameIdx();
1326 break;
1327 }
1328
1329 // Materialize the address of the d8 spill slot into the scratch register r4.
1330 // This can be fairly complicated if the stack frame is large, so just use
1331 // the normal frame index elimination mechanism to do it. This code runs as
1332 // the initial part of the epilog where the stack and base pointers haven't
1333 // been changed yet.
1334 bool isThumb = AFI->isThumbFunction();
1335 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1336
1337 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
Diana Picus8a73f552017-01-13 10:18:01 +00001338 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1339 .addFrameIndex(D8SpillFI)
1340 .addImm(0)
1341 .add(predOps(ARMCC::AL))
1342 .add(condCodeOp());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001343
1344 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1345 unsigned NextReg = ARM::D8;
1346
1347 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1348 if (NumAlignedDPRCS2Regs >= 6) {
1349 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001350 &ARM::QQPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001351 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1352 .addReg(ARM::R4, RegState::Define)
1353 .addReg(ARM::R4, RegState::Kill)
1354 .addImm(16)
1355 .addReg(SupReg, RegState::ImplicitDefine)
1356 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001357 NextReg += 4;
1358 NumAlignedDPRCS2Regs -= 4;
1359 }
1360
1361 // We won't modify r4 beyond this point. It currently points to the next
1362 // register to be spilled.
1363 unsigned R4BaseReg = NextReg;
1364
1365 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1366 if (NumAlignedDPRCS2Regs >= 4) {
1367 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001368 &ARM::QQPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001369 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1370 .addReg(ARM::R4)
1371 .addImm(16)
1372 .addReg(SupReg, RegState::ImplicitDefine)
1373 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001374 NextReg += 4;
1375 NumAlignedDPRCS2Regs -= 4;
1376 }
1377
1378 // 16-byte aligned vld1.64 with 2 d-regs.
1379 if (NumAlignedDPRCS2Regs >= 2) {
1380 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001381 &ARM::QPRRegClass);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001382 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1383 .addReg(ARM::R4)
1384 .addImm(16)
1385 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001386 NextReg += 2;
1387 NumAlignedDPRCS2Regs -= 2;
1388 }
1389
1390 // Finally, use a vanilla vldr.64 for the remaining odd register.
1391 if (NumAlignedDPRCS2Regs)
Diana Picus4f8c3e12017-01-13 09:37:56 +00001392 BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1393 .addReg(ARM::R4)
1394 .addImm(2 * (NextReg - R4BaseReg))
1395 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001396
1397 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001398 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001399}
1400
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001401bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001402 MachineBasicBlock::iterator MI,
1403 const std::vector<CalleeSavedInfo> &CSI,
1404 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001405 if (CSI.empty())
1406 return false;
1407
1408 MachineFunction &MF = *MBB.getParent();
1409 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001410
1411 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001412 unsigned PushOneOpc = AFI->isThumbFunction() ?
1413 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001414 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001415 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1416 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001417 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001418 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001419 MachineInstr::FrameSetup);
1420 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001421 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1422
1423 // The code above does not insert spill code for the aligned DPRCS2 registers.
1424 // The stack realignment code will be inserted between the push instructions
1425 // and these spills.
1426 if (NumAlignedDPRCS2Regs)
1427 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001428
1429 return true;
1430}
1431
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001432bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001433 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001434 std::vector<CalleeSavedInfo> &CSI,
Bob Wilson657f2272011-01-13 21:10:12 +00001435 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001436 if (CSI.empty())
1437 return false;
1438
1439 MachineFunction &MF = *MBB.getParent();
1440 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001441 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001442 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1443
1444 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1445 // registers. Do that here instead.
1446 if (NumAlignedDPRCS2Regs)
1447 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001448
1449 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001450 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001451 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001452 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1453 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001454 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001455 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001456 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001457 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001458
1459 return true;
1460}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001461
1462// FIXME: Make generic?
1463static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1464 const ARMBaseInstrInfo &TII) {
1465 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001466 for (auto &MBB : MF) {
1467 for (auto &MI : MBB)
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001468 FnSize += TII.getInstSizeInBytes(MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001469 }
1470 return FnSize;
1471}
1472
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001473/// estimateRSStackSizeLimit - Look at each instruction that references stack
1474/// frames and return the stack size limit beyond which some of these
1475/// instructions will require a scratch register during their expansion later.
1476// FIXME: Move to TII?
1477static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001478 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001479 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1480 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001481 for (auto &MBB : MF) {
1482 for (auto &MI : MBB) {
1483 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1484 if (!MI.getOperand(i).isFI())
1485 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001486
1487 // When using ADDri to get the address of a stack object, 255 is the
1488 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001489 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001490 Limit = std::min(Limit, (1U << 8) - 1);
1491 break;
1492 }
1493
1494 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001495 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001496 case ARMII::AddrMode3:
1497 case ARMII::AddrModeT2_i8:
1498 Limit = std::min(Limit, (1U << 8) - 1);
1499 break;
1500 case ARMII::AddrMode5:
1501 case ARMII::AddrModeT2_i8s4:
1502 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1503 break;
1504 case ARMII::AddrModeT2_i12:
1505 // i12 supports only positive offset so these will be converted to
1506 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1507 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1508 Limit = std::min(Limit, (1U << 8) - 1);
1509 break;
1510 case ARMII::AddrMode4:
1511 case ARMII::AddrMode6:
1512 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1513 // immediate offset for stack references.
1514 return 0;
1515 default:
1516 break;
1517 }
1518 break; // At most one FI per instruction
1519 }
1520 }
1521 }
1522
1523 return Limit;
1524}
1525
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001526// In functions that realign the stack, it can be an advantage to spill the
1527// callee-saved vector registers after realigning the stack. The vst1 and vld1
1528// instructions take alignment hints that can improve performance.
Matthias Braun02564862015-07-14 17:17:13 +00001529static void
1530checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001531 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1532 if (!SpillAlignedNEONRegs)
1533 return;
1534
1535 // Naked functions don't spill callee-saved registers.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001536 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001537 return;
1538
1539 // We are planning to use NEON instructions vst1 / vld1.
Eric Christopher1b21f002015-01-29 00:19:33 +00001540 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001541 return;
1542
1543 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopher1b21f002015-01-29 00:19:33 +00001544 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001545 return;
1546
1547 // Aligned spills require stack realignment.
Eric Christopher1b21f002015-01-29 00:19:33 +00001548 if (!static_cast<const ARMBaseRegisterInfo *>(
1549 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001550 return;
1551
1552 // We always spill contiguous d-registers starting from d8. Count how many
1553 // needs spilling. The register allocator will almost always use the
1554 // callee-saved registers in order, but it can happen that there are holes in
1555 // the range. Registers above the hole will be spilled to the standard DPRCS
1556 // area.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001557 unsigned NumSpills = 0;
1558 for (; NumSpills < 8; ++NumSpills)
Matthias Braun02564862015-07-14 17:17:13 +00001559 if (!SavedRegs.test(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001560 break;
1561
1562 // Don't do this for just one d-register. It's not worth it.
1563 if (NumSpills < 2)
1564 return;
1565
1566 // Spill the first NumSpills D-registers after realigning the stack.
1567 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1568
1569 // A scratch register is required for the vst1 / vld1 instructions.
Matthias Braun02564862015-07-14 17:17:13 +00001570 SavedRegs.set(ARM::R4);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001571}
1572
Matthias Braun02564862015-07-14 17:17:13 +00001573void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1574 BitVector &SavedRegs,
1575 RegScavenger *RS) const {
1576 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001577 // This tells PEI to spill the FP as if it is any other callee-save register
1578 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1579 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1580 // to combine multiple loads / stores.
1581 bool CanEliminateFrame = true;
1582 bool CS1Spilled = false;
1583 bool LRSpilled = false;
1584 unsigned NumGPRSpills = 0;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001585 unsigned NumFPRSpills = 0;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001586 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1587 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001588 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001589 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001590 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001591 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001592 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +00001593 MachineFrameInfo &MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001594 MachineRegisterInfo &MRI = MF.getRegInfo();
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001595 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1596 (void)TRI; // Silence unused warning in non-assert builds.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001597 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1598
1599 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1600 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001601 // since it's not always possible to restore sp from fp in a single
1602 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001603 // FIXME: It will be better just to find spare register here.
1604 if (AFI->isThumb2Function() &&
Matthias Braun941a7052016-07-28 18:40:00 +00001605 (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Matthias Braun02564862015-07-14 17:17:13 +00001606 SavedRegs.set(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001607
Evan Cheng572756a2011-01-16 05:14:33 +00001608 if (AFI->isThumb1OnlyFunction()) {
1609 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001610 if (AFI->getArgRegsSaveSize() > 0)
Matthias Braun02564862015-07-14 17:17:13 +00001611 SavedRegs.set(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001612
Momchil Velikovd6a4ab32017-10-22 11:56:35 +00001613 // Spill R4 if Thumb1 epilogue has to restore SP from FP or the function
1614 // requires stack alignment. We don't know for sure what the stack size
1615 // will be, but for this, an estimate is good enough. If there anything
1616 // changes it, it'll be a spill, which implies we've used all the registers
1617 // and so R4 is already used, so not marking it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001618 // FIXME: It will be better just to find spare register here.
Momchil Velikovd6a4ab32017-10-22 11:56:35 +00001619 if (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF) ||
1620 MFI.estimateStackSize(MF) > 508)
Matthias Braun02564862015-07-14 17:17:13 +00001621 SavedRegs.set(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001622 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001623
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001624 // See if we can spill vector registers to aligned stack.
Matthias Braun02564862015-07-14 17:17:13 +00001625 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001626
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001627 // Spill the BasePtr if it's used.
1628 if (RegInfo->hasBasePointer(MF))
Matthias Braun02564862015-07-14 17:17:13 +00001629 SavedRegs.set(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001630
1631 // Don't spill FP if the frame can be eliminated. This is determined
Matthias Braun02564862015-07-14 17:17:13 +00001632 // by scanning the callee-save registers to see if any is modified.
Craig Topper840beec2014-04-04 05:16:06 +00001633 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001634 for (unsigned i = 0; CSRegs[i]; ++i) {
1635 unsigned Reg = CSRegs[i];
1636 bool Spilled = false;
Matthias Braun02564862015-07-14 17:17:13 +00001637 if (SavedRegs.test(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001638 Spilled = true;
1639 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001640 }
1641
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001642 if (!ARM::GPRRegClass.contains(Reg)) {
1643 if (Spilled) {
1644 if (ARM::SPRRegClass.contains(Reg))
1645 NumFPRSpills++;
1646 else if (ARM::DPRRegClass.contains(Reg))
1647 NumFPRSpills += 2;
1648 else if (ARM::QPRRegClass.contains(Reg))
1649 NumFPRSpills += 4;
1650 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001651 continue;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001652 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001653
1654 if (Spilled) {
1655 NumGPRSpills++;
1656
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001657 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001658 if (Reg == ARM::LR)
1659 LRSpilled = true;
1660 CS1Spilled = true;
1661 continue;
1662 }
1663
1664 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1665 switch (Reg) {
1666 case ARM::LR:
1667 LRSpilled = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00001668 LLVM_FALLTHROUGH;
Tim Northoverd8407452013-10-01 14:33:28 +00001669 case ARM::R0: case ARM::R1:
1670 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001671 case ARM::R4: case ARM::R5:
1672 case ARM::R6: case ARM::R7:
1673 CS1Spilled = true;
1674 break;
1675 default:
1676 break;
1677 }
1678 } else {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001679 if (!STI.splitFramePushPop(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001680 UnspilledCS1GPRs.push_back(Reg);
1681 continue;
1682 }
1683
1684 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001685 case ARM::R0: case ARM::R1:
1686 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001687 case ARM::R4: case ARM::R5:
1688 case ARM::R6: case ARM::R7:
1689 case ARM::LR:
1690 UnspilledCS1GPRs.push_back(Reg);
1691 break;
1692 default:
1693 UnspilledCS2GPRs.push_back(Reg);
1694 break;
1695 }
1696 }
1697 }
1698
1699 bool ForceLRSpill = false;
1700 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1701 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1702 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1703 // use of BL to implement far jump. If it turns out that it's not needed
1704 // then the branch fix up path will undo it.
1705 if (FnSize >= (1 << 11)) {
1706 CanEliminateFrame = false;
1707 ForceLRSpill = true;
1708 }
1709 }
1710
1711 // If any of the stack slot references may be out of range of an immediate
1712 // offset, make sure a register (or a spill slot) is available for the
1713 // register scavenger. Note that if we're indexing off the frame pointer, the
1714 // effective stack size is 4 bytes larger since the FP points to the stack
1715 // slot of the previous FP. Also, if we have variable sized objects in the
1716 // function, stack slot references will often be negative, and some of
1717 // our instructions are positive-offset only, so conservatively consider
1718 // that case to want a spill slot (or register) as well. Similarly, if
1719 // the function adjusts the stack pointer during execution and the
1720 // adjustments aren't already part of our stack size estimate, our offset
1721 // calculations may be off, so be conservative.
1722 // FIXME: We could add logic to be more precise about negative offsets
1723 // and which instructions will need a scratch register for them. Is it
1724 // worth the effort and added fragility?
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001725 unsigned EstimatedStackSize =
Matthias Braun941a7052016-07-28 18:40:00 +00001726 MFI.estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001727
1728 // Determine biggest (positive) SP offset in MachineFrameInfo.
1729 int MaxFixedOffset = 0;
1730 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
1731 int MaxObjectOffset = MFI.getObjectOffset(I) + MFI.getObjectSize(I);
1732 MaxFixedOffset = std::max(MaxFixedOffset, MaxObjectOffset);
1733 }
1734
Matthias Braun44047422017-04-05 16:58:41 +00001735 bool HasFP = hasFP(MF);
1736 if (HasFP) {
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001737 if (AFI->hasStackFrame())
1738 EstimatedStackSize += 4;
1739 } else {
1740 // If FP is not used, SP will be used to access arguments, so count the
1741 // size of arguments into the estimation.
Matthias Braun8aaa3682017-04-19 21:11:44 +00001742 EstimatedStackSize += MaxFixedOffset;
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001743 }
1744 EstimatedStackSize += 16; // For possible paddings.
1745
Matthias Braun8aaa3682017-04-19 21:11:44 +00001746 unsigned EstimatedRSStackSizeLimit = estimateRSStackSizeLimit(MF, this);
1747 int MaxFPOffset = getMaxFPOffset(*MF.getFunction(), *AFI);
1748 bool BigFrameOffsets = EstimatedStackSize >= EstimatedRSStackSizeLimit ||
1749 MFI.hasVarSizedObjects() ||
1750 (MFI.adjustsStack() && !canSimplifyCallFramePseudos(MF)) ||
1751 // For large argument stacks fp relative addressed may overflow.
1752 (HasFP && (MaxFixedOffset - MaxFPOffset) >= (int)EstimatedRSStackSizeLimit);
Matthias Braun8aaa3682017-04-19 21:11:44 +00001753 if (BigFrameOffsets ||
1754 !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001755 AFI->setHasStackFrame(true);
1756
Matthias Braun44047422017-04-05 16:58:41 +00001757 if (HasFP) {
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001758 SavedRegs.set(FramePtr);
1759 // If the frame pointer is required by the ABI, also spill LR so that we
1760 // emit a complete frame record.
1761 if (MF.getTarget().Options.DisableFramePointerElim(MF) && !LRSpilled) {
1762 SavedRegs.set(ARM::LR);
1763 LRSpilled = true;
1764 NumGPRSpills++;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001765 auto LRPos = llvm::find(UnspilledCS1GPRs, ARM::LR);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001766 if (LRPos != UnspilledCS1GPRs.end())
1767 UnspilledCS1GPRs.erase(LRPos);
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001768 }
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001769 auto FPPos = llvm::find(UnspilledCS1GPRs, FramePtr);
Oliver Stannard9aa6f012016-08-23 09:19:22 +00001770 if (FPPos != UnspilledCS1GPRs.end())
1771 UnspilledCS1GPRs.erase(FPPos);
1772 NumGPRSpills++;
1773 if (FramePtr == ARM::R7)
1774 CS1Spilled = true;
1775 }
1776
Matthias Braunc618a462017-07-28 01:36:32 +00001777 // This is true when we inserted a spill for an unused register that can now
1778 // be used for register scavenging.
1779 bool ExtraCSSpill = false;
1780
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001781 if (AFI->isThumb1OnlyFunction()) {
1782 // For Thumb1-only targets, we need some low registers when we save and
1783 // restore the high registers (which aren't allocatable, but could be
1784 // used by inline assembly) because the push/pop instructions can not
1785 // access high registers. If necessary, we might need to push more low
1786 // registers to ensure that there is at least one free that can be used
1787 // for the saving & restoring, and preferably we should ensure that as
1788 // many as are needed are available so that fewer push/pop instructions
1789 // are required.
1790
1791 // Low registers which are not currently pushed, but could be (r4-r7).
1792 SmallVector<unsigned, 4> AvailableRegs;
1793
1794 // Unused argument registers (r0-r3) can be clobbered in the prologue for
1795 // free.
1796 int EntryRegDeficit = 0;
1797 for (unsigned Reg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3}) {
1798 if (!MF.getRegInfo().isLiveIn(Reg)) {
1799 --EntryRegDeficit;
1800 DEBUG(dbgs() << PrintReg(Reg, TRI)
1801 << " is unused argument register, EntryRegDeficit = "
1802 << EntryRegDeficit << "\n");
1803 }
1804 }
1805
1806 // Unused return registers can be clobbered in the epilogue for free.
1807 int ExitRegDeficit = AFI->getReturnRegsCount() - 4;
1808 DEBUG(dbgs() << AFI->getReturnRegsCount()
1809 << " return regs used, ExitRegDeficit = " << ExitRegDeficit
1810 << "\n");
1811
1812 int RegDeficit = std::max(EntryRegDeficit, ExitRegDeficit);
1813 DEBUG(dbgs() << "RegDeficit = " << RegDeficit << "\n");
1814
1815 // r4-r6 can be used in the prologue if they are pushed by the first push
1816 // instruction.
1817 for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6}) {
1818 if (SavedRegs.test(Reg)) {
1819 --RegDeficit;
1820 DEBUG(dbgs() << PrintReg(Reg, TRI)
1821 << " is saved low register, RegDeficit = " << RegDeficit
1822 << "\n");
1823 } else {
1824 AvailableRegs.push_back(Reg);
1825 DEBUG(dbgs()
1826 << PrintReg(Reg, TRI)
1827 << " is non-saved low register, adding to AvailableRegs\n");
1828 }
1829 }
1830
1831 // r7 can be used if it is not being used as the frame pointer.
Matthias Braun44047422017-04-05 16:58:41 +00001832 if (!HasFP) {
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001833 if (SavedRegs.test(ARM::R7)) {
1834 --RegDeficit;
1835 DEBUG(dbgs() << "%R7 is saved low register, RegDeficit = "
1836 << RegDeficit << "\n");
1837 } else {
1838 AvailableRegs.push_back(ARM::R7);
1839 DEBUG(dbgs()
1840 << "%R7 is non-saved low register, adding to AvailableRegs\n");
1841 }
1842 }
1843
1844 // Each of r8-r11 needs to be copied to a low register, then pushed.
1845 for (unsigned Reg : {ARM::R8, ARM::R9, ARM::R10, ARM::R11}) {
1846 if (SavedRegs.test(Reg)) {
1847 ++RegDeficit;
1848 DEBUG(dbgs() << PrintReg(Reg, TRI)
1849 << " is saved high register, RegDeficit = " << RegDeficit
1850 << "\n");
1851 }
1852 }
1853
1854 // LR can only be used by PUSH, not POP, and can't be used at all if the
1855 // llvm.returnaddress intrinsic is used. This is only worth doing if we
1856 // are more limited at function entry than exit.
1857 if ((EntryRegDeficit > ExitRegDeficit) &&
1858 !(MF.getRegInfo().isLiveIn(ARM::LR) &&
1859 MF.getFrameInfo().isReturnAddressTaken())) {
1860 if (SavedRegs.test(ARM::LR)) {
1861 --RegDeficit;
1862 DEBUG(dbgs() << "%LR is saved register, RegDeficit = " << RegDeficit
1863 << "\n");
1864 } else {
1865 AvailableRegs.push_back(ARM::LR);
1866 DEBUG(dbgs() << "%LR is not saved, adding to AvailableRegs\n");
1867 }
1868 }
1869
1870 // If there are more high registers that need pushing than low registers
1871 // available, push some more low registers so that we can use fewer push
1872 // instructions. This might not reduce RegDeficit all the way to zero,
1873 // because we can only guarantee that r4-r6 are available, but r8-r11 may
1874 // need saving.
1875 DEBUG(dbgs() << "Final RegDeficit = " << RegDeficit << "\n");
1876 for (; RegDeficit > 0 && !AvailableRegs.empty(); --RegDeficit) {
1877 unsigned Reg = AvailableRegs.pop_back_val();
1878 DEBUG(dbgs() << "Spilling " << PrintReg(Reg, TRI)
1879 << " to make up reg deficit\n");
1880 SavedRegs.set(Reg);
1881 NumGPRSpills++;
1882 CS1Spilled = true;
Matthias Braunc618a462017-07-28 01:36:32 +00001883 assert(!MRI.isReserved(Reg) && "Should not be reserved");
1884 if (!MRI.isPhysRegUsed(Reg))
1885 ExtraCSSpill = true;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001886 UnspilledCS1GPRs.erase(llvm::find(UnspilledCS1GPRs, Reg));
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001887 if (Reg == ARM::LR)
1888 LRSpilled = true;
1889 }
1890 DEBUG(dbgs() << "After adding spills, RegDeficit = " << RegDeficit << "\n");
1891 }
1892
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001893 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1894 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1895 if (!LRSpilled && CS1Spilled) {
Matthias Braun02564862015-07-14 17:17:13 +00001896 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001897 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001898 SmallVectorImpl<unsigned>::iterator LRPos;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001899 LRPos = llvm::find(UnspilledCS1GPRs, (unsigned)ARM::LR);
Tim Northoverd8407452013-10-01 14:33:28 +00001900 if (LRPos != UnspilledCS1GPRs.end())
1901 UnspilledCS1GPRs.erase(LRPos);
1902
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001903 ForceLRSpill = false;
Matthias Braunc618a462017-07-28 01:36:32 +00001904 if (!MRI.isReserved(ARM::LR) && !MRI.isPhysRegUsed(ARM::LR))
1905 ExtraCSSpill = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001906 }
1907
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001908 // If stack and double are 8-byte aligned and we are spilling an odd number
1909 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1910 // the integer and double callee save areas.
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001911 DEBUG(dbgs() << "NumGPRSpills = " << NumGPRSpills << "\n");
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001912 unsigned TargetAlign = getStackAlignment();
Tim Northoverdc0d9e42014-11-05 00:27:20 +00001913 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001914 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1915 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1916 unsigned Reg = UnspilledCS1GPRs[i];
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001917 // Don't spill high register if the function is thumb. In the case of
1918 // Windows on ARM, accept R11 (frame pointer)
Peter Collingbourne78f1ecc2015-04-23 20:31:26 +00001919 if (!AFI->isThumbFunction() ||
Saleem Abdulrasool1825fac2015-10-09 03:19:03 +00001920 (STI.isTargetWindows() && Reg == ARM::R11) ||
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001921 isARMLowRegister(Reg) || Reg == ARM::LR) {
Matthias Braun02564862015-07-14 17:17:13 +00001922 SavedRegs.set(Reg);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001923 DEBUG(dbgs() << "Spilling " << PrintReg(Reg, TRI)
1924 << " to make up alignment\n");
Matthias Braunc618a462017-07-28 01:36:32 +00001925 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001926 ExtraCSSpill = true;
1927 break;
1928 }
1929 }
1930 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1931 unsigned Reg = UnspilledCS2GPRs.front();
Matthias Braun02564862015-07-14 17:17:13 +00001932 SavedRegs.set(Reg);
Reid Klecknerbdfc05f2016-10-11 21:14:03 +00001933 DEBUG(dbgs() << "Spilling " << PrintReg(Reg, TRI)
1934 << " to make up alignment\n");
Matthias Braunc618a462017-07-28 01:36:32 +00001935 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001936 ExtraCSSpill = true;
1937 }
1938 }
1939
1940 // Estimate if we might need to scavenge a register at some point in order
1941 // to materialize a stack offset. If so, either spill one additional
1942 // callee-saved register or reserve a special spill slot to facilitate
1943 // register scavenging. Thumb1 needs a spill slot for stack pointer
1944 // adjustments also, even when the frame itself is small.
Matthias Braun8aaa3682017-04-19 21:11:44 +00001945 if (BigFrameOffsets && !ExtraCSSpill) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001946 // If any non-reserved CS register isn't spilled, just spill one or two
1947 // extra. That should take care of it!
1948 unsigned NumExtras = TargetAlign / 4;
1949 SmallVector<unsigned, 2> Extras;
1950 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1951 unsigned Reg = UnspilledCS1GPRs.back();
1952 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001953 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001954 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1955 Reg == ARM::LR)) {
1956 Extras.push_back(Reg);
1957 NumExtras--;
1958 }
1959 }
1960 // For non-Thumb1 functions, also check for hi-reg CS registers
1961 if (!AFI->isThumb1OnlyFunction()) {
1962 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1963 unsigned Reg = UnspilledCS2GPRs.back();
1964 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001965 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001966 Extras.push_back(Reg);
1967 NumExtras--;
1968 }
1969 }
1970 }
Matthias Braunc618a462017-07-28 01:36:32 +00001971 if (NumExtras == 0) {
1972 for (unsigned Reg : Extras) {
1973 SavedRegs.set(Reg);
1974 if (!MRI.isPhysRegUsed(Reg))
1975 ExtraCSSpill = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001976 }
Matthias Braunc618a462017-07-28 01:36:32 +00001977 }
1978 if (!ExtraCSSpill && !AFI->isThumb1OnlyFunction()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001979 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1980 // closest to SP or frame pointer.
Weiming Zhao5b5501e2016-05-08 05:11:54 +00001981 assert(RS && "Register scavenging not provided");
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001982 const TargetRegisterClass &RC = ARM::GPRRegClass;
1983 unsigned Size = TRI->getSpillSize(RC);
1984 unsigned Align = TRI->getSpillAlignment(RC);
1985 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001986 }
1987 }
1988 }
1989
1990 if (ForceLRSpill) {
Matthias Braun02564862015-07-14 17:17:13 +00001991 SavedRegs.set(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001992 AFI->setLRIsSpilledForFarJump(true);
1993 }
1994}
Eli Bendersky8da87162013-02-21 20:05:00 +00001995
Hans Wennborge1a2e902016-03-31 18:33:38 +00001996MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
1997 MachineFunction &MF, MachineBasicBlock &MBB,
1998 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +00001999 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00002000 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00002001 if (!hasReservedCallFrame(MF)) {
2002 // If we have alloca, convert as follows:
2003 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
2004 // ADJCALLSTACKUP -> add, sp, sp, amount
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00002005 MachineInstr &Old = *I;
2006 DebugLoc dl = Old.getDebugLoc();
Serge Pavlov5943a962017-04-19 03:12:05 +00002007 unsigned Amount = TII.getFrameSize(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +00002008 if (Amount != 0) {
2009 // We need to keep the stack aligned properly. To do this, we round the
2010 // amount of space needed for the outgoing arguments up to the next
2011 // alignment boundary.
Guozhi Weif66d3842015-08-17 22:36:27 +00002012 Amount = alignSPAdjust(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +00002013
2014 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2015 assert(!AFI->isThumb1OnlyFunction() &&
2016 "This eliminateCallFramePseudoInstr does not support Thumb1!");
2017 bool isARM = !AFI->isThumbFunction();
2018
2019 // Replace the pseudo instruction with a new instruction...
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00002020 unsigned Opc = Old.getOpcode();
2021 int PIdx = Old.findFirstPredOperandIdx();
2022 ARMCC::CondCodes Pred =
2023 (PIdx == -1) ? ARMCC::AL
2024 : (ARMCC::CondCodes)Old.getOperand(PIdx).getImm();
Serge Pavlov5943a962017-04-19 03:12:05 +00002025 unsigned PredReg = TII.getFramePred(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +00002026 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
Eli Bendersky8da87162013-02-21 20:05:00 +00002027 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
2028 Pred, PredReg);
2029 } else {
Eli Bendersky8da87162013-02-21 20:05:00 +00002030 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
2031 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
2032 Pred, PredReg);
2033 }
2034 }
2035 }
Hans Wennborge1a2e902016-03-31 18:33:38 +00002036 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00002037}
2038
Oliver Stannardb14c6252014-04-02 16:10:33 +00002039/// Get the minimum constant for ARM that is greater than or equal to the
2040/// argument. In ARM, constants can have any value that can be produced by
2041/// rotating an 8-bit value to the right by an even number of bits within a
2042/// 32-bit word.
2043static uint32_t alignToARMConstant(uint32_t Value) {
2044 unsigned Shifted = 0;
2045
2046 if (Value == 0)
2047 return 0;
2048
2049 while (!(Value & 0xC0000000)) {
2050 Value = Value << 2;
2051 Shifted += 2;
2052 }
2053
2054 bool Carry = (Value & 0x00FFFFFF);
2055 Value = ((Value & 0xFF000000) >> 24) + Carry;
2056
2057 if (Value & 0x0000100)
2058 Value = Value & 0x000001FC;
2059
2060 if (Shifted > 24)
2061 Value = Value >> (Shifted - 24);
2062 else
2063 Value = Value << (24 - Shifted);
2064
2065 return Value;
2066}
2067
2068// The stack limit in the TCB is set to this many bytes above the actual
2069// stack limit.
2070static const uint64_t kSplitStackAvailable = 256;
2071
2072// Adjust the function prologue to enable split stacks. This currently only
2073// supports android and linux.
2074//
2075// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
2076// must be well defined in order to allow for consistent implementations of the
2077// __morestack helper function. The ABI is also not a normal ABI in that it
2078// doesn't follow the normal calling conventions because this allows the
2079// prologue of each function to be optimized further.
2080//
2081// Currently, the ABI looks like (when calling __morestack)
2082//
2083// * r4 holds the minimum stack size requested for this function call
2084// * r5 holds the stack size of the arguments to the function
2085// * the beginning of the function is 3 instructions after the call to
2086// __morestack
2087//
2088// Implementations of __morestack should use r4 to allocate a new stack, r5 to
2089// place the arguments on to the new stack, and the 3-instruction knowledge to
2090// jump directly to the body of the function when working on the new stack.
2091//
2092// An old (and possibly no longer compatible) implementation of __morestack for
2093// ARM can be found at [1].
2094//
2095// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
Quentin Colombet61b305e2015-05-05 17:38:16 +00002096void ARMFrameLowering::adjustForSegmentedStacks(
2097 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
Oliver Stannardb14c6252014-04-02 16:10:33 +00002098 unsigned Opcode;
2099 unsigned CFIIndex;
Eric Christopher22b2ad22015-02-20 08:24:37 +00002100 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
Oliver Stannardb14c6252014-04-02 16:10:33 +00002101 bool Thumb = ST->isThumb();
2102
2103 // Sadly, this currently doesn't support varargs, platforms other than
2104 // android/linux. Note that thumb1/thumb2 are support for android/linux.
2105 if (MF.getFunction()->isVarArg())
2106 report_fatal_error("Segmented stacks do not support vararg functions.");
2107 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00002108 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00002109
Matthias Braun941a7052016-07-28 18:40:00 +00002110 MachineFrameInfo &MFI = MF.getFrameInfo();
Oliver Stannardb14c6252014-04-02 16:10:33 +00002111 MachineModuleInfo &MMI = MF.getMMI();
2112 MCContext &Context = MMI.getContext();
2113 const MCRegisterInfo *MRI = Context.getRegisterInfo();
2114 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00002115 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002116 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
2117 DebugLoc DL;
2118
Matthias Braun941a7052016-07-28 18:40:00 +00002119 uint64_t StackSize = MFI.getStackSize();
Tim Northoverf9e798b2014-05-22 13:03:43 +00002120
2121 // Do not generate a prologue for functions with a stack of size zero
2122 if (StackSize == 0)
2123 return;
2124
Oliver Stannardb14c6252014-04-02 16:10:33 +00002125 // Use R4 and R5 as scratch registers.
2126 // We save R4 and R5 before use and restore them before leaving the function.
2127 unsigned ScratchReg0 = ARM::R4;
2128 unsigned ScratchReg1 = ARM::R5;
2129 uint64_t AlignedStackSize;
2130
2131 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
2132 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
2133 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
2134 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
2135 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
2136
Quentin Colombet71a71482015-07-20 21:42:14 +00002137 // Grab everything that reaches PrologueMBB to update there liveness as well.
2138 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
2139 SmallVector<MachineBasicBlock *, 2> WalkList;
2140 WalkList.push_back(&PrologueMBB);
2141
2142 do {
2143 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
2144 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
2145 if (BeforePrologueRegion.insert(PredBB).second)
2146 WalkList.push_back(PredBB);
2147 }
2148 } while (!WalkList.empty());
2149
2150 // The order in that list is important.
2151 // The blocks will all be inserted before PrologueMBB using that order.
2152 // Therefore the block that should appear first in the CFG should appear
2153 // first in the list.
2154 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
2155 PostStackMBB};
Quentin Colombet71a71482015-07-20 21:42:14 +00002156
Craig Topper80720812015-12-01 06:13:01 +00002157 for (MachineBasicBlock *B : AddedBlocks)
2158 BeforePrologueRegion.insert(B);
Quentin Colombet71a71482015-07-20 21:42:14 +00002159
Matthias Braund9da1622015-09-09 18:08:03 +00002160 for (const auto &LI : PrologueMBB.liveins()) {
Quentin Colombet71a71482015-07-20 21:42:14 +00002161 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
Matthias Braunb2b7ef12015-08-24 22:59:52 +00002162 PredBB->addLiveIn(LI);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002163 }
2164
Quentin Colombet71a71482015-07-20 21:42:14 +00002165 // Remove the newly added blocks from the list, since we know
2166 // we do not have to do the following updates for them.
Craig Topper80720812015-12-01 06:13:01 +00002167 for (MachineBasicBlock *B : AddedBlocks) {
2168 BeforePrologueRegion.erase(B);
2169 MF.insert(PrologueMBB.getIterator(), B);
Quentin Colombet71a71482015-07-20 21:42:14 +00002170 }
2171
2172 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
2173 // Make sure the LiveIns are still sorted and unique.
2174 MBB->sortUniqueLiveIns();
2175 // Replace the edges to PrologueMBB by edges to the sequences
2176 // we are about to add.
2177 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
2178 }
Oliver Stannardb14c6252014-04-02 16:10:33 +00002179
2180 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00002181 AlignedStackSize = alignToARMConstant(StackSize);
2182
2183 // When the frame size is less than 256 we just compare the stack
2184 // boundary directly to the value of the stack pointer, per gcc.
2185 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
2186
2187 // We will use two of the callee save registers as scratch registers so we
2188 // need to save those registers onto the stack.
2189 // We will use SR0 to hold stack limit and SR1 to hold the stack size
2190 // requested and arguments for __morestack().
2191 // SR0: Scratch Register #0
2192 // SR1: Scratch Register #1
2193 // push {SR0, SR1}
2194 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002195 BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))
2196 .add(predOps(ARMCC::AL))
2197 .addReg(ScratchReg0)
2198 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002199 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002200 BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
2201 .addReg(ARM::SP, RegState::Define)
2202 .addReg(ARM::SP)
2203 .add(predOps(ARMCC::AL))
2204 .addReg(ScratchReg0)
2205 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002206 }
2207
2208 // Emit the relevant DWARF information about the change in stack pointer as
2209 // well as where to find both r4 and r5 (the callee-save registers)
2210 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00002211 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002212 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2213 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002214 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002215 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
2216 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2217 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002218 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002219 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
2220 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2221 .addCFIIndex(CFIIndex);
2222
2223 // mov SR1, sp
2224 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002225 BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
2226 .addReg(ARM::SP)
2227 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002228 } else if (CompareStackPointer) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002229 BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
2230 .addReg(ARM::SP)
2231 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002232 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002233 }
2234
2235 // sub SR1, sp, #StackSize
2236 if (!CompareStackPointer && Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002237 BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)
2238 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002239 .addReg(ScratchReg1)
2240 .addImm(AlignedStackSize)
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::SUBri), ScratchReg1)
2244 .addReg(ARM::SP)
2245 .addImm(AlignedStackSize)
2246 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002247 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002248 }
2249
2250 if (Thumb && ST->isThumb1Only()) {
2251 unsigned PCLabelId = ARMFI->createPICLabelUId();
2252 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Oliver Stannard92e0fc02014-04-03 08:45:16 +00002253 MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002254 MachineConstantPool *MCP = MF.getConstantPool();
Tim Northover956b0082015-10-02 18:07:13 +00002255 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002256
2257 // ldr SR0, [pc, offset(STACK_LIMIT)]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002258 BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
2259 .addConstantPoolIndex(CPI)
2260 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002261
2262 // ldr SR0, [SR0]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002263 BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
2264 .addReg(ScratchReg0)
2265 .addImm(0)
2266 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002267 } else {
2268 // Get TLS base address from the coprocessor
2269 // mrc p15, #0, SR0, c13, c0, #3
Diana Picus4f8c3e12017-01-13 09:37:56 +00002270 BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2271 .addImm(15)
2272 .addImm(0)
2273 .addImm(13)
2274 .addImm(0)
2275 .addImm(3)
2276 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002277
2278 // Use the last tls slot on android and a private field of the TCP on linux.
2279 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2280 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2281
2282 // Get the stack limit from the right offset
2283 // ldr SR0, [sr0, #4 * TlsOffset]
Diana Picus4f8c3e12017-01-13 09:37:56 +00002284 BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2285 .addReg(ScratchReg0)
2286 .addImm(4 * TlsOffset)
2287 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002288 }
2289
2290 // Compare stack limit with stack size requested.
2291 // cmp SR0, SR1
2292 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
Diana Picus4f8c3e12017-01-13 09:37:56 +00002293 BuildMI(GetMBB, DL, TII.get(Opcode))
2294 .addReg(ScratchReg0)
2295 .addReg(ScratchReg1)
2296 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002297
2298 // This jump is taken if StackLimit < SP - stack required.
2299 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2300 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2301 .addImm(ARMCC::LO)
2302 .addReg(ARM::CPSR);
2303
2304
2305 // Calling __morestack(StackSize, Size of stack arguments).
2306 // __morestack knows that the stack size requested is in SR0(r4)
2307 // and amount size of stack arguments is in SR1(r5).
2308
2309 // Pass first argument for the __morestack by Scratch Register #0.
2310 // The amount size of stack required
2311 if (Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002312 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg0)
2313 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002314 .addImm(AlignedStackSize)
2315 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002316 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002317 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2318 .addImm(AlignedStackSize)
2319 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002320 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002321 }
2322 // Pass second argument for the __morestack by Scratch Register #1.
2323 // The amount size of stack consumed to save function arguments.
2324 if (Thumb) {
Diana Picus8a73f552017-01-13 10:18:01 +00002325 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)
2326 .add(condCodeOp())
Diana Picus4f8c3e12017-01-13 09:37:56 +00002327 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2328 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002329 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002330 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2331 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2332 .add(predOps(ARMCC::AL))
Diana Picusbd66b7d2017-01-20 08:15:24 +00002333 .add(condCodeOp());
Oliver Stannardb14c6252014-04-02 16:10:33 +00002334 }
2335
2336 // push {lr} - Save return address of this function.
2337 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002338 BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))
2339 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002340 .addReg(ARM::LR);
2341 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002342 BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2343 .addReg(ARM::SP, RegState::Define)
2344 .addReg(ARM::SP)
2345 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002346 .addReg(ARM::LR);
2347 }
2348
2349 // Emit the DWARF info about the change in stack as well as where to find the
2350 // previous link register
2351 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00002352 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002353 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2354 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002355 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002356 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2357 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2358 .addCFIIndex(CFIIndex);
2359
2360 // Call __morestack().
2361 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002362 BuildMI(AllocMBB, DL, TII.get(ARM::tBL))
2363 .add(predOps(ARMCC::AL))
Oliver Stannardb14c6252014-04-02 16:10:33 +00002364 .addExternalSymbol("__morestack");
2365 } else {
2366 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2367 .addExternalSymbol("__morestack");
2368 }
2369
2370 // pop {lr} - Restore return address of this original function.
2371 if (Thumb) {
2372 if (ST->isThumb1Only()) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002373 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2374 .add(predOps(ARMCC::AL))
2375 .addReg(ScratchReg0);
2376 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2377 .addReg(ScratchReg0)
2378 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002379 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002380 BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2381 .addReg(ARM::LR, RegState::Define)
2382 .addReg(ARM::SP, RegState::Define)
2383 .addReg(ARM::SP)
2384 .addImm(4)
2385 .add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002386 }
2387 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002388 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2389 .addReg(ARM::SP, RegState::Define)
2390 .addReg(ARM::SP)
2391 .add(predOps(ARMCC::AL))
2392 .addReg(ARM::LR);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002393 }
2394
2395 // Restore SR0 and SR1 in case of __morestack() was called.
2396 // __morestack() will skip PostStackMBB block so we need to restore
2397 // scratch registers from here.
2398 // pop {SR0, SR1}
2399 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002400 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2401 .add(predOps(ARMCC::AL))
2402 .addReg(ScratchReg0)
2403 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002404 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002405 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2406 .addReg(ARM::SP, RegState::Define)
2407 .addReg(ARM::SP)
2408 .add(predOps(ARMCC::AL))
2409 .addReg(ScratchReg0)
2410 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002411 }
2412
2413 // Update the CFA offset now that we've popped
Matthias Braunf23ef432016-11-30 23:48:42 +00002414 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002415 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2416 .addCFIIndex(CFIIndex);
2417
Joerg Sonnenberger0f76a352017-08-28 20:20:47 +00002418 // Return from this function.
2419 BuildMI(AllocMBB, DL, TII.get(ST->getReturnOpcode())).add(predOps(ARMCC::AL));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002420
2421 // Restore SR0 and SR1 in case of __morestack() was not called.
2422 // pop {SR0, SR1}
2423 if (Thumb) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002424 BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))
2425 .add(predOps(ARMCC::AL))
2426 .addReg(ScratchReg0)
2427 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002428 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00002429 BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2430 .addReg(ARM::SP, RegState::Define)
2431 .addReg(ARM::SP)
2432 .add(predOps(ARMCC::AL))
2433 .addReg(ScratchReg0)
2434 .addReg(ScratchReg1);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002435 }
2436
2437 // Update the CFA offset now that we've popped
Matthias Braunf23ef432016-11-30 23:48:42 +00002438 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
Oliver Stannardb14c6252014-04-02 16:10:33 +00002439 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2440 .addCFIIndex(CFIIndex);
2441
2442 // Tell debuggers that r4 and r5 are now the same as they were in the
2443 // previous function, that they're the "Same Value".
Matthias Braunf23ef432016-11-30 23:48:42 +00002444 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002445 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2446 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2447 .addCFIIndex(CFIIndex);
Matthias Braunf23ef432016-11-30 23:48:42 +00002448 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
Oliver Stannardb14c6252014-04-02 16:10:33 +00002449 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2450 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2451 .addCFIIndex(CFIIndex);
2452
2453 // Organizing MBB lists
Quentin Colombet61b305e2015-05-05 17:38:16 +00002454 PostStackMBB->addSuccessor(&PrologueMBB);
Oliver Stannardb14c6252014-04-02 16:10:33 +00002455
2456 AllocMBB->addSuccessor(PostStackMBB);
2457
2458 GetMBB->addSuccessor(PostStackMBB);
2459 GetMBB->addSuccessor(AllocMBB);
2460
2461 McrMBB->addSuccessor(GetMBB);
2462
2463 PrevStackMBB->addSuccessor(McrMBB);
2464
Filipe Cabecinhas0da99372016-04-29 15:22:48 +00002465#ifdef EXPENSIVE_CHECKS
Oliver Stannardb14c6252014-04-02 16:10:33 +00002466 MF.verify();
2467#endif
2468}