LLVM  9.0.0svn
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 
17 
18 namespace llvm {
19 
20 class MachineInstrBuilder;
21 class MCCFIInstruction;
22 class X86InstrInfo;
23 class X86Subtarget;
24 class X86RegisterInfo;
25 
27 public:
28  X86FrameLowering(const X86Subtarget &STI, unsigned StackAlignOverride);
29 
30  // Cached subtarget predicates.
31 
32  const X86Subtarget &STI;
33  const X86InstrInfo &TII;
35 
36  unsigned SlotSize;
37 
38  /// Is64Bit implies that x86_64 instructions are available.
39  bool Is64Bit;
40 
41  bool IsLP64;
42 
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.
47 
48  unsigned StackPtr;
49 
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.
54  MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
55  bool InProlog) const;
56 
57  /// Replace a StackProbe inline-stub with the actual probe code inline.
59  MachineBasicBlock &PrologMBB) const override;
60 
63  const DebugLoc &DL) const;
64 
65  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
66  /// the function.
67  void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
68  void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
69 
71  MachineBasicBlock &PrologueMBB) const override;
72 
74  MachineBasicBlock &PrologueMBB) const override;
75 
76  void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
77  RegScavenger *RS = nullptr) const override;
78 
79  bool
81  const TargetRegisterInfo *TRI,
82  std::vector<CalleeSavedInfo> &CSI) const override;
83 
86  const std::vector<CalleeSavedInfo> &CSI,
87  const TargetRegisterInfo *TRI) const override;
88 
91  std::vector<CalleeSavedInfo> &CSI,
92  const TargetRegisterInfo *TRI) const override;
93 
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;
98 
99  int getFrameIndexReference(const MachineFunction &MF, int FI,
100  unsigned &FrameReg) const override;
101 
103  int FI, unsigned &SPReg, int Adjustment) const;
104  int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
105  unsigned &FrameReg,
106  bool IgnoreSPUpdates) const override;
107 
110  MachineBasicBlock::iterator MI) const override;
111 
112  unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
113 
115  RegScavenger *RS) const override;
116 
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.
122  bool doMergeWithPrevious) const;
123 
124  /// Emit a series of instructions to increment / decrement the stack
125  /// pointer by a constant value.
127  const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
128 
129  /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
130  bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
131 
132  /// Check whether or not the given \p MBB can be used as a prologue
133  /// for the target.
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
139  /// prologue.
140  bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
141 
142  /// Check whether or not the given \p MBB can be used as a epilogue
143  /// for the target.
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;
148 
149  /// Returns true if the target will correctly handle shrink wrapping.
150  bool enableShrinkWrapping(const MachineFunction &MF) const override;
151 
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;
158 
159  /// Wraps up getting a CFI index and building a MachineInstr for it.
161  const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
162 
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.
168  const DebugLoc &DL, bool RestoreSP = false) const;
169 
170  int getInitialCFAOffset(const MachineFunction &MF) const override;
171 
172  unsigned getInitialCFARegister(const MachineFunction &MF) const override;
173 
174 private:
175  uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
176 
177  /// Emit target stack probe as a call to a helper function
178  void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
179  MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
180  bool InProlog) const;
181 
182  /// Emit target stack probe as an inline sequence.
183  void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
185  const DebugLoc &DL, bool InProlog) const;
186 
187  /// Emit a stub to later inline the target stack probe.
188  void emitStackProbeInlineStub(MachineFunction &MF, MachineBasicBlock &MBB,
190  const DebugLoc &DL, bool InProlog) const;
191 
192  /// Aligns the stack pointer by ANDing it with -MaxAlign.
193  void BuildStackAlignAND(MachineBasicBlock &MBB,
194  MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
195  unsigned Reg, uint64_t MaxAlign) const;
196 
197  /// Make small positive stack adjustments using POPs.
198  bool adjustStackWithPops(MachineBasicBlock &MBB,
199  MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
200  int Offset) const;
201 
202  /// Adjusts the stack pointer using LEA, SUB, or ADD.
203  MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
205  const DebugLoc &DL, int64_t Offset,
206  bool InEpilogue) const;
207 
208  unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
209 
210  unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
211 
212  /// Materialize the catchret target MBB in RAX.
213  void emitCatchRetReturnValue(MachineBasicBlock &MBB,
215  MachineInstr *CatchRet) const;
216 };
217 
218 } // End llvm namespace
219 
220 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned Reg
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.
A debug info location.
Definition: DebugLoc.h:33
X86FrameLowering(const X86Subtarget &STI, unsigned StackAlignOverride)
bool Uses64BitFramePtr
True if the 64-bit frame or stack pointer should be used.
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...
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override
bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const
Check that LEA can be used on SP in an epilogue sequence for MF.
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
bool needsFrameIndexResolution(const MachineFunction &MF) const override
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 inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologMBB) const override
Replace a StackProbe inline-stub with the actual probe code inline.
bool Is64Bit
Is64Bit implies that x86_64 instructions are available.
void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, const MCCFIInstruction &CFIInst) const
Wraps up getting a CFI index and building a MachineInstr for it.
unsigned getInitialCFARegister(const MachineFunction &MF) const override
Return initial CFA register 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.
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
const X86InstrInfo & TII
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const
Emit target stack probe code.
const X86Subtarget & STI
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
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...
Information about stack frame layout on the target.
int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, unsigned &FrameReg, bool IgnoreSPUpdates) const override
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override
canSimplifyCallFramePseudos - If there is a reserved call frame, the call frame pseudos can be simpli...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
int getFrameIndexReferenceSP(const MachineFunction &MF, int FI, unsigned &SPReg, int Adjustment) const
Representation of each machine instruction.
Definition: MachineInstr.h:63
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Adjust the prologue to have the function use segmented stacks.
const X86RegisterInfo * TRI
int getInitialCFAOffset(const MachineFunction &MF) const override
Return initial CFA offset value i.e.
int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
IRTranslator LLVM IR MI
void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL) const
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &ObjectsToAllocate) const override
Order the symbols in the local stack.
int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, bool doMergeWithPrevious) const
Check the instruction before/after the passed instruction.