LLVM  10.0.0
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/CallSite.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/IR/Function.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.
45  int FI, unsigned &FrameReg) const {
46  const MachineFrameInfo &MFI = MF.getFrameInfo();
48 
49  // By default, assume all frame indices are referenced via whatever
50  // getFrameRegister() says. The target can override this if it's doing
51  // something different.
52  FrameReg = RI->getFrameRegister(MF);
53 
54  return MFI.getObjectOffset(FI) + MFI.getStackSize() -
56 }
57 
59  const MachineFunction &MF) const {
60  return MF.getFrameInfo().hasStackObjects();
61 }
62 
64  BitVector &CalleeSaves) const {
66  CalleeSaves.resize(TRI.getNumRegs());
67 
68  const MachineFrameInfo &MFI = MF.getFrameInfo();
69  if (!MFI.isCalleeSavedInfoValid())
70  return;
71 
72  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
73  CalleeSaves.set(Info.getReg());
74 }
75 
77  BitVector &SavedRegs,
78  RegScavenger *RS) const {
80 
81  // Resize before the early returns. Some backends expect that
82  // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
83  // saved registers.
84  SavedRegs.resize(TRI.getNumRegs());
85 
86  // When interprocedural register allocation is enabled caller saved registers
87  // are preferred over callee saved registers.
88  if (MF.getTarget().Options.EnableIPRA &&
91  return;
92 
93  // Get the callee saved register list...
94  const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
95 
96  // Early exit if there are no callee saved registers.
97  if (!CSRegs || CSRegs[0] == 0)
98  return;
99 
100  // In Naked functions we aren't going to save any registers.
101  if (MF.getFunction().hasFnAttribute(Attribute::Naked))
102  return;
103 
104  // Noreturn+nounwind functions never restore CSR, so no saves are needed.
105  // Purely noreturn functions may still return through throws, so those must
106  // save CSR for caller exception handlers.
107  //
108  // If the function uses longjmp to break out of its current path of
109  // execution we do not need the CSR spills either: setjmp stores all CSRs
110  // it was called with into the jmp_buf, which longjmp then restores.
111  if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
112  MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
113  !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
115  return;
116 
117  // Functions which call __builtin_unwind_init get all their registers saved.
118  bool CallsUnwindInit = MF.callsUnwindInit();
119  const MachineRegisterInfo &MRI = MF.getRegInfo();
120  for (unsigned i = 0; CSRegs[i]; ++i) {
121  unsigned Reg = CSRegs[i];
122  if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
123  SavedRegs.set(Reg);
124  }
125 }
126 
128  const MachineFunction &MF) const {
129  // When HHVM function is called, the stack is skewed as the return address
130  // is removed from the stack before we enter the function.
132  return MF.getTarget().getAllocaPointerSize();
133 
134  return 0;
135 }
136 
138  if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
139  !F.hasFnAttribute(Attribute::NoRecurse))
140  return false;
141  // Function should not be optimized as tail call.
142  for (const User *U : F.users())
143  if (auto CS = ImmutableCallSite(U))
144  if (CS.isTailCall())
145  return false;
146  return true;
147 }
148 
150  llvm_unreachable("getInitialCFAOffset() not implemented!");
151 }
152 
154  const {
155  llvm_unreachable("getInitialCFARegister() not implemented!");
156 }
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:371
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
BitVector & set()
Definition: BitVector.h:397
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Calling convention used by HipHop Virtual Machine (HHVM) to perform calls to and from translation cac...
Definition: CallingConv.h:174
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:218
virtual int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool hasStackObjects() const
Return true if there are any stack objects in this function.
bool isPhysRegModified(unsigned PhysReg, bool SkipNoReturnDef=false) const
Return true if the specified register is modified in this function.
unsigned Reg
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
unsigned const TargetRegisterInfo * TRI
F(f)
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
unsigned EnableIPRA
This flag enables InterProcedural Register Allocation (IPRA).
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
This file contains the simple types necessary to represent the attributes associated with functions a...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const
Return the skew that has to be applied to stack alignment under certain conditions (e...
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
unsigned getAllocaPointerSize() const
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool isSafeForNoCSROpt(const Function &F)
Check if given function is safe for not having callee saved registers.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
bool callsUnwindInit() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
iterator_range< user_iterator > users()
Definition: Value.h:420
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
virtual void getCalleeSaves(const MachineFunction &MF, BitVector &SavedRegs) const
Returns the callee-saved registers as computed by determineCalleeSaves in the BitVector SavedRegs...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const
Returns true if the target can safely skip saving callee-saved registers for noreturn nounwind functi...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
TargetOptions Options
Establish a view to a call site for examination.
Definition: CallSite.h:697
int getOffsetAdjustment() const
Return the correction for frame offsets.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
bool hasAddressTaken(const User **=nullptr) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
Definition: Function.cpp:1439
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
virtual int getInitialCFAOffset(const MachineFunction &MF) const
Return initial CFA offset value i.e.
virtual unsigned getInitialCFARegister(const MachineFunction &MF) const
Return initial CFA register value i.e.
virtual bool isProfitableForNoCSROpt(const Function &F) const
Check if the no-CSR optimisation is profitable for the given function.