Home | History | Annotate | Download | only in X86
      1 //===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- C++ -*-==//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This class implements X86-specific bits of TargetFrameLowering class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
     15 #define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
     16 
     17 #include "llvm/CodeGen/TargetFrameLowering.h"
     18 
     19 namespace llvm {
     20 
     21 class MachineInstrBuilder;
     22 class MCCFIInstruction;
     23 class X86InstrInfo;
     24 class X86Subtarget;
     25 class X86RegisterInfo;
     26 
     27 class X86FrameLowering : public TargetFrameLowering {
     28 public:
     29   X86FrameLowering(const X86Subtarget &STI, unsigned StackAlignOverride);
     30 
     31   // Cached subtarget predicates.
     32 
     33   const X86Subtarget &STI;
     34   const X86InstrInfo &TII;
     35   const X86RegisterInfo *TRI;
     36 
     37   unsigned SlotSize;
     38 
     39   /// Is64Bit implies that x86_64 instructions are available.
     40   bool Is64Bit;
     41 
     42   bool IsLP64;
     43 
     44   /// True if the 64-bit frame or stack pointer should be used. True for most
     45   /// 64-bit targets with the exception of x32. If this is false, 32-bit
     46   /// instruction operands should be used to manipulate StackPtr and FramePtr.
     47   bool Uses64BitFramePtr;
     48 
     49   unsigned StackPtr;
     50 
     51   /// Emit target stack probe code. This is required for all
     52   /// large stack allocations on Windows. The caller is required to materialize
     53   /// the number of bytes to probe in RAX/EAX.
     54   void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
     55                       MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
     56                       bool InProlog) const;
     57 
     58   /// Replace a StackProbe inline-stub with the actual probe code inline.
     59   void inlineStackProbe(MachineFunction &MF,
     60                         MachineBasicBlock &PrologMBB) const override;
     61 
     62   void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
     63                                  MachineBasicBlock::iterator MBBI,
     64                                  const DebugLoc &DL) const;
     65 
     66   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
     67   /// the function.
     68   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
     69   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
     70 
     71   void adjustForSegmentedStacks(MachineFunction &MF,
     72                                 MachineBasicBlock &PrologueMBB) const override;
     73 
     74   void adjustForHiPEPrologue(MachineFunction &MF,
     75                              MachineBasicBlock &PrologueMBB) const override;
     76 
     77   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
     78                             RegScavenger *RS = nullptr) const override;
     79 
     80   bool
     81   assignCalleeSavedSpillSlots(MachineFunction &MF,
     82                               const TargetRegisterInfo *TRI,
     83                               std::vector<CalleeSavedInfo> &CSI) const override;
     84 
     85   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
     86                                  MachineBasicBlock::iterator MI,
     87                                  const std::vector<CalleeSavedInfo> &CSI,
     88                                  const TargetRegisterInfo *TRI) const override;
     89 
     90   bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
     91                                   MachineBasicBlock::iterator MI,
     92                                   std::vector<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                              unsigned &FrameReg) const override;
    102 
    103   int getFrameIndexReferenceSP(const MachineFunction &MF,
    104                                int FI, unsigned &SPReg, int Adjustment) const;
    105   int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
    106                                      unsigned &FrameReg,
    107                                      bool IgnoreSPUpdates) const override;
    108 
    109   MachineBasicBlock::iterator
    110   eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
    111                                 MachineBasicBlock::iterator MI) const override;
    112 
    113   unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
    114 
    115   void processFunctionBeforeFrameFinalized(MachineFunction &MF,
    116                                            RegScavenger *RS) const override;
    117 
    118   /// Check the instruction before/after the passed instruction. If
    119   /// it is an ADD/SUB/LEA instruction it is deleted argument and the
    120   /// stack adjustment is returned as a positive value for ADD/LEA and
    121   /// a negative for SUB.
    122   int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
    123                      bool doMergeWithPrevious) const;
    124 
    125   /// Emit a series of instructions to increment / decrement the stack
    126   /// pointer by a constant value.
    127   void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
    128                     const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
    129 
    130   /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
    131   bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
    132 
    133   /// Check whether or not the given \p MBB can be used as a prologue
    134   /// for the target.
    135   /// The prologue will be inserted first in this basic block.
    136   /// This method is used by the shrink-wrapping pass to decide if
    137   /// \p MBB will be correctly handled by the target.
    138   /// As soon as the target enable shrink-wrapping without overriding
    139   /// this method, we assume that each basic block is a valid
    140   /// prologue.
    141   bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
    142 
    143   /// Check whether or not the given \p MBB can be used as a epilogue
    144   /// for the target.
    145   /// The epilogue will be inserted before the first terminator of that block.
    146   /// This method is used by the shrink-wrapping pass to decide if
    147   /// \p MBB will be correctly handled by the target.
    148   bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
    149 
    150   /// Returns true if the target will correctly handle shrink wrapping.
    151   bool enableShrinkWrapping(const MachineFunction &MF) const override;
    152 
    153   /// Order the symbols in the local stack.
    154   /// We want to place the local stack objects in some sort of sensible order.
    155   /// The heuristic we use is to try and pack them according to static number
    156   /// of uses and size in order to minimize code size.
    157   void orderFrameObjects(const MachineFunction &MF,
    158                          SmallVectorImpl<int> &ObjectsToAllocate) const override;
    159 
    160   /// Wraps up getting a CFI index and building a MachineInstr for it.
    161   void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
    162                 const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
    163 
    164   /// Sets up EBP and optionally ESI based on the incoming EBP value.  Only
    165   /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
    166   MachineBasicBlock::iterator
    167   restoreWin32EHStackPointers(MachineBasicBlock &MBB,
    168                               MachineBasicBlock::iterator MBBI,
    169                               const DebugLoc &DL, bool RestoreSP = false) const;
    170 
    171   int getInitialCFAOffset(const MachineFunction &MF) const override;
    172 
    173   unsigned getInitialCFARegister(const MachineFunction &MF) const override;
    174 
    175 private:
    176   uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
    177 
    178   /// Emit target stack probe as a call to a helper function
    179   void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
    180                           MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
    181                           bool InProlog) const;
    182 
    183   /// Emit target stack probe as an inline sequence.
    184   void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
    185                             MachineBasicBlock::iterator MBBI,
    186                             const DebugLoc &DL, bool InProlog) const;
    187 
    188   /// Emit a stub to later inline the target stack probe.
    189   void emitStackProbeInlineStub(MachineFunction &MF, MachineBasicBlock &MBB,
    190                                 MachineBasicBlock::iterator MBBI,
    191                                 const DebugLoc &DL, bool InProlog) const;
    192 
    193   /// Aligns the stack pointer by ANDing it with -MaxAlign.
    194   void BuildStackAlignAND(MachineBasicBlock &MBB,
    195                           MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
    196                           unsigned Reg, uint64_t MaxAlign) const;
    197 
    198   /// Make small positive stack adjustments using POPs.
    199   bool adjustStackWithPops(MachineBasicBlock &MBB,
    200                            MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
    201                            int Offset) const;
    202 
    203   /// Adjusts the stack pointer using LEA, SUB, or ADD.
    204   MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
    205                                            MachineBasicBlock::iterator MBBI,
    206                                            const DebugLoc &DL, int64_t Offset,
    207                                            bool InEpilogue) const;
    208 
    209   unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
    210 
    211   unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
    212 
    213   /// Materialize the catchret target MBB in RAX.
    214   void emitCatchRetReturnValue(MachineBasicBlock &MBB,
    215                                MachineBasicBlock::iterator MBBI,
    216                                MachineInstr *CatchRet) const;
    217 };
    218 
    219 } // End llvm namespace
    220 
    221 #endif
    222