LLVM  15.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 
18 #include "llvm/Support/TypeSize.h"
19 
20 namespace llvm {
21 
22 class MachineInstrBuilder;
23 class MCCFIInstruction;
24 class X86InstrInfo;
25 class X86Subtarget;
26 class X86RegisterInfo;
27 
29 public:
30  X86FrameLowering(const X86Subtarget &STI, MaybeAlign StackAlignOverride);
31 
32  // Cached subtarget predicates.
33 
34  const X86Subtarget &STI;
35  const X86InstrInfo &TII;
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.
57  void emitStackProbe(
59  MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
61 
62  bool stackProbeFunctionModifiesSP() const override;
63 
64  /// Replace a StackProbe inline-stub with the actual probe code inline.
66  MachineBasicBlock &PrologMBB) const override;
67 
70 
73  const DebugLoc &DL, bool IsPrologue) const;
74 
75  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
76  /// the function.
77  void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
78  void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
79 
81  MachineBasicBlock &PrologueMBB) const override;
82 
84  MachineBasicBlock &PrologueMBB) const override;
85 
86  void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
87  RegScavenger *RS = nullptr) const override;
88 
89  bool
91  const TargetRegisterInfo *TRI,
92  std::vector<CalleeSavedInfo> &CSI) const override;
93 
97  const TargetRegisterInfo *TRI) const override;
98 
99  bool
103  const TargetRegisterInfo *TRI) const override;
104 
105  bool hasFP(const MachineFunction &MF) const override;
106  bool hasReservedCallFrame(const MachineFunction &MF) const override;
107  bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
108  bool needsFrameIndexResolution(const MachineFunction &MF) const override;
109 
111  Register &FrameReg) const override;
112 
113  int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI,
114  Register &SPReg) const;
116  Register &SPReg, int Adjustment) const;
119  Register &FrameReg,
120  bool IgnoreSPUpdates) const override;
121 
124  MachineBasicBlock::iterator MI) const override;
125 
126  unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
127 
129  RegScavenger *RS) const override;
130 
131  void
133  RegScavenger *RS) const override;
134 
135  /// Check the instruction before/after the passed instruction. If
136  /// it is an ADD/SUB/LEA instruction it is deleted argument and the
137  /// stack adjustment is returned as a positive value for ADD/LEA and
138  /// a negative for SUB.
140  bool doMergeWithPrevious) const;
141 
142  /// Emit a series of instructions to increment / decrement the stack
143  /// pointer by a constant value.
145  const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
146 
147  /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
148  bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
149 
150  /// Check whether or not the given \p MBB can be used as a prologue
151  /// for the target.
152  /// The prologue will be inserted first in this basic block.
153  /// This method is used by the shrink-wrapping pass to decide if
154  /// \p MBB will be correctly handled by the target.
155  /// As soon as the target enable shrink-wrapping without overriding
156  /// this method, we assume that each basic block is a valid
157  /// prologue.
158  bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
159 
160  /// Check whether or not the given \p MBB can be used as a epilogue
161  /// for the target.
162  /// The epilogue will be inserted before the first terminator of that block.
163  /// This method is used by the shrink-wrapping pass to decide if
164  /// \p MBB will be correctly handled by the target.
165  bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
166 
167  /// Returns true if the target will correctly handle shrink wrapping.
168  bool enableShrinkWrapping(const MachineFunction &MF) const override;
169 
170  /// Order the symbols in the local stack.
171  /// We want to place the local stack objects in some sort of sensible order.
172  /// The heuristic we use is to try and pack them according to static number
173  /// of uses and size in order to minimize code size.
174  void orderFrameObjects(const MachineFunction &MF,
175  SmallVectorImpl<int> &ObjectsToAllocate) const override;
176 
177  /// Wraps up getting a CFI index and building a MachineInstr for it.
179  const DebugLoc &DL, const MCCFIInstruction &CFIInst,
181 
182  /// Sets up EBP and optionally ESI based on the incoming EBP value. Only
183  /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
187  const DebugLoc &DL, bool RestoreSP = false) const;
188 
190 
191  int getInitialCFAOffset(const MachineFunction &MF) const override;
192 
193  Register getInitialCFARegister(const MachineFunction &MF) const override;
194 
195  /// Return true if the function has a redzone (accessible bytes past the
196  /// frame of the top of stack function) as part of it's ABI.
197  bool has128ByteRedZone(const MachineFunction& MF) const;
198 
199 private:
200  bool isWin64Prologue(const MachineFunction &MF) const;
201 
202  bool needsDwarfCFI(const MachineFunction &MF) const;
203 
204  uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
205 
206  /// Emit target stack probe as a call to a helper function
207  void emitStackProbeCall(
209  MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
211 
212  /// Emit target stack probe as an inline sequence.
213  void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
215  const DebugLoc &DL, bool InProlog) const;
216  void emitStackProbeInlineWindowsCoreCLR64(MachineFunction &MF,
219  const DebugLoc &DL,
220  bool InProlog) const;
221  void emitStackProbeInlineGeneric(MachineFunction &MF, MachineBasicBlock &MBB,
223  const DebugLoc &DL, bool InProlog) const;
224 
225  void emitStackProbeInlineGenericBlock(MachineFunction &MF,
228  const DebugLoc &DL, uint64_t Offset,
229  uint64_t Align) const;
230 
231  void emitStackProbeInlineGenericLoop(MachineFunction &MF,
234  const DebugLoc &DL, uint64_t Offset,
235  uint64_t Align) const;
236 
237  /// Emit target zero call-used regs.
238  void emitZeroCallUsedRegs(BitVector RegsToZero,
239  MachineBasicBlock &MBB) const override;
240 
241  void adjustFrameForMsvcCxxEh(MachineFunction &MF) const;
242 
243  /// Aligns the stack pointer by ANDing it with -MaxAlign.
244  void BuildStackAlignAND(MachineBasicBlock &MBB,
246  unsigned Reg, uint64_t MaxAlign) const;
247 
248  /// Make small positive stack adjustments using POPs.
249  bool adjustStackWithPops(MachineBasicBlock &MBB,
251  int Offset) const;
252 
253  /// Adjusts the stack pointer using LEA, SUB, or ADD.
254  MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
256  const DebugLoc &DL, int64_t Offset,
257  bool InEpilogue) const;
258 
259  unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
260 
261  unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
262 
263  /// Materialize the catchret target MBB in RAX.
264  void emitCatchRetReturnValue(MachineBasicBlock &MBB,
266  MachineInstr *CatchRet) const;
267 };
268 
269 } // End llvm namespace
270 
271 #endif
llvm::X86FrameLowering::hasReservedCallFrame
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Definition: X86FrameLowering.cpp:61
llvm::X86FrameLowering::canUseAsEpilogue
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
Definition: X86FrameLowering.cpp:3516
llvm::X86FrameLowering::TRI
const X86RegisterInfo * TRI
Definition: X86FrameLowering.h:36
llvm::X86FrameLowering::adjustForHiPEPrologue
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.
Definition: X86FrameLowering.cpp:3188
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::X86FrameLowering::spillCalleeSavedRegisters
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...
Definition: X86FrameLowering.cpp:2683
TargetFrameLowering.h
llvm::TargetFrameLowering
Information about stack frame layout on the target.
Definition: TargetFrameLowering.h:43
llvm::X86FrameLowering::hasFP
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register.
Definition: X86FrameLowering.cpp:95
TypeSize.h
llvm::X86FrameLowering::emitCalleeSavedFrameMoves
void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool IsPrologue) const
Definition: X86FrameLowering.cpp:472
llvm::X86Subtarget
Definition: X86Subtarget.h:52
llvm::X86Disassembler::Reg
Reg
All possible values of the reg field in the ModR/M byte.
Definition: X86DisassemblerDecoder.h:462
llvm::X86FrameLowering::emitCalleeSavedFrameMovesFullCFA
void emitCalleeSavedFrameMovesFullCFA(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const override
Emits Dwarf Info specifying offsets of callee saved registers and frame pointer.
Definition: X86FrameLowering.cpp:451
llvm::X86FrameLowering::assignCalleeSavedSpillSlots
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
Definition: X86FrameLowering.cpp:2570
llvm::X86FrameLowering::enableShrinkWrapping
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
Definition: X86FrameLowering.cpp:3542
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:234
llvm::X86FrameLowering::orderFrameObjects
void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &ObjectsToAllocate) const override
Order the symbols in the local stack.
Definition: X86FrameLowering.cpp:3702
llvm::X86FrameLowering::eliminateCallFramePseudoInstr
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...
Definition: X86FrameLowering.cpp:3397
llvm::Optional
Definition: APInt.h:33
llvm::X86FrameLowering::canUseAsPrologue
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a prologue for the target.
Definition: X86FrameLowering.cpp:3506
llvm::X86FrameLowering::has128ByteRedZone
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...
Definition: X86FrameLowering.cpp:1367
llvm::X86FrameLowering::getFrameIndexReferencePreferSP
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 ...
Definition: X86FrameLowering.cpp:2488
llvm::X86FrameLowering::restoreWinEHStackPointersInParent
void restoreWinEHStackPointersInParent(MachineFunction &MF) const
Definition: X86FrameLowering.cpp:3857
llvm::X86FrameLowering::stackProbeFunctionModifiesSP
bool stackProbeFunctionModifiesSP() const override
Does the stack probe function call return with a modified stack pointer?
Definition: X86FrameLowering.cpp:597
llvm::X86FrameLowering::getWin64EHFrameIndexRef
int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI, Register &SPReg) const
Definition: X86FrameLowering.cpp:2462
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:306
llvm::X86FrameLowering::needsFrameIndexResolution
bool needsFrameIndexResolution(const MachineFunction &MF) const override
Definition: X86FrameLowering.cpp:87
llvm::X86FrameLowering::getFrameIndexReferenceSP
StackOffset getFrameIndexReferenceSP(const MachineFunction &MF, int FI, Register &SPReg, int Adjustment) const
Definition: X86FrameLowering.cpp:2478
llvm::X86FrameLowering::canUseLEAForSPInEpilogue
bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const
Check that LEA can be used on SP in an epilogue sequence for MF.
Definition: X86FrameLowering.cpp:2082
llvm::X86FrameLowering::TII
const X86InstrInfo & TII
Definition: X86FrameLowering.h:35
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:147
llvm::X86FrameLowering::emitStackProbe
void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog, Optional< MachineFunction::DebugInstrOperandPair > InstrNum=None) const
Emit target stack probe code.
Definition: X86FrameLowering.cpp:580
llvm::X86FrameLowering::processFunctionBeforeFrameIndicesReplaced
void processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameIndicesReplaced - This method is called immediately before MO_FrameIndex op...
Definition: X86FrameLowering.cpp:3851
llvm::X86FrameLowering::restoreWin32EHStackPointers
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.
Definition: X86FrameLowering.cpp:3559
llvm::BitVector
Definition: BitVector.h:75
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::None
const NoneType None
Definition: None.h:24
llvm::X86FrameLowering::processFunctionBeforeFrameFinalized
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
Definition: X86FrameLowering.cpp:3786
llvm::MCCFIInstruction
Definition: MCDwarf.h:472
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::X86FrameLowering::emitSPUpdate
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.
Definition: X86FrameLowering.cpp:218
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::MachineInstrBuilder
Definition: MachineInstrBuilder.h:69
uint64_t
llvm::X86FrameLowering
Definition: X86FrameLowering.h:28
llvm::X86FrameLowering::emitPrologue
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
Definition: X86FrameLowering.cpp:1473
llvm::X86FrameLowering::restoreCalleeSavedRegisters
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...
Definition: X86FrameLowering.cpp:2783
llvm::RegScavenger
Definition: RegisterScavenging.h:34
llvm::MachineInstr::NoFlags
@ NoFlags
Definition: MachineInstr.h:83
llvm::X86FrameLowering::emitEpilogue
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Definition: X86FrameLowering.cpp:2166
llvm::X86FrameLowering::mergeSPUpdates
int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, bool doMergeWithPrevious) const
Check the instruction before/after the passed instruction.
Definition: X86FrameLowering.cpp:375
llvm::MachineInstr::MIFlag
MIFlag
Definition: MachineInstr.h:82
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::X86InstrInfo
Definition: X86InstrInfo.h:138
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
llvm::StackOffset
StackOffset is a class to represent an offset with 2 dimensions, named fixed and scalable,...
Definition: TypeSize.h:134
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::X86FrameLowering::adjustForSegmentedStacks
void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Adjust the prologue to have the function use segmented stacks.
Definition: X86FrameLowering.cpp:2904
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::X86FrameLowering::SlotSize
unsigned SlotSize
Definition: X86FrameLowering.h:38
llvm::X86FrameLowering::STI
const X86Subtarget & STI
Definition: X86FrameLowering.h:34
llvm::X86FrameLowering::inlineStackProbe
void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologMBB) const override
Replace a StackProbe inline-stub with the actual probe code inline.
Definition: X86FrameLowering.cpp:601
llvm::X86FrameLowering::determineCalleeSaves
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Definition: X86FrameLowering.cpp:2837
llvm::X86FrameLowering::IsLP64
bool IsLP64
Definition: X86FrameLowering.h:43
llvm::X86FrameLowering::X86FrameLowering
X86FrameLowering(const X86Subtarget &STI, MaybeAlign StackAlignOverride)
Definition: X86FrameLowering.cpp:47
llvm::X86FrameLowering::canSimplifyCallFramePseudos
bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override
canSimplifyCallFramePseudos - If there is a reserved call frame, the call frame pseudos can be simpli...
Definition: X86FrameLowering.cpp:72
llvm::X86FrameLowering::Is64Bit
bool Is64Bit
Is64Bit implies that x86_64 instructions are available.
Definition: X86FrameLowering.h:41
llvm::X86FrameLowering::Uses64BitFramePtr
bool Uses64BitFramePtr
True if the 64-bit frame or stack pointer should be used.
Definition: X86FrameLowering.h:48
llvm::SmallVectorImpl< int >
llvm::X86FrameLowering::BuildCFI
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.
Definition: X86FrameLowering.cpp:437
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::X86FrameLowering::StackPtr
unsigned StackPtr
Definition: X86FrameLowering.h:50
llvm::X86FrameLowering::getWinEHParentFrameOffset
unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override
Definition: X86FrameLowering.cpp:3774
MachineFunction.h
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::X86FrameLowering::getFrameIndexReference
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...
Definition: X86FrameLowering.cpp:2380
llvm::X86FrameLowering::getInitialCFAOffset
int getInitialCFAOffset(const MachineFunction &MF) const override
Return initial CFA offset value i.e.
Definition: X86FrameLowering.cpp:3622
llvm::X86FrameLowering::getInitialCFARegister
Register getInitialCFARegister(const MachineFunction &MF) const override
Return initial CFA register value i.e.
Definition: X86FrameLowering.cpp:3627
llvm::X86RegisterInfo
Definition: X86RegisterInfo.h:24