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 
16 #include "llvm/CodeGen/TargetFrameLowering.h"
17 
18 namespace llvm {
19 
20 class MachineInstrBuilder;
21 class MCCFIInstruction;
22 class X86InstrInfo;
23 class X86Subtarget;
24 class X86RegisterInfo;
25 
26 class X86FrameLowering : public TargetFrameLowering {
27 public:
28   X86FrameLowering(const X86Subtarget &STI, MaybeAlign StackAlignOverride);
29 
30   // Cached subtarget predicates.
31 
32   const X86Subtarget &STI;
33   const X86InstrInfo &TII;
34   const X86RegisterInfo *TRI;
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.
46   bool Uses64BitFramePtr;
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.
53   void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
54                       MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
55                       bool InProlog) const;
56 
57   /// Replace a StackProbe inline-stub with the actual probe code inline.
58   void inlineStackProbe(MachineFunction &MF,
59                         MachineBasicBlock &PrologMBB) const override;
60 
61   void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
62                                  MachineBasicBlock::iterator MBBI,
63                                  const DebugLoc &DL, bool IsPrologue) 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 
70   void adjustForSegmentedStacks(MachineFunction &MF,
71                                 MachineBasicBlock &PrologueMBB) const override;
72 
73   void adjustForHiPEPrologue(MachineFunction &MF,
74                              MachineBasicBlock &PrologueMBB) const override;
75 
76   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
77                             RegScavenger *RS = nullptr) const override;
78 
79   bool
80   assignCalleeSavedSpillSlots(MachineFunction &MF,
81                               const TargetRegisterInfo *TRI,
82                               std::vector<CalleeSavedInfo> &CSI) const override;
83 
84   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
85                                  MachineBasicBlock::iterator MI,
86                                  ArrayRef<CalleeSavedInfo> CSI,
87                                  const TargetRegisterInfo *TRI) const override;
88 
89   bool
90   restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
91                               MachineBasicBlock::iterator MI,
92                               MutableArrayRef<CalleeSavedInfo> CSI,
93                               const TargetRegisterInfo *TRI) const override;
94 
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;
99 
100   int getFrameIndexReference(const MachineFunction &MF, int FI,
101                              Register &FrameReg) const override;
102 
103   int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI,
104                               Register &SPReg) const;
105   int getFrameIndexReferenceSP(const MachineFunction &MF, int FI,
106                                Register &SPReg, int Adjustment) const;
107   int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
108                                      Register &FrameReg,
109                                      bool IgnoreSPUpdates) const override;
110 
111   MachineBasicBlock::iterator
112   eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
113                                 MachineBasicBlock::iterator MI) const override;
114 
115   unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
116 
117   void processFunctionBeforeFrameFinalized(MachineFunction &MF,
118                                            RegScavenger *RS) const override;
119 
120   void
121   processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF,
122                                             RegScavenger *RS) const override;
123 
124   /// Check the instruction before/after the passed instruction. If
125   /// it is an ADD/SUB/LEA instruction it is deleted argument and the
126   /// stack adjustment is returned as a positive value for ADD/LEA and
127   /// a negative for SUB.
128   int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
129                      bool doMergeWithPrevious) const;
130 
131   /// Emit a series of instructions to increment / decrement the stack
132   /// pointer by a constant value.
133   void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
134                     const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
135 
136   /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
137   bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
138 
139   /// Check whether or not the given \p MBB can be used as a prologue
140   /// for the target.
141   /// The prologue will be inserted first in this basic block.
142   /// This method is used by the shrink-wrapping pass to decide if
143   /// \p MBB will be correctly handled by the target.
144   /// As soon as the target enable shrink-wrapping without overriding
145   /// this method, we assume that each basic block is a valid
146   /// prologue.
147   bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
148 
149   /// Check whether or not the given \p MBB can be used as a epilogue
150   /// for the target.
151   /// The epilogue will be inserted before the first terminator of that block.
152   /// This method is used by the shrink-wrapping pass to decide if
153   /// \p MBB will be correctly handled by the target.
154   bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
155 
156   /// Returns true if the target will correctly handle shrink wrapping.
157   bool enableShrinkWrapping(const MachineFunction &MF) const override;
158 
159   /// Order the symbols in the local stack.
160   /// We want to place the local stack objects in some sort of sensible order.
161   /// The heuristic we use is to try and pack them according to static number
162   /// of uses and size in order to minimize code size.
163   void orderFrameObjects(const MachineFunction &MF,
164                          SmallVectorImpl<int> &ObjectsToAllocate) const override;
165 
166   /// Wraps up getting a CFI index and building a MachineInstr for it.
167   void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
168                 const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
169 
170   /// Sets up EBP and optionally ESI based on the incoming EBP value.  Only
171   /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
172   MachineBasicBlock::iterator
173   restoreWin32EHStackPointers(MachineBasicBlock &MBB,
174                               MachineBasicBlock::iterator MBBI,
175                               const DebugLoc &DL, bool RestoreSP = false) const;
176 
177   void restoreWinEHStackPointersInParent(MachineFunction &MF) const;
178 
179   int getInitialCFAOffset(const MachineFunction &MF) const override;
180 
181   Register getInitialCFARegister(const MachineFunction &MF) const override;
182 
183   /// Return true if the function has a redzone (accessible bytes past the
184   /// frame of the top of stack function) as part of it's ABI.
185   bool has128ByteRedZone(const MachineFunction& MF) const;
186 
187 private:
188   uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
189 
190   /// Emit target stack probe as a call to a helper function
191   void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
192                           MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
193                           bool InProlog) const;
194 
195   /// Emit target stack probe as an inline sequence.
196   void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
197                             MachineBasicBlock::iterator MBBI,
198                             const DebugLoc &DL, bool InProlog) const;
199   void emitStackProbeInlineWindowsCoreCLR64(MachineFunction &MF,
200                                             MachineBasicBlock &MBB,
201                                             MachineBasicBlock::iterator MBBI,
202                                             const DebugLoc &DL,
203                                             bool InProlog) const;
204   void emitStackProbeInlineGeneric(MachineFunction &MF, MachineBasicBlock &MBB,
205                                    MachineBasicBlock::iterator MBBI,
206                                    const DebugLoc &DL, bool InProlog) const;
207 
208   void emitStackProbeInlineGenericBlock(MachineFunction &MF,
209                                         MachineBasicBlock &MBB,
210                                         MachineBasicBlock::iterator MBBI,
211                                         const DebugLoc &DL,
212                                         uint64_t Offset) const;
213 
214   void emitStackProbeInlineGenericLoop(MachineFunction &MF,
215                                        MachineBasicBlock &MBB,
216                                        MachineBasicBlock::iterator MBBI,
217                                        const DebugLoc &DL,
218                                        uint64_t Offset) const;
219 
220   /// Emit a stub to later inline the target stack probe.
221   MachineInstr *emitStackProbeInlineStub(MachineFunction &MF,
222                                          MachineBasicBlock &MBB,
223                                          MachineBasicBlock::iterator MBBI,
224                                          const DebugLoc &DL,
225                                          bool InProlog) const;
226 
227   /// Aligns the stack pointer by ANDing it with -MaxAlign.
228   void BuildStackAlignAND(MachineBasicBlock &MBB,
229                           MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
230                           unsigned Reg, uint64_t MaxAlign) const;
231 
232   /// Make small positive stack adjustments using POPs.
233   bool adjustStackWithPops(MachineBasicBlock &MBB,
234                            MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
235                            int Offset) const;
236 
237   /// Adjusts the stack pointer using LEA, SUB, or ADD.
238   MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
239                                            MachineBasicBlock::iterator MBBI,
240                                            const DebugLoc &DL, int64_t Offset,
241                                            bool InEpilogue) const;
242 
243   unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
244 
245   unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
246 
247   /// Materialize the catchret target MBB in RAX.
248   void emitCatchRetReturnValue(MachineBasicBlock &MBB,
249                                MachineBasicBlock::iterator MBBI,
250                                MachineInstr *CatchRet) const;
251 };
252 
253 } // End llvm namespace
254 
255 #endif
256