blob: 6dc5d19862a94c5ec2e8f9dcdb99b3d2db4016af [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===- AArch64FrameLowering.cpp - AArch64 Frame Lowering -------*- C++ -*-====//
2//
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//
10// This file contains the AArch64 implementation of TargetFrameLowering class.
11//
Kristof Beyls17cb8982015-04-09 08:49:47 +000012// On AArch64, stack frames are structured as follows:
13//
14// The stack grows downward.
15//
16// All of the individual frame areas on the frame below are optional, i.e. it's
17// possible to create a function so that the particular area isn't present
18// in the frame.
19//
20// At function entry, the "frame" looks as follows:
21//
22// | | Higher address
23// |-----------------------------------|
24// | |
25// | arguments passed on the stack |
26// | |
27// |-----------------------------------| <- sp
28// | | Lower address
29//
30//
31// After the prologue has run, the frame has the following general structure.
32// Note that this doesn't depict the case where a red-zone is used. Also,
33// technically the last frame area (VLAs) doesn't get created until in the
34// main function body, after the prologue is run. However, it's depicted here
35// for completeness.
36//
37// | | Higher address
38// |-----------------------------------|
39// | |
40// | arguments passed on the stack |
41// | |
42// |-----------------------------------|
43// | |
Martin Storsjo68266fa2017-07-13 17:03:12 +000044// | (Win64 only) varargs from reg |
45// | |
46// |-----------------------------------|
47// | |
Kristof Beyls17cb8982015-04-09 08:49:47 +000048// | prev_fp, prev_lr |
49// | (a.k.a. "frame record") |
50// |-----------------------------------| <- fp(=x29)
51// | |
52// | other callee-saved registers |
53// | |
54// |-----------------------------------|
55// |.empty.space.to.make.part.below....|
56// |.aligned.in.case.it.needs.more.than| (size of this area is unknown at
57// |.the.standard.16-byte.alignment....| compile time; if present)
58// |-----------------------------------|
59// | |
60// | local variables of fixed size |
61// | including spill slots |
62// |-----------------------------------| <- bp(not defined by ABI,
63// |.variable-sized.local.variables....| LLVM chooses X19)
64// |.(VLAs)............................| (size of this area is unknown at
65// |...................................| compile time)
66// |-----------------------------------| <- sp
67// | | Lower address
68//
69//
70// To access the data in a frame, at-compile time, a constant offset must be
71// computable from one of the pointers (fp, bp, sp) to access it. The size
72// of the areas with a dotted background cannot be computed at compile-time
73// if they are present, making it required to have all three of fp, bp and
74// sp to be set up to be able to access all contents in the frame areas,
75// assuming all of the frame areas are non-empty.
76//
77// For most functions, some of the frame areas are empty. For those functions,
78// it may not be necessary to set up fp or bp:
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000079// * A base pointer is definitely needed when there are both VLAs and local
Kristof Beyls17cb8982015-04-09 08:49:47 +000080// variables with more-than-default alignment requirements.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000081// * A frame pointer is definitely needed when there are local variables with
Kristof Beyls17cb8982015-04-09 08:49:47 +000082// more-than-default alignment requirements.
83//
84// In some cases when a base pointer is not strictly needed, it is generated
85// anyway when offsets from the frame pointer to access local variables become
86// so large that the offset can't be encoded in the immediate fields of loads
87// or stores.
88//
89// FIXME: also explain the redzone concept.
90// FIXME: also explain the concept of reserved call frames.
91//
Tim Northover3b0846e2014-05-24 12:50:23 +000092//===----------------------------------------------------------------------===//
93
94#include "AArch64FrameLowering.h"
95#include "AArch64InstrInfo.h"
96#include "AArch64MachineFunctionInfo.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +000097#include "AArch64RegisterInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000098#include "AArch64Subtarget.h"
99#include "AArch64TargetMachine.h"
Martin Storsjo2778fd02017-12-20 06:51:45 +0000100#include "MCTargetDesc/AArch64AddressingModes.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000101#include "llvm/ADT/SmallVector.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000102#include "llvm/ADT/Statistic.h"
Matthias Braun332bb5c2016-07-06 21:31:27 +0000103#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000104#include "llvm/CodeGen/MachineBasicBlock.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000105#include "llvm/CodeGen/MachineFrameInfo.h"
106#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000107#include "llvm/CodeGen/MachineInstr.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000108#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000109#include "llvm/CodeGen/MachineMemOperand.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000110#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000111#include "llvm/CodeGen/MachineOperand.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000112#include "llvm/CodeGen/MachineRegisterInfo.h"
113#include "llvm/CodeGen/RegisterScavenging.h"
David Blaikie3f833ed2017-11-08 01:01:31 +0000114#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +0000115#include "llvm/CodeGen/TargetRegisterInfo.h"
116#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000117#include "llvm/IR/Attributes.h"
118#include "llvm/IR/CallingConv.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +0000119#include "llvm/IR/DataLayout.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000120#include "llvm/IR/DebugLoc.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +0000121#include "llvm/IR/Function.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000122#include "llvm/MC/MCDwarf.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000123#include "llvm/Support/CommandLine.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +0000124#include "llvm/Support/Debug.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000125#include "llvm/Support/ErrorHandling.h"
126#include "llvm/Support/MathExtras.h"
Tim Northover3b0846e2014-05-24 12:50:23 +0000127#include "llvm/Support/raw_ostream.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000128#include "llvm/Target/TargetMachine.h"
129#include "llvm/Target/TargetOptions.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +0000130#include <cassert>
131#include <cstdint>
132#include <iterator>
133#include <vector>
Tim Northover3b0846e2014-05-24 12:50:23 +0000134
135using namespace llvm;
136
137#define DEBUG_TYPE "frame-info"
138
139static cl::opt<bool> EnableRedZone("aarch64-redzone",
140 cl::desc("enable use of redzone on AArch64"),
141 cl::init(false), cl::Hidden);
142
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000143static cl::opt<bool>
144 ReverseCSRRestoreSeq("reverse-csr-restore-seq",
145 cl::desc("reverse the CSR restore sequence"),
146 cl::init(false), cl::Hidden);
147
Tim Northover3b0846e2014-05-24 12:50:23 +0000148STATISTIC(NumRedZoneFunctions, "Number of functions using red zone");
149
Matthias Braun5c290dc2018-01-19 03:16:36 +0000150/// This is the biggest offset to the stack pointer we can encode in aarch64
151/// instructions (without using a separate calculation and a temp register).
152/// Note that the exception here are vector stores/loads which cannot encode any
153/// displacements (see estimateRSStackSizeLimit(), isAArch64FrameOffsetLegal()).
154static const unsigned DefaultSafeSPDisplacement = 255;
155
Kristof Beyls2af1e902017-05-30 06:58:41 +0000156/// Look at each instruction that references stack frames and return the stack
157/// size limit beyond which some of these instructions will require a scratch
158/// register during their expansion later.
159static unsigned estimateRSStackSizeLimit(MachineFunction &MF) {
160 // FIXME: For now, just conservatively guestimate based on unscaled indexing
161 // range. We'll end up allocating an unnecessary spill slot a lot, but
162 // realistically that's not a big deal at this stage of the game.
163 for (MachineBasicBlock &MBB : MF) {
164 for (MachineInstr &MI : MBB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000165 if (MI.isDebugInstr() || MI.isPseudo() ||
Kristof Beyls2af1e902017-05-30 06:58:41 +0000166 MI.getOpcode() == AArch64::ADDXri ||
167 MI.getOpcode() == AArch64::ADDSXri)
168 continue;
169
Javed Absard13d4192017-10-30 22:00:06 +0000170 for (const MachineOperand &MO : MI.operands()) {
171 if (!MO.isFI())
Kristof Beyls2af1e902017-05-30 06:58:41 +0000172 continue;
173
174 int Offset = 0;
175 if (isAArch64FrameOffsetLegal(MI, Offset, nullptr, nullptr, nullptr) ==
176 AArch64FrameOffsetCannotUpdate)
177 return 0;
178 }
179 }
180 }
Matthias Braun5c290dc2018-01-19 03:16:36 +0000181 return DefaultSafeSPDisplacement;
Kristof Beyls2af1e902017-05-30 06:58:41 +0000182}
183
Tim Northover3b0846e2014-05-24 12:50:23 +0000184bool AArch64FrameLowering::canUseRedZone(const MachineFunction &MF) const {
185 if (!EnableRedZone)
186 return false;
187 // Don't use the red zone if the function explicitly asks us not to.
188 // This is typically used for kernel code.
Matthias Braunf1caa282017-12-15 22:22:58 +0000189 if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone))
Tim Northover3b0846e2014-05-24 12:50:23 +0000190 return false;
191
Matthias Braun941a7052016-07-28 18:40:00 +0000192 const MachineFrameInfo &MFI = MF.getFrameInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000193 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
194 unsigned NumBytes = AFI->getLocalStackSize();
195
Matthias Braun941a7052016-07-28 18:40:00 +0000196 return !(MFI.hasCalls() || hasFP(MF) || NumBytes > 128);
Tim Northover3b0846e2014-05-24 12:50:23 +0000197}
198
199/// hasFP - Return true if the specified function should have a dedicated frame
200/// pointer register.
201bool AArch64FrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000202 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000203 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Geoff Berry62c1a1e2016-03-02 17:58:31 +0000204 // Retain behavior of always omitting the FP for leaf functions when possible.
Matthias Braun5c290dc2018-01-19 03:16:36 +0000205 if (MFI.hasCalls() && MF.getTarget().Options.DisableFramePointerElim(MF))
206 return true;
207 if (MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
208 MFI.hasStackMap() || MFI.hasPatchPoint() ||
209 RegInfo->needsStackRealignment(MF))
210 return true;
211 // With large callframes around we may need to use FP to access the scavenging
212 // emergency spillslot.
213 //
214 // Unfortunately some calls to hasFP() like machine verifier ->
215 // getReservedReg() -> hasFP in the middle of global isel are too early
216 // to know the max call frame size. Hopefully conservatively returning "true"
217 // in those cases is fine.
218 // DefaultSafeSPDisplacement is fine as we only emergency spill GP regs.
219 if (!MFI.isMaxCallFrameSizeComputed() ||
220 MFI.getMaxCallFrameSize() > DefaultSafeSPDisplacement)
221 return true;
222
223 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000224}
225
226/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
227/// not required, we reserve argument space for call sites in the function
228/// immediately on entry to the current function. This eliminates the need for
229/// add/sub sp brackets around call sites. Returns true if the call frame is
230/// included as part of the stack frame.
231bool
232AArch64FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000233 return !MF.getFrameInfo().hasVarSizedObjects();
Tim Northover3b0846e2014-05-24 12:50:23 +0000234}
235
Hans Wennborge1a2e902016-03-31 18:33:38 +0000236MachineBasicBlock::iterator AArch64FrameLowering::eliminateCallFramePseudoInstr(
Tim Northover3b0846e2014-05-24 12:50:23 +0000237 MachineFunction &MF, MachineBasicBlock &MBB,
238 MachineBasicBlock::iterator I) const {
Eric Christopherfc6de422014-08-05 02:39:49 +0000239 const AArch64InstrInfo *TII =
240 static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo());
Tim Northover3b0846e2014-05-24 12:50:23 +0000241 DebugLoc DL = I->getDebugLoc();
Matthias Braunfa3872e2015-05-18 20:27:55 +0000242 unsigned Opc = I->getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +0000243 bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode();
244 uint64_t CalleePopAmount = IsDestroy ? I->getOperand(1).getImm() : 0;
245
Eric Christopherfc6de422014-08-05 02:39:49 +0000246 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
Tim Northover3b0846e2014-05-24 12:50:23 +0000247 if (!TFI->hasReservedCallFrame(MF)) {
248 unsigned Align = getStackAlignment();
249
250 int64_t Amount = I->getOperand(0).getImm();
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000251 Amount = alignTo(Amount, Align);
Tim Northover3b0846e2014-05-24 12:50:23 +0000252 if (!IsDestroy)
253 Amount = -Amount;
254
255 // N.b. if CalleePopAmount is valid but zero (i.e. callee would pop, but it
256 // doesn't have to pop anything), then the first operand will be zero too so
257 // this adjustment is a no-op.
258 if (CalleePopAmount == 0) {
259 // FIXME: in-function stack adjustment for calls is limited to 24-bits
260 // because there's no guaranteed temporary register available.
261 //
Sylvestre Ledru469de192014-08-11 18:04:46 +0000262 // ADD/SUB (immediate) has only LSL #0 and LSL #12 available.
Tim Northover3b0846e2014-05-24 12:50:23 +0000263 // 1) For offset <= 12-bit, we use LSL #0
264 // 2) For 12-bit <= offset <= 24-bit, we use two instructions. One uses
265 // LSL #0, and the other uses LSL #12.
266 //
Chad Rosier401a4ab2016-01-19 16:50:45 +0000267 // Most call frames will be allocated at the start of a function so
Tim Northover3b0846e2014-05-24 12:50:23 +0000268 // this is OK, but it is a limitation that needs dealing with.
269 assert(Amount > -0xffffff && Amount < 0xffffff && "call frame too large");
270 emitFrameOffset(MBB, I, DL, AArch64::SP, AArch64::SP, Amount, TII);
271 }
272 } else if (CalleePopAmount != 0) {
273 // If the calling convention demands that the callee pops arguments from the
274 // stack, we want to add it back if we have a reserved call frame.
275 assert(CalleePopAmount < 0xffffff && "call frame too large");
276 emitFrameOffset(MBB, I, DL, AArch64::SP, AArch64::SP, -CalleePopAmount,
277 TII);
278 }
Hans Wennborge1a2e902016-03-31 18:33:38 +0000279 return MBB.erase(I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000280}
281
282void AArch64FrameLowering::emitCalleeSavedFrameMoves(
Geoff Berry62d47252016-02-25 16:36:08 +0000283 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const {
Tim Northover3b0846e2014-05-24 12:50:23 +0000284 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000285 MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf23ef432016-11-30 23:48:42 +0000286 const TargetSubtargetInfo &STI = MF.getSubtarget();
287 const MCRegisterInfo *MRI = STI.getRegisterInfo();
288 const TargetInstrInfo *TII = STI.getInstrInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000289 DebugLoc DL = MBB.findDebugLoc(MBBI);
290
291 // Add callee saved registers to move list.
Matthias Braun941a7052016-07-28 18:40:00 +0000292 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000293 if (CSI.empty())
294 return;
295
Tim Northover3b0846e2014-05-24 12:50:23 +0000296 for (const auto &Info : CSI) {
297 unsigned Reg = Info.getReg();
Geoff Berry62d47252016-02-25 16:36:08 +0000298 int64_t Offset =
Matthias Braun941a7052016-07-28 18:40:00 +0000299 MFI.getObjectOffset(Info.getFrameIdx()) - getOffsetOfLocalArea();
Tim Northover3b0846e2014-05-24 12:50:23 +0000300 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +0000301 unsigned CFIIndex = MF.addFrameInst(
Geoff Berry62d47252016-02-25 16:36:08 +0000302 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
Tim Northover3b0846e2014-05-24 12:50:23 +0000303 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000304 .addCFIIndex(CFIIndex)
305 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover3b0846e2014-05-24 12:50:23 +0000306 }
307}
308
Geoff Berry7e4ba3d2016-02-19 18:27:32 +0000309// Find a scratch register that we can use at the start of the prologue to
310// re-align the stack pointer. We avoid using callee-save registers since they
311// may appear to be free when this is called from canUseAsPrologue (during
312// shrink wrapping), but then no longer be free when this is called from
313// emitPrologue.
314//
315// FIXME: This is a bit conservative, since in the above case we could use one
316// of the callee-save registers as a scratch temp to re-align the stack pointer,
317// but we would then have to make sure that we were in fact saving at least one
318// callee-save register in the prologue, which is additional complexity that
319// doesn't seem worth the benefit.
320static unsigned findScratchNonCalleeSaveRegister(MachineBasicBlock *MBB) {
321 MachineFunction *MF = MBB->getParent();
322
323 // If MBB is an entry block, use X9 as the scratch register
324 if (&MF->front() == MBB)
325 return AArch64::X9;
326
Eric Christopher60a245e2017-03-31 23:12:27 +0000327 const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>();
Matthias Braunac4307c2017-05-26 21:51:00 +0000328 const AArch64RegisterInfo &TRI = *Subtarget.getRegisterInfo();
Eric Christopher60a245e2017-03-31 23:12:27 +0000329 LivePhysRegs LiveRegs(TRI);
Matthias Braun332bb5c2016-07-06 21:31:27 +0000330 LiveRegs.addLiveIns(*MBB);
Geoff Berry7e4ba3d2016-02-19 18:27:32 +0000331
Matthias Braun332bb5c2016-07-06 21:31:27 +0000332 // Mark callee saved registers as used so we will not choose them.
Matthias Braunac4307c2017-05-26 21:51:00 +0000333 const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(MF);
Geoff Berry7e4ba3d2016-02-19 18:27:32 +0000334 for (unsigned i = 0; CSRegs[i]; ++i)
Matthias Braun332bb5c2016-07-06 21:31:27 +0000335 LiveRegs.addReg(CSRegs[i]);
Geoff Berry7e4ba3d2016-02-19 18:27:32 +0000336
Matthias Braun332bb5c2016-07-06 21:31:27 +0000337 // Prefer X9 since it was historically used for the prologue scratch reg.
338 const MachineRegisterInfo &MRI = MF->getRegInfo();
339 if (LiveRegs.available(MRI, AArch64::X9))
340 return AArch64::X9;
Geoff Berry7e4ba3d2016-02-19 18:27:32 +0000341
Matthias Braun332bb5c2016-07-06 21:31:27 +0000342 for (unsigned Reg : AArch64::GPR64RegClass) {
343 if (LiveRegs.available(MRI, Reg))
344 return Reg;
345 }
Geoff Berry7e4ba3d2016-02-19 18:27:32 +0000346 return AArch64::NoRegister;
347}
348
349bool AArch64FrameLowering::canUseAsPrologue(
350 const MachineBasicBlock &MBB) const {
351 const MachineFunction *MF = MBB.getParent();
352 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
353 const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>();
354 const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
355
356 // Don't need a scratch register if we're not going to re-align the stack.
357 if (!RegInfo->needsStackRealignment(*MF))
358 return true;
359 // Otherwise, we can use any block as long as it has a scratch register
360 // available.
361 return findScratchNonCalleeSaveRegister(TmpMBB) != AArch64::NoRegister;
362}
363
Martin Storsjo2778fd02017-12-20 06:51:45 +0000364static bool windowsRequiresStackProbe(MachineFunction &MF,
365 unsigned StackSizeInBytes) {
366 const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
367 if (!Subtarget.isTargetWindows())
368 return false;
369 const Function &F = MF.getFunction();
370 // TODO: When implementing stack protectors, take that into account
371 // for the probe threshold.
372 unsigned StackProbeSize = 4096;
373 if (F.hasFnAttribute("stack-probe-size"))
374 F.getFnAttribute("stack-probe-size")
375 .getValueAsString()
376 .getAsInteger(0, StackProbeSize);
Hans Wennborg89c35fc2018-02-23 13:46:25 +0000377 return (StackSizeInBytes >= StackProbeSize) &&
378 !F.hasFnAttribute("no-stack-arg-probe");
Martin Storsjo2778fd02017-12-20 06:51:45 +0000379}
380
Geoff Berrya5335642016-05-06 16:34:59 +0000381bool AArch64FrameLowering::shouldCombineCSRLocalStackBump(
382 MachineFunction &MF, unsigned StackBumpBytes) const {
383 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000384 const MachineFrameInfo &MFI = MF.getFrameInfo();
Geoff Berrya5335642016-05-06 16:34:59 +0000385 const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
386 const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
387
388 if (AFI->getLocalStackSize() == 0)
389 return false;
390
391 // 512 is the maximum immediate for stp/ldp that will be used for
392 // callee-save save/restores
Martin Storsjo2778fd02017-12-20 06:51:45 +0000393 if (StackBumpBytes >= 512 || windowsRequiresStackProbe(MF, StackBumpBytes))
Geoff Berrya5335642016-05-06 16:34:59 +0000394 return false;
395
Matthias Braun941a7052016-07-28 18:40:00 +0000396 if (MFI.hasVarSizedObjects())
Geoff Berrya5335642016-05-06 16:34:59 +0000397 return false;
398
399 if (RegInfo->needsStackRealignment(MF))
400 return false;
401
402 // This isn't strictly necessary, but it simplifies things a bit since the
403 // current RedZone handling code assumes the SP is adjusted by the
404 // callee-save save/restore code.
405 if (canUseRedZone(MF))
406 return false;
407
408 return true;
409}
410
411// Convert callee-save register save/restore instruction to do stack pointer
412// decrement/increment to allocate/deallocate the callee-save stack area by
413// converting store/load to use pre/post increment version.
414static MachineBasicBlock::iterator convertCalleeSaveRestoreToSPPrePostIncDec(
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000415 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
416 const DebugLoc &DL, const TargetInstrInfo *TII, int CSStackSizeInc) {
Peter Collingbournef11eb3e2018-04-04 21:55:44 +0000417 // Ignore instructions that do not operate on SP, i.e. shadow call stack
418 // instructions.
419 while (MBBI->getOpcode() == AArch64::STRXpost ||
420 MBBI->getOpcode() == AArch64::LDRXpre) {
421 assert(MBBI->getOperand(0).getReg() != AArch64::SP);
422 ++MBBI;
423 }
424
Geoff Berrya5335642016-05-06 16:34:59 +0000425 unsigned NewOpc;
426 bool NewIsUnscaled = false;
427 switch (MBBI->getOpcode()) {
428 default:
429 llvm_unreachable("Unexpected callee-save save/restore opcode!");
430 case AArch64::STPXi:
431 NewOpc = AArch64::STPXpre;
432 break;
433 case AArch64::STPDi:
434 NewOpc = AArch64::STPDpre;
435 break;
436 case AArch64::STRXui:
437 NewOpc = AArch64::STRXpre;
438 NewIsUnscaled = true;
439 break;
440 case AArch64::STRDui:
441 NewOpc = AArch64::STRDpre;
442 NewIsUnscaled = true;
443 break;
444 case AArch64::LDPXi:
445 NewOpc = AArch64::LDPXpost;
446 break;
447 case AArch64::LDPDi:
448 NewOpc = AArch64::LDPDpost;
449 break;
450 case AArch64::LDRXui:
451 NewOpc = AArch64::LDRXpost;
452 NewIsUnscaled = true;
453 break;
454 case AArch64::LDRDui:
455 NewOpc = AArch64::LDRDpost;
456 NewIsUnscaled = true;
457 break;
458 }
459
460 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc));
461 MIB.addReg(AArch64::SP, RegState::Define);
462
463 // Copy all operands other than the immediate offset.
464 unsigned OpndIdx = 0;
465 for (unsigned OpndEnd = MBBI->getNumOperands() - 1; OpndIdx < OpndEnd;
466 ++OpndIdx)
Diana Picus116bbab2017-01-13 09:58:52 +0000467 MIB.add(MBBI->getOperand(OpndIdx));
Geoff Berrya5335642016-05-06 16:34:59 +0000468
469 assert(MBBI->getOperand(OpndIdx).getImm() == 0 &&
470 "Unexpected immediate offset in first/last callee-save save/restore "
471 "instruction!");
472 assert(MBBI->getOperand(OpndIdx - 1).getReg() == AArch64::SP &&
473 "Unexpected base register in callee-save save/restore instruction!");
474 // Last operand is immediate offset that needs fixing.
475 assert(CSStackSizeInc % 8 == 0);
476 int64_t CSStackSizeIncImm = CSStackSizeInc;
477 if (!NewIsUnscaled)
478 CSStackSizeIncImm /= 8;
479 MIB.addImm(CSStackSizeIncImm);
480
481 MIB.setMIFlags(MBBI->getFlags());
482 MIB.setMemRefs(MBBI->memoperands_begin(), MBBI->memoperands_end());
483
484 return std::prev(MBB.erase(MBBI));
485}
486
487// Fixup callee-save register save/restore instructions to take into account
488// combined SP bump by adding the local stack size to the stack offsets.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000489static void fixupCalleeSaveRestoreStackOffset(MachineInstr &MI,
Geoff Berrya5335642016-05-06 16:34:59 +0000490 unsigned LocalStackSize) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000491 unsigned Opc = MI.getOpcode();
Peter Collingbournef11eb3e2018-04-04 21:55:44 +0000492
493 // Ignore instructions that do not operate on SP, i.e. shadow call stack
494 // instructions.
495 if (Opc == AArch64::STRXpost || Opc == AArch64::LDRXpre) {
496 assert(MI.getOperand(0).getReg() != AArch64::SP);
497 return;
498 }
499
Geoff Berrya5335642016-05-06 16:34:59 +0000500 (void)Opc;
501 assert((Opc == AArch64::STPXi || Opc == AArch64::STPDi ||
502 Opc == AArch64::STRXui || Opc == AArch64::STRDui ||
503 Opc == AArch64::LDPXi || Opc == AArch64::LDPDi ||
504 Opc == AArch64::LDRXui || Opc == AArch64::LDRDui) &&
505 "Unexpected callee-save save/restore opcode!");
506
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000507 unsigned OffsetIdx = MI.getNumExplicitOperands() - 1;
508 assert(MI.getOperand(OffsetIdx - 1).getReg() == AArch64::SP &&
Geoff Berrya5335642016-05-06 16:34:59 +0000509 "Unexpected base register in callee-save save/restore instruction!");
510 // Last operand is immediate offset that needs fixing.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000511 MachineOperand &OffsetOpnd = MI.getOperand(OffsetIdx);
Geoff Berrya5335642016-05-06 16:34:59 +0000512 // All generated opcodes have scaled offsets.
513 assert(LocalStackSize % 8 == 0);
514 OffsetOpnd.setImm(OffsetOpnd.getImm() + LocalStackSize / 8);
515}
516
Francis Visoiu Mistrihc855e922018-04-27 15:30:54 +0000517static void adaptForLdStOpt(MachineBasicBlock &MBB,
518 MachineBasicBlock::iterator FirstSPPopI,
519 MachineBasicBlock::iterator LastPopI) {
520 // Sometimes (when we restore in the same order as we save), we can end up
521 // with code like this:
522 //
523 // ldp x26, x25, [sp]
524 // ldp x24, x23, [sp, #16]
525 // ldp x22, x21, [sp, #32]
526 // ldp x20, x19, [sp, #48]
527 // add sp, sp, #64
528 //
529 // In this case, it is always better to put the first ldp at the end, so
530 // that the load-store optimizer can run and merge the ldp and the add into
531 // a post-index ldp.
532 // If we managed to grab the first pop instruction, move it to the end.
533 if (ReverseCSRRestoreSeq)
534 MBB.splice(FirstSPPopI, &MBB, LastPopI);
535 // We should end up with something like this now:
536 //
537 // ldp x24, x23, [sp, #16]
538 // ldp x22, x21, [sp, #32]
539 // ldp x20, x19, [sp, #48]
540 // ldp x26, x25, [sp]
541 // add sp, sp, #64
542 //
543 // and the load-store optimizer can merge the last two instructions into:
544 //
545 // ldp x26, x25, [sp], #64
546 //
547}
548
Quentin Colombet61b305e2015-05-05 17:38:16 +0000549void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
550 MachineBasicBlock &MBB) const {
Tim Northover3b0846e2014-05-24 12:50:23 +0000551 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000552 const MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000553 const Function &F = MF.getFunction();
Ahmed Bougacha66834ec2015-12-16 22:54:06 +0000554 const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
555 const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
556 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000557 MachineModuleInfo &MMI = MF.getMMI();
Tim Northover775aaeb2015-11-05 21:54:58 +0000558 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
Matthias Braunf1caa282017-12-15 22:22:58 +0000559 bool needsFrameMoves = MMI.hasDebugInfo() || F.needsUnwindTableEntry();
Tim Northover775aaeb2015-11-05 21:54:58 +0000560 bool HasFP = hasFP(MF);
561
Jessica Paquette8aa6cd52018-04-12 16:16:18 +0000562 // At this point, we're going to decide whether or not the function uses a
563 // redzone. In most cases, the function doesn't have a redzone so let's
564 // assume that's false and set it to true in the case that there's a redzone.
565 AFI->setHasRedZone(false);
566
Tim Northover775aaeb2015-11-05 21:54:58 +0000567 // Debug location must be unknown since the first debug location is used
568 // to determine the end of the prologue.
569 DebugLoc DL;
570
571 // All calls are tail calls in GHC calling conv, and functions have no
572 // prologue/epilogue.
Matthias Braunf1caa282017-12-15 22:22:58 +0000573 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
Greg Fitzgeraldfa78d082015-01-19 17:40:05 +0000574 return;
575
Matthias Braun941a7052016-07-28 18:40:00 +0000576 int NumBytes = (int)MFI.getStackSize();
Martin Storsjo2778fd02017-12-20 06:51:45 +0000577 if (!AFI->hasStackFrame() && !windowsRequiresStackProbe(MF, NumBytes)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000578 assert(!HasFP && "unexpected function without stack frame but with FP");
579
580 // All of the stack allocation is for locals.
581 AFI->setLocalStackSize(NumBytes);
582
Chad Rosier27c352d2016-03-14 18:24:34 +0000583 if (!NumBytes)
584 return;
Tim Northover3b0846e2014-05-24 12:50:23 +0000585 // REDZONE: If the stack size is less than 128 bytes, we don't need
586 // to actually allocate.
Jessica Paquette642f6c62018-04-03 21:56:10 +0000587 if (canUseRedZone(MF)) {
588 AFI->setHasRedZone(true);
Chad Rosier27c352d2016-03-14 18:24:34 +0000589 ++NumRedZoneFunctions;
Jessica Paquette642f6c62018-04-03 21:56:10 +0000590 } else {
Tim Northover3b0846e2014-05-24 12:50:23 +0000591 emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII,
592 MachineInstr::FrameSetup);
593
Chad Rosier27c352d2016-03-14 18:24:34 +0000594 // Label used to tie together the PROLOG_LABEL and the MachineMoves.
595 MCSymbol *FrameLabel = MMI.getContext().createTempSymbol();
Tim Northover3b0846e2014-05-24 12:50:23 +0000596 // Encode the stack size of the leaf function.
Matthias Braunf23ef432016-11-30 23:48:42 +0000597 unsigned CFIIndex = MF.addFrameInst(
Tim Northover3b0846e2014-05-24 12:50:23 +0000598 MCCFIInstruction::createDefCfaOffset(FrameLabel, -NumBytes));
599 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000600 .addCFIIndex(CFIIndex)
601 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover3b0846e2014-05-24 12:50:23 +0000602 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000603 return;
604 }
605
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000606 bool IsWin64 =
Matthias Braunf1caa282017-12-15 22:22:58 +0000607 Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv());
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000608 unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0;
609
610 auto PrologueSaveSize = AFI->getCalleeSavedStackSize() + FixedObject;
Chad Rosier27c352d2016-03-14 18:24:34 +0000611 // All of the remaining stack allocations are for locals.
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000612 AFI->setLocalStackSize(NumBytes - PrologueSaveSize);
Tim Northover3b0846e2014-05-24 12:50:23 +0000613
Geoff Berrya5335642016-05-06 16:34:59 +0000614 bool CombineSPBump = shouldCombineCSRLocalStackBump(MF, NumBytes);
615 if (CombineSPBump) {
616 emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII,
617 MachineInstr::FrameSetup);
618 NumBytes = 0;
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000619 } else if (PrologueSaveSize != 0) {
Geoff Berrya5335642016-05-06 16:34:59 +0000620 MBBI = convertCalleeSaveRestoreToSPPrePostIncDec(MBB, MBBI, DL, TII,
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000621 -PrologueSaveSize);
622 NumBytes -= PrologueSaveSize;
Geoff Berrya5335642016-05-06 16:34:59 +0000623 }
624 assert(NumBytes >= 0 && "Negative stack allocation size!?");
625
626 // Move past the saves of the callee-saved registers, fixing up the offsets
627 // and pre-inc if we decided to combine the callee-save and local stack
628 // pointer bump above.
Geoff Berry04bf91a2016-02-01 16:29:19 +0000629 MachineBasicBlock::iterator End = MBB.end();
Geoff Berrya5335642016-05-06 16:34:59 +0000630 while (MBBI != End && MBBI->getFlag(MachineInstr::FrameSetup)) {
631 if (CombineSPBump)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000632 fixupCalleeSaveRestoreStackOffset(*MBBI, AFI->getLocalStackSize());
Tim Northover3b0846e2014-05-24 12:50:23 +0000633 ++MBBI;
Geoff Berrya5335642016-05-06 16:34:59 +0000634 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000635 if (HasFP) {
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000636 // Only set up FP if we actually need to. Frame pointer is fp =
637 // sp - fixedobject - 16.
638 int FPOffset = AFI->getCalleeSavedStackSize() - 16;
Geoff Berrya5335642016-05-06 16:34:59 +0000639 if (CombineSPBump)
640 FPOffset += AFI->getLocalStackSize();
Chad Rosier27c352d2016-03-14 18:24:34 +0000641
Tim Northover3b0846e2014-05-24 12:50:23 +0000642 // Issue sub fp, sp, FPOffset or
643 // mov fp,sp when FPOffset is zero.
644 // Note: All stores of callee-saved registers are marked as "FrameSetup".
645 // This code marks the instruction(s) that set the FP also.
646 emitFrameOffset(MBB, MBBI, DL, AArch64::FP, AArch64::SP, FPOffset, TII,
647 MachineInstr::FrameSetup);
648 }
649
Martin Storsjo2778fd02017-12-20 06:51:45 +0000650 if (windowsRequiresStackProbe(MF, NumBytes)) {
651 uint32_t NumWords = NumBytes >> 4;
652
653 BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVi64imm), AArch64::X15)
654 .addImm(NumWords)
655 .setMIFlags(MachineInstr::FrameSetup);
656
657 switch (MF.getTarget().getCodeModel()) {
658 case CodeModel::Small:
659 case CodeModel::Medium:
660 case CodeModel::Kernel:
661 BuildMI(MBB, MBBI, DL, TII->get(AArch64::BL))
662 .addExternalSymbol("__chkstk")
663 .addReg(AArch64::X15, RegState::Implicit)
664 .setMIFlags(MachineInstr::FrameSetup);
665 break;
666 case CodeModel::Large:
667 BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVaddrEXT))
668 .addReg(AArch64::X16, RegState::Define)
669 .addExternalSymbol("__chkstk")
670 .addExternalSymbol("__chkstk")
671 .setMIFlags(MachineInstr::FrameSetup);
672
673 BuildMI(MBB, MBBI, DL, TII->get(AArch64::BLR))
674 .addReg(AArch64::X16, RegState::Kill)
675 .addReg(AArch64::X15, RegState::Implicit | RegState::Define)
676 .setMIFlags(MachineInstr::FrameSetup);
677 break;
678 }
679
680 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SUBXrx64), AArch64::SP)
681 .addReg(AArch64::SP, RegState::Kill)
682 .addReg(AArch64::X15, RegState::Kill)
683 .addImm(AArch64_AM::getArithExtendImm(AArch64_AM::UXTX, 4))
684 .setMIFlags(MachineInstr::FrameSetup);
685 NumBytes = 0;
686 }
687
Tim Northover3b0846e2014-05-24 12:50:23 +0000688 // Allocate space for the rest of the frame.
Chad Rosier27c352d2016-03-14 18:24:34 +0000689 if (NumBytes) {
690 const bool NeedsRealignment = RegInfo->needsStackRealignment(MF);
691 unsigned scratchSPReg = AArch64::SP;
Kristof Beyls17cb8982015-04-09 08:49:47 +0000692
Chad Rosier27c352d2016-03-14 18:24:34 +0000693 if (NeedsRealignment) {
694 scratchSPReg = findScratchNonCalleeSaveRegister(&MBB);
695 assert(scratchSPReg != AArch64::NoRegister);
696 }
Kristof Beyls17cb8982015-04-09 08:49:47 +0000697
Chad Rosier27c352d2016-03-14 18:24:34 +0000698 // If we're a leaf function, try using the red zone.
699 if (!canUseRedZone(MF))
700 // FIXME: in the case of dynamic re-alignment, NumBytes doesn't have
701 // the correct value here, as NumBytes also includes padding bytes,
702 // which shouldn't be counted here.
703 emitFrameOffset(MBB, MBBI, DL, scratchSPReg, AArch64::SP, -NumBytes, TII,
704 MachineInstr::FrameSetup);
Kristof Beyls17cb8982015-04-09 08:49:47 +0000705
Chad Rosier27c352d2016-03-14 18:24:34 +0000706 if (NeedsRealignment) {
Matthias Braun941a7052016-07-28 18:40:00 +0000707 const unsigned Alignment = MFI.getMaxAlignment();
Chad Rosier27c352d2016-03-14 18:24:34 +0000708 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
709 assert(NrBitsToZero > 1);
710 assert(scratchSPReg != AArch64::SP);
Kristof Beyls17cb8982015-04-09 08:49:47 +0000711
Chad Rosier27c352d2016-03-14 18:24:34 +0000712 // SUB X9, SP, NumBytes
713 // -- X9 is temporary register, so shouldn't contain any live data here,
714 // -- free to use. This is already produced by emitFrameOffset above.
715 // AND SP, X9, 0b11111...0000
716 // The logical immediates have a non-trivial encoding. The following
717 // formula computes the encoded immediate with all ones but
718 // NrBitsToZero zero bits as least significant bits.
719 uint32_t andMaskEncoded = (1 << 12) // = N
720 | ((64 - NrBitsToZero) << 6) // immr
721 | ((64 - NrBitsToZero - 1) << 0); // imms
722
723 BuildMI(MBB, MBBI, DL, TII->get(AArch64::ANDXri), AArch64::SP)
724 .addReg(scratchSPReg, RegState::Kill)
725 .addImm(andMaskEncoded);
726 AFI->setStackRealigned(true);
727 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000728 }
729
730 // If we need a base pointer, set it up here. It's whatever the value of the
731 // stack pointer is at this point. Any variable size objects will be allocated
732 // after this, so we can still use the base pointer to reference locals.
733 //
734 // FIXME: Clarify FrameSetup flags here.
735 // Note: Use emitFrameOffset() like above for FP if the FrameSetup flag is
736 // needed.
Kristof Beyls17cb8982015-04-09 08:49:47 +0000737 if (RegInfo->hasBasePointer(MF)) {
738 TII->copyPhysReg(MBB, MBBI, DL, RegInfo->getBaseRegister(), AArch64::SP,
739 false);
740 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000741
742 if (needsFrameMoves) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000743 const DataLayout &TD = MF.getDataLayout();
744 const int StackGrowth = -TD.getPointerSize(0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000745 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Tim Northover3b0846e2014-05-24 12:50:23 +0000746 // An example of the prologue:
747 //
748 // .globl __foo
749 // .align 2
750 // __foo:
751 // Ltmp0:
752 // .cfi_startproc
753 // .cfi_personality 155, ___gxx_personality_v0
754 // Leh_func_begin:
755 // .cfi_lsda 16, Lexception33
756 //
757 // stp xa,bx, [sp, -#offset]!
758 // ...
759 // stp x28, x27, [sp, #offset-32]
760 // stp fp, lr, [sp, #offset-16]
761 // add fp, sp, #offset - 16
762 // sub sp, sp, #1360
763 //
764 // The Stack:
765 // +-------------------------------------------+
766 // 10000 | ........ | ........ | ........ | ........ |
767 // 10004 | ........ | ........ | ........ | ........ |
768 // +-------------------------------------------+
769 // 10008 | ........ | ........ | ........ | ........ |
770 // 1000c | ........ | ........ | ........ | ........ |
771 // +===========================================+
772 // 10010 | X28 Register |
773 // 10014 | X28 Register |
774 // +-------------------------------------------+
775 // 10018 | X27 Register |
776 // 1001c | X27 Register |
777 // +===========================================+
778 // 10020 | Frame Pointer |
779 // 10024 | Frame Pointer |
780 // +-------------------------------------------+
781 // 10028 | Link Register |
782 // 1002c | Link Register |
783 // +===========================================+
784 // 10030 | ........ | ........ | ........ | ........ |
785 // 10034 | ........ | ........ | ........ | ........ |
786 // +-------------------------------------------+
787 // 10038 | ........ | ........ | ........ | ........ |
788 // 1003c | ........ | ........ | ........ | ........ |
789 // +-------------------------------------------+
790 //
791 // [sp] = 10030 :: >>initial value<<
792 // sp = 10020 :: stp fp, lr, [sp, #-16]!
793 // fp = sp == 10020 :: mov fp, sp
794 // [sp] == 10020 :: stp x28, x27, [sp, #-16]!
795 // sp == 10010 :: >>final value<<
796 //
797 // The frame pointer (w29) points to address 10020. If we use an offset of
798 // '16' from 'w29', we get the CFI offsets of -8 for w30, -16 for w29, -24
799 // for w27, and -32 for w28:
800 //
801 // Ltmp1:
802 // .cfi_def_cfa w29, 16
803 // Ltmp2:
804 // .cfi_offset w30, -8
805 // Ltmp3:
806 // .cfi_offset w29, -16
807 // Ltmp4:
808 // .cfi_offset w27, -24
809 // Ltmp5:
810 // .cfi_offset w28, -32
811
812 if (HasFP) {
813 // Define the current CFA rule to use the provided FP.
814 unsigned Reg = RegInfo->getDwarfRegNum(FramePtr, true);
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000815 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
816 nullptr, Reg, 2 * StackGrowth - FixedObject));
Tim Northover3b0846e2014-05-24 12:50:23 +0000817 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000818 .addCFIIndex(CFIIndex)
819 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover3b0846e2014-05-24 12:50:23 +0000820 } else {
821 // Encode the stack size of the leaf function.
Matthias Braunf23ef432016-11-30 23:48:42 +0000822 unsigned CFIIndex = MF.addFrameInst(
Matthias Braun941a7052016-07-28 18:40:00 +0000823 MCCFIInstruction::createDefCfaOffset(nullptr, -MFI.getStackSize()));
Tim Northover3b0846e2014-05-24 12:50:23 +0000824 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
Adrian Prantlb9fa9452014-12-16 00:20:49 +0000825 .addCFIIndex(CFIIndex)
826 .setMIFlags(MachineInstr::FrameSetup);
Tim Northover3b0846e2014-05-24 12:50:23 +0000827 }
828
Geoff Berry62d47252016-02-25 16:36:08 +0000829 // Now emit the moves for whatever callee saved regs we have (including FP,
830 // LR if those are saved).
831 emitCalleeSavedFrameMoves(MBB, MBBI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000832 }
833}
834
Tim Northover3b0846e2014-05-24 12:50:23 +0000835void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
836 MachineBasicBlock &MBB) const {
837 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Matthias Braun941a7052016-07-28 18:40:00 +0000838 MachineFrameInfo &MFI = MF.getFrameInfo();
Ahmed Bougacha66834ec2015-12-16 22:54:06 +0000839 const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
Ahmed Bougacha66834ec2015-12-16 22:54:06 +0000840 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Quentin Colombet61b305e2015-05-05 17:38:16 +0000841 DebugLoc DL;
842 bool IsTailCallReturn = false;
843 if (MBB.end() != MBBI) {
844 DL = MBBI->getDebugLoc();
845 unsigned RetOpcode = MBBI->getOpcode();
846 IsTailCallReturn = RetOpcode == AArch64::TCRETURNdi ||
847 RetOpcode == AArch64::TCRETURNri;
848 }
Matthias Braun941a7052016-07-28 18:40:00 +0000849 int NumBytes = MFI.getStackSize();
Tim Northover3b0846e2014-05-24 12:50:23 +0000850 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
851
Greg Fitzgeraldfa78d082015-01-19 17:40:05 +0000852 // All calls are tail calls in GHC calling conv, and functions have no
853 // prologue/epilogue.
Matthias Braunf1caa282017-12-15 22:22:58 +0000854 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
Greg Fitzgeraldfa78d082015-01-19 17:40:05 +0000855 return;
856
Kristof Beyls17cb8982015-04-09 08:49:47 +0000857 // Initial and residual are named for consistency with the prologue. Note that
Tim Northover3b0846e2014-05-24 12:50:23 +0000858 // in the epilogue, the residual adjustment is executed first.
859 uint64_t ArgumentPopSize = 0;
Quentin Colombet61b305e2015-05-05 17:38:16 +0000860 if (IsTailCallReturn) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000861 MachineOperand &StackAdjust = MBBI->getOperand(1);
862
863 // For a tail-call in a callee-pops-arguments environment, some or all of
864 // the stack may actually be in use for the call's arguments, this is
865 // calculated during LowerCall and consumed here...
866 ArgumentPopSize = StackAdjust.getImm();
867 } else {
868 // ... otherwise the amount to pop is *all* of the argument space,
869 // conveniently stored in the MachineFunctionInfo by
870 // LowerFormalArguments. This will, of course, be zero for the C calling
871 // convention.
872 ArgumentPopSize = AFI->getArgumentStackToRestore();
873 }
874
875 // The stack frame should be like below,
876 //
877 // ---------------------- ---
878 // | | |
879 // | BytesInStackArgArea| CalleeArgStackSize
880 // | (NumReusableBytes) | (of tail call)
881 // | | ---
882 // | | |
883 // ---------------------| --- |
884 // | | | |
885 // | CalleeSavedReg | | |
Geoff Berry04bf91a2016-02-01 16:29:19 +0000886 // | (CalleeSavedStackSize)| | |
Tim Northover3b0846e2014-05-24 12:50:23 +0000887 // | | | |
888 // ---------------------| | NumBytes
889 // | | StackSize (StackAdjustUp)
890 // | LocalStackSize | | |
891 // | (covering callee | | |
892 // | args) | | |
893 // | | | |
894 // ---------------------- --- ---
895 //
896 // So NumBytes = StackSize + BytesInStackArgArea - CalleeArgStackSize
897 // = StackSize + ArgumentPopSize
898 //
899 // AArch64TargetLowering::LowerCall figures out ArgumentPopSize and keeps
900 // it as the 2nd argument of AArch64ISD::TC_RETURN.
Tim Northover3b0846e2014-05-24 12:50:23 +0000901
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000902 bool IsWin64 =
Matthias Braunf1caa282017-12-15 22:22:58 +0000903 Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv());
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000904 unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0;
905
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000906 uint64_t AfterCSRPopSize = ArgumentPopSize;
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000907 auto PrologueSaveSize = AFI->getCalleeSavedStackSize() + FixedObject;
Geoff Berrya5335642016-05-06 16:34:59 +0000908 bool CombineSPBump = shouldCombineCSRLocalStackBump(MF, NumBytes);
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000909 // Assume we can't combine the last pop with the sp restore.
Geoff Berrya5335642016-05-06 16:34:59 +0000910
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000911 if (!CombineSPBump && PrologueSaveSize != 0) {
912 MachineBasicBlock::iterator Pop = std::prev(MBB.getFirstTerminator());
913 // Converting the last ldp to a post-index ldp is valid only if the last
914 // ldp's offset is 0.
915 const MachineOperand &OffsetOp = Pop->getOperand(Pop->getNumOperands() - 1);
916 // If the offset is 0, convert it to a post-index ldp.
917 if (OffsetOp.getImm() == 0) {
918 convertCalleeSaveRestoreToSPPrePostIncDec(MBB, Pop, DL, TII,
919 PrologueSaveSize);
920 } else {
921 // If not, make sure to emit an add after the last ldp.
922 // We're doing this by transfering the size to be restored from the
923 // adjustment *before* the CSR pops to the adjustment *after* the CSR
924 // pops.
925 AfterCSRPopSize += PrologueSaveSize;
926 }
927 }
Geoff Berrya5335642016-05-06 16:34:59 +0000928
Tim Northover3b0846e2014-05-24 12:50:23 +0000929 // Move past the restores of the callee-saved registers.
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000930 // If we plan on combining the sp bump of the local stack size and the callee
931 // save stack size, we might need to adjust the CSR save and restore offsets.
Quentin Colombet61b305e2015-05-05 17:38:16 +0000932 MachineBasicBlock::iterator LastPopI = MBB.getFirstTerminator();
Matthias Braun45419292015-12-17 03:18:47 +0000933 MachineBasicBlock::iterator Begin = MBB.begin();
934 while (LastPopI != Begin) {
935 --LastPopI;
Geoff Berry04bf91a2016-02-01 16:29:19 +0000936 if (!LastPopI->getFlag(MachineInstr::FrameDestroy)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000937 ++LastPopI;
Matthias Braun45419292015-12-17 03:18:47 +0000938 break;
Geoff Berrya5335642016-05-06 16:34:59 +0000939 } else if (CombineSPBump)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000940 fixupCalleeSaveRestoreStackOffset(*LastPopI, AFI->getLocalStackSize());
Tim Northover3b0846e2014-05-24 12:50:23 +0000941 }
Geoff Berrya5335642016-05-06 16:34:59 +0000942
943 // If there is a single SP update, insert it before the ret and we're done.
944 if (CombineSPBump) {
945 emitFrameOffset(MBB, MBB.getFirstTerminator(), DL, AArch64::SP, AArch64::SP,
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000946 NumBytes + AfterCSRPopSize, TII,
Geoff Berrya5335642016-05-06 16:34:59 +0000947 MachineInstr::FrameDestroy);
948 return;
949 }
950
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000951 NumBytes -= PrologueSaveSize;
Tim Northover3b0846e2014-05-24 12:50:23 +0000952 assert(NumBytes >= 0 && "Negative stack allocation size!?");
953
954 if (!hasFP(MF)) {
Geoff Berrya1c62692016-02-23 16:54:36 +0000955 bool RedZone = canUseRedZone(MF);
Tim Northover3b0846e2014-05-24 12:50:23 +0000956 // If this was a redzone leaf function, we don't need to restore the
Geoff Berrya1c62692016-02-23 16:54:36 +0000957 // stack pointer (but we may need to pop stack args for fastcc).
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000958 if (RedZone && AfterCSRPopSize == 0)
Geoff Berrya1c62692016-02-23 16:54:36 +0000959 return;
960
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000961 bool NoCalleeSaveRestore = PrologueSaveSize == 0;
Geoff Berrya1c62692016-02-23 16:54:36 +0000962 int StackRestoreBytes = RedZone ? 0 : NumBytes;
963 if (NoCalleeSaveRestore)
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000964 StackRestoreBytes += AfterCSRPopSize;
Francis Visoiu Mistrihc855e922018-04-27 15:30:54 +0000965
Geoff Berrya1c62692016-02-23 16:54:36 +0000966 // If we were able to combine the local stack pop with the argument pop,
967 // then we're done.
Francis Visoiu Mistrihc855e922018-04-27 15:30:54 +0000968 bool Done = NoCalleeSaveRestore || AfterCSRPopSize == 0;
969
970 // If we're done after this, make sure to help the load store optimizer.
971 if (Done)
972 adaptForLdStOpt(MBB, MBB.getFirstTerminator(), LastPopI);
973
974 emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP,
975 StackRestoreBytes, TII, MachineInstr::FrameDestroy);
976 if (Done)
Geoff Berrya1c62692016-02-23 16:54:36 +0000977 return;
Francis Visoiu Mistrihc855e922018-04-27 15:30:54 +0000978
Geoff Berrya1c62692016-02-23 16:54:36 +0000979 NumBytes = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000980 }
981
982 // Restore the original stack pointer.
983 // FIXME: Rather than doing the math here, we should instead just use
984 // non-post-indexed loads for the restores if we aren't actually going to
985 // be able to save any instructions.
Matthias Braun941a7052016-07-28 18:40:00 +0000986 if (MFI.hasVarSizedObjects() || AFI->isStackRealigned())
Tim Northover3b0846e2014-05-24 12:50:23 +0000987 emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::FP,
Martin Storsjoeacf4e42017-08-01 21:13:54 +0000988 -AFI->getCalleeSavedStackSize() + 16, TII,
989 MachineInstr::FrameDestroy);
Chad Rosier6d986552016-03-14 18:17:41 +0000990 else if (NumBytes)
991 emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP, NumBytes, TII,
992 MachineInstr::FrameDestroy);
Geoff Berrya1c62692016-02-23 16:54:36 +0000993
994 // This must be placed after the callee-save restore code because that code
995 // assumes the SP is at the same location as it was after the callee-save save
996 // code in the prologue.
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +0000997 if (AfterCSRPopSize) {
Peter Collingbournef11eb3e2018-04-04 21:55:44 +0000998 // Find an insertion point for the first ldp so that it goes before the
999 // shadow call stack epilog instruction. This ensures that the restore of
1000 // lr from x18 is placed after the restore from sp.
1001 auto FirstSPPopI = MBB.getFirstTerminator();
1002 while (FirstSPPopI != Begin) {
1003 auto Prev = std::prev(FirstSPPopI);
1004 if (Prev->getOpcode() != AArch64::LDRXpre ||
1005 Prev->getOperand(0).getReg() == AArch64::SP)
1006 break;
1007 FirstSPPopI = Prev;
1008 }
1009
Francis Visoiu Mistrihc855e922018-04-27 15:30:54 +00001010 adaptForLdStOpt(MBB, FirstSPPopI, LastPopI);
1011
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001012 emitFrameOffset(MBB, FirstSPPopI, DL, AArch64::SP, AArch64::SP,
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +00001013 AfterCSRPopSize, TII, MachineInstr::FrameDestroy);
1014 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001015}
1016
Tim Northover3b0846e2014-05-24 12:50:23 +00001017/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
1018/// debug info. It's the same as what we use for resolving the code-gen
1019/// references for now. FIXME: This can go wrong when references are
1020/// SP-relative and simple call frames aren't used.
1021int AArch64FrameLowering::getFrameIndexReference(const MachineFunction &MF,
1022 int FI,
1023 unsigned &FrameReg) const {
1024 return resolveFrameIndexReference(MF, FI, FrameReg);
1025}
1026
1027int AArch64FrameLowering::resolveFrameIndexReference(const MachineFunction &MF,
1028 int FI, unsigned &FrameReg,
1029 bool PreferFP) const {
Matthias Braun941a7052016-07-28 18:40:00 +00001030 const MachineFrameInfo &MFI = MF.getFrameInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001031 const AArch64RegisterInfo *RegInfo = static_cast<const AArch64RegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001032 MF.getSubtarget().getRegisterInfo());
Tim Northover3b0846e2014-05-24 12:50:23 +00001033 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
Martin Storsjoeacf4e42017-08-01 21:13:54 +00001034 const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
1035 bool IsWin64 =
Matthias Braunf1caa282017-12-15 22:22:58 +00001036 Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv());
Martin Storsjoeacf4e42017-08-01 21:13:54 +00001037 unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0;
1038 int FPOffset = MFI.getObjectOffset(FI) + FixedObject + 16;
Matthias Braun941a7052016-07-28 18:40:00 +00001039 int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
1040 bool isFixed = MFI.isFixedObjectIndex(FI);
Geoff Berry08ab8c952018-04-26 18:50:45 +00001041 bool isCSR = !isFixed && MFI.getObjectOffset(FI) >=
1042 -((int)AFI->getCalleeSavedStackSize());
Tim Northover3b0846e2014-05-24 12:50:23 +00001043
1044 // Use frame pointer to reference fixed objects. Use it for locals if
Kristof Beyls17cb8982015-04-09 08:49:47 +00001045 // there are VLAs or a dynamically realigned SP (and thus the SP isn't
1046 // reliable as a base). Make sure useFPForScavengingIndex() does the
1047 // right thing for the emergency spill slot.
Tim Northover3b0846e2014-05-24 12:50:23 +00001048 bool UseFP = false;
1049 if (AFI->hasStackFrame()) {
1050 // Note: Keeping the following as multiple 'if' statements rather than
1051 // merging to a single expression for readability.
1052 //
1053 // Argument access should always use the FP.
1054 if (isFixed) {
1055 UseFP = hasFP(MF);
Geoff Berry08ab8c952018-04-26 18:50:45 +00001056 } else if (isCSR && RegInfo->needsStackRealignment(MF)) {
1057 // References to the CSR area must use FP if we're re-aligning the stack
1058 // since the dynamically-sized alignment padding is between the SP/BP and
1059 // the CSR area.
1060 assert(hasFP(MF) && "Re-aligned stack must have frame pointer");
1061 UseFP = true;
Francis Visoiu Mistrihf2c22052018-04-10 11:29:40 +00001062 } else if (hasFP(MF) && !RegInfo->needsStackRealignment(MF)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001063 // If the FPOffset is negative, we have to keep in mind that the
1064 // available offset range for negative offsets is smaller than for
Francis Visoiu Mistrihf2c22052018-04-10 11:29:40 +00001065 // positive ones. If an offset is
Tim Northover3b0846e2014-05-24 12:50:23 +00001066 // available via the FP and the SP, use whichever is closest.
Francis Visoiu Mistrihf2c22052018-04-10 11:29:40 +00001067 bool FPOffsetFits = FPOffset >= -256;
1068 PreferFP |= Offset > -FPOffset;
1069
1070 if (MFI.hasVarSizedObjects()) {
1071 // If we have variable sized objects, we can use either FP or BP, as the
1072 // SP offset is unknown. We can use the base pointer if we have one and
1073 // FP is not preferred. If not, we're stuck with using FP.
1074 bool CanUseBP = RegInfo->hasBasePointer(MF);
1075 if (FPOffsetFits && CanUseBP) // Both are ok. Pick the best.
1076 UseFP = PreferFP;
1077 else if (!CanUseBP) // Can't use BP. Forced to use FP.
1078 UseFP = true;
1079 // else we can use BP and FP, but the offset from FP won't fit.
1080 // That will make us scavenge registers which we can probably avoid by
1081 // using BP. If it won't fit for BP either, we'll scavenge anyway.
Francis Visoiu Mistrih64639222018-04-11 12:36:55 +00001082 } else if (FPOffset >= 0) {
Francis Visoiu Mistrihf2c22052018-04-10 11:29:40 +00001083 // Use SP or FP, whichever gives us the best chance of the offset
1084 // being in range for direct access. If the FPOffset is positive,
1085 // that'll always be best, as the SP will be even further away.
Tim Northover3b0846e2014-05-24 12:50:23 +00001086 UseFP = true;
Francis Visoiu Mistrihf2c22052018-04-10 11:29:40 +00001087 } else {
1088 // We have the choice between FP and (SP or BP).
1089 if (FPOffsetFits && PreferFP) // If FP is the best fit, use it.
1090 UseFP = true;
1091 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001092 }
1093 }
1094
Geoff Berry08ab8c952018-04-26 18:50:45 +00001095 assert(((isFixed || isCSR) || !RegInfo->needsStackRealignment(MF) || !UseFP) &&
Kristof Beyls17cb8982015-04-09 08:49:47 +00001096 "In the presence of dynamic stack pointer realignment, "
Geoff Berry08ab8c952018-04-26 18:50:45 +00001097 "non-argument/CSR objects cannot be accessed through the frame pointer");
Kristof Beyls17cb8982015-04-09 08:49:47 +00001098
Tim Northover3b0846e2014-05-24 12:50:23 +00001099 if (UseFP) {
1100 FrameReg = RegInfo->getFrameRegister(MF);
1101 return FPOffset;
1102 }
1103
1104 // Use the base pointer if we have one.
1105 if (RegInfo->hasBasePointer(MF))
1106 FrameReg = RegInfo->getBaseRegister();
1107 else {
Francis Visoiu Mistrihf2c22052018-04-10 11:29:40 +00001108 assert(!MFI.hasVarSizedObjects() &&
1109 "Can't use SP when we have var sized objects.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001110 FrameReg = AArch64::SP;
1111 // If we're using the red zone for this function, the SP won't actually
1112 // be adjusted, so the offsets will be negative. They're also all
1113 // within range of the signed 9-bit immediate instructions.
1114 if (canUseRedZone(MF))
1115 Offset -= AFI->getLocalStackSize();
1116 }
1117
1118 return Offset;
1119}
1120
1121static unsigned getPrologueDeath(MachineFunction &MF, unsigned Reg) {
Matthias Braun74a0bd32016-04-13 21:43:16 +00001122 // Do not set a kill flag on values that are also marked as live-in. This
1123 // happens with the @llvm-returnaddress intrinsic and with arguments passed in
1124 // callee saved registers.
1125 // Omitting the kill flags is conservatively correct even if the live-in
1126 // is not used after all.
1127 bool IsLiveIn = MF.getRegInfo().isLiveIn(Reg);
1128 return getKillRegState(!IsLiveIn);
Tim Northover3b0846e2014-05-24 12:50:23 +00001129}
1130
Manman Ren57518142016-04-11 21:08:06 +00001131static bool produceCompactUnwindFrame(MachineFunction &MF) {
1132 const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
Matthias Braunf1caa282017-12-15 22:22:58 +00001133 AttributeList Attrs = MF.getFunction().getAttributes();
Manman Ren57518142016-04-11 21:08:06 +00001134 return Subtarget.isTargetMachO() &&
1135 !(Subtarget.getTargetLowering()->supportSwiftError() &&
1136 Attrs.hasAttrSomewhere(Attribute::SwiftError));
1137}
1138
Benjamin Kramerb7d33112016-08-06 11:13:10 +00001139namespace {
Eugene Zelenko11f69072017-01-25 00:29:26 +00001140
Geoff Berry29d4a692016-02-01 19:07:06 +00001141struct RegPairInfo {
Eugene Zelenko11f69072017-01-25 00:29:26 +00001142 unsigned Reg1 = AArch64::NoRegister;
1143 unsigned Reg2 = AArch64::NoRegister;
Geoff Berry29d4a692016-02-01 19:07:06 +00001144 int FrameIdx;
1145 int Offset;
1146 bool IsGPR;
Eugene Zelenko11f69072017-01-25 00:29:26 +00001147
1148 RegPairInfo() = default;
1149
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001150 bool isPaired() const { return Reg2 != AArch64::NoRegister; }
Geoff Berry29d4a692016-02-01 19:07:06 +00001151};
Eugene Zelenko11f69072017-01-25 00:29:26 +00001152
Benjamin Kramerb7d33112016-08-06 11:13:10 +00001153} // end anonymous namespace
Geoff Berry29d4a692016-02-01 19:07:06 +00001154
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001155static void computeCalleeSaveRegisterPairs(
1156 MachineFunction &MF, const std::vector<CalleeSavedInfo> &CSI,
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001157 const TargetRegisterInfo *TRI, SmallVectorImpl<RegPairInfo> &RegPairs,
1158 bool &NeedShadowCallStackProlog) {
Geoff Berry29d4a692016-02-01 19:07:06 +00001159
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001160 if (CSI.empty())
1161 return;
1162
1163 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +00001164 MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +00001165 CallingConv::ID CC = MF.getFunction().getCallingConv();
Tim Northover3b0846e2014-05-24 12:50:23 +00001166 unsigned Count = CSI.size();
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001167 (void)CC;
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001168 // MachO's compact unwind format relies on all registers being stored in
1169 // pairs.
Manman Ren57518142016-04-11 21:08:06 +00001170 assert((!produceCompactUnwindFrame(MF) ||
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001171 CC == CallingConv::PreserveMost ||
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001172 (Count & 1) == 0) &&
1173 "Odd number of callee-saved regs to spill!");
Martin Storsjo68266fa2017-07-13 17:03:12 +00001174 int Offset = AFI->getCalleeSavedStackSize();
1175
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001176 for (unsigned i = 0; i < Count; ++i) {
Geoff Berry29d4a692016-02-01 19:07:06 +00001177 RegPairInfo RPI;
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001178 RPI.Reg1 = CSI[i].getReg();
1179
1180 assert(AArch64::GPR64RegClass.contains(RPI.Reg1) ||
1181 AArch64::FPR64RegClass.contains(RPI.Reg1));
1182 RPI.IsGPR = AArch64::GPR64RegClass.contains(RPI.Reg1);
1183
1184 // Add the next reg to the pair if it is in the same register class.
1185 if (i + 1 < Count) {
1186 unsigned NextReg = CSI[i + 1].getReg();
1187 if ((RPI.IsGPR && AArch64::GPR64RegClass.contains(NextReg)) ||
1188 (!RPI.IsGPR && AArch64::FPR64RegClass.contains(NextReg)))
1189 RPI.Reg2 = NextReg;
1190 }
Geoff Berry29d4a692016-02-01 19:07:06 +00001191
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001192 // If either of the registers to be saved is the lr register, it means that
1193 // we also need to save lr in the shadow call stack.
1194 if ((RPI.Reg1 == AArch64::LR || RPI.Reg2 == AArch64::LR) &&
1195 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)) {
1196 if (!MF.getSubtarget<AArch64Subtarget>().isX18Reserved())
1197 report_fatal_error("Must reserve x18 to use shadow call stack");
1198 NeedShadowCallStackProlog = true;
1199 }
1200
Tim Northover3b0846e2014-05-24 12:50:23 +00001201 // GPRs and FPRs are saved in pairs of 64-bit regs. We expect the CSI
1202 // list to come in sorted by frame index so that we can issue the store
1203 // pair instructions directly. Assert if we see anything otherwise.
1204 //
1205 // The order of the registers in the list is controlled by
1206 // getCalleeSavedRegs(), so they will always be in-order, as well.
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001207 assert((!RPI.isPaired() ||
1208 (CSI[i].getFrameIdx() + 1 == CSI[i + 1].getFrameIdx())) &&
Tim Northover3b0846e2014-05-24 12:50:23 +00001209 "Out of order callee saved regs!");
Geoff Berry29d4a692016-02-01 19:07:06 +00001210
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001211 // MachO's compact unwind format relies on all registers being stored in
1212 // adjacent register pairs.
Manman Ren57518142016-04-11 21:08:06 +00001213 assert((!produceCompactUnwindFrame(MF) ||
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001214 CC == CallingConv::PreserveMost ||
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001215 (RPI.isPaired() &&
1216 ((RPI.Reg1 == AArch64::LR && RPI.Reg2 == AArch64::FP) ||
1217 RPI.Reg1 + 1 == RPI.Reg2))) &&
1218 "Callee-save registers not saved as adjacent register pair!");
1219
1220 RPI.FrameIdx = CSI[i].getFrameIdx();
1221
1222 if (Count * 8 != AFI->getCalleeSavedStackSize() && !RPI.isPaired()) {
1223 // Round up size of non-pair to pair size if we need to pad the
1224 // callee-save area to ensure 16-byte alignment.
1225 Offset -= 16;
Matthias Braun941a7052016-07-28 18:40:00 +00001226 assert(MFI.getObjectAlignment(RPI.FrameIdx) <= 16);
1227 MFI.setObjectAlignment(RPI.FrameIdx, 16);
Geoff Berry66f6b652016-06-02 16:22:07 +00001228 AFI->setCalleeSaveStackHasFreeSpace(true);
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001229 } else
1230 Offset -= RPI.isPaired() ? 16 : 8;
1231 assert(Offset % 8 == 0);
1232 RPI.Offset = Offset / 8;
Geoff Berry29d4a692016-02-01 19:07:06 +00001233 assert((RPI.Offset >= -64 && RPI.Offset <= 63) &&
1234 "Offset out of bounds for LDP/STP immediate");
1235
1236 RegPairs.push_back(RPI);
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001237 if (RPI.isPaired())
1238 ++i;
Geoff Berry29d4a692016-02-01 19:07:06 +00001239 }
1240}
1241
1242bool AArch64FrameLowering::spillCalleeSavedRegisters(
1243 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1244 const std::vector<CalleeSavedInfo> &CSI,
1245 const TargetRegisterInfo *TRI) const {
1246 MachineFunction &MF = *MBB.getParent();
1247 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1248 DebugLoc DL;
1249 SmallVector<RegPairInfo, 8> RegPairs;
1250
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001251 bool NeedShadowCallStackProlog = false;
1252 computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs,
1253 NeedShadowCallStackProlog);
Matthias Braun88c8c982017-05-27 03:38:02 +00001254 const MachineRegisterInfo &MRI = MF.getRegInfo();
Geoff Berry29d4a692016-02-01 19:07:06 +00001255
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001256 if (NeedShadowCallStackProlog) {
1257 // Shadow call stack prolog: str x30, [x18], #8
1258 BuildMI(MBB, MI, DL, TII.get(AArch64::STRXpost))
1259 .addReg(AArch64::X18, RegState::Define)
1260 .addReg(AArch64::LR)
1261 .addReg(AArch64::X18)
1262 .addImm(8)
1263 .setMIFlag(MachineInstr::FrameSetup);
1264
1265 // This instruction also makes x18 live-in to the entry block.
1266 MBB.addLiveIn(AArch64::X18);
1267 }
1268
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001269 for (auto RPII = RegPairs.rbegin(), RPIE = RegPairs.rend(); RPII != RPIE;
Geoff Berry29d4a692016-02-01 19:07:06 +00001270 ++RPII) {
1271 RegPairInfo RPI = *RPII;
1272 unsigned Reg1 = RPI.Reg1;
1273 unsigned Reg2 = RPI.Reg2;
1274 unsigned StrOpc;
1275
Geoff Berrya5335642016-05-06 16:34:59 +00001276 // Issue sequence of spills for cs regs. The first spill may be converted
1277 // to a pre-decrement store later by emitPrologue if the callee-save stack
1278 // area allocation can't be combined with the local stack area allocation.
Tim Northover3b0846e2014-05-24 12:50:23 +00001279 // For example:
Geoff Berrya5335642016-05-06 16:34:59 +00001280 // stp x22, x21, [sp, #0] // addImm(+0)
Tim Northover3b0846e2014-05-24 12:50:23 +00001281 // stp x20, x19, [sp, #16] // addImm(+2)
1282 // stp fp, lr, [sp, #32] // addImm(+4)
1283 // Rationale: This sequence saves uop updates compared to a sequence of
1284 // pre-increment spills like stp xi,xj,[sp,#-16]!
Geoff Berry29d4a692016-02-01 19:07:06 +00001285 // Note: Similar rationale and sequence for restores in epilog.
Geoff Berrya5335642016-05-06 16:34:59 +00001286 if (RPI.IsGPR)
1287 StrOpc = RPI.isPaired() ? AArch64::STPXi : AArch64::STRXui;
1288 else
1289 StrOpc = RPI.isPaired() ? AArch64::STPDi : AArch64::STRDui;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001290 LLVM_DEBUG(dbgs() << "CSR spill: (" << printReg(Reg1, TRI);
1291 if (RPI.isPaired()) dbgs() << ", " << printReg(Reg2, TRI);
1292 dbgs() << ") -> fi#(" << RPI.FrameIdx;
1293 if (RPI.isPaired()) dbgs() << ", " << RPI.FrameIdx + 1;
1294 dbgs() << ")\n");
Geoff Berry29d4a692016-02-01 19:07:06 +00001295
Tim Northover3b0846e2014-05-24 12:50:23 +00001296 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc));
Matthias Braun88c8c982017-05-27 03:38:02 +00001297 if (!MRI.isReserved(Reg1))
1298 MBB.addLiveIn(Reg1);
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001299 if (RPI.isPaired()) {
Matthias Braun88c8c982017-05-27 03:38:02 +00001300 if (!MRI.isReserved(Reg2))
1301 MBB.addLiveIn(Reg2);
Geoff Berrya5335642016-05-06 16:34:59 +00001302 MIB.addReg(Reg2, getPrologueDeath(MF, Reg2));
Geoff Berryc3764062016-04-15 15:16:19 +00001303 MIB.addMemOperand(MF.getMachineMemOperand(
1304 MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx + 1),
1305 MachineMemOperand::MOStore, 8, 8));
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001306 }
Geoff Berrya5335642016-05-06 16:34:59 +00001307 MIB.addReg(Reg1, getPrologueDeath(MF, Reg1))
1308 .addReg(AArch64::SP)
1309 .addImm(RPI.Offset) // [sp, #offset*8], where factor*8 is implicit
1310 .setMIFlag(MachineInstr::FrameSetup);
Geoff Berryc3764062016-04-15 15:16:19 +00001311 MIB.addMemOperand(MF.getMachineMemOperand(
1312 MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx),
1313 MachineMemOperand::MOStore, 8, 8));
Tim Northover3b0846e2014-05-24 12:50:23 +00001314 }
1315 return true;
1316}
1317
1318bool AArch64FrameLowering::restoreCalleeSavedRegisters(
1319 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00001320 std::vector<CalleeSavedInfo> &CSI,
Tim Northover3b0846e2014-05-24 12:50:23 +00001321 const TargetRegisterInfo *TRI) const {
1322 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001323 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001324 DebugLoc DL;
Geoff Berry29d4a692016-02-01 19:07:06 +00001325 SmallVector<RegPairInfo, 8> RegPairs;
Tim Northover3b0846e2014-05-24 12:50:23 +00001326
1327 if (MI != MBB.end())
1328 DL = MI->getDebugLoc();
1329
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001330 bool NeedShadowCallStackProlog = false;
1331 computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs,
1332 NeedShadowCallStackProlog);
Geoff Berry29d4a692016-02-01 19:07:06 +00001333
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +00001334 auto EmitMI = [&](const RegPairInfo &RPI) {
Geoff Berry29d4a692016-02-01 19:07:06 +00001335 unsigned Reg1 = RPI.Reg1;
1336 unsigned Reg2 = RPI.Reg2;
1337
Geoff Berrya5335642016-05-06 16:34:59 +00001338 // Issue sequence of restores for cs regs. The last restore may be converted
1339 // to a post-increment load later by emitEpilogue if the callee-save stack
1340 // area allocation can't be combined with the local stack area allocation.
Tim Northover3b0846e2014-05-24 12:50:23 +00001341 // For example:
1342 // ldp fp, lr, [sp, #32] // addImm(+4)
1343 // ldp x20, x19, [sp, #16] // addImm(+2)
Geoff Berrya5335642016-05-06 16:34:59 +00001344 // ldp x22, x21, [sp, #0] // addImm(+0)
Tim Northover3b0846e2014-05-24 12:50:23 +00001345 // Note: see comment in spillCalleeSavedRegisters()
1346 unsigned LdrOpc;
Geoff Berrya5335642016-05-06 16:34:59 +00001347 if (RPI.IsGPR)
1348 LdrOpc = RPI.isPaired() ? AArch64::LDPXi : AArch64::LDRXui;
1349 else
1350 LdrOpc = RPI.isPaired() ? AArch64::LDPDi : AArch64::LDRDui;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001351 LLVM_DEBUG(dbgs() << "CSR restore: (" << printReg(Reg1, TRI);
1352 if (RPI.isPaired()) dbgs() << ", " << printReg(Reg2, TRI);
1353 dbgs() << ") -> fi#(" << RPI.FrameIdx;
1354 if (RPI.isPaired()) dbgs() << ", " << RPI.FrameIdx + 1;
1355 dbgs() << ")\n");
Tim Northover3b0846e2014-05-24 12:50:23 +00001356
Tim Northover3b0846e2014-05-24 12:50:23 +00001357 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdrOpc));
Geoff Berryc3764062016-04-15 15:16:19 +00001358 if (RPI.isPaired()) {
Geoff Berrya5335642016-05-06 16:34:59 +00001359 MIB.addReg(Reg2, getDefRegState(true));
Geoff Berryc3764062016-04-15 15:16:19 +00001360 MIB.addMemOperand(MF.getMachineMemOperand(
1361 MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx + 1),
1362 MachineMemOperand::MOLoad, 8, 8));
Geoff Berryc3764062016-04-15 15:16:19 +00001363 }
Geoff Berrya5335642016-05-06 16:34:59 +00001364 MIB.addReg(Reg1, getDefRegState(true))
1365 .addReg(AArch64::SP)
1366 .addImm(RPI.Offset) // [sp, #offset*8] where the factor*8 is implicit
1367 .setMIFlag(MachineInstr::FrameDestroy);
Geoff Berryc3764062016-04-15 15:16:19 +00001368 MIB.addMemOperand(MF.getMachineMemOperand(
1369 MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx),
1370 MachineMemOperand::MOLoad, 8, 8));
Francis Visoiu Mistrih164560b2018-03-14 20:34:03 +00001371 };
1372
1373 if (ReverseCSRRestoreSeq)
1374 for (const RegPairInfo &RPI : reverse(RegPairs))
1375 EmitMI(RPI);
1376 else
1377 for (const RegPairInfo &RPI : RegPairs)
1378 EmitMI(RPI);
Peter Collingbournef11eb3e2018-04-04 21:55:44 +00001379
1380 if (NeedShadowCallStackProlog) {
1381 // Shadow call stack epilog: ldr x30, [x18, #-8]!
1382 BuildMI(MBB, MI, DL, TII.get(AArch64::LDRXpre))
1383 .addReg(AArch64::X18, RegState::Define)
1384 .addReg(AArch64::LR, RegState::Define)
1385 .addReg(AArch64::X18)
1386 .addImm(-8)
1387 .setMIFlag(MachineInstr::FrameDestroy);
1388 }
1389
Tim Northover3b0846e2014-05-24 12:50:23 +00001390 return true;
1391}
1392
Matthias Braun02564862015-07-14 17:17:13 +00001393void AArch64FrameLowering::determineCalleeSaves(MachineFunction &MF,
1394 BitVector &SavedRegs,
1395 RegScavenger *RS) const {
1396 // All calls are tail calls in GHC calling conv, and functions have no
1397 // prologue/epilogue.
Matthias Braunf1caa282017-12-15 22:22:58 +00001398 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
Matthias Braun02564862015-07-14 17:17:13 +00001399 return;
1400
1401 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Tim Northover3b0846e2014-05-24 12:50:23 +00001402 const AArch64RegisterInfo *RegInfo = static_cast<const AArch64RegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001403 MF.getSubtarget().getRegisterInfo());
Tim Northover3b0846e2014-05-24 12:50:23 +00001404 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001405 unsigned UnspilledCSGPR = AArch64::NoRegister;
1406 unsigned UnspilledCSGPRPaired = AArch64::NoRegister;
Tim Northover3b0846e2014-05-24 12:50:23 +00001407
Martin Storsjo2778fd02017-12-20 06:51:45 +00001408 MachineFrameInfo &MFI = MF.getFrameInfo();
1409 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
1410
1411 unsigned BasePointerReg = RegInfo->hasBasePointer(MF)
1412 ? RegInfo->getBaseRegister()
1413 : (unsigned)AArch64::NoRegister;
1414
1415 unsigned SpillEstimate = SavedRegs.count();
1416 for (unsigned i = 0; CSRegs[i]; ++i) {
1417 unsigned Reg = CSRegs[i];
1418 unsigned PairedReg = CSRegs[i ^ 1];
1419 if (Reg == BasePointerReg)
1420 SpillEstimate++;
1421 if (produceCompactUnwindFrame(MF) && !SavedRegs.test(PairedReg))
1422 SpillEstimate++;
1423 }
1424 SpillEstimate += 2; // Conservatively include FP+LR in the estimate
1425 unsigned StackEstimate = MFI.estimateStackSize(MF) + 8 * SpillEstimate;
1426
Tim Northover3b0846e2014-05-24 12:50:23 +00001427 // The frame record needs to be created by saving the appropriate registers
Martin Storsjo2778fd02017-12-20 06:51:45 +00001428 if (hasFP(MF) || windowsRequiresStackProbe(MF, StackEstimate)) {
Matthias Braun02564862015-07-14 17:17:13 +00001429 SavedRegs.set(AArch64::FP);
1430 SavedRegs.set(AArch64::LR);
Tim Northover3b0846e2014-05-24 12:50:23 +00001431 }
1432
Matthias Braund78597e2017-04-21 22:42:08 +00001433 unsigned ExtraCSSpill = 0;
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001434 // Figure out which callee-saved registers to save/restore.
1435 for (unsigned i = 0; CSRegs[i]; ++i) {
1436 const unsigned Reg = CSRegs[i];
Tim Northover3b0846e2014-05-24 12:50:23 +00001437
Geoff Berry7e4ba3d2016-02-19 18:27:32 +00001438 // Add the base pointer register to SavedRegs if it is callee-save.
1439 if (Reg == BasePointerReg)
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001440 SavedRegs.set(Reg);
Tim Northover3b0846e2014-05-24 12:50:23 +00001441
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001442 bool RegUsed = SavedRegs.test(Reg);
1443 unsigned PairedReg = CSRegs[i ^ 1];
1444 if (!RegUsed) {
1445 if (AArch64::GPR64RegClass.contains(Reg) &&
1446 !RegInfo->isReservedReg(MF, Reg)) {
1447 UnspilledCSGPR = Reg;
1448 UnspilledCSGPRPaired = PairedReg;
Tim Northover3b0846e2014-05-24 12:50:23 +00001449 }
1450 continue;
1451 }
1452
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001453 // MachO's compact unwind format relies on all registers being stored in
1454 // pairs.
1455 // FIXME: the usual format is actually better if unwinding isn't needed.
Manman Ren57518142016-04-11 21:08:06 +00001456 if (produceCompactUnwindFrame(MF) && !SavedRegs.test(PairedReg)) {
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001457 SavedRegs.set(PairedReg);
Geoff Berry74cb7182016-05-16 20:52:28 +00001458 if (AArch64::GPR64RegClass.contains(PairedReg) &&
1459 !RegInfo->isReservedReg(MF, PairedReg))
Matthias Braund78597e2017-04-21 22:42:08 +00001460 ExtraCSSpill = PairedReg;
Tim Northover3b0846e2014-05-24 12:50:23 +00001461 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001462 }
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001463
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001464 LLVM_DEBUG(dbgs() << "*** determineCalleeSaves\nUsed CSRs:";
1465 for (unsigned Reg
1466 : SavedRegs.set_bits()) dbgs()
1467 << ' ' << printReg(Reg, RegInfo);
1468 dbgs() << "\n";);
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001469
1470 // If any callee-saved registers are used, the frame cannot be eliminated.
1471 unsigned NumRegsSpilled = SavedRegs.count();
1472 bool CanEliminateFrame = NumRegsSpilled == 0;
Tim Northover3b0846e2014-05-24 12:50:23 +00001473
Tim Northover3b0846e2014-05-24 12:50:23 +00001474 // The CSR spill slots have not been allocated yet, so estimateStackSize
1475 // won't include them.
Matthias Braun941a7052016-07-28 18:40:00 +00001476 unsigned CFSize = MFI.estimateStackSize(MF) + 8 * NumRegsSpilled;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001477 LLVM_DEBUG(dbgs() << "Estimated stack frame size: " << CFSize << " bytes.\n");
Kristof Beyls2af1e902017-05-30 06:58:41 +00001478 unsigned EstimatedStackSizeLimit = estimateRSStackSizeLimit(MF);
1479 bool BigStack = (CFSize > EstimatedStackSizeLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001480 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF))
1481 AFI->setHasStackFrame(true);
1482
1483 // Estimate if we might need to scavenge a register at some point in order
1484 // to materialize a stack offset. If so, either spill one additional
1485 // callee-saved register or reserve a special spill slot to facilitate
1486 // register scavenging. If we already spilled an extra callee-saved register
1487 // above to keep the number of spills even, we don't need to do anything else
1488 // here.
Matthias Braund78597e2017-04-21 22:42:08 +00001489 if (BigStack) {
1490 if (!ExtraCSSpill && UnspilledCSGPR != AArch64::NoRegister) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001491 LLVM_DEBUG(dbgs() << "Spilling " << printReg(UnspilledCSGPR, RegInfo)
1492 << " to get a scratch register.\n");
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001493 SavedRegs.set(UnspilledCSGPR);
1494 // MachO's compact unwind format relies on all registers being stored in
1495 // pairs, so if we need to spill one extra for BigStack, then we need to
1496 // store the pair.
Manman Ren57518142016-04-11 21:08:06 +00001497 if (produceCompactUnwindFrame(MF))
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001498 SavedRegs.set(UnspilledCSGPRPaired);
Matthias Braund78597e2017-04-21 22:42:08 +00001499 ExtraCSSpill = UnspilledCSGPRPaired;
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001500 NumRegsSpilled = SavedRegs.count();
Tim Northover3b0846e2014-05-24 12:50:23 +00001501 }
1502
1503 // If we didn't find an extra callee-saved register to spill, create
1504 // an emergency spill slot.
Matthias Braund78597e2017-04-21 22:42:08 +00001505 if (!ExtraCSSpill || MF.getRegInfo().isPhysRegUsed(ExtraCSSpill)) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001506 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1507 const TargetRegisterClass &RC = AArch64::GPR64RegClass;
1508 unsigned Size = TRI->getSpillSize(RC);
1509 unsigned Align = TRI->getSpillAlignment(RC);
1510 int FI = MFI.CreateStackObject(Size, Align, false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001511 RS->addScavengingFrameIndex(FI);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001512 LLVM_DEBUG(dbgs() << "No available CS registers, allocated fi#" << FI
1513 << " as the emergency spill slot.\n");
Tim Northover3b0846e2014-05-24 12:50:23 +00001514 }
1515 }
Geoff Berry04bf91a2016-02-01 16:29:19 +00001516
Geoff Berryc25d3bd2016-02-12 16:31:41 +00001517 // Round up to register pair alignment to avoid additional SP adjustment
1518 // instructions.
1519 AFI->setCalleeSavedStackSize(alignTo(8 * NumRegsSpilled, 16));
Tim Northover3b0846e2014-05-24 12:50:23 +00001520}
Geoff Berry66f6b652016-06-02 16:22:07 +00001521
1522bool AArch64FrameLowering::enableStackSlotScavenging(
1523 const MachineFunction &MF) const {
1524 const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
1525 return AFI->hasCalleeSaveStackFreeSpace();
1526}