X86FrameLowering.h 12 KB

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