X86FrameLowering.h 12 KB

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