17330f729Sjoerg //===- ARMFrameLowering.cpp - ARM Frame Information -----------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file contains the ARM implementation of TargetFrameLowering class.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
12*82d56013Sjoerg //
13*82d56013Sjoerg // This file contains the ARM implementation of TargetFrameLowering class.
14*82d56013Sjoerg //
15*82d56013Sjoerg // On ARM, stack frames are structured as follows:
16*82d56013Sjoerg //
17*82d56013Sjoerg // The stack grows downward.
18*82d56013Sjoerg //
19*82d56013Sjoerg // All of the individual frame areas on the frame below are optional, i.e. it's
20*82d56013Sjoerg // possible to create a function so that the particular area isn't present
21*82d56013Sjoerg // in the frame.
22*82d56013Sjoerg //
23*82d56013Sjoerg // At function entry, the "frame" looks as follows:
24*82d56013Sjoerg //
25*82d56013Sjoerg // | | Higher address
26*82d56013Sjoerg // |-----------------------------------|
27*82d56013Sjoerg // | |
28*82d56013Sjoerg // | arguments passed on the stack |
29*82d56013Sjoerg // | |
30*82d56013Sjoerg // |-----------------------------------| <- sp
31*82d56013Sjoerg // | | Lower address
32*82d56013Sjoerg //
33*82d56013Sjoerg //
34*82d56013Sjoerg // After the prologue has run, the frame has the following general structure.
35*82d56013Sjoerg // Technically the last frame area (VLAs) doesn't get created until in the
36*82d56013Sjoerg // main function body, after the prologue is run. However, it's depicted here
37*82d56013Sjoerg // for completeness.
38*82d56013Sjoerg //
39*82d56013Sjoerg // | | Higher address
40*82d56013Sjoerg // |-----------------------------------|
41*82d56013Sjoerg // | |
42*82d56013Sjoerg // | arguments passed on the stack |
43*82d56013Sjoerg // | |
44*82d56013Sjoerg // |-----------------------------------| <- (sp at function entry)
45*82d56013Sjoerg // | |
46*82d56013Sjoerg // | varargs from registers |
47*82d56013Sjoerg // | |
48*82d56013Sjoerg // |-----------------------------------|
49*82d56013Sjoerg // | |
50*82d56013Sjoerg // | prev_fp, prev_lr |
51*82d56013Sjoerg // | (a.k.a. "frame record") |
52*82d56013Sjoerg // | |
53*82d56013Sjoerg // |- - - - - - - - - - - - - - - - - -| <- fp (r7 or r11)
54*82d56013Sjoerg // | |
55*82d56013Sjoerg // | callee-saved gpr registers |
56*82d56013Sjoerg // | |
57*82d56013Sjoerg // |-----------------------------------|
58*82d56013Sjoerg // | |
59*82d56013Sjoerg // | callee-saved fp/simd regs |
60*82d56013Sjoerg // | |
61*82d56013Sjoerg // |-----------------------------------|
62*82d56013Sjoerg // |.empty.space.to.make.part.below....|
63*82d56013Sjoerg // |.aligned.in.case.it.needs.more.than| (size of this area is unknown at
64*82d56013Sjoerg // |.the.standard.8-byte.alignment.....| compile time; if present)
65*82d56013Sjoerg // |-----------------------------------|
66*82d56013Sjoerg // | |
67*82d56013Sjoerg // | local variables of fixed size |
68*82d56013Sjoerg // | including spill slots |
69*82d56013Sjoerg // |-----------------------------------| <- base pointer (not defined by ABI,
70*82d56013Sjoerg // |.variable-sized.local.variables....| LLVM chooses r6)
71*82d56013Sjoerg // |.(VLAs)............................| (size of this area is unknown at
72*82d56013Sjoerg // |...................................| compile time)
73*82d56013Sjoerg // |-----------------------------------| <- sp
74*82d56013Sjoerg // | | Lower address
75*82d56013Sjoerg //
76*82d56013Sjoerg //
77*82d56013Sjoerg // To access the data in a frame, at-compile time, a constant offset must be
78*82d56013Sjoerg // computable from one of the pointers (fp, bp, sp) to access it. The size
79*82d56013Sjoerg // of the areas with a dotted background cannot be computed at compile-time
80*82d56013Sjoerg // if they are present, making it required to have all three of fp, bp and
81*82d56013Sjoerg // sp to be set up to be able to access all contents in the frame areas,
82*82d56013Sjoerg // assuming all of the frame areas are non-empty.
83*82d56013Sjoerg //
84*82d56013Sjoerg // For most functions, some of the frame areas are empty. For those functions,
85*82d56013Sjoerg // it may not be necessary to set up fp or bp:
86*82d56013Sjoerg // * A base pointer is definitely needed when there are both VLAs and local
87*82d56013Sjoerg // variables with more-than-default alignment requirements.
88*82d56013Sjoerg // * A frame pointer is definitely needed when there are local variables with
89*82d56013Sjoerg // more-than-default alignment requirements.
90*82d56013Sjoerg //
91*82d56013Sjoerg // In some cases when a base pointer is not strictly needed, it is generated
92*82d56013Sjoerg // anyway when offsets from the frame pointer to access local variables become
93*82d56013Sjoerg // so large that the offset can't be encoded in the immediate fields of loads
94*82d56013Sjoerg // or stores.
95*82d56013Sjoerg //
96*82d56013Sjoerg // The frame pointer might be chosen to be r7 or r11, depending on the target
97*82d56013Sjoerg // architecture and operating system. See ARMSubtarget::useR7AsFramePointer for
98*82d56013Sjoerg // details.
99*82d56013Sjoerg //
100*82d56013Sjoerg // Outgoing function arguments must be at the bottom of the stack frame when
101*82d56013Sjoerg // calling another function. If we do not have variable-sized stack objects, we
102*82d56013Sjoerg // can allocate a "reserved call frame" area at the bottom of the local
103*82d56013Sjoerg // variable area, large enough for all outgoing calls. If we do have VLAs, then
104*82d56013Sjoerg // the stack pointer must be decremented and incremented around each call to
105*82d56013Sjoerg // make space for the arguments below the VLAs.
106*82d56013Sjoerg //
107*82d56013Sjoerg //===----------------------------------------------------------------------===//
1087330f729Sjoerg
1097330f729Sjoerg #include "ARMFrameLowering.h"
1107330f729Sjoerg #include "ARMBaseInstrInfo.h"
1117330f729Sjoerg #include "ARMBaseRegisterInfo.h"
1127330f729Sjoerg #include "ARMConstantPoolValue.h"
1137330f729Sjoerg #include "ARMMachineFunctionInfo.h"
1147330f729Sjoerg #include "ARMSubtarget.h"
1157330f729Sjoerg #include "MCTargetDesc/ARMAddressingModes.h"
1167330f729Sjoerg #include "MCTargetDesc/ARMBaseInfo.h"
1177330f729Sjoerg #include "Utils/ARMBaseInfo.h"
1187330f729Sjoerg #include "llvm/ADT/BitVector.h"
1197330f729Sjoerg #include "llvm/ADT/STLExtras.h"
1207330f729Sjoerg #include "llvm/ADT/SmallPtrSet.h"
1217330f729Sjoerg #include "llvm/ADT/SmallVector.h"
1227330f729Sjoerg #include "llvm/CodeGen/MachineBasicBlock.h"
1237330f729Sjoerg #include "llvm/CodeGen/MachineConstantPool.h"
1247330f729Sjoerg #include "llvm/CodeGen/MachineFrameInfo.h"
1257330f729Sjoerg #include "llvm/CodeGen/MachineFunction.h"
1267330f729Sjoerg #include "llvm/CodeGen/MachineInstr.h"
1277330f729Sjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
1287330f729Sjoerg #include "llvm/CodeGen/MachineJumpTableInfo.h"
1297330f729Sjoerg #include "llvm/CodeGen/MachineModuleInfo.h"
1307330f729Sjoerg #include "llvm/CodeGen/MachineOperand.h"
1317330f729Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
1327330f729Sjoerg #include "llvm/CodeGen/RegisterScavenging.h"
1337330f729Sjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
1347330f729Sjoerg #include "llvm/CodeGen/TargetOpcodes.h"
1357330f729Sjoerg #include "llvm/CodeGen/TargetRegisterInfo.h"
1367330f729Sjoerg #include "llvm/CodeGen/TargetSubtargetInfo.h"
1377330f729Sjoerg #include "llvm/IR/Attributes.h"
1387330f729Sjoerg #include "llvm/IR/CallingConv.h"
1397330f729Sjoerg #include "llvm/IR/DebugLoc.h"
1407330f729Sjoerg #include "llvm/IR/Function.h"
1417330f729Sjoerg #include "llvm/MC/MCContext.h"
1427330f729Sjoerg #include "llvm/MC/MCDwarf.h"
1437330f729Sjoerg #include "llvm/MC/MCInstrDesc.h"
1447330f729Sjoerg #include "llvm/MC/MCRegisterInfo.h"
1457330f729Sjoerg #include "llvm/Support/CodeGen.h"
1467330f729Sjoerg #include "llvm/Support/CommandLine.h"
1477330f729Sjoerg #include "llvm/Support/Compiler.h"
1487330f729Sjoerg #include "llvm/Support/Debug.h"
1497330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
1507330f729Sjoerg #include "llvm/Support/MathExtras.h"
1517330f729Sjoerg #include "llvm/Support/raw_ostream.h"
1527330f729Sjoerg #include "llvm/Target/TargetMachine.h"
1537330f729Sjoerg #include "llvm/Target/TargetOptions.h"
1547330f729Sjoerg #include <algorithm>
1557330f729Sjoerg #include <cassert>
1567330f729Sjoerg #include <cstddef>
1577330f729Sjoerg #include <cstdint>
1587330f729Sjoerg #include <iterator>
1597330f729Sjoerg #include <utility>
1607330f729Sjoerg #include <vector>
1617330f729Sjoerg
1627330f729Sjoerg #define DEBUG_TYPE "arm-frame-lowering"
1637330f729Sjoerg
1647330f729Sjoerg using namespace llvm;
1657330f729Sjoerg
1667330f729Sjoerg static cl::opt<bool>
1677330f729Sjoerg SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
1687330f729Sjoerg cl::desc("Align ARM NEON spills in prolog and epilog"));
1697330f729Sjoerg
1707330f729Sjoerg static MachineBasicBlock::iterator
1717330f729Sjoerg skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1727330f729Sjoerg unsigned NumAlignedDPRCS2Regs);
1737330f729Sjoerg
ARMFrameLowering(const ARMSubtarget & sti)1747330f729Sjoerg ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
1757330f729Sjoerg : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, Align(4)),
1767330f729Sjoerg STI(sti) {}
1777330f729Sjoerg
keepFramePointer(const MachineFunction & MF) const1787330f729Sjoerg bool ARMFrameLowering::keepFramePointer(const MachineFunction &MF) const {
1797330f729Sjoerg // iOS always has a FP for backtracking, force other targets to keep their FP
1807330f729Sjoerg // when doing FastISel. The emitted code is currently superior, and in cases
1817330f729Sjoerg // like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
1827330f729Sjoerg return MF.getSubtarget<ARMSubtarget>().useFastISel();
1837330f729Sjoerg }
1847330f729Sjoerg
1857330f729Sjoerg /// Returns true if the target can safely skip saving callee-saved registers
1867330f729Sjoerg /// for noreturn nounwind functions.
enableCalleeSaveSkip(const MachineFunction & MF) const1877330f729Sjoerg bool ARMFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
1887330f729Sjoerg assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
1897330f729Sjoerg MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
1907330f729Sjoerg !MF.getFunction().hasFnAttribute(Attribute::UWTable));
1917330f729Sjoerg
1927330f729Sjoerg // Frame pointer and link register are not treated as normal CSR, thus we
1937330f729Sjoerg // can always skip CSR saves for nonreturning functions.
1947330f729Sjoerg return true;
1957330f729Sjoerg }
1967330f729Sjoerg
1977330f729Sjoerg /// hasFP - Return true if the specified function should have a dedicated frame
1987330f729Sjoerg /// pointer register. This is true if the function has variable sized allocas
1997330f729Sjoerg /// or if frame pointer elimination is disabled.
hasFP(const MachineFunction & MF) const2007330f729Sjoerg bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
2017330f729Sjoerg const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
2027330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
2037330f729Sjoerg
2047330f729Sjoerg // ABI-required frame pointer.
2057330f729Sjoerg if (MF.getTarget().Options.DisableFramePointerElim(MF))
2067330f729Sjoerg return true;
2077330f729Sjoerg
2087330f729Sjoerg // Frame pointer required for use within this function.
209*82d56013Sjoerg return (RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
2107330f729Sjoerg MFI.isFrameAddressTaken());
2117330f729Sjoerg }
2127330f729Sjoerg
2137330f729Sjoerg /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
2147330f729Sjoerg /// not required, we reserve argument space for call sites in the function
2157330f729Sjoerg /// immediately on entry to the current function. This eliminates the need for
2167330f729Sjoerg /// add/sub sp brackets around call sites. Returns true if the call frame is
2177330f729Sjoerg /// included as part of the stack frame.
hasReservedCallFrame(const MachineFunction & MF) const2187330f729Sjoerg bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
2197330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
2207330f729Sjoerg unsigned CFSize = MFI.getMaxCallFrameSize();
2217330f729Sjoerg // It's not always a good idea to include the call frame as part of the
2227330f729Sjoerg // stack frame. ARM (especially Thumb) has small immediate offset to
2237330f729Sjoerg // address the stack frame. So a large call frame can cause poor codegen
2247330f729Sjoerg // and may even makes it impossible to scavenge a register.
2257330f729Sjoerg if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
2267330f729Sjoerg return false;
2277330f729Sjoerg
2287330f729Sjoerg return !MFI.hasVarSizedObjects();
2297330f729Sjoerg }
2307330f729Sjoerg
2317330f729Sjoerg /// canSimplifyCallFramePseudos - If there is a reserved call frame, the
2327330f729Sjoerg /// call frame pseudos can be simplified. Unlike most targets, having a FP
2337330f729Sjoerg /// is not sufficient here since we still may reference some objects via SP
2347330f729Sjoerg /// even when FP is available in Thumb2 mode.
2357330f729Sjoerg bool
canSimplifyCallFramePseudos(const MachineFunction & MF) const2367330f729Sjoerg ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
2377330f729Sjoerg return hasReservedCallFrame(MF) || MF.getFrameInfo().hasVarSizedObjects();
2387330f729Sjoerg }
2397330f729Sjoerg
emitRegPlusImmediate(bool isARM,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const DebugLoc & dl,const ARMBaseInstrInfo & TII,unsigned DestReg,unsigned SrcReg,int NumBytes,unsigned MIFlags=MachineInstr::NoFlags,ARMCC::CondCodes Pred=ARMCC::AL,unsigned PredReg=0)2407330f729Sjoerg static void emitRegPlusImmediate(
2417330f729Sjoerg bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
2427330f729Sjoerg const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
2437330f729Sjoerg unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
2447330f729Sjoerg ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
2457330f729Sjoerg if (isARM)
2467330f729Sjoerg emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
2477330f729Sjoerg Pred, PredReg, TII, MIFlags);
2487330f729Sjoerg else
2497330f729Sjoerg emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
2507330f729Sjoerg Pred, PredReg, TII, MIFlags);
2517330f729Sjoerg }
2527330f729Sjoerg
emitSPUpdate(bool isARM,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const DebugLoc & dl,const ARMBaseInstrInfo & TII,int NumBytes,unsigned MIFlags=MachineInstr::NoFlags,ARMCC::CondCodes Pred=ARMCC::AL,unsigned PredReg=0)2537330f729Sjoerg static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
2547330f729Sjoerg MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
2557330f729Sjoerg const ARMBaseInstrInfo &TII, int NumBytes,
2567330f729Sjoerg unsigned MIFlags = MachineInstr::NoFlags,
2577330f729Sjoerg ARMCC::CondCodes Pred = ARMCC::AL,
2587330f729Sjoerg unsigned PredReg = 0) {
2597330f729Sjoerg emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
2607330f729Sjoerg MIFlags, Pred, PredReg);
2617330f729Sjoerg }
2627330f729Sjoerg
sizeOfSPAdjustment(const MachineInstr & MI)2637330f729Sjoerg static int sizeOfSPAdjustment(const MachineInstr &MI) {
2647330f729Sjoerg int RegSize;
2657330f729Sjoerg switch (MI.getOpcode()) {
2667330f729Sjoerg case ARM::VSTMDDB_UPD:
2677330f729Sjoerg RegSize = 8;
2687330f729Sjoerg break;
2697330f729Sjoerg case ARM::STMDB_UPD:
2707330f729Sjoerg case ARM::t2STMDB_UPD:
2717330f729Sjoerg RegSize = 4;
2727330f729Sjoerg break;
2737330f729Sjoerg case ARM::t2STR_PRE:
2747330f729Sjoerg case ARM::STR_PRE_IMM:
2757330f729Sjoerg return 4;
2767330f729Sjoerg default:
2777330f729Sjoerg llvm_unreachable("Unknown push or pop like instruction");
2787330f729Sjoerg }
2797330f729Sjoerg
2807330f729Sjoerg int count = 0;
2817330f729Sjoerg // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2827330f729Sjoerg // pred) so the list starts at 4.
2837330f729Sjoerg for (int i = MI.getNumOperands() - 1; i >= 4; --i)
2847330f729Sjoerg count += RegSize;
2857330f729Sjoerg return count;
2867330f729Sjoerg }
2877330f729Sjoerg
WindowsRequiresStackProbe(const MachineFunction & MF,size_t StackSizeInBytes)2887330f729Sjoerg static bool WindowsRequiresStackProbe(const MachineFunction &MF,
2897330f729Sjoerg size_t StackSizeInBytes) {
2907330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
2917330f729Sjoerg const Function &F = MF.getFunction();
2927330f729Sjoerg unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
2937330f729Sjoerg if (F.hasFnAttribute("stack-probe-size"))
2947330f729Sjoerg F.getFnAttribute("stack-probe-size")
2957330f729Sjoerg .getValueAsString()
2967330f729Sjoerg .getAsInteger(0, StackProbeSize);
2977330f729Sjoerg return (StackSizeInBytes >= StackProbeSize) &&
2987330f729Sjoerg !F.hasFnAttribute("no-stack-arg-probe");
2997330f729Sjoerg }
3007330f729Sjoerg
3017330f729Sjoerg namespace {
3027330f729Sjoerg
3037330f729Sjoerg struct StackAdjustingInsts {
3047330f729Sjoerg struct InstInfo {
3057330f729Sjoerg MachineBasicBlock::iterator I;
3067330f729Sjoerg unsigned SPAdjust;
3077330f729Sjoerg bool BeforeFPSet;
3087330f729Sjoerg };
3097330f729Sjoerg
3107330f729Sjoerg SmallVector<InstInfo, 4> Insts;
3117330f729Sjoerg
addInst__anonc5e3ca320111::StackAdjustingInsts3127330f729Sjoerg void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
3137330f729Sjoerg bool BeforeFPSet = false) {
3147330f729Sjoerg InstInfo Info = {I, SPAdjust, BeforeFPSet};
3157330f729Sjoerg Insts.push_back(Info);
3167330f729Sjoerg }
3177330f729Sjoerg
addExtraBytes__anonc5e3ca320111::StackAdjustingInsts3187330f729Sjoerg void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
3197330f729Sjoerg auto Info =
3207330f729Sjoerg llvm::find_if(Insts, [&](InstInfo &Info) { return Info.I == I; });
3217330f729Sjoerg assert(Info != Insts.end() && "invalid sp adjusting instruction");
3227330f729Sjoerg Info->SPAdjust += ExtraBytes;
3237330f729Sjoerg }
3247330f729Sjoerg
emitDefCFAOffsets__anonc5e3ca320111::StackAdjustingInsts3257330f729Sjoerg void emitDefCFAOffsets(MachineBasicBlock &MBB, const DebugLoc &dl,
3267330f729Sjoerg const ARMBaseInstrInfo &TII, bool HasFP) {
3277330f729Sjoerg MachineFunction &MF = *MBB.getParent();
3287330f729Sjoerg unsigned CFAOffset = 0;
3297330f729Sjoerg for (auto &Info : Insts) {
3307330f729Sjoerg if (HasFP && !Info.BeforeFPSet)
3317330f729Sjoerg return;
3327330f729Sjoerg
333*82d56013Sjoerg CFAOffset += Info.SPAdjust;
3347330f729Sjoerg unsigned CFIIndex = MF.addFrameInst(
335*82d56013Sjoerg MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset));
3367330f729Sjoerg BuildMI(MBB, std::next(Info.I), dl,
3377330f729Sjoerg TII.get(TargetOpcode::CFI_INSTRUCTION))
3387330f729Sjoerg .addCFIIndex(CFIIndex)
3397330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
3407330f729Sjoerg }
3417330f729Sjoerg }
3427330f729Sjoerg };
3437330f729Sjoerg
3447330f729Sjoerg } // end anonymous namespace
3457330f729Sjoerg
3467330f729Sjoerg /// Emit an instruction sequence that will align the address in
3477330f729Sjoerg /// register Reg by zero-ing out the lower bits. For versions of the
3487330f729Sjoerg /// architecture that support Neon, this must be done in a single
3497330f729Sjoerg /// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
3507330f729Sjoerg /// single instruction. That function only gets called when optimizing
3517330f729Sjoerg /// spilling of D registers on a core with the Neon instruction set
3527330f729Sjoerg /// present.
emitAligningInstructions(MachineFunction & MF,ARMFunctionInfo * AFI,const TargetInstrInfo & TII,MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,const unsigned Reg,const Align Alignment,const bool MustBeSingleInstruction)3537330f729Sjoerg static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
3547330f729Sjoerg const TargetInstrInfo &TII,
3557330f729Sjoerg MachineBasicBlock &MBB,
3567330f729Sjoerg MachineBasicBlock::iterator MBBI,
3577330f729Sjoerg const DebugLoc &DL, const unsigned Reg,
358*82d56013Sjoerg const Align Alignment,
3597330f729Sjoerg const bool MustBeSingleInstruction) {
3607330f729Sjoerg const ARMSubtarget &AST =
3617330f729Sjoerg static_cast<const ARMSubtarget &>(MF.getSubtarget());
3627330f729Sjoerg const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
363*82d56013Sjoerg const unsigned AlignMask = Alignment.value() - 1U;
364*82d56013Sjoerg const unsigned NrBitsToZero = Log2(Alignment);
3657330f729Sjoerg assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
3667330f729Sjoerg if (!AFI->isThumbFunction()) {
3677330f729Sjoerg // if the BFC instruction is available, use that to zero the lower
3687330f729Sjoerg // bits:
3697330f729Sjoerg // bfc Reg, #0, log2(Alignment)
3707330f729Sjoerg // otherwise use BIC, if the mask to zero the required number of bits
3717330f729Sjoerg // can be encoded in the bic immediate field
3727330f729Sjoerg // bic Reg, Reg, Alignment-1
3737330f729Sjoerg // otherwise, emit
3747330f729Sjoerg // lsr Reg, Reg, log2(Alignment)
3757330f729Sjoerg // lsl Reg, Reg, log2(Alignment)
3767330f729Sjoerg if (CanUseBFC) {
3777330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
3787330f729Sjoerg .addReg(Reg, RegState::Kill)
3797330f729Sjoerg .addImm(~AlignMask)
3807330f729Sjoerg .add(predOps(ARMCC::AL));
3817330f729Sjoerg } else if (AlignMask <= 255) {
3827330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
3837330f729Sjoerg .addReg(Reg, RegState::Kill)
3847330f729Sjoerg .addImm(AlignMask)
3857330f729Sjoerg .add(predOps(ARMCC::AL))
3867330f729Sjoerg .add(condCodeOp());
3877330f729Sjoerg } else {
3887330f729Sjoerg assert(!MustBeSingleInstruction &&
3897330f729Sjoerg "Shouldn't call emitAligningInstructions demanding a single "
3907330f729Sjoerg "instruction to be emitted for large stack alignment for a target "
3917330f729Sjoerg "without BFC.");
3927330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
3937330f729Sjoerg .addReg(Reg, RegState::Kill)
3947330f729Sjoerg .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))
3957330f729Sjoerg .add(predOps(ARMCC::AL))
3967330f729Sjoerg .add(condCodeOp());
3977330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
3987330f729Sjoerg .addReg(Reg, RegState::Kill)
3997330f729Sjoerg .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))
4007330f729Sjoerg .add(predOps(ARMCC::AL))
4017330f729Sjoerg .add(condCodeOp());
4027330f729Sjoerg }
4037330f729Sjoerg } else {
4047330f729Sjoerg // Since this is only reached for Thumb-2 targets, the BFC instruction
4057330f729Sjoerg // should always be available.
4067330f729Sjoerg assert(CanUseBFC);
4077330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
4087330f729Sjoerg .addReg(Reg, RegState::Kill)
4097330f729Sjoerg .addImm(~AlignMask)
4107330f729Sjoerg .add(predOps(ARMCC::AL));
4117330f729Sjoerg }
4127330f729Sjoerg }
4137330f729Sjoerg
4147330f729Sjoerg /// We need the offset of the frame pointer relative to other MachineFrameInfo
4157330f729Sjoerg /// offsets which are encoded relative to SP at function begin.
4167330f729Sjoerg /// See also emitPrologue() for how the FP is set up.
4177330f729Sjoerg /// Unfortunately we cannot determine this value in determineCalleeSaves() yet
4187330f729Sjoerg /// as assignCalleeSavedSpillSlots() hasn't run at this point. Instead we use
4197330f729Sjoerg /// this to produce a conservative estimate that we check in an assert() later.
getMaxFPOffset(const ARMSubtarget & STI,const ARMFunctionInfo & AFI)420*82d56013Sjoerg static int getMaxFPOffset(const ARMSubtarget &STI, const ARMFunctionInfo &AFI) {
4217330f729Sjoerg // For Thumb1, push.w isn't available, so the first push will always push
4227330f729Sjoerg // r7 and lr onto the stack first.
4237330f729Sjoerg if (AFI.isThumb1OnlyFunction())
4247330f729Sjoerg return -AFI.getArgRegsSaveSize() - (2 * 4);
4257330f729Sjoerg // This is a conservative estimation: Assume the frame pointer being r7 and
4267330f729Sjoerg // pc("r15") up to r8 getting spilled before (= 8 registers).
427*82d56013Sjoerg int FPCXTSaveSize = (STI.hasV8_1MMainlineOps() && AFI.isCmseNSEntryFunction()) ? 4 : 0;
428*82d56013Sjoerg return - FPCXTSaveSize - AFI.getArgRegsSaveSize() - (8 * 4);
4297330f729Sjoerg }
4307330f729Sjoerg
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const4317330f729Sjoerg void ARMFrameLowering::emitPrologue(MachineFunction &MF,
4327330f729Sjoerg MachineBasicBlock &MBB) const {
4337330f729Sjoerg MachineBasicBlock::iterator MBBI = MBB.begin();
4347330f729Sjoerg MachineFrameInfo &MFI = MF.getFrameInfo();
4357330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
4367330f729Sjoerg MachineModuleInfo &MMI = MF.getMMI();
4377330f729Sjoerg MCContext &Context = MMI.getContext();
4387330f729Sjoerg const TargetMachine &TM = MF.getTarget();
4397330f729Sjoerg const MCRegisterInfo *MRI = Context.getRegisterInfo();
4407330f729Sjoerg const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
4417330f729Sjoerg const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
4427330f729Sjoerg assert(!AFI->isThumb1OnlyFunction() &&
4437330f729Sjoerg "This emitPrologue does not support Thumb1!");
4447330f729Sjoerg bool isARM = !AFI->isThumbFunction();
445*82d56013Sjoerg Align Alignment = STI.getFrameLowering()->getStackAlign();
4467330f729Sjoerg unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
4477330f729Sjoerg unsigned NumBytes = MFI.getStackSize();
4487330f729Sjoerg const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
449*82d56013Sjoerg int FPCXTSaveSize = 0;
4507330f729Sjoerg
4517330f729Sjoerg // Debug location must be unknown since the first debug location is used
4527330f729Sjoerg // to determine the end of the prologue.
4537330f729Sjoerg DebugLoc dl;
4547330f729Sjoerg
4557330f729Sjoerg Register FramePtr = RegInfo->getFrameRegister(MF);
4567330f729Sjoerg
4577330f729Sjoerg // Determine the sizes of each callee-save spill areas and record which frame
4587330f729Sjoerg // belongs to which callee-save spill areas.
4597330f729Sjoerg unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
4607330f729Sjoerg int FramePtrSpillFI = 0;
4617330f729Sjoerg int D8SpillFI = 0;
4627330f729Sjoerg
4637330f729Sjoerg // All calls are tail calls in GHC calling conv, and functions have no
4647330f729Sjoerg // prologue/epilogue.
4657330f729Sjoerg if (MF.getFunction().getCallingConv() == CallingConv::GHC)
4667330f729Sjoerg return;
4677330f729Sjoerg
4687330f729Sjoerg StackAdjustingInsts DefCFAOffsetCandidates;
4697330f729Sjoerg bool HasFP = hasFP(MF);
4707330f729Sjoerg
4717330f729Sjoerg // Allocate the vararg register save area.
4727330f729Sjoerg if (ArgRegsSaveSize) {
4737330f729Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
4747330f729Sjoerg MachineInstr::FrameSetup);
4757330f729Sjoerg DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
4767330f729Sjoerg }
4777330f729Sjoerg
4787330f729Sjoerg if (!AFI->hasStackFrame() &&
4797330f729Sjoerg (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
4807330f729Sjoerg if (NumBytes - ArgRegsSaveSize != 0) {
4817330f729Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
4827330f729Sjoerg MachineInstr::FrameSetup);
4837330f729Sjoerg DefCFAOffsetCandidates.addInst(std::prev(MBBI),
4847330f729Sjoerg NumBytes - ArgRegsSaveSize, true);
4857330f729Sjoerg }
4867330f729Sjoerg DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
4877330f729Sjoerg return;
4887330f729Sjoerg }
4897330f729Sjoerg
4907330f729Sjoerg // Determine spill area sizes.
4917330f729Sjoerg for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
4927330f729Sjoerg unsigned Reg = CSI[i].getReg();
4937330f729Sjoerg int FI = CSI[i].getFrameIdx();
4947330f729Sjoerg switch (Reg) {
4957330f729Sjoerg case ARM::R8:
4967330f729Sjoerg case ARM::R9:
4977330f729Sjoerg case ARM::R10:
4987330f729Sjoerg case ARM::R11:
4997330f729Sjoerg case ARM::R12:
5007330f729Sjoerg if (STI.splitFramePushPop(MF)) {
5017330f729Sjoerg GPRCS2Size += 4;
5027330f729Sjoerg break;
5037330f729Sjoerg }
5047330f729Sjoerg LLVM_FALLTHROUGH;
5057330f729Sjoerg case ARM::R0:
5067330f729Sjoerg case ARM::R1:
5077330f729Sjoerg case ARM::R2:
5087330f729Sjoerg case ARM::R3:
5097330f729Sjoerg case ARM::R4:
5107330f729Sjoerg case ARM::R5:
5117330f729Sjoerg case ARM::R6:
5127330f729Sjoerg case ARM::R7:
5137330f729Sjoerg case ARM::LR:
5147330f729Sjoerg if (Reg == FramePtr)
5157330f729Sjoerg FramePtrSpillFI = FI;
5167330f729Sjoerg GPRCS1Size += 4;
5177330f729Sjoerg break;
518*82d56013Sjoerg case ARM::FPCXTNS:
519*82d56013Sjoerg FPCXTSaveSize = 4;
520*82d56013Sjoerg break;
5217330f729Sjoerg default:
5227330f729Sjoerg // This is a DPR. Exclude the aligned DPRCS2 spills.
5237330f729Sjoerg if (Reg == ARM::D8)
5247330f729Sjoerg D8SpillFI = FI;
5257330f729Sjoerg if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
5267330f729Sjoerg DPRCSSize += 8;
5277330f729Sjoerg }
5287330f729Sjoerg }
5297330f729Sjoerg
530*82d56013Sjoerg // Move past FPCXT area.
5317330f729Sjoerg MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
532*82d56013Sjoerg if (FPCXTSaveSize > 0) {
533*82d56013Sjoerg LastPush = MBBI++;
534*82d56013Sjoerg DefCFAOffsetCandidates.addInst(LastPush, FPCXTSaveSize, true);
535*82d56013Sjoerg }
536*82d56013Sjoerg
537*82d56013Sjoerg // Move past area 1.
5387330f729Sjoerg if (GPRCS1Size > 0) {
5397330f729Sjoerg GPRCS1Push = LastPush = MBBI++;
5407330f729Sjoerg DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
5417330f729Sjoerg }
5427330f729Sjoerg
5437330f729Sjoerg // Determine starting offsets of spill areas.
544*82d56013Sjoerg unsigned FPCXTOffset = NumBytes - ArgRegsSaveSize - FPCXTSaveSize;
545*82d56013Sjoerg unsigned GPRCS1Offset = FPCXTOffset - GPRCS1Size;
5467330f729Sjoerg unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
547*82d56013Sjoerg Align DPRAlign = DPRCSSize ? std::min(Align(8), Alignment) : Align(4);
548*82d56013Sjoerg unsigned DPRGapSize =
549*82d56013Sjoerg (GPRCS1Size + GPRCS2Size + FPCXTSaveSize + ArgRegsSaveSize) %
550*82d56013Sjoerg DPRAlign.value();
551*82d56013Sjoerg
5527330f729Sjoerg unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
5537330f729Sjoerg int FramePtrOffsetInPush = 0;
5547330f729Sjoerg if (HasFP) {
5557330f729Sjoerg int FPOffset = MFI.getObjectOffset(FramePtrSpillFI);
556*82d56013Sjoerg assert(getMaxFPOffset(STI, *AFI) <= FPOffset &&
5577330f729Sjoerg "Max FP estimation is wrong");
558*82d56013Sjoerg FramePtrOffsetInPush = FPOffset + ArgRegsSaveSize + FPCXTSaveSize;
5597330f729Sjoerg AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
5607330f729Sjoerg NumBytes);
5617330f729Sjoerg }
5627330f729Sjoerg AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
5637330f729Sjoerg AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
5647330f729Sjoerg AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
5657330f729Sjoerg
5667330f729Sjoerg // Move past area 2.
5677330f729Sjoerg if (GPRCS2Size > 0) {
5687330f729Sjoerg GPRCS2Push = LastPush = MBBI++;
5697330f729Sjoerg DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
5707330f729Sjoerg }
5717330f729Sjoerg
5727330f729Sjoerg // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
5737330f729Sjoerg // .cfi_offset operations will reflect that.
5747330f729Sjoerg if (DPRGapSize) {
5757330f729Sjoerg assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
5767330f729Sjoerg if (LastPush != MBB.end() &&
5777330f729Sjoerg tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, DPRGapSize))
5787330f729Sjoerg DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
5797330f729Sjoerg else {
5807330f729Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
5817330f729Sjoerg MachineInstr::FrameSetup);
5827330f729Sjoerg DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
5837330f729Sjoerg }
5847330f729Sjoerg }
5857330f729Sjoerg
5867330f729Sjoerg // Move past area 3.
5877330f729Sjoerg if (DPRCSSize > 0) {
5887330f729Sjoerg // Since vpush register list cannot have gaps, there may be multiple vpush
5897330f729Sjoerg // instructions in the prologue.
5907330f729Sjoerg while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
5917330f729Sjoerg DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(*MBBI));
5927330f729Sjoerg LastPush = MBBI++;
5937330f729Sjoerg }
5947330f729Sjoerg }
5957330f729Sjoerg
5967330f729Sjoerg // Move past the aligned DPRCS2 area.
5977330f729Sjoerg if (AFI->getNumAlignedDPRCS2Regs() > 0) {
5987330f729Sjoerg MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
5997330f729Sjoerg // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
6007330f729Sjoerg // leaves the stack pointer pointing to the DPRCS2 area.
6017330f729Sjoerg //
6027330f729Sjoerg // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
6037330f729Sjoerg NumBytes += MFI.getObjectOffset(D8SpillFI);
6047330f729Sjoerg } else
6057330f729Sjoerg NumBytes = DPRCSOffset;
6067330f729Sjoerg
6077330f729Sjoerg if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
6087330f729Sjoerg uint32_t NumWords = NumBytes >> 2;
6097330f729Sjoerg
6107330f729Sjoerg if (NumWords < 65536)
6117330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
6127330f729Sjoerg .addImm(NumWords)
6137330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup)
6147330f729Sjoerg .add(predOps(ARMCC::AL));
6157330f729Sjoerg else
6167330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
6177330f729Sjoerg .addImm(NumWords)
6187330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
6197330f729Sjoerg
6207330f729Sjoerg switch (TM.getCodeModel()) {
6217330f729Sjoerg case CodeModel::Tiny:
6227330f729Sjoerg llvm_unreachable("Tiny code model not available on ARM.");
6237330f729Sjoerg case CodeModel::Small:
6247330f729Sjoerg case CodeModel::Medium:
6257330f729Sjoerg case CodeModel::Kernel:
6267330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
6277330f729Sjoerg .add(predOps(ARMCC::AL))
6287330f729Sjoerg .addExternalSymbol("__chkstk")
6297330f729Sjoerg .addReg(ARM::R4, RegState::Implicit)
6307330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
6317330f729Sjoerg break;
6327330f729Sjoerg case CodeModel::Large:
6337330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
6347330f729Sjoerg .addExternalSymbol("__chkstk")
6357330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
6367330f729Sjoerg
6377330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
6387330f729Sjoerg .add(predOps(ARMCC::AL))
6397330f729Sjoerg .addReg(ARM::R12, RegState::Kill)
6407330f729Sjoerg .addReg(ARM::R4, RegState::Implicit)
6417330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
6427330f729Sjoerg break;
6437330f729Sjoerg }
6447330f729Sjoerg
6457330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), ARM::SP)
6467330f729Sjoerg .addReg(ARM::SP, RegState::Kill)
6477330f729Sjoerg .addReg(ARM::R4, RegState::Kill)
6487330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup)
6497330f729Sjoerg .add(predOps(ARMCC::AL))
6507330f729Sjoerg .add(condCodeOp());
6517330f729Sjoerg NumBytes = 0;
6527330f729Sjoerg }
6537330f729Sjoerg
6547330f729Sjoerg if (NumBytes) {
6557330f729Sjoerg // Adjust SP after all the callee-save spills.
6567330f729Sjoerg if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
6577330f729Sjoerg tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, NumBytes))
6587330f729Sjoerg DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
6597330f729Sjoerg else {
6607330f729Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
6617330f729Sjoerg MachineInstr::FrameSetup);
6627330f729Sjoerg DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
6637330f729Sjoerg }
6647330f729Sjoerg
6657330f729Sjoerg if (HasFP && isARM)
6667330f729Sjoerg // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
6677330f729Sjoerg // Note it's not safe to do this in Thumb2 mode because it would have
6687330f729Sjoerg // taken two instructions:
6697330f729Sjoerg // mov sp, r7
6707330f729Sjoerg // sub sp, #24
6717330f729Sjoerg // If an interrupt is taken between the two instructions, then sp is in
6727330f729Sjoerg // an inconsistent state (pointing to the middle of callee-saved area).
6737330f729Sjoerg // The interrupt handler can end up clobbering the registers.
6747330f729Sjoerg AFI->setShouldRestoreSPFromFP(true);
6757330f729Sjoerg }
6767330f729Sjoerg
6777330f729Sjoerg // Set FP to point to the stack slot that contains the previous FP.
6787330f729Sjoerg // For iOS, FP is R7, which has now been stored in spill area 1.
6797330f729Sjoerg // Otherwise, if this is not iOS, all the callee-saved registers go
6807330f729Sjoerg // into spill area 1, including the FP in R11. In either case, it
6817330f729Sjoerg // is in area one and the adjustment needs to take place just after
6827330f729Sjoerg // that push.
6837330f729Sjoerg if (HasFP) {
6847330f729Sjoerg MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
6857330f729Sjoerg unsigned PushSize = sizeOfSPAdjustment(*GPRCS1Push);
6867330f729Sjoerg emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
6877330f729Sjoerg dl, TII, FramePtr, ARM::SP,
6887330f729Sjoerg PushSize + FramePtrOffsetInPush,
6897330f729Sjoerg MachineInstr::FrameSetup);
6907330f729Sjoerg if (FramePtrOffsetInPush + PushSize != 0) {
691*82d56013Sjoerg unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
6927330f729Sjoerg nullptr, MRI->getDwarfRegNum(FramePtr, true),
693*82d56013Sjoerg FPCXTSaveSize + ArgRegsSaveSize - FramePtrOffsetInPush));
6947330f729Sjoerg BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
6957330f729Sjoerg .addCFIIndex(CFIIndex)
6967330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
6977330f729Sjoerg } else {
6987330f729Sjoerg unsigned CFIIndex =
6997330f729Sjoerg MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
7007330f729Sjoerg nullptr, MRI->getDwarfRegNum(FramePtr, true)));
7017330f729Sjoerg BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
7027330f729Sjoerg .addCFIIndex(CFIIndex)
7037330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
7047330f729Sjoerg }
7057330f729Sjoerg }
7067330f729Sjoerg
7077330f729Sjoerg // Now that the prologue's actual instructions are finalised, we can insert
7087330f729Sjoerg // the necessary DWARF cf instructions to describe the situation. Start by
7097330f729Sjoerg // recording where each register ended up:
7107330f729Sjoerg if (GPRCS1Size > 0) {
7117330f729Sjoerg MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
7127330f729Sjoerg int CFIIndex;
7137330f729Sjoerg for (const auto &Entry : CSI) {
7147330f729Sjoerg unsigned Reg = Entry.getReg();
7157330f729Sjoerg int FI = Entry.getFrameIdx();
7167330f729Sjoerg switch (Reg) {
7177330f729Sjoerg case ARM::R8:
7187330f729Sjoerg case ARM::R9:
7197330f729Sjoerg case ARM::R10:
7207330f729Sjoerg case ARM::R11:
7217330f729Sjoerg case ARM::R12:
7227330f729Sjoerg if (STI.splitFramePushPop(MF))
7237330f729Sjoerg break;
7247330f729Sjoerg LLVM_FALLTHROUGH;
7257330f729Sjoerg case ARM::R0:
7267330f729Sjoerg case ARM::R1:
7277330f729Sjoerg case ARM::R2:
7287330f729Sjoerg case ARM::R3:
7297330f729Sjoerg case ARM::R4:
7307330f729Sjoerg case ARM::R5:
7317330f729Sjoerg case ARM::R6:
7327330f729Sjoerg case ARM::R7:
7337330f729Sjoerg case ARM::LR:
7347330f729Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
7357330f729Sjoerg nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
7367330f729Sjoerg BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
7377330f729Sjoerg .addCFIIndex(CFIIndex)
7387330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
7397330f729Sjoerg break;
7407330f729Sjoerg }
7417330f729Sjoerg }
7427330f729Sjoerg }
7437330f729Sjoerg
7447330f729Sjoerg if (GPRCS2Size > 0) {
7457330f729Sjoerg MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
7467330f729Sjoerg for (const auto &Entry : CSI) {
7477330f729Sjoerg unsigned Reg = Entry.getReg();
7487330f729Sjoerg int FI = Entry.getFrameIdx();
7497330f729Sjoerg switch (Reg) {
7507330f729Sjoerg case ARM::R8:
7517330f729Sjoerg case ARM::R9:
7527330f729Sjoerg case ARM::R10:
7537330f729Sjoerg case ARM::R11:
7547330f729Sjoerg case ARM::R12:
7557330f729Sjoerg if (STI.splitFramePushPop(MF)) {
7567330f729Sjoerg unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
7577330f729Sjoerg unsigned Offset = MFI.getObjectOffset(FI);
7587330f729Sjoerg unsigned CFIIndex = MF.addFrameInst(
7597330f729Sjoerg MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
7607330f729Sjoerg BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
7617330f729Sjoerg .addCFIIndex(CFIIndex)
7627330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
7637330f729Sjoerg }
7647330f729Sjoerg break;
7657330f729Sjoerg }
7667330f729Sjoerg }
7677330f729Sjoerg }
7687330f729Sjoerg
7697330f729Sjoerg if (DPRCSSize > 0) {
7707330f729Sjoerg // Since vpush register list cannot have gaps, there may be multiple vpush
7717330f729Sjoerg // instructions in the prologue.
7727330f729Sjoerg MachineBasicBlock::iterator Pos = std::next(LastPush);
7737330f729Sjoerg for (const auto &Entry : CSI) {
7747330f729Sjoerg unsigned Reg = Entry.getReg();
7757330f729Sjoerg int FI = Entry.getFrameIdx();
7767330f729Sjoerg if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
7777330f729Sjoerg (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
7787330f729Sjoerg unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
7797330f729Sjoerg unsigned Offset = MFI.getObjectOffset(FI);
7807330f729Sjoerg unsigned CFIIndex = MF.addFrameInst(
7817330f729Sjoerg MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
7827330f729Sjoerg BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
7837330f729Sjoerg .addCFIIndex(CFIIndex)
7847330f729Sjoerg .setMIFlags(MachineInstr::FrameSetup);
7857330f729Sjoerg }
7867330f729Sjoerg }
7877330f729Sjoerg }
7887330f729Sjoerg
7897330f729Sjoerg // Now we can emit descriptions of where the canonical frame address was
7907330f729Sjoerg // throughout the process. If we have a frame pointer, it takes over the job
7917330f729Sjoerg // half-way through, so only the first few .cfi_def_cfa_offset instructions
7927330f729Sjoerg // actually get emitted.
7937330f729Sjoerg DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
7947330f729Sjoerg
7957330f729Sjoerg if (STI.isTargetELF() && hasFP(MF))
7967330f729Sjoerg MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
7977330f729Sjoerg AFI->getFramePtrSpillOffset());
7987330f729Sjoerg
799*82d56013Sjoerg AFI->setFPCXTSaveAreaSize(FPCXTSaveSize);
8007330f729Sjoerg AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
8017330f729Sjoerg AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
8027330f729Sjoerg AFI->setDPRCalleeSavedGapSize(DPRGapSize);
8037330f729Sjoerg AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
8047330f729Sjoerg
8057330f729Sjoerg // If we need dynamic stack realignment, do it here. Be paranoid and make
8067330f729Sjoerg // sure if we also have VLAs, we have a base pointer for frame access.
8077330f729Sjoerg // If aligned NEON registers were spilled, the stack has already been
8087330f729Sjoerg // realigned.
809*82d56013Sjoerg if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->hasStackRealignment(MF)) {
810*82d56013Sjoerg Align MaxAlign = MFI.getMaxAlign();
8117330f729Sjoerg assert(!AFI->isThumb1OnlyFunction());
8127330f729Sjoerg if (!AFI->isThumbFunction()) {
8137330f729Sjoerg emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
8147330f729Sjoerg false);
8157330f729Sjoerg } else {
8167330f729Sjoerg // We cannot use sp as source/dest register here, thus we're using r4 to
8177330f729Sjoerg // perform the calculations. We're emitting the following sequence:
8187330f729Sjoerg // mov r4, sp
8197330f729Sjoerg // -- use emitAligningInstructions to produce best sequence to zero
8207330f729Sjoerg // -- out lower bits in r4
8217330f729Sjoerg // mov sp, r4
8227330f729Sjoerg // FIXME: It will be better just to find spare register here.
8237330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
8247330f729Sjoerg .addReg(ARM::SP, RegState::Kill)
8257330f729Sjoerg .add(predOps(ARMCC::AL));
8267330f729Sjoerg emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
8277330f729Sjoerg false);
8287330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
8297330f729Sjoerg .addReg(ARM::R4, RegState::Kill)
8307330f729Sjoerg .add(predOps(ARMCC::AL));
8317330f729Sjoerg }
8327330f729Sjoerg
8337330f729Sjoerg AFI->setShouldRestoreSPFromFP(true);
8347330f729Sjoerg }
8357330f729Sjoerg
8367330f729Sjoerg // If we need a base pointer, set it up here. It's whatever the value
8377330f729Sjoerg // of the stack pointer is at this point. Any variable size objects
8387330f729Sjoerg // will be allocated after this, so we can still use the base pointer
8397330f729Sjoerg // to reference locals.
8407330f729Sjoerg // FIXME: Clarify FrameSetup flags here.
8417330f729Sjoerg if (RegInfo->hasBasePointer(MF)) {
8427330f729Sjoerg if (isARM)
8437330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), RegInfo->getBaseRegister())
8447330f729Sjoerg .addReg(ARM::SP)
8457330f729Sjoerg .add(predOps(ARMCC::AL))
8467330f729Sjoerg .add(condCodeOp());
8477330f729Sjoerg else
8487330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), RegInfo->getBaseRegister())
8497330f729Sjoerg .addReg(ARM::SP)
8507330f729Sjoerg .add(predOps(ARMCC::AL));
8517330f729Sjoerg }
8527330f729Sjoerg
8537330f729Sjoerg // If the frame has variable sized objects then the epilogue must restore
8547330f729Sjoerg // the sp from fp. We can assume there's an FP here since hasFP already
8557330f729Sjoerg // checks for hasVarSizedObjects.
8567330f729Sjoerg if (MFI.hasVarSizedObjects())
8577330f729Sjoerg AFI->setShouldRestoreSPFromFP(true);
8587330f729Sjoerg }
8597330f729Sjoerg
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const8607330f729Sjoerg void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
8617330f729Sjoerg MachineBasicBlock &MBB) const {
8627330f729Sjoerg MachineFrameInfo &MFI = MF.getFrameInfo();
8637330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
8647330f729Sjoerg const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
8657330f729Sjoerg const ARMBaseInstrInfo &TII =
8667330f729Sjoerg *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
8677330f729Sjoerg assert(!AFI->isThumb1OnlyFunction() &&
8687330f729Sjoerg "This emitEpilogue does not support Thumb1!");
8697330f729Sjoerg bool isARM = !AFI->isThumbFunction();
8707330f729Sjoerg
8717330f729Sjoerg unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
8727330f729Sjoerg int NumBytes = (int)MFI.getStackSize();
8737330f729Sjoerg Register FramePtr = RegInfo->getFrameRegister(MF);
8747330f729Sjoerg
8757330f729Sjoerg // All calls are tail calls in GHC calling conv, and functions have no
8767330f729Sjoerg // prologue/epilogue.
8777330f729Sjoerg if (MF.getFunction().getCallingConv() == CallingConv::GHC)
8787330f729Sjoerg return;
8797330f729Sjoerg
8807330f729Sjoerg // First put ourselves on the first (from top) terminator instructions.
8817330f729Sjoerg MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
8827330f729Sjoerg DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
8837330f729Sjoerg
8847330f729Sjoerg if (!AFI->hasStackFrame()) {
8857330f729Sjoerg if (NumBytes - ArgRegsSaveSize != 0)
886*82d56013Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize,
887*82d56013Sjoerg MachineInstr::FrameDestroy);
8887330f729Sjoerg } else {
8897330f729Sjoerg // Unwind MBBI to point to first LDR / VLDRD.
8907330f729Sjoerg if (MBBI != MBB.begin()) {
8917330f729Sjoerg do {
8927330f729Sjoerg --MBBI;
893*82d56013Sjoerg } while (MBBI != MBB.begin() &&
894*82d56013Sjoerg MBBI->getFlag(MachineInstr::FrameDestroy));
895*82d56013Sjoerg if (!MBBI->getFlag(MachineInstr::FrameDestroy))
8967330f729Sjoerg ++MBBI;
8977330f729Sjoerg }
8987330f729Sjoerg
8997330f729Sjoerg // Move SP to start of FP callee save spill area.
9007330f729Sjoerg NumBytes -= (ArgRegsSaveSize +
901*82d56013Sjoerg AFI->getFPCXTSaveAreaSize() +
9027330f729Sjoerg AFI->getGPRCalleeSavedArea1Size() +
9037330f729Sjoerg AFI->getGPRCalleeSavedArea2Size() +
9047330f729Sjoerg AFI->getDPRCalleeSavedGapSize() +
9057330f729Sjoerg AFI->getDPRCalleeSavedAreaSize());
9067330f729Sjoerg
9077330f729Sjoerg // Reset SP based on frame pointer only if the stack frame extends beyond
9087330f729Sjoerg // frame pointer stack slot or target is ELF and the function has FP.
9097330f729Sjoerg if (AFI->shouldRestoreSPFromFP()) {
9107330f729Sjoerg NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
9117330f729Sjoerg if (NumBytes) {
9127330f729Sjoerg if (isARM)
9137330f729Sjoerg emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
914*82d56013Sjoerg ARMCC::AL, 0, TII,
915*82d56013Sjoerg MachineInstr::FrameDestroy);
9167330f729Sjoerg else {
9177330f729Sjoerg // It's not possible to restore SP from FP in a single instruction.
9187330f729Sjoerg // For iOS, this looks like:
9197330f729Sjoerg // mov sp, r7
9207330f729Sjoerg // sub sp, #24
9217330f729Sjoerg // This is bad, if an interrupt is taken after the mov, sp is in an
9227330f729Sjoerg // inconsistent state.
9237330f729Sjoerg // Use the first callee-saved register as a scratch register.
9247330f729Sjoerg assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
9257330f729Sjoerg "No scratch register to restore SP from FP!");
9267330f729Sjoerg emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
927*82d56013Sjoerg ARMCC::AL, 0, TII, MachineInstr::FrameDestroy);
9287330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
9297330f729Sjoerg .addReg(ARM::R4)
930*82d56013Sjoerg .add(predOps(ARMCC::AL))
931*82d56013Sjoerg .setMIFlag(MachineInstr::FrameDestroy);
9327330f729Sjoerg }
9337330f729Sjoerg } else {
9347330f729Sjoerg // Thumb2 or ARM.
9357330f729Sjoerg if (isARM)
9367330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
9377330f729Sjoerg .addReg(FramePtr)
9387330f729Sjoerg .add(predOps(ARMCC::AL))
939*82d56013Sjoerg .add(condCodeOp())
940*82d56013Sjoerg .setMIFlag(MachineInstr::FrameDestroy);
9417330f729Sjoerg else
9427330f729Sjoerg BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
9437330f729Sjoerg .addReg(FramePtr)
944*82d56013Sjoerg .add(predOps(ARMCC::AL))
945*82d56013Sjoerg .setMIFlag(MachineInstr::FrameDestroy);
9467330f729Sjoerg }
9477330f729Sjoerg } else if (NumBytes &&
9487330f729Sjoerg !tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
949*82d56013Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes,
950*82d56013Sjoerg MachineInstr::FrameDestroy);
9517330f729Sjoerg
9527330f729Sjoerg // Increment past our save areas.
9537330f729Sjoerg if (MBBI != MBB.end() && AFI->getDPRCalleeSavedAreaSize()) {
9547330f729Sjoerg MBBI++;
9557330f729Sjoerg // Since vpop register list cannot have gaps, there may be multiple vpop
9567330f729Sjoerg // instructions in the epilogue.
9577330f729Sjoerg while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VLDMDIA_UPD)
9587330f729Sjoerg MBBI++;
9597330f729Sjoerg }
9607330f729Sjoerg if (AFI->getDPRCalleeSavedGapSize()) {
9617330f729Sjoerg assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
9627330f729Sjoerg "unexpected DPR alignment gap");
963*82d56013Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize(),
964*82d56013Sjoerg MachineInstr::FrameDestroy);
9657330f729Sjoerg }
9667330f729Sjoerg
9677330f729Sjoerg if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
9687330f729Sjoerg if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
969*82d56013Sjoerg if (AFI->getFPCXTSaveAreaSize()) MBBI++;
9707330f729Sjoerg }
9717330f729Sjoerg
9727330f729Sjoerg if (ArgRegsSaveSize)
973*82d56013Sjoerg emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize,
974*82d56013Sjoerg MachineInstr::FrameDestroy);
9757330f729Sjoerg }
9767330f729Sjoerg
9777330f729Sjoerg /// getFrameIndexReference - Provide a base+offset reference to an FI slot for
9787330f729Sjoerg /// debug info. It's the same as what we use for resolving the code-gen
9797330f729Sjoerg /// references for now. FIXME: This can go wrong when references are
9807330f729Sjoerg /// SP-relative and simple call frames aren't used.
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const981*82d56013Sjoerg StackOffset ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF,
982*82d56013Sjoerg int FI,
983*82d56013Sjoerg Register &FrameReg) const {
984*82d56013Sjoerg return StackOffset::getFixed(ResolveFrameIndexReference(MF, FI, FrameReg, 0));
9857330f729Sjoerg }
9867330f729Sjoerg
ResolveFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg,int SPAdj) const987*82d56013Sjoerg int ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
988*82d56013Sjoerg int FI, Register &FrameReg,
9897330f729Sjoerg int SPAdj) const {
9907330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
9917330f729Sjoerg const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
9927330f729Sjoerg MF.getSubtarget().getRegisterInfo());
9937330f729Sjoerg const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
9947330f729Sjoerg int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
9957330f729Sjoerg int FPOffset = Offset - AFI->getFramePtrSpillOffset();
9967330f729Sjoerg bool isFixed = MFI.isFixedObjectIndex(FI);
9977330f729Sjoerg
9987330f729Sjoerg FrameReg = ARM::SP;
9997330f729Sjoerg Offset += SPAdj;
10007330f729Sjoerg
10017330f729Sjoerg // SP can move around if there are allocas. We may also lose track of SP
10027330f729Sjoerg // when emergency spilling inside a non-reserved call frame setup.
10037330f729Sjoerg bool hasMovingSP = !hasReservedCallFrame(MF);
10047330f729Sjoerg
10057330f729Sjoerg // When dynamically realigning the stack, use the frame pointer for
10067330f729Sjoerg // parameters, and the stack/base pointer for locals.
1007*82d56013Sjoerg if (RegInfo->hasStackRealignment(MF)) {
10087330f729Sjoerg assert(hasFP(MF) && "dynamic stack realignment without a FP!");
10097330f729Sjoerg if (isFixed) {
10107330f729Sjoerg FrameReg = RegInfo->getFrameRegister(MF);
10117330f729Sjoerg Offset = FPOffset;
10127330f729Sjoerg } else if (hasMovingSP) {
10137330f729Sjoerg assert(RegInfo->hasBasePointer(MF) &&
10147330f729Sjoerg "VLAs and dynamic stack alignment, but missing base pointer!");
10157330f729Sjoerg FrameReg = RegInfo->getBaseRegister();
10167330f729Sjoerg Offset -= SPAdj;
10177330f729Sjoerg }
10187330f729Sjoerg return Offset;
10197330f729Sjoerg }
10207330f729Sjoerg
10217330f729Sjoerg // If there is a frame pointer, use it when we can.
10227330f729Sjoerg if (hasFP(MF) && AFI->hasStackFrame()) {
10237330f729Sjoerg // Use frame pointer to reference fixed objects. Use it for locals if
10247330f729Sjoerg // there are VLAs (and thus the SP isn't reliable as a base).
10257330f729Sjoerg if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
10267330f729Sjoerg FrameReg = RegInfo->getFrameRegister(MF);
10277330f729Sjoerg return FPOffset;
10287330f729Sjoerg } else if (hasMovingSP) {
10297330f729Sjoerg assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
10307330f729Sjoerg if (AFI->isThumb2Function()) {
10317330f729Sjoerg // Try to use the frame pointer if we can, else use the base pointer
10327330f729Sjoerg // since it's available. This is handy for the emergency spill slot, in
10337330f729Sjoerg // particular.
10347330f729Sjoerg if (FPOffset >= -255 && FPOffset < 0) {
10357330f729Sjoerg FrameReg = RegInfo->getFrameRegister(MF);
10367330f729Sjoerg return FPOffset;
10377330f729Sjoerg }
10387330f729Sjoerg }
10397330f729Sjoerg } else if (AFI->isThumbFunction()) {
10407330f729Sjoerg // Prefer SP to base pointer, if the offset is suitably aligned and in
10417330f729Sjoerg // range as the effective range of the immediate offset is bigger when
10427330f729Sjoerg // basing off SP.
10437330f729Sjoerg // Use add <rd>, sp, #<imm8>
10447330f729Sjoerg // ldr <rd>, [sp, #<imm8>]
10457330f729Sjoerg if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
10467330f729Sjoerg return Offset;
10477330f729Sjoerg // In Thumb2 mode, the negative offset is very limited. Try to avoid
10487330f729Sjoerg // out of range references. ldr <rt>,[<rn>, #-<imm8>]
10497330f729Sjoerg if (AFI->isThumb2Function() && FPOffset >= -255 && FPOffset < 0) {
10507330f729Sjoerg FrameReg = RegInfo->getFrameRegister(MF);
10517330f729Sjoerg return FPOffset;
10527330f729Sjoerg }
10537330f729Sjoerg } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
10547330f729Sjoerg // Otherwise, use SP or FP, whichever is closer to the stack slot.
10557330f729Sjoerg FrameReg = RegInfo->getFrameRegister(MF);
10567330f729Sjoerg return FPOffset;
10577330f729Sjoerg }
10587330f729Sjoerg }
10597330f729Sjoerg // Use the base pointer if we have one.
10607330f729Sjoerg // FIXME: Maybe prefer sp on Thumb1 if it's legal and the offset is cheaper?
10617330f729Sjoerg // That can happen if we forced a base pointer for a large call frame.
10627330f729Sjoerg if (RegInfo->hasBasePointer(MF)) {
10637330f729Sjoerg FrameReg = RegInfo->getBaseRegister();
10647330f729Sjoerg Offset -= SPAdj;
10657330f729Sjoerg }
10667330f729Sjoerg return Offset;
10677330f729Sjoerg }
10687330f729Sjoerg
emitPushInst(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,ArrayRef<CalleeSavedInfo> CSI,unsigned StmOpc,unsigned StrOpc,bool NoGap,bool (* Func)(unsigned,bool),unsigned NumAlignedDPRCS2Regs,unsigned MIFlags) const10697330f729Sjoerg void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
10707330f729Sjoerg MachineBasicBlock::iterator MI,
1071*82d56013Sjoerg ArrayRef<CalleeSavedInfo> CSI,
10727330f729Sjoerg unsigned StmOpc, unsigned StrOpc,
1073*82d56013Sjoerg bool NoGap, bool (*Func)(unsigned, bool),
10747330f729Sjoerg unsigned NumAlignedDPRCS2Regs,
10757330f729Sjoerg unsigned MIFlags) const {
10767330f729Sjoerg MachineFunction &MF = *MBB.getParent();
10777330f729Sjoerg const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
10787330f729Sjoerg const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
10797330f729Sjoerg
10807330f729Sjoerg DebugLoc DL;
10817330f729Sjoerg
10827330f729Sjoerg using RegAndKill = std::pair<unsigned, bool>;
10837330f729Sjoerg
10847330f729Sjoerg SmallVector<RegAndKill, 4> Regs;
10857330f729Sjoerg unsigned i = CSI.size();
10867330f729Sjoerg while (i != 0) {
10877330f729Sjoerg unsigned LastReg = 0;
10887330f729Sjoerg for (; i != 0; --i) {
10897330f729Sjoerg unsigned Reg = CSI[i-1].getReg();
10907330f729Sjoerg if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
10917330f729Sjoerg
10927330f729Sjoerg // D-registers in the aligned area DPRCS2 are NOT spilled here.
10937330f729Sjoerg if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
10947330f729Sjoerg continue;
10957330f729Sjoerg
10967330f729Sjoerg const MachineRegisterInfo &MRI = MF.getRegInfo();
10977330f729Sjoerg bool isLiveIn = MRI.isLiveIn(Reg);
10987330f729Sjoerg if (!isLiveIn && !MRI.isReserved(Reg))
10997330f729Sjoerg MBB.addLiveIn(Reg);
11007330f729Sjoerg // If NoGap is true, push consecutive registers and then leave the rest
11017330f729Sjoerg // for other instructions. e.g.
11027330f729Sjoerg // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
11037330f729Sjoerg if (NoGap && LastReg && LastReg != Reg-1)
11047330f729Sjoerg break;
11057330f729Sjoerg LastReg = Reg;
11067330f729Sjoerg // Do not set a kill flag on values that are also marked as live-in. This
11077330f729Sjoerg // happens with the @llvm-returnaddress intrinsic and with arguments
11087330f729Sjoerg // passed in callee saved registers.
11097330f729Sjoerg // Omitting the kill flags is conservatively correct even if the live-in
11107330f729Sjoerg // is not used after all.
11117330f729Sjoerg Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
11127330f729Sjoerg }
11137330f729Sjoerg
11147330f729Sjoerg if (Regs.empty())
11157330f729Sjoerg continue;
11167330f729Sjoerg
11177330f729Sjoerg llvm::sort(Regs, [&](const RegAndKill &LHS, const RegAndKill &RHS) {
11187330f729Sjoerg return TRI.getEncodingValue(LHS.first) < TRI.getEncodingValue(RHS.first);
11197330f729Sjoerg });
11207330f729Sjoerg
11217330f729Sjoerg if (Regs.size() > 1 || StrOpc== 0) {
11227330f729Sjoerg MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
11237330f729Sjoerg .addReg(ARM::SP)
11247330f729Sjoerg .setMIFlags(MIFlags)
11257330f729Sjoerg .add(predOps(ARMCC::AL));
11267330f729Sjoerg for (unsigned i = 0, e = Regs.size(); i < e; ++i)
11277330f729Sjoerg MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
11287330f729Sjoerg } else if (Regs.size() == 1) {
11297330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(StrOpc), ARM::SP)
11307330f729Sjoerg .addReg(Regs[0].first, getKillRegState(Regs[0].second))
11317330f729Sjoerg .addReg(ARM::SP)
11327330f729Sjoerg .setMIFlags(MIFlags)
11337330f729Sjoerg .addImm(-4)
11347330f729Sjoerg .add(predOps(ARMCC::AL));
11357330f729Sjoerg }
11367330f729Sjoerg Regs.clear();
11377330f729Sjoerg
11387330f729Sjoerg // Put any subsequent vpush instructions before this one: they will refer to
11397330f729Sjoerg // higher register numbers so need to be pushed first in order to preserve
11407330f729Sjoerg // monotonicity.
11417330f729Sjoerg if (MI != MBB.begin())
11427330f729Sjoerg --MI;
11437330f729Sjoerg }
11447330f729Sjoerg }
11457330f729Sjoerg
emitPopInst(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,MutableArrayRef<CalleeSavedInfo> CSI,unsigned LdmOpc,unsigned LdrOpc,bool isVarArg,bool NoGap,bool (* Func)(unsigned,bool),unsigned NumAlignedDPRCS2Regs) const11467330f729Sjoerg void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
11477330f729Sjoerg MachineBasicBlock::iterator MI,
1148*82d56013Sjoerg MutableArrayRef<CalleeSavedInfo> CSI,
11497330f729Sjoerg unsigned LdmOpc, unsigned LdrOpc,
11507330f729Sjoerg bool isVarArg, bool NoGap,
11517330f729Sjoerg bool (*Func)(unsigned, bool),
11527330f729Sjoerg unsigned NumAlignedDPRCS2Regs) const {
11537330f729Sjoerg MachineFunction &MF = *MBB.getParent();
11547330f729Sjoerg const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
11557330f729Sjoerg const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
11567330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
11577330f729Sjoerg DebugLoc DL;
11587330f729Sjoerg bool isTailCall = false;
11597330f729Sjoerg bool isInterrupt = false;
11607330f729Sjoerg bool isTrap = false;
1161*82d56013Sjoerg bool isCmseEntry = false;
11627330f729Sjoerg if (MBB.end() != MI) {
11637330f729Sjoerg DL = MI->getDebugLoc();
11647330f729Sjoerg unsigned RetOpcode = MI->getOpcode();
11657330f729Sjoerg isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
11667330f729Sjoerg isInterrupt =
11677330f729Sjoerg RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
11687330f729Sjoerg isTrap =
11697330f729Sjoerg RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
11707330f729Sjoerg RetOpcode == ARM::tTRAP;
1171*82d56013Sjoerg isCmseEntry = (RetOpcode == ARM::tBXNS || RetOpcode == ARM::tBXNS_RET);
11727330f729Sjoerg }
11737330f729Sjoerg
11747330f729Sjoerg SmallVector<unsigned, 4> Regs;
11757330f729Sjoerg unsigned i = CSI.size();
11767330f729Sjoerg while (i != 0) {
11777330f729Sjoerg unsigned LastReg = 0;
11787330f729Sjoerg bool DeleteRet = false;
11797330f729Sjoerg for (; i != 0; --i) {
11807330f729Sjoerg CalleeSavedInfo &Info = CSI[i-1];
11817330f729Sjoerg unsigned Reg = Info.getReg();
11827330f729Sjoerg if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
11837330f729Sjoerg
11847330f729Sjoerg // The aligned reloads from area DPRCS2 are not inserted here.
11857330f729Sjoerg if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
11867330f729Sjoerg continue;
11877330f729Sjoerg
11887330f729Sjoerg if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
1189*82d56013Sjoerg !isCmseEntry && !isTrap && STI.hasV5TOps()) {
11907330f729Sjoerg if (MBB.succ_empty()) {
11917330f729Sjoerg Reg = ARM::PC;
11927330f729Sjoerg // Fold the return instruction into the LDM.
11937330f729Sjoerg DeleteRet = true;
11947330f729Sjoerg LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
11957330f729Sjoerg // We 'restore' LR into PC so it is not live out of the return block:
11967330f729Sjoerg // Clear Restored bit.
11977330f729Sjoerg Info.setRestored(false);
11987330f729Sjoerg } else
11997330f729Sjoerg LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
12007330f729Sjoerg }
12017330f729Sjoerg
12027330f729Sjoerg // If NoGap is true, pop consecutive registers and then leave the rest
12037330f729Sjoerg // for other instructions. e.g.
12047330f729Sjoerg // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
12057330f729Sjoerg if (NoGap && LastReg && LastReg != Reg-1)
12067330f729Sjoerg break;
12077330f729Sjoerg
12087330f729Sjoerg LastReg = Reg;
12097330f729Sjoerg Regs.push_back(Reg);
12107330f729Sjoerg }
12117330f729Sjoerg
12127330f729Sjoerg if (Regs.empty())
12137330f729Sjoerg continue;
12147330f729Sjoerg
12157330f729Sjoerg llvm::sort(Regs, [&](unsigned LHS, unsigned RHS) {
12167330f729Sjoerg return TRI.getEncodingValue(LHS) < TRI.getEncodingValue(RHS);
12177330f729Sjoerg });
12187330f729Sjoerg
12197330f729Sjoerg if (Regs.size() > 1 || LdrOpc == 0) {
12207330f729Sjoerg MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
12217330f729Sjoerg .addReg(ARM::SP)
1222*82d56013Sjoerg .add(predOps(ARMCC::AL))
1223*82d56013Sjoerg .setMIFlags(MachineInstr::FrameDestroy);
12247330f729Sjoerg for (unsigned i = 0, e = Regs.size(); i < e; ++i)
12257330f729Sjoerg MIB.addReg(Regs[i], getDefRegState(true));
12267330f729Sjoerg if (DeleteRet) {
12277330f729Sjoerg if (MI != MBB.end()) {
12287330f729Sjoerg MIB.copyImplicitOps(*MI);
12297330f729Sjoerg MI->eraseFromParent();
12307330f729Sjoerg }
12317330f729Sjoerg }
12327330f729Sjoerg MI = MIB;
12337330f729Sjoerg } else if (Regs.size() == 1) {
12347330f729Sjoerg // If we adjusted the reg to PC from LR above, switch it back here. We
12357330f729Sjoerg // only do that for LDM.
12367330f729Sjoerg if (Regs[0] == ARM::PC)
12377330f729Sjoerg Regs[0] = ARM::LR;
12387330f729Sjoerg MachineInstrBuilder MIB =
12397330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
12407330f729Sjoerg .addReg(ARM::SP, RegState::Define)
1241*82d56013Sjoerg .addReg(ARM::SP)
1242*82d56013Sjoerg .setMIFlags(MachineInstr::FrameDestroy);
12437330f729Sjoerg // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
12447330f729Sjoerg // that refactoring is complete (eventually).
12457330f729Sjoerg if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
12467330f729Sjoerg MIB.addReg(0);
12477330f729Sjoerg MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
12487330f729Sjoerg } else
12497330f729Sjoerg MIB.addImm(4);
12507330f729Sjoerg MIB.add(predOps(ARMCC::AL));
12517330f729Sjoerg }
12527330f729Sjoerg Regs.clear();
12537330f729Sjoerg
12547330f729Sjoerg // Put any subsequent vpop instructions after this one: they will refer to
12557330f729Sjoerg // higher register numbers so need to be popped afterwards.
12567330f729Sjoerg if (MI != MBB.end())
12577330f729Sjoerg ++MI;
12587330f729Sjoerg }
12597330f729Sjoerg }
12607330f729Sjoerg
12617330f729Sjoerg /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
12627330f729Sjoerg /// starting from d8. Also insert stack realignment code and leave the stack
12637330f729Sjoerg /// pointer pointing to the d8 spill slot.
emitAlignedDPRCS2Spills(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned NumAlignedDPRCS2Regs,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI)12647330f729Sjoerg static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
12657330f729Sjoerg MachineBasicBlock::iterator MI,
12667330f729Sjoerg unsigned NumAlignedDPRCS2Regs,
1267*82d56013Sjoerg ArrayRef<CalleeSavedInfo> CSI,
12687330f729Sjoerg const TargetRegisterInfo *TRI) {
12697330f729Sjoerg MachineFunction &MF = *MBB.getParent();
12707330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
12717330f729Sjoerg DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
12727330f729Sjoerg const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
12737330f729Sjoerg MachineFrameInfo &MFI = MF.getFrameInfo();
12747330f729Sjoerg
12757330f729Sjoerg // Mark the D-register spill slots as properly aligned. Since MFI computes
12767330f729Sjoerg // stack slot layout backwards, this can actually mean that the d-reg stack
12777330f729Sjoerg // slot offsets can be wrong. The offset for d8 will always be correct.
12787330f729Sjoerg for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
12797330f729Sjoerg unsigned DNum = CSI[i].getReg() - ARM::D8;
12807330f729Sjoerg if (DNum > NumAlignedDPRCS2Regs - 1)
12817330f729Sjoerg continue;
12827330f729Sjoerg int FI = CSI[i].getFrameIdx();
12837330f729Sjoerg // The even-numbered registers will be 16-byte aligned, the odd-numbered
12847330f729Sjoerg // registers will be 8-byte aligned.
1285*82d56013Sjoerg MFI.setObjectAlignment(FI, DNum % 2 ? Align(8) : Align(16));
12867330f729Sjoerg
12877330f729Sjoerg // The stack slot for D8 needs to be maximally aligned because this is
12887330f729Sjoerg // actually the point where we align the stack pointer. MachineFrameInfo
12897330f729Sjoerg // computes all offsets relative to the incoming stack pointer which is a
12907330f729Sjoerg // bit weird when realigning the stack. Any extra padding for this
12917330f729Sjoerg // over-alignment is not realized because the code inserted below adjusts
12927330f729Sjoerg // the stack pointer by numregs * 8 before aligning the stack pointer.
12937330f729Sjoerg if (DNum == 0)
1294*82d56013Sjoerg MFI.setObjectAlignment(FI, MFI.getMaxAlign());
12957330f729Sjoerg }
12967330f729Sjoerg
12977330f729Sjoerg // Move the stack pointer to the d8 spill slot, and align it at the same
12987330f729Sjoerg // time. Leave the stack slot address in the scratch register r4.
12997330f729Sjoerg //
13007330f729Sjoerg // sub r4, sp, #numregs * 8
13017330f729Sjoerg // bic r4, r4, #align - 1
13027330f729Sjoerg // mov sp, r4
13037330f729Sjoerg //
13047330f729Sjoerg bool isThumb = AFI->isThumbFunction();
13057330f729Sjoerg assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
13067330f729Sjoerg AFI->setShouldRestoreSPFromFP(true);
13077330f729Sjoerg
13087330f729Sjoerg // sub r4, sp, #numregs * 8
13097330f729Sjoerg // The immediate is <= 64, so it doesn't need any special encoding.
13107330f729Sjoerg unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
13117330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
13127330f729Sjoerg .addReg(ARM::SP)
13137330f729Sjoerg .addImm(8 * NumAlignedDPRCS2Regs)
13147330f729Sjoerg .add(predOps(ARMCC::AL))
13157330f729Sjoerg .add(condCodeOp());
13167330f729Sjoerg
1317*82d56013Sjoerg Align MaxAlign = MF.getFrameInfo().getMaxAlign();
13187330f729Sjoerg // We must set parameter MustBeSingleInstruction to true, since
13197330f729Sjoerg // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
13207330f729Sjoerg // stack alignment. Luckily, this can always be done since all ARM
13217330f729Sjoerg // architecture versions that support Neon also support the BFC
13227330f729Sjoerg // instruction.
13237330f729Sjoerg emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
13247330f729Sjoerg
13257330f729Sjoerg // mov sp, r4
13267330f729Sjoerg // The stack pointer must be adjusted before spilling anything, otherwise
13277330f729Sjoerg // the stack slots could be clobbered by an interrupt handler.
13287330f729Sjoerg // Leave r4 live, it is used below.
13297330f729Sjoerg Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
13307330f729Sjoerg MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
13317330f729Sjoerg .addReg(ARM::R4)
13327330f729Sjoerg .add(predOps(ARMCC::AL));
13337330f729Sjoerg if (!isThumb)
13347330f729Sjoerg MIB.add(condCodeOp());
13357330f729Sjoerg
13367330f729Sjoerg // Now spill NumAlignedDPRCS2Regs registers starting from d8.
13377330f729Sjoerg // r4 holds the stack slot address.
13387330f729Sjoerg unsigned NextReg = ARM::D8;
13397330f729Sjoerg
13407330f729Sjoerg // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
13417330f729Sjoerg // The writeback is only needed when emitting two vst1.64 instructions.
13427330f729Sjoerg if (NumAlignedDPRCS2Regs >= 6) {
13437330f729Sjoerg unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
13447330f729Sjoerg &ARM::QQPRRegClass);
13457330f729Sjoerg MBB.addLiveIn(SupReg);
13467330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), ARM::R4)
13477330f729Sjoerg .addReg(ARM::R4, RegState::Kill)
13487330f729Sjoerg .addImm(16)
13497330f729Sjoerg .addReg(NextReg)
13507330f729Sjoerg .addReg(SupReg, RegState::ImplicitKill)
13517330f729Sjoerg .add(predOps(ARMCC::AL));
13527330f729Sjoerg NextReg += 4;
13537330f729Sjoerg NumAlignedDPRCS2Regs -= 4;
13547330f729Sjoerg }
13557330f729Sjoerg
13567330f729Sjoerg // We won't modify r4 beyond this point. It currently points to the next
13577330f729Sjoerg // register to be spilled.
13587330f729Sjoerg unsigned R4BaseReg = NextReg;
13597330f729Sjoerg
13607330f729Sjoerg // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
13617330f729Sjoerg if (NumAlignedDPRCS2Regs >= 4) {
13627330f729Sjoerg unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
13637330f729Sjoerg &ARM::QQPRRegClass);
13647330f729Sjoerg MBB.addLiveIn(SupReg);
13657330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
13667330f729Sjoerg .addReg(ARM::R4)
13677330f729Sjoerg .addImm(16)
13687330f729Sjoerg .addReg(NextReg)
13697330f729Sjoerg .addReg(SupReg, RegState::ImplicitKill)
13707330f729Sjoerg .add(predOps(ARMCC::AL));
13717330f729Sjoerg NextReg += 4;
13727330f729Sjoerg NumAlignedDPRCS2Regs -= 4;
13737330f729Sjoerg }
13747330f729Sjoerg
13757330f729Sjoerg // 16-byte aligned vst1.64 with 2 d-regs.
13767330f729Sjoerg if (NumAlignedDPRCS2Regs >= 2) {
13777330f729Sjoerg unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
13787330f729Sjoerg &ARM::QPRRegClass);
13797330f729Sjoerg MBB.addLiveIn(SupReg);
13807330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
13817330f729Sjoerg .addReg(ARM::R4)
13827330f729Sjoerg .addImm(16)
13837330f729Sjoerg .addReg(SupReg)
13847330f729Sjoerg .add(predOps(ARMCC::AL));
13857330f729Sjoerg NextReg += 2;
13867330f729Sjoerg NumAlignedDPRCS2Regs -= 2;
13877330f729Sjoerg }
13887330f729Sjoerg
13897330f729Sjoerg // Finally, use a vanilla vstr.64 for the odd last register.
13907330f729Sjoerg if (NumAlignedDPRCS2Regs) {
13917330f729Sjoerg MBB.addLiveIn(NextReg);
13927330f729Sjoerg // vstr.64 uses addrmode5 which has an offset scale of 4.
13937330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
13947330f729Sjoerg .addReg(NextReg)
13957330f729Sjoerg .addReg(ARM::R4)
13967330f729Sjoerg .addImm((NextReg - R4BaseReg) * 2)
13977330f729Sjoerg .add(predOps(ARMCC::AL));
13987330f729Sjoerg }
13997330f729Sjoerg
14007330f729Sjoerg // The last spill instruction inserted should kill the scratch register r4.
14017330f729Sjoerg std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
14027330f729Sjoerg }
14037330f729Sjoerg
14047330f729Sjoerg /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
14057330f729Sjoerg /// iterator to the following instruction.
14067330f729Sjoerg static MachineBasicBlock::iterator
skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,unsigned NumAlignedDPRCS2Regs)14077330f729Sjoerg skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
14087330f729Sjoerg unsigned NumAlignedDPRCS2Regs) {
14097330f729Sjoerg // sub r4, sp, #numregs * 8
14107330f729Sjoerg // bic r4, r4, #align - 1
14117330f729Sjoerg // mov sp, r4
14127330f729Sjoerg ++MI; ++MI; ++MI;
14137330f729Sjoerg assert(MI->mayStore() && "Expecting spill instruction");
14147330f729Sjoerg
14157330f729Sjoerg // These switches all fall through.
14167330f729Sjoerg switch(NumAlignedDPRCS2Regs) {
14177330f729Sjoerg case 7:
14187330f729Sjoerg ++MI;
14197330f729Sjoerg assert(MI->mayStore() && "Expecting spill instruction");
14207330f729Sjoerg LLVM_FALLTHROUGH;
14217330f729Sjoerg default:
14227330f729Sjoerg ++MI;
14237330f729Sjoerg assert(MI->mayStore() && "Expecting spill instruction");
14247330f729Sjoerg LLVM_FALLTHROUGH;
14257330f729Sjoerg case 1:
14267330f729Sjoerg case 2:
14277330f729Sjoerg case 4:
14287330f729Sjoerg assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
14297330f729Sjoerg ++MI;
14307330f729Sjoerg }
14317330f729Sjoerg return MI;
14327330f729Sjoerg }
14337330f729Sjoerg
14347330f729Sjoerg /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
14357330f729Sjoerg /// starting from d8. These instructions are assumed to execute while the
14367330f729Sjoerg /// stack is still aligned, unlike the code inserted by emitPopInst.
emitAlignedDPRCS2Restores(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned NumAlignedDPRCS2Regs,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI)14377330f729Sjoerg static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
14387330f729Sjoerg MachineBasicBlock::iterator MI,
14397330f729Sjoerg unsigned NumAlignedDPRCS2Regs,
1440*82d56013Sjoerg ArrayRef<CalleeSavedInfo> CSI,
14417330f729Sjoerg const TargetRegisterInfo *TRI) {
14427330f729Sjoerg MachineFunction &MF = *MBB.getParent();
14437330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
14447330f729Sjoerg DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
14457330f729Sjoerg const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
14467330f729Sjoerg
14477330f729Sjoerg // Find the frame index assigned to d8.
14487330f729Sjoerg int D8SpillFI = 0;
14497330f729Sjoerg for (unsigned i = 0, e = CSI.size(); i != e; ++i)
14507330f729Sjoerg if (CSI[i].getReg() == ARM::D8) {
14517330f729Sjoerg D8SpillFI = CSI[i].getFrameIdx();
14527330f729Sjoerg break;
14537330f729Sjoerg }
14547330f729Sjoerg
14557330f729Sjoerg // Materialize the address of the d8 spill slot into the scratch register r4.
14567330f729Sjoerg // This can be fairly complicated if the stack frame is large, so just use
14577330f729Sjoerg // the normal frame index elimination mechanism to do it. This code runs as
14587330f729Sjoerg // the initial part of the epilog where the stack and base pointers haven't
14597330f729Sjoerg // been changed yet.
14607330f729Sjoerg bool isThumb = AFI->isThumbFunction();
14617330f729Sjoerg assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
14627330f729Sjoerg
14637330f729Sjoerg unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
14647330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
14657330f729Sjoerg .addFrameIndex(D8SpillFI)
14667330f729Sjoerg .addImm(0)
14677330f729Sjoerg .add(predOps(ARMCC::AL))
14687330f729Sjoerg .add(condCodeOp());
14697330f729Sjoerg
14707330f729Sjoerg // Now restore NumAlignedDPRCS2Regs registers starting from d8.
14717330f729Sjoerg unsigned NextReg = ARM::D8;
14727330f729Sjoerg
14737330f729Sjoerg // 16-byte aligned vld1.64 with 4 d-regs and writeback.
14747330f729Sjoerg if (NumAlignedDPRCS2Regs >= 6) {
14757330f729Sjoerg unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
14767330f729Sjoerg &ARM::QQPRRegClass);
14777330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
14787330f729Sjoerg .addReg(ARM::R4, RegState::Define)
14797330f729Sjoerg .addReg(ARM::R4, RegState::Kill)
14807330f729Sjoerg .addImm(16)
14817330f729Sjoerg .addReg(SupReg, RegState::ImplicitDefine)
14827330f729Sjoerg .add(predOps(ARMCC::AL));
14837330f729Sjoerg NextReg += 4;
14847330f729Sjoerg NumAlignedDPRCS2Regs -= 4;
14857330f729Sjoerg }
14867330f729Sjoerg
14877330f729Sjoerg // We won't modify r4 beyond this point. It currently points to the next
14887330f729Sjoerg // register to be spilled.
14897330f729Sjoerg unsigned R4BaseReg = NextReg;
14907330f729Sjoerg
14917330f729Sjoerg // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
14927330f729Sjoerg if (NumAlignedDPRCS2Regs >= 4) {
14937330f729Sjoerg unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
14947330f729Sjoerg &ARM::QQPRRegClass);
14957330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
14967330f729Sjoerg .addReg(ARM::R4)
14977330f729Sjoerg .addImm(16)
14987330f729Sjoerg .addReg(SupReg, RegState::ImplicitDefine)
14997330f729Sjoerg .add(predOps(ARMCC::AL));
15007330f729Sjoerg NextReg += 4;
15017330f729Sjoerg NumAlignedDPRCS2Regs -= 4;
15027330f729Sjoerg }
15037330f729Sjoerg
15047330f729Sjoerg // 16-byte aligned vld1.64 with 2 d-regs.
15057330f729Sjoerg if (NumAlignedDPRCS2Regs >= 2) {
15067330f729Sjoerg unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
15077330f729Sjoerg &ARM::QPRRegClass);
15087330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
15097330f729Sjoerg .addReg(ARM::R4)
15107330f729Sjoerg .addImm(16)
15117330f729Sjoerg .add(predOps(ARMCC::AL));
15127330f729Sjoerg NextReg += 2;
15137330f729Sjoerg NumAlignedDPRCS2Regs -= 2;
15147330f729Sjoerg }
15157330f729Sjoerg
15167330f729Sjoerg // Finally, use a vanilla vldr.64 for the remaining odd register.
15177330f729Sjoerg if (NumAlignedDPRCS2Regs)
15187330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
15197330f729Sjoerg .addReg(ARM::R4)
15207330f729Sjoerg .addImm(2 * (NextReg - R4BaseReg))
15217330f729Sjoerg .add(predOps(ARMCC::AL));
15227330f729Sjoerg
15237330f729Sjoerg // Last store kills r4.
15247330f729Sjoerg std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
15257330f729Sjoerg }
15267330f729Sjoerg
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const1527*82d56013Sjoerg bool ARMFrameLowering::spillCalleeSavedRegisters(
1528*82d56013Sjoerg MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1529*82d56013Sjoerg ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
15307330f729Sjoerg if (CSI.empty())
15317330f729Sjoerg return false;
15327330f729Sjoerg
15337330f729Sjoerg MachineFunction &MF = *MBB.getParent();
15347330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
15357330f729Sjoerg
15367330f729Sjoerg unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
15377330f729Sjoerg unsigned PushOneOpc = AFI->isThumbFunction() ?
15387330f729Sjoerg ARM::t2STR_PRE : ARM::STR_PRE_IMM;
15397330f729Sjoerg unsigned FltOpc = ARM::VSTMDDB_UPD;
15407330f729Sjoerg unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1541*82d56013Sjoerg // Save the non-secure floating point context.
1542*82d56013Sjoerg if (llvm::any_of(CSI, [](const CalleeSavedInfo &C) {
1543*82d56013Sjoerg return C.getReg() == ARM::FPCXTNS;
1544*82d56013Sjoerg })) {
1545*82d56013Sjoerg BuildMI(MBB, MI, DebugLoc(), STI.getInstrInfo()->get(ARM::VSTR_FPCXTNS_pre),
1546*82d56013Sjoerg ARM::SP)
1547*82d56013Sjoerg .addReg(ARM::SP)
1548*82d56013Sjoerg .addImm(-4)
1549*82d56013Sjoerg .add(predOps(ARMCC::AL));
1550*82d56013Sjoerg }
15517330f729Sjoerg emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
15527330f729Sjoerg MachineInstr::FrameSetup);
15537330f729Sjoerg emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
15547330f729Sjoerg MachineInstr::FrameSetup);
15557330f729Sjoerg emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
15567330f729Sjoerg NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
15577330f729Sjoerg
15587330f729Sjoerg // The code above does not insert spill code for the aligned DPRCS2 registers.
15597330f729Sjoerg // The stack realignment code will be inserted between the push instructions
15607330f729Sjoerg // and these spills.
15617330f729Sjoerg if (NumAlignedDPRCS2Regs)
15627330f729Sjoerg emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
15637330f729Sjoerg
15647330f729Sjoerg return true;
15657330f729Sjoerg }
15667330f729Sjoerg
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const1567*82d56013Sjoerg bool ARMFrameLowering::restoreCalleeSavedRegisters(
1568*82d56013Sjoerg MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1569*82d56013Sjoerg MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
15707330f729Sjoerg if (CSI.empty())
15717330f729Sjoerg return false;
15727330f729Sjoerg
15737330f729Sjoerg MachineFunction &MF = *MBB.getParent();
15747330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
15757330f729Sjoerg bool isVarArg = AFI->getArgRegsSaveSize() > 0;
15767330f729Sjoerg unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
15777330f729Sjoerg
15787330f729Sjoerg // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
15797330f729Sjoerg // registers. Do that here instead.
15807330f729Sjoerg if (NumAlignedDPRCS2Regs)
15817330f729Sjoerg emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
15827330f729Sjoerg
15837330f729Sjoerg unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
15847330f729Sjoerg unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
15857330f729Sjoerg unsigned FltOpc = ARM::VLDMDIA_UPD;
15867330f729Sjoerg emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
15877330f729Sjoerg NumAlignedDPRCS2Regs);
15887330f729Sjoerg emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
15897330f729Sjoerg &isARMArea2Register, 0);
15907330f729Sjoerg emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
15917330f729Sjoerg &isARMArea1Register, 0);
15927330f729Sjoerg
15937330f729Sjoerg return true;
15947330f729Sjoerg }
15957330f729Sjoerg
15967330f729Sjoerg // FIXME: Make generic?
EstimateFunctionSizeInBytes(const MachineFunction & MF,const ARMBaseInstrInfo & TII)15977330f729Sjoerg static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF,
15987330f729Sjoerg const ARMBaseInstrInfo &TII) {
15997330f729Sjoerg unsigned FnSize = 0;
16007330f729Sjoerg for (auto &MBB : MF) {
16017330f729Sjoerg for (auto &MI : MBB)
16027330f729Sjoerg FnSize += TII.getInstSizeInBytes(MI);
16037330f729Sjoerg }
16047330f729Sjoerg if (MF.getJumpTableInfo())
16057330f729Sjoerg for (auto &Table: MF.getJumpTableInfo()->getJumpTables())
16067330f729Sjoerg FnSize += Table.MBBs.size() * 4;
16077330f729Sjoerg FnSize += MF.getConstantPool()->getConstants().size() * 4;
16087330f729Sjoerg return FnSize;
16097330f729Sjoerg }
16107330f729Sjoerg
16117330f729Sjoerg /// estimateRSStackSizeLimit - Look at each instruction that references stack
16127330f729Sjoerg /// frames and return the stack size limit beyond which some of these
16137330f729Sjoerg /// instructions will require a scratch register during their expansion later.
16147330f729Sjoerg // FIXME: Move to TII?
estimateRSStackSizeLimit(MachineFunction & MF,const TargetFrameLowering * TFI,bool & HasNonSPFrameIndex)16157330f729Sjoerg static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
16167330f729Sjoerg const TargetFrameLowering *TFI,
16177330f729Sjoerg bool &HasNonSPFrameIndex) {
16187330f729Sjoerg const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
16197330f729Sjoerg const ARMBaseInstrInfo &TII =
16207330f729Sjoerg *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
16217330f729Sjoerg const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
16227330f729Sjoerg unsigned Limit = (1 << 12) - 1;
16237330f729Sjoerg for (auto &MBB : MF) {
16247330f729Sjoerg for (auto &MI : MBB) {
16257330f729Sjoerg if (MI.isDebugInstr())
16267330f729Sjoerg continue;
16277330f729Sjoerg for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
16287330f729Sjoerg if (!MI.getOperand(i).isFI())
16297330f729Sjoerg continue;
16307330f729Sjoerg
16317330f729Sjoerg // When using ADDri to get the address of a stack object, 255 is the
16327330f729Sjoerg // largest offset guaranteed to fit in the immediate offset.
16337330f729Sjoerg if (MI.getOpcode() == ARM::ADDri) {
16347330f729Sjoerg Limit = std::min(Limit, (1U << 8) - 1);
16357330f729Sjoerg break;
16367330f729Sjoerg }
16377330f729Sjoerg // t2ADDri will not require an extra register, it can reuse the
16387330f729Sjoerg // destination.
16397330f729Sjoerg if (MI.getOpcode() == ARM::t2ADDri || MI.getOpcode() == ARM::t2ADDri12)
16407330f729Sjoerg break;
16417330f729Sjoerg
16427330f729Sjoerg const MCInstrDesc &MCID = MI.getDesc();
16437330f729Sjoerg const TargetRegisterClass *RegClass = TII.getRegClass(MCID, i, TRI, MF);
16447330f729Sjoerg if (RegClass && !RegClass->contains(ARM::SP))
16457330f729Sjoerg HasNonSPFrameIndex = true;
16467330f729Sjoerg
16477330f729Sjoerg // Otherwise check the addressing mode.
16487330f729Sjoerg switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
16497330f729Sjoerg case ARMII::AddrMode_i12:
16507330f729Sjoerg case ARMII::AddrMode2:
16517330f729Sjoerg // Default 12 bit limit.
16527330f729Sjoerg break;
16537330f729Sjoerg case ARMII::AddrMode3:
16547330f729Sjoerg case ARMII::AddrModeT2_i8:
16557330f729Sjoerg Limit = std::min(Limit, (1U << 8) - 1);
16567330f729Sjoerg break;
16577330f729Sjoerg case ARMII::AddrMode5FP16:
16587330f729Sjoerg Limit = std::min(Limit, ((1U << 8) - 1) * 2);
16597330f729Sjoerg break;
16607330f729Sjoerg case ARMII::AddrMode5:
16617330f729Sjoerg case ARMII::AddrModeT2_i8s4:
16627330f729Sjoerg case ARMII::AddrModeT2_ldrex:
16637330f729Sjoerg Limit = std::min(Limit, ((1U << 8) - 1) * 4);
16647330f729Sjoerg break;
16657330f729Sjoerg case ARMII::AddrModeT2_i12:
16667330f729Sjoerg // i12 supports only positive offset so these will be converted to
16677330f729Sjoerg // i8 opcodes. See llvm::rewriteT2FrameIndex.
16687330f729Sjoerg if (TFI->hasFP(MF) && AFI->hasStackFrame())
16697330f729Sjoerg Limit = std::min(Limit, (1U << 8) - 1);
16707330f729Sjoerg break;
16717330f729Sjoerg case ARMII::AddrMode4:
16727330f729Sjoerg case ARMII::AddrMode6:
16737330f729Sjoerg // Addressing modes 4 & 6 (load/store) instructions can't encode an
16747330f729Sjoerg // immediate offset for stack references.
16757330f729Sjoerg return 0;
16767330f729Sjoerg case ARMII::AddrModeT2_i7:
16777330f729Sjoerg Limit = std::min(Limit, ((1U << 7) - 1) * 1);
16787330f729Sjoerg break;
16797330f729Sjoerg case ARMII::AddrModeT2_i7s2:
16807330f729Sjoerg Limit = std::min(Limit, ((1U << 7) - 1) * 2);
16817330f729Sjoerg break;
16827330f729Sjoerg case ARMII::AddrModeT2_i7s4:
16837330f729Sjoerg Limit = std::min(Limit, ((1U << 7) - 1) * 4);
16847330f729Sjoerg break;
16857330f729Sjoerg default:
16867330f729Sjoerg llvm_unreachable("Unhandled addressing mode in stack size limit calculation");
16877330f729Sjoerg }
16887330f729Sjoerg break; // At most one FI per instruction
16897330f729Sjoerg }
16907330f729Sjoerg }
16917330f729Sjoerg }
16927330f729Sjoerg
16937330f729Sjoerg return Limit;
16947330f729Sjoerg }
16957330f729Sjoerg
16967330f729Sjoerg // In functions that realign the stack, it can be an advantage to spill the
16977330f729Sjoerg // callee-saved vector registers after realigning the stack. The vst1 and vld1
16987330f729Sjoerg // instructions take alignment hints that can improve performance.
16997330f729Sjoerg static void
checkNumAlignedDPRCS2Regs(MachineFunction & MF,BitVector & SavedRegs)17007330f729Sjoerg checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
17017330f729Sjoerg MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
17027330f729Sjoerg if (!SpillAlignedNEONRegs)
17037330f729Sjoerg return;
17047330f729Sjoerg
17057330f729Sjoerg // Naked functions don't spill callee-saved registers.
17067330f729Sjoerg if (MF.getFunction().hasFnAttribute(Attribute::Naked))
17077330f729Sjoerg return;
17087330f729Sjoerg
17097330f729Sjoerg // We are planning to use NEON instructions vst1 / vld1.
17107330f729Sjoerg if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
17117330f729Sjoerg return;
17127330f729Sjoerg
17137330f729Sjoerg // Don't bother if the default stack alignment is sufficiently high.
1714*82d56013Sjoerg if (MF.getSubtarget().getFrameLowering()->getStackAlign() >= Align(8))
17157330f729Sjoerg return;
17167330f729Sjoerg
17177330f729Sjoerg // Aligned spills require stack realignment.
17187330f729Sjoerg if (!static_cast<const ARMBaseRegisterInfo *>(
17197330f729Sjoerg MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
17207330f729Sjoerg return;
17217330f729Sjoerg
17227330f729Sjoerg // We always spill contiguous d-registers starting from d8. Count how many
17237330f729Sjoerg // needs spilling. The register allocator will almost always use the
17247330f729Sjoerg // callee-saved registers in order, but it can happen that there are holes in
17257330f729Sjoerg // the range. Registers above the hole will be spilled to the standard DPRCS
17267330f729Sjoerg // area.
17277330f729Sjoerg unsigned NumSpills = 0;
17287330f729Sjoerg for (; NumSpills < 8; ++NumSpills)
17297330f729Sjoerg if (!SavedRegs.test(ARM::D8 + NumSpills))
17307330f729Sjoerg break;
17317330f729Sjoerg
17327330f729Sjoerg // Don't do this for just one d-register. It's not worth it.
17337330f729Sjoerg if (NumSpills < 2)
17347330f729Sjoerg return;
17357330f729Sjoerg
17367330f729Sjoerg // Spill the first NumSpills D-registers after realigning the stack.
17377330f729Sjoerg MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
17387330f729Sjoerg
17397330f729Sjoerg // A scratch register is required for the vst1 / vld1 instructions.
17407330f729Sjoerg SavedRegs.set(ARM::R4);
17417330f729Sjoerg }
17427330f729Sjoerg
enableShrinkWrapping(const MachineFunction & MF) const1743*82d56013Sjoerg bool ARMFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
1744*82d56013Sjoerg // For CMSE entry functions, we want to save the FPCXT_NS immediately
1745*82d56013Sjoerg // upon function entry (resp. restore it immmediately before return)
1746*82d56013Sjoerg if (STI.hasV8_1MMainlineOps() &&
1747*82d56013Sjoerg MF.getInfo<ARMFunctionInfo>()->isCmseNSEntryFunction())
1748*82d56013Sjoerg return false;
1749*82d56013Sjoerg
1750*82d56013Sjoerg return true;
1751*82d56013Sjoerg }
1752*82d56013Sjoerg
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const17537330f729Sjoerg void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
17547330f729Sjoerg BitVector &SavedRegs,
17557330f729Sjoerg RegScavenger *RS) const {
17567330f729Sjoerg TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
17577330f729Sjoerg // This tells PEI to spill the FP as if it is any other callee-save register
17587330f729Sjoerg // to take advantage the eliminateFrameIndex machinery. This also ensures it
17597330f729Sjoerg // is spilled in the order specified by getCalleeSavedRegs() to make it easier
17607330f729Sjoerg // to combine multiple loads / stores.
17617330f729Sjoerg bool CanEliminateFrame = true;
17627330f729Sjoerg bool CS1Spilled = false;
17637330f729Sjoerg bool LRSpilled = false;
17647330f729Sjoerg unsigned NumGPRSpills = 0;
17657330f729Sjoerg unsigned NumFPRSpills = 0;
17667330f729Sjoerg SmallVector<unsigned, 4> UnspilledCS1GPRs;
17677330f729Sjoerg SmallVector<unsigned, 4> UnspilledCS2GPRs;
17687330f729Sjoerg const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
17697330f729Sjoerg MF.getSubtarget().getRegisterInfo());
17707330f729Sjoerg const ARMBaseInstrInfo &TII =
17717330f729Sjoerg *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
17727330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
17737330f729Sjoerg MachineFrameInfo &MFI = MF.getFrameInfo();
17747330f729Sjoerg MachineRegisterInfo &MRI = MF.getRegInfo();
17757330f729Sjoerg const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
17767330f729Sjoerg (void)TRI; // Silence unused warning in non-assert builds.
17777330f729Sjoerg Register FramePtr = RegInfo->getFrameRegister(MF);
17787330f729Sjoerg
17797330f729Sjoerg // Spill R4 if Thumb2 function requires stack realignment - it will be used as
17807330f729Sjoerg // scratch register. Also spill R4 if Thumb2 function has varsized objects,
17817330f729Sjoerg // since it's not always possible to restore sp from fp in a single
17827330f729Sjoerg // instruction.
17837330f729Sjoerg // FIXME: It will be better just to find spare register here.
17847330f729Sjoerg if (AFI->isThumb2Function() &&
1785*82d56013Sjoerg (MFI.hasVarSizedObjects() || RegInfo->hasStackRealignment(MF)))
17867330f729Sjoerg SavedRegs.set(ARM::R4);
17877330f729Sjoerg
17887330f729Sjoerg // If a stack probe will be emitted, spill R4 and LR, since they are
17897330f729Sjoerg // clobbered by the stack probe call.
17907330f729Sjoerg // This estimate should be a safe, conservative estimate. The actual
17917330f729Sjoerg // stack probe is enabled based on the size of the local objects;
17927330f729Sjoerg // this estimate also includes the varargs store size.
17937330f729Sjoerg if (STI.isTargetWindows() &&
17947330f729Sjoerg WindowsRequiresStackProbe(MF, MFI.estimateStackSize(MF))) {
17957330f729Sjoerg SavedRegs.set(ARM::R4);
17967330f729Sjoerg SavedRegs.set(ARM::LR);
17977330f729Sjoerg }
17987330f729Sjoerg
17997330f729Sjoerg if (AFI->isThumb1OnlyFunction()) {
18007330f729Sjoerg // Spill LR if Thumb1 function uses variable length argument lists.
18017330f729Sjoerg if (AFI->getArgRegsSaveSize() > 0)
18027330f729Sjoerg SavedRegs.set(ARM::LR);
18037330f729Sjoerg
18047330f729Sjoerg // Spill R4 if Thumb1 epilogue has to restore SP from FP or the function
18057330f729Sjoerg // requires stack alignment. We don't know for sure what the stack size
18067330f729Sjoerg // will be, but for this, an estimate is good enough. If there anything
18077330f729Sjoerg // changes it, it'll be a spill, which implies we've used all the registers
18087330f729Sjoerg // and so R4 is already used, so not marking it here will be OK.
18097330f729Sjoerg // FIXME: It will be better just to find spare register here.
1810*82d56013Sjoerg if (MFI.hasVarSizedObjects() || RegInfo->hasStackRealignment(MF) ||
18117330f729Sjoerg MFI.estimateStackSize(MF) > 508)
18127330f729Sjoerg SavedRegs.set(ARM::R4);
18137330f729Sjoerg }
18147330f729Sjoerg
18157330f729Sjoerg // See if we can spill vector registers to aligned stack.
18167330f729Sjoerg checkNumAlignedDPRCS2Regs(MF, SavedRegs);
18177330f729Sjoerg
18187330f729Sjoerg // Spill the BasePtr if it's used.
18197330f729Sjoerg if (RegInfo->hasBasePointer(MF))
18207330f729Sjoerg SavedRegs.set(RegInfo->getBaseRegister());
18217330f729Sjoerg
1822*82d56013Sjoerg // On v8.1-M.Main CMSE entry functions save/restore FPCXT.
1823*82d56013Sjoerg if (STI.hasV8_1MMainlineOps() && AFI->isCmseNSEntryFunction())
1824*82d56013Sjoerg CanEliminateFrame = false;
1825*82d56013Sjoerg
18267330f729Sjoerg // Don't spill FP if the frame can be eliminated. This is determined
18277330f729Sjoerg // by scanning the callee-save registers to see if any is modified.
18287330f729Sjoerg const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
18297330f729Sjoerg for (unsigned i = 0; CSRegs[i]; ++i) {
18307330f729Sjoerg unsigned Reg = CSRegs[i];
18317330f729Sjoerg bool Spilled = false;
18327330f729Sjoerg if (SavedRegs.test(Reg)) {
18337330f729Sjoerg Spilled = true;
18347330f729Sjoerg CanEliminateFrame = false;
18357330f729Sjoerg }
18367330f729Sjoerg
18377330f729Sjoerg if (!ARM::GPRRegClass.contains(Reg)) {
18387330f729Sjoerg if (Spilled) {
18397330f729Sjoerg if (ARM::SPRRegClass.contains(Reg))
18407330f729Sjoerg NumFPRSpills++;
18417330f729Sjoerg else if (ARM::DPRRegClass.contains(Reg))
18427330f729Sjoerg NumFPRSpills += 2;
18437330f729Sjoerg else if (ARM::QPRRegClass.contains(Reg))
18447330f729Sjoerg NumFPRSpills += 4;
18457330f729Sjoerg }
18467330f729Sjoerg continue;
18477330f729Sjoerg }
18487330f729Sjoerg
18497330f729Sjoerg if (Spilled) {
18507330f729Sjoerg NumGPRSpills++;
18517330f729Sjoerg
18527330f729Sjoerg if (!STI.splitFramePushPop(MF)) {
18537330f729Sjoerg if (Reg == ARM::LR)
18547330f729Sjoerg LRSpilled = true;
18557330f729Sjoerg CS1Spilled = true;
18567330f729Sjoerg continue;
18577330f729Sjoerg }
18587330f729Sjoerg
18597330f729Sjoerg // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
18607330f729Sjoerg switch (Reg) {
18617330f729Sjoerg case ARM::LR:
18627330f729Sjoerg LRSpilled = true;
18637330f729Sjoerg LLVM_FALLTHROUGH;
18647330f729Sjoerg case ARM::R0: case ARM::R1:
18657330f729Sjoerg case ARM::R2: case ARM::R3:
18667330f729Sjoerg case ARM::R4: case ARM::R5:
18677330f729Sjoerg case ARM::R6: case ARM::R7:
18687330f729Sjoerg CS1Spilled = true;
18697330f729Sjoerg break;
18707330f729Sjoerg default:
18717330f729Sjoerg break;
18727330f729Sjoerg }
18737330f729Sjoerg } else {
18747330f729Sjoerg if (!STI.splitFramePushPop(MF)) {
18757330f729Sjoerg UnspilledCS1GPRs.push_back(Reg);
18767330f729Sjoerg continue;
18777330f729Sjoerg }
18787330f729Sjoerg
18797330f729Sjoerg switch (Reg) {
18807330f729Sjoerg case ARM::R0: case ARM::R1:
18817330f729Sjoerg case ARM::R2: case ARM::R3:
18827330f729Sjoerg case ARM::R4: case ARM::R5:
18837330f729Sjoerg case ARM::R6: case ARM::R7:
18847330f729Sjoerg case ARM::LR:
18857330f729Sjoerg UnspilledCS1GPRs.push_back(Reg);
18867330f729Sjoerg break;
18877330f729Sjoerg default:
18887330f729Sjoerg UnspilledCS2GPRs.push_back(Reg);
18897330f729Sjoerg break;
18907330f729Sjoerg }
18917330f729Sjoerg }
18927330f729Sjoerg }
18937330f729Sjoerg
18947330f729Sjoerg bool ForceLRSpill = false;
18957330f729Sjoerg if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
18967330f729Sjoerg unsigned FnSize = EstimateFunctionSizeInBytes(MF, TII);
18977330f729Sjoerg // Force LR to be spilled if the Thumb function size is > 2048. This enables
1898*82d56013Sjoerg // use of BL to implement far jump.
18997330f729Sjoerg if (FnSize >= (1 << 11)) {
19007330f729Sjoerg CanEliminateFrame = false;
19017330f729Sjoerg ForceLRSpill = true;
19027330f729Sjoerg }
19037330f729Sjoerg }
19047330f729Sjoerg
19057330f729Sjoerg // If any of the stack slot references may be out of range of an immediate
19067330f729Sjoerg // offset, make sure a register (or a spill slot) is available for the
19077330f729Sjoerg // register scavenger. Note that if we're indexing off the frame pointer, the
19087330f729Sjoerg // effective stack size is 4 bytes larger since the FP points to the stack
19097330f729Sjoerg // slot of the previous FP. Also, if we have variable sized objects in the
19107330f729Sjoerg // function, stack slot references will often be negative, and some of
19117330f729Sjoerg // our instructions are positive-offset only, so conservatively consider
19127330f729Sjoerg // that case to want a spill slot (or register) as well. Similarly, if
19137330f729Sjoerg // the function adjusts the stack pointer during execution and the
19147330f729Sjoerg // adjustments aren't already part of our stack size estimate, our offset
19157330f729Sjoerg // calculations may be off, so be conservative.
19167330f729Sjoerg // FIXME: We could add logic to be more precise about negative offsets
19177330f729Sjoerg // and which instructions will need a scratch register for them. Is it
19187330f729Sjoerg // worth the effort and added fragility?
19197330f729Sjoerg unsigned EstimatedStackSize =
19207330f729Sjoerg MFI.estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
19217330f729Sjoerg
19227330f729Sjoerg // Determine biggest (positive) SP offset in MachineFrameInfo.
19237330f729Sjoerg int MaxFixedOffset = 0;
19247330f729Sjoerg for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
19257330f729Sjoerg int MaxObjectOffset = MFI.getObjectOffset(I) + MFI.getObjectSize(I);
19267330f729Sjoerg MaxFixedOffset = std::max(MaxFixedOffset, MaxObjectOffset);
19277330f729Sjoerg }
19287330f729Sjoerg
19297330f729Sjoerg bool HasFP = hasFP(MF);
19307330f729Sjoerg if (HasFP) {
19317330f729Sjoerg if (AFI->hasStackFrame())
19327330f729Sjoerg EstimatedStackSize += 4;
19337330f729Sjoerg } else {
19347330f729Sjoerg // If FP is not used, SP will be used to access arguments, so count the
19357330f729Sjoerg // size of arguments into the estimation.
19367330f729Sjoerg EstimatedStackSize += MaxFixedOffset;
19377330f729Sjoerg }
19387330f729Sjoerg EstimatedStackSize += 16; // For possible paddings.
19397330f729Sjoerg
19407330f729Sjoerg unsigned EstimatedRSStackSizeLimit, EstimatedRSFixedSizeLimit;
19417330f729Sjoerg bool HasNonSPFrameIndex = false;
19427330f729Sjoerg if (AFI->isThumb1OnlyFunction()) {
19437330f729Sjoerg // For Thumb1, don't bother to iterate over the function. The only
19447330f729Sjoerg // instruction that requires an emergency spill slot is a store to a
19457330f729Sjoerg // frame index.
19467330f729Sjoerg //
19477330f729Sjoerg // tSTRspi, which is used for sp-relative accesses, has an 8-bit unsigned
19487330f729Sjoerg // immediate. tSTRi, which is used for bp- and fp-relative accesses, has
19497330f729Sjoerg // a 5-bit unsigned immediate.
19507330f729Sjoerg //
19517330f729Sjoerg // We could try to check if the function actually contains a tSTRspi
19527330f729Sjoerg // that might need the spill slot, but it's not really important.
19537330f729Sjoerg // Functions with VLAs or extremely large call frames are rare, and
19547330f729Sjoerg // if a function is allocating more than 1KB of stack, an extra 4-byte
19557330f729Sjoerg // slot probably isn't relevant.
19567330f729Sjoerg if (RegInfo->hasBasePointer(MF))
19577330f729Sjoerg EstimatedRSStackSizeLimit = (1U << 5) * 4;
19587330f729Sjoerg else
19597330f729Sjoerg EstimatedRSStackSizeLimit = (1U << 8) * 4;
19607330f729Sjoerg EstimatedRSFixedSizeLimit = (1U << 5) * 4;
19617330f729Sjoerg } else {
19627330f729Sjoerg EstimatedRSStackSizeLimit =
19637330f729Sjoerg estimateRSStackSizeLimit(MF, this, HasNonSPFrameIndex);
19647330f729Sjoerg EstimatedRSFixedSizeLimit = EstimatedRSStackSizeLimit;
19657330f729Sjoerg }
19667330f729Sjoerg // Final estimate of whether sp or bp-relative accesses might require
19677330f729Sjoerg // scavenging.
19687330f729Sjoerg bool HasLargeStack = EstimatedStackSize > EstimatedRSStackSizeLimit;
19697330f729Sjoerg
19707330f729Sjoerg // If the stack pointer moves and we don't have a base pointer, the
19717330f729Sjoerg // estimate logic doesn't work. The actual offsets might be larger when
19727330f729Sjoerg // we're constructing a call frame, or we might need to use negative
19737330f729Sjoerg // offsets from fp.
19747330f729Sjoerg bool HasMovingSP = MFI.hasVarSizedObjects() ||
19757330f729Sjoerg (MFI.adjustsStack() && !canSimplifyCallFramePseudos(MF));
19767330f729Sjoerg bool HasBPOrFixedSP = RegInfo->hasBasePointer(MF) || !HasMovingSP;
19777330f729Sjoerg
19787330f729Sjoerg // If we have a frame pointer, we assume arguments will be accessed
19797330f729Sjoerg // relative to the frame pointer. Check whether fp-relative accesses to
19807330f729Sjoerg // arguments require scavenging.
19817330f729Sjoerg //
19827330f729Sjoerg // We could do slightly better on Thumb1; in some cases, an sp-relative
19837330f729Sjoerg // offset would be legal even though an fp-relative offset is not.
1984*82d56013Sjoerg int MaxFPOffset = getMaxFPOffset(STI, *AFI);
19857330f729Sjoerg bool HasLargeArgumentList =
19867330f729Sjoerg HasFP && (MaxFixedOffset - MaxFPOffset) > (int)EstimatedRSFixedSizeLimit;
19877330f729Sjoerg
19887330f729Sjoerg bool BigFrameOffsets = HasLargeStack || !HasBPOrFixedSP ||
19897330f729Sjoerg HasLargeArgumentList || HasNonSPFrameIndex;
19907330f729Sjoerg LLVM_DEBUG(dbgs() << "EstimatedLimit: " << EstimatedRSStackSizeLimit
19917330f729Sjoerg << "; EstimatedStack: " << EstimatedStackSize
19927330f729Sjoerg << "; EstimatedFPStack: " << MaxFixedOffset - MaxFPOffset
19937330f729Sjoerg << "; BigFrameOffsets: " << BigFrameOffsets << "\n");
19947330f729Sjoerg if (BigFrameOffsets ||
19957330f729Sjoerg !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
19967330f729Sjoerg AFI->setHasStackFrame(true);
19977330f729Sjoerg
19987330f729Sjoerg if (HasFP) {
19997330f729Sjoerg SavedRegs.set(FramePtr);
20007330f729Sjoerg // If the frame pointer is required by the ABI, also spill LR so that we
20017330f729Sjoerg // emit a complete frame record.
20027330f729Sjoerg if (MF.getTarget().Options.DisableFramePointerElim(MF) && !LRSpilled) {
20037330f729Sjoerg SavedRegs.set(ARM::LR);
20047330f729Sjoerg LRSpilled = true;
20057330f729Sjoerg NumGPRSpills++;
20067330f729Sjoerg auto LRPos = llvm::find(UnspilledCS1GPRs, ARM::LR);
20077330f729Sjoerg if (LRPos != UnspilledCS1GPRs.end())
20087330f729Sjoerg UnspilledCS1GPRs.erase(LRPos);
20097330f729Sjoerg }
20107330f729Sjoerg auto FPPos = llvm::find(UnspilledCS1GPRs, FramePtr);
20117330f729Sjoerg if (FPPos != UnspilledCS1GPRs.end())
20127330f729Sjoerg UnspilledCS1GPRs.erase(FPPos);
20137330f729Sjoerg NumGPRSpills++;
20147330f729Sjoerg if (FramePtr == ARM::R7)
20157330f729Sjoerg CS1Spilled = true;
20167330f729Sjoerg }
20177330f729Sjoerg
20187330f729Sjoerg // This is true when we inserted a spill for a callee-save GPR which is
20197330f729Sjoerg // not otherwise used by the function. This guaranteees it is possible
20207330f729Sjoerg // to scavenge a register to hold the address of a stack slot. On Thumb1,
20217330f729Sjoerg // the register must be a valid operand to tSTRi, i.e. r4-r7. For other
20227330f729Sjoerg // subtargets, this is any GPR, i.e. r4-r11 or lr.
20237330f729Sjoerg //
20247330f729Sjoerg // If we don't insert a spill, we instead allocate an emergency spill
20257330f729Sjoerg // slot, which can be used by scavenging to spill an arbitrary register.
20267330f729Sjoerg //
20277330f729Sjoerg // We currently don't try to figure out whether any specific instruction
20287330f729Sjoerg // requires scavening an additional register.
20297330f729Sjoerg bool ExtraCSSpill = false;
20307330f729Sjoerg
20317330f729Sjoerg if (AFI->isThumb1OnlyFunction()) {
20327330f729Sjoerg // For Thumb1-only targets, we need some low registers when we save and
20337330f729Sjoerg // restore the high registers (which aren't allocatable, but could be
20347330f729Sjoerg // used by inline assembly) because the push/pop instructions can not
20357330f729Sjoerg // access high registers. If necessary, we might need to push more low
20367330f729Sjoerg // registers to ensure that there is at least one free that can be used
20377330f729Sjoerg // for the saving & restoring, and preferably we should ensure that as
20387330f729Sjoerg // many as are needed are available so that fewer push/pop instructions
20397330f729Sjoerg // are required.
20407330f729Sjoerg
20417330f729Sjoerg // Low registers which are not currently pushed, but could be (r4-r7).
20427330f729Sjoerg SmallVector<unsigned, 4> AvailableRegs;
20437330f729Sjoerg
20447330f729Sjoerg // Unused argument registers (r0-r3) can be clobbered in the prologue for
20457330f729Sjoerg // free.
20467330f729Sjoerg int EntryRegDeficit = 0;
20477330f729Sjoerg for (unsigned Reg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3}) {
20487330f729Sjoerg if (!MF.getRegInfo().isLiveIn(Reg)) {
20497330f729Sjoerg --EntryRegDeficit;
20507330f729Sjoerg LLVM_DEBUG(dbgs()
20517330f729Sjoerg << printReg(Reg, TRI)
20527330f729Sjoerg << " is unused argument register, EntryRegDeficit = "
20537330f729Sjoerg << EntryRegDeficit << "\n");
20547330f729Sjoerg }
20557330f729Sjoerg }
20567330f729Sjoerg
20577330f729Sjoerg // Unused return registers can be clobbered in the epilogue for free.
20587330f729Sjoerg int ExitRegDeficit = AFI->getReturnRegsCount() - 4;
20597330f729Sjoerg LLVM_DEBUG(dbgs() << AFI->getReturnRegsCount()
20607330f729Sjoerg << " return regs used, ExitRegDeficit = "
20617330f729Sjoerg << ExitRegDeficit << "\n");
20627330f729Sjoerg
20637330f729Sjoerg int RegDeficit = std::max(EntryRegDeficit, ExitRegDeficit);
20647330f729Sjoerg LLVM_DEBUG(dbgs() << "RegDeficit = " << RegDeficit << "\n");
20657330f729Sjoerg
20667330f729Sjoerg // r4-r6 can be used in the prologue if they are pushed by the first push
20677330f729Sjoerg // instruction.
20687330f729Sjoerg for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6}) {
20697330f729Sjoerg if (SavedRegs.test(Reg)) {
20707330f729Sjoerg --RegDeficit;
20717330f729Sjoerg LLVM_DEBUG(dbgs() << printReg(Reg, TRI)
20727330f729Sjoerg << " is saved low register, RegDeficit = "
20737330f729Sjoerg << RegDeficit << "\n");
20747330f729Sjoerg } else {
20757330f729Sjoerg AvailableRegs.push_back(Reg);
20767330f729Sjoerg LLVM_DEBUG(
20777330f729Sjoerg dbgs()
20787330f729Sjoerg << printReg(Reg, TRI)
20797330f729Sjoerg << " is non-saved low register, adding to AvailableRegs\n");
20807330f729Sjoerg }
20817330f729Sjoerg }
20827330f729Sjoerg
20837330f729Sjoerg // r7 can be used if it is not being used as the frame pointer.
20847330f729Sjoerg if (!HasFP) {
20857330f729Sjoerg if (SavedRegs.test(ARM::R7)) {
20867330f729Sjoerg --RegDeficit;
20877330f729Sjoerg LLVM_DEBUG(dbgs() << "%r7 is saved low register, RegDeficit = "
20887330f729Sjoerg << RegDeficit << "\n");
20897330f729Sjoerg } else {
20907330f729Sjoerg AvailableRegs.push_back(ARM::R7);
20917330f729Sjoerg LLVM_DEBUG(
20927330f729Sjoerg dbgs()
20937330f729Sjoerg << "%r7 is non-saved low register, adding to AvailableRegs\n");
20947330f729Sjoerg }
20957330f729Sjoerg }
20967330f729Sjoerg
20977330f729Sjoerg // Each of r8-r11 needs to be copied to a low register, then pushed.
20987330f729Sjoerg for (unsigned Reg : {ARM::R8, ARM::R9, ARM::R10, ARM::R11}) {
20997330f729Sjoerg if (SavedRegs.test(Reg)) {
21007330f729Sjoerg ++RegDeficit;
21017330f729Sjoerg LLVM_DEBUG(dbgs() << printReg(Reg, TRI)
21027330f729Sjoerg << " is saved high register, RegDeficit = "
21037330f729Sjoerg << RegDeficit << "\n");
21047330f729Sjoerg }
21057330f729Sjoerg }
21067330f729Sjoerg
21077330f729Sjoerg // LR can only be used by PUSH, not POP, and can't be used at all if the
21087330f729Sjoerg // llvm.returnaddress intrinsic is used. This is only worth doing if we
21097330f729Sjoerg // are more limited at function entry than exit.
21107330f729Sjoerg if ((EntryRegDeficit > ExitRegDeficit) &&
21117330f729Sjoerg !(MF.getRegInfo().isLiveIn(ARM::LR) &&
21127330f729Sjoerg MF.getFrameInfo().isReturnAddressTaken())) {
21137330f729Sjoerg if (SavedRegs.test(ARM::LR)) {
21147330f729Sjoerg --RegDeficit;
21157330f729Sjoerg LLVM_DEBUG(dbgs() << "%lr is saved register, RegDeficit = "
21167330f729Sjoerg << RegDeficit << "\n");
21177330f729Sjoerg } else {
21187330f729Sjoerg AvailableRegs.push_back(ARM::LR);
21197330f729Sjoerg LLVM_DEBUG(dbgs() << "%lr is not saved, adding to AvailableRegs\n");
21207330f729Sjoerg }
21217330f729Sjoerg }
21227330f729Sjoerg
21237330f729Sjoerg // If there are more high registers that need pushing than low registers
21247330f729Sjoerg // available, push some more low registers so that we can use fewer push
21257330f729Sjoerg // instructions. This might not reduce RegDeficit all the way to zero,
21267330f729Sjoerg // because we can only guarantee that r4-r6 are available, but r8-r11 may
21277330f729Sjoerg // need saving.
21287330f729Sjoerg LLVM_DEBUG(dbgs() << "Final RegDeficit = " << RegDeficit << "\n");
21297330f729Sjoerg for (; RegDeficit > 0 && !AvailableRegs.empty(); --RegDeficit) {
21307330f729Sjoerg unsigned Reg = AvailableRegs.pop_back_val();
21317330f729Sjoerg LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
21327330f729Sjoerg << " to make up reg deficit\n");
21337330f729Sjoerg SavedRegs.set(Reg);
21347330f729Sjoerg NumGPRSpills++;
21357330f729Sjoerg CS1Spilled = true;
21367330f729Sjoerg assert(!MRI.isReserved(Reg) && "Should not be reserved");
21377330f729Sjoerg if (Reg != ARM::LR && !MRI.isPhysRegUsed(Reg))
21387330f729Sjoerg ExtraCSSpill = true;
21397330f729Sjoerg UnspilledCS1GPRs.erase(llvm::find(UnspilledCS1GPRs, Reg));
21407330f729Sjoerg if (Reg == ARM::LR)
21417330f729Sjoerg LRSpilled = true;
21427330f729Sjoerg }
21437330f729Sjoerg LLVM_DEBUG(dbgs() << "After adding spills, RegDeficit = " << RegDeficit
21447330f729Sjoerg << "\n");
21457330f729Sjoerg }
21467330f729Sjoerg
21477330f729Sjoerg // Avoid spilling LR in Thumb1 if there's a tail call: it's expensive to
21487330f729Sjoerg // restore LR in that case.
21497330f729Sjoerg bool ExpensiveLRRestore = AFI->isThumb1OnlyFunction() && MFI.hasTailCall();
21507330f729Sjoerg
21517330f729Sjoerg // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
21527330f729Sjoerg // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
21537330f729Sjoerg if (!LRSpilled && CS1Spilled && !ExpensiveLRRestore) {
21547330f729Sjoerg SavedRegs.set(ARM::LR);
21557330f729Sjoerg NumGPRSpills++;
21567330f729Sjoerg SmallVectorImpl<unsigned>::iterator LRPos;
21577330f729Sjoerg LRPos = llvm::find(UnspilledCS1GPRs, (unsigned)ARM::LR);
21587330f729Sjoerg if (LRPos != UnspilledCS1GPRs.end())
21597330f729Sjoerg UnspilledCS1GPRs.erase(LRPos);
21607330f729Sjoerg
21617330f729Sjoerg ForceLRSpill = false;
21627330f729Sjoerg if (!MRI.isReserved(ARM::LR) && !MRI.isPhysRegUsed(ARM::LR) &&
21637330f729Sjoerg !AFI->isThumb1OnlyFunction())
21647330f729Sjoerg ExtraCSSpill = true;
21657330f729Sjoerg }
21667330f729Sjoerg
21677330f729Sjoerg // If stack and double are 8-byte aligned and we are spilling an odd number
21687330f729Sjoerg // of GPRs, spill one extra callee save GPR so we won't have to pad between
21697330f729Sjoerg // the integer and double callee save areas.
21707330f729Sjoerg LLVM_DEBUG(dbgs() << "NumGPRSpills = " << NumGPRSpills << "\n");
2171*82d56013Sjoerg const Align TargetAlign = getStackAlign();
2172*82d56013Sjoerg if (TargetAlign >= Align(8) && (NumGPRSpills & 1)) {
21737330f729Sjoerg if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
21747330f729Sjoerg for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
21757330f729Sjoerg unsigned Reg = UnspilledCS1GPRs[i];
21767330f729Sjoerg // Don't spill high register if the function is thumb. In the case of
21777330f729Sjoerg // Windows on ARM, accept R11 (frame pointer)
21787330f729Sjoerg if (!AFI->isThumbFunction() ||
21797330f729Sjoerg (STI.isTargetWindows() && Reg == ARM::R11) ||
21807330f729Sjoerg isARMLowRegister(Reg) ||
21817330f729Sjoerg (Reg == ARM::LR && !ExpensiveLRRestore)) {
21827330f729Sjoerg SavedRegs.set(Reg);
21837330f729Sjoerg LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
21847330f729Sjoerg << " to make up alignment\n");
21857330f729Sjoerg if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg) &&
21867330f729Sjoerg !(Reg == ARM::LR && AFI->isThumb1OnlyFunction()))
21877330f729Sjoerg ExtraCSSpill = true;
21887330f729Sjoerg break;
21897330f729Sjoerg }
21907330f729Sjoerg }
21917330f729Sjoerg } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
21927330f729Sjoerg unsigned Reg = UnspilledCS2GPRs.front();
21937330f729Sjoerg SavedRegs.set(Reg);
21947330f729Sjoerg LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
21957330f729Sjoerg << " to make up alignment\n");
21967330f729Sjoerg if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
21977330f729Sjoerg ExtraCSSpill = true;
21987330f729Sjoerg }
21997330f729Sjoerg }
22007330f729Sjoerg
22017330f729Sjoerg // Estimate if we might need to scavenge a register at some point in order
22027330f729Sjoerg // to materialize a stack offset. If so, either spill one additional
22037330f729Sjoerg // callee-saved register or reserve a special spill slot to facilitate
22047330f729Sjoerg // register scavenging. Thumb1 needs a spill slot for stack pointer
22057330f729Sjoerg // adjustments also, even when the frame itself is small.
22067330f729Sjoerg if (BigFrameOffsets && !ExtraCSSpill) {
22077330f729Sjoerg // If any non-reserved CS register isn't spilled, just spill one or two
22087330f729Sjoerg // extra. That should take care of it!
2209*82d56013Sjoerg unsigned NumExtras = TargetAlign.value() / 4;
22107330f729Sjoerg SmallVector<unsigned, 2> Extras;
22117330f729Sjoerg while (NumExtras && !UnspilledCS1GPRs.empty()) {
2212*82d56013Sjoerg unsigned Reg = UnspilledCS1GPRs.pop_back_val();
22137330f729Sjoerg if (!MRI.isReserved(Reg) &&
22147330f729Sjoerg (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg))) {
22157330f729Sjoerg Extras.push_back(Reg);
22167330f729Sjoerg NumExtras--;
22177330f729Sjoerg }
22187330f729Sjoerg }
22197330f729Sjoerg // For non-Thumb1 functions, also check for hi-reg CS registers
22207330f729Sjoerg if (!AFI->isThumb1OnlyFunction()) {
22217330f729Sjoerg while (NumExtras && !UnspilledCS2GPRs.empty()) {
2222*82d56013Sjoerg unsigned Reg = UnspilledCS2GPRs.pop_back_val();
22237330f729Sjoerg if (!MRI.isReserved(Reg)) {
22247330f729Sjoerg Extras.push_back(Reg);
22257330f729Sjoerg NumExtras--;
22267330f729Sjoerg }
22277330f729Sjoerg }
22287330f729Sjoerg }
22297330f729Sjoerg if (NumExtras == 0) {
22307330f729Sjoerg for (unsigned Reg : Extras) {
22317330f729Sjoerg SavedRegs.set(Reg);
22327330f729Sjoerg if (!MRI.isPhysRegUsed(Reg))
22337330f729Sjoerg ExtraCSSpill = true;
22347330f729Sjoerg }
22357330f729Sjoerg }
22367330f729Sjoerg if (!ExtraCSSpill && RS) {
22377330f729Sjoerg // Reserve a slot closest to SP or frame pointer.
22387330f729Sjoerg LLVM_DEBUG(dbgs() << "Reserving emergency spill slot\n");
22397330f729Sjoerg const TargetRegisterClass &RC = ARM::GPRRegClass;
22407330f729Sjoerg unsigned Size = TRI->getSpillSize(RC);
2241*82d56013Sjoerg Align Alignment = TRI->getSpillAlign(RC);
2242*82d56013Sjoerg RS->addScavengingFrameIndex(
2243*82d56013Sjoerg MFI.CreateStackObject(Size, Alignment, false));
22447330f729Sjoerg }
22457330f729Sjoerg }
22467330f729Sjoerg }
22477330f729Sjoerg
2248*82d56013Sjoerg if (ForceLRSpill)
22497330f729Sjoerg SavedRegs.set(ARM::LR);
22507330f729Sjoerg AFI->setLRIsSpilled(SavedRegs.test(ARM::LR));
22517330f729Sjoerg }
22527330f729Sjoerg
getCalleeSaves(const MachineFunction & MF,BitVector & SavedRegs) const22537330f729Sjoerg void ARMFrameLowering::getCalleeSaves(const MachineFunction &MF,
22547330f729Sjoerg BitVector &SavedRegs) const {
22557330f729Sjoerg TargetFrameLowering::getCalleeSaves(MF, SavedRegs);
22567330f729Sjoerg
22577330f729Sjoerg // If we have the "returned" parameter attribute which guarantees that we
22587330f729Sjoerg // return the value which was passed in r0 unmodified (e.g. C++ 'structors),
22597330f729Sjoerg // record that fact for IPRA.
22607330f729Sjoerg const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
22617330f729Sjoerg if (AFI->getPreservesR0())
22627330f729Sjoerg SavedRegs.set(ARM::R0);
22637330f729Sjoerg }
22647330f729Sjoerg
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const2265*82d56013Sjoerg bool ARMFrameLowering::assignCalleeSavedSpillSlots(
2266*82d56013Sjoerg MachineFunction &MF, const TargetRegisterInfo *TRI,
2267*82d56013Sjoerg std::vector<CalleeSavedInfo> &CSI) const {
2268*82d56013Sjoerg // For CMSE entry functions, handle floating-point context as if it was a
2269*82d56013Sjoerg // callee-saved register.
2270*82d56013Sjoerg if (STI.hasV8_1MMainlineOps() &&
2271*82d56013Sjoerg MF.getInfo<ARMFunctionInfo>()->isCmseNSEntryFunction()) {
2272*82d56013Sjoerg CSI.emplace_back(ARM::FPCXTNS);
2273*82d56013Sjoerg CSI.back().setRestored(false);
2274*82d56013Sjoerg }
2275*82d56013Sjoerg
2276*82d56013Sjoerg return false;
2277*82d56013Sjoerg }
2278*82d56013Sjoerg
2279*82d56013Sjoerg const TargetFrameLowering::SpillSlot *
getCalleeSavedSpillSlots(unsigned & NumEntries) const2280*82d56013Sjoerg ARMFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const {
2281*82d56013Sjoerg static const SpillSlot FixedSpillOffsets[] = {{ARM::FPCXTNS, -4}};
2282*82d56013Sjoerg NumEntries = array_lengthof(FixedSpillOffsets);
2283*82d56013Sjoerg return FixedSpillOffsets;
2284*82d56013Sjoerg }
2285*82d56013Sjoerg
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const22867330f729Sjoerg MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
22877330f729Sjoerg MachineFunction &MF, MachineBasicBlock &MBB,
22887330f729Sjoerg MachineBasicBlock::iterator I) const {
22897330f729Sjoerg const ARMBaseInstrInfo &TII =
22907330f729Sjoerg *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
22917330f729Sjoerg if (!hasReservedCallFrame(MF)) {
22927330f729Sjoerg // If we have alloca, convert as follows:
22937330f729Sjoerg // ADJCALLSTACKDOWN -> sub, sp, sp, amount
22947330f729Sjoerg // ADJCALLSTACKUP -> add, sp, sp, amount
22957330f729Sjoerg MachineInstr &Old = *I;
22967330f729Sjoerg DebugLoc dl = Old.getDebugLoc();
22977330f729Sjoerg unsigned Amount = TII.getFrameSize(Old);
22987330f729Sjoerg if (Amount != 0) {
22997330f729Sjoerg // We need to keep the stack aligned properly. To do this, we round the
23007330f729Sjoerg // amount of space needed for the outgoing arguments up to the next
23017330f729Sjoerg // alignment boundary.
23027330f729Sjoerg Amount = alignSPAdjust(Amount);
23037330f729Sjoerg
23047330f729Sjoerg ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
23057330f729Sjoerg assert(!AFI->isThumb1OnlyFunction() &&
23067330f729Sjoerg "This eliminateCallFramePseudoInstr does not support Thumb1!");
23077330f729Sjoerg bool isARM = !AFI->isThumbFunction();
23087330f729Sjoerg
23097330f729Sjoerg // Replace the pseudo instruction with a new instruction...
23107330f729Sjoerg unsigned Opc = Old.getOpcode();
23117330f729Sjoerg int PIdx = Old.findFirstPredOperandIdx();
23127330f729Sjoerg ARMCC::CondCodes Pred =
23137330f729Sjoerg (PIdx == -1) ? ARMCC::AL
23147330f729Sjoerg : (ARMCC::CondCodes)Old.getOperand(PIdx).getImm();
23157330f729Sjoerg unsigned PredReg = TII.getFramePred(Old);
23167330f729Sjoerg if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
23177330f729Sjoerg emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
23187330f729Sjoerg Pred, PredReg);
23197330f729Sjoerg } else {
23207330f729Sjoerg assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
23217330f729Sjoerg emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
23227330f729Sjoerg Pred, PredReg);
23237330f729Sjoerg }
23247330f729Sjoerg }
23257330f729Sjoerg }
23267330f729Sjoerg return MBB.erase(I);
23277330f729Sjoerg }
23287330f729Sjoerg
23297330f729Sjoerg /// Get the minimum constant for ARM that is greater than or equal to the
23307330f729Sjoerg /// argument. In ARM, constants can have any value that can be produced by
23317330f729Sjoerg /// rotating an 8-bit value to the right by an even number of bits within a
23327330f729Sjoerg /// 32-bit word.
alignToARMConstant(uint32_t Value)23337330f729Sjoerg static uint32_t alignToARMConstant(uint32_t Value) {
23347330f729Sjoerg unsigned Shifted = 0;
23357330f729Sjoerg
23367330f729Sjoerg if (Value == 0)
23377330f729Sjoerg return 0;
23387330f729Sjoerg
23397330f729Sjoerg while (!(Value & 0xC0000000)) {
23407330f729Sjoerg Value = Value << 2;
23417330f729Sjoerg Shifted += 2;
23427330f729Sjoerg }
23437330f729Sjoerg
23447330f729Sjoerg bool Carry = (Value & 0x00FFFFFF);
23457330f729Sjoerg Value = ((Value & 0xFF000000) >> 24) + Carry;
23467330f729Sjoerg
23477330f729Sjoerg if (Value & 0x0000100)
23487330f729Sjoerg Value = Value & 0x000001FC;
23497330f729Sjoerg
23507330f729Sjoerg if (Shifted > 24)
23517330f729Sjoerg Value = Value >> (Shifted - 24);
23527330f729Sjoerg else
23537330f729Sjoerg Value = Value << (24 - Shifted);
23547330f729Sjoerg
23557330f729Sjoerg return Value;
23567330f729Sjoerg }
23577330f729Sjoerg
23587330f729Sjoerg // The stack limit in the TCB is set to this many bytes above the actual
23597330f729Sjoerg // stack limit.
23607330f729Sjoerg static const uint64_t kSplitStackAvailable = 256;
23617330f729Sjoerg
23627330f729Sjoerg // Adjust the function prologue to enable split stacks. This currently only
23637330f729Sjoerg // supports android and linux.
23647330f729Sjoerg //
23657330f729Sjoerg // The ABI of the segmented stack prologue is a little arbitrarily chosen, but
23667330f729Sjoerg // must be well defined in order to allow for consistent implementations of the
23677330f729Sjoerg // __morestack helper function. The ABI is also not a normal ABI in that it
23687330f729Sjoerg // doesn't follow the normal calling conventions because this allows the
23697330f729Sjoerg // prologue of each function to be optimized further.
23707330f729Sjoerg //
23717330f729Sjoerg // Currently, the ABI looks like (when calling __morestack)
23727330f729Sjoerg //
23737330f729Sjoerg // * r4 holds the minimum stack size requested for this function call
23747330f729Sjoerg // * r5 holds the stack size of the arguments to the function
23757330f729Sjoerg // * the beginning of the function is 3 instructions after the call to
23767330f729Sjoerg // __morestack
23777330f729Sjoerg //
23787330f729Sjoerg // Implementations of __morestack should use r4 to allocate a new stack, r5 to
23797330f729Sjoerg // place the arguments on to the new stack, and the 3-instruction knowledge to
23807330f729Sjoerg // jump directly to the body of the function when working on the new stack.
23817330f729Sjoerg //
23827330f729Sjoerg // An old (and possibly no longer compatible) implementation of __morestack for
23837330f729Sjoerg // ARM can be found at [1].
23847330f729Sjoerg //
23857330f729Sjoerg // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
adjustForSegmentedStacks(MachineFunction & MF,MachineBasicBlock & PrologueMBB) const23867330f729Sjoerg void ARMFrameLowering::adjustForSegmentedStacks(
23877330f729Sjoerg MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
23887330f729Sjoerg unsigned Opcode;
23897330f729Sjoerg unsigned CFIIndex;
23907330f729Sjoerg const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
23917330f729Sjoerg bool Thumb = ST->isThumb();
23927330f729Sjoerg
23937330f729Sjoerg // Sadly, this currently doesn't support varargs, platforms other than
23947330f729Sjoerg // android/linux. Note that thumb1/thumb2 are support for android/linux.
23957330f729Sjoerg if (MF.getFunction().isVarArg())
23967330f729Sjoerg report_fatal_error("Segmented stacks do not support vararg functions.");
23977330f729Sjoerg if (!ST->isTargetAndroid() && !ST->isTargetLinux())
23987330f729Sjoerg report_fatal_error("Segmented stacks not supported on this platform.");
23997330f729Sjoerg
24007330f729Sjoerg MachineFrameInfo &MFI = MF.getFrameInfo();
24017330f729Sjoerg MachineModuleInfo &MMI = MF.getMMI();
24027330f729Sjoerg MCContext &Context = MMI.getContext();
24037330f729Sjoerg const MCRegisterInfo *MRI = Context.getRegisterInfo();
24047330f729Sjoerg const ARMBaseInstrInfo &TII =
24057330f729Sjoerg *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
24067330f729Sjoerg ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
24077330f729Sjoerg DebugLoc DL;
24087330f729Sjoerg
24097330f729Sjoerg uint64_t StackSize = MFI.getStackSize();
24107330f729Sjoerg
24117330f729Sjoerg // Do not generate a prologue for leaf functions with a stack of size zero.
24127330f729Sjoerg // For non-leaf functions we have to allow for the possibility that the
24137330f729Sjoerg // callis to a non-split function, as in PR37807. This function could also
24147330f729Sjoerg // take the address of a non-split function. When the linker tries to adjust
24157330f729Sjoerg // its non-existent prologue, it would fail with an error. Mark the object
24167330f729Sjoerg // file so that such failures are not errors. See this Go language bug-report
24177330f729Sjoerg // https://go-review.googlesource.com/c/go/+/148819/
24187330f729Sjoerg if (StackSize == 0 && !MFI.hasTailCall()) {
24197330f729Sjoerg MF.getMMI().setHasNosplitStack(true);
24207330f729Sjoerg return;
24217330f729Sjoerg }
24227330f729Sjoerg
24237330f729Sjoerg // Use R4 and R5 as scratch registers.
24247330f729Sjoerg // We save R4 and R5 before use and restore them before leaving the function.
24257330f729Sjoerg unsigned ScratchReg0 = ARM::R4;
24267330f729Sjoerg unsigned ScratchReg1 = ARM::R5;
24277330f729Sjoerg uint64_t AlignedStackSize;
24287330f729Sjoerg
24297330f729Sjoerg MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
24307330f729Sjoerg MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
24317330f729Sjoerg MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
24327330f729Sjoerg MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
24337330f729Sjoerg MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
24347330f729Sjoerg
24357330f729Sjoerg // Grab everything that reaches PrologueMBB to update there liveness as well.
24367330f729Sjoerg SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
24377330f729Sjoerg SmallVector<MachineBasicBlock *, 2> WalkList;
24387330f729Sjoerg WalkList.push_back(&PrologueMBB);
24397330f729Sjoerg
24407330f729Sjoerg do {
24417330f729Sjoerg MachineBasicBlock *CurMBB = WalkList.pop_back_val();
24427330f729Sjoerg for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
24437330f729Sjoerg if (BeforePrologueRegion.insert(PredBB).second)
24447330f729Sjoerg WalkList.push_back(PredBB);
24457330f729Sjoerg }
24467330f729Sjoerg } while (!WalkList.empty());
24477330f729Sjoerg
24487330f729Sjoerg // The order in that list is important.
24497330f729Sjoerg // The blocks will all be inserted before PrologueMBB using that order.
24507330f729Sjoerg // Therefore the block that should appear first in the CFG should appear
24517330f729Sjoerg // first in the list.
24527330f729Sjoerg MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
24537330f729Sjoerg PostStackMBB};
24547330f729Sjoerg
24557330f729Sjoerg for (MachineBasicBlock *B : AddedBlocks)
24567330f729Sjoerg BeforePrologueRegion.insert(B);
24577330f729Sjoerg
24587330f729Sjoerg for (const auto &LI : PrologueMBB.liveins()) {
24597330f729Sjoerg for (MachineBasicBlock *PredBB : BeforePrologueRegion)
24607330f729Sjoerg PredBB->addLiveIn(LI);
24617330f729Sjoerg }
24627330f729Sjoerg
24637330f729Sjoerg // Remove the newly added blocks from the list, since we know
24647330f729Sjoerg // we do not have to do the following updates for them.
24657330f729Sjoerg for (MachineBasicBlock *B : AddedBlocks) {
24667330f729Sjoerg BeforePrologueRegion.erase(B);
24677330f729Sjoerg MF.insert(PrologueMBB.getIterator(), B);
24687330f729Sjoerg }
24697330f729Sjoerg
24707330f729Sjoerg for (MachineBasicBlock *MBB : BeforePrologueRegion) {
24717330f729Sjoerg // Make sure the LiveIns are still sorted and unique.
24727330f729Sjoerg MBB->sortUniqueLiveIns();
24737330f729Sjoerg // Replace the edges to PrologueMBB by edges to the sequences
24747330f729Sjoerg // we are about to add.
24757330f729Sjoerg MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
24767330f729Sjoerg }
24777330f729Sjoerg
24787330f729Sjoerg // The required stack size that is aligned to ARM constant criterion.
24797330f729Sjoerg AlignedStackSize = alignToARMConstant(StackSize);
24807330f729Sjoerg
24817330f729Sjoerg // When the frame size is less than 256 we just compare the stack
24827330f729Sjoerg // boundary directly to the value of the stack pointer, per gcc.
24837330f729Sjoerg bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
24847330f729Sjoerg
24857330f729Sjoerg // We will use two of the callee save registers as scratch registers so we
24867330f729Sjoerg // need to save those registers onto the stack.
24877330f729Sjoerg // We will use SR0 to hold stack limit and SR1 to hold the stack size
24887330f729Sjoerg // requested and arguments for __morestack().
24897330f729Sjoerg // SR0: Scratch Register #0
24907330f729Sjoerg // SR1: Scratch Register #1
24917330f729Sjoerg // push {SR0, SR1}
24927330f729Sjoerg if (Thumb) {
24937330f729Sjoerg BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))
24947330f729Sjoerg .add(predOps(ARMCC::AL))
24957330f729Sjoerg .addReg(ScratchReg0)
24967330f729Sjoerg .addReg(ScratchReg1);
24977330f729Sjoerg } else {
24987330f729Sjoerg BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
24997330f729Sjoerg .addReg(ARM::SP, RegState::Define)
25007330f729Sjoerg .addReg(ARM::SP)
25017330f729Sjoerg .add(predOps(ARMCC::AL))
25027330f729Sjoerg .addReg(ScratchReg0)
25037330f729Sjoerg .addReg(ScratchReg1);
25047330f729Sjoerg }
25057330f729Sjoerg
25067330f729Sjoerg // Emit the relevant DWARF information about the change in stack pointer as
25077330f729Sjoerg // well as where to find both r4 and r5 (the callee-save registers)
2508*82d56013Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 8));
25097330f729Sjoerg BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
25107330f729Sjoerg .addCFIIndex(CFIIndex);
25117330f729Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
25127330f729Sjoerg nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
25137330f729Sjoerg BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
25147330f729Sjoerg .addCFIIndex(CFIIndex);
25157330f729Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
25167330f729Sjoerg nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
25177330f729Sjoerg BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
25187330f729Sjoerg .addCFIIndex(CFIIndex);
25197330f729Sjoerg
25207330f729Sjoerg // mov SR1, sp
25217330f729Sjoerg if (Thumb) {
25227330f729Sjoerg BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
25237330f729Sjoerg .addReg(ARM::SP)
25247330f729Sjoerg .add(predOps(ARMCC::AL));
25257330f729Sjoerg } else if (CompareStackPointer) {
25267330f729Sjoerg BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
25277330f729Sjoerg .addReg(ARM::SP)
25287330f729Sjoerg .add(predOps(ARMCC::AL))
25297330f729Sjoerg .add(condCodeOp());
25307330f729Sjoerg }
25317330f729Sjoerg
25327330f729Sjoerg // sub SR1, sp, #StackSize
25337330f729Sjoerg if (!CompareStackPointer && Thumb) {
25347330f729Sjoerg BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)
25357330f729Sjoerg .add(condCodeOp())
25367330f729Sjoerg .addReg(ScratchReg1)
25377330f729Sjoerg .addImm(AlignedStackSize)
25387330f729Sjoerg .add(predOps(ARMCC::AL));
25397330f729Sjoerg } else if (!CompareStackPointer) {
25407330f729Sjoerg BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
25417330f729Sjoerg .addReg(ARM::SP)
25427330f729Sjoerg .addImm(AlignedStackSize)
25437330f729Sjoerg .add(predOps(ARMCC::AL))
25447330f729Sjoerg .add(condCodeOp());
25457330f729Sjoerg }
25467330f729Sjoerg
25477330f729Sjoerg if (Thumb && ST->isThumb1Only()) {
25487330f729Sjoerg unsigned PCLabelId = ARMFI->createPICLabelUId();
25497330f729Sjoerg ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
25507330f729Sjoerg MF.getFunction().getContext(), "__STACK_LIMIT", PCLabelId, 0);
25517330f729Sjoerg MachineConstantPool *MCP = MF.getConstantPool();
2552*82d56013Sjoerg unsigned CPI = MCP->getConstantPoolIndex(NewCPV, Align(4));
25537330f729Sjoerg
25547330f729Sjoerg // ldr SR0, [pc, offset(STACK_LIMIT)]
25557330f729Sjoerg BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
25567330f729Sjoerg .addConstantPoolIndex(CPI)
25577330f729Sjoerg .add(predOps(ARMCC::AL));
25587330f729Sjoerg
25597330f729Sjoerg // ldr SR0, [SR0]
25607330f729Sjoerg BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
25617330f729Sjoerg .addReg(ScratchReg0)
25627330f729Sjoerg .addImm(0)
25637330f729Sjoerg .add(predOps(ARMCC::AL));
25647330f729Sjoerg } else {
25657330f729Sjoerg // Get TLS base address from the coprocessor
25667330f729Sjoerg // mrc p15, #0, SR0, c13, c0, #3
2567*82d56013Sjoerg BuildMI(McrMBB, DL, TII.get(Thumb ? ARM::t2MRC : ARM::MRC),
2568*82d56013Sjoerg ScratchReg0)
25697330f729Sjoerg .addImm(15)
25707330f729Sjoerg .addImm(0)
25717330f729Sjoerg .addImm(13)
25727330f729Sjoerg .addImm(0)
25737330f729Sjoerg .addImm(3)
25747330f729Sjoerg .add(predOps(ARMCC::AL));
25757330f729Sjoerg
25767330f729Sjoerg // Use the last tls slot on android and a private field of the TCP on linux.
25777330f729Sjoerg assert(ST->isTargetAndroid() || ST->isTargetLinux());
25787330f729Sjoerg unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
25797330f729Sjoerg
25807330f729Sjoerg // Get the stack limit from the right offset
25817330f729Sjoerg // ldr SR0, [sr0, #4 * TlsOffset]
2582*82d56013Sjoerg BuildMI(GetMBB, DL, TII.get(Thumb ? ARM::t2LDRi12 : ARM::LDRi12),
2583*82d56013Sjoerg ScratchReg0)
25847330f729Sjoerg .addReg(ScratchReg0)
25857330f729Sjoerg .addImm(4 * TlsOffset)
25867330f729Sjoerg .add(predOps(ARMCC::AL));
25877330f729Sjoerg }
25887330f729Sjoerg
25897330f729Sjoerg // Compare stack limit with stack size requested.
25907330f729Sjoerg // cmp SR0, SR1
25917330f729Sjoerg Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
25927330f729Sjoerg BuildMI(GetMBB, DL, TII.get(Opcode))
25937330f729Sjoerg .addReg(ScratchReg0)
25947330f729Sjoerg .addReg(ScratchReg1)
25957330f729Sjoerg .add(predOps(ARMCC::AL));
25967330f729Sjoerg
25977330f729Sjoerg // This jump is taken if StackLimit < SP - stack required.
25987330f729Sjoerg Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
25997330f729Sjoerg BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
26007330f729Sjoerg .addImm(ARMCC::LO)
26017330f729Sjoerg .addReg(ARM::CPSR);
26027330f729Sjoerg
26037330f729Sjoerg
26047330f729Sjoerg // Calling __morestack(StackSize, Size of stack arguments).
26057330f729Sjoerg // __morestack knows that the stack size requested is in SR0(r4)
26067330f729Sjoerg // and amount size of stack arguments is in SR1(r5).
26077330f729Sjoerg
26087330f729Sjoerg // Pass first argument for the __morestack by Scratch Register #0.
26097330f729Sjoerg // The amount size of stack required
26107330f729Sjoerg if (Thumb) {
26117330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg0)
26127330f729Sjoerg .add(condCodeOp())
26137330f729Sjoerg .addImm(AlignedStackSize)
26147330f729Sjoerg .add(predOps(ARMCC::AL));
26157330f729Sjoerg } else {
26167330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
26177330f729Sjoerg .addImm(AlignedStackSize)
26187330f729Sjoerg .add(predOps(ARMCC::AL))
26197330f729Sjoerg .add(condCodeOp());
26207330f729Sjoerg }
26217330f729Sjoerg // Pass second argument for the __morestack by Scratch Register #1.
26227330f729Sjoerg // The amount size of stack consumed to save function arguments.
26237330f729Sjoerg if (Thumb) {
26247330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)
26257330f729Sjoerg .add(condCodeOp())
26267330f729Sjoerg .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
26277330f729Sjoerg .add(predOps(ARMCC::AL));
26287330f729Sjoerg } else {
26297330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
26307330f729Sjoerg .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
26317330f729Sjoerg .add(predOps(ARMCC::AL))
26327330f729Sjoerg .add(condCodeOp());
26337330f729Sjoerg }
26347330f729Sjoerg
26357330f729Sjoerg // push {lr} - Save return address of this function.
26367330f729Sjoerg if (Thumb) {
26377330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))
26387330f729Sjoerg .add(predOps(ARMCC::AL))
26397330f729Sjoerg .addReg(ARM::LR);
26407330f729Sjoerg } else {
26417330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
26427330f729Sjoerg .addReg(ARM::SP, RegState::Define)
26437330f729Sjoerg .addReg(ARM::SP)
26447330f729Sjoerg .add(predOps(ARMCC::AL))
26457330f729Sjoerg .addReg(ARM::LR);
26467330f729Sjoerg }
26477330f729Sjoerg
26487330f729Sjoerg // Emit the DWARF info about the change in stack as well as where to find the
26497330f729Sjoerg // previous link register
2650*82d56013Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 12));
26517330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
26527330f729Sjoerg .addCFIIndex(CFIIndex);
26537330f729Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
26547330f729Sjoerg nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
26557330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
26567330f729Sjoerg .addCFIIndex(CFIIndex);
26577330f729Sjoerg
26587330f729Sjoerg // Call __morestack().
26597330f729Sjoerg if (Thumb) {
26607330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tBL))
26617330f729Sjoerg .add(predOps(ARMCC::AL))
26627330f729Sjoerg .addExternalSymbol("__morestack");
26637330f729Sjoerg } else {
26647330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::BL))
26657330f729Sjoerg .addExternalSymbol("__morestack");
26667330f729Sjoerg }
26677330f729Sjoerg
26687330f729Sjoerg // pop {lr} - Restore return address of this original function.
26697330f729Sjoerg if (Thumb) {
26707330f729Sjoerg if (ST->isThumb1Only()) {
26717330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
26727330f729Sjoerg .add(predOps(ARMCC::AL))
26737330f729Sjoerg .addReg(ScratchReg0);
26747330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
26757330f729Sjoerg .addReg(ScratchReg0)
26767330f729Sjoerg .add(predOps(ARMCC::AL));
26777330f729Sjoerg } else {
26787330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
26797330f729Sjoerg .addReg(ARM::LR, RegState::Define)
26807330f729Sjoerg .addReg(ARM::SP, RegState::Define)
26817330f729Sjoerg .addReg(ARM::SP)
26827330f729Sjoerg .addImm(4)
26837330f729Sjoerg .add(predOps(ARMCC::AL));
26847330f729Sjoerg }
26857330f729Sjoerg } else {
26867330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
26877330f729Sjoerg .addReg(ARM::SP, RegState::Define)
26887330f729Sjoerg .addReg(ARM::SP)
26897330f729Sjoerg .add(predOps(ARMCC::AL))
26907330f729Sjoerg .addReg(ARM::LR);
26917330f729Sjoerg }
26927330f729Sjoerg
26937330f729Sjoerg // Restore SR0 and SR1 in case of __morestack() was called.
26947330f729Sjoerg // __morestack() will skip PostStackMBB block so we need to restore
26957330f729Sjoerg // scratch registers from here.
26967330f729Sjoerg // pop {SR0, SR1}
26977330f729Sjoerg if (Thumb) {
26987330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
26997330f729Sjoerg .add(predOps(ARMCC::AL))
27007330f729Sjoerg .addReg(ScratchReg0)
27017330f729Sjoerg .addReg(ScratchReg1);
27027330f729Sjoerg } else {
27037330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
27047330f729Sjoerg .addReg(ARM::SP, RegState::Define)
27057330f729Sjoerg .addReg(ARM::SP)
27067330f729Sjoerg .add(predOps(ARMCC::AL))
27077330f729Sjoerg .addReg(ScratchReg0)
27087330f729Sjoerg .addReg(ScratchReg1);
27097330f729Sjoerg }
27107330f729Sjoerg
27117330f729Sjoerg // Update the CFA offset now that we've popped
2712*82d56013Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 0));
27137330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
27147330f729Sjoerg .addCFIIndex(CFIIndex);
27157330f729Sjoerg
27167330f729Sjoerg // Return from this function.
27177330f729Sjoerg BuildMI(AllocMBB, DL, TII.get(ST->getReturnOpcode())).add(predOps(ARMCC::AL));
27187330f729Sjoerg
27197330f729Sjoerg // Restore SR0 and SR1 in case of __morestack() was not called.
27207330f729Sjoerg // pop {SR0, SR1}
27217330f729Sjoerg if (Thumb) {
27227330f729Sjoerg BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))
27237330f729Sjoerg .add(predOps(ARMCC::AL))
27247330f729Sjoerg .addReg(ScratchReg0)
27257330f729Sjoerg .addReg(ScratchReg1);
27267330f729Sjoerg } else {
27277330f729Sjoerg BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
27287330f729Sjoerg .addReg(ARM::SP, RegState::Define)
27297330f729Sjoerg .addReg(ARM::SP)
27307330f729Sjoerg .add(predOps(ARMCC::AL))
27317330f729Sjoerg .addReg(ScratchReg0)
27327330f729Sjoerg .addReg(ScratchReg1);
27337330f729Sjoerg }
27347330f729Sjoerg
27357330f729Sjoerg // Update the CFA offset now that we've popped
2736*82d56013Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 0));
27377330f729Sjoerg BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
27387330f729Sjoerg .addCFIIndex(CFIIndex);
27397330f729Sjoerg
27407330f729Sjoerg // Tell debuggers that r4 and r5 are now the same as they were in the
27417330f729Sjoerg // previous function, that they're the "Same Value".
27427330f729Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
27437330f729Sjoerg nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
27447330f729Sjoerg BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
27457330f729Sjoerg .addCFIIndex(CFIIndex);
27467330f729Sjoerg CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
27477330f729Sjoerg nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
27487330f729Sjoerg BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
27497330f729Sjoerg .addCFIIndex(CFIIndex);
27507330f729Sjoerg
27517330f729Sjoerg // Organizing MBB lists
27527330f729Sjoerg PostStackMBB->addSuccessor(&PrologueMBB);
27537330f729Sjoerg
27547330f729Sjoerg AllocMBB->addSuccessor(PostStackMBB);
27557330f729Sjoerg
27567330f729Sjoerg GetMBB->addSuccessor(PostStackMBB);
27577330f729Sjoerg GetMBB->addSuccessor(AllocMBB);
27587330f729Sjoerg
27597330f729Sjoerg McrMBB->addSuccessor(GetMBB);
27607330f729Sjoerg
27617330f729Sjoerg PrevStackMBB->addSuccessor(McrMBB);
27627330f729Sjoerg
27637330f729Sjoerg #ifdef EXPENSIVE_CHECKS
27647330f729Sjoerg MF.verify();
27657330f729Sjoerg #endif
27667330f729Sjoerg }
2767