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"
17 #include "llvm/ADT/StringSwitch.h"
23 class CalleeSavedInfo
;
24 class MachineFunction
;
27 namespace TargetStackID
{
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.
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
{
46 StackGrowsUp
, // Adding to the stack increases the stack address
47 StackGrowsDown
// Adding to the stack decreases the stack address
50 // Maps a callee saved register to a stack slot with a fixed offset.
53 int Offset
; // Offset relative to stack pointer on function entry.
56 StackDirection StackDir
;
57 unsigned StackAlignment
;
58 unsigned TransientStackAlignment
;
60 bool StackRealignable
;
62 TargetFrameLowering(StackDirection D
, unsigned StackAl
, int LAO
,
63 unsigned TransAl
= 1, bool StackReal
= true)
64 : StackDir(D
), StackAlignment(StackAl
), TransientStackAlignment(TransAl
),
65 LocalAreaOffset(LAO
), StackRealignable(StackReal
) {}
67 virtual ~TargetFrameLowering();
69 // These methods return information that describes the abstract stack layout
70 // of the target machine.
72 /// getStackGrowthDirection - Return the direction the stack grows
74 StackDirection
getStackGrowthDirection() const { return StackDir
; }
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.
80 unsigned getStackAlignment() const { return StackAlignment
; }
82 /// alignSPAdjust - This method aligns the stack adjustment to the correct
85 int alignSPAdjust(int SPAdj
) const {
87 SPAdj
= -alignTo(-SPAdj
, StackAlignment
);
89 SPAdj
= alignTo(SPAdj
, StackAlignment
);
94 /// getTransientStackAlignment - This method returns the number of bytes to
95 /// which the stack pointer must be aligned at all times, even between
98 unsigned getTransientStackAlignment() const {
99 return TransientStackAlignment
;
102 /// isStackRealignable - This method returns whether the stack can be
104 bool isStackRealignable() const {
105 return StackRealignable
;
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
111 virtual unsigned getStackAlignmentSkew(const MachineFunction
&MF
) const;
113 /// getOffsetOfLocalArea - This method returns the offset of the local area
114 /// from the stack pointer on entrance to a function.
116 int getOffsetOfLocalArea() const { return LocalAreaOffset
; }
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
121 virtual bool isFPCloseToIncomingSP() const { return true; }
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
130 assignCalleeSavedSpillSlots(MachineFunction
&MF
,
131 const TargetRegisterInfo
*TRI
,
132 std::vector
<CalleeSavedInfo
> &CSI
) const {
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.
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.
145 virtual const SpillSlot
*
146 getCalleeSavedSpillSlots(unsigned &NumEntries
) const {
151 /// targetHandlesStackFrameRounding - Returns true if the target is
152 /// responsible for rounding up the stack frame (probably at emitPrologue
154 virtual bool targetHandlesStackFrameRounding() const {
158 /// Returns true if the target will correctly handle shrink wrapping.
159 virtual bool enableShrinkWrapping(const MachineFunction
&MF
) const {
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
166 virtual bool enableStackSlotScavenging(const MachineFunction
&MF
) const {
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;
174 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
176 virtual void emitPrologue(MachineFunction
&MF
,
177 MachineBasicBlock
&MBB
) const = 0;
178 virtual void emitEpilogue(MachineFunction
&MF
,
179 MachineBasicBlock
&MBB
) const = 0;
181 /// Replace a StackProbe stub (if any) with the actual probe code inline
182 virtual void inlineStackProbe(MachineFunction
&MF
,
183 MachineBasicBlock
&PrologueMBB
) const {}
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 {}
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 {}
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 {
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 {
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 {
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;
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 {
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
);
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;
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;
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
,
269 bool IgnoreSPUpdates
) const {
270 // Always safe to dispatch to getFrameIndexReference.
271 return getFrameIndexReference(MF
, FI
, FrameReg
);
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
,
279 // By default, dispatch to getFrameIndexReference. Interested targets can
282 return getFrameIndexReference(MF
, FI
, FrameReg
);
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;
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.
300 virtual void processFunctionBeforeFrameFinalized(MachineFunction
&MF
,
301 RegScavenger
*RS
= nullptr) const {
304 virtual unsigned getWinEHParentFrameOffset(const MachineFunction
&MF
) const {
305 report_fatal_error("WinEH not implemented for this target");
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 "
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.
330 orderFrameObjects(const MachineFunction
&MF
,
331 SmallVectorImpl
<int> &objectsToAllocate
) const {
334 /// Check whether or not the given \p MBB can be used as a prologue
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
342 virtual bool canUseAsPrologue(const MachineBasicBlock
&MBB
) const {
346 /// Check whether or not the given \p MBB can be used as a epilogue
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
354 virtual bool canUseAsEpilogue(const MachineBasicBlock
&MBB
) const {
358 virtual bool isSupportedStackID(TargetStackID::Value ID
) const {
362 case TargetStackID::Default
:
363 case TargetStackID::NoAlloc
:
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 if (!F
.hasLocalLinkage() || F
.hasAddressTaken() ||
372 !F
.hasFnAttribute(Attribute::NoRecurse
))
374 // Function should not be optimized as tail call.
375 for (const User
*U
: F
.users())
376 if (auto CS
= ImmutableCallSite(U
))
382 /// Check if the no-CSR optimisation is profitable for the given function.
383 virtual bool isProfitableForNoCSROpt(const Function
&F
) const {
387 /// Return initial CFA offset value i.e. the one valid at the beginning of the
388 /// function (before any stack operations).
389 virtual int getInitialCFAOffset(const MachineFunction
&MF
) const;
391 /// Return initial CFA register value i.e. the one valid at the beginning of
392 /// the function (before any stack operations).
393 virtual unsigned getInitialCFARegister(const MachineFunction
&MF
) const;
396 } // End llvm namespace