LLVM  3.7.0
TargetFrameLowering.h
Go to the documentation of this file.
1 //===-- llvm/Target/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_TARGET_TARGETFRAMELOWERING_H
15 #define LLVM_TARGET_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  /// getTransientStackAlignment - This method returns the number of bytes to
74  /// which the stack pointer must be aligned at all times, even between
75  /// calls.
76  ///
77  unsigned getTransientStackAlignment() const {
78  return TransientStackAlignment;
79  }
80 
81  /// isStackRealignable - This method returns whether the stack can be
82  /// realigned.
83  bool isStackRealignable() const {
84  return StackRealignable;
85  }
86 
87  /// getOffsetOfLocalArea - This method returns the offset of the local area
88  /// from the stack pointer on entrance to a function.
89  ///
90  int getOffsetOfLocalArea() const { return LocalAreaOffset; }
91 
92  /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
93  /// the incoming stack pointer, false if it is close to the post-prologue
94  /// stack pointer.
95  virtual bool isFPCloseToIncomingSP() const { return true; }
96 
97  /// assignCalleeSavedSpillSlots - Allows target to override spill slot
98  /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
99  /// assign frame slots to all CSI entries and return true. If this method
100  /// returns false, spill slots will be assigned using generic implementation.
101  /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
102  /// CSI.
103  virtual bool
105  const TargetRegisterInfo *TRI,
106  std::vector<CalleeSavedInfo> &CSI) const {
107  return false;
108  }
109 
110  /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
111  /// pairs, that contains an entry for each callee saved register that must be
112  /// spilled to a particular stack location if it is spilled.
113  ///
114  /// Each entry in this array contains a <register,offset> pair, indicating the
115  /// fixed offset from the incoming stack pointer that each register should be
116  /// spilled at. If a register is not listed here, the code generator is
117  /// allowed to spill it anywhere it chooses.
118  ///
119  virtual const SpillSlot *
120  getCalleeSavedSpillSlots(unsigned &NumEntries) const {
121  NumEntries = 0;
122  return nullptr;
123  }
124 
125  /// targetHandlesStackFrameRounding - Returns true if the target is
126  /// responsible for rounding up the stack frame (probably at emitPrologue
127  /// time).
128  virtual bool targetHandlesStackFrameRounding() const {
129  return false;
130  }
131 
132  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
133  /// the function.
134  virtual void emitPrologue(MachineFunction &MF,
135  MachineBasicBlock &MBB) const = 0;
136  virtual void emitEpilogue(MachineFunction &MF,
137  MachineBasicBlock &MBB) const = 0;
138 
139  /// Adjust the prologue to have the function use segmented stacks. This works
140  /// by adding a check even before the "normal" function prologue.
142  MachineBasicBlock &PrologueMBB) const {}
143 
144  /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
145  /// the assembly prologue to explicitly handle the stack.
147  MachineBasicBlock &PrologueMBB) const {}
148 
149  /// Adjust the prologue to add an allocation at a fixed offset from the frame
150  /// pointer.
151  virtual void
153  MachineBasicBlock &PrologueMBB) const {}
154 
155  /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
156  /// saved registers and returns true if it isn't possible / profitable to do
157  /// so by issuing a series of store instructions via
158  /// storeRegToStackSlot(). Returns false otherwise.
161  const std::vector<CalleeSavedInfo> &CSI,
162  const TargetRegisterInfo *TRI) const {
163  return false;
164  }
165 
166  /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
167  /// saved registers and returns true if it isn't possible / profitable to do
168  /// so by issuing a series of load instructions via loadRegToStackSlot().
169  /// Returns false otherwise.
172  const std::vector<CalleeSavedInfo> &CSI,
173  const TargetRegisterInfo *TRI) const {
174  return false;
175  }
176 
177  /// Return true if the target needs to disable frame pointer elimination.
178  virtual bool noFramePointerElim(const MachineFunction &MF) const;
179 
180  /// hasFP - Return true if the specified function should have a dedicated
181  /// frame pointer register. For most targets this is true only if the function
182  /// has variable sized allocas or if frame pointer elimination is disabled.
183  virtual bool hasFP(const MachineFunction &MF) const = 0;
184 
185  /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
186  /// not required, we reserve argument space for call sites in the function
187  /// immediately on entry to the current function. This eliminates the need for
188  /// add/sub sp brackets around call sites. Returns true if the call frame is
189  /// included as part of the stack frame.
190  virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
191  return !hasFP(MF);
192  }
193 
194  /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
195  /// call frame pseudo ops before doing frame index elimination. This is
196  /// possible only when frame index references between the pseudos won't
197  /// need adjusting for the call frame adjustments. Normally, that's true
198  /// if the function has a reserved call frame or a frame pointer. Some
199  /// targets (Thumb2, for example) may have more complicated criteria,
200  /// however, and can override this behavior.
201  virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
202  return hasReservedCallFrame(MF) || hasFP(MF);
203  }
204 
205  // needsFrameIndexResolution - Do we need to perform FI resolution for
206  // this function. Normally, this is required only when the function
207  // has any stack objects. However, targets may want to override this.
208  virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
209 
210  /// getFrameIndexOffset - Returns the displacement from the frame register to
211  /// the stack frame of the specified index.
212  virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
213 
214  /// getFrameIndexReference - This method should return the base register
215  /// and offset used to reference a frame index location. The offset is
216  /// returned directly, and the base register is returned via FrameReg.
217  virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
218  unsigned &FrameReg) const;
219 
220  /// Same as above, except that the 'base register' will always be RSP, not
221  /// RBP on x86. This is used exclusively for lowering STATEPOINT nodes.
222  /// TODO: This should really be a parameterizable choice.
223  virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
224  unsigned &FrameReg) const {
225  // default to calling normal version, we override this on x86 only
226  llvm_unreachable("unimplemented for non-x86");
227  return 0;
228  }
229 
230  /// This method determines which of the registers reported by
231  /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
232  /// The default implementation checks populates the \p SavedRegs bitset with
233  /// all registers which are modified in the function, targets may override
234  /// this function to save additional registers.
235  /// This method also sets up the register scavenger ensuring there is a free
236  /// register or a frameindex available.
237  virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
238  RegScavenger *RS = nullptr) const;
239 
240  /// processFunctionBeforeFrameFinalized - This method is called immediately
241  /// before the specified function's frame layout (MF.getFrameInfo()) is
242  /// finalized. Once the frame is finalized, MO_FrameIndex operands are
243  /// replaced with direct constants. This method is optional.
244  ///
246  RegScavenger *RS = nullptr) const {
247  }
248 
249  /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
250  /// code insertion to eliminate call frame setup and destroy pseudo
251  /// instructions (but only if the Target is using them). It is responsible
252  /// for eliminating these instructions, replacing them with concrete
253  /// instructions. This method need only be implemented if using call frame
254  /// setup/destroy pseudo instructions.
255  ///
256  virtual void
258  MachineBasicBlock &MBB,
260  llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
261  "target!");
262  }
263 
264  /// Check whether or not the given \p MBB can be used as a prologue
265  /// for the target.
266  /// The prologue will be inserted first in this basic block.
267  /// This method is used by the shrink-wrapping pass to decide if
268  /// \p MBB will be correctly handled by the target.
269  /// As soon as the target enable shrink-wrapping without overriding
270  /// this method, we assume that each basic block is a valid
271  /// prologue.
272  virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
273  return true;
274  }
275 
276  /// Check whether or not the given \p MBB can be used as a epilogue
277  /// for the target.
278  /// The epilogue will be inserted before the first terminator of that block.
279  /// This method is used by the shrink-wrapping pass to decide if
280  /// \p MBB will be correctly handled by the target.
281  /// As soon as the target enable shrink-wrapping without overriding
282  /// this method, we assume that each basic block is a valid
283  /// epilogue.
284  virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
285  return true;
286  }
287 };
288 
289 } // End llvm namespace
290 
291 #endif
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const
Check whether or not the given MBB can be used as a epilogue for the target.
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 const SpillSlot * getCalleeSavedSpillSlots(unsigned &NumEntries) const
getCalleeSavedSpillSlots - This method returns a pointer to an array of pairs, that contains an entry...
bool isStackRealignable() const
isStackRealignable - This method returns whether the stack can be realigned.
virtual bool targetHandlesStackFrameRounding() const
targetHandlesStackFrameRounding - Returns true if the target is responsible for rounding up the stack...
virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const
Check whether or not the given MBB can be used as a prologue for the target.
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
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...
unsigned getTransientStackAlignment() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
virtual void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
bundle_iterator< MachineInstr, instr_iterator > iterator
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 void eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
eliminateCallFramePseudoInstr - This method is called during prolog/epilog code insertion to eliminat...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const
getFrameIndexOffset - Returns the displacement from the frame register to the stack frame of the spec...
virtual bool noFramePointerElim(const MachineFunction &MF) const
Return true if the target needs to disable frame pointer elimination.
virtual void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
Information about stack frame layout on the target.
virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const
canSimplifyCallFramePseudos - When possible, it's best to simplify the call frame pseudo ops before d...
TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO, unsigned TransAl=1, bool StackReal=true)
virtual void adjustForFrameAllocatePrologue(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to add an allocation at a fixed offset from the frame pointer.
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
virtual void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to have the function use segmented stacks.
virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI, unsigned &FrameReg) const
Same as above, except that the 'base register' will always be RSP, not RBP on x86.
virtual bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
virtual bool isFPCloseToIncomingSP() const
isFPCloseToIncomingSP - Return true if the frame pointer is close to the incoming stack pointer...