TargetFrameLowering.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //===-- llvm/Target/TargetFrameLowering.h ---------------------------*- 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. // Interface to describe the layout of a stack frame on the target machine.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TARGET_TARGETFRAMELOWERING_H
  14. #define LLVM_TARGET_TARGETFRAMELOWERING_H
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include <utility>
  17. #include <vector>
  18. namespace llvm {
  19. class BitVector;
  20. class CalleeSavedInfo;
  21. class MachineFunction;
  22. class RegScavenger;
  23. /// Information about stack frame layout on the target. It holds the direction
  24. /// of stack growth, the known stack alignment on entry to each function, and
  25. /// the offset to the locals area.
  26. ///
  27. /// The offset to the local area is the offset from the stack pointer on
  28. /// function entry to the first location where function data (local variables,
  29. /// spill locations) can be stored.
  30. class TargetFrameLowering {
  31. public:
  32. enum StackDirection {
  33. StackGrowsUp, // Adding to the stack increases the stack address
  34. StackGrowsDown // Adding to the stack decreases the stack address
  35. };
  36. // Maps a callee saved register to a stack slot with a fixed offset.
  37. struct SpillSlot {
  38. unsigned Reg;
  39. int Offset; // Offset relative to stack pointer on function entry.
  40. };
  41. private:
  42. StackDirection StackDir;
  43. unsigned StackAlignment;
  44. unsigned TransientStackAlignment;
  45. int LocalAreaOffset;
  46. bool StackRealignable;
  47. public:
  48. TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
  49. unsigned TransAl = 1, bool StackReal = true)
  50. : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
  51. LocalAreaOffset(LAO), StackRealignable(StackReal) {}
  52. virtual ~TargetFrameLowering();
  53. // These methods return information that describes the abstract stack layout
  54. // of the target machine.
  55. /// getStackGrowthDirection - Return the direction the stack grows
  56. ///
  57. StackDirection getStackGrowthDirection() const { return StackDir; }
  58. /// getStackAlignment - This method returns the number of bytes to which the
  59. /// stack pointer must be aligned on entry to a function. Typically, this
  60. /// is the largest alignment for any data object in the target.
  61. ///
  62. unsigned getStackAlignment() const { return StackAlignment; }
  63. /// getTransientStackAlignment - This method returns the number of bytes to
  64. /// which the stack pointer must be aligned at all times, even between
  65. /// calls.
  66. ///
  67. unsigned getTransientStackAlignment() const {
  68. return TransientStackAlignment;
  69. }
  70. /// isStackRealignable - This method returns whether the stack can be
  71. /// realigned.
  72. bool isStackRealignable() const {
  73. return StackRealignable;
  74. }
  75. /// getOffsetOfLocalArea - This method returns the offset of the local area
  76. /// from the stack pointer on entrance to a function.
  77. ///
  78. int getOffsetOfLocalArea() const { return LocalAreaOffset; }
  79. /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
  80. /// the incoming stack pointer, false if it is close to the post-prologue
  81. /// stack pointer.
  82. virtual bool isFPCloseToIncomingSP() const { return true; }
  83. /// assignCalleeSavedSpillSlots - Allows target to override spill slot
  84. /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
  85. /// assign frame slots to all CSI entries and return true. If this method
  86. /// returns false, spill slots will be assigned using generic implementation.
  87. /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
  88. /// CSI.
  89. virtual bool
  90. assignCalleeSavedSpillSlots(MachineFunction &MF,
  91. const TargetRegisterInfo *TRI,
  92. std::vector<CalleeSavedInfo> &CSI) const {
  93. return false;
  94. }
  95. /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
  96. /// pairs, that contains an entry for each callee saved register that must be
  97. /// spilled to a particular stack location if it is spilled.
  98. ///
  99. /// Each entry in this array contains a <register,offset> pair, indicating the
  100. /// fixed offset from the incoming stack pointer that each register should be
  101. /// spilled at. If a register is not listed here, the code generator is
  102. /// allowed to spill it anywhere it chooses.
  103. ///
  104. virtual const SpillSlot *
  105. getCalleeSavedSpillSlots(unsigned &NumEntries) const {
  106. NumEntries = 0;
  107. return nullptr;
  108. }
  109. /// targetHandlesStackFrameRounding - Returns true if the target is
  110. /// responsible for rounding up the stack frame (probably at emitPrologue
  111. /// time).
  112. virtual bool targetHandlesStackFrameRounding() const {
  113. return false;
  114. }
  115. /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
  116. /// the function.
  117. virtual void emitPrologue(MachineFunction &MF,
  118. MachineBasicBlock &MBB) const = 0;
  119. virtual void emitEpilogue(MachineFunction &MF,
  120. MachineBasicBlock &MBB) const = 0;
  121. /// Adjust the prologue to have the function use segmented stacks. This works
  122. /// by adding a check even before the "normal" function prologue.
  123. virtual void adjustForSegmentedStacks(MachineFunction &MF,
  124. MachineBasicBlock &PrologueMBB) const {}
  125. /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
  126. /// the assembly prologue to explicitly handle the stack.
  127. virtual void adjustForHiPEPrologue(MachineFunction &MF,
  128. MachineBasicBlock &PrologueMBB) const {}
  129. /// Adjust the prologue to add an allocation at a fixed offset from the frame
  130. /// pointer.
  131. virtual void
  132. adjustForFrameAllocatePrologue(MachineFunction &MF,
  133. MachineBasicBlock &PrologueMBB) const {}
  134. /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
  135. /// saved registers and returns true if it isn't possible / profitable to do
  136. /// so by issuing a series of store instructions via
  137. /// storeRegToStackSlot(). Returns false otherwise.
  138. virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
  139. MachineBasicBlock::iterator MI,
  140. const std::vector<CalleeSavedInfo> &CSI,
  141. const TargetRegisterInfo *TRI) const {
  142. return false;
  143. }
  144. /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
  145. /// saved registers and returns true if it isn't possible / profitable to do
  146. /// so by issuing a series of load instructions via loadRegToStackSlot().
  147. /// Returns false otherwise.
  148. virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
  149. MachineBasicBlock::iterator MI,
  150. const std::vector<CalleeSavedInfo> &CSI,
  151. const TargetRegisterInfo *TRI) const {
  152. return false;
  153. }
  154. /// Return true if the target needs to disable frame pointer elimination.
  155. virtual bool noFramePointerElim(const MachineFunction &MF) const;
  156. /// hasFP - Return true if the specified function should have a dedicated
  157. /// frame pointer register. For most targets this is true only if the function
  158. /// has variable sized allocas or if frame pointer elimination is disabled.
  159. virtual bool hasFP(const MachineFunction &MF) const = 0;
  160. /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
  161. /// not required, we reserve argument space for call sites in the function
  162. /// immediately on entry to the current function. This eliminates the need for
  163. /// add/sub sp brackets around call sites. Returns true if the call frame is
  164. /// included as part of the stack frame.
  165. virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
  166. return !hasFP(MF);
  167. }
  168. /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
  169. /// call frame pseudo ops before doing frame index elimination. This is
  170. /// possible only when frame index references between the pseudos won't
  171. /// need adjusting for the call frame adjustments. Normally, that's true
  172. /// if the function has a reserved call frame or a frame pointer. Some
  173. /// targets (Thumb2, for example) may have more complicated criteria,
  174. /// however, and can override this behavior.
  175. virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
  176. return hasReservedCallFrame(MF) || hasFP(MF);
  177. }
  178. // needsFrameIndexResolution - Do we need to perform FI resolution for
  179. // this function. Normally, this is required only when the function
  180. // has any stack objects. However, targets may want to override this.
  181. virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
  182. /// getFrameIndexOffset - Returns the displacement from the frame register to
  183. /// the stack frame of the specified index.
  184. virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
  185. /// getFrameIndexReference - This method should return the base register
  186. /// and offset used to reference a frame index location. The offset is
  187. /// returned directly, and the base register is returned via FrameReg.
  188. virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
  189. unsigned &FrameReg) const;
  190. /// Same as above, except that the 'base register' will always be RSP, not
  191. /// RBP on x86. This is used exclusively for lowering STATEPOINT nodes.
  192. /// TODO: This should really be a parameterizable choice.
  193. virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
  194. unsigned &FrameReg) const {
  195. // default to calling normal version, we override this on x86 only
  196. llvm_unreachable("unimplemented for non-x86");
  197. return 0;
  198. }
  199. /// This method determines which of the registers reported by
  200. /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
  201. /// The default implementation checks populates the \p SavedRegs bitset with
  202. /// all registers which are modified in the function, targets may override
  203. /// this function to save additional registers.
  204. /// This method also sets up the register scavenger ensuring there is a free
  205. /// register or a frameindex available.
  206. virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
  207. RegScavenger *RS = nullptr) const;
  208. /// processFunctionBeforeFrameFinalized - This method is called immediately
  209. /// before the specified function's frame layout (MF.getFrameInfo()) is
  210. /// finalized. Once the frame is finalized, MO_FrameIndex operands are
  211. /// replaced with direct constants. This method is optional.
  212. ///
  213. virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
  214. RegScavenger *RS = nullptr) const {
  215. }
  216. /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
  217. /// code insertion to eliminate call frame setup and destroy pseudo
  218. /// instructions (but only if the Target is using them). It is responsible
  219. /// for eliminating these instructions, replacing them with concrete
  220. /// instructions. This method need only be implemented if using call frame
  221. /// setup/destroy pseudo instructions.
  222. ///
  223. virtual void
  224. eliminateCallFramePseudoInstr(MachineFunction &MF,
  225. MachineBasicBlock &MBB,
  226. MachineBasicBlock::iterator MI) const {
  227. llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
  228. "target!");
  229. }
  230. /// Check whether or not the given \p MBB can be used as a prologue
  231. /// for the target.
  232. /// The prologue will be inserted first in this basic block.
  233. /// This method is used by the shrink-wrapping pass to decide if
  234. /// \p MBB will be correctly handled by the target.
  235. /// As soon as the target enable shrink-wrapping without overriding
  236. /// this method, we assume that each basic block is a valid
  237. /// prologue.
  238. virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
  239. return true;
  240. }
  241. /// Check whether or not the given \p MBB can be used as a epilogue
  242. /// for the target.
  243. /// The epilogue will be inserted before the first terminator of that block.
  244. /// This method is used by the shrink-wrapping pass to decide if
  245. /// \p MBB will be correctly handled by the target.
  246. /// As soon as the target enable shrink-wrapping without overriding
  247. /// this method, we assume that each basic block is a valid
  248. /// epilogue.
  249. virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
  250. return true;
  251. }
  252. };
  253. } // End llvm namespace
  254. #endif