1*0b57cec5SDimitry Andric //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // Implements the layout of a stack frame on the target machine.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
14*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
15*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
16*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
1781ad6265SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
18*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
19*0b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
215ffd83dbSDimitry Andric #include "llvm/IR/InstrTypes.h"
2281ad6265SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
23*0b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
24*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
25*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
26*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric using namespace llvm;
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric TargetFrameLowering::~TargetFrameLowering() = default;
31*0b57cec5SDimitry Andric 
enableCalleeSaveSkip(const MachineFunction & MF) const32*0b57cec5SDimitry Andric bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
33*0b57cec5SDimitry Andric   assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
34*0b57cec5SDimitry Andric          MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
35*0b57cec5SDimitry Andric          !MF.getFunction().hasFnAttribute(Attribute::UWTable));
36*0b57cec5SDimitry Andric   return false;
37*0b57cec5SDimitry Andric }
38*0b57cec5SDimitry Andric 
enableCFIFixup(MachineFunction & MF) const3981ad6265SDimitry Andric bool TargetFrameLowering::enableCFIFixup(MachineFunction &MF) const {
4081ad6265SDimitry Andric   return MF.needsFrameMoves() &&
4181ad6265SDimitry Andric          !MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
4281ad6265SDimitry Andric }
4381ad6265SDimitry Andric 
44*0b57cec5SDimitry Andric /// Returns the displacement from the frame register to the stack
45*0b57cec5SDimitry Andric /// frame of the specified index, along with the frame register used
46*0b57cec5SDimitry Andric /// (in output arg FrameReg). This is the default implementation which
47*0b57cec5SDimitry Andric /// is overridden for some targets.
48e8d8bef9SDimitry Andric StackOffset
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const49e8d8bef9SDimitry Andric TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
505ffd83dbSDimitry Andric                                             Register &FrameReg) const {
51*0b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
52*0b57cec5SDimitry Andric   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric   // By default, assume all frame indices are referenced via whatever
55*0b57cec5SDimitry Andric   // getFrameRegister() says. The target can override this if it's doing
56*0b57cec5SDimitry Andric   // something different.
57*0b57cec5SDimitry Andric   FrameReg = RI->getFrameRegister(MF);
58*0b57cec5SDimitry Andric 
59e8d8bef9SDimitry Andric   return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
60e8d8bef9SDimitry Andric                                getOffsetOfLocalArea() +
61e8d8bef9SDimitry Andric                                MFI.getOffsetAdjustment());
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric 
needsFrameIndexResolution(const MachineFunction & MF) const64*0b57cec5SDimitry Andric bool TargetFrameLowering::needsFrameIndexResolution(
65*0b57cec5SDimitry Andric     const MachineFunction &MF) const {
66*0b57cec5SDimitry Andric   return MF.getFrameInfo().hasStackObjects();
67*0b57cec5SDimitry Andric }
68*0b57cec5SDimitry Andric 
getCalleeSaves(const MachineFunction & MF,BitVector & CalleeSaves) const69480093f4SDimitry Andric void TargetFrameLowering::getCalleeSaves(const MachineFunction &MF,
70480093f4SDimitry Andric                                          BitVector &CalleeSaves) const {
71480093f4SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
72480093f4SDimitry Andric   CalleeSaves.resize(TRI.getNumRegs());
73480093f4SDimitry Andric 
74480093f4SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
75480093f4SDimitry Andric   if (!MFI.isCalleeSavedInfoValid())
76480093f4SDimitry Andric     return;
77480093f4SDimitry Andric 
78480093f4SDimitry Andric   for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
79480093f4SDimitry Andric     CalleeSaves.set(Info.getReg());
80480093f4SDimitry Andric }
81480093f4SDimitry Andric 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const82*0b57cec5SDimitry Andric void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
83*0b57cec5SDimitry Andric                                                BitVector &SavedRegs,
84*0b57cec5SDimitry Andric                                                RegScavenger *RS) const {
85*0b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
86*0b57cec5SDimitry Andric 
87*0b57cec5SDimitry Andric   // Resize before the early returns. Some backends expect that
88*0b57cec5SDimitry Andric   // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
89*0b57cec5SDimitry Andric   // saved registers.
90*0b57cec5SDimitry Andric   SavedRegs.resize(TRI.getNumRegs());
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   // When interprocedural register allocation is enabled caller saved registers
93*0b57cec5SDimitry Andric   // are preferred over callee saved registers.
948bcb0991SDimitry Andric   if (MF.getTarget().Options.EnableIPRA &&
958bcb0991SDimitry Andric       isSafeForNoCSROpt(MF.getFunction()) &&
968bcb0991SDimitry Andric       isProfitableForNoCSROpt(MF.getFunction()))
97*0b57cec5SDimitry Andric     return;
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric   // Get the callee saved register list...
100*0b57cec5SDimitry Andric   const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric   // Early exit if there are no callee saved registers.
103*0b57cec5SDimitry Andric   if (!CSRegs || CSRegs[0] == 0)
104*0b57cec5SDimitry Andric     return;
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric   // In Naked functions we aren't going to save any registers.
107*0b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
108*0b57cec5SDimitry Andric     return;
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric   // Noreturn+nounwind functions never restore CSR, so no saves are needed.
111*0b57cec5SDimitry Andric   // Purely noreturn functions may still return through throws, so those must
112*0b57cec5SDimitry Andric   // save CSR for caller exception handlers.
113*0b57cec5SDimitry Andric   //
114*0b57cec5SDimitry Andric   // If the function uses longjmp to break out of its current path of
115*0b57cec5SDimitry Andric   // execution we do not need the CSR spills either: setjmp stores all CSRs
116*0b57cec5SDimitry Andric   // it was called with into the jmp_buf, which longjmp then restores.
117*0b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
118*0b57cec5SDimitry Andric         MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
119*0b57cec5SDimitry Andric         !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
120*0b57cec5SDimitry Andric         enableCalleeSaveSkip(MF))
121*0b57cec5SDimitry Andric     return;
122*0b57cec5SDimitry Andric 
123*0b57cec5SDimitry Andric   // Functions which call __builtin_unwind_init get all their registers saved.
124*0b57cec5SDimitry Andric   bool CallsUnwindInit = MF.callsUnwindInit();
125*0b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
126*0b57cec5SDimitry Andric   for (unsigned i = 0; CSRegs[i]; ++i) {
127*0b57cec5SDimitry Andric     unsigned Reg = CSRegs[i];
128*0b57cec5SDimitry Andric     if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
129*0b57cec5SDimitry Andric       SavedRegs.set(Reg);
130*0b57cec5SDimitry Andric   }
131*0b57cec5SDimitry Andric }
132*0b57cec5SDimitry Andric 
allocateScavengingFrameIndexesNearIncomingSP(const MachineFunction & MF) const1334824e7fdSDimitry Andric bool TargetFrameLowering::allocateScavengingFrameIndexesNearIncomingSP(
1344824e7fdSDimitry Andric   const MachineFunction &MF) const {
1354824e7fdSDimitry Andric   if (!hasFP(MF))
1364824e7fdSDimitry Andric     return false;
1374824e7fdSDimitry Andric 
1384824e7fdSDimitry Andric   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
1394824e7fdSDimitry Andric   return RegInfo->useFPForScavengingIndex(MF) &&
1404824e7fdSDimitry Andric          !RegInfo->hasStackRealignment(MF);
1414824e7fdSDimitry Andric }
1424824e7fdSDimitry Andric 
isSafeForNoCSROpt(const Function & F)1438bcb0991SDimitry Andric bool TargetFrameLowering::isSafeForNoCSROpt(const Function &F) {
1448bcb0991SDimitry Andric   if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
1458bcb0991SDimitry Andric       !F.hasFnAttribute(Attribute::NoRecurse))
1468bcb0991SDimitry Andric     return false;
1478bcb0991SDimitry Andric   // Function should not be optimized as tail call.
1488bcb0991SDimitry Andric   for (const User *U : F.users())
1495ffd83dbSDimitry Andric     if (auto *CB = dyn_cast<CallBase>(U))
1505ffd83dbSDimitry Andric       if (CB->isTailCall())
1518bcb0991SDimitry Andric         return false;
1528bcb0991SDimitry Andric   return true;
1538bcb0991SDimitry Andric }
1548bcb0991SDimitry Andric 
getInitialCFAOffset(const MachineFunction & MF) const155*0b57cec5SDimitry Andric int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
156*0b57cec5SDimitry Andric   llvm_unreachable("getInitialCFAOffset() not implemented!");
157*0b57cec5SDimitry Andric }
158*0b57cec5SDimitry Andric 
1595ffd83dbSDimitry Andric Register
getInitialCFARegister(const MachineFunction & MF) const1605ffd83dbSDimitry Andric TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) const {
161*0b57cec5SDimitry Andric   llvm_unreachable("getInitialCFARegister() not implemented!");
162*0b57cec5SDimitry Andric }
1635ffd83dbSDimitry Andric 
1645ffd83dbSDimitry Andric TargetFrameLowering::DwarfFrameBase
getDwarfFrameBase(const MachineFunction & MF) const1655ffd83dbSDimitry Andric TargetFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
1665ffd83dbSDimitry Andric   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
1675ffd83dbSDimitry Andric   return DwarfFrameBase{DwarfFrameBase::Register, {RI->getFrameRegister(MF)}};
1685ffd83dbSDimitry Andric }
169