xref: /freebsd/contrib/llvm-project/llvm/lib/Target/X86/X86FrameLowering.h (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- C++ -*-==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This class implements X86-specific bits of TargetFrameLowering class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
140b57cec5SDimitry Andric #define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
150b57cec5SDimitry Andric 
164824e7fdSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
18e8d8bef9SDimitry Andric #include "llvm/Support/TypeSize.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric class MachineInstrBuilder;
230b57cec5SDimitry Andric class MCCFIInstruction;
240b57cec5SDimitry Andric class X86InstrInfo;
250b57cec5SDimitry Andric class X86Subtarget;
260b57cec5SDimitry Andric class X86RegisterInfo;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric class X86FrameLowering : public TargetFrameLowering {
290b57cec5SDimitry Andric public:
308bcb0991SDimitry Andric   X86FrameLowering(const X86Subtarget &STI, MaybeAlign StackAlignOverride);
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric   // Cached subtarget predicates.
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   const X86Subtarget &STI;
350b57cec5SDimitry Andric   const X86InstrInfo &TII;
360b57cec5SDimitry Andric   const X86RegisterInfo *TRI;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   unsigned SlotSize;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   /// Is64Bit implies that x86_64 instructions are available.
410b57cec5SDimitry Andric   bool Is64Bit;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   bool IsLP64;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   /// True if the 64-bit frame or stack pointer should be used. True for most
460b57cec5SDimitry Andric   /// 64-bit targets with the exception of x32. If this is false, 32-bit
470b57cec5SDimitry Andric   /// instruction operands should be used to manipulate StackPtr and FramePtr.
480b57cec5SDimitry Andric   bool Uses64BitFramePtr;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   unsigned StackPtr;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   /// Emit target stack probe code. This is required for all
530b57cec5SDimitry Andric   /// large stack allocations on Windows. The caller is required to materialize
540b57cec5SDimitry Andric   /// the number of bytes to probe in RAX/EAX.
554824e7fdSDimitry Andric   /// \p InstrNum optionally contains a debug-info instruction number for the
564824e7fdSDimitry Andric   ///    new stack pointer.
574824e7fdSDimitry Andric   void emitStackProbe(
584824e7fdSDimitry Andric       MachineFunction &MF, MachineBasicBlock &MBB,
594824e7fdSDimitry Andric       MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
604824e7fdSDimitry Andric       Optional<MachineFunction::DebugInstrOperandPair> InstrNum = None) const;
614824e7fdSDimitry Andric 
624824e7fdSDimitry Andric   bool stackProbeFunctionModifiesSP() const override;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   /// Replace a StackProbe inline-stub with the actual probe code inline.
650b57cec5SDimitry Andric   void inlineStackProbe(MachineFunction &MF,
660b57cec5SDimitry Andric                         MachineBasicBlock &PrologMBB) const override;
670b57cec5SDimitry Andric 
68*04eeddc0SDimitry Andric   void emitCalleeSavedFrameMovesFullCFA(
69*04eeddc0SDimitry Andric       MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const override;
705ffd83dbSDimitry Andric 
710b57cec5SDimitry Andric   void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
720b57cec5SDimitry Andric                                  MachineBasicBlock::iterator MBBI,
73fe6060f1SDimitry Andric                                  const DebugLoc &DL, bool IsPrologue) const;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
760b57cec5SDimitry Andric   /// the function.
770b57cec5SDimitry Andric   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
780b57cec5SDimitry Andric   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   void adjustForSegmentedStacks(MachineFunction &MF,
810b57cec5SDimitry Andric                                 MachineBasicBlock &PrologueMBB) const override;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   void adjustForHiPEPrologue(MachineFunction &MF,
840b57cec5SDimitry Andric                              MachineBasicBlock &PrologueMBB) const override;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
870b57cec5SDimitry Andric                             RegScavenger *RS = nullptr) const override;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   bool
900b57cec5SDimitry Andric   assignCalleeSavedSpillSlots(MachineFunction &MF,
910b57cec5SDimitry Andric                               const TargetRegisterInfo *TRI,
920b57cec5SDimitry Andric                               std::vector<CalleeSavedInfo> &CSI) const override;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
950b57cec5SDimitry Andric                                  MachineBasicBlock::iterator MI,
965ffd83dbSDimitry Andric                                  ArrayRef<CalleeSavedInfo> CSI,
970b57cec5SDimitry Andric                                  const TargetRegisterInfo *TRI) const override;
980b57cec5SDimitry Andric 
995ffd83dbSDimitry Andric   bool
1005ffd83dbSDimitry Andric   restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1010b57cec5SDimitry Andric                               MachineBasicBlock::iterator MI,
1025ffd83dbSDimitry Andric                               MutableArrayRef<CalleeSavedInfo> CSI,
1030b57cec5SDimitry Andric                               const TargetRegisterInfo *TRI) const override;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   bool hasFP(const MachineFunction &MF) const override;
1060b57cec5SDimitry Andric   bool hasReservedCallFrame(const MachineFunction &MF) const override;
1070b57cec5SDimitry Andric   bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
1080b57cec5SDimitry Andric   bool needsFrameIndexResolution(const MachineFunction &MF) const override;
1090b57cec5SDimitry Andric 
110e8d8bef9SDimitry Andric   StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
1115ffd83dbSDimitry Andric                                      Register &FrameReg) const override;
1120b57cec5SDimitry Andric 
1135ffd83dbSDimitry Andric   int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI,
1145ffd83dbSDimitry Andric                               Register &SPReg) const;
115e8d8bef9SDimitry Andric   StackOffset getFrameIndexReferenceSP(const MachineFunction &MF, int FI,
1165ffd83dbSDimitry Andric                                        Register &SPReg, int Adjustment) const;
117e8d8bef9SDimitry Andric   StackOffset
118e8d8bef9SDimitry Andric   getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
1195ffd83dbSDimitry Andric                                  Register &FrameReg,
1200b57cec5SDimitry Andric                                  bool IgnoreSPUpdates) const override;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   MachineBasicBlock::iterator
1230b57cec5SDimitry Andric   eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1240b57cec5SDimitry Andric                                 MachineBasicBlock::iterator MI) const override;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   void processFunctionBeforeFrameFinalized(MachineFunction &MF,
1290b57cec5SDimitry Andric                                            RegScavenger *RS) const override;
1300b57cec5SDimitry Andric 
1315ffd83dbSDimitry Andric   void
1325ffd83dbSDimitry Andric   processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF,
1335ffd83dbSDimitry Andric                                             RegScavenger *RS) const override;
1345ffd83dbSDimitry Andric 
1350b57cec5SDimitry Andric   /// Check the instruction before/after the passed instruction. If
1360b57cec5SDimitry Andric   /// it is an ADD/SUB/LEA instruction it is deleted argument and the
1370b57cec5SDimitry Andric   /// stack adjustment is returned as a positive value for ADD/LEA and
1380b57cec5SDimitry Andric   /// a negative for SUB.
1390b57cec5SDimitry Andric   int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1400b57cec5SDimitry Andric                      bool doMergeWithPrevious) const;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   /// Emit a series of instructions to increment / decrement the stack
1430b57cec5SDimitry Andric   /// pointer by a constant value.
1440b57cec5SDimitry Andric   void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1450b57cec5SDimitry Andric                     const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
1480b57cec5SDimitry Andric   bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   /// Check whether or not the given \p MBB can be used as a prologue
1510b57cec5SDimitry Andric   /// for the target.
1520b57cec5SDimitry Andric   /// The prologue will be inserted first in this basic block.
1530b57cec5SDimitry Andric   /// This method is used by the shrink-wrapping pass to decide if
1540b57cec5SDimitry Andric   /// \p MBB will be correctly handled by the target.
1550b57cec5SDimitry Andric   /// As soon as the target enable shrink-wrapping without overriding
1560b57cec5SDimitry Andric   /// this method, we assume that each basic block is a valid
1570b57cec5SDimitry Andric   /// prologue.
1580b57cec5SDimitry Andric   bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   /// Check whether or not the given \p MBB can be used as a epilogue
1610b57cec5SDimitry Andric   /// for the target.
1620b57cec5SDimitry Andric   /// The epilogue will be inserted before the first terminator of that block.
1630b57cec5SDimitry Andric   /// This method is used by the shrink-wrapping pass to decide if
1640b57cec5SDimitry Andric   /// \p MBB will be correctly handled by the target.
1650b57cec5SDimitry Andric   bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   /// Returns true if the target will correctly handle shrink wrapping.
1680b57cec5SDimitry Andric   bool enableShrinkWrapping(const MachineFunction &MF) const override;
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   /// Order the symbols in the local stack.
1710b57cec5SDimitry Andric   /// We want to place the local stack objects in some sort of sensible order.
1720b57cec5SDimitry Andric   /// The heuristic we use is to try and pack them according to static number
1730b57cec5SDimitry Andric   /// of uses and size in order to minimize code size.
1740b57cec5SDimitry Andric   void orderFrameObjects(const MachineFunction &MF,
1750b57cec5SDimitry Andric                          SmallVectorImpl<int> &ObjectsToAllocate) const override;
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   /// Wraps up getting a CFI index and building a MachineInstr for it.
1780b57cec5SDimitry Andric   void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1790b57cec5SDimitry Andric                 const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   /// Sets up EBP and optionally ESI based on the incoming EBP value.  Only
1820b57cec5SDimitry Andric   /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
1830b57cec5SDimitry Andric   MachineBasicBlock::iterator
1840b57cec5SDimitry Andric   restoreWin32EHStackPointers(MachineBasicBlock &MBB,
1850b57cec5SDimitry Andric                               MachineBasicBlock::iterator MBBI,
1860b57cec5SDimitry Andric                               const DebugLoc &DL, bool RestoreSP = false) const;
1870b57cec5SDimitry Andric 
1885ffd83dbSDimitry Andric   void restoreWinEHStackPointersInParent(MachineFunction &MF) const;
1895ffd83dbSDimitry Andric 
1900b57cec5SDimitry Andric   int getInitialCFAOffset(const MachineFunction &MF) const override;
1910b57cec5SDimitry Andric 
1925ffd83dbSDimitry Andric   Register getInitialCFARegister(const MachineFunction &MF) const override;
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   /// Return true if the function has a redzone (accessible bytes past the
1950b57cec5SDimitry Andric   /// frame of the top of stack function) as part of it's ABI.
1960b57cec5SDimitry Andric   bool has128ByteRedZone(const MachineFunction& MF) const;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric private:
199fe6060f1SDimitry Andric   bool isWin64Prologue(const MachineFunction &MF) const;
200fe6060f1SDimitry Andric 
201fe6060f1SDimitry Andric   bool needsDwarfCFI(const MachineFunction &MF) const;
202fe6060f1SDimitry Andric 
2030b57cec5SDimitry Andric   uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   /// Emit target stack probe as a call to a helper function
2064824e7fdSDimitry Andric   void emitStackProbeCall(
2074824e7fdSDimitry Andric       MachineFunction &MF, MachineBasicBlock &MBB,
2084824e7fdSDimitry Andric       MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
2094824e7fdSDimitry Andric       Optional<MachineFunction::DebugInstrOperandPair> InstrNum) const;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   /// Emit target stack probe as an inline sequence.
2120b57cec5SDimitry Andric   void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
2130b57cec5SDimitry Andric                             MachineBasicBlock::iterator MBBI,
2140b57cec5SDimitry Andric                             const DebugLoc &DL, bool InProlog) const;
2155ffd83dbSDimitry Andric   void emitStackProbeInlineWindowsCoreCLR64(MachineFunction &MF,
2165ffd83dbSDimitry Andric                                             MachineBasicBlock &MBB,
2175ffd83dbSDimitry Andric                                             MachineBasicBlock::iterator MBBI,
2185ffd83dbSDimitry Andric                                             const DebugLoc &DL,
2195ffd83dbSDimitry Andric                                             bool InProlog) const;
2205ffd83dbSDimitry Andric   void emitStackProbeInlineGeneric(MachineFunction &MF, MachineBasicBlock &MBB,
2210b57cec5SDimitry Andric                                    MachineBasicBlock::iterator MBBI,
2220b57cec5SDimitry Andric                                    const DebugLoc &DL, bool InProlog) const;
2230b57cec5SDimitry Andric 
2245ffd83dbSDimitry Andric   void emitStackProbeInlineGenericBlock(MachineFunction &MF,
2255ffd83dbSDimitry Andric                                         MachineBasicBlock &MBB,
2265ffd83dbSDimitry Andric                                         MachineBasicBlock::iterator MBBI,
227eaeb601bSDimitry Andric                                         const DebugLoc &DL, uint64_t Offset,
228eaeb601bSDimitry Andric                                         uint64_t Align) const;
2295ffd83dbSDimitry Andric 
2305ffd83dbSDimitry Andric   void emitStackProbeInlineGenericLoop(MachineFunction &MF,
2315ffd83dbSDimitry Andric                                        MachineBasicBlock &MBB,
2325ffd83dbSDimitry Andric                                        MachineBasicBlock::iterator MBBI,
233eaeb601bSDimitry Andric                                        const DebugLoc &DL, uint64_t Offset,
234eaeb601bSDimitry Andric                                        uint64_t Align) const;
2355ffd83dbSDimitry Andric 
236e8d8bef9SDimitry Andric   void adjustFrameForMsvcCxxEh(MachineFunction &MF) const;
2375ffd83dbSDimitry Andric 
2380b57cec5SDimitry Andric   /// Aligns the stack pointer by ANDing it with -MaxAlign.
2390b57cec5SDimitry Andric   void BuildStackAlignAND(MachineBasicBlock &MBB,
2400b57cec5SDimitry Andric                           MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
2410b57cec5SDimitry Andric                           unsigned Reg, uint64_t MaxAlign) const;
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   /// Make small positive stack adjustments using POPs.
2440b57cec5SDimitry Andric   bool adjustStackWithPops(MachineBasicBlock &MBB,
2450b57cec5SDimitry Andric                            MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
2460b57cec5SDimitry Andric                            int Offset) const;
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   /// Adjusts the stack pointer using LEA, SUB, or ADD.
2490b57cec5SDimitry Andric   MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
2500b57cec5SDimitry Andric                                            MachineBasicBlock::iterator MBBI,
2510b57cec5SDimitry Andric                                            const DebugLoc &DL, int64_t Offset,
2520b57cec5SDimitry Andric                                            bool InEpilogue) const;
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric   unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   /// Materialize the catchret target MBB in RAX.
2590b57cec5SDimitry Andric   void emitCatchRetReturnValue(MachineBasicBlock &MBB,
2600b57cec5SDimitry Andric                                MachineBasicBlock::iterator MBBI,
2610b57cec5SDimitry Andric                                MachineInstr *CatchRet) const;
2620b57cec5SDimitry Andric };
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric } // End llvm namespace
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric #endif
267