1 //===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- 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 // This class implements X86-specific bits of TargetFrameLowering class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
14 #define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
16 #include "llvm/CodeGen/TargetFrameLowering.h"
20 class MachineInstrBuilder
;
21 class MCCFIInstruction
;
24 class X86RegisterInfo
;
26 class X86FrameLowering
: public TargetFrameLowering
{
28 X86FrameLowering(const X86Subtarget
&STI
, unsigned StackAlignOverride
);
30 // Cached subtarget predicates.
32 const X86Subtarget
&STI
;
33 const X86InstrInfo
&TII
;
34 const X86RegisterInfo
*TRI
;
38 /// Is64Bit implies that x86_64 instructions are available.
43 /// True if the 64-bit frame or stack pointer should be used. True for most
44 /// 64-bit targets with the exception of x32. If this is false, 32-bit
45 /// instruction operands should be used to manipulate StackPtr and FramePtr.
46 bool Uses64BitFramePtr
;
50 /// Emit target stack probe code. This is required for all
51 /// large stack allocations on Windows. The caller is required to materialize
52 /// the number of bytes to probe in RAX/EAX.
53 void emitStackProbe(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
54 MachineBasicBlock::iterator MBBI
, const DebugLoc
&DL
,
57 /// Replace a StackProbe inline-stub with the actual probe code inline.
58 void inlineStackProbe(MachineFunction
&MF
,
59 MachineBasicBlock
&PrologMBB
) const override
;
61 void emitCalleeSavedFrameMoves(MachineBasicBlock
&MBB
,
62 MachineBasicBlock::iterator MBBI
,
63 const DebugLoc
&DL
) const;
65 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
67 void emitPrologue(MachineFunction
&MF
, MachineBasicBlock
&MBB
) const override
;
68 void emitEpilogue(MachineFunction
&MF
, MachineBasicBlock
&MBB
) const override
;
70 void adjustForSegmentedStacks(MachineFunction
&MF
,
71 MachineBasicBlock
&PrologueMBB
) const override
;
73 void adjustForHiPEPrologue(MachineFunction
&MF
,
74 MachineBasicBlock
&PrologueMBB
) const override
;
76 void determineCalleeSaves(MachineFunction
&MF
, BitVector
&SavedRegs
,
77 RegScavenger
*RS
= nullptr) const override
;
80 assignCalleeSavedSpillSlots(MachineFunction
&MF
,
81 const TargetRegisterInfo
*TRI
,
82 std::vector
<CalleeSavedInfo
> &CSI
) const override
;
84 bool spillCalleeSavedRegisters(MachineBasicBlock
&MBB
,
85 MachineBasicBlock::iterator MI
,
86 const std::vector
<CalleeSavedInfo
> &CSI
,
87 const TargetRegisterInfo
*TRI
) const override
;
89 bool restoreCalleeSavedRegisters(MachineBasicBlock
&MBB
,
90 MachineBasicBlock::iterator MI
,
91 std::vector
<CalleeSavedInfo
> &CSI
,
92 const TargetRegisterInfo
*TRI
) const override
;
94 bool hasFP(const MachineFunction
&MF
) const override
;
95 bool hasReservedCallFrame(const MachineFunction
&MF
) const override
;
96 bool canSimplifyCallFramePseudos(const MachineFunction
&MF
) const override
;
97 bool needsFrameIndexResolution(const MachineFunction
&MF
) const override
;
99 int getFrameIndexReference(const MachineFunction
&MF
, int FI
,
100 unsigned &FrameReg
) const override
;
102 int getFrameIndexReferenceSP(const MachineFunction
&MF
,
103 int FI
, unsigned &SPReg
, int Adjustment
) const;
104 int getFrameIndexReferencePreferSP(const MachineFunction
&MF
, int FI
,
106 bool IgnoreSPUpdates
) const override
;
108 MachineBasicBlock::iterator
109 eliminateCallFramePseudoInstr(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
110 MachineBasicBlock::iterator MI
) const override
;
112 unsigned getWinEHParentFrameOffset(const MachineFunction
&MF
) const override
;
114 void processFunctionBeforeFrameFinalized(MachineFunction
&MF
,
115 RegScavenger
*RS
) const override
;
117 /// Check the instruction before/after the passed instruction. If
118 /// it is an ADD/SUB/LEA instruction it is deleted argument and the
119 /// stack adjustment is returned as a positive value for ADD/LEA and
120 /// a negative for SUB.
121 int mergeSPUpdates(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator
&MBBI
,
122 bool doMergeWithPrevious
) const;
124 /// Emit a series of instructions to increment / decrement the stack
125 /// pointer by a constant value.
126 void emitSPUpdate(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator
&MBBI
,
127 const DebugLoc
&DL
, int64_t NumBytes
, bool InEpilogue
) const;
129 /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
130 bool canUseLEAForSPInEpilogue(const MachineFunction
&MF
) const;
132 /// Check whether or not the given \p MBB can be used as a prologue
134 /// The prologue will be inserted first in this basic block.
135 /// This method is used by the shrink-wrapping pass to decide if
136 /// \p MBB will be correctly handled by the target.
137 /// As soon as the target enable shrink-wrapping without overriding
138 /// this method, we assume that each basic block is a valid
140 bool canUseAsPrologue(const MachineBasicBlock
&MBB
) const override
;
142 /// Check whether or not the given \p MBB can be used as a epilogue
144 /// The epilogue will be inserted before the first terminator of that block.
145 /// This method is used by the shrink-wrapping pass to decide if
146 /// \p MBB will be correctly handled by the target.
147 bool canUseAsEpilogue(const MachineBasicBlock
&MBB
) const override
;
149 /// Returns true if the target will correctly handle shrink wrapping.
150 bool enableShrinkWrapping(const MachineFunction
&MF
) const override
;
152 /// Order the symbols in the local stack.
153 /// We want to place the local stack objects in some sort of sensible order.
154 /// The heuristic we use is to try and pack them according to static number
155 /// of uses and size in order to minimize code size.
156 void orderFrameObjects(const MachineFunction
&MF
,
157 SmallVectorImpl
<int> &ObjectsToAllocate
) const override
;
159 /// Wraps up getting a CFI index and building a MachineInstr for it.
160 void BuildCFI(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MBBI
,
161 const DebugLoc
&DL
, const MCCFIInstruction
&CFIInst
) const;
163 /// Sets up EBP and optionally ESI based on the incoming EBP value. Only
164 /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
165 MachineBasicBlock::iterator
166 restoreWin32EHStackPointers(MachineBasicBlock
&MBB
,
167 MachineBasicBlock::iterator MBBI
,
168 const DebugLoc
&DL
, bool RestoreSP
= false) const;
170 int getInitialCFAOffset(const MachineFunction
&MF
) const override
;
172 unsigned getInitialCFARegister(const MachineFunction
&MF
) const override
;
174 /// Return true if the function has a redzone (accessible bytes past the
175 /// frame of the top of stack function) as part of it's ABI.
176 bool has128ByteRedZone(const MachineFunction
& MF
) const;
179 uint64_t calculateMaxStackAlign(const MachineFunction
&MF
) const;
181 /// Emit target stack probe as a call to a helper function
182 void emitStackProbeCall(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
183 MachineBasicBlock::iterator MBBI
, const DebugLoc
&DL
,
184 bool InProlog
) const;
186 /// Emit target stack probe as an inline sequence.
187 void emitStackProbeInline(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
188 MachineBasicBlock::iterator MBBI
,
189 const DebugLoc
&DL
, bool InProlog
) const;
191 /// Emit a stub to later inline the target stack probe.
192 void emitStackProbeInlineStub(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
193 MachineBasicBlock::iterator MBBI
,
194 const DebugLoc
&DL
, bool InProlog
) const;
196 /// Aligns the stack pointer by ANDing it with -MaxAlign.
197 void BuildStackAlignAND(MachineBasicBlock
&MBB
,
198 MachineBasicBlock::iterator MBBI
, const DebugLoc
&DL
,
199 unsigned Reg
, uint64_t MaxAlign
) const;
201 /// Make small positive stack adjustments using POPs.
202 bool adjustStackWithPops(MachineBasicBlock
&MBB
,
203 MachineBasicBlock::iterator MBBI
, const DebugLoc
&DL
,
206 /// Adjusts the stack pointer using LEA, SUB, or ADD.
207 MachineInstrBuilder
BuildStackAdjustment(MachineBasicBlock
&MBB
,
208 MachineBasicBlock::iterator MBBI
,
209 const DebugLoc
&DL
, int64_t Offset
,
210 bool InEpilogue
) const;
212 unsigned getPSPSlotOffsetFromSP(const MachineFunction
&MF
) const;
214 unsigned getWinEHFuncletFrameSize(const MachineFunction
&MF
) const;
216 /// Materialize the catchret target MBB in RAX.
217 void emitCatchRetReturnValue(MachineBasicBlock
&MBB
,
218 MachineBasicBlock::iterator MBBI
,
219 MachineInstr
*CatchRet
) const;
222 } // End llvm namespace