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