1 //===-- llvm/CodeGen/TargetFrameLowering.h ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Interface to describe the layout of a stack frame on the target machine.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CODEGEN_TARGETFRAMELOWERING_H
14 #define LLVM_CODEGEN_TARGETFRAMELOWERING_H
16 #include "llvm/CodeGen/MachineBasicBlock.h"
22 class CalleeSavedInfo
;
23 class MachineFunction
;
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.
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
{
36 StackGrowsUp
, // Adding to the stack increases the stack address
37 StackGrowsDown
// Adding to the stack decreases the stack address
40 // Maps a callee saved register to a stack slot with a fixed offset.
43 int Offset
; // Offset relative to stack pointer on function entry.
46 StackDirection StackDir
;
47 unsigned StackAlignment
;
48 unsigned TransientStackAlignment
;
50 bool StackRealignable
;
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
) {}
57 virtual ~TargetFrameLowering();
59 // These methods return information that describes the abstract stack layout
60 // of the target machine.
62 /// getStackGrowthDirection - Return the direction the stack grows
64 StackDirection
getStackGrowthDirection() const { return StackDir
; }
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.
70 unsigned getStackAlignment() const { return StackAlignment
; }
72 /// alignSPAdjust - This method aligns the stack adjustment to the correct
75 int alignSPAdjust(int SPAdj
) const {
77 SPAdj
= -alignTo(-SPAdj
, StackAlignment
);
79 SPAdj
= alignTo(SPAdj
, StackAlignment
);
84 /// getTransientStackAlignment - This method returns the number of bytes to
85 /// which the stack pointer must be aligned at all times, even between
88 unsigned getTransientStackAlignment() const {
89 return TransientStackAlignment
;
92 /// isStackRealignable - This method returns whether the stack can be
94 bool isStackRealignable() const {
95 return StackRealignable
;
98 /// Return the skew that has to be applied to stack alignment under
99 /// certain conditions (e.g. stack was adjusted before function \p MF
101 virtual unsigned getStackAlignmentSkew(const MachineFunction
&MF
) const;
103 /// getOffsetOfLocalArea - This method returns the offset of the local area
104 /// from the stack pointer on entrance to a function.
106 int getOffsetOfLocalArea() const { return LocalAreaOffset
; }
108 /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
109 /// the incoming stack pointer, false if it is close to the post-prologue
111 virtual bool isFPCloseToIncomingSP() const { return true; }
113 /// assignCalleeSavedSpillSlots - Allows target to override spill slot
114 /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
115 /// assign frame slots to all CSI entries and return true. If this method
116 /// returns false, spill slots will be assigned using generic implementation.
117 /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
120 assignCalleeSavedSpillSlots(MachineFunction
&MF
,
121 const TargetRegisterInfo
*TRI
,
122 std::vector
<CalleeSavedInfo
> &CSI
) const {
126 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
127 /// pairs, that contains an entry for each callee saved register that must be
128 /// spilled to a particular stack location if it is spilled.
130 /// Each entry in this array contains a <register,offset> pair, indicating the
131 /// fixed offset from the incoming stack pointer that each register should be
132 /// spilled at. If a register is not listed here, the code generator is
133 /// allowed to spill it anywhere it chooses.
135 virtual const SpillSlot
*
136 getCalleeSavedSpillSlots(unsigned &NumEntries
) const {
141 /// targetHandlesStackFrameRounding - Returns true if the target is
142 /// responsible for rounding up the stack frame (probably at emitPrologue
144 virtual bool targetHandlesStackFrameRounding() const {
148 /// Returns true if the target will correctly handle shrink wrapping.
149 virtual bool enableShrinkWrapping(const MachineFunction
&MF
) const {
153 /// Returns true if the stack slot holes in the fixed and callee-save stack
154 /// area should be used when allocating other stack locations to reduce stack
156 virtual bool enableStackSlotScavenging(const MachineFunction
&MF
) const {
160 /// Returns true if the target can safely skip saving callee-saved registers
161 /// for noreturn nounwind functions.
162 virtual bool enableCalleeSaveSkip(const MachineFunction
&MF
) const;
164 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
166 virtual void emitPrologue(MachineFunction
&MF
,
167 MachineBasicBlock
&MBB
) const = 0;
168 virtual void emitEpilogue(MachineFunction
&MF
,
169 MachineBasicBlock
&MBB
) const = 0;
171 /// Replace a StackProbe stub (if any) with the actual probe code inline
172 virtual void inlineStackProbe(MachineFunction
&MF
,
173 MachineBasicBlock
&PrologueMBB
) const {}
175 /// Adjust the prologue to have the function use segmented stacks. This works
176 /// by adding a check even before the "normal" function prologue.
177 virtual void adjustForSegmentedStacks(MachineFunction
&MF
,
178 MachineBasicBlock
&PrologueMBB
) const {}
180 /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
181 /// the assembly prologue to explicitly handle the stack.
182 virtual void adjustForHiPEPrologue(MachineFunction
&MF
,
183 MachineBasicBlock
&PrologueMBB
) const {}
185 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
186 /// saved registers and returns true if it isn't possible / profitable to do
187 /// so by issuing a series of store instructions via
188 /// storeRegToStackSlot(). Returns false otherwise.
189 virtual bool spillCalleeSavedRegisters(MachineBasicBlock
&MBB
,
190 MachineBasicBlock::iterator MI
,
191 const std::vector
<CalleeSavedInfo
> &CSI
,
192 const TargetRegisterInfo
*TRI
) const {
196 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
197 /// saved registers and returns true if it isn't possible / profitable to do
198 /// so by issuing a series of load instructions via loadRegToStackSlot().
199 /// If it returns true, and any of the registers in CSI is not restored,
200 /// it sets the corresponding Restored flag in CSI to false.
201 /// Returns false otherwise.
202 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock
&MBB
,
203 MachineBasicBlock::iterator MI
,
204 std::vector
<CalleeSavedInfo
> &CSI
,
205 const TargetRegisterInfo
*TRI
) const {
209 /// Return true if the target wants to keep the frame pointer regardless of
210 /// the function attribute "frame-pointer".
211 virtual bool keepFramePointer(const MachineFunction
&MF
) const {
215 /// hasFP - Return true if the specified function should have a dedicated
216 /// frame pointer register. For most targets this is true only if the function
217 /// has variable sized allocas or if frame pointer elimination is disabled.
218 virtual bool hasFP(const MachineFunction
&MF
) const = 0;
220 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
221 /// not required, we reserve argument space for call sites in the function
222 /// immediately on entry to the current function. This eliminates the need for
223 /// add/sub sp brackets around call sites. Returns true if the call frame is
224 /// included as part of the stack frame.
225 virtual bool hasReservedCallFrame(const MachineFunction
&MF
) const {
229 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
230 /// call frame pseudo ops before doing frame index elimination. This is
231 /// possible only when frame index references between the pseudos won't
232 /// need adjusting for the call frame adjustments. Normally, that's true
233 /// if the function has a reserved call frame or a frame pointer. Some
234 /// targets (Thumb2, for example) may have more complicated criteria,
235 /// however, and can override this behavior.
236 virtual bool canSimplifyCallFramePseudos(const MachineFunction
&MF
) const {
237 return hasReservedCallFrame(MF
) || hasFP(MF
);
240 // needsFrameIndexResolution - Do we need to perform FI resolution for
241 // this function. Normally, this is required only when the function
242 // has any stack objects. However, targets may want to override this.
243 virtual bool needsFrameIndexResolution(const MachineFunction
&MF
) const;
245 /// getFrameIndexReference - This method should return the base register
246 /// and offset used to reference a frame index location. The offset is
247 /// returned directly, and the base register is returned via FrameReg.
248 virtual int getFrameIndexReference(const MachineFunction
&MF
, int FI
,
249 unsigned &FrameReg
) const;
251 /// Same as \c getFrameIndexReference, except that the stack pointer (as
252 /// opposed to the frame pointer) will be the preferred value for \p
253 /// FrameReg. This is generally used for emitting statepoint or EH tables that
254 /// use offsets from RSP. If \p IgnoreSPUpdates is true, the returned
255 /// offset is only guaranteed to be valid with respect to the value of SP at
256 /// the end of the prologue.
257 virtual int getFrameIndexReferencePreferSP(const MachineFunction
&MF
, int FI
,
259 bool IgnoreSPUpdates
) const {
260 // Always safe to dispatch to getFrameIndexReference.
261 return getFrameIndexReference(MF
, FI
, FrameReg
);
264 /// getNonLocalFrameIndexReference - This method returns the offset used to
265 /// reference a frame index location. The offset can be from either FP/BP/SP
266 /// based on which base register is returned by llvm.localaddress.
267 virtual int getNonLocalFrameIndexReference(const MachineFunction
&MF
,
269 // By default, dispatch to getFrameIndexReference. Interested targets can
272 return getFrameIndexReference(MF
, FI
, FrameReg
);
275 /// This method determines which of the registers reported by
276 /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
277 /// The default implementation checks populates the \p SavedRegs bitset with
278 /// all registers which are modified in the function, targets may override
279 /// this function to save additional registers.
280 /// This method also sets up the register scavenger ensuring there is a free
281 /// register or a frameindex available.
282 virtual void determineCalleeSaves(MachineFunction
&MF
, BitVector
&SavedRegs
,
283 RegScavenger
*RS
= nullptr) const;
285 /// processFunctionBeforeFrameFinalized - This method is called immediately
286 /// before the specified function's frame layout (MF.getFrameInfo()) is
287 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
288 /// replaced with direct constants. This method is optional.
290 virtual void processFunctionBeforeFrameFinalized(MachineFunction
&MF
,
291 RegScavenger
*RS
= nullptr) const {
294 virtual unsigned getWinEHParentFrameOffset(const MachineFunction
&MF
) const {
295 report_fatal_error("WinEH not implemented for this target");
298 /// This method is called during prolog/epilog code insertion to eliminate
299 /// call frame setup and destroy pseudo instructions (but only if the Target
300 /// is using them). It is responsible for eliminating these instructions,
301 /// replacing them with concrete instructions. This method need only be
302 /// implemented if using call frame setup/destroy pseudo instructions.
303 /// Returns an iterator pointing to the instruction after the replaced one.
304 virtual MachineBasicBlock::iterator
305 eliminateCallFramePseudoInstr(MachineFunction
&MF
,
306 MachineBasicBlock
&MBB
,
307 MachineBasicBlock::iterator MI
) const {
308 llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
313 /// Order the symbols in the local stack frame.
314 /// The list of objects that we want to order is in \p objectsToAllocate as
315 /// indices into the MachineFrameInfo. The array can be reordered in any way
316 /// upon return. The contents of the array, however, may not be modified (i.e.
317 /// only their order may be changed).
318 /// By default, just maintain the original order.
320 orderFrameObjects(const MachineFunction
&MF
,
321 SmallVectorImpl
<int> &objectsToAllocate
) const {
324 /// Check whether or not the given \p MBB can be used as a prologue
326 /// The prologue will be inserted first in this basic block.
327 /// This method is used by the shrink-wrapping pass to decide if
328 /// \p MBB will be correctly handled by the target.
329 /// As soon as the target enable shrink-wrapping without overriding
330 /// this method, we assume that each basic block is a valid
332 virtual bool canUseAsPrologue(const MachineBasicBlock
&MBB
) const {
336 /// Check whether or not the given \p MBB can be used as a epilogue
338 /// The epilogue will be inserted before the first terminator of that block.
339 /// This method is used by the shrink-wrapping pass to decide if
340 /// \p MBB will be correctly handled by the target.
341 /// As soon as the target enable shrink-wrapping without overriding
342 /// this method, we assume that each basic block is a valid
344 virtual bool canUseAsEpilogue(const MachineBasicBlock
&MBB
) const {
348 /// Check if given function is safe for not having callee saved registers.
349 /// This is used when interprocedural register allocation is enabled.
350 static bool isSafeForNoCSROpt(const Function
&F
) {
351 if (!F
.hasLocalLinkage() || F
.hasAddressTaken() ||
352 !F
.hasFnAttribute(Attribute::NoRecurse
))
354 // Function should not be optimized as tail call.
355 for (const User
*U
: F
.users())
356 if (auto CS
= ImmutableCallSite(U
))
362 /// Return initial CFA offset value i.e. the one valid at the beginning of the
363 /// function (before any stack operations).
364 virtual int getInitialCFAOffset(const MachineFunction
&MF
) const;
366 /// Return initial CFA register value i.e. the one valid at the beginning of
367 /// the function (before any stack operations).
368 virtual unsigned getInitialCFARegister(const MachineFunction
&MF
) const;
371 } // End llvm namespace