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