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