LLVM 24.0.0git
TargetFrameLoweringImpl.cpp
Go to the documentation of this file.
1//===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implements the layout of a stack frame on the target machine.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/BitVector.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/InstrTypes.h"
26
27using namespace llvm;
28
30
32 assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
33 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
34 !MF.getFunction().hasFnAttribute(Attribute::UWTable));
35 return false;
36}
37
41
42/// Returns the displacement from the frame register to the stack
43/// frame of the specified index, along with the frame register used
44/// (in output arg FrameReg). This is the default implementation which
45/// is overridden for some targets.
48 Register &FrameReg) const {
49 const MachineFrameInfo &MFI = MF.getFrameInfo();
51
52 // By default, assume all frame indices are referenced via whatever
53 // getFrameRegister() says. The target can override this if it's doing
54 // something different.
55 FrameReg = RI->getFrameRegister(MF);
56
60}
61
62/// Returns the offset from the stack pointer to the slot of the specified
63/// index. This function serves to provide a comparable offset from a single
64/// reference point (the value of the stack-pointer at function entry) that can
65/// be used for analysis. This is the default implementation using
66/// MachineFrameInfo offsets.
69 int FI) const {
70 // To display the true offset from SP, we need to subtract the offset to the
71 // local area from MFI's ObjectOffset.
74}
75
80
82 BitVector &CalleeSaves) const {
84 CalleeSaves.resize(TRI.getNumRegs());
85
86 const MachineFrameInfo &MFI = MF.getFrameInfo();
87 if (!MFI.isCalleeSavedInfoValid())
88 return;
89
90 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
91 CalleeSaves.set(Info.getReg());
92}
93
94const MCPhysReg *
97
98 // Get the callee saved register list...
99 const MCPhysReg *CSRegs = nullptr;
100
101 // When interprocedural register allocation is enabled, callee saved register
102 // list should be empty, since caller saved registers are preferred over
103 // callee saved registers. Unless it has some risked CSR to be optimized out.
104 if (MF.getTarget().Options.EnableIPRA &&
107 CSRegs = TRI.getIPRACSRegs(&MF);
108 else
109 CSRegs = MF.getRegInfo().getCalleeSavedRegs();
110
111 // Early exit if there are no callee saved registers.
112 if (!CSRegs || CSRegs[0] == 0)
113 return nullptr;
114
115 // In Naked functions we aren't going to save any registers.
116 if (MF.getFunction().hasFnAttribute(Attribute::Naked))
117 return nullptr;
118
119 // Noreturn+nounwind functions never restore CSR, so no saves are needed.
120 // Purely noreturn functions may still return through throws, so those must
121 // save CSR for caller exception handlers.
122 //
123 // If the function uses longjmp to break out of its current path of
124 // execution we do not need the CSR spills either: setjmp stores all CSRs
125 // it was called with into the jmp_buf, which longjmp then restores.
126 if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
127 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
128 !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
130 return nullptr;
131
132 return CSRegs;
133}
134
136 MachineFunction &MF, const MCPhysReg *CSRegs, BitVector &SavedRegs) const {
137 // Functions which call __builtin_unwind_init get all their registers saved.
138 if (!MF.callsUnwindInit())
139 return;
140 for (unsigned i = 0; CSRegs[i]; ++i) {
141 unsigned Reg = CSRegs[i];
142 SavedRegs.set(Reg);
143 }
144}
145
147 BitVector &PrologCSRs,
148 RegScavenger *RS) const {
150
151 // Resize before the early returns. Some backends expect that
152 // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
153 // saved registers.
154 PrologCSRs.resize(TRI.getNumRegs());
155
156 // Get the callee saved register list...
157 const MCPhysReg *CSRegs = getMustPreserveRegisters(MF);
158 // Early exit if there are no callee saved registers.
159 if (!CSRegs || CSRegs[0] == 0)
160 return;
161
162 determineUncondPrologCalleeSaves(MF, CSRegs, PrologCSRs);
163
164 const MachineRegisterInfo &MRI = MF.getRegInfo();
165 for (unsigned i = 0; CSRegs[i]; ++i) {
166 unsigned Reg = CSRegs[i];
167 if (MRI.isPhysRegModified(Reg))
168 PrologCSRs.set(Reg);
169 }
170}
171
173 const MachineFunction &MF) const {
174 if (!hasFP(MF))
175 return false;
176
177 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
178 return RegInfo->useFPForScavengingIndex(MF) &&
179 !RegInfo->hasStackRealignment(MF);
180}
181
183 if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
184 !F.hasFnAttribute(Attribute::NoRecurse))
185 return false;
186 // Function should not be optimized as tail call.
187 for (const User *U : F.users())
188 if (auto *CB = dyn_cast<CallBase>(U))
189 if (CB->isTailCall())
190 return false;
191 return true;
192}
193
195 llvm_unreachable("getInitialCFAOffset() not implemented!");
196}
197
200 llvm_unreachable("getInitialCFARegister() not implemented!");
201}
202
208
211 const CalleeSavedInfo &CS, const TargetInstrInfo *TII,
212 const TargetRegisterInfo *TRI) const {
213 // Insert the spill to the stack frame.
214 MCRegister Reg = CS.getReg();
215
216 if (CS.isSpilledToReg()) {
217 BuildMI(SaveBlock, MI, DebugLoc(), TII->get(TargetOpcode::COPY),
218 CS.getDstReg())
219 .addReg(Reg, getKillRegState(true));
220 } else {
221 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
222 TII->storeRegToStackSlot(SaveBlock, MI, Reg, true, CS.getFrameIdx(), RC,
223 Register());
224 }
225}
226
229 const CalleeSavedInfo &CS, const TargetInstrInfo *TII,
230 const TargetRegisterInfo *TRI) const {
231 MCRegister Reg = CS.getReg();
232 if (CS.isSpilledToReg()) {
233 BuildMI(MBB, MI, DebugLoc(), TII->get(TargetOpcode::COPY), Reg)
234 .addReg(CS.getDstReg(), getKillRegState(true));
235 } else {
236 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
237 TII->loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, Register());
238 assert(MI != MBB.begin() && "loadRegFromStackSlot didn't insert any code!");
239 }
240}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file contains the simple types necessary to represent the attributes associated with functions a...
This file implements the BitVector class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
Register const TargetRegisterInfo * TRI
MachineRegisterInfo & MRI
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:355
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
MCRegister getReg() const
MCRegister getDstReg() const
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool hasStackObjects() const
Return true if there are any stack objects in this function.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
bool usesWindowsUnwindInfo() const
True if this function describes its unwind information with Windows SEH unwind opcodes (....
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr unsigned id() const
Definition Register.h:100
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:31
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:47
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual bool allocateScavengingFrameIndexesNearIncomingSP(const MachineFunction &MF) const
Control the placement of special register scavenging spill slots when allocating a stack frame.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const
Returns true if the target can safely skip saving callee-saved registers for noreturn nounwind functi...
void restoreCalleeSavedRegister(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const CalleeSavedInfo &CS, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
void spillCalleeSavedRegister(MachineBasicBlock &SaveBlock, MachineBasicBlock::iterator MI, const CalleeSavedInfo &CS, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
spillCalleeSavedRegister - Default implementation for spilling a single callee saved register.
void determineUncondPrologCalleeSaves(MachineFunction &MF, const MCPhysReg *CSRegs, BitVector &UncondPrologCSRs) const
This method determines which of the registers reported by getMustPreserveRegisters() must be saved in...
virtual Register getInitialCFARegister(const MachineFunction &MF) const
Return initial CFA register value i.e.
virtual void getCalleeSaves(const MachineFunction &MF, BitVector &SavedRegs) const
Returns the callee-saved registers as computed by determineCalleeSaves in the BitVector SavedRegs.
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
virtual DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const
Return the frame base information to be encoded in the DWARF subprogram debug info.
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
virtual bool isProfitableForNoCSROpt(const Function &F) const
Check if the no-CSR optimisation is profitable for the given function.
static bool isSafeForNoCSROpt(const Function &F)
Check if given function is safe for not having callee saved registers.
virtual bool enableCFIFixup(const MachineFunction &MF) const
Returns true if we may need to fix the unwind information for the function.
virtual StackOffset getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI) const
getFrameIndexReferenceFromSP - This method returns the offset from the stack pointer to the slot of t...
virtual int getInitialCFAOffset(const MachineFunction &MF) const
Return initial CFA offset value i.e.
const MCPhysReg * getMustPreserveRegisters(const MachineFunction &MF) const
Return the list of registers which must be preserved by the function: the value on exit must be the s...
virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
TargetInstrInfo - Interface to description of machine instruction set.
TargetOptions Options
unsigned EnableIPRA
This flag enables InterProcedural Register Allocation (IPRA).
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual bool useFPForScavengingIndex(const MachineFunction &MF) const
Returns true if the target wants to use frame pointer based accesses to spill to the scavenger emerge...
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58