LLVM  14.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 
14 #include "llvm/ADT/BitVector.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/InstrTypes.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/Support/Compiler.h"
28 
29 using namespace llvm;
30 
32 
34  assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
35  MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
36  !MF.getFunction().hasFnAttribute(Attribute::UWTable));
37  return false;
38 }
39 
40 /// Returns the displacement from the frame register to the stack
41 /// frame of the specified index, along with the frame register used
42 /// (in output arg FrameReg). This is the default implementation which
43 /// is overridden for some targets.
46  Register &FrameReg) const {
47  const MachineFrameInfo &MFI = MF.getFrameInfo();
49 
50  // By default, assume all frame indices are referenced via whatever
51  // getFrameRegister() says. The target can override this if it's doing
52  // something different.
53  FrameReg = RI->getFrameRegister(MF);
54 
55  return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
57  MFI.getOffsetAdjustment());
58 }
59 
61  const MachineFunction &MF) const {
62  return MF.getFrameInfo().hasStackObjects();
63 }
64 
66  BitVector &CalleeSaves) const {
68  CalleeSaves.resize(TRI.getNumRegs());
69 
70  const MachineFrameInfo &MFI = MF.getFrameInfo();
71  if (!MFI.isCalleeSavedInfoValid())
72  return;
73 
74  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
75  CalleeSaves.set(Info.getReg());
76 }
77 
79  BitVector &SavedRegs,
80  RegScavenger *RS) const {
82 
83  // Resize before the early returns. Some backends expect that
84  // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
85  // saved registers.
86  SavedRegs.resize(TRI.getNumRegs());
87 
88  // When interprocedural register allocation is enabled caller saved registers
89  // are preferred over callee saved registers.
90  if (MF.getTarget().Options.EnableIPRA &&
93  return;
94 
95  // Get the callee saved register list...
96  const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
97 
98  // Early exit if there are no callee saved registers.
99  if (!CSRegs || CSRegs[0] == 0)
100  return;
101 
102  // In Naked functions we aren't going to save any registers.
103  if (MF.getFunction().hasFnAttribute(Attribute::Naked))
104  return;
105 
106  // Noreturn+nounwind functions never restore CSR, so no saves are needed.
107  // Purely noreturn functions may still return through throws, so those must
108  // save CSR for caller exception handlers.
109  //
110  // If the function uses longjmp to break out of its current path of
111  // execution we do not need the CSR spills either: setjmp stores all CSRs
112  // it was called with into the jmp_buf, which longjmp then restores.
113  if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
114  MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
115  !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
117  return;
118 
119  // Functions which call __builtin_unwind_init get all their registers saved.
120  bool CallsUnwindInit = MF.callsUnwindInit();
121  const MachineRegisterInfo &MRI = MF.getRegInfo();
122  for (unsigned i = 0; CSRegs[i]; ++i) {
123  unsigned Reg = CSRegs[i];
124  if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
125  SavedRegs.set(Reg);
126  }
127 }
128 
130  const MachineFunction &MF) const {
131  // When HHVM function is called, the stack is skewed as the return address
132  // is removed from the stack before we enter the function.
134  return MF.getTarget().getAllocaPointerSize();
135 
136  return 0;
137 }
138 
140  if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
141  !F.hasFnAttribute(Attribute::NoRecurse))
142  return false;
143  // Function should not be optimized as tail call.
144  for (const User *U : F.users())
145  if (auto *CB = dyn_cast<CallBase>(U))
146  if (CB->isTailCall())
147  return false;
148  return true;
149 }
150 
152  llvm_unreachable("getInitialCFAOffset() not implemented!");
153 }
154 
155 Register
157  llvm_unreachable("getInitialCFARegister() not implemented!");
158 }
159 
164 }
i
i
Definition: README.txt:29
llvm::TargetFrameLowering::getCalleeSaves
virtual void getCalleeSaves(const MachineFunction &MF, BitVector &SavedRegs) const
Returns the callee-saved registers as computed by determineCalleeSaves in the BitVector SavedRegs.
Definition: TargetFrameLoweringImpl.cpp:65
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::TargetFrameLowering::needsFrameIndexResolution
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
Definition: TargetFrameLoweringImpl.cpp:60
TargetFrameLowering.h
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::Function
Definition: Function.h:61
llvm::BitVector::set
BitVector & set()
Definition: BitVector.h:343
llvm::MCRegisterInfo::getNumRegs
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
Definition: MCRegisterInfo.h:491
llvm::TargetFrameLowering::getInitialCFARegister
virtual Register getInitialCFARegister(const MachineFunction &MF) const
Return initial CFA register value i.e.
Definition: TargetFrameLoweringImpl.cpp:156
llvm::MachineFrameInfo::getOffsetAdjustment
int getOffsetAdjustment() const
Return the correction for frame offsets.
Definition: MachineFrameInfo.h:560
llvm::TargetSubtargetInfo::getRegisterInfo
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Definition: TargetSubtargetInfo.h:124
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:231
llvm::StackOffset::getFixed
ScalarTy getFixed() const
Definition: TypeSize.h:149
llvm::BitVector::resize
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:333
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:355
llvm::TargetFrameLowering::DwarfFrameBase
Definition: TargetFrameLowering.h:56
llvm::MachineFunction::callsUnwindInit
bool callsUnwindInit() const
Definition: MachineFunction.h:1037
llvm::TargetFrameLowering::getDwarfFrameBase
virtual DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const
Return the frame base information to be encoded in the DWARF subprogram debug info.
Definition: TargetFrameLoweringImpl.cpp:161
llvm::TargetFrameLowering::~TargetFrameLowering
virtual ~TargetFrameLowering()
llvm::TargetFrameLowering::enableCalleeSaveSkip
virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const
Returns true if the target can safely skip saving callee-saved registers for noreturn nounwind functi...
Definition: TargetFrameLoweringImpl.cpp:33
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
F
#define F(x, y, z)
Definition: MD5.cpp:56
MachineRegisterInfo.h
llvm::MachineFrameInfo::hasStackObjects
bool hasStackObjects() const
Return true if there are any stack objects in this function.
Definition: MachineFrameInfo.h:346
llvm::TargetFrameLowering::isProfitableForNoCSROpt
virtual bool isProfitableForNoCSROpt(const Function &F) const
Check if the no-CSR optimisation is profitable for the given function.
Definition: TargetFrameLowering.h:433
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:632
llvm::TargetFrameLowering::getOffsetOfLocalArea
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
Definition: TargetFrameLowering.h:140
TargetMachine.h
llvm::TargetFrameLowering::DwarfFrameBase::Register
@ Register
Definition: TargetFrameLowering.h:59
llvm::User
Definition: User.h:44
llvm::TargetOptions::EnableIPRA
unsigned EnableIPRA
This flag enables InterProcedural Register Allocation (IPRA).
Definition: TargetOptions.h:269
InstrTypes.h
BitVector.h
llvm::MachineFrameInfo::getStackSize
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Definition: MachineFrameInfo.h:551
llvm::MachineFrameInfo::getObjectOffset
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
Definition: MachineFrameInfo.h:492
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::BitVector
Definition: BitVector.h:74
llvm::MachineFrameInfo::isCalleeSavedInfoValid
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
Definition: MachineFrameInfo.h:766
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:622
llvm::Function::getCallingConv
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:238
llvm::TargetRegisterInfo::getFrameRegister
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
llvm::MachineRegisterInfo::getCalleeSavedRegs
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
Definition: MachineRegisterInfo.cpp:621
llvm::RegScavenger
Definition: RegisterScavenging.h:34
MCRegisterInfo.h
llvm::TargetFrameLowering::isSafeForNoCSROpt
static bool isSafeForNoCSROpt(const Function &F)
Check if given function is safe for not having callee saved registers.
Definition: TargetFrameLoweringImpl.cpp:139
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:115
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:638
llvm::MachineFunction
Definition: MachineFunction.h:230
TargetOptions.h
llvm::MachineFrameInfo::getCalleeSavedInfo
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
Definition: MachineFrameInfo.h:753
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::StackOffset
StackOffset is a class to represent an offset with 2 dimensions, named fixed and scalable,...
Definition: TypeSize.h:134
Compiler.h
TargetSubtargetInfo.h
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
CallingConv.h
Attributes.h
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:588
llvm::CalleeSavedInfo
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
Definition: MachineFrameInfo.h:34
uint16_t
llvm::MachineFunction::getTarget
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Definition: MachineFunction.h:618
MachineFrameInfo.h
llvm::TargetFrameLowering::getFrameIndexReference
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...
Definition: TargetFrameLoweringImpl.cpp:45
Function.h
llvm::MachineRegisterInfo::isPhysRegModified
bool isPhysRegModified(MCRegister PhysReg, bool SkipNoReturnDef=false) const
Return true if the specified register is modified in this function.
Definition: MachineRegisterInfo.cpp:570
llvm::TargetMachine::getAllocaPointerSize
unsigned getAllocaPointerSize() const
Definition: TargetMachine.h:192
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:107
llvm::CallingConv::HHVM
@ HHVM
Calling convention used by HipHop Virtual Machine (HHVM) to perform calls to and from translation cac...
Definition: CallingConv.h:179
llvm::TargetFrameLowering::getInitialCFAOffset
virtual int getInitialCFAOffset(const MachineFunction &MF) const
Return initial CFA offset value i.e.
Definition: TargetFrameLoweringImpl.cpp:151
llvm::TargetFrameLowering::determineCalleeSaves
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Definition: TargetFrameLoweringImpl.cpp:78
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:220
MachineFunction.h
TargetRegisterInfo.h
llvm::TargetFrameLowering::getStackAlignmentSkew
virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const
Return the skew that has to be applied to stack alignment under certain conditions (e....
Definition: TargetFrameLoweringImpl.cpp:129