LLVM  16.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"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/InstrTypes.h"
23 #include "llvm/MC/MCAsmInfo.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 
41  return MF.needsFrameMoves() &&
43 }
44 
45 /// Returns the displacement from the frame register to the stack
46 /// frame of the specified index, along with the frame register used
47 /// (in output arg FrameReg). This is the default implementation which
48 /// is overridden for some targets.
51  Register &FrameReg) const {
52  const MachineFrameInfo &MFI = MF.getFrameInfo();
54 
55  // By default, assume all frame indices are referenced via whatever
56  // getFrameRegister() says. The target can override this if it's doing
57  // something different.
58  FrameReg = RI->getFrameRegister(MF);
59 
60  return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
62  MFI.getOffsetAdjustment());
63 }
64 
66  const MachineFunction &MF) const {
67  return MF.getFrameInfo().hasStackObjects();
68 }
69 
71  BitVector &CalleeSaves) const {
73  CalleeSaves.resize(TRI.getNumRegs());
74 
75  const MachineFrameInfo &MFI = MF.getFrameInfo();
76  if (!MFI.isCalleeSavedInfoValid())
77  return;
78 
79  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
80  CalleeSaves.set(Info.getReg());
81 }
82 
84  BitVector &SavedRegs,
85  RegScavenger *RS) const {
87 
88  // Resize before the early returns. Some backends expect that
89  // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
90  // saved registers.
91  SavedRegs.resize(TRI.getNumRegs());
92 
93  // When interprocedural register allocation is enabled caller saved registers
94  // are preferred over callee saved registers.
95  if (MF.getTarget().Options.EnableIPRA &&
98  return;
99 
100  // Get the callee saved register list...
101  const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
102 
103  // Early exit if there are no callee saved registers.
104  if (!CSRegs || CSRegs[0] == 0)
105  return;
106 
107  // In Naked functions we aren't going to save any registers.
108  if (MF.getFunction().hasFnAttribute(Attribute::Naked))
109  return;
110 
111  // Noreturn+nounwind functions never restore CSR, so no saves are needed.
112  // Purely noreturn functions may still return through throws, so those must
113  // save CSR for caller exception handlers.
114  //
115  // If the function uses longjmp to break out of its current path of
116  // execution we do not need the CSR spills either: setjmp stores all CSRs
117  // it was called with into the jmp_buf, which longjmp then restores.
118  if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
119  MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
120  !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
122  return;
123 
124  // Functions which call __builtin_unwind_init get all their registers saved.
125  bool CallsUnwindInit = MF.callsUnwindInit();
126  const MachineRegisterInfo &MRI = MF.getRegInfo();
127  for (unsigned i = 0; CSRegs[i]; ++i) {
128  unsigned Reg = CSRegs[i];
129  if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
130  SavedRegs.set(Reg);
131  }
132 }
133 
135  const MachineFunction &MF) const {
136  // When HHVM function is called, the stack is skewed as the return address
137  // is removed from the stack before we enter the function.
139  return MF.getTarget().getAllocaPointerSize();
140 
141  return 0;
142 }
143 
145  const MachineFunction &MF) const {
146  if (!hasFP(MF))
147  return false;
148 
149  const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
150  return RegInfo->useFPForScavengingIndex(MF) &&
151  !RegInfo->hasStackRealignment(MF);
152 }
153 
155  if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
156  !F.hasFnAttribute(Attribute::NoRecurse))
157  return false;
158  // Function should not be optimized as tail call.
159  for (const User *U : F.users())
160  if (auto *CB = dyn_cast<CallBase>(U))
161  if (CB->isTailCall())
162  return false;
163  return true;
164 }
165 
167  llvm_unreachable("getInitialCFAOffset() not implemented!");
168 }
169 
170 Register
172  llvm_unreachable("getInitialCFARegister() not implemented!");
173 }
174 
179 }
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:70
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::TargetFrameLowering::needsFrameIndexResolution
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
Definition: TargetFrameLoweringImpl.cpp:65
TargetFrameLowering.h
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:50
llvm::Function
Definition: Function.h:60
llvm::BitVector::set
BitVector & set()
Definition: BitVector.h:344
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:171
llvm::X86Disassembler::Reg
Reg
All possible values of the reg field in the ModR/M byte.
Definition: X86DisassemblerDecoder.h:462
llvm::MachineFrameInfo::getOffsetAdjustment
int getOffsetAdjustment() const
Return the correction for frame offsets.
Definition: MachineFrameInfo.h:586
llvm::TargetSubtargetInfo::getRegisterInfo
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Definition: TargetSubtargetInfo.h:125
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:237
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:334
llvm::TargetFrameLowering::DwarfFrameBase
Definition: TargetFrameLowering.h:56
llvm::MachineFunction::callsUnwindInit
bool callsUnwindInit() const
Definition: MachineFunction.h:1090
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:176
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
llvm::TargetRegisterInfo::useFPForScavengingIndex
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...
Definition: TargetRegisterInfo.h:921
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1628
F
#define F(x, y, z)
Definition: MD5.cpp:55
MachineRegisterInfo.h
llvm::MachineFrameInfo::hasStackObjects
bool hasStackObjects() const
Return true if there are any stack objects in this function.
Definition: MachineFrameInfo.h:349
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:451
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:666
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::enableCFIFixup
virtual bool enableCFIFixup(MachineFunction &MF) const
Returns true if we may need to fix the unwind information for the function.
Definition: TargetFrameLoweringImpl.cpp:40
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:297
InstrTypes.h
llvm::CallingConv::HHVM
@ HHVM
Used by HipHop Virtual Machine (HHVM) to perform calls to and from translation cache,...
Definition: CallingConv.h:165
llvm::TargetFrameLowering::hasFP
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register.
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:577
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:518
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::BitVector
Definition: BitVector.h:75
llvm::MachineFrameInfo::isCalleeSavedInfoValid
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
Definition: MachineFrameInfo.h:792
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:656
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:628
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:623
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:154
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:118
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:672
llvm::MachineFunction
Definition: MachineFunction.h:257
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:779
llvm::TargetMachine::getMCAsmInfo
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
Definition: TargetMachine.h:205
MCAsmInfo.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
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:622
llvm::MachineFunction::needsFrameMoves
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
Definition: MachineFunction.cpp:613
llvm::CalleeSavedInfo
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
Definition: MachineFrameInfo.h:33
llvm::TargetFrameLowering::allocateScavengingFrameIndexesNearIncomingSP
virtual bool allocateScavengingFrameIndexesNearIncomingSP(const MachineFunction &MF) const
Control the placement of special register scavenging spill slots when allocating a stack frame.
Definition: TargetFrameLoweringImpl.cpp:144
uint16_t
llvm::MachineFunction::getTarget
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Definition: MachineFunction.h:652
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:50
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:572
llvm::TargetMachine::getAllocaPointerSize
unsigned getAllocaPointerSize() const
Definition: TargetMachine.h:195
llvm::TargetRegisterInfo::hasStackRealignment
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
Definition: TargetRegisterInfo.h:968
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:105
llvm::TargetFrameLowering::getInitialCFAOffset
virtual int getInitialCFAOffset(const MachineFunction &MF) const
Return initial CFA offset value i.e.
Definition: TargetFrameLoweringImpl.cpp:166
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:83
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:210
MachineFunction.h
llvm::MCAsmInfo::usesWindowsCFI
bool usesWindowsCFI() const
Definition: MCAsmInfo.h:793
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:134