xref: /src/contrib/llvm-project/llvm/lib/CodeGen/TargetFrameLoweringImpl.cpp (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
17ab83427SDimitry Andric //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
263faed5bSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663faed5bSDimitry Andric //
763faed5bSDimitry Andric //===----------------------------------------------------------------------===//
863faed5bSDimitry Andric //
963faed5bSDimitry Andric // Implements the layout of a stack frame on the target machine.
1063faed5bSDimitry Andric //
1163faed5bSDimitry Andric //===----------------------------------------------------------------------===//
1263faed5bSDimitry Andric 
13ee8648bdSDimitry Andric #include "llvm/ADT/BitVector.h"
1463faed5bSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
1563faed5bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
16ee8648bdSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
17145449b1SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
18044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
197ab83427SDimitry Andric #include "llvm/IR/Attributes.h"
205a5ac124SDimitry Andric #include "llvm/IR/Function.h"
21cfca06d7SDimitry Andric #include "llvm/IR/InstrTypes.h"
22145449b1SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
237ab83427SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
247ab83427SDimitry Andric #include "llvm/Support/Compiler.h"
257ab83427SDimitry Andric #include "llvm/Target/TargetMachine.h"
267ab83427SDimitry Andric #include "llvm/Target/TargetOptions.h"
277ab83427SDimitry Andric 
2863faed5bSDimitry Andric using namespace llvm;
2963faed5bSDimitry Andric 
307ab83427SDimitry Andric TargetFrameLowering::~TargetFrameLowering() = default;
3163faed5bSDimitry Andric 
enableCalleeSaveSkip(const MachineFunction & MF) const32eb11fae6SDimitry Andric bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
33eb11fae6SDimitry Andric   assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
34eb11fae6SDimitry Andric          MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
35eb11fae6SDimitry Andric          !MF.getFunction().hasFnAttribute(Attribute::UWTable));
36eb11fae6SDimitry Andric   return false;
37eb11fae6SDimitry Andric }
38eb11fae6SDimitry Andric 
enableCFIFixup(MachineFunction & MF) const39145449b1SDimitry Andric bool TargetFrameLowering::enableCFIFixup(MachineFunction &MF) const {
40145449b1SDimitry Andric   return MF.needsFrameMoves() &&
41145449b1SDimitry Andric          !MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
42145449b1SDimitry Andric }
43145449b1SDimitry Andric 
44dd58ef01SDimitry Andric /// Returns the displacement from the frame register to the stack
45dd58ef01SDimitry Andric /// frame of the specified index, along with the frame register used
46dd58ef01SDimitry Andric /// (in output arg FrameReg). This is the default implementation which
47dd58ef01SDimitry Andric /// is overridden for some targets.
48b60736ecSDimitry Andric StackOffset
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const49b60736ecSDimitry Andric TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
50cfca06d7SDimitry Andric                                             Register &FrameReg) const {
51b915e9e0SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
5267c32a98SDimitry Andric   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
5363faed5bSDimitry Andric 
5463faed5bSDimitry Andric   // By default, assume all frame indices are referenced via whatever
5563faed5bSDimitry Andric   // getFrameRegister() says. The target can override this if it's doing
5663faed5bSDimitry Andric   // something different.
5763faed5bSDimitry Andric   FrameReg = RI->getFrameRegister(MF);
58dd58ef01SDimitry Andric 
59b60736ecSDimitry Andric   return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
60b60736ecSDimitry Andric                                getOffsetOfLocalArea() +
61b60736ecSDimitry Andric                                MFI.getOffsetAdjustment());
6263faed5bSDimitry Andric }
635a5ac124SDimitry Andric 
649b950333SDimitry Andric /// Returns the offset from the stack pointer to the slot of the specified
659b950333SDimitry Andric /// index. This function serves to provide a comparable offset from a single
669b950333SDimitry Andric /// reference point (the value of the stack-pointer at function entry) that can
679b950333SDimitry Andric /// be used for analysis. This is the default implementation using
689b950333SDimitry Andric /// MachineFrameInfo offsets.
699b950333SDimitry Andric StackOffset
getFrameIndexReferenceFromSP(const MachineFunction & MF,int FI) const709b950333SDimitry Andric TargetFrameLowering::getFrameIndexReferenceFromSP(const MachineFunction &MF,
719b950333SDimitry Andric                                                   int FI) const {
729b950333SDimitry Andric   // To display the true offset from SP, we need to subtract the offset to the
739b950333SDimitry Andric   // local area from MFI's ObjectOffset.
749b950333SDimitry Andric   return StackOffset::getFixed(MF.getFrameInfo().getObjectOffset(FI) -
759b950333SDimitry Andric                                getOffsetOfLocalArea());
769b950333SDimitry Andric }
779b950333SDimitry Andric 
needsFrameIndexResolution(const MachineFunction & MF) const785a5ac124SDimitry Andric bool TargetFrameLowering::needsFrameIndexResolution(
795a5ac124SDimitry Andric     const MachineFunction &MF) const {
80b915e9e0SDimitry Andric   return MF.getFrameInfo().hasStackObjects();
815a5ac124SDimitry Andric }
82ee8648bdSDimitry Andric 
getCalleeSaves(const MachineFunction & MF,BitVector & CalleeSaves) const83706b4fc4SDimitry Andric void TargetFrameLowering::getCalleeSaves(const MachineFunction &MF,
84706b4fc4SDimitry Andric                                          BitVector &CalleeSaves) const {
85706b4fc4SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
86706b4fc4SDimitry Andric   CalleeSaves.resize(TRI.getNumRegs());
87706b4fc4SDimitry Andric 
88706b4fc4SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
89706b4fc4SDimitry Andric   if (!MFI.isCalleeSavedInfoValid())
90706b4fc4SDimitry Andric     return;
91706b4fc4SDimitry Andric 
92706b4fc4SDimitry Andric   for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
93706b4fc4SDimitry Andric     CalleeSaves.set(Info.getReg());
94706b4fc4SDimitry Andric }
95706b4fc4SDimitry Andric 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const96ee8648bdSDimitry Andric void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
97ee8648bdSDimitry Andric                                                BitVector &SavedRegs,
98ee8648bdSDimitry Andric                                                RegScavenger *RS) const {
99ee8648bdSDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10001095a5dSDimitry Andric 
10101095a5dSDimitry Andric   // Resize before the early returns. Some backends expect that
10201095a5dSDimitry Andric   // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
10301095a5dSDimitry Andric   // saved registers.
10401095a5dSDimitry Andric   SavedRegs.resize(TRI.getNumRegs());
10501095a5dSDimitry Andric 
10601095a5dSDimitry Andric   // When interprocedural register allocation is enabled caller saved registers
10701095a5dSDimitry Andric   // are preferred over callee saved registers.
1081d5ae102SDimitry Andric   if (MF.getTarget().Options.EnableIPRA &&
1091d5ae102SDimitry Andric       isSafeForNoCSROpt(MF.getFunction()) &&
1101d5ae102SDimitry Andric       isProfitableForNoCSROpt(MF.getFunction()))
11101095a5dSDimitry Andric     return;
11201095a5dSDimitry Andric 
11301095a5dSDimitry Andric   // Get the callee saved register list...
11471d5a254SDimitry Andric   const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
115ee8648bdSDimitry Andric 
116ee8648bdSDimitry Andric   // Early exit if there are no callee saved registers.
117ee8648bdSDimitry Andric   if (!CSRegs || CSRegs[0] == 0)
118ee8648bdSDimitry Andric     return;
119ee8648bdSDimitry Andric 
120ee8648bdSDimitry Andric   // In Naked functions we aren't going to save any registers.
121044eb2f6SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
122ee8648bdSDimitry Andric     return;
123ee8648bdSDimitry Andric 
124eb11fae6SDimitry Andric   // Noreturn+nounwind functions never restore CSR, so no saves are needed.
125eb11fae6SDimitry Andric   // Purely noreturn functions may still return through throws, so those must
126eb11fae6SDimitry Andric   // save CSR for caller exception handlers.
127eb11fae6SDimitry Andric   //
128eb11fae6SDimitry Andric   // If the function uses longjmp to break out of its current path of
129eb11fae6SDimitry Andric   // execution we do not need the CSR spills either: setjmp stores all CSRs
130eb11fae6SDimitry Andric   // it was called with into the jmp_buf, which longjmp then restores.
131eb11fae6SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
132eb11fae6SDimitry Andric         MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
133eb11fae6SDimitry Andric         !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
134eb11fae6SDimitry Andric         enableCalleeSaveSkip(MF))
135eb11fae6SDimitry Andric     return;
136eb11fae6SDimitry Andric 
137ee8648bdSDimitry Andric   // Functions which call __builtin_unwind_init get all their registers saved.
138b915e9e0SDimitry Andric   bool CallsUnwindInit = MF.callsUnwindInit();
139ee8648bdSDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
140ee8648bdSDimitry Andric   for (unsigned i = 0; CSRegs[i]; ++i) {
141ee8648bdSDimitry Andric     unsigned Reg = CSRegs[i];
142ee8648bdSDimitry Andric     if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
143ee8648bdSDimitry Andric       SavedRegs.set(Reg);
144ee8648bdSDimitry Andric   }
145ee8648bdSDimitry Andric }
146dd58ef01SDimitry Andric 
allocateScavengingFrameIndexesNearIncomingSP(const MachineFunction & MF) const147f65dcba8SDimitry Andric bool TargetFrameLowering::allocateScavengingFrameIndexesNearIncomingSP(
148f65dcba8SDimitry Andric   const MachineFunction &MF) const {
149f65dcba8SDimitry Andric   if (!hasFP(MF))
150f65dcba8SDimitry Andric     return false;
151f65dcba8SDimitry Andric 
152f65dcba8SDimitry Andric   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
153f65dcba8SDimitry Andric   return RegInfo->useFPForScavengingIndex(MF) &&
154f65dcba8SDimitry Andric          !RegInfo->hasStackRealignment(MF);
155f65dcba8SDimitry Andric }
156f65dcba8SDimitry Andric 
isSafeForNoCSROpt(const Function & F)1571d5ae102SDimitry Andric bool TargetFrameLowering::isSafeForNoCSROpt(const Function &F) {
1581d5ae102SDimitry Andric   if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
1591d5ae102SDimitry Andric       !F.hasFnAttribute(Attribute::NoRecurse))
1601d5ae102SDimitry Andric     return false;
1611d5ae102SDimitry Andric   // Function should not be optimized as tail call.
1621d5ae102SDimitry Andric   for (const User *U : F.users())
163cfca06d7SDimitry Andric     if (auto *CB = dyn_cast<CallBase>(U))
164cfca06d7SDimitry Andric       if (CB->isTailCall())
1651d5ae102SDimitry Andric         return false;
1661d5ae102SDimitry Andric   return true;
1671d5ae102SDimitry Andric }
1681d5ae102SDimitry Andric 
getInitialCFAOffset(const MachineFunction & MF) const169eb11fae6SDimitry Andric int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
170eb11fae6SDimitry Andric   llvm_unreachable("getInitialCFAOffset() not implemented!");
171eb11fae6SDimitry Andric }
172eb11fae6SDimitry Andric 
173cfca06d7SDimitry Andric Register
getInitialCFARegister(const MachineFunction & MF) const174cfca06d7SDimitry Andric TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) const {
175eb11fae6SDimitry Andric   llvm_unreachable("getInitialCFARegister() not implemented!");
176eb11fae6SDimitry Andric }
177cfca06d7SDimitry Andric 
178cfca06d7SDimitry Andric TargetFrameLowering::DwarfFrameBase
getDwarfFrameBase(const MachineFunction & MF) const179cfca06d7SDimitry Andric TargetFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
180cfca06d7SDimitry Andric   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
181cfca06d7SDimitry Andric   return DwarfFrameBase{DwarfFrameBase::Register, {RI->getFrameRegister(MF)}};
182cfca06d7SDimitry Andric }
183