LLVM 20.0.0git
X86FrameLowering.h
Go to the documentation of this file.
1//===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- 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// This class implements X86-specific bits of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
14#define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
15
19
20namespace llvm {
21
22class MachineInstrBuilder;
23class MCCFIInstruction;
24class X86InstrInfo;
25class X86Subtarget;
26class X86RegisterInfo;
27
29public:
30 X86FrameLowering(const X86Subtarget &STI, MaybeAlign StackAlignOverride);
31
32 // Cached subtarget predicates.
33
37
38 unsigned SlotSize;
39
40 /// Is64Bit implies that x86_64 instructions are available.
41 bool Is64Bit;
42
43 bool IsLP64;
44
45 /// True if the 64-bit frame or stack pointer should be used. True for most
46 /// 64-bit targets with the exception of x32. If this is false, 32-bit
47 /// instruction operands should be used to manipulate StackPtr and FramePtr.
49
50 unsigned StackPtr;
51
52 /// Emit target stack probe code. This is required for all
53 /// large stack allocations on Windows. The caller is required to materialize
54 /// the number of bytes to probe in RAX/EAX.
55 /// \p InstrNum optionally contains a debug-info instruction number for the
56 /// new stack pointer.
59 bool InProlog,
60 std::optional<MachineFunction::DebugInstrOperandPair>
61 InstrNum = std::nullopt) const;
62
63 bool stackProbeFunctionModifiesSP() const override;
64
65 /// Replace a StackProbe inline-stub with the actual probe code inline.
67 MachineBasicBlock &PrologMBB) const override;
68
71
74 const DebugLoc &DL, bool IsPrologue) const;
75
76 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
77 /// the function.
78 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
79 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
80
82 MachineBasicBlock &PrologueMBB) const override;
83
85 MachineBasicBlock &PrologueMBB) const override;
86
88 RegScavenger *RS = nullptr) const override;
89
90 bool
93 std::vector<CalleeSavedInfo> &CSI) const override;
94
98 const TargetRegisterInfo *TRI) const override;
99
100 bool
104 const TargetRegisterInfo *TRI) const override;
105
106 void spillFPBP(MachineFunction &MF) const override;
107
108 bool hasFP(const MachineFunction &MF) const override;
109 bool hasReservedCallFrame(const MachineFunction &MF) const override;
110 bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
111 bool needsFrameIndexResolution(const MachineFunction &MF) const override;
112
114 Register &FrameReg) const override;
115
116 int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI,
117 Register &SPReg) const;
119 Register &SPReg, int Adjustment) const;
122 Register &FrameReg,
123 bool IgnoreSPUpdates) const override;
124
127 MachineBasicBlock::iterator MI) const override;
128
129 unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
130
132 RegScavenger *RS) const override;
133
134 void
136 RegScavenger *RS) const override;
137
138 /// Check the instruction before/after the passed instruction. If
139 /// it is an ADD/SUB/LEA instruction it is deleted argument and the
140 /// stack adjustment is returned as a positive value for ADD/LEA and
141 /// a negative for SUB.
143 bool doMergeWithPrevious) const;
144
145 /// Emit a series of instructions to increment / decrement the stack
146 /// pointer by a constant value.
148 const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
149
150 /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
151 bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
152
153 /// Check whether or not the given \p MBB can be used as a prologue
154 /// for the target.
155 /// The prologue will be inserted first in this basic block.
156 /// This method is used by the shrink-wrapping pass to decide if
157 /// \p MBB will be correctly handled by the target.
158 /// As soon as the target enable shrink-wrapping without overriding
159 /// this method, we assume that each basic block is a valid
160 /// prologue.
161 bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
162
163 /// Check whether or not the given \p MBB can be used as a epilogue
164 /// for the target.
165 /// The epilogue will be inserted before the first terminator of that block.
166 /// This method is used by the shrink-wrapping pass to decide if
167 /// \p MBB will be correctly handled by the target.
168 bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
169
170 /// Returns true if the target will correctly handle shrink wrapping.
171 bool enableShrinkWrapping(const MachineFunction &MF) const override;
172
173 /// Order the symbols in the local stack.
174 /// We want to place the local stack objects in some sort of sensible order.
175 /// The heuristic we use is to try and pack them according to static number
176 /// of uses and size in order to minimize code size.
177 void orderFrameObjects(const MachineFunction &MF,
178 SmallVectorImpl<int> &ObjectsToAllocate) const override;
179
180 /// Wraps up getting a CFI index and building a MachineInstr for it.
182 const DebugLoc &DL, const MCCFIInstruction &CFIInst,
184
185 /// Sets up EBP and optionally ESI based on the incoming EBP value. Only
186 /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
190 const DebugLoc &DL, bool RestoreSP = false) const;
191
193
194 int getInitialCFAOffset(const MachineFunction &MF) const override;
195
196 Register getInitialCFARegister(const MachineFunction &MF) const override;
197
198 DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const override;
199
200 /// Return true if the function has a redzone (accessible bytes past the
201 /// frame of the top of stack function) as part of it's ABI.
202 bool has128ByteRedZone(const MachineFunction& MF) const;
203
204private:
205 bool isWin64Prologue(const MachineFunction &MF) const;
206
207 bool needsDwarfCFI(const MachineFunction &MF) const;
208
209 uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
210
211 /// Emit target stack probe as a call to a helper function
212 void emitStackProbeCall(
214 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
215 std::optional<MachineFunction::DebugInstrOperandPair> InstrNum) const;
216
217 /// Emit target stack probe as an inline sequence.
218 void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
220 const DebugLoc &DL, bool InProlog) const;
221 void emitStackProbeInlineWindowsCoreCLR64(MachineFunction &MF,
224 const DebugLoc &DL,
225 bool InProlog) const;
226 void emitStackProbeInlineGeneric(MachineFunction &MF, MachineBasicBlock &MBB,
228 const DebugLoc &DL, bool InProlog) const;
229
230 void emitStackProbeInlineGenericBlock(MachineFunction &MF,
233 const DebugLoc &DL, uint64_t Offset,
234 uint64_t Align) const;
235
236 void emitStackProbeInlineGenericLoop(MachineFunction &MF,
239 const DebugLoc &DL, uint64_t Offset,
240 uint64_t Align) const;
241
242 /// Emit target zero call-used regs.
243 void emitZeroCallUsedRegs(BitVector RegsToZero,
244 MachineBasicBlock &MBB) const override;
245
246 void adjustFrameForMsvcCxxEh(MachineFunction &MF) const;
247
248 /// Aligns the stack pointer by ANDing it with -MaxAlign.
249 void BuildStackAlignAND(MachineBasicBlock &MBB,
251 unsigned Reg, uint64_t MaxAlign) const;
252
253 /// Make small positive stack adjustments using POPs.
254 bool adjustStackWithPops(MachineBasicBlock &MBB,
256 int Offset) const;
257
258 /// Adjusts the stack pointer using LEA, SUB, or ADD.
259 MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
261 const DebugLoc &DL, int64_t Offset,
262 bool InEpilogue) const;
263
264 unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
265
266 unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
267
268 /// Materialize the catchret target MBB in RAX.
269 void emitCatchRetReturnValue(MachineBasicBlock &MBB,
271 MachineInstr *CatchRet) const;
272
273 /// Issue instructions to allocate stack space and spill frame pointer and/or
274 /// base pointer to stack using stack pointer register.
275 void spillFPBPUsingSP(MachineFunction &MF,
277 Register BP, int SPAdjust) const;
278
279 /// Issue instructions to restore frame pointer and/or base pointer from stack
280 /// using stack pointer register, and free stack space.
281 void restoreFPBPUsingSP(MachineFunction &MF,
282 const MachineBasicBlock::iterator AfterMI,
283 Register FP, Register BP, int SPAdjust) const;
284
285 void saveAndRestoreFPBPUsingSP(MachineFunction &MF,
288 bool SpillFP, bool SpillBP) const;
289
290 // If MI uses fp/bp, but target can handle it, and doesn't want to be spilled
291 // again, this function should return true, and update MI so we will not check
292 // any instructions from related sequence.
293 bool skipSpillFPBP(MachineFunction &MF,
295};
296
297} // End llvm namespace
298
299#endif
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
IRTranslator LLVM IR MI
unsigned Reg
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A debug info location.
Definition: DebugLoc.h:33
Representation of each machine instruction.
Definition: MachineInstr.h:69
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:33
Information about stack frame layout on the target.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool has128ByteRedZone(const MachineFunction &MF) const
Return true if the function has a redzone (accessible bytes past the frame of the top of stack functi...
void spillFPBP(MachineFunction &MF) const override
If a function uses base pointer and the base pointer is clobbered by inline asm, RA doesn't detect th...
bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override
canSimplifyCallFramePseudos - If there is a reserved call frame, the call frame pseudos can be simpli...
bool needsFrameIndexResolution(const MachineFunction &MF) const override
const X86RegisterInfo * TRI
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register.
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
MachineBasicBlock::iterator restoreWin32EHStackPointers(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool RestoreSP=false) const
Sets up EBP and optionally ESI based on the incoming EBP value.
int getInitialCFAOffset(const MachineFunction &MF) const override
Return initial CFA offset value i.e.
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a prologue for the target.
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog, std::optional< MachineFunction::DebugInstrOperandPair > InstrNum=std::nullopt) const
Emit target stack probe code.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool IsPrologue) const
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
StackOffset getFrameIndexReferenceSP(const MachineFunction &MF, int FI, Register &SPReg, int Adjustment) const
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologMBB) const override
Replace a StackProbe inline-stub with the actual probe code inline.
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
const X86InstrInfo & TII
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const
Emit a series of instructions to increment / decrement the stack pointer by a constant value.
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
bool Is64Bit
Is64Bit implies that x86_64 instructions are available.
Register getInitialCFARegister(const MachineFunction &MF) const override
Return initial CFA register value i.e.
bool Uses64BitFramePtr
True if the 64-bit frame or stack pointer should be used.
unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override
void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Adjust the prologue to have the function use segmented stacks.
DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const override
Return the frame base information to be encoded in the DWARF subprogram debug info.
void emitCalleeSavedFrameMovesFullCFA(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const override
Emits Dwarf Info specifying offsets of callee saved registers and frame pointer.
int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI, Register &SPReg) const
bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const
Check that LEA can be used on SP in an epilogue sequence for MF.
bool stackProbeFunctionModifiesSP() const override
Does the stack probe function call return with a modified stack pointer?
void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &ObjectsToAllocate) const override
Order the symbols in the local stack.
void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, const MCCFIInstruction &CFIInst, MachineInstr::MIFlag Flag=MachineInstr::NoFlags) const
Wraps up getting a CFI index and building a MachineInstr for it.
int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, bool doMergeWithPrevious) const
Check the instruction before/after the passed instruction.
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
void processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameIndicesReplaced - This method is called immediately before MO_FrameIndex op...
StackOffset getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, Register &FrameReg, bool IgnoreSPUpdates) const override
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
void restoreWinEHStackPointersInParent(MachineFunction &MF) const
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
void adjustForHiPEPrologue(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Erlang programs may need a special prologue to handle the stack size they might need at runtime.
const X86Subtarget & STI
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117