1 //===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements X86-specific bits of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
15 #define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
17 #include "llvm/Target/TargetFrameLowering.h"
21 class MachineInstrBuilder;
22 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 TargetInstrInfo &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. Returns instruction just
53 /// after the expansion.
54 MachineInstr *emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
55 MachineBasicBlock::iterator MBBI,
56 const DebugLoc &DL, bool InProlog) const;
58 /// Replace a StackProbe inline-stub with the actual probe code inline.
59 void inlineStackProbe(MachineFunction &MF,
60 MachineBasicBlock &PrologMBB) const override;
62 void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MBBI,
64 const DebugLoc &DL) const;
66 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
68 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
69 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
71 void adjustForSegmentedStacks(MachineFunction &MF,
72 MachineBasicBlock &PrologueMBB) const override;
74 void adjustForHiPEPrologue(MachineFunction &MF,
75 MachineBasicBlock &PrologueMBB) const override;
77 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
78 RegScavenger *RS = nullptr) const override;
81 assignCalleeSavedSpillSlots(MachineFunction &MF,
82 const TargetRegisterInfo *TRI,
83 std::vector<CalleeSavedInfo> &CSI) const override;
85 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator MI,
87 const std::vector<CalleeSavedInfo> &CSI,
88 const TargetRegisterInfo *TRI) const override;
90 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
91 MachineBasicBlock::iterator MI,
92 const std::vector<CalleeSavedInfo> &CSI,
93 const TargetRegisterInfo *TRI) const override;
95 bool hasFP(const MachineFunction &MF) const override;
96 bool hasReservedCallFrame(const MachineFunction &MF) const override;
97 bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
98 bool needsFrameIndexResolution(const MachineFunction &MF) const override;
100 int getFrameIndexReference(const MachineFunction &MF, int FI,
101 unsigned &FrameReg) const override;
103 int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
105 bool IgnoreSPUpdates) const override;
107 MachineBasicBlock::iterator
108 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
109 MachineBasicBlock::iterator MI) const override;
111 unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
113 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
114 RegScavenger *RS) const override;
116 /// Check the instruction before/after the passed instruction. If
117 /// it is an ADD/SUB/LEA instruction it is deleted argument and the
118 /// stack adjustment is returned as a positive value for ADD/LEA and
119 /// a negative for SUB.
120 int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
121 bool doMergeWithPrevious) const;
123 /// Emit a series of instructions to increment / decrement the stack
124 /// pointer by a constant value.
125 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
126 int64_t NumBytes, bool InEpilogue) const;
128 /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
129 bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
131 /// Check whether or not the given \p MBB can be used as a prologue
133 /// The prologue will be inserted first in this basic block.
134 /// This method is used by the shrink-wrapping pass to decide if
135 /// \p MBB will be correctly handled by the target.
136 /// As soon as the target enable shrink-wrapping without overriding
137 /// this method, we assume that each basic block is a valid
139 bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
141 /// Check whether or not the given \p MBB can be used as a epilogue
143 /// The epilogue will be inserted before the first terminator of that block.
144 /// This method is used by the shrink-wrapping pass to decide if
145 /// \p MBB will be correctly handled by the target.
146 bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
148 /// Returns true if the target will correctly handle shrink wrapping.
149 bool enableShrinkWrapping(const MachineFunction &MF) const override;
151 /// Order the symbols in the local stack.
152 /// We want to place the local stack objects in some sort of sensible order.
153 /// The heuristic we use is to try and pack them according to static number
154 /// of uses and size in order to minimize code size.
155 void orderFrameObjects(const MachineFunction &MF,
156 SmallVectorImpl<int> &ObjectsToAllocate) const override;
158 /// convertArgMovsToPushes - This method tries to convert a call sequence
159 /// that uses sub and mov instructions to put the argument onto the stack
160 /// into a series of pushes.
161 /// Returns true if the transformation succeeded, false if not.
162 bool convertArgMovsToPushes(MachineFunction &MF,
163 MachineBasicBlock &MBB,
164 MachineBasicBlock::iterator I,
165 uint64_t Amount) const;
167 /// Wraps up getting a CFI index and building a MachineInstr for it.
168 void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
169 const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
171 /// Sets up EBP and optionally ESI based on the incoming EBP value. Only
172 /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
173 MachineBasicBlock::iterator
174 restoreWin32EHStackPointers(MachineBasicBlock &MBB,
175 MachineBasicBlock::iterator MBBI,
176 const DebugLoc &DL, bool RestoreSP = false) const;
179 uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
181 /// Emit target stack probe as a call to a helper function
182 MachineInstr *emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
183 MachineBasicBlock::iterator MBBI,
184 const DebugLoc &DL, bool InProlog) const;
186 /// Emit target stack probe as an inline sequence.
187 MachineInstr *emitStackProbeInline(MachineFunction &MF,
188 MachineBasicBlock &MBB,
189 MachineBasicBlock::iterator MBBI,
190 const DebugLoc &DL, bool InProlog) const;
192 /// Emit a stub to later inline the target stack probe.
193 MachineInstr *emitStackProbeInlineStub(MachineFunction &MF,
194 MachineBasicBlock &MBB,
195 MachineBasicBlock::iterator MBBI,
197 bool InProlog) const;
199 /// Aligns the stack pointer by ANDing it with -MaxAlign.
200 void BuildStackAlignAND(MachineBasicBlock &MBB,
201 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
202 unsigned Reg, uint64_t MaxAlign) const;
204 /// Make small positive stack adjustments using POPs.
205 bool adjustStackWithPops(MachineBasicBlock &MBB,
206 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
209 /// Adjusts the stack pointer using LEA, SUB, or ADD.
210 MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
211 MachineBasicBlock::iterator MBBI,
212 const DebugLoc &DL, int64_t Offset,
213 bool InEpilogue) const;
215 unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
217 unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
220 } // End llvm namespace