1f4a2713aSLionel Sambuc //===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains the ARM implementation of TargetFrameLowering class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "ARMFrameLowering.h"
15f4a2713aSLionel Sambuc #include "ARMBaseInstrInfo.h"
16f4a2713aSLionel Sambuc #include "ARMBaseRegisterInfo.h"
17*0a6a1f1dSLionel Sambuc #include "ARMConstantPoolValue.h"
18f4a2713aSLionel Sambuc #include "ARMMachineFunctionInfo.h"
19f4a2713aSLionel Sambuc #include "MCTargetDesc/ARMAddressingModes.h"
20f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h"
21f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunction.h"
22f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineModuleInfo.h"
24f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
25f4a2713aSLionel Sambuc #include "llvm/CodeGen/RegisterScavenging.h"
26f4a2713aSLionel Sambuc #include "llvm/IR/CallingConv.h"
27f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCContext.h"
29f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
30f4a2713aSLionel Sambuc #include "llvm/Target/TargetOptions.h"
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc using namespace llvm;
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc static cl::opt<bool>
35f4a2713aSLionel Sambuc SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
36f4a2713aSLionel Sambuc cl::desc("Align ARM NEON spills in prolog and epilog"));
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc static MachineBasicBlock::iterator
39f4a2713aSLionel Sambuc skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
40f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs);
41f4a2713aSLionel Sambuc
ARMFrameLowering(const ARMSubtarget & sti)42*0a6a1f1dSLionel Sambuc ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
43*0a6a1f1dSLionel Sambuc : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
44*0a6a1f1dSLionel Sambuc STI(sti) {}
45*0a6a1f1dSLionel Sambuc
46f4a2713aSLionel Sambuc /// hasFP - Return true if the specified function should have a dedicated frame
47f4a2713aSLionel Sambuc /// pointer register. This is true if the function has variable sized allocas
48f4a2713aSLionel Sambuc /// or if frame pointer elimination is disabled.
hasFP(const MachineFunction & MF) const49f4a2713aSLionel Sambuc bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
50*0a6a1f1dSLionel Sambuc const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc // iOS requires FP not to be clobbered for backtracing purpose.
53f4a2713aSLionel Sambuc if (STI.isTargetIOS())
54f4a2713aSLionel Sambuc return true;
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc const MachineFrameInfo *MFI = MF.getFrameInfo();
57f4a2713aSLionel Sambuc // Always eliminate non-leaf frame pointers.
58f4a2713aSLionel Sambuc return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
59f4a2713aSLionel Sambuc MFI->hasCalls()) ||
60f4a2713aSLionel Sambuc RegInfo->needsStackRealignment(MF) ||
61f4a2713aSLionel Sambuc MFI->hasVarSizedObjects() ||
62f4a2713aSLionel Sambuc MFI->isFrameAddressTaken());
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc
65f4a2713aSLionel Sambuc /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
66f4a2713aSLionel Sambuc /// not required, we reserve argument space for call sites in the function
67f4a2713aSLionel Sambuc /// immediately on entry to the current function. This eliminates the need for
68f4a2713aSLionel Sambuc /// add/sub sp brackets around call sites. Returns true if the call frame is
69f4a2713aSLionel Sambuc /// included as part of the stack frame.
hasReservedCallFrame(const MachineFunction & MF) const70f4a2713aSLionel Sambuc bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
71f4a2713aSLionel Sambuc const MachineFrameInfo *FFI = MF.getFrameInfo();
72f4a2713aSLionel Sambuc unsigned CFSize = FFI->getMaxCallFrameSize();
73f4a2713aSLionel Sambuc // It's not always a good idea to include the call frame as part of the
74f4a2713aSLionel Sambuc // stack frame. ARM (especially Thumb) has small immediate offset to
75f4a2713aSLionel Sambuc // address the stack frame. So a large call frame can cause poor codegen
76f4a2713aSLionel Sambuc // and may even makes it impossible to scavenge a register.
77f4a2713aSLionel Sambuc if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
78f4a2713aSLionel Sambuc return false;
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc return !MF.getFrameInfo()->hasVarSizedObjects();
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc /// canSimplifyCallFramePseudos - If there is a reserved call frame, the
84f4a2713aSLionel Sambuc /// call frame pseudos can be simplified. Unlike most targets, having a FP
85f4a2713aSLionel Sambuc /// is not sufficient here since we still may reference some objects via SP
86f4a2713aSLionel Sambuc /// even when FP is available in Thumb2 mode.
87f4a2713aSLionel Sambuc bool
canSimplifyCallFramePseudos(const MachineFunction & MF) const88f4a2713aSLionel Sambuc ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
89f4a2713aSLionel Sambuc return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc
isCSRestore(MachineInstr * MI,const ARMBaseInstrInfo & TII,const MCPhysReg * CSRegs)92f4a2713aSLionel Sambuc static bool isCSRestore(MachineInstr *MI,
93f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII,
94*0a6a1f1dSLionel Sambuc const MCPhysReg *CSRegs) {
95f4a2713aSLionel Sambuc // Integer spill area is handled with "pop".
96f4a2713aSLionel Sambuc if (isPopOpcode(MI->getOpcode())) {
97f4a2713aSLionel Sambuc // The first two operands are predicates. The last two are
98f4a2713aSLionel Sambuc // imp-def and imp-use of SP. Check everything in between.
99f4a2713aSLionel Sambuc for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
100f4a2713aSLionel Sambuc if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
101f4a2713aSLionel Sambuc return false;
102f4a2713aSLionel Sambuc return true;
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
105f4a2713aSLionel Sambuc MI->getOpcode() == ARM::LDR_POST_REG ||
106f4a2713aSLionel Sambuc MI->getOpcode() == ARM::t2LDR_POST) &&
107f4a2713aSLionel Sambuc isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
108f4a2713aSLionel Sambuc MI->getOperand(1).getReg() == ARM::SP)
109f4a2713aSLionel Sambuc return true;
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc return false;
112f4a2713aSLionel Sambuc }
113f4a2713aSLionel Sambuc
emitRegPlusImmediate(bool isARM,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,DebugLoc dl,const ARMBaseInstrInfo & TII,unsigned DestReg,unsigned SrcReg,int NumBytes,unsigned MIFlags=MachineInstr::NoFlags,ARMCC::CondCodes Pred=ARMCC::AL,unsigned PredReg=0)114f4a2713aSLionel Sambuc static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB,
115f4a2713aSLionel Sambuc MachineBasicBlock::iterator &MBBI, DebugLoc dl,
116f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII, unsigned DestReg,
117f4a2713aSLionel Sambuc unsigned SrcReg, int NumBytes,
118f4a2713aSLionel Sambuc unsigned MIFlags = MachineInstr::NoFlags,
119f4a2713aSLionel Sambuc ARMCC::CondCodes Pred = ARMCC::AL,
120f4a2713aSLionel Sambuc unsigned PredReg = 0) {
121f4a2713aSLionel Sambuc if (isARM)
122f4a2713aSLionel Sambuc emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
123f4a2713aSLionel Sambuc Pred, PredReg, TII, MIFlags);
124f4a2713aSLionel Sambuc else
125f4a2713aSLionel Sambuc emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
126f4a2713aSLionel Sambuc Pred, PredReg, TII, MIFlags);
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
emitSPUpdate(bool isARM,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,DebugLoc dl,const ARMBaseInstrInfo & TII,int NumBytes,unsigned MIFlags=MachineInstr::NoFlags,ARMCC::CondCodes Pred=ARMCC::AL,unsigned PredReg=0)129f4a2713aSLionel Sambuc static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
130f4a2713aSLionel Sambuc MachineBasicBlock::iterator &MBBI, DebugLoc dl,
131f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII, int NumBytes,
132f4a2713aSLionel Sambuc unsigned MIFlags = MachineInstr::NoFlags,
133f4a2713aSLionel Sambuc ARMCC::CondCodes Pred = ARMCC::AL,
134f4a2713aSLionel Sambuc unsigned PredReg = 0) {
135f4a2713aSLionel Sambuc emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
136f4a2713aSLionel Sambuc MIFlags, Pred, PredReg);
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc
sizeOfSPAdjustment(const MachineInstr * MI)139*0a6a1f1dSLionel Sambuc static int sizeOfSPAdjustment(const MachineInstr *MI) {
140*0a6a1f1dSLionel Sambuc int RegSize;
141*0a6a1f1dSLionel Sambuc switch (MI->getOpcode()) {
142*0a6a1f1dSLionel Sambuc case ARM::VSTMDDB_UPD:
143*0a6a1f1dSLionel Sambuc RegSize = 8;
144*0a6a1f1dSLionel Sambuc break;
145*0a6a1f1dSLionel Sambuc case ARM::STMDB_UPD:
146*0a6a1f1dSLionel Sambuc case ARM::t2STMDB_UPD:
147*0a6a1f1dSLionel Sambuc RegSize = 4;
148*0a6a1f1dSLionel Sambuc break;
149*0a6a1f1dSLionel Sambuc case ARM::t2STR_PRE:
150*0a6a1f1dSLionel Sambuc case ARM::STR_PRE_IMM:
151*0a6a1f1dSLionel Sambuc return 4;
152*0a6a1f1dSLionel Sambuc default:
153*0a6a1f1dSLionel Sambuc llvm_unreachable("Unknown push or pop like instruction");
154*0a6a1f1dSLionel Sambuc }
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc int count = 0;
157*0a6a1f1dSLionel Sambuc // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
158*0a6a1f1dSLionel Sambuc // pred) so the list starts at 4.
159*0a6a1f1dSLionel Sambuc for (int i = MI->getNumOperands() - 1; i >= 4; --i)
160*0a6a1f1dSLionel Sambuc count += RegSize;
161*0a6a1f1dSLionel Sambuc return count;
162*0a6a1f1dSLionel Sambuc }
163*0a6a1f1dSLionel Sambuc
WindowsRequiresStackProbe(const MachineFunction & MF,size_t StackSizeInBytes)164*0a6a1f1dSLionel Sambuc static bool WindowsRequiresStackProbe(const MachineFunction &MF,
165*0a6a1f1dSLionel Sambuc size_t StackSizeInBytes) {
166*0a6a1f1dSLionel Sambuc const MachineFrameInfo *MFI = MF.getFrameInfo();
167*0a6a1f1dSLionel Sambuc if (MFI->getStackProtectorIndex() > 0)
168*0a6a1f1dSLionel Sambuc return StackSizeInBytes >= 4080;
169*0a6a1f1dSLionel Sambuc return StackSizeInBytes >= 4096;
170*0a6a1f1dSLionel Sambuc }
171*0a6a1f1dSLionel Sambuc
172*0a6a1f1dSLionel Sambuc namespace {
173*0a6a1f1dSLionel Sambuc struct StackAdjustingInsts {
174*0a6a1f1dSLionel Sambuc struct InstInfo {
175*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator I;
176*0a6a1f1dSLionel Sambuc unsigned SPAdjust;
177*0a6a1f1dSLionel Sambuc bool BeforeFPSet;
178*0a6a1f1dSLionel Sambuc };
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc SmallVector<InstInfo, 4> Insts;
181*0a6a1f1dSLionel Sambuc
addInst__anon6fbbd19a0111::StackAdjustingInsts182*0a6a1f1dSLionel Sambuc void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
183*0a6a1f1dSLionel Sambuc bool BeforeFPSet = false) {
184*0a6a1f1dSLionel Sambuc InstInfo Info = {I, SPAdjust, BeforeFPSet};
185*0a6a1f1dSLionel Sambuc Insts.push_back(Info);
186*0a6a1f1dSLionel Sambuc }
187*0a6a1f1dSLionel Sambuc
addExtraBytes__anon6fbbd19a0111::StackAdjustingInsts188*0a6a1f1dSLionel Sambuc void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
189*0a6a1f1dSLionel Sambuc auto Info = std::find_if(Insts.begin(), Insts.end(),
190*0a6a1f1dSLionel Sambuc [&](InstInfo &Info) { return Info.I == I; });
191*0a6a1f1dSLionel Sambuc assert(Info != Insts.end() && "invalid sp adjusting instruction");
192*0a6a1f1dSLionel Sambuc Info->SPAdjust += ExtraBytes;
193*0a6a1f1dSLionel Sambuc }
194*0a6a1f1dSLionel Sambuc
emitDefCFAOffsets__anon6fbbd19a0111::StackAdjustingInsts195*0a6a1f1dSLionel Sambuc void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
196*0a6a1f1dSLionel Sambuc DebugLoc dl, const ARMBaseInstrInfo &TII, bool HasFP) {
197*0a6a1f1dSLionel Sambuc unsigned CFAOffset = 0;
198*0a6a1f1dSLionel Sambuc for (auto &Info : Insts) {
199*0a6a1f1dSLionel Sambuc if (HasFP && !Info.BeforeFPSet)
200*0a6a1f1dSLionel Sambuc return;
201*0a6a1f1dSLionel Sambuc
202*0a6a1f1dSLionel Sambuc CFAOffset -= Info.SPAdjust;
203*0a6a1f1dSLionel Sambuc unsigned CFIIndex = MMI.addFrameInst(
204*0a6a1f1dSLionel Sambuc MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
205*0a6a1f1dSLionel Sambuc BuildMI(MBB, std::next(Info.I), dl,
206*0a6a1f1dSLionel Sambuc TII.get(TargetOpcode::CFI_INSTRUCTION))
207*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex)
208*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
209*0a6a1f1dSLionel Sambuc }
210*0a6a1f1dSLionel Sambuc }
211*0a6a1f1dSLionel Sambuc };
212*0a6a1f1dSLionel Sambuc }
213*0a6a1f1dSLionel Sambuc
214*0a6a1f1dSLionel Sambuc /// Emit an instruction sequence that will align the address in
215*0a6a1f1dSLionel Sambuc /// register Reg by zero-ing out the lower bits. For versions of the
216*0a6a1f1dSLionel Sambuc /// architecture that support Neon, this must be done in a single
217*0a6a1f1dSLionel Sambuc /// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
218*0a6a1f1dSLionel Sambuc /// single instruction. That function only gets called when optimizing
219*0a6a1f1dSLionel Sambuc /// spilling of D registers on a core with the Neon instruction set
220*0a6a1f1dSLionel Sambuc /// present.
emitAligningInstructions(MachineFunction & MF,ARMFunctionInfo * AFI,const TargetInstrInfo & TII,MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,DebugLoc DL,const unsigned Reg,const unsigned Alignment,const bool MustBeSingleInstruction)221*0a6a1f1dSLionel Sambuc static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
222*0a6a1f1dSLionel Sambuc const TargetInstrInfo &TII,
223*0a6a1f1dSLionel Sambuc MachineBasicBlock &MBB,
224*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator MBBI,
225*0a6a1f1dSLionel Sambuc DebugLoc DL, const unsigned Reg,
226*0a6a1f1dSLionel Sambuc const unsigned Alignment,
227*0a6a1f1dSLionel Sambuc const bool MustBeSingleInstruction) {
228*0a6a1f1dSLionel Sambuc const ARMSubtarget &AST = MF.getTarget().getSubtarget<ARMSubtarget>();
229*0a6a1f1dSLionel Sambuc const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
230*0a6a1f1dSLionel Sambuc const unsigned AlignMask = Alignment - 1;
231*0a6a1f1dSLionel Sambuc const unsigned NrBitsToZero = countTrailingZeros(Alignment);
232*0a6a1f1dSLionel Sambuc assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
233*0a6a1f1dSLionel Sambuc if (!AFI->isThumbFunction()) {
234*0a6a1f1dSLionel Sambuc // if the BFC instruction is available, use that to zero the lower
235*0a6a1f1dSLionel Sambuc // bits:
236*0a6a1f1dSLionel Sambuc // bfc Reg, #0, log2(Alignment)
237*0a6a1f1dSLionel Sambuc // otherwise use BIC, if the mask to zero the required number of bits
238*0a6a1f1dSLionel Sambuc // can be encoded in the bic immediate field
239*0a6a1f1dSLionel Sambuc // bic Reg, Reg, Alignment-1
240*0a6a1f1dSLionel Sambuc // otherwise, emit
241*0a6a1f1dSLionel Sambuc // lsr Reg, Reg, log2(Alignment)
242*0a6a1f1dSLionel Sambuc // lsl Reg, Reg, log2(Alignment)
243*0a6a1f1dSLionel Sambuc if (CanUseBFC) {
244*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
245*0a6a1f1dSLionel Sambuc .addReg(Reg, RegState::Kill)
246*0a6a1f1dSLionel Sambuc .addImm(~AlignMask));
247*0a6a1f1dSLionel Sambuc } else if (AlignMask <= 255) {
248*0a6a1f1dSLionel Sambuc AddDefaultCC(
249*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
250*0a6a1f1dSLionel Sambuc .addReg(Reg, RegState::Kill)
251*0a6a1f1dSLionel Sambuc .addImm(AlignMask)));
252*0a6a1f1dSLionel Sambuc } else {
253*0a6a1f1dSLionel Sambuc assert(!MustBeSingleInstruction &&
254*0a6a1f1dSLionel Sambuc "Shouldn't call emitAligningInstructions demanding a single "
255*0a6a1f1dSLionel Sambuc "instruction to be emitted for large stack alignment for a target "
256*0a6a1f1dSLionel Sambuc "without BFC.");
257*0a6a1f1dSLionel Sambuc AddDefaultCC(AddDefaultPred(
258*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
259*0a6a1f1dSLionel Sambuc .addReg(Reg, RegState::Kill)
260*0a6a1f1dSLionel Sambuc .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))));
261*0a6a1f1dSLionel Sambuc AddDefaultCC(AddDefaultPred(
262*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
263*0a6a1f1dSLionel Sambuc .addReg(Reg, RegState::Kill)
264*0a6a1f1dSLionel Sambuc .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))));
265*0a6a1f1dSLionel Sambuc }
266*0a6a1f1dSLionel Sambuc } else {
267*0a6a1f1dSLionel Sambuc // Since this is only reached for Thumb-2 targets, the BFC instruction
268*0a6a1f1dSLionel Sambuc // should always be available.
269*0a6a1f1dSLionel Sambuc assert(CanUseBFC);
270*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
271*0a6a1f1dSLionel Sambuc .addReg(Reg, RegState::Kill)
272*0a6a1f1dSLionel Sambuc .addImm(~AlignMask));
273*0a6a1f1dSLionel Sambuc }
274*0a6a1f1dSLionel Sambuc }
275*0a6a1f1dSLionel Sambuc
emitPrologue(MachineFunction & MF) const276f4a2713aSLionel Sambuc void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
277f4a2713aSLionel Sambuc MachineBasicBlock &MBB = MF.front();
278f4a2713aSLionel Sambuc MachineBasicBlock::iterator MBBI = MBB.begin();
279f4a2713aSLionel Sambuc MachineFrameInfo *MFI = MF.getFrameInfo();
280f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
281*0a6a1f1dSLionel Sambuc MachineModuleInfo &MMI = MF.getMMI();
282*0a6a1f1dSLionel Sambuc MCContext &Context = MMI.getContext();
283*0a6a1f1dSLionel Sambuc const TargetMachine &TM = MF.getTarget();
284*0a6a1f1dSLionel Sambuc const MCRegisterInfo *MRI = Context.getRegisterInfo();
285*0a6a1f1dSLionel Sambuc const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
286*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getRegisterInfo());
287*0a6a1f1dSLionel Sambuc const ARMBaseInstrInfo &TII = *static_cast<const ARMBaseInstrInfo *>(
288*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getInstrInfo());
289f4a2713aSLionel Sambuc assert(!AFI->isThumb1OnlyFunction() &&
290f4a2713aSLionel Sambuc "This emitPrologue does not support Thumb1!");
291f4a2713aSLionel Sambuc bool isARM = !AFI->isThumbFunction();
292*0a6a1f1dSLionel Sambuc unsigned Align =
293*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
294f4a2713aSLionel Sambuc unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
295f4a2713aSLionel Sambuc unsigned NumBytes = MFI->getStackSize();
296f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
297f4a2713aSLionel Sambuc DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
298f4a2713aSLionel Sambuc unsigned FramePtr = RegInfo->getFrameRegister(MF);
299f4a2713aSLionel Sambuc
300f4a2713aSLionel Sambuc // Determine the sizes of each callee-save spill areas and record which frame
301f4a2713aSLionel Sambuc // belongs to which callee-save spill areas.
302f4a2713aSLionel Sambuc unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
303f4a2713aSLionel Sambuc int FramePtrSpillFI = 0;
304f4a2713aSLionel Sambuc int D8SpillFI = 0;
305f4a2713aSLionel Sambuc
306f4a2713aSLionel Sambuc // All calls are tail calls in GHC calling conv, and functions have no
307f4a2713aSLionel Sambuc // prologue/epilogue.
308f4a2713aSLionel Sambuc if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
309f4a2713aSLionel Sambuc return;
310f4a2713aSLionel Sambuc
311*0a6a1f1dSLionel Sambuc StackAdjustingInsts DefCFAOffsetCandidates;
312*0a6a1f1dSLionel Sambuc
313*0a6a1f1dSLionel Sambuc // Allocate the vararg register save area.
314*0a6a1f1dSLionel Sambuc if (ArgRegsSaveSize) {
315f4a2713aSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
316f4a2713aSLionel Sambuc MachineInstr::FrameSetup);
317*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
318*0a6a1f1dSLionel Sambuc }
319f4a2713aSLionel Sambuc
320*0a6a1f1dSLionel Sambuc if (!AFI->hasStackFrame() &&
321*0a6a1f1dSLionel Sambuc (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
322*0a6a1f1dSLionel Sambuc if (NumBytes - ArgRegsSaveSize != 0) {
323*0a6a1f1dSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
324f4a2713aSLionel Sambuc MachineInstr::FrameSetup);
325*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(std::prev(MBBI),
326*0a6a1f1dSLionel Sambuc NumBytes - ArgRegsSaveSize, true);
327*0a6a1f1dSLionel Sambuc }
328f4a2713aSLionel Sambuc return;
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc
331*0a6a1f1dSLionel Sambuc // Determine spill area sizes.
332f4a2713aSLionel Sambuc for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
333f4a2713aSLionel Sambuc unsigned Reg = CSI[i].getReg();
334f4a2713aSLionel Sambuc int FI = CSI[i].getFrameIdx();
335f4a2713aSLionel Sambuc switch (Reg) {
336*0a6a1f1dSLionel Sambuc case ARM::R8:
337*0a6a1f1dSLionel Sambuc case ARM::R9:
338*0a6a1f1dSLionel Sambuc case ARM::R10:
339*0a6a1f1dSLionel Sambuc case ARM::R11:
340*0a6a1f1dSLionel Sambuc case ARM::R12:
341*0a6a1f1dSLionel Sambuc if (STI.isTargetDarwin()) {
342*0a6a1f1dSLionel Sambuc GPRCS2Size += 4;
343*0a6a1f1dSLionel Sambuc break;
344*0a6a1f1dSLionel Sambuc }
345*0a6a1f1dSLionel Sambuc // fallthrough
346f4a2713aSLionel Sambuc case ARM::R0:
347f4a2713aSLionel Sambuc case ARM::R1:
348f4a2713aSLionel Sambuc case ARM::R2:
349f4a2713aSLionel Sambuc case ARM::R3:
350f4a2713aSLionel Sambuc case ARM::R4:
351f4a2713aSLionel Sambuc case ARM::R5:
352f4a2713aSLionel Sambuc case ARM::R6:
353f4a2713aSLionel Sambuc case ARM::R7:
354f4a2713aSLionel Sambuc case ARM::LR:
355f4a2713aSLionel Sambuc if (Reg == FramePtr)
356f4a2713aSLionel Sambuc FramePtrSpillFI = FI;
357f4a2713aSLionel Sambuc GPRCS1Size += 4;
358f4a2713aSLionel Sambuc break;
359f4a2713aSLionel Sambuc default:
360f4a2713aSLionel Sambuc // This is a DPR. Exclude the aligned DPRCS2 spills.
361f4a2713aSLionel Sambuc if (Reg == ARM::D8)
362f4a2713aSLionel Sambuc D8SpillFI = FI;
363f4a2713aSLionel Sambuc if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
364f4a2713aSLionel Sambuc DPRCSSize += 8;
365f4a2713aSLionel Sambuc }
366f4a2713aSLionel Sambuc }
367f4a2713aSLionel Sambuc
368f4a2713aSLionel Sambuc // Move past area 1.
369*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
370*0a6a1f1dSLionel Sambuc if (GPRCS1Size > 0) {
371*0a6a1f1dSLionel Sambuc GPRCS1Push = LastPush = MBBI++;
372*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
373*0a6a1f1dSLionel Sambuc }
374f4a2713aSLionel Sambuc
375f4a2713aSLionel Sambuc // Determine starting offsets of spill areas.
376f4a2713aSLionel Sambuc bool HasFP = hasFP(MF);
377*0a6a1f1dSLionel Sambuc unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
378*0a6a1f1dSLionel Sambuc unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
379*0a6a1f1dSLionel Sambuc unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
380*0a6a1f1dSLionel Sambuc unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
381*0a6a1f1dSLionel Sambuc unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
382f4a2713aSLionel Sambuc int FramePtrOffsetInPush = 0;
383f4a2713aSLionel Sambuc if (HasFP) {
384*0a6a1f1dSLionel Sambuc FramePtrOffsetInPush =
385*0a6a1f1dSLionel Sambuc MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize;
386f4a2713aSLionel Sambuc AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
387f4a2713aSLionel Sambuc NumBytes);
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
390f4a2713aSLionel Sambuc AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
391f4a2713aSLionel Sambuc AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
392f4a2713aSLionel Sambuc
393f4a2713aSLionel Sambuc // Move past area 2.
394f4a2713aSLionel Sambuc if (GPRCS2Size > 0) {
395*0a6a1f1dSLionel Sambuc GPRCS2Push = LastPush = MBBI++;
396*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
397*0a6a1f1dSLionel Sambuc }
398*0a6a1f1dSLionel Sambuc
399*0a6a1f1dSLionel Sambuc // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
400*0a6a1f1dSLionel Sambuc // .cfi_offset operations will reflect that.
401*0a6a1f1dSLionel Sambuc if (DPRGapSize) {
402*0a6a1f1dSLionel Sambuc assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
403*0a6a1f1dSLionel Sambuc if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize))
404*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
405*0a6a1f1dSLionel Sambuc else {
406*0a6a1f1dSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
407*0a6a1f1dSLionel Sambuc MachineInstr::FrameSetup);
408*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
409*0a6a1f1dSLionel Sambuc }
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc
412f4a2713aSLionel Sambuc // Move past area 3.
413f4a2713aSLionel Sambuc if (DPRCSSize > 0) {
414f4a2713aSLionel Sambuc // Since vpush register list cannot have gaps, there may be multiple vpush
415f4a2713aSLionel Sambuc // instructions in the prologue.
416*0a6a1f1dSLionel Sambuc while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
417*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI));
418f4a2713aSLionel Sambuc LastPush = MBBI++;
419f4a2713aSLionel Sambuc }
420*0a6a1f1dSLionel Sambuc }
421f4a2713aSLionel Sambuc
422f4a2713aSLionel Sambuc // Move past the aligned DPRCS2 area.
423f4a2713aSLionel Sambuc if (AFI->getNumAlignedDPRCS2Regs() > 0) {
424f4a2713aSLionel Sambuc MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
425f4a2713aSLionel Sambuc // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
426f4a2713aSLionel Sambuc // leaves the stack pointer pointing to the DPRCS2 area.
427f4a2713aSLionel Sambuc //
428f4a2713aSLionel Sambuc // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
429f4a2713aSLionel Sambuc NumBytes += MFI->getObjectOffset(D8SpillFI);
430f4a2713aSLionel Sambuc } else
431f4a2713aSLionel Sambuc NumBytes = DPRCSOffset;
432f4a2713aSLionel Sambuc
433*0a6a1f1dSLionel Sambuc if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
434*0a6a1f1dSLionel Sambuc uint32_t NumWords = NumBytes >> 2;
435*0a6a1f1dSLionel Sambuc
436*0a6a1f1dSLionel Sambuc if (NumWords < 65536)
437*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
438*0a6a1f1dSLionel Sambuc .addImm(NumWords)
439*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup));
440*0a6a1f1dSLionel Sambuc else
441*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
442*0a6a1f1dSLionel Sambuc .addImm(NumWords)
443*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
444*0a6a1f1dSLionel Sambuc
445*0a6a1f1dSLionel Sambuc switch (TM.getCodeModel()) {
446*0a6a1f1dSLionel Sambuc case CodeModel::Small:
447*0a6a1f1dSLionel Sambuc case CodeModel::Medium:
448*0a6a1f1dSLionel Sambuc case CodeModel::Default:
449*0a6a1f1dSLionel Sambuc case CodeModel::Kernel:
450*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
451*0a6a1f1dSLionel Sambuc .addImm((unsigned)ARMCC::AL).addReg(0)
452*0a6a1f1dSLionel Sambuc .addExternalSymbol("__chkstk")
453*0a6a1f1dSLionel Sambuc .addReg(ARM::R4, RegState::Implicit)
454*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
455*0a6a1f1dSLionel Sambuc break;
456*0a6a1f1dSLionel Sambuc case CodeModel::Large:
457*0a6a1f1dSLionel Sambuc case CodeModel::JITDefault:
458*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
459*0a6a1f1dSLionel Sambuc .addExternalSymbol("__chkstk")
460*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
461*0a6a1f1dSLionel Sambuc
462*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
463*0a6a1f1dSLionel Sambuc .addImm((unsigned)ARMCC::AL).addReg(0)
464*0a6a1f1dSLionel Sambuc .addReg(ARM::R12, RegState::Kill)
465*0a6a1f1dSLionel Sambuc .addReg(ARM::R4, RegState::Implicit)
466*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
467*0a6a1f1dSLionel Sambuc break;
468*0a6a1f1dSLionel Sambuc }
469*0a6a1f1dSLionel Sambuc
470*0a6a1f1dSLionel Sambuc AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr),
471*0a6a1f1dSLionel Sambuc ARM::SP)
472*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define)
473*0a6a1f1dSLionel Sambuc .addReg(ARM::R4, RegState::Kill)
474*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup)));
475*0a6a1f1dSLionel Sambuc NumBytes = 0;
476*0a6a1f1dSLionel Sambuc }
477*0a6a1f1dSLionel Sambuc
478f4a2713aSLionel Sambuc if (NumBytes) {
479f4a2713aSLionel Sambuc // Adjust SP after all the callee-save spills.
480*0a6a1f1dSLionel Sambuc if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes))
481*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
482*0a6a1f1dSLionel Sambuc else {
483f4a2713aSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
484f4a2713aSLionel Sambuc MachineInstr::FrameSetup);
485*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
486*0a6a1f1dSLionel Sambuc }
487f4a2713aSLionel Sambuc
488f4a2713aSLionel Sambuc if (HasFP && isARM)
489f4a2713aSLionel Sambuc // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
490f4a2713aSLionel Sambuc // Note it's not safe to do this in Thumb2 mode because it would have
491f4a2713aSLionel Sambuc // taken two instructions:
492f4a2713aSLionel Sambuc // mov sp, r7
493f4a2713aSLionel Sambuc // sub sp, #24
494f4a2713aSLionel Sambuc // If an interrupt is taken between the two instructions, then sp is in
495f4a2713aSLionel Sambuc // an inconsistent state (pointing to the middle of callee-saved area).
496f4a2713aSLionel Sambuc // The interrupt handler can end up clobbering the registers.
497f4a2713aSLionel Sambuc AFI->setShouldRestoreSPFromFP(true);
498f4a2713aSLionel Sambuc }
499f4a2713aSLionel Sambuc
500f4a2713aSLionel Sambuc // Set FP to point to the stack slot that contains the previous FP.
501f4a2713aSLionel Sambuc // For iOS, FP is R7, which has now been stored in spill area 1.
502f4a2713aSLionel Sambuc // Otherwise, if this is not iOS, all the callee-saved registers go
503f4a2713aSLionel Sambuc // into spill area 1, including the FP in R11. In either case, it
504f4a2713aSLionel Sambuc // is in area one and the adjustment needs to take place just after
505f4a2713aSLionel Sambuc // that push.
506*0a6a1f1dSLionel Sambuc if (HasFP) {
507*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
508*0a6a1f1dSLionel Sambuc unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push);
509*0a6a1f1dSLionel Sambuc emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
510*0a6a1f1dSLionel Sambuc dl, TII, FramePtr, ARM::SP,
511*0a6a1f1dSLionel Sambuc PushSize + FramePtrOffsetInPush,
512f4a2713aSLionel Sambuc MachineInstr::FrameSetup);
513*0a6a1f1dSLionel Sambuc if (FramePtrOffsetInPush + PushSize != 0) {
514*0a6a1f1dSLionel Sambuc unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
515*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(FramePtr, true),
516*0a6a1f1dSLionel Sambuc -(ArgRegsSaveSize - FramePtrOffsetInPush)));
517*0a6a1f1dSLionel Sambuc BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
518*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex)
519*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
520*0a6a1f1dSLionel Sambuc } else {
521*0a6a1f1dSLionel Sambuc unsigned CFIIndex =
522*0a6a1f1dSLionel Sambuc MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
523*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(FramePtr, true)));
524*0a6a1f1dSLionel Sambuc BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
525*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex)
526*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
527*0a6a1f1dSLionel Sambuc }
528*0a6a1f1dSLionel Sambuc }
529f4a2713aSLionel Sambuc
530*0a6a1f1dSLionel Sambuc // Now that the prologue's actual instructions are finalised, we can insert
531*0a6a1f1dSLionel Sambuc // the necessary DWARF cf instructions to describe the situation. Start by
532*0a6a1f1dSLionel Sambuc // recording where each register ended up:
533*0a6a1f1dSLionel Sambuc if (GPRCS1Size > 0) {
534*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
535*0a6a1f1dSLionel Sambuc int CFIIndex;
536*0a6a1f1dSLionel Sambuc for (const auto &Entry : CSI) {
537*0a6a1f1dSLionel Sambuc unsigned Reg = Entry.getReg();
538*0a6a1f1dSLionel Sambuc int FI = Entry.getFrameIdx();
539*0a6a1f1dSLionel Sambuc switch (Reg) {
540*0a6a1f1dSLionel Sambuc case ARM::R8:
541*0a6a1f1dSLionel Sambuc case ARM::R9:
542*0a6a1f1dSLionel Sambuc case ARM::R10:
543*0a6a1f1dSLionel Sambuc case ARM::R11:
544*0a6a1f1dSLionel Sambuc case ARM::R12:
545*0a6a1f1dSLionel Sambuc if (STI.isTargetDarwin())
546*0a6a1f1dSLionel Sambuc break;
547*0a6a1f1dSLionel Sambuc // fallthrough
548*0a6a1f1dSLionel Sambuc case ARM::R0:
549*0a6a1f1dSLionel Sambuc case ARM::R1:
550*0a6a1f1dSLionel Sambuc case ARM::R2:
551*0a6a1f1dSLionel Sambuc case ARM::R3:
552*0a6a1f1dSLionel Sambuc case ARM::R4:
553*0a6a1f1dSLionel Sambuc case ARM::R5:
554*0a6a1f1dSLionel Sambuc case ARM::R6:
555*0a6a1f1dSLionel Sambuc case ARM::R7:
556*0a6a1f1dSLionel Sambuc case ARM::LR:
557*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
558*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
559*0a6a1f1dSLionel Sambuc BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
560*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex)
561*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
562*0a6a1f1dSLionel Sambuc break;
563*0a6a1f1dSLionel Sambuc }
564*0a6a1f1dSLionel Sambuc }
565*0a6a1f1dSLionel Sambuc }
566*0a6a1f1dSLionel Sambuc
567*0a6a1f1dSLionel Sambuc if (GPRCS2Size > 0) {
568*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
569*0a6a1f1dSLionel Sambuc for (const auto &Entry : CSI) {
570*0a6a1f1dSLionel Sambuc unsigned Reg = Entry.getReg();
571*0a6a1f1dSLionel Sambuc int FI = Entry.getFrameIdx();
572*0a6a1f1dSLionel Sambuc switch (Reg) {
573*0a6a1f1dSLionel Sambuc case ARM::R8:
574*0a6a1f1dSLionel Sambuc case ARM::R9:
575*0a6a1f1dSLionel Sambuc case ARM::R10:
576*0a6a1f1dSLionel Sambuc case ARM::R11:
577*0a6a1f1dSLionel Sambuc case ARM::R12:
578*0a6a1f1dSLionel Sambuc if (STI.isTargetDarwin()) {
579*0a6a1f1dSLionel Sambuc unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
580*0a6a1f1dSLionel Sambuc unsigned Offset = MFI->getObjectOffset(FI);
581*0a6a1f1dSLionel Sambuc unsigned CFIIndex = MMI.addFrameInst(
582*0a6a1f1dSLionel Sambuc MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
583*0a6a1f1dSLionel Sambuc BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
584*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex)
585*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
586*0a6a1f1dSLionel Sambuc }
587*0a6a1f1dSLionel Sambuc break;
588*0a6a1f1dSLionel Sambuc }
589*0a6a1f1dSLionel Sambuc }
590*0a6a1f1dSLionel Sambuc }
591*0a6a1f1dSLionel Sambuc
592*0a6a1f1dSLionel Sambuc if (DPRCSSize > 0) {
593*0a6a1f1dSLionel Sambuc // Since vpush register list cannot have gaps, there may be multiple vpush
594*0a6a1f1dSLionel Sambuc // instructions in the prologue.
595*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator Pos = std::next(LastPush);
596*0a6a1f1dSLionel Sambuc for (const auto &Entry : CSI) {
597*0a6a1f1dSLionel Sambuc unsigned Reg = Entry.getReg();
598*0a6a1f1dSLionel Sambuc int FI = Entry.getFrameIdx();
599*0a6a1f1dSLionel Sambuc if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
600*0a6a1f1dSLionel Sambuc (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
601*0a6a1f1dSLionel Sambuc unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
602*0a6a1f1dSLionel Sambuc unsigned Offset = MFI->getObjectOffset(FI);
603*0a6a1f1dSLionel Sambuc unsigned CFIIndex = MMI.addFrameInst(
604*0a6a1f1dSLionel Sambuc MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
605*0a6a1f1dSLionel Sambuc BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
606*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex)
607*0a6a1f1dSLionel Sambuc .setMIFlags(MachineInstr::FrameSetup);
608*0a6a1f1dSLionel Sambuc }
609*0a6a1f1dSLionel Sambuc }
610*0a6a1f1dSLionel Sambuc }
611*0a6a1f1dSLionel Sambuc
612*0a6a1f1dSLionel Sambuc // Now we can emit descriptions of where the canonical frame address was
613*0a6a1f1dSLionel Sambuc // throughout the process. If we have a frame pointer, it takes over the job
614*0a6a1f1dSLionel Sambuc // half-way through, so only the first few .cfi_def_cfa_offset instructions
615*0a6a1f1dSLionel Sambuc // actually get emitted.
616*0a6a1f1dSLionel Sambuc DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP);
617f4a2713aSLionel Sambuc
618f4a2713aSLionel Sambuc if (STI.isTargetELF() && hasFP(MF))
619f4a2713aSLionel Sambuc MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
620f4a2713aSLionel Sambuc AFI->getFramePtrSpillOffset());
621f4a2713aSLionel Sambuc
622f4a2713aSLionel Sambuc AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
623f4a2713aSLionel Sambuc AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
624*0a6a1f1dSLionel Sambuc AFI->setDPRCalleeSavedGapSize(DPRGapSize);
625f4a2713aSLionel Sambuc AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
626f4a2713aSLionel Sambuc
627f4a2713aSLionel Sambuc // If we need dynamic stack realignment, do it here. Be paranoid and make
628f4a2713aSLionel Sambuc // sure if we also have VLAs, we have a base pointer for frame access.
629f4a2713aSLionel Sambuc // If aligned NEON registers were spilled, the stack has already been
630f4a2713aSLionel Sambuc // realigned.
631f4a2713aSLionel Sambuc if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
632f4a2713aSLionel Sambuc unsigned MaxAlign = MFI->getMaxAlignment();
633f4a2713aSLionel Sambuc assert(!AFI->isThumb1OnlyFunction());
634f4a2713aSLionel Sambuc if (!AFI->isThumbFunction()) {
635*0a6a1f1dSLionel Sambuc emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
636*0a6a1f1dSLionel Sambuc false);
637f4a2713aSLionel Sambuc } else {
638*0a6a1f1dSLionel Sambuc // We cannot use sp as source/dest register here, thus we're using r4 to
639*0a6a1f1dSLionel Sambuc // perform the calculations. We're emitting the following sequence:
640f4a2713aSLionel Sambuc // mov r4, sp
641*0a6a1f1dSLionel Sambuc // -- use emitAligningInstructions to produce best sequence to zero
642*0a6a1f1dSLionel Sambuc // -- out lower bits in r4
643f4a2713aSLionel Sambuc // mov sp, r4
644f4a2713aSLionel Sambuc // FIXME: It will be better just to find spare register here.
645f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
646f4a2713aSLionel Sambuc .addReg(ARM::SP, RegState::Kill));
647*0a6a1f1dSLionel Sambuc emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
648*0a6a1f1dSLionel Sambuc false);
649f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
650f4a2713aSLionel Sambuc .addReg(ARM::R4, RegState::Kill));
651f4a2713aSLionel Sambuc }
652f4a2713aSLionel Sambuc
653f4a2713aSLionel Sambuc AFI->setShouldRestoreSPFromFP(true);
654f4a2713aSLionel Sambuc }
655f4a2713aSLionel Sambuc
656f4a2713aSLionel Sambuc // If we need a base pointer, set it up here. It's whatever the value
657f4a2713aSLionel Sambuc // of the stack pointer is at this point. Any variable size objects
658f4a2713aSLionel Sambuc // will be allocated after this, so we can still use the base pointer
659f4a2713aSLionel Sambuc // to reference locals.
660f4a2713aSLionel Sambuc // FIXME: Clarify FrameSetup flags here.
661f4a2713aSLionel Sambuc if (RegInfo->hasBasePointer(MF)) {
662f4a2713aSLionel Sambuc if (isARM)
663f4a2713aSLionel Sambuc BuildMI(MBB, MBBI, dl,
664f4a2713aSLionel Sambuc TII.get(ARM::MOVr), RegInfo->getBaseRegister())
665f4a2713aSLionel Sambuc .addReg(ARM::SP)
666f4a2713aSLionel Sambuc .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
667f4a2713aSLionel Sambuc else
668f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
669f4a2713aSLionel Sambuc RegInfo->getBaseRegister())
670f4a2713aSLionel Sambuc .addReg(ARM::SP));
671f4a2713aSLionel Sambuc }
672f4a2713aSLionel Sambuc
673f4a2713aSLionel Sambuc // If the frame has variable sized objects then the epilogue must restore
674f4a2713aSLionel Sambuc // the sp from fp. We can assume there's an FP here since hasFP already
675f4a2713aSLionel Sambuc // checks for hasVarSizedObjects.
676f4a2713aSLionel Sambuc if (MFI->hasVarSizedObjects())
677f4a2713aSLionel Sambuc AFI->setShouldRestoreSPFromFP(true);
678f4a2713aSLionel Sambuc }
679f4a2713aSLionel Sambuc
680*0a6a1f1dSLionel Sambuc // Resolve TCReturn pseudo-instruction
fixTCReturn(MachineFunction & MF,MachineBasicBlock & MBB) const681*0a6a1f1dSLionel Sambuc void ARMFrameLowering::fixTCReturn(MachineFunction &MF,
682f4a2713aSLionel Sambuc MachineBasicBlock &MBB) const {
683f4a2713aSLionel Sambuc MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
684f4a2713aSLionel Sambuc assert(MBBI->isReturn() && "Can only insert epilog into returning blocks");
685f4a2713aSLionel Sambuc unsigned RetOpcode = MBBI->getOpcode();
686f4a2713aSLionel Sambuc DebugLoc dl = MBBI->getDebugLoc();
687*0a6a1f1dSLionel Sambuc const ARMBaseInstrInfo &TII =
688*0a6a1f1dSLionel Sambuc *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
689*0a6a1f1dSLionel Sambuc
690*0a6a1f1dSLionel Sambuc if (!(RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri))
691*0a6a1f1dSLionel Sambuc return;
692*0a6a1f1dSLionel Sambuc
693*0a6a1f1dSLionel Sambuc // Tail call return: adjust the stack pointer and jump to callee.
694*0a6a1f1dSLionel Sambuc MBBI = MBB.getLastNonDebugInstr();
695*0a6a1f1dSLionel Sambuc MachineOperand &JumpTarget = MBBI->getOperand(0);
696*0a6a1f1dSLionel Sambuc
697*0a6a1f1dSLionel Sambuc // Jump to label or value in register.
698*0a6a1f1dSLionel Sambuc if (RetOpcode == ARM::TCRETURNdi) {
699*0a6a1f1dSLionel Sambuc unsigned TCOpcode = STI.isThumb() ?
700*0a6a1f1dSLionel Sambuc (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) :
701*0a6a1f1dSLionel Sambuc ARM::TAILJMPd;
702*0a6a1f1dSLionel Sambuc MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
703*0a6a1f1dSLionel Sambuc if (JumpTarget.isGlobal())
704*0a6a1f1dSLionel Sambuc MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
705*0a6a1f1dSLionel Sambuc JumpTarget.getTargetFlags());
706*0a6a1f1dSLionel Sambuc else {
707*0a6a1f1dSLionel Sambuc assert(JumpTarget.isSymbol());
708*0a6a1f1dSLionel Sambuc MIB.addExternalSymbol(JumpTarget.getSymbolName(),
709*0a6a1f1dSLionel Sambuc JumpTarget.getTargetFlags());
710*0a6a1f1dSLionel Sambuc }
711*0a6a1f1dSLionel Sambuc
712*0a6a1f1dSLionel Sambuc // Add the default predicate in Thumb mode.
713*0a6a1f1dSLionel Sambuc if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
714*0a6a1f1dSLionel Sambuc } else if (RetOpcode == ARM::TCRETURNri) {
715*0a6a1f1dSLionel Sambuc BuildMI(MBB, MBBI, dl,
716*0a6a1f1dSLionel Sambuc TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
717*0a6a1f1dSLionel Sambuc addReg(JumpTarget.getReg(), RegState::Kill);
718*0a6a1f1dSLionel Sambuc }
719*0a6a1f1dSLionel Sambuc
720*0a6a1f1dSLionel Sambuc MachineInstr *NewMI = std::prev(MBBI);
721*0a6a1f1dSLionel Sambuc for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
722*0a6a1f1dSLionel Sambuc NewMI->addOperand(MBBI->getOperand(i));
723*0a6a1f1dSLionel Sambuc
724*0a6a1f1dSLionel Sambuc // Delete the pseudo instruction TCRETURN.
725*0a6a1f1dSLionel Sambuc MBB.erase(MBBI);
726*0a6a1f1dSLionel Sambuc MBBI = NewMI;
727*0a6a1f1dSLionel Sambuc }
728*0a6a1f1dSLionel Sambuc
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const729*0a6a1f1dSLionel Sambuc void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
730*0a6a1f1dSLionel Sambuc MachineBasicBlock &MBB) const {
731*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
732*0a6a1f1dSLionel Sambuc assert(MBBI->isReturn() && "Can only insert epilog into returning blocks");
733*0a6a1f1dSLionel Sambuc DebugLoc dl = MBBI->getDebugLoc();
734f4a2713aSLionel Sambuc MachineFrameInfo *MFI = MF.getFrameInfo();
735f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
736*0a6a1f1dSLionel Sambuc const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
737f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII =
738*0a6a1f1dSLionel Sambuc *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
739f4a2713aSLionel Sambuc assert(!AFI->isThumb1OnlyFunction() &&
740f4a2713aSLionel Sambuc "This emitEpilogue does not support Thumb1!");
741f4a2713aSLionel Sambuc bool isARM = !AFI->isThumbFunction();
742f4a2713aSLionel Sambuc
743*0a6a1f1dSLionel Sambuc unsigned Align = MF.getTarget()
744*0a6a1f1dSLionel Sambuc .getSubtargetImpl()
745*0a6a1f1dSLionel Sambuc ->getFrameLowering()
746*0a6a1f1dSLionel Sambuc ->getStackAlignment();
747f4a2713aSLionel Sambuc unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
748f4a2713aSLionel Sambuc int NumBytes = (int)MFI->getStackSize();
749f4a2713aSLionel Sambuc unsigned FramePtr = RegInfo->getFrameRegister(MF);
750f4a2713aSLionel Sambuc
751f4a2713aSLionel Sambuc // All calls are tail calls in GHC calling conv, and functions have no
752f4a2713aSLionel Sambuc // prologue/epilogue.
753*0a6a1f1dSLionel Sambuc if (MF.getFunction()->getCallingConv() == CallingConv::GHC) {
754*0a6a1f1dSLionel Sambuc fixTCReturn(MF, MBB);
755f4a2713aSLionel Sambuc return;
756*0a6a1f1dSLionel Sambuc }
757f4a2713aSLionel Sambuc
758f4a2713aSLionel Sambuc if (!AFI->hasStackFrame()) {
759*0a6a1f1dSLionel Sambuc if (NumBytes - ArgRegsSaveSize != 0)
760*0a6a1f1dSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
761f4a2713aSLionel Sambuc } else {
762f4a2713aSLionel Sambuc // Unwind MBBI to point to first LDR / VLDRD.
763*0a6a1f1dSLionel Sambuc const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
764f4a2713aSLionel Sambuc if (MBBI != MBB.begin()) {
765f4a2713aSLionel Sambuc do {
766f4a2713aSLionel Sambuc --MBBI;
767f4a2713aSLionel Sambuc } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
768f4a2713aSLionel Sambuc if (!isCSRestore(MBBI, TII, CSRegs))
769f4a2713aSLionel Sambuc ++MBBI;
770f4a2713aSLionel Sambuc }
771f4a2713aSLionel Sambuc
772f4a2713aSLionel Sambuc // Move SP to start of FP callee save spill area.
773*0a6a1f1dSLionel Sambuc NumBytes -= (ArgRegsSaveSize +
774*0a6a1f1dSLionel Sambuc AFI->getGPRCalleeSavedArea1Size() +
775f4a2713aSLionel Sambuc AFI->getGPRCalleeSavedArea2Size() +
776*0a6a1f1dSLionel Sambuc AFI->getDPRCalleeSavedGapSize() +
777f4a2713aSLionel Sambuc AFI->getDPRCalleeSavedAreaSize());
778f4a2713aSLionel Sambuc
779f4a2713aSLionel Sambuc // Reset SP based on frame pointer only if the stack frame extends beyond
780f4a2713aSLionel Sambuc // frame pointer stack slot or target is ELF and the function has FP.
781f4a2713aSLionel Sambuc if (AFI->shouldRestoreSPFromFP()) {
782f4a2713aSLionel Sambuc NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
783f4a2713aSLionel Sambuc if (NumBytes) {
784f4a2713aSLionel Sambuc if (isARM)
785f4a2713aSLionel Sambuc emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
786f4a2713aSLionel Sambuc ARMCC::AL, 0, TII);
787f4a2713aSLionel Sambuc else {
788f4a2713aSLionel Sambuc // It's not possible to restore SP from FP in a single instruction.
789f4a2713aSLionel Sambuc // For iOS, this looks like:
790f4a2713aSLionel Sambuc // mov sp, r7
791f4a2713aSLionel Sambuc // sub sp, #24
792f4a2713aSLionel Sambuc // This is bad, if an interrupt is taken after the mov, sp is in an
793f4a2713aSLionel Sambuc // inconsistent state.
794f4a2713aSLionel Sambuc // Use the first callee-saved register as a scratch register.
795f4a2713aSLionel Sambuc assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
796f4a2713aSLionel Sambuc "No scratch register to restore SP from FP!");
797f4a2713aSLionel Sambuc emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
798f4a2713aSLionel Sambuc ARMCC::AL, 0, TII);
799f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
800f4a2713aSLionel Sambuc ARM::SP)
801f4a2713aSLionel Sambuc .addReg(ARM::R4));
802f4a2713aSLionel Sambuc }
803f4a2713aSLionel Sambuc } else {
804f4a2713aSLionel Sambuc // Thumb2 or ARM.
805f4a2713aSLionel Sambuc if (isARM)
806f4a2713aSLionel Sambuc BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
807f4a2713aSLionel Sambuc .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
808f4a2713aSLionel Sambuc else
809f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
810f4a2713aSLionel Sambuc ARM::SP)
811f4a2713aSLionel Sambuc .addReg(FramePtr));
812f4a2713aSLionel Sambuc }
813*0a6a1f1dSLionel Sambuc } else if (NumBytes &&
814*0a6a1f1dSLionel Sambuc !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
815f4a2713aSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
816f4a2713aSLionel Sambuc
817f4a2713aSLionel Sambuc // Increment past our save areas.
818f4a2713aSLionel Sambuc if (AFI->getDPRCalleeSavedAreaSize()) {
819f4a2713aSLionel Sambuc MBBI++;
820f4a2713aSLionel Sambuc // Since vpop register list cannot have gaps, there may be multiple vpop
821f4a2713aSLionel Sambuc // instructions in the epilogue.
822f4a2713aSLionel Sambuc while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
823f4a2713aSLionel Sambuc MBBI++;
824f4a2713aSLionel Sambuc }
825*0a6a1f1dSLionel Sambuc if (AFI->getDPRCalleeSavedGapSize()) {
826*0a6a1f1dSLionel Sambuc assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
827*0a6a1f1dSLionel Sambuc "unexpected DPR alignment gap");
828*0a6a1f1dSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
829*0a6a1f1dSLionel Sambuc }
830*0a6a1f1dSLionel Sambuc
831f4a2713aSLionel Sambuc if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
832f4a2713aSLionel Sambuc if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
833f4a2713aSLionel Sambuc }
834f4a2713aSLionel Sambuc
835*0a6a1f1dSLionel Sambuc fixTCReturn(MF, MBB);
836f4a2713aSLionel Sambuc
837f4a2713aSLionel Sambuc if (ArgRegsSaveSize)
838f4a2713aSLionel Sambuc emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
839f4a2713aSLionel Sambuc }
840f4a2713aSLionel Sambuc
841f4a2713aSLionel Sambuc /// getFrameIndexReference - Provide a base+offset reference to an FI slot for
842f4a2713aSLionel Sambuc /// debug info. It's the same as what we use for resolving the code-gen
843f4a2713aSLionel Sambuc /// references for now. FIXME: This can go wrong when references are
844f4a2713aSLionel Sambuc /// SP-relative and simple call frames aren't used.
845f4a2713aSLionel Sambuc int
getFrameIndexReference(const MachineFunction & MF,int FI,unsigned & FrameReg) const846f4a2713aSLionel Sambuc ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
847f4a2713aSLionel Sambuc unsigned &FrameReg) const {
848f4a2713aSLionel Sambuc return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
849f4a2713aSLionel Sambuc }
850f4a2713aSLionel Sambuc
851f4a2713aSLionel Sambuc int
ResolveFrameIndexReference(const MachineFunction & MF,int FI,unsigned & FrameReg,int SPAdj) const852f4a2713aSLionel Sambuc ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
853f4a2713aSLionel Sambuc int FI, unsigned &FrameReg,
854f4a2713aSLionel Sambuc int SPAdj) const {
855f4a2713aSLionel Sambuc const MachineFrameInfo *MFI = MF.getFrameInfo();
856*0a6a1f1dSLionel Sambuc const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
857*0a6a1f1dSLionel Sambuc MF.getSubtarget().getRegisterInfo());
858f4a2713aSLionel Sambuc const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
859f4a2713aSLionel Sambuc int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
860f4a2713aSLionel Sambuc int FPOffset = Offset - AFI->getFramePtrSpillOffset();
861f4a2713aSLionel Sambuc bool isFixed = MFI->isFixedObjectIndex(FI);
862f4a2713aSLionel Sambuc
863f4a2713aSLionel Sambuc FrameReg = ARM::SP;
864f4a2713aSLionel Sambuc Offset += SPAdj;
865f4a2713aSLionel Sambuc
866f4a2713aSLionel Sambuc // SP can move around if there are allocas. We may also lose track of SP
867f4a2713aSLionel Sambuc // when emergency spilling inside a non-reserved call frame setup.
868f4a2713aSLionel Sambuc bool hasMovingSP = !hasReservedCallFrame(MF);
869f4a2713aSLionel Sambuc
870f4a2713aSLionel Sambuc // When dynamically realigning the stack, use the frame pointer for
871f4a2713aSLionel Sambuc // parameters, and the stack/base pointer for locals.
872f4a2713aSLionel Sambuc if (RegInfo->needsStackRealignment(MF)) {
873f4a2713aSLionel Sambuc assert (hasFP(MF) && "dynamic stack realignment without a FP!");
874f4a2713aSLionel Sambuc if (isFixed) {
875f4a2713aSLionel Sambuc FrameReg = RegInfo->getFrameRegister(MF);
876f4a2713aSLionel Sambuc Offset = FPOffset;
877f4a2713aSLionel Sambuc } else if (hasMovingSP) {
878f4a2713aSLionel Sambuc assert(RegInfo->hasBasePointer(MF) &&
879f4a2713aSLionel Sambuc "VLAs and dynamic stack alignment, but missing base pointer!");
880f4a2713aSLionel Sambuc FrameReg = RegInfo->getBaseRegister();
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc return Offset;
883f4a2713aSLionel Sambuc }
884f4a2713aSLionel Sambuc
885f4a2713aSLionel Sambuc // If there is a frame pointer, use it when we can.
886f4a2713aSLionel Sambuc if (hasFP(MF) && AFI->hasStackFrame()) {
887f4a2713aSLionel Sambuc // Use frame pointer to reference fixed objects. Use it for locals if
888f4a2713aSLionel Sambuc // there are VLAs (and thus the SP isn't reliable as a base).
889f4a2713aSLionel Sambuc if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
890f4a2713aSLionel Sambuc FrameReg = RegInfo->getFrameRegister(MF);
891f4a2713aSLionel Sambuc return FPOffset;
892f4a2713aSLionel Sambuc } else if (hasMovingSP) {
893f4a2713aSLionel Sambuc assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
894f4a2713aSLionel Sambuc if (AFI->isThumb2Function()) {
895f4a2713aSLionel Sambuc // Try to use the frame pointer if we can, else use the base pointer
896f4a2713aSLionel Sambuc // since it's available. This is handy for the emergency spill slot, in
897f4a2713aSLionel Sambuc // particular.
898f4a2713aSLionel Sambuc if (FPOffset >= -255 && FPOffset < 0) {
899f4a2713aSLionel Sambuc FrameReg = RegInfo->getFrameRegister(MF);
900f4a2713aSLionel Sambuc return FPOffset;
901f4a2713aSLionel Sambuc }
902f4a2713aSLionel Sambuc }
903f4a2713aSLionel Sambuc } else if (AFI->isThumb2Function()) {
904f4a2713aSLionel Sambuc // Use add <rd>, sp, #<imm8>
905f4a2713aSLionel Sambuc // ldr <rd>, [sp, #<imm8>]
906f4a2713aSLionel Sambuc // if at all possible to save space.
907f4a2713aSLionel Sambuc if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
908f4a2713aSLionel Sambuc return Offset;
909f4a2713aSLionel Sambuc // In Thumb2 mode, the negative offset is very limited. Try to avoid
910f4a2713aSLionel Sambuc // out of range references. ldr <rt>,[<rn>, #-<imm8>]
911f4a2713aSLionel Sambuc if (FPOffset >= -255 && FPOffset < 0) {
912f4a2713aSLionel Sambuc FrameReg = RegInfo->getFrameRegister(MF);
913f4a2713aSLionel Sambuc return FPOffset;
914f4a2713aSLionel Sambuc }
915f4a2713aSLionel Sambuc } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
916f4a2713aSLionel Sambuc // Otherwise, use SP or FP, whichever is closer to the stack slot.
917f4a2713aSLionel Sambuc FrameReg = RegInfo->getFrameRegister(MF);
918f4a2713aSLionel Sambuc return FPOffset;
919f4a2713aSLionel Sambuc }
920f4a2713aSLionel Sambuc }
921f4a2713aSLionel Sambuc // Use the base pointer if we have one.
922f4a2713aSLionel Sambuc if (RegInfo->hasBasePointer(MF))
923f4a2713aSLionel Sambuc FrameReg = RegInfo->getBaseRegister();
924f4a2713aSLionel Sambuc return Offset;
925f4a2713aSLionel Sambuc }
926f4a2713aSLionel Sambuc
getFrameIndexOffset(const MachineFunction & MF,int FI) const927f4a2713aSLionel Sambuc int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
928f4a2713aSLionel Sambuc int FI) const {
929f4a2713aSLionel Sambuc unsigned FrameReg;
930f4a2713aSLionel Sambuc return getFrameIndexReference(MF, FI, FrameReg);
931f4a2713aSLionel Sambuc }
932f4a2713aSLionel Sambuc
emitPushInst(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,unsigned StmOpc,unsigned StrOpc,bool NoGap,bool (* Func)(unsigned,bool),unsigned NumAlignedDPRCS2Regs,unsigned MIFlags) const933f4a2713aSLionel Sambuc void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
934f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI,
935f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI,
936f4a2713aSLionel Sambuc unsigned StmOpc, unsigned StrOpc,
937f4a2713aSLionel Sambuc bool NoGap,
938f4a2713aSLionel Sambuc bool(*Func)(unsigned, bool),
939f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs,
940f4a2713aSLionel Sambuc unsigned MIFlags) const {
941f4a2713aSLionel Sambuc MachineFunction &MF = *MBB.getParent();
942*0a6a1f1dSLionel Sambuc const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
943f4a2713aSLionel Sambuc
944f4a2713aSLionel Sambuc DebugLoc DL;
945f4a2713aSLionel Sambuc if (MI != MBB.end()) DL = MI->getDebugLoc();
946f4a2713aSLionel Sambuc
947f4a2713aSLionel Sambuc SmallVector<std::pair<unsigned,bool>, 4> Regs;
948f4a2713aSLionel Sambuc unsigned i = CSI.size();
949f4a2713aSLionel Sambuc while (i != 0) {
950f4a2713aSLionel Sambuc unsigned LastReg = 0;
951f4a2713aSLionel Sambuc for (; i != 0; --i) {
952f4a2713aSLionel Sambuc unsigned Reg = CSI[i-1].getReg();
953*0a6a1f1dSLionel Sambuc if (!(Func)(Reg, STI.isTargetDarwin())) continue;
954f4a2713aSLionel Sambuc
955f4a2713aSLionel Sambuc // D-registers in the aligned area DPRCS2 are NOT spilled here.
956f4a2713aSLionel Sambuc if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
957f4a2713aSLionel Sambuc continue;
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc // Add the callee-saved register as live-in unless it's LR and
960f4a2713aSLionel Sambuc // @llvm.returnaddress is called. If LR is returned for
961f4a2713aSLionel Sambuc // @llvm.returnaddress then it's already added to the function and
962f4a2713aSLionel Sambuc // entry block live-in sets.
963f4a2713aSLionel Sambuc bool isKill = true;
964f4a2713aSLionel Sambuc if (Reg == ARM::LR) {
965f4a2713aSLionel Sambuc if (MF.getFrameInfo()->isReturnAddressTaken() &&
966f4a2713aSLionel Sambuc MF.getRegInfo().isLiveIn(Reg))
967f4a2713aSLionel Sambuc isKill = false;
968f4a2713aSLionel Sambuc }
969f4a2713aSLionel Sambuc
970f4a2713aSLionel Sambuc if (isKill)
971f4a2713aSLionel Sambuc MBB.addLiveIn(Reg);
972f4a2713aSLionel Sambuc
973f4a2713aSLionel Sambuc // If NoGap is true, push consecutive registers and then leave the rest
974f4a2713aSLionel Sambuc // for other instructions. e.g.
975f4a2713aSLionel Sambuc // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
976f4a2713aSLionel Sambuc if (NoGap && LastReg && LastReg != Reg-1)
977f4a2713aSLionel Sambuc break;
978f4a2713aSLionel Sambuc LastReg = Reg;
979f4a2713aSLionel Sambuc Regs.push_back(std::make_pair(Reg, isKill));
980f4a2713aSLionel Sambuc }
981f4a2713aSLionel Sambuc
982f4a2713aSLionel Sambuc if (Regs.empty())
983f4a2713aSLionel Sambuc continue;
984f4a2713aSLionel Sambuc if (Regs.size() > 1 || StrOpc== 0) {
985f4a2713aSLionel Sambuc MachineInstrBuilder MIB =
986f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
987f4a2713aSLionel Sambuc .addReg(ARM::SP).setMIFlags(MIFlags));
988f4a2713aSLionel Sambuc for (unsigned i = 0, e = Regs.size(); i < e; ++i)
989f4a2713aSLionel Sambuc MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
990f4a2713aSLionel Sambuc } else if (Regs.size() == 1) {
991f4a2713aSLionel Sambuc MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
992f4a2713aSLionel Sambuc ARM::SP)
993f4a2713aSLionel Sambuc .addReg(Regs[0].first, getKillRegState(Regs[0].second))
994f4a2713aSLionel Sambuc .addReg(ARM::SP).setMIFlags(MIFlags)
995f4a2713aSLionel Sambuc .addImm(-4);
996f4a2713aSLionel Sambuc AddDefaultPred(MIB);
997f4a2713aSLionel Sambuc }
998f4a2713aSLionel Sambuc Regs.clear();
999*0a6a1f1dSLionel Sambuc
1000*0a6a1f1dSLionel Sambuc // Put any subsequent vpush instructions before this one: they will refer to
1001*0a6a1f1dSLionel Sambuc // higher register numbers so need to be pushed first in order to preserve
1002*0a6a1f1dSLionel Sambuc // monotonicity.
1003*0a6a1f1dSLionel Sambuc --MI;
1004f4a2713aSLionel Sambuc }
1005f4a2713aSLionel Sambuc }
1006f4a2713aSLionel Sambuc
emitPopInst(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,unsigned LdmOpc,unsigned LdrOpc,bool isVarArg,bool NoGap,bool (* Func)(unsigned,bool),unsigned NumAlignedDPRCS2Regs) const1007f4a2713aSLionel Sambuc void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
1008f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI,
1009f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI,
1010f4a2713aSLionel Sambuc unsigned LdmOpc, unsigned LdrOpc,
1011f4a2713aSLionel Sambuc bool isVarArg, bool NoGap,
1012f4a2713aSLionel Sambuc bool(*Func)(unsigned, bool),
1013f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs) const {
1014f4a2713aSLionel Sambuc MachineFunction &MF = *MBB.getParent();
1015*0a6a1f1dSLionel Sambuc const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1016f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1017f4a2713aSLionel Sambuc DebugLoc DL = MI->getDebugLoc();
1018f4a2713aSLionel Sambuc unsigned RetOpcode = MI->getOpcode();
1019f4a2713aSLionel Sambuc bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
1020f4a2713aSLionel Sambuc RetOpcode == ARM::TCRETURNri);
1021f4a2713aSLionel Sambuc bool isInterrupt =
1022f4a2713aSLionel Sambuc RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
1023f4a2713aSLionel Sambuc
1024f4a2713aSLionel Sambuc SmallVector<unsigned, 4> Regs;
1025f4a2713aSLionel Sambuc unsigned i = CSI.size();
1026f4a2713aSLionel Sambuc while (i != 0) {
1027f4a2713aSLionel Sambuc unsigned LastReg = 0;
1028f4a2713aSLionel Sambuc bool DeleteRet = false;
1029f4a2713aSLionel Sambuc for (; i != 0; --i) {
1030f4a2713aSLionel Sambuc unsigned Reg = CSI[i-1].getReg();
1031*0a6a1f1dSLionel Sambuc if (!(Func)(Reg, STI.isTargetDarwin())) continue;
1032f4a2713aSLionel Sambuc
1033f4a2713aSLionel Sambuc // The aligned reloads from area DPRCS2 are not inserted here.
1034f4a2713aSLionel Sambuc if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
1035f4a2713aSLionel Sambuc continue;
1036f4a2713aSLionel Sambuc
1037f4a2713aSLionel Sambuc if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
1038f4a2713aSLionel Sambuc STI.hasV5TOps()) {
1039f4a2713aSLionel Sambuc Reg = ARM::PC;
1040f4a2713aSLionel Sambuc LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
1041f4a2713aSLionel Sambuc // Fold the return instruction into the LDM.
1042f4a2713aSLionel Sambuc DeleteRet = true;
1043f4a2713aSLionel Sambuc }
1044f4a2713aSLionel Sambuc
1045f4a2713aSLionel Sambuc // If NoGap is true, pop consecutive registers and then leave the rest
1046f4a2713aSLionel Sambuc // for other instructions. e.g.
1047f4a2713aSLionel Sambuc // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1048f4a2713aSLionel Sambuc if (NoGap && LastReg && LastReg != Reg-1)
1049f4a2713aSLionel Sambuc break;
1050f4a2713aSLionel Sambuc
1051f4a2713aSLionel Sambuc LastReg = Reg;
1052f4a2713aSLionel Sambuc Regs.push_back(Reg);
1053f4a2713aSLionel Sambuc }
1054f4a2713aSLionel Sambuc
1055f4a2713aSLionel Sambuc if (Regs.empty())
1056f4a2713aSLionel Sambuc continue;
1057f4a2713aSLionel Sambuc if (Regs.size() > 1 || LdrOpc == 0) {
1058f4a2713aSLionel Sambuc MachineInstrBuilder MIB =
1059f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
1060f4a2713aSLionel Sambuc .addReg(ARM::SP));
1061f4a2713aSLionel Sambuc for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1062f4a2713aSLionel Sambuc MIB.addReg(Regs[i], getDefRegState(true));
1063f4a2713aSLionel Sambuc if (DeleteRet) {
1064f4a2713aSLionel Sambuc MIB.copyImplicitOps(&*MI);
1065f4a2713aSLionel Sambuc MI->eraseFromParent();
1066f4a2713aSLionel Sambuc }
1067f4a2713aSLionel Sambuc MI = MIB;
1068f4a2713aSLionel Sambuc } else if (Regs.size() == 1) {
1069f4a2713aSLionel Sambuc // If we adjusted the reg to PC from LR above, switch it back here. We
1070f4a2713aSLionel Sambuc // only do that for LDM.
1071f4a2713aSLionel Sambuc if (Regs[0] == ARM::PC)
1072f4a2713aSLionel Sambuc Regs[0] = ARM::LR;
1073f4a2713aSLionel Sambuc MachineInstrBuilder MIB =
1074f4a2713aSLionel Sambuc BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1075f4a2713aSLionel Sambuc .addReg(ARM::SP, RegState::Define)
1076f4a2713aSLionel Sambuc .addReg(ARM::SP);
1077f4a2713aSLionel Sambuc // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1078f4a2713aSLionel Sambuc // that refactoring is complete (eventually).
1079f4a2713aSLionel Sambuc if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
1080f4a2713aSLionel Sambuc MIB.addReg(0);
1081f4a2713aSLionel Sambuc MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1082f4a2713aSLionel Sambuc } else
1083f4a2713aSLionel Sambuc MIB.addImm(4);
1084f4a2713aSLionel Sambuc AddDefaultPred(MIB);
1085f4a2713aSLionel Sambuc }
1086f4a2713aSLionel Sambuc Regs.clear();
1087*0a6a1f1dSLionel Sambuc
1088*0a6a1f1dSLionel Sambuc // Put any subsequent vpop instructions after this one: they will refer to
1089*0a6a1f1dSLionel Sambuc // higher register numbers so need to be popped afterwards.
1090*0a6a1f1dSLionel Sambuc ++MI;
1091f4a2713aSLionel Sambuc }
1092f4a2713aSLionel Sambuc }
1093f4a2713aSLionel Sambuc
1094f4a2713aSLionel Sambuc /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
1095f4a2713aSLionel Sambuc /// starting from d8. Also insert stack realignment code and leave the stack
1096f4a2713aSLionel Sambuc /// pointer pointing to the d8 spill slot.
emitAlignedDPRCS2Spills(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned NumAlignedDPRCS2Regs,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI)1097f4a2713aSLionel Sambuc static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1098f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI,
1099f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs,
1100f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI,
1101f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI) {
1102f4a2713aSLionel Sambuc MachineFunction &MF = *MBB.getParent();
1103f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1104f4a2713aSLionel Sambuc DebugLoc DL = MI->getDebugLoc();
1105*0a6a1f1dSLionel Sambuc const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1106f4a2713aSLionel Sambuc MachineFrameInfo &MFI = *MF.getFrameInfo();
1107f4a2713aSLionel Sambuc
1108f4a2713aSLionel Sambuc // Mark the D-register spill slots as properly aligned. Since MFI computes
1109f4a2713aSLionel Sambuc // stack slot layout backwards, this can actually mean that the d-reg stack
1110f4a2713aSLionel Sambuc // slot offsets can be wrong. The offset for d8 will always be correct.
1111f4a2713aSLionel Sambuc for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1112f4a2713aSLionel Sambuc unsigned DNum = CSI[i].getReg() - ARM::D8;
1113f4a2713aSLionel Sambuc if (DNum >= 8)
1114f4a2713aSLionel Sambuc continue;
1115f4a2713aSLionel Sambuc int FI = CSI[i].getFrameIdx();
1116f4a2713aSLionel Sambuc // The even-numbered registers will be 16-byte aligned, the odd-numbered
1117f4a2713aSLionel Sambuc // registers will be 8-byte aligned.
1118f4a2713aSLionel Sambuc MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1119f4a2713aSLionel Sambuc
1120f4a2713aSLionel Sambuc // The stack slot for D8 needs to be maximally aligned because this is
1121f4a2713aSLionel Sambuc // actually the point where we align the stack pointer. MachineFrameInfo
1122f4a2713aSLionel Sambuc // computes all offsets relative to the incoming stack pointer which is a
1123f4a2713aSLionel Sambuc // bit weird when realigning the stack. Any extra padding for this
1124f4a2713aSLionel Sambuc // over-alignment is not realized because the code inserted below adjusts
1125f4a2713aSLionel Sambuc // the stack pointer by numregs * 8 before aligning the stack pointer.
1126f4a2713aSLionel Sambuc if (DNum == 0)
1127f4a2713aSLionel Sambuc MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1128f4a2713aSLionel Sambuc }
1129f4a2713aSLionel Sambuc
1130f4a2713aSLionel Sambuc // Move the stack pointer to the d8 spill slot, and align it at the same
1131f4a2713aSLionel Sambuc // time. Leave the stack slot address in the scratch register r4.
1132f4a2713aSLionel Sambuc //
1133f4a2713aSLionel Sambuc // sub r4, sp, #numregs * 8
1134f4a2713aSLionel Sambuc // bic r4, r4, #align - 1
1135f4a2713aSLionel Sambuc // mov sp, r4
1136f4a2713aSLionel Sambuc //
1137f4a2713aSLionel Sambuc bool isThumb = AFI->isThumbFunction();
1138f4a2713aSLionel Sambuc assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1139f4a2713aSLionel Sambuc AFI->setShouldRestoreSPFromFP(true);
1140f4a2713aSLionel Sambuc
1141f4a2713aSLionel Sambuc // sub r4, sp, #numregs * 8
1142f4a2713aSLionel Sambuc // The immediate is <= 64, so it doesn't need any special encoding.
1143f4a2713aSLionel Sambuc unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
1144f4a2713aSLionel Sambuc AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1145f4a2713aSLionel Sambuc .addReg(ARM::SP)
1146f4a2713aSLionel Sambuc .addImm(8 * NumAlignedDPRCS2Regs)));
1147f4a2713aSLionel Sambuc
1148f4a2713aSLionel Sambuc unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
1149*0a6a1f1dSLionel Sambuc // We must set parameter MustBeSingleInstruction to true, since
1150*0a6a1f1dSLionel Sambuc // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1151*0a6a1f1dSLionel Sambuc // stack alignment. Luckily, this can always be done since all ARM
1152*0a6a1f1dSLionel Sambuc // architecture versions that support Neon also support the BFC
1153*0a6a1f1dSLionel Sambuc // instruction.
1154*0a6a1f1dSLionel Sambuc emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
1155f4a2713aSLionel Sambuc
1156f4a2713aSLionel Sambuc // mov sp, r4
1157f4a2713aSLionel Sambuc // The stack pointer must be adjusted before spilling anything, otherwise
1158f4a2713aSLionel Sambuc // the stack slots could be clobbered by an interrupt handler.
1159f4a2713aSLionel Sambuc // Leave r4 live, it is used below.
1160f4a2713aSLionel Sambuc Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1161f4a2713aSLionel Sambuc MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
1162f4a2713aSLionel Sambuc .addReg(ARM::R4);
1163f4a2713aSLionel Sambuc MIB = AddDefaultPred(MIB);
1164f4a2713aSLionel Sambuc if (!isThumb)
1165f4a2713aSLionel Sambuc AddDefaultCC(MIB);
1166f4a2713aSLionel Sambuc
1167f4a2713aSLionel Sambuc // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1168f4a2713aSLionel Sambuc // r4 holds the stack slot address.
1169f4a2713aSLionel Sambuc unsigned NextReg = ARM::D8;
1170f4a2713aSLionel Sambuc
1171f4a2713aSLionel Sambuc // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1172f4a2713aSLionel Sambuc // The writeback is only needed when emitting two vst1.64 instructions.
1173f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs >= 6) {
1174f4a2713aSLionel Sambuc unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1175f4a2713aSLionel Sambuc &ARM::QQPRRegClass);
1176f4a2713aSLionel Sambuc MBB.addLiveIn(SupReg);
1177f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
1178f4a2713aSLionel Sambuc ARM::R4)
1179f4a2713aSLionel Sambuc .addReg(ARM::R4, RegState::Kill).addImm(16)
1180f4a2713aSLionel Sambuc .addReg(NextReg)
1181f4a2713aSLionel Sambuc .addReg(SupReg, RegState::ImplicitKill));
1182f4a2713aSLionel Sambuc NextReg += 4;
1183f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs -= 4;
1184f4a2713aSLionel Sambuc }
1185f4a2713aSLionel Sambuc
1186f4a2713aSLionel Sambuc // We won't modify r4 beyond this point. It currently points to the next
1187f4a2713aSLionel Sambuc // register to be spilled.
1188f4a2713aSLionel Sambuc unsigned R4BaseReg = NextReg;
1189f4a2713aSLionel Sambuc
1190f4a2713aSLionel Sambuc // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1191f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs >= 4) {
1192f4a2713aSLionel Sambuc unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1193f4a2713aSLionel Sambuc &ARM::QQPRRegClass);
1194f4a2713aSLionel Sambuc MBB.addLiveIn(SupReg);
1195f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1196f4a2713aSLionel Sambuc .addReg(ARM::R4).addImm(16).addReg(NextReg)
1197f4a2713aSLionel Sambuc .addReg(SupReg, RegState::ImplicitKill));
1198f4a2713aSLionel Sambuc NextReg += 4;
1199f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs -= 4;
1200f4a2713aSLionel Sambuc }
1201f4a2713aSLionel Sambuc
1202f4a2713aSLionel Sambuc // 16-byte aligned vst1.64 with 2 d-regs.
1203f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs >= 2) {
1204f4a2713aSLionel Sambuc unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1205f4a2713aSLionel Sambuc &ARM::QPRRegClass);
1206f4a2713aSLionel Sambuc MBB.addLiveIn(SupReg);
1207f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
1208f4a2713aSLionel Sambuc .addReg(ARM::R4).addImm(16).addReg(SupReg));
1209f4a2713aSLionel Sambuc NextReg += 2;
1210f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs -= 2;
1211f4a2713aSLionel Sambuc }
1212f4a2713aSLionel Sambuc
1213f4a2713aSLionel Sambuc // Finally, use a vanilla vstr.64 for the odd last register.
1214f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs) {
1215f4a2713aSLionel Sambuc MBB.addLiveIn(NextReg);
1216f4a2713aSLionel Sambuc // vstr.64 uses addrmode5 which has an offset scale of 4.
1217f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1218f4a2713aSLionel Sambuc .addReg(NextReg)
1219f4a2713aSLionel Sambuc .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
1220f4a2713aSLionel Sambuc }
1221f4a2713aSLionel Sambuc
1222f4a2713aSLionel Sambuc // The last spill instruction inserted should kill the scratch register r4.
1223*0a6a1f1dSLionel Sambuc std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
1224f4a2713aSLionel Sambuc }
1225f4a2713aSLionel Sambuc
1226f4a2713aSLionel Sambuc /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1227f4a2713aSLionel Sambuc /// iterator to the following instruction.
1228f4a2713aSLionel Sambuc static MachineBasicBlock::iterator
skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,unsigned NumAlignedDPRCS2Regs)1229f4a2713aSLionel Sambuc skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1230f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs) {
1231f4a2713aSLionel Sambuc // sub r4, sp, #numregs * 8
1232f4a2713aSLionel Sambuc // bic r4, r4, #align - 1
1233f4a2713aSLionel Sambuc // mov sp, r4
1234f4a2713aSLionel Sambuc ++MI; ++MI; ++MI;
1235f4a2713aSLionel Sambuc assert(MI->mayStore() && "Expecting spill instruction");
1236f4a2713aSLionel Sambuc
1237f4a2713aSLionel Sambuc // These switches all fall through.
1238f4a2713aSLionel Sambuc switch(NumAlignedDPRCS2Regs) {
1239f4a2713aSLionel Sambuc case 7:
1240f4a2713aSLionel Sambuc ++MI;
1241f4a2713aSLionel Sambuc assert(MI->mayStore() && "Expecting spill instruction");
1242f4a2713aSLionel Sambuc default:
1243f4a2713aSLionel Sambuc ++MI;
1244f4a2713aSLionel Sambuc assert(MI->mayStore() && "Expecting spill instruction");
1245f4a2713aSLionel Sambuc case 1:
1246f4a2713aSLionel Sambuc case 2:
1247f4a2713aSLionel Sambuc case 4:
1248f4a2713aSLionel Sambuc assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1249f4a2713aSLionel Sambuc ++MI;
1250f4a2713aSLionel Sambuc }
1251f4a2713aSLionel Sambuc return MI;
1252f4a2713aSLionel Sambuc }
1253f4a2713aSLionel Sambuc
1254f4a2713aSLionel Sambuc /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1255f4a2713aSLionel Sambuc /// starting from d8. These instructions are assumed to execute while the
1256f4a2713aSLionel Sambuc /// stack is still aligned, unlike the code inserted by emitPopInst.
emitAlignedDPRCS2Restores(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned NumAlignedDPRCS2Regs,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI)1257f4a2713aSLionel Sambuc static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1258f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI,
1259f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs,
1260f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI,
1261f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI) {
1262f4a2713aSLionel Sambuc MachineFunction &MF = *MBB.getParent();
1263f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1264f4a2713aSLionel Sambuc DebugLoc DL = MI->getDebugLoc();
1265*0a6a1f1dSLionel Sambuc const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1266f4a2713aSLionel Sambuc
1267f4a2713aSLionel Sambuc // Find the frame index assigned to d8.
1268f4a2713aSLionel Sambuc int D8SpillFI = 0;
1269f4a2713aSLionel Sambuc for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1270f4a2713aSLionel Sambuc if (CSI[i].getReg() == ARM::D8) {
1271f4a2713aSLionel Sambuc D8SpillFI = CSI[i].getFrameIdx();
1272f4a2713aSLionel Sambuc break;
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc
1275f4a2713aSLionel Sambuc // Materialize the address of the d8 spill slot into the scratch register r4.
1276f4a2713aSLionel Sambuc // This can be fairly complicated if the stack frame is large, so just use
1277f4a2713aSLionel Sambuc // the normal frame index elimination mechanism to do it. This code runs as
1278f4a2713aSLionel Sambuc // the initial part of the epilog where the stack and base pointers haven't
1279f4a2713aSLionel Sambuc // been changed yet.
1280f4a2713aSLionel Sambuc bool isThumb = AFI->isThumbFunction();
1281f4a2713aSLionel Sambuc assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1282f4a2713aSLionel Sambuc
1283f4a2713aSLionel Sambuc unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1284f4a2713aSLionel Sambuc AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1285f4a2713aSLionel Sambuc .addFrameIndex(D8SpillFI).addImm(0)));
1286f4a2713aSLionel Sambuc
1287f4a2713aSLionel Sambuc // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1288f4a2713aSLionel Sambuc unsigned NextReg = ARM::D8;
1289f4a2713aSLionel Sambuc
1290f4a2713aSLionel Sambuc // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1291f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs >= 6) {
1292f4a2713aSLionel Sambuc unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1293f4a2713aSLionel Sambuc &ARM::QQPRRegClass);
1294f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1295f4a2713aSLionel Sambuc .addReg(ARM::R4, RegState::Define)
1296f4a2713aSLionel Sambuc .addReg(ARM::R4, RegState::Kill).addImm(16)
1297f4a2713aSLionel Sambuc .addReg(SupReg, RegState::ImplicitDefine));
1298f4a2713aSLionel Sambuc NextReg += 4;
1299f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs -= 4;
1300f4a2713aSLionel Sambuc }
1301f4a2713aSLionel Sambuc
1302f4a2713aSLionel Sambuc // We won't modify r4 beyond this point. It currently points to the next
1303f4a2713aSLionel Sambuc // register to be spilled.
1304f4a2713aSLionel Sambuc unsigned R4BaseReg = NextReg;
1305f4a2713aSLionel Sambuc
1306f4a2713aSLionel Sambuc // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1307f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs >= 4) {
1308f4a2713aSLionel Sambuc unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1309f4a2713aSLionel Sambuc &ARM::QQPRRegClass);
1310f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1311f4a2713aSLionel Sambuc .addReg(ARM::R4).addImm(16)
1312f4a2713aSLionel Sambuc .addReg(SupReg, RegState::ImplicitDefine));
1313f4a2713aSLionel Sambuc NextReg += 4;
1314f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs -= 4;
1315f4a2713aSLionel Sambuc }
1316f4a2713aSLionel Sambuc
1317f4a2713aSLionel Sambuc // 16-byte aligned vld1.64 with 2 d-regs.
1318f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs >= 2) {
1319f4a2713aSLionel Sambuc unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1320f4a2713aSLionel Sambuc &ARM::QPRRegClass);
1321f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1322f4a2713aSLionel Sambuc .addReg(ARM::R4).addImm(16));
1323f4a2713aSLionel Sambuc NextReg += 2;
1324f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs -= 2;
1325f4a2713aSLionel Sambuc }
1326f4a2713aSLionel Sambuc
1327f4a2713aSLionel Sambuc // Finally, use a vanilla vldr.64 for the remaining odd register.
1328f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs)
1329f4a2713aSLionel Sambuc AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1330f4a2713aSLionel Sambuc .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
1331f4a2713aSLionel Sambuc
1332f4a2713aSLionel Sambuc // Last store kills r4.
1333*0a6a1f1dSLionel Sambuc std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
1334f4a2713aSLionel Sambuc }
1335f4a2713aSLionel Sambuc
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const1336f4a2713aSLionel Sambuc bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1337f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI,
1338f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI,
1339f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI) const {
1340f4a2713aSLionel Sambuc if (CSI.empty())
1341f4a2713aSLionel Sambuc return false;
1342f4a2713aSLionel Sambuc
1343f4a2713aSLionel Sambuc MachineFunction &MF = *MBB.getParent();
1344f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1345f4a2713aSLionel Sambuc
1346f4a2713aSLionel Sambuc unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
1347f4a2713aSLionel Sambuc unsigned PushOneOpc = AFI->isThumbFunction() ?
1348f4a2713aSLionel Sambuc ARM::t2STR_PRE : ARM::STR_PRE_IMM;
1349f4a2713aSLionel Sambuc unsigned FltOpc = ARM::VSTMDDB_UPD;
1350f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1351f4a2713aSLionel Sambuc emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
1352f4a2713aSLionel Sambuc MachineInstr::FrameSetup);
1353f4a2713aSLionel Sambuc emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
1354f4a2713aSLionel Sambuc MachineInstr::FrameSetup);
1355f4a2713aSLionel Sambuc emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
1356f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1357f4a2713aSLionel Sambuc
1358f4a2713aSLionel Sambuc // The code above does not insert spill code for the aligned DPRCS2 registers.
1359f4a2713aSLionel Sambuc // The stack realignment code will be inserted between the push instructions
1360f4a2713aSLionel Sambuc // and these spills.
1361f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs)
1362f4a2713aSLionel Sambuc emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
1363f4a2713aSLionel Sambuc
1364f4a2713aSLionel Sambuc return true;
1365f4a2713aSLionel Sambuc }
1366f4a2713aSLionel Sambuc
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const1367f4a2713aSLionel Sambuc bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1368f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI,
1369f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI,
1370f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI) const {
1371f4a2713aSLionel Sambuc if (CSI.empty())
1372f4a2713aSLionel Sambuc return false;
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc MachineFunction &MF = *MBB.getParent();
1375f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1376f4a2713aSLionel Sambuc bool isVarArg = AFI->getArgRegsSaveSize() > 0;
1377f4a2713aSLionel Sambuc unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1378f4a2713aSLionel Sambuc
1379f4a2713aSLionel Sambuc // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1380f4a2713aSLionel Sambuc // registers. Do that here instead.
1381f4a2713aSLionel Sambuc if (NumAlignedDPRCS2Regs)
1382f4a2713aSLionel Sambuc emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
1383f4a2713aSLionel Sambuc
1384f4a2713aSLionel Sambuc unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
1385f4a2713aSLionel Sambuc unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
1386f4a2713aSLionel Sambuc unsigned FltOpc = ARM::VLDMDIA_UPD;
1387f4a2713aSLionel Sambuc emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1388f4a2713aSLionel Sambuc NumAlignedDPRCS2Regs);
1389f4a2713aSLionel Sambuc emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
1390f4a2713aSLionel Sambuc &isARMArea2Register, 0);
1391f4a2713aSLionel Sambuc emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
1392f4a2713aSLionel Sambuc &isARMArea1Register, 0);
1393f4a2713aSLionel Sambuc
1394f4a2713aSLionel Sambuc return true;
1395f4a2713aSLionel Sambuc }
1396f4a2713aSLionel Sambuc
1397f4a2713aSLionel Sambuc // FIXME: Make generic?
GetFunctionSizeInBytes(const MachineFunction & MF,const ARMBaseInstrInfo & TII)1398f4a2713aSLionel Sambuc static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1399f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII) {
1400f4a2713aSLionel Sambuc unsigned FnSize = 0;
1401*0a6a1f1dSLionel Sambuc for (auto &MBB : MF) {
1402*0a6a1f1dSLionel Sambuc for (auto &MI : MBB)
1403*0a6a1f1dSLionel Sambuc FnSize += TII.GetInstSizeInBytes(&MI);
1404f4a2713aSLionel Sambuc }
1405f4a2713aSLionel Sambuc return FnSize;
1406f4a2713aSLionel Sambuc }
1407f4a2713aSLionel Sambuc
1408f4a2713aSLionel Sambuc /// estimateRSStackSizeLimit - Look at each instruction that references stack
1409f4a2713aSLionel Sambuc /// frames and return the stack size limit beyond which some of these
1410f4a2713aSLionel Sambuc /// instructions will require a scratch register during their expansion later.
1411f4a2713aSLionel Sambuc // FIXME: Move to TII?
estimateRSStackSizeLimit(MachineFunction & MF,const TargetFrameLowering * TFI)1412f4a2713aSLionel Sambuc static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
1413f4a2713aSLionel Sambuc const TargetFrameLowering *TFI) {
1414f4a2713aSLionel Sambuc const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1415f4a2713aSLionel Sambuc unsigned Limit = (1 << 12) - 1;
1416*0a6a1f1dSLionel Sambuc for (auto &MBB : MF) {
1417*0a6a1f1dSLionel Sambuc for (auto &MI : MBB) {
1418*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1419*0a6a1f1dSLionel Sambuc if (!MI.getOperand(i).isFI())
1420*0a6a1f1dSLionel Sambuc continue;
1421f4a2713aSLionel Sambuc
1422f4a2713aSLionel Sambuc // When using ADDri to get the address of a stack object, 255 is the
1423f4a2713aSLionel Sambuc // largest offset guaranteed to fit in the immediate offset.
1424*0a6a1f1dSLionel Sambuc if (MI.getOpcode() == ARM::ADDri) {
1425f4a2713aSLionel Sambuc Limit = std::min(Limit, (1U << 8) - 1);
1426f4a2713aSLionel Sambuc break;
1427f4a2713aSLionel Sambuc }
1428f4a2713aSLionel Sambuc
1429f4a2713aSLionel Sambuc // Otherwise check the addressing mode.
1430*0a6a1f1dSLionel Sambuc switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
1431f4a2713aSLionel Sambuc case ARMII::AddrMode3:
1432f4a2713aSLionel Sambuc case ARMII::AddrModeT2_i8:
1433f4a2713aSLionel Sambuc Limit = std::min(Limit, (1U << 8) - 1);
1434f4a2713aSLionel Sambuc break;
1435f4a2713aSLionel Sambuc case ARMII::AddrMode5:
1436f4a2713aSLionel Sambuc case ARMII::AddrModeT2_i8s4:
1437f4a2713aSLionel Sambuc Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1438f4a2713aSLionel Sambuc break;
1439f4a2713aSLionel Sambuc case ARMII::AddrModeT2_i12:
1440f4a2713aSLionel Sambuc // i12 supports only positive offset so these will be converted to
1441f4a2713aSLionel Sambuc // i8 opcodes. See llvm::rewriteT2FrameIndex.
1442f4a2713aSLionel Sambuc if (TFI->hasFP(MF) && AFI->hasStackFrame())
1443f4a2713aSLionel Sambuc Limit = std::min(Limit, (1U << 8) - 1);
1444f4a2713aSLionel Sambuc break;
1445f4a2713aSLionel Sambuc case ARMII::AddrMode4:
1446f4a2713aSLionel Sambuc case ARMII::AddrMode6:
1447f4a2713aSLionel Sambuc // Addressing modes 4 & 6 (load/store) instructions can't encode an
1448f4a2713aSLionel Sambuc // immediate offset for stack references.
1449f4a2713aSLionel Sambuc return 0;
1450f4a2713aSLionel Sambuc default:
1451f4a2713aSLionel Sambuc break;
1452f4a2713aSLionel Sambuc }
1453f4a2713aSLionel Sambuc break; // At most one FI per instruction
1454f4a2713aSLionel Sambuc }
1455f4a2713aSLionel Sambuc }
1456f4a2713aSLionel Sambuc }
1457f4a2713aSLionel Sambuc
1458f4a2713aSLionel Sambuc return Limit;
1459f4a2713aSLionel Sambuc }
1460f4a2713aSLionel Sambuc
1461f4a2713aSLionel Sambuc // In functions that realign the stack, it can be an advantage to spill the
1462f4a2713aSLionel Sambuc // callee-saved vector registers after realigning the stack. The vst1 and vld1
1463f4a2713aSLionel Sambuc // instructions take alignment hints that can improve performance.
1464f4a2713aSLionel Sambuc //
checkNumAlignedDPRCS2Regs(MachineFunction & MF)1465f4a2713aSLionel Sambuc static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) {
1466f4a2713aSLionel Sambuc MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1467f4a2713aSLionel Sambuc if (!SpillAlignedNEONRegs)
1468f4a2713aSLionel Sambuc return;
1469f4a2713aSLionel Sambuc
1470f4a2713aSLionel Sambuc // Naked functions don't spill callee-saved registers.
1471f4a2713aSLionel Sambuc if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1472f4a2713aSLionel Sambuc Attribute::Naked))
1473f4a2713aSLionel Sambuc return;
1474f4a2713aSLionel Sambuc
1475f4a2713aSLionel Sambuc // We are planning to use NEON instructions vst1 / vld1.
1476f4a2713aSLionel Sambuc if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON())
1477f4a2713aSLionel Sambuc return;
1478f4a2713aSLionel Sambuc
1479f4a2713aSLionel Sambuc // Don't bother if the default stack alignment is sufficiently high.
1480*0a6a1f1dSLionel Sambuc if (MF.getTarget()
1481*0a6a1f1dSLionel Sambuc .getSubtargetImpl()
1482*0a6a1f1dSLionel Sambuc ->getFrameLowering()
1483*0a6a1f1dSLionel Sambuc ->getStackAlignment() >= 8)
1484f4a2713aSLionel Sambuc return;
1485f4a2713aSLionel Sambuc
1486f4a2713aSLionel Sambuc // Aligned spills require stack realignment.
1487*0a6a1f1dSLionel Sambuc const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
1488*0a6a1f1dSLionel Sambuc MF.getSubtarget().getRegisterInfo());
1489f4a2713aSLionel Sambuc if (!RegInfo->canRealignStack(MF))
1490f4a2713aSLionel Sambuc return;
1491f4a2713aSLionel Sambuc
1492f4a2713aSLionel Sambuc // We always spill contiguous d-registers starting from d8. Count how many
1493f4a2713aSLionel Sambuc // needs spilling. The register allocator will almost always use the
1494f4a2713aSLionel Sambuc // callee-saved registers in order, but it can happen that there are holes in
1495f4a2713aSLionel Sambuc // the range. Registers above the hole will be spilled to the standard DPRCS
1496f4a2713aSLionel Sambuc // area.
1497f4a2713aSLionel Sambuc MachineRegisterInfo &MRI = MF.getRegInfo();
1498f4a2713aSLionel Sambuc unsigned NumSpills = 0;
1499f4a2713aSLionel Sambuc for (; NumSpills < 8; ++NumSpills)
1500f4a2713aSLionel Sambuc if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills))
1501f4a2713aSLionel Sambuc break;
1502f4a2713aSLionel Sambuc
1503f4a2713aSLionel Sambuc // Don't do this for just one d-register. It's not worth it.
1504f4a2713aSLionel Sambuc if (NumSpills < 2)
1505f4a2713aSLionel Sambuc return;
1506f4a2713aSLionel Sambuc
1507f4a2713aSLionel Sambuc // Spill the first NumSpills D-registers after realigning the stack.
1508f4a2713aSLionel Sambuc MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1509f4a2713aSLionel Sambuc
1510f4a2713aSLionel Sambuc // A scratch register is required for the vst1 / vld1 instructions.
1511f4a2713aSLionel Sambuc MF.getRegInfo().setPhysRegUsed(ARM::R4);
1512f4a2713aSLionel Sambuc }
1513f4a2713aSLionel Sambuc
1514f4a2713aSLionel Sambuc void
processFunctionBeforeCalleeSavedScan(MachineFunction & MF,RegScavenger * RS) const1515f4a2713aSLionel Sambuc ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
1516f4a2713aSLionel Sambuc RegScavenger *RS) const {
1517f4a2713aSLionel Sambuc // This tells PEI to spill the FP as if it is any other callee-save register
1518f4a2713aSLionel Sambuc // to take advantage the eliminateFrameIndex machinery. This also ensures it
1519f4a2713aSLionel Sambuc // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1520f4a2713aSLionel Sambuc // to combine multiple loads / stores.
1521f4a2713aSLionel Sambuc bool CanEliminateFrame = true;
1522f4a2713aSLionel Sambuc bool CS1Spilled = false;
1523f4a2713aSLionel Sambuc bool LRSpilled = false;
1524f4a2713aSLionel Sambuc unsigned NumGPRSpills = 0;
1525f4a2713aSLionel Sambuc SmallVector<unsigned, 4> UnspilledCS1GPRs;
1526f4a2713aSLionel Sambuc SmallVector<unsigned, 4> UnspilledCS2GPRs;
1527*0a6a1f1dSLionel Sambuc const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
1528*0a6a1f1dSLionel Sambuc MF.getSubtarget().getRegisterInfo());
1529f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII =
1530*0a6a1f1dSLionel Sambuc *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
1531f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1532f4a2713aSLionel Sambuc MachineFrameInfo *MFI = MF.getFrameInfo();
1533f4a2713aSLionel Sambuc MachineRegisterInfo &MRI = MF.getRegInfo();
1534f4a2713aSLionel Sambuc unsigned FramePtr = RegInfo->getFrameRegister(MF);
1535f4a2713aSLionel Sambuc
1536f4a2713aSLionel Sambuc // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1537f4a2713aSLionel Sambuc // scratch register. Also spill R4 if Thumb2 function has varsized objects,
1538f4a2713aSLionel Sambuc // since it's not always possible to restore sp from fp in a single
1539f4a2713aSLionel Sambuc // instruction.
1540f4a2713aSLionel Sambuc // FIXME: It will be better just to find spare register here.
1541f4a2713aSLionel Sambuc if (AFI->isThumb2Function() &&
1542f4a2713aSLionel Sambuc (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
1543f4a2713aSLionel Sambuc MRI.setPhysRegUsed(ARM::R4);
1544f4a2713aSLionel Sambuc
1545f4a2713aSLionel Sambuc if (AFI->isThumb1OnlyFunction()) {
1546f4a2713aSLionel Sambuc // Spill LR if Thumb1 function uses variable length argument lists.
1547f4a2713aSLionel Sambuc if (AFI->getArgRegsSaveSize() > 0)
1548f4a2713aSLionel Sambuc MRI.setPhysRegUsed(ARM::LR);
1549f4a2713aSLionel Sambuc
1550f4a2713aSLionel Sambuc // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1551f4a2713aSLionel Sambuc // for sure what the stack size will be, but for this, an estimate is good
1552f4a2713aSLionel Sambuc // enough. If there anything changes it, it'll be a spill, which implies
1553f4a2713aSLionel Sambuc // we've used all the registers and so R4 is already used, so not marking
1554f4a2713aSLionel Sambuc // it here will be OK.
1555f4a2713aSLionel Sambuc // FIXME: It will be better just to find spare register here.
1556f4a2713aSLionel Sambuc unsigned StackSize = MFI->estimateStackSize(MF);
1557f4a2713aSLionel Sambuc if (MFI->hasVarSizedObjects() || StackSize > 508)
1558f4a2713aSLionel Sambuc MRI.setPhysRegUsed(ARM::R4);
1559f4a2713aSLionel Sambuc }
1560f4a2713aSLionel Sambuc
1561f4a2713aSLionel Sambuc // See if we can spill vector registers to aligned stack.
1562f4a2713aSLionel Sambuc checkNumAlignedDPRCS2Regs(MF);
1563f4a2713aSLionel Sambuc
1564f4a2713aSLionel Sambuc // Spill the BasePtr if it's used.
1565f4a2713aSLionel Sambuc if (RegInfo->hasBasePointer(MF))
1566f4a2713aSLionel Sambuc MRI.setPhysRegUsed(RegInfo->getBaseRegister());
1567f4a2713aSLionel Sambuc
1568f4a2713aSLionel Sambuc // Don't spill FP if the frame can be eliminated. This is determined
1569f4a2713aSLionel Sambuc // by scanning the callee-save registers to see if any is used.
1570*0a6a1f1dSLionel Sambuc const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
1571f4a2713aSLionel Sambuc for (unsigned i = 0; CSRegs[i]; ++i) {
1572f4a2713aSLionel Sambuc unsigned Reg = CSRegs[i];
1573f4a2713aSLionel Sambuc bool Spilled = false;
1574f4a2713aSLionel Sambuc if (MRI.isPhysRegUsed(Reg)) {
1575f4a2713aSLionel Sambuc Spilled = true;
1576f4a2713aSLionel Sambuc CanEliminateFrame = false;
1577f4a2713aSLionel Sambuc }
1578f4a2713aSLionel Sambuc
1579f4a2713aSLionel Sambuc if (!ARM::GPRRegClass.contains(Reg))
1580f4a2713aSLionel Sambuc continue;
1581f4a2713aSLionel Sambuc
1582f4a2713aSLionel Sambuc if (Spilled) {
1583f4a2713aSLionel Sambuc NumGPRSpills++;
1584f4a2713aSLionel Sambuc
1585*0a6a1f1dSLionel Sambuc if (!STI.isTargetDarwin()) {
1586f4a2713aSLionel Sambuc if (Reg == ARM::LR)
1587f4a2713aSLionel Sambuc LRSpilled = true;
1588f4a2713aSLionel Sambuc CS1Spilled = true;
1589f4a2713aSLionel Sambuc continue;
1590f4a2713aSLionel Sambuc }
1591f4a2713aSLionel Sambuc
1592f4a2713aSLionel Sambuc // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1593f4a2713aSLionel Sambuc switch (Reg) {
1594f4a2713aSLionel Sambuc case ARM::LR:
1595f4a2713aSLionel Sambuc LRSpilled = true;
1596f4a2713aSLionel Sambuc // Fallthrough
1597f4a2713aSLionel Sambuc case ARM::R0: case ARM::R1:
1598f4a2713aSLionel Sambuc case ARM::R2: case ARM::R3:
1599f4a2713aSLionel Sambuc case ARM::R4: case ARM::R5:
1600f4a2713aSLionel Sambuc case ARM::R6: case ARM::R7:
1601f4a2713aSLionel Sambuc CS1Spilled = true;
1602f4a2713aSLionel Sambuc break;
1603f4a2713aSLionel Sambuc default:
1604f4a2713aSLionel Sambuc break;
1605f4a2713aSLionel Sambuc }
1606f4a2713aSLionel Sambuc } else {
1607*0a6a1f1dSLionel Sambuc if (!STI.isTargetDarwin()) {
1608f4a2713aSLionel Sambuc UnspilledCS1GPRs.push_back(Reg);
1609f4a2713aSLionel Sambuc continue;
1610f4a2713aSLionel Sambuc }
1611f4a2713aSLionel Sambuc
1612f4a2713aSLionel Sambuc switch (Reg) {
1613f4a2713aSLionel Sambuc case ARM::R0: case ARM::R1:
1614f4a2713aSLionel Sambuc case ARM::R2: case ARM::R3:
1615f4a2713aSLionel Sambuc case ARM::R4: case ARM::R5:
1616f4a2713aSLionel Sambuc case ARM::R6: case ARM::R7:
1617f4a2713aSLionel Sambuc case ARM::LR:
1618f4a2713aSLionel Sambuc UnspilledCS1GPRs.push_back(Reg);
1619f4a2713aSLionel Sambuc break;
1620f4a2713aSLionel Sambuc default:
1621f4a2713aSLionel Sambuc UnspilledCS2GPRs.push_back(Reg);
1622f4a2713aSLionel Sambuc break;
1623f4a2713aSLionel Sambuc }
1624f4a2713aSLionel Sambuc }
1625f4a2713aSLionel Sambuc }
1626f4a2713aSLionel Sambuc
1627f4a2713aSLionel Sambuc bool ForceLRSpill = false;
1628f4a2713aSLionel Sambuc if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1629f4a2713aSLionel Sambuc unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1630f4a2713aSLionel Sambuc // Force LR to be spilled if the Thumb function size is > 2048. This enables
1631f4a2713aSLionel Sambuc // use of BL to implement far jump. If it turns out that it's not needed
1632f4a2713aSLionel Sambuc // then the branch fix up path will undo it.
1633f4a2713aSLionel Sambuc if (FnSize >= (1 << 11)) {
1634f4a2713aSLionel Sambuc CanEliminateFrame = false;
1635f4a2713aSLionel Sambuc ForceLRSpill = true;
1636f4a2713aSLionel Sambuc }
1637f4a2713aSLionel Sambuc }
1638f4a2713aSLionel Sambuc
1639f4a2713aSLionel Sambuc // If any of the stack slot references may be out of range of an immediate
1640f4a2713aSLionel Sambuc // offset, make sure a register (or a spill slot) is available for the
1641f4a2713aSLionel Sambuc // register scavenger. Note that if we're indexing off the frame pointer, the
1642f4a2713aSLionel Sambuc // effective stack size is 4 bytes larger since the FP points to the stack
1643f4a2713aSLionel Sambuc // slot of the previous FP. Also, if we have variable sized objects in the
1644f4a2713aSLionel Sambuc // function, stack slot references will often be negative, and some of
1645f4a2713aSLionel Sambuc // our instructions are positive-offset only, so conservatively consider
1646f4a2713aSLionel Sambuc // that case to want a spill slot (or register) as well. Similarly, if
1647f4a2713aSLionel Sambuc // the function adjusts the stack pointer during execution and the
1648f4a2713aSLionel Sambuc // adjustments aren't already part of our stack size estimate, our offset
1649f4a2713aSLionel Sambuc // calculations may be off, so be conservative.
1650f4a2713aSLionel Sambuc // FIXME: We could add logic to be more precise about negative offsets
1651f4a2713aSLionel Sambuc // and which instructions will need a scratch register for them. Is it
1652f4a2713aSLionel Sambuc // worth the effort and added fragility?
1653f4a2713aSLionel Sambuc bool BigStack =
1654f4a2713aSLionel Sambuc (RS &&
1655f4a2713aSLionel Sambuc (MFI->estimateStackSize(MF) +
1656f4a2713aSLionel Sambuc ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
1657f4a2713aSLionel Sambuc estimateRSStackSizeLimit(MF, this)))
1658f4a2713aSLionel Sambuc || MFI->hasVarSizedObjects()
1659f4a2713aSLionel Sambuc || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
1660f4a2713aSLionel Sambuc
1661f4a2713aSLionel Sambuc bool ExtraCSSpill = false;
1662f4a2713aSLionel Sambuc if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1663f4a2713aSLionel Sambuc AFI->setHasStackFrame(true);
1664f4a2713aSLionel Sambuc
1665f4a2713aSLionel Sambuc // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1666f4a2713aSLionel Sambuc // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1667f4a2713aSLionel Sambuc if (!LRSpilled && CS1Spilled) {
1668f4a2713aSLionel Sambuc MRI.setPhysRegUsed(ARM::LR);
1669f4a2713aSLionel Sambuc NumGPRSpills++;
1670f4a2713aSLionel Sambuc SmallVectorImpl<unsigned>::iterator LRPos;
1671f4a2713aSLionel Sambuc LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1672f4a2713aSLionel Sambuc (unsigned)ARM::LR);
1673f4a2713aSLionel Sambuc if (LRPos != UnspilledCS1GPRs.end())
1674f4a2713aSLionel Sambuc UnspilledCS1GPRs.erase(LRPos);
1675f4a2713aSLionel Sambuc
1676f4a2713aSLionel Sambuc ForceLRSpill = false;
1677f4a2713aSLionel Sambuc ExtraCSSpill = true;
1678f4a2713aSLionel Sambuc }
1679f4a2713aSLionel Sambuc
1680f4a2713aSLionel Sambuc if (hasFP(MF)) {
1681f4a2713aSLionel Sambuc MRI.setPhysRegUsed(FramePtr);
1682*0a6a1f1dSLionel Sambuc auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1683*0a6a1f1dSLionel Sambuc FramePtr);
1684*0a6a1f1dSLionel Sambuc if (FPPos != UnspilledCS1GPRs.end())
1685*0a6a1f1dSLionel Sambuc UnspilledCS1GPRs.erase(FPPos);
1686f4a2713aSLionel Sambuc NumGPRSpills++;
1687f4a2713aSLionel Sambuc }
1688f4a2713aSLionel Sambuc
1689f4a2713aSLionel Sambuc // If stack and double are 8-byte aligned and we are spilling an odd number
1690f4a2713aSLionel Sambuc // of GPRs, spill one extra callee save GPR so we won't have to pad between
1691f4a2713aSLionel Sambuc // the integer and double callee save areas.
1692f4a2713aSLionel Sambuc unsigned TargetAlign = getStackAlignment();
1693*0a6a1f1dSLionel Sambuc if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
1694f4a2713aSLionel Sambuc if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1695f4a2713aSLionel Sambuc for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1696f4a2713aSLionel Sambuc unsigned Reg = UnspilledCS1GPRs[i];
1697f4a2713aSLionel Sambuc // Don't spill high register if the function is thumb1
1698f4a2713aSLionel Sambuc if (!AFI->isThumb1OnlyFunction() ||
1699f4a2713aSLionel Sambuc isARMLowRegister(Reg) || Reg == ARM::LR) {
1700f4a2713aSLionel Sambuc MRI.setPhysRegUsed(Reg);
1701f4a2713aSLionel Sambuc if (!MRI.isReserved(Reg))
1702f4a2713aSLionel Sambuc ExtraCSSpill = true;
1703f4a2713aSLionel Sambuc break;
1704f4a2713aSLionel Sambuc }
1705f4a2713aSLionel Sambuc }
1706f4a2713aSLionel Sambuc } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1707f4a2713aSLionel Sambuc unsigned Reg = UnspilledCS2GPRs.front();
1708f4a2713aSLionel Sambuc MRI.setPhysRegUsed(Reg);
1709f4a2713aSLionel Sambuc if (!MRI.isReserved(Reg))
1710f4a2713aSLionel Sambuc ExtraCSSpill = true;
1711f4a2713aSLionel Sambuc }
1712f4a2713aSLionel Sambuc }
1713f4a2713aSLionel Sambuc
1714f4a2713aSLionel Sambuc // Estimate if we might need to scavenge a register at some point in order
1715f4a2713aSLionel Sambuc // to materialize a stack offset. If so, either spill one additional
1716f4a2713aSLionel Sambuc // callee-saved register or reserve a special spill slot to facilitate
1717f4a2713aSLionel Sambuc // register scavenging. Thumb1 needs a spill slot for stack pointer
1718f4a2713aSLionel Sambuc // adjustments also, even when the frame itself is small.
1719f4a2713aSLionel Sambuc if (BigStack && !ExtraCSSpill) {
1720f4a2713aSLionel Sambuc // If any non-reserved CS register isn't spilled, just spill one or two
1721f4a2713aSLionel Sambuc // extra. That should take care of it!
1722f4a2713aSLionel Sambuc unsigned NumExtras = TargetAlign / 4;
1723f4a2713aSLionel Sambuc SmallVector<unsigned, 2> Extras;
1724f4a2713aSLionel Sambuc while (NumExtras && !UnspilledCS1GPRs.empty()) {
1725f4a2713aSLionel Sambuc unsigned Reg = UnspilledCS1GPRs.back();
1726f4a2713aSLionel Sambuc UnspilledCS1GPRs.pop_back();
1727f4a2713aSLionel Sambuc if (!MRI.isReserved(Reg) &&
1728f4a2713aSLionel Sambuc (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1729f4a2713aSLionel Sambuc Reg == ARM::LR)) {
1730f4a2713aSLionel Sambuc Extras.push_back(Reg);
1731f4a2713aSLionel Sambuc NumExtras--;
1732f4a2713aSLionel Sambuc }
1733f4a2713aSLionel Sambuc }
1734f4a2713aSLionel Sambuc // For non-Thumb1 functions, also check for hi-reg CS registers
1735f4a2713aSLionel Sambuc if (!AFI->isThumb1OnlyFunction()) {
1736f4a2713aSLionel Sambuc while (NumExtras && !UnspilledCS2GPRs.empty()) {
1737f4a2713aSLionel Sambuc unsigned Reg = UnspilledCS2GPRs.back();
1738f4a2713aSLionel Sambuc UnspilledCS2GPRs.pop_back();
1739f4a2713aSLionel Sambuc if (!MRI.isReserved(Reg)) {
1740f4a2713aSLionel Sambuc Extras.push_back(Reg);
1741f4a2713aSLionel Sambuc NumExtras--;
1742f4a2713aSLionel Sambuc }
1743f4a2713aSLionel Sambuc }
1744f4a2713aSLionel Sambuc }
1745f4a2713aSLionel Sambuc if (Extras.size() && NumExtras == 0) {
1746f4a2713aSLionel Sambuc for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1747f4a2713aSLionel Sambuc MRI.setPhysRegUsed(Extras[i]);
1748f4a2713aSLionel Sambuc }
1749f4a2713aSLionel Sambuc } else if (!AFI->isThumb1OnlyFunction()) {
1750f4a2713aSLionel Sambuc // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1751f4a2713aSLionel Sambuc // closest to SP or frame pointer.
1752f4a2713aSLionel Sambuc const TargetRegisterClass *RC = &ARM::GPRRegClass;
1753f4a2713aSLionel Sambuc RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1754f4a2713aSLionel Sambuc RC->getAlignment(),
1755f4a2713aSLionel Sambuc false));
1756f4a2713aSLionel Sambuc }
1757f4a2713aSLionel Sambuc }
1758f4a2713aSLionel Sambuc }
1759f4a2713aSLionel Sambuc
1760f4a2713aSLionel Sambuc if (ForceLRSpill) {
1761f4a2713aSLionel Sambuc MRI.setPhysRegUsed(ARM::LR);
1762f4a2713aSLionel Sambuc AFI->setLRIsSpilledForFarJump(true);
1763f4a2713aSLionel Sambuc }
1764f4a2713aSLionel Sambuc }
1765f4a2713aSLionel Sambuc
1766f4a2713aSLionel Sambuc
1767f4a2713aSLionel Sambuc void ARMFrameLowering::
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const1768f4a2713aSLionel Sambuc eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1769f4a2713aSLionel Sambuc MachineBasicBlock::iterator I) const {
1770f4a2713aSLionel Sambuc const ARMBaseInstrInfo &TII =
1771*0a6a1f1dSLionel Sambuc *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
1772f4a2713aSLionel Sambuc if (!hasReservedCallFrame(MF)) {
1773f4a2713aSLionel Sambuc // If we have alloca, convert as follows:
1774f4a2713aSLionel Sambuc // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1775f4a2713aSLionel Sambuc // ADJCALLSTACKUP -> add, sp, sp, amount
1776f4a2713aSLionel Sambuc MachineInstr *Old = I;
1777f4a2713aSLionel Sambuc DebugLoc dl = Old->getDebugLoc();
1778f4a2713aSLionel Sambuc unsigned Amount = Old->getOperand(0).getImm();
1779f4a2713aSLionel Sambuc if (Amount != 0) {
1780f4a2713aSLionel Sambuc // We need to keep the stack aligned properly. To do this, we round the
1781f4a2713aSLionel Sambuc // amount of space needed for the outgoing arguments up to the next
1782f4a2713aSLionel Sambuc // alignment boundary.
1783f4a2713aSLionel Sambuc unsigned Align = getStackAlignment();
1784f4a2713aSLionel Sambuc Amount = (Amount+Align-1)/Align*Align;
1785f4a2713aSLionel Sambuc
1786f4a2713aSLionel Sambuc ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1787f4a2713aSLionel Sambuc assert(!AFI->isThumb1OnlyFunction() &&
1788f4a2713aSLionel Sambuc "This eliminateCallFramePseudoInstr does not support Thumb1!");
1789f4a2713aSLionel Sambuc bool isARM = !AFI->isThumbFunction();
1790f4a2713aSLionel Sambuc
1791f4a2713aSLionel Sambuc // Replace the pseudo instruction with a new instruction...
1792f4a2713aSLionel Sambuc unsigned Opc = Old->getOpcode();
1793f4a2713aSLionel Sambuc int PIdx = Old->findFirstPredOperandIdx();
1794f4a2713aSLionel Sambuc ARMCC::CondCodes Pred = (PIdx == -1)
1795f4a2713aSLionel Sambuc ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1796f4a2713aSLionel Sambuc if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1797f4a2713aSLionel Sambuc // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1798f4a2713aSLionel Sambuc unsigned PredReg = Old->getOperand(2).getReg();
1799f4a2713aSLionel Sambuc emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1800f4a2713aSLionel Sambuc Pred, PredReg);
1801f4a2713aSLionel Sambuc } else {
1802f4a2713aSLionel Sambuc // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1803f4a2713aSLionel Sambuc unsigned PredReg = Old->getOperand(3).getReg();
1804f4a2713aSLionel Sambuc assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1805f4a2713aSLionel Sambuc emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1806f4a2713aSLionel Sambuc Pred, PredReg);
1807f4a2713aSLionel Sambuc }
1808f4a2713aSLionel Sambuc }
1809f4a2713aSLionel Sambuc }
1810f4a2713aSLionel Sambuc MBB.erase(I);
1811f4a2713aSLionel Sambuc }
1812f4a2713aSLionel Sambuc
1813*0a6a1f1dSLionel Sambuc /// Get the minimum constant for ARM that is greater than or equal to the
1814*0a6a1f1dSLionel Sambuc /// argument. In ARM, constants can have any value that can be produced by
1815*0a6a1f1dSLionel Sambuc /// rotating an 8-bit value to the right by an even number of bits within a
1816*0a6a1f1dSLionel Sambuc /// 32-bit word.
alignToARMConstant(uint32_t Value)1817*0a6a1f1dSLionel Sambuc static uint32_t alignToARMConstant(uint32_t Value) {
1818*0a6a1f1dSLionel Sambuc unsigned Shifted = 0;
1819*0a6a1f1dSLionel Sambuc
1820*0a6a1f1dSLionel Sambuc if (Value == 0)
1821*0a6a1f1dSLionel Sambuc return 0;
1822*0a6a1f1dSLionel Sambuc
1823*0a6a1f1dSLionel Sambuc while (!(Value & 0xC0000000)) {
1824*0a6a1f1dSLionel Sambuc Value = Value << 2;
1825*0a6a1f1dSLionel Sambuc Shifted += 2;
1826*0a6a1f1dSLionel Sambuc }
1827*0a6a1f1dSLionel Sambuc
1828*0a6a1f1dSLionel Sambuc bool Carry = (Value & 0x00FFFFFF);
1829*0a6a1f1dSLionel Sambuc Value = ((Value & 0xFF000000) >> 24) + Carry;
1830*0a6a1f1dSLionel Sambuc
1831*0a6a1f1dSLionel Sambuc if (Value & 0x0000100)
1832*0a6a1f1dSLionel Sambuc Value = Value & 0x000001FC;
1833*0a6a1f1dSLionel Sambuc
1834*0a6a1f1dSLionel Sambuc if (Shifted > 24)
1835*0a6a1f1dSLionel Sambuc Value = Value >> (Shifted - 24);
1836*0a6a1f1dSLionel Sambuc else
1837*0a6a1f1dSLionel Sambuc Value = Value << (24 - Shifted);
1838*0a6a1f1dSLionel Sambuc
1839*0a6a1f1dSLionel Sambuc return Value;
1840*0a6a1f1dSLionel Sambuc }
1841*0a6a1f1dSLionel Sambuc
1842*0a6a1f1dSLionel Sambuc // The stack limit in the TCB is set to this many bytes above the actual
1843*0a6a1f1dSLionel Sambuc // stack limit.
1844*0a6a1f1dSLionel Sambuc static const uint64_t kSplitStackAvailable = 256;
1845*0a6a1f1dSLionel Sambuc
1846*0a6a1f1dSLionel Sambuc // Adjust the function prologue to enable split stacks. This currently only
1847*0a6a1f1dSLionel Sambuc // supports android and linux.
1848*0a6a1f1dSLionel Sambuc //
1849*0a6a1f1dSLionel Sambuc // The ABI of the segmented stack prologue is a little arbitrarily chosen, but
1850*0a6a1f1dSLionel Sambuc // must be well defined in order to allow for consistent implementations of the
1851*0a6a1f1dSLionel Sambuc // __morestack helper function. The ABI is also not a normal ABI in that it
1852*0a6a1f1dSLionel Sambuc // doesn't follow the normal calling conventions because this allows the
1853*0a6a1f1dSLionel Sambuc // prologue of each function to be optimized further.
1854*0a6a1f1dSLionel Sambuc //
1855*0a6a1f1dSLionel Sambuc // Currently, the ABI looks like (when calling __morestack)
1856*0a6a1f1dSLionel Sambuc //
1857*0a6a1f1dSLionel Sambuc // * r4 holds the minimum stack size requested for this function call
1858*0a6a1f1dSLionel Sambuc // * r5 holds the stack size of the arguments to the function
1859*0a6a1f1dSLionel Sambuc // * the beginning of the function is 3 instructions after the call to
1860*0a6a1f1dSLionel Sambuc // __morestack
1861*0a6a1f1dSLionel Sambuc //
1862*0a6a1f1dSLionel Sambuc // Implementations of __morestack should use r4 to allocate a new stack, r5 to
1863*0a6a1f1dSLionel Sambuc // place the arguments on to the new stack, and the 3-instruction knowledge to
1864*0a6a1f1dSLionel Sambuc // jump directly to the body of the function when working on the new stack.
1865*0a6a1f1dSLionel Sambuc //
1866*0a6a1f1dSLionel Sambuc // An old (and possibly no longer compatible) implementation of __morestack for
1867*0a6a1f1dSLionel Sambuc // ARM can be found at [1].
1868*0a6a1f1dSLionel Sambuc //
1869*0a6a1f1dSLionel Sambuc // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
adjustForSegmentedStacks(MachineFunction & MF) const1870*0a6a1f1dSLionel Sambuc void ARMFrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const {
1871*0a6a1f1dSLionel Sambuc unsigned Opcode;
1872*0a6a1f1dSLionel Sambuc unsigned CFIIndex;
1873*0a6a1f1dSLionel Sambuc const ARMSubtarget *ST = &MF.getTarget().getSubtarget<ARMSubtarget>();
1874*0a6a1f1dSLionel Sambuc bool Thumb = ST->isThumb();
1875*0a6a1f1dSLionel Sambuc
1876*0a6a1f1dSLionel Sambuc // Sadly, this currently doesn't support varargs, platforms other than
1877*0a6a1f1dSLionel Sambuc // android/linux. Note that thumb1/thumb2 are support for android/linux.
1878*0a6a1f1dSLionel Sambuc if (MF.getFunction()->isVarArg())
1879*0a6a1f1dSLionel Sambuc report_fatal_error("Segmented stacks do not support vararg functions.");
1880*0a6a1f1dSLionel Sambuc if (!ST->isTargetAndroid() && !ST->isTargetLinux())
1881*0a6a1f1dSLionel Sambuc report_fatal_error("Segmented stacks not supported on this platform.");
1882*0a6a1f1dSLionel Sambuc
1883*0a6a1f1dSLionel Sambuc MachineBasicBlock &prologueMBB = MF.front();
1884*0a6a1f1dSLionel Sambuc MachineFrameInfo *MFI = MF.getFrameInfo();
1885*0a6a1f1dSLionel Sambuc MachineModuleInfo &MMI = MF.getMMI();
1886*0a6a1f1dSLionel Sambuc MCContext &Context = MMI.getContext();
1887*0a6a1f1dSLionel Sambuc const MCRegisterInfo *MRI = Context.getRegisterInfo();
1888*0a6a1f1dSLionel Sambuc const ARMBaseInstrInfo &TII =
1889*0a6a1f1dSLionel Sambuc *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
1890*0a6a1f1dSLionel Sambuc ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
1891*0a6a1f1dSLionel Sambuc DebugLoc DL;
1892*0a6a1f1dSLionel Sambuc
1893*0a6a1f1dSLionel Sambuc uint64_t StackSize = MFI->getStackSize();
1894*0a6a1f1dSLionel Sambuc
1895*0a6a1f1dSLionel Sambuc // Do not generate a prologue for functions with a stack of size zero
1896*0a6a1f1dSLionel Sambuc if (StackSize == 0)
1897*0a6a1f1dSLionel Sambuc return;
1898*0a6a1f1dSLionel Sambuc
1899*0a6a1f1dSLionel Sambuc // Use R4 and R5 as scratch registers.
1900*0a6a1f1dSLionel Sambuc // We save R4 and R5 before use and restore them before leaving the function.
1901*0a6a1f1dSLionel Sambuc unsigned ScratchReg0 = ARM::R4;
1902*0a6a1f1dSLionel Sambuc unsigned ScratchReg1 = ARM::R5;
1903*0a6a1f1dSLionel Sambuc uint64_t AlignedStackSize;
1904*0a6a1f1dSLionel Sambuc
1905*0a6a1f1dSLionel Sambuc MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
1906*0a6a1f1dSLionel Sambuc MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
1907*0a6a1f1dSLionel Sambuc MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
1908*0a6a1f1dSLionel Sambuc MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
1909*0a6a1f1dSLionel Sambuc MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
1910*0a6a1f1dSLionel Sambuc
1911*0a6a1f1dSLionel Sambuc for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(),
1912*0a6a1f1dSLionel Sambuc e = prologueMBB.livein_end();
1913*0a6a1f1dSLionel Sambuc i != e; ++i) {
1914*0a6a1f1dSLionel Sambuc AllocMBB->addLiveIn(*i);
1915*0a6a1f1dSLionel Sambuc GetMBB->addLiveIn(*i);
1916*0a6a1f1dSLionel Sambuc McrMBB->addLiveIn(*i);
1917*0a6a1f1dSLionel Sambuc PrevStackMBB->addLiveIn(*i);
1918*0a6a1f1dSLionel Sambuc PostStackMBB->addLiveIn(*i);
1919*0a6a1f1dSLionel Sambuc }
1920*0a6a1f1dSLionel Sambuc
1921*0a6a1f1dSLionel Sambuc MF.push_front(PostStackMBB);
1922*0a6a1f1dSLionel Sambuc MF.push_front(AllocMBB);
1923*0a6a1f1dSLionel Sambuc MF.push_front(GetMBB);
1924*0a6a1f1dSLionel Sambuc MF.push_front(McrMBB);
1925*0a6a1f1dSLionel Sambuc MF.push_front(PrevStackMBB);
1926*0a6a1f1dSLionel Sambuc
1927*0a6a1f1dSLionel Sambuc // The required stack size that is aligned to ARM constant criterion.
1928*0a6a1f1dSLionel Sambuc AlignedStackSize = alignToARMConstant(StackSize);
1929*0a6a1f1dSLionel Sambuc
1930*0a6a1f1dSLionel Sambuc // When the frame size is less than 256 we just compare the stack
1931*0a6a1f1dSLionel Sambuc // boundary directly to the value of the stack pointer, per gcc.
1932*0a6a1f1dSLionel Sambuc bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
1933*0a6a1f1dSLionel Sambuc
1934*0a6a1f1dSLionel Sambuc // We will use two of the callee save registers as scratch registers so we
1935*0a6a1f1dSLionel Sambuc // need to save those registers onto the stack.
1936*0a6a1f1dSLionel Sambuc // We will use SR0 to hold stack limit and SR1 to hold the stack size
1937*0a6a1f1dSLionel Sambuc // requested and arguments for __morestack().
1938*0a6a1f1dSLionel Sambuc // SR0: Scratch Register #0
1939*0a6a1f1dSLionel Sambuc // SR1: Scratch Register #1
1940*0a6a1f1dSLionel Sambuc // push {SR0, SR1}
1941*0a6a1f1dSLionel Sambuc if (Thumb) {
1942*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH)))
1943*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0).addReg(ScratchReg1);
1944*0a6a1f1dSLionel Sambuc } else {
1945*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
1946*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define).addReg(ARM::SP))
1947*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0).addReg(ScratchReg1);
1948*0a6a1f1dSLionel Sambuc }
1949*0a6a1f1dSLionel Sambuc
1950*0a6a1f1dSLionel Sambuc // Emit the relevant DWARF information about the change in stack pointer as
1951*0a6a1f1dSLionel Sambuc // well as where to find both r4 and r5 (the callee-save registers)
1952*0a6a1f1dSLionel Sambuc CFIIndex =
1953*0a6a1f1dSLionel Sambuc MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
1954*0a6a1f1dSLionel Sambuc BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1955*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
1956*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1957*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
1958*0a6a1f1dSLionel Sambuc BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1959*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
1960*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1961*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
1962*0a6a1f1dSLionel Sambuc BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1963*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
1964*0a6a1f1dSLionel Sambuc
1965*0a6a1f1dSLionel Sambuc // mov SR1, sp
1966*0a6a1f1dSLionel Sambuc if (Thumb) {
1967*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
1968*0a6a1f1dSLionel Sambuc .addReg(ARM::SP));
1969*0a6a1f1dSLionel Sambuc } else if (CompareStackPointer) {
1970*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
1971*0a6a1f1dSLionel Sambuc .addReg(ARM::SP)).addReg(0);
1972*0a6a1f1dSLionel Sambuc }
1973*0a6a1f1dSLionel Sambuc
1974*0a6a1f1dSLionel Sambuc // sub SR1, sp, #StackSize
1975*0a6a1f1dSLionel Sambuc if (!CompareStackPointer && Thumb) {
1976*0a6a1f1dSLionel Sambuc AddDefaultPred(
1977*0a6a1f1dSLionel Sambuc AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1))
1978*0a6a1f1dSLionel Sambuc .addReg(ScratchReg1).addImm(AlignedStackSize));
1979*0a6a1f1dSLionel Sambuc } else if (!CompareStackPointer) {
1980*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
1981*0a6a1f1dSLionel Sambuc .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0);
1982*0a6a1f1dSLionel Sambuc }
1983*0a6a1f1dSLionel Sambuc
1984*0a6a1f1dSLionel Sambuc if (Thumb && ST->isThumb1Only()) {
1985*0a6a1f1dSLionel Sambuc unsigned PCLabelId = ARMFI->createPICLabelUId();
1986*0a6a1f1dSLionel Sambuc ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
1987*0a6a1f1dSLionel Sambuc MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
1988*0a6a1f1dSLionel Sambuc MachineConstantPool *MCP = MF.getConstantPool();
1989*0a6a1f1dSLionel Sambuc unsigned CPI = MCP->getConstantPoolIndex(NewCPV, MF.getAlignment());
1990*0a6a1f1dSLionel Sambuc
1991*0a6a1f1dSLionel Sambuc // ldr SR0, [pc, offset(STACK_LIMIT)]
1992*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
1993*0a6a1f1dSLionel Sambuc .addConstantPoolIndex(CPI));
1994*0a6a1f1dSLionel Sambuc
1995*0a6a1f1dSLionel Sambuc // ldr SR0, [SR0]
1996*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
1997*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0).addImm(0));
1998*0a6a1f1dSLionel Sambuc } else {
1999*0a6a1f1dSLionel Sambuc // Get TLS base address from the coprocessor
2000*0a6a1f1dSLionel Sambuc // mrc p15, #0, SR0, c13, c0, #3
2001*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2002*0a6a1f1dSLionel Sambuc .addImm(15)
2003*0a6a1f1dSLionel Sambuc .addImm(0)
2004*0a6a1f1dSLionel Sambuc .addImm(13)
2005*0a6a1f1dSLionel Sambuc .addImm(0)
2006*0a6a1f1dSLionel Sambuc .addImm(3));
2007*0a6a1f1dSLionel Sambuc
2008*0a6a1f1dSLionel Sambuc // Use the last tls slot on android and a private field of the TCP on linux.
2009*0a6a1f1dSLionel Sambuc assert(ST->isTargetAndroid() || ST->isTargetLinux());
2010*0a6a1f1dSLionel Sambuc unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2011*0a6a1f1dSLionel Sambuc
2012*0a6a1f1dSLionel Sambuc // Get the stack limit from the right offset
2013*0a6a1f1dSLionel Sambuc // ldr SR0, [sr0, #4 * TlsOffset]
2014*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2015*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0).addImm(4 * TlsOffset));
2016*0a6a1f1dSLionel Sambuc }
2017*0a6a1f1dSLionel Sambuc
2018*0a6a1f1dSLionel Sambuc // Compare stack limit with stack size requested.
2019*0a6a1f1dSLionel Sambuc // cmp SR0, SR1
2020*0a6a1f1dSLionel Sambuc Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
2021*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode))
2022*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0)
2023*0a6a1f1dSLionel Sambuc .addReg(ScratchReg1));
2024*0a6a1f1dSLionel Sambuc
2025*0a6a1f1dSLionel Sambuc // This jump is taken if StackLimit < SP - stack required.
2026*0a6a1f1dSLionel Sambuc Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2027*0a6a1f1dSLionel Sambuc BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2028*0a6a1f1dSLionel Sambuc .addImm(ARMCC::LO)
2029*0a6a1f1dSLionel Sambuc .addReg(ARM::CPSR);
2030*0a6a1f1dSLionel Sambuc
2031*0a6a1f1dSLionel Sambuc
2032*0a6a1f1dSLionel Sambuc // Calling __morestack(StackSize, Size of stack arguments).
2033*0a6a1f1dSLionel Sambuc // __morestack knows that the stack size requested is in SR0(r4)
2034*0a6a1f1dSLionel Sambuc // and amount size of stack arguments is in SR1(r5).
2035*0a6a1f1dSLionel Sambuc
2036*0a6a1f1dSLionel Sambuc // Pass first argument for the __morestack by Scratch Register #0.
2037*0a6a1f1dSLionel Sambuc // The amount size of stack required
2038*0a6a1f1dSLionel Sambuc if (Thumb) {
2039*0a6a1f1dSLionel Sambuc AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8),
2040*0a6a1f1dSLionel Sambuc ScratchReg0)).addImm(AlignedStackSize));
2041*0a6a1f1dSLionel Sambuc } else {
2042*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2043*0a6a1f1dSLionel Sambuc .addImm(AlignedStackSize)).addReg(0);
2044*0a6a1f1dSLionel Sambuc }
2045*0a6a1f1dSLionel Sambuc // Pass second argument for the __morestack by Scratch Register #1.
2046*0a6a1f1dSLionel Sambuc // The amount size of stack consumed to save function arguments.
2047*0a6a1f1dSLionel Sambuc if (Thumb) {
2048*0a6a1f1dSLionel Sambuc AddDefaultPred(
2049*0a6a1f1dSLionel Sambuc AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1))
2050*0a6a1f1dSLionel Sambuc .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())));
2051*0a6a1f1dSLionel Sambuc } else {
2052*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2053*0a6a1f1dSLionel Sambuc .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())))
2054*0a6a1f1dSLionel Sambuc .addReg(0);
2055*0a6a1f1dSLionel Sambuc }
2056*0a6a1f1dSLionel Sambuc
2057*0a6a1f1dSLionel Sambuc // push {lr} - Save return address of this function.
2058*0a6a1f1dSLionel Sambuc if (Thumb) {
2059*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH)))
2060*0a6a1f1dSLionel Sambuc .addReg(ARM::LR);
2061*0a6a1f1dSLionel Sambuc } else {
2062*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2063*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define)
2064*0a6a1f1dSLionel Sambuc .addReg(ARM::SP))
2065*0a6a1f1dSLionel Sambuc .addReg(ARM::LR);
2066*0a6a1f1dSLionel Sambuc }
2067*0a6a1f1dSLionel Sambuc
2068*0a6a1f1dSLionel Sambuc // Emit the DWARF info about the change in stack as well as where to find the
2069*0a6a1f1dSLionel Sambuc // previous link register
2070*0a6a1f1dSLionel Sambuc CFIIndex =
2071*0a6a1f1dSLionel Sambuc MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
2072*0a6a1f1dSLionel Sambuc BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2073*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
2074*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
2075*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2076*0a6a1f1dSLionel Sambuc BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2077*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
2078*0a6a1f1dSLionel Sambuc
2079*0a6a1f1dSLionel Sambuc // Call __morestack().
2080*0a6a1f1dSLionel Sambuc if (Thumb) {
2081*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL)))
2082*0a6a1f1dSLionel Sambuc .addExternalSymbol("__morestack");
2083*0a6a1f1dSLionel Sambuc } else {
2084*0a6a1f1dSLionel Sambuc BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2085*0a6a1f1dSLionel Sambuc .addExternalSymbol("__morestack");
2086*0a6a1f1dSLionel Sambuc }
2087*0a6a1f1dSLionel Sambuc
2088*0a6a1f1dSLionel Sambuc // pop {lr} - Restore return address of this original function.
2089*0a6a1f1dSLionel Sambuc if (Thumb) {
2090*0a6a1f1dSLionel Sambuc if (ST->isThumb1Only()) {
2091*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2092*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0);
2093*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2094*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0));
2095*0a6a1f1dSLionel Sambuc } else {
2096*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2097*0a6a1f1dSLionel Sambuc .addReg(ARM::LR, RegState::Define)
2098*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define)
2099*0a6a1f1dSLionel Sambuc .addReg(ARM::SP)
2100*0a6a1f1dSLionel Sambuc .addImm(4));
2101*0a6a1f1dSLionel Sambuc }
2102*0a6a1f1dSLionel Sambuc } else {
2103*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2104*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define)
2105*0a6a1f1dSLionel Sambuc .addReg(ARM::SP))
2106*0a6a1f1dSLionel Sambuc .addReg(ARM::LR);
2107*0a6a1f1dSLionel Sambuc }
2108*0a6a1f1dSLionel Sambuc
2109*0a6a1f1dSLionel Sambuc // Restore SR0 and SR1 in case of __morestack() was called.
2110*0a6a1f1dSLionel Sambuc // __morestack() will skip PostStackMBB block so we need to restore
2111*0a6a1f1dSLionel Sambuc // scratch registers from here.
2112*0a6a1f1dSLionel Sambuc // pop {SR0, SR1}
2113*0a6a1f1dSLionel Sambuc if (Thumb) {
2114*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2115*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0)
2116*0a6a1f1dSLionel Sambuc .addReg(ScratchReg1);
2117*0a6a1f1dSLionel Sambuc } else {
2118*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2119*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define)
2120*0a6a1f1dSLionel Sambuc .addReg(ARM::SP))
2121*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0)
2122*0a6a1f1dSLionel Sambuc .addReg(ScratchReg1);
2123*0a6a1f1dSLionel Sambuc }
2124*0a6a1f1dSLionel Sambuc
2125*0a6a1f1dSLionel Sambuc // Update the CFA offset now that we've popped
2126*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2127*0a6a1f1dSLionel Sambuc BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2128*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
2129*0a6a1f1dSLionel Sambuc
2130*0a6a1f1dSLionel Sambuc // bx lr - Return from this function.
2131*0a6a1f1dSLionel Sambuc Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
2132*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode)));
2133*0a6a1f1dSLionel Sambuc
2134*0a6a1f1dSLionel Sambuc // Restore SR0 and SR1 in case of __morestack() was not called.
2135*0a6a1f1dSLionel Sambuc // pop {SR0, SR1}
2136*0a6a1f1dSLionel Sambuc if (Thumb) {
2137*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP)))
2138*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0)
2139*0a6a1f1dSLionel Sambuc .addReg(ScratchReg1);
2140*0a6a1f1dSLionel Sambuc } else {
2141*0a6a1f1dSLionel Sambuc AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2142*0a6a1f1dSLionel Sambuc .addReg(ARM::SP, RegState::Define)
2143*0a6a1f1dSLionel Sambuc .addReg(ARM::SP))
2144*0a6a1f1dSLionel Sambuc .addReg(ScratchReg0)
2145*0a6a1f1dSLionel Sambuc .addReg(ScratchReg1);
2146*0a6a1f1dSLionel Sambuc }
2147*0a6a1f1dSLionel Sambuc
2148*0a6a1f1dSLionel Sambuc // Update the CFA offset now that we've popped
2149*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2150*0a6a1f1dSLionel Sambuc BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2151*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
2152*0a6a1f1dSLionel Sambuc
2153*0a6a1f1dSLionel Sambuc // Tell debuggers that r4 and r5 are now the same as they were in the
2154*0a6a1f1dSLionel Sambuc // previous function, that they're the "Same Value".
2155*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2156*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2157*0a6a1f1dSLionel Sambuc BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2158*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
2159*0a6a1f1dSLionel Sambuc CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2160*0a6a1f1dSLionel Sambuc nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2161*0a6a1f1dSLionel Sambuc BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2162*0a6a1f1dSLionel Sambuc .addCFIIndex(CFIIndex);
2163*0a6a1f1dSLionel Sambuc
2164*0a6a1f1dSLionel Sambuc // Organizing MBB lists
2165*0a6a1f1dSLionel Sambuc PostStackMBB->addSuccessor(&prologueMBB);
2166*0a6a1f1dSLionel Sambuc
2167*0a6a1f1dSLionel Sambuc AllocMBB->addSuccessor(PostStackMBB);
2168*0a6a1f1dSLionel Sambuc
2169*0a6a1f1dSLionel Sambuc GetMBB->addSuccessor(PostStackMBB);
2170*0a6a1f1dSLionel Sambuc GetMBB->addSuccessor(AllocMBB);
2171*0a6a1f1dSLionel Sambuc
2172*0a6a1f1dSLionel Sambuc McrMBB->addSuccessor(GetMBB);
2173*0a6a1f1dSLionel Sambuc
2174*0a6a1f1dSLionel Sambuc PrevStackMBB->addSuccessor(McrMBB);
2175*0a6a1f1dSLionel Sambuc
2176*0a6a1f1dSLionel Sambuc #ifdef XDEBUG
2177*0a6a1f1dSLionel Sambuc MF.verify();
2178*0a6a1f1dSLionel Sambuc #endif
2179*0a6a1f1dSLionel Sambuc }
2180