LLVM  9.0.0svn
TargetFrameLowering.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/TargetFrameLowering.h ----------------------*- C++ -*-===//
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 // Interface to describe the layout of a stack frame on the target machine.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_TARGETFRAMELOWERING_H
14 #define LLVM_CODEGEN_TARGETFRAMELOWERING_H
15 
17 #include <utility>
18 #include <vector>
19 
20 namespace llvm {
21  class BitVector;
22  class CalleeSavedInfo;
23  class MachineFunction;
24  class RegScavenger;
25 
26 /// Information about stack frame layout on the target. It holds the direction
27 /// of stack growth, the known stack alignment on entry to each function, and
28 /// the offset to the locals area.
29 ///
30 /// The offset to the local area is the offset from the stack pointer on
31 /// function entry to the first location where function data (local variables,
32 /// spill locations) can be stored.
34 public:
36  StackGrowsUp, // Adding to the stack increases the stack address
37  StackGrowsDown // Adding to the stack decreases the stack address
38  };
39 
40  // Maps a callee saved register to a stack slot with a fixed offset.
41  struct SpillSlot {
42  unsigned Reg;
43  int Offset; // Offset relative to stack pointer on function entry.
44  };
45 private:
46  StackDirection StackDir;
47  unsigned StackAlignment;
48  unsigned TransientStackAlignment;
49  int LocalAreaOffset;
50  bool StackRealignable;
51 public:
52  TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
53  unsigned TransAl = 1, bool StackReal = true)
54  : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
55  LocalAreaOffset(LAO), StackRealignable(StackReal) {}
56 
57  virtual ~TargetFrameLowering();
58 
59  // These methods return information that describes the abstract stack layout
60  // of the target machine.
61 
62  /// getStackGrowthDirection - Return the direction the stack grows
63  ///
64  StackDirection getStackGrowthDirection() const { return StackDir; }
65 
66  /// getStackAlignment - This method returns the number of bytes to which the
67  /// stack pointer must be aligned on entry to a function. Typically, this
68  /// is the largest alignment for any data object in the target.
69  ///
70  unsigned getStackAlignment() const { return StackAlignment; }
71 
72  /// alignSPAdjust - This method aligns the stack adjustment to the correct
73  /// alignment.
74  ///
75  int alignSPAdjust(int SPAdj) const {
76  if (SPAdj < 0) {
77  SPAdj = -alignTo(-SPAdj, StackAlignment);
78  } else {
79  SPAdj = alignTo(SPAdj, StackAlignment);
80  }
81  return SPAdj;
82  }
83 
84  /// getTransientStackAlignment - This method returns the number of bytes to
85  /// which the stack pointer must be aligned at all times, even between
86  /// calls.
87  ///
88  unsigned getTransientStackAlignment() const {
89  return TransientStackAlignment;
90  }
91 
92  /// isStackRealignable - This method returns whether the stack can be
93  /// realigned.
94  bool isStackRealignable() const {
95  return StackRealignable;
96  }
97 
98  /// Return the skew that has to be applied to stack alignment under
99  /// certain conditions (e.g. stack was adjusted before function \p MF
100  /// was called).
101  virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const;
102 
103  /// getOffsetOfLocalArea - This method returns the offset of the local area
104  /// from the stack pointer on entrance to a function.
105  ///
106  int getOffsetOfLocalArea() const { return LocalAreaOffset; }
107 
108  /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
109  /// the incoming stack pointer, false if it is close to the post-prologue
110  /// stack pointer.
111  virtual bool isFPCloseToIncomingSP() const { return true; }
112 
113  /// assignCalleeSavedSpillSlots - Allows target to override spill slot
114  /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
115  /// assign frame slots to all CSI entries and return true. If this method
116  /// returns false, spill slots will be assigned using generic implementation.
117  /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
118  /// CSI.
119  virtual bool
121  const TargetRegisterInfo *TRI,
122  std::vector<CalleeSavedInfo> &CSI) const {
123  return false;
124  }
125 
126  /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
127  /// pairs, that contains an entry for each callee saved register that must be
128  /// spilled to a particular stack location if it is spilled.
129  ///
130  /// Each entry in this array contains a <register,offset> pair, indicating the
131  /// fixed offset from the incoming stack pointer that each register should be
132  /// spilled at. If a register is not listed here, the code generator is
133  /// allowed to spill it anywhere it chooses.
134  ///
135  virtual const SpillSlot *
136  getCalleeSavedSpillSlots(unsigned &NumEntries) const {
137  NumEntries = 0;
138  return nullptr;
139  }
140 
141  /// targetHandlesStackFrameRounding - Returns true if the target is
142  /// responsible for rounding up the stack frame (probably at emitPrologue
143  /// time).
144  virtual bool targetHandlesStackFrameRounding() const {
145  return false;
146  }
147 
148  /// Returns true if the target will correctly handle shrink wrapping.
149  virtual bool enableShrinkWrapping(const MachineFunction &MF) const {
150  return false;
151  }
152 
153  /// Returns true if the stack slot holes in the fixed and callee-save stack
154  /// area should be used when allocating other stack locations to reduce stack
155  /// size.
156  virtual bool enableStackSlotScavenging(const MachineFunction &MF) const {
157  return false;
158  }
159 
160  /// Returns true if the target can safely skip saving callee-saved registers
161  /// for noreturn nounwind functions.
162  virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const;
163 
164  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
165  /// the function.
166  virtual void emitPrologue(MachineFunction &MF,
167  MachineBasicBlock &MBB) const = 0;
168  virtual void emitEpilogue(MachineFunction &MF,
169  MachineBasicBlock &MBB) const = 0;
170 
171  /// Replace a StackProbe stub (if any) with the actual probe code inline
173  MachineBasicBlock &PrologueMBB) const {}
174 
175  /// Adjust the prologue to have the function use segmented stacks. This works
176  /// by adding a check even before the "normal" function prologue.
178  MachineBasicBlock &PrologueMBB) const {}
179 
180  /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
181  /// the assembly prologue to explicitly handle the stack.
183  MachineBasicBlock &PrologueMBB) const {}
184 
185  /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
186  /// saved registers and returns true if it isn't possible / profitable to do
187  /// so by issuing a series of store instructions via
188  /// storeRegToStackSlot(). Returns false otherwise.
191  const std::vector<CalleeSavedInfo> &CSI,
192  const TargetRegisterInfo *TRI) const {
193  return false;
194  }
195 
196  /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
197  /// saved registers and returns true if it isn't possible / profitable to do
198  /// so by issuing a series of load instructions via loadRegToStackSlot().
199  /// If it returns true, and any of the registers in CSI is not restored,
200  /// it sets the corresponding Restored flag in CSI to false.
201  /// Returns false otherwise.
204  std::vector<CalleeSavedInfo> &CSI,
205  const TargetRegisterInfo *TRI) const {
206  return false;
207  }
208 
209  /// Return true if the target wants to keep the frame pointer regardless of
210  /// the function attribute "frame-pointer".
211  virtual bool keepFramePointer(const MachineFunction &MF) const {
212  return false;
213  }
214 
215  /// hasFP - Return true if the specified function should have a dedicated
216  /// frame pointer register. For most targets this is true only if the function
217  /// has variable sized allocas or if frame pointer elimination is disabled.
218  virtual bool hasFP(const MachineFunction &MF) const = 0;
219 
220  /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
221  /// not required, we reserve argument space for call sites in the function
222  /// immediately on entry to the current function. This eliminates the need for
223  /// add/sub sp brackets around call sites. Returns true if the call frame is
224  /// included as part of the stack frame.
225  virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
226  return !hasFP(MF);
227  }
228 
229  /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
230  /// call frame pseudo ops before doing frame index elimination. This is
231  /// possible only when frame index references between the pseudos won't
232  /// need adjusting for the call frame adjustments. Normally, that's true
233  /// if the function has a reserved call frame or a frame pointer. Some
234  /// targets (Thumb2, for example) may have more complicated criteria,
235  /// however, and can override this behavior.
236  virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
237  return hasReservedCallFrame(MF) || hasFP(MF);
238  }
239 
240  // needsFrameIndexResolution - Do we need to perform FI resolution for
241  // this function. Normally, this is required only when the function
242  // has any stack objects. However, targets may want to override this.
243  virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
244 
245  /// getFrameIndexReference - This method should return the base register
246  /// and offset used to reference a frame index location. The offset is
247  /// returned directly, and the base register is returned via FrameReg.
248  virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
249  unsigned &FrameReg) const;
250 
251  /// Same as \c getFrameIndexReference, except that the stack pointer (as
252  /// opposed to the frame pointer) will be the preferred value for \p
253  /// FrameReg. This is generally used for emitting statepoint or EH tables that
254  /// use offsets from RSP. If \p IgnoreSPUpdates is true, the returned
255  /// offset is only guaranteed to be valid with respect to the value of SP at
256  /// the end of the prologue.
257  virtual int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
258  unsigned &FrameReg,
259  bool IgnoreSPUpdates) const {
260  // Always safe to dispatch to getFrameIndexReference.
261  return getFrameIndexReference(MF, FI, FrameReg);
262  }
263 
264  /// This method determines which of the registers reported by
265  /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
266  /// The default implementation checks populates the \p SavedRegs bitset with
267  /// all registers which are modified in the function, targets may override
268  /// this function to save additional registers.
269  /// This method also sets up the register scavenger ensuring there is a free
270  /// register or a frameindex available.
271  virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
272  RegScavenger *RS = nullptr) const;
273 
274  /// processFunctionBeforeFrameFinalized - This method is called immediately
275  /// before the specified function's frame layout (MF.getFrameInfo()) is
276  /// finalized. Once the frame is finalized, MO_FrameIndex operands are
277  /// replaced with direct constants. This method is optional.
278  ///
280  RegScavenger *RS = nullptr) const {
281  }
282 
283  virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const {
284  report_fatal_error("WinEH not implemented for this target");
285  }
286 
287  /// This method is called during prolog/epilog code insertion to eliminate
288  /// call frame setup and destroy pseudo instructions (but only if the Target
289  /// is using them). It is responsible for eliminating these instructions,
290  /// replacing them with concrete instructions. This method need only be
291  /// implemented if using call frame setup/destroy pseudo instructions.
292  /// Returns an iterator pointing to the instruction after the replaced one.
295  MachineBasicBlock &MBB,
297  llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
298  "target!");
299  }
300 
301 
302  /// Order the symbols in the local stack frame.
303  /// The list of objects that we want to order is in \p objectsToAllocate as
304  /// indices into the MachineFrameInfo. The array can be reordered in any way
305  /// upon return. The contents of the array, however, may not be modified (i.e.
306  /// only their order may be changed).
307  /// By default, just maintain the original order.
308  virtual void
310  SmallVectorImpl<int> &objectsToAllocate) const {
311  }
312 
313  /// Check whether or not the given \p MBB can be used as a prologue
314  /// for the target.
315  /// The prologue will be inserted first in this basic block.
316  /// This method is used by the shrink-wrapping pass to decide if
317  /// \p MBB will be correctly handled by the target.
318  /// As soon as the target enable shrink-wrapping without overriding
319  /// this method, we assume that each basic block is a valid
320  /// prologue.
321  virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
322  return true;
323  }
324 
325  /// Check whether or not the given \p MBB can be used as a epilogue
326  /// for the target.
327  /// The epilogue will be inserted before the first terminator of that block.
328  /// This method is used by the shrink-wrapping pass to decide if
329  /// \p MBB will be correctly handled by the target.
330  /// As soon as the target enable shrink-wrapping without overriding
331  /// this method, we assume that each basic block is a valid
332  /// epilogue.
333  virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
334  return true;
335  }
336 
337  /// Check if given function is safe for not having callee saved registers.
338  /// This is used when interprocedural register allocation is enabled.
339  static bool isSafeForNoCSROpt(const Function &F) {
340  if (!F.hasLocalLinkage() || F.hasAddressTaken() ||
341  !F.hasFnAttribute(Attribute::NoRecurse))
342  return false;
343  // Function should not be optimized as tail call.
344  for (const User *U : F.users())
345  if (auto CS = ImmutableCallSite(U))
346  if (CS.isTailCall())
347  return false;
348  return true;
349  }
350 
351  /// Return initial CFA offset value i.e. the one valid at the beginning of the
352  /// function (before any stack operations).
353  virtual int getInitialCFAOffset(const MachineFunction &MF) const;
354 
355  /// Return initial CFA register value i.e. the one valid at the beginning of
356  /// the function (before any stack operations).
357  virtual unsigned getInitialCFARegister(const MachineFunction &MF) const;
358 };
359 
360 } // End llvm namespace
361 
362 #endif
bool hasLocalLinkage() const
Definition: GlobalValue.h:435
virtual bool enableShrinkWrapping(const MachineFunction &MF) const
Returns true if the target will correctly handle shrink wrapping.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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 bool keepFramePointer(const MachineFunction &MF) const
Return true if the target wants to keep the frame pointer regardless of the function attribute "frame...
virtual void adjustForHiPEPrologue(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in the assembly prologue to ex...
virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const
canSimplifyCallFramePseudos - When possible, it&#39;s best to simplify the call frame pseudo ops before d...
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:320
virtual MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
unsigned const TargetRegisterInfo * TRI
F(f)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:684
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
virtual int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, unsigned &FrameReg, bool IgnoreSPUpdates) const
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
static bool isSafeForNoCSROpt(const Function &F)
Check if given function is safe for not having callee saved registers.
virtual const SpillSlot * getCalleeSavedSpillSlots(unsigned &NumEntries) const
getCalleeSavedSpillSlots - This method returns a pointer to an array of pairs, that contains an entry...
virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const
Check whether or not the given MBB can be used as a epilogue for the target.
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register...
virtual bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const
Return the skew that has to be applied to stack alignment under certain conditions (e...
virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment. ...
virtual bool targetHandlesStackFrameRounding() const
targetHandlesStackFrameRounding - Returns true if the target is responsible for rounding up the stack...
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
virtual void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to have the function use segmented stacks.
virtual void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
virtual void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
Information about stack frame layout on the target.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
unsigned getTransientStackAlignment() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO, unsigned TransAl=1, bool StackReal=true)
iterator_range< user_iterator > users()
Definition: Value.h:399
virtual bool isFPCloseToIncomingSP() const
isFPCloseToIncomingSP - Return true if the frame pointer is close to the incoming stack pointer...
virtual bool enableStackSlotScavenging(const MachineFunction &MF) const
Returns true if the stack slot holes in the fixed and callee-save stack area should be used when allo...
virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const
Returns true if the target can safely skip saving callee-saved registers for noreturn nounwind functi...
Establish a view to a call site for examination.
Definition: CallSite.h:886
virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const
Check whether or not the given MBB can be used as a prologue for the target.
virtual void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Replace a StackProbe stub (if any) with the actual probe code inline.
bool isStackRealignable() const
isStackRealignable - This method returns whether the stack can be realigned.
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:1253
virtual void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &objectsToAllocate) const
Order the symbols in the local stack frame.
IRTranslator LLVM IR MI
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.