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