10b57cec5SDimitry Andric //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implements the layout of a stack frame on the target machine.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
1781ad6265SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
190b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
200b57cec5SDimitry Andric #include "llvm/IR/Function.h"
215ffd83dbSDimitry Andric #include "llvm/IR/InstrTypes.h"
2281ad6265SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
250b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
260b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric TargetFrameLowering::~TargetFrameLowering() = default;
310b57cec5SDimitry Andric
enableCalleeSaveSkip(const MachineFunction & MF) const320b57cec5SDimitry Andric bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
330b57cec5SDimitry Andric assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
340b57cec5SDimitry Andric MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
350b57cec5SDimitry Andric !MF.getFunction().hasFnAttribute(Attribute::UWTable));
360b57cec5SDimitry Andric return false;
370b57cec5SDimitry Andric }
380b57cec5SDimitry 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
440b57cec5SDimitry Andric /// Returns the displacement from the frame register to the stack
450b57cec5SDimitry Andric /// frame of the specified index, along with the frame register used
460b57cec5SDimitry Andric /// (in output arg FrameReg). This is the default implementation which
470b57cec5SDimitry 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 {
510b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
520b57cec5SDimitry Andric const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric // By default, assume all frame indices are referenced via whatever
550b57cec5SDimitry Andric // getFrameRegister() says. The target can override this if it's doing
560b57cec5SDimitry Andric // something different.
570b57cec5SDimitry Andric FrameReg = RI->getFrameRegister(MF);
580b57cec5SDimitry Andric
59e8d8bef9SDimitry Andric return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
60e8d8bef9SDimitry Andric getOffsetOfLocalArea() +
61e8d8bef9SDimitry Andric MFI.getOffsetAdjustment());
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric
64*52418fc2SDimitry Andric /// Returns the offset from the stack pointer to the slot of the specified
65*52418fc2SDimitry Andric /// index. This function serves to provide a comparable offset from a single
66*52418fc2SDimitry Andric /// reference point (the value of the stack-pointer at function entry) that can
67*52418fc2SDimitry Andric /// be used for analysis. This is the default implementation using
68*52418fc2SDimitry Andric /// MachineFrameInfo offsets.
69*52418fc2SDimitry Andric StackOffset
getFrameIndexReferenceFromSP(const MachineFunction & MF,int FI) const70*52418fc2SDimitry Andric TargetFrameLowering::getFrameIndexReferenceFromSP(const MachineFunction &MF,
71*52418fc2SDimitry Andric int FI) const {
72*52418fc2SDimitry Andric // To display the true offset from SP, we need to subtract the offset to the
73*52418fc2SDimitry Andric // local area from MFI's ObjectOffset.
74*52418fc2SDimitry Andric return StackOffset::getFixed(MF.getFrameInfo().getObjectOffset(FI) -
75*52418fc2SDimitry Andric getOffsetOfLocalArea());
76*52418fc2SDimitry Andric }
77*52418fc2SDimitry Andric
needsFrameIndexResolution(const MachineFunction & MF) const780b57cec5SDimitry Andric bool TargetFrameLowering::needsFrameIndexResolution(
790b57cec5SDimitry Andric const MachineFunction &MF) const {
800b57cec5SDimitry Andric return MF.getFrameInfo().hasStackObjects();
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
getCalleeSaves(const MachineFunction & MF,BitVector & CalleeSaves) const83480093f4SDimitry Andric void TargetFrameLowering::getCalleeSaves(const MachineFunction &MF,
84480093f4SDimitry Andric BitVector &CalleeSaves) const {
85480093f4SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
86480093f4SDimitry Andric CalleeSaves.resize(TRI.getNumRegs());
87480093f4SDimitry Andric
88480093f4SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
89480093f4SDimitry Andric if (!MFI.isCalleeSavedInfoValid())
90480093f4SDimitry Andric return;
91480093f4SDimitry Andric
92480093f4SDimitry Andric for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
93480093f4SDimitry Andric CalleeSaves.set(Info.getReg());
94480093f4SDimitry Andric }
95480093f4SDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const960b57cec5SDimitry Andric void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
970b57cec5SDimitry Andric BitVector &SavedRegs,
980b57cec5SDimitry Andric RegScavenger *RS) const {
990b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric // Resize before the early returns. Some backends expect that
1020b57cec5SDimitry Andric // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
1030b57cec5SDimitry Andric // saved registers.
1040b57cec5SDimitry Andric SavedRegs.resize(TRI.getNumRegs());
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // When interprocedural register allocation is enabled caller saved registers
1070b57cec5SDimitry Andric // are preferred over callee saved registers.
1088bcb0991SDimitry Andric if (MF.getTarget().Options.EnableIPRA &&
1098bcb0991SDimitry Andric isSafeForNoCSROpt(MF.getFunction()) &&
1108bcb0991SDimitry Andric isProfitableForNoCSROpt(MF.getFunction()))
1110b57cec5SDimitry Andric return;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric // Get the callee saved register list...
1140b57cec5SDimitry Andric const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric // Early exit if there are no callee saved registers.
1170b57cec5SDimitry Andric if (!CSRegs || CSRegs[0] == 0)
1180b57cec5SDimitry Andric return;
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric // In Naked functions we aren't going to save any registers.
1210b57cec5SDimitry Andric if (MF.getFunction().hasFnAttribute(Attribute::Naked))
1220b57cec5SDimitry Andric return;
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric // Noreturn+nounwind functions never restore CSR, so no saves are needed.
1250b57cec5SDimitry Andric // Purely noreturn functions may still return through throws, so those must
1260b57cec5SDimitry Andric // save CSR for caller exception handlers.
1270b57cec5SDimitry Andric //
1280b57cec5SDimitry Andric // If the function uses longjmp to break out of its current path of
1290b57cec5SDimitry Andric // execution we do not need the CSR spills either: setjmp stores all CSRs
1300b57cec5SDimitry Andric // it was called with into the jmp_buf, which longjmp then restores.
1310b57cec5SDimitry Andric if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
1320b57cec5SDimitry Andric MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
1330b57cec5SDimitry Andric !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
1340b57cec5SDimitry Andric enableCalleeSaveSkip(MF))
1350b57cec5SDimitry Andric return;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric // Functions which call __builtin_unwind_init get all their registers saved.
1380b57cec5SDimitry Andric bool CallsUnwindInit = MF.callsUnwindInit();
1390b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
1400b57cec5SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) {
1410b57cec5SDimitry Andric unsigned Reg = CSRegs[i];
1420b57cec5SDimitry Andric if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
1430b57cec5SDimitry Andric SavedRegs.set(Reg);
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
allocateScavengingFrameIndexesNearIncomingSP(const MachineFunction & MF) const1474824e7fdSDimitry Andric bool TargetFrameLowering::allocateScavengingFrameIndexesNearIncomingSP(
1484824e7fdSDimitry Andric const MachineFunction &MF) const {
1494824e7fdSDimitry Andric if (!hasFP(MF))
1504824e7fdSDimitry Andric return false;
1514824e7fdSDimitry Andric
1524824e7fdSDimitry Andric const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
1534824e7fdSDimitry Andric return RegInfo->useFPForScavengingIndex(MF) &&
1544824e7fdSDimitry Andric !RegInfo->hasStackRealignment(MF);
1554824e7fdSDimitry Andric }
1564824e7fdSDimitry Andric
isSafeForNoCSROpt(const Function & F)1578bcb0991SDimitry Andric bool TargetFrameLowering::isSafeForNoCSROpt(const Function &F) {
1588bcb0991SDimitry Andric if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
1598bcb0991SDimitry Andric !F.hasFnAttribute(Attribute::NoRecurse))
1608bcb0991SDimitry Andric return false;
1618bcb0991SDimitry Andric // Function should not be optimized as tail call.
1628bcb0991SDimitry Andric for (const User *U : F.users())
1635ffd83dbSDimitry Andric if (auto *CB = dyn_cast<CallBase>(U))
1645ffd83dbSDimitry Andric if (CB->isTailCall())
1658bcb0991SDimitry Andric return false;
1668bcb0991SDimitry Andric return true;
1678bcb0991SDimitry Andric }
1688bcb0991SDimitry Andric
getInitialCFAOffset(const MachineFunction & MF) const1690b57cec5SDimitry Andric int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
1700b57cec5SDimitry Andric llvm_unreachable("getInitialCFAOffset() not implemented!");
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric
1735ffd83dbSDimitry Andric Register
getInitialCFARegister(const MachineFunction & MF) const1745ffd83dbSDimitry Andric TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) const {
1750b57cec5SDimitry Andric llvm_unreachable("getInitialCFARegister() not implemented!");
1760b57cec5SDimitry Andric }
1775ffd83dbSDimitry Andric
1785ffd83dbSDimitry Andric TargetFrameLowering::DwarfFrameBase
getDwarfFrameBase(const MachineFunction & MF) const1795ffd83dbSDimitry Andric TargetFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
1805ffd83dbSDimitry Andric const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
1815ffd83dbSDimitry Andric return DwarfFrameBase{DwarfFrameBase::Register, {RI->getFrameRegister(MF)}};
1825ffd83dbSDimitry Andric }
183