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