TargetFrameLowering.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //===-- llvm/CodeGen/TargetFrameLowering.h ----------------------*- 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. // Interface to describe the layout of a stack frame on the target machine.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CODEGEN_TARGETFRAMELOWERING_H
  13. #define LLVM_CODEGEN_TARGETFRAMELOWERING_H
  14. #include "llvm/CodeGen/MachineBasicBlock.h"
  15. #include "llvm/Support/TypeSize.h"
  16. #include <vector>
  17. namespace llvm {
  18. class BitVector;
  19. class CalleeSavedInfo;
  20. class MachineFunction;
  21. class RegScavenger;
  22. namespace TargetStackID {
  23. enum Value {
  24. Default = 0,
  25. SGPRSpill = 1,
  26. ScalableVector = 2,
  27. NoAlloc = 255
  28. };
  29. }
  30. /// Information about stack frame layout on the target. It holds the direction
  31. /// of stack growth, the known stack alignment on entry to each function, and
  32. /// the offset to the locals area.
  33. ///
  34. /// The offset to the local area is the offset from the stack pointer on
  35. /// function entry to the first location where function data (local variables,
  36. /// spill locations) can be stored.
  37. class TargetFrameLowering {
  38. public:
  39. enum StackDirection {
  40. StackGrowsUp, // Adding to the stack increases the stack address
  41. StackGrowsDown // Adding to the stack decreases the stack address
  42. };
  43. // Maps a callee saved register to a stack slot with a fixed offset.
  44. struct SpillSlot {
  45. unsigned Reg;
  46. int Offset; // Offset relative to stack pointer on function entry.
  47. };
  48. struct DwarfFrameBase {
  49. // The frame base may be either a register (the default), the CFA,
  50. // or a WebAssembly-specific location description.
  51. enum FrameBaseKind { Register, CFA, WasmFrameBase } Kind;
  52. struct WasmFrameBase {
  53. unsigned Kind; // Wasm local, global, or value stack
  54. unsigned Index;
  55. };
  56. union {
  57. unsigned Reg;
  58. struct WasmFrameBase WasmLoc;
  59. } Location;
  60. };
  61. private:
  62. StackDirection StackDir;
  63. Align StackAlignment;
  64. Align TransientStackAlignment;
  65. int LocalAreaOffset;
  66. bool StackRealignable;
  67. public:
  68. TargetFrameLowering(StackDirection D, Align StackAl, int LAO,
  69. Align TransAl = Align(1), bool StackReal = true)
  70. : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
  71. LocalAreaOffset(LAO), StackRealignable(StackReal) {}
  72. virtual ~TargetFrameLowering();
  73. // These methods return information that describes the abstract stack layout
  74. // of the target machine.
  75. /// getStackGrowthDirection - Return the direction the stack grows
  76. ///
  77. StackDirection getStackGrowthDirection() const { return StackDir; }
  78. /// getStackAlignment - This method returns the number of bytes to which the
  79. /// stack pointer must be aligned on entry to a function. Typically, this
  80. /// is the largest alignment for any data object in the target.
  81. ///
  82. unsigned getStackAlignment() const { return StackAlignment.value(); }
  83. /// getStackAlignment - This method returns the number of bytes to which the
  84. /// stack pointer must be aligned on entry to a function. Typically, this
  85. /// is the largest alignment for any data object in the target.
  86. ///
  87. Align getStackAlign() const { return StackAlignment; }
  88. /// alignSPAdjust - This method aligns the stack adjustment to the correct
  89. /// alignment.
  90. ///
  91. int alignSPAdjust(int SPAdj) const {
  92. if (SPAdj < 0) {
  93. SPAdj = -alignTo(-SPAdj, StackAlignment);
  94. } else {
  95. SPAdj = alignTo(SPAdj, StackAlignment);
  96. }
  97. return SPAdj;
  98. }
  99. /// getTransientStackAlignment - This method returns the number of bytes to
  100. /// which the stack pointer must be aligned at all times, even between
  101. /// calls.
  102. ///
  103. Align getTransientStackAlign() const { return TransientStackAlignment; }
  104. /// isStackRealignable - This method returns whether the stack can be
  105. /// realigned.
  106. bool isStackRealignable() const {
  107. return StackRealignable;
  108. }
  109. /// Return the skew that has to be applied to stack alignment under
  110. /// certain conditions (e.g. stack was adjusted before function \p MF
  111. /// was called).
  112. virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const;
  113. /// This method returns whether or not it is safe for an object with the
  114. /// given stack id to be bundled into the local area.
  115. virtual bool isStackIdSafeForLocalArea(unsigned StackId) const {
  116. return true;
  117. }
  118. /// getOffsetOfLocalArea - This method returns the offset of the local area
  119. /// from the stack pointer on entrance to a function.
  120. ///
  121. int getOffsetOfLocalArea() const { return LocalAreaOffset; }
  122. /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
  123. /// the incoming stack pointer, false if it is close to the post-prologue
  124. /// stack pointer.
  125. virtual bool isFPCloseToIncomingSP() const { return true; }
  126. /// assignCalleeSavedSpillSlots - Allows target to override spill slot
  127. /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
  128. /// assign frame slots to all CSI entries and return true. If this method
  129. /// returns false, spill slots will be assigned using generic implementation.
  130. /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
  131. /// CSI.
  132. virtual bool assignCalleeSavedSpillSlots(MachineFunction &MF,
  133. const TargetRegisterInfo *TRI,
  134. std::vector<CalleeSavedInfo> &CSI,
  135. unsigned &MinCSFrameIndex,
  136. unsigned &MaxCSFrameIndex) const {
  137. return assignCalleeSavedSpillSlots(MF, TRI, CSI);
  138. }
  139. virtual bool
  140. assignCalleeSavedSpillSlots(MachineFunction &MF,
  141. const TargetRegisterInfo *TRI,
  142. std::vector<CalleeSavedInfo> &CSI) const {
  143. return false;
  144. }
  145. /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
  146. /// pairs, that contains an entry for each callee saved register that must be
  147. /// spilled to a particular stack location if it is spilled.
  148. ///
  149. /// Each entry in this array contains a <register,offset> pair, indicating the
  150. /// fixed offset from the incoming stack pointer that each register should be
  151. /// spilled at. If a register is not listed here, the code generator is
  152. /// allowed to spill it anywhere it chooses.
  153. ///
  154. virtual const SpillSlot *
  155. getCalleeSavedSpillSlots(unsigned &NumEntries) const {
  156. NumEntries = 0;
  157. return nullptr;
  158. }
  159. /// targetHandlesStackFrameRounding - Returns true if the target is
  160. /// responsible for rounding up the stack frame (probably at emitPrologue
  161. /// time).
  162. virtual bool targetHandlesStackFrameRounding() const {
  163. return false;
  164. }
  165. /// Returns true if the target will correctly handle shrink wrapping.
  166. virtual bool enableShrinkWrapping(const MachineFunction &MF) const {
  167. return false;
  168. }
  169. /// Returns true if the stack slot holes in the fixed and callee-save stack
  170. /// area should be used when allocating other stack locations to reduce stack
  171. /// size.
  172. virtual bool enableStackSlotScavenging(const MachineFunction &MF) const {
  173. return false;
  174. }
  175. /// Returns true if the target can safely skip saving callee-saved registers
  176. /// for noreturn nounwind functions.
  177. virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const;
  178. /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
  179. /// the function.
  180. virtual void emitPrologue(MachineFunction &MF,
  181. MachineBasicBlock &MBB) const = 0;
  182. virtual void emitEpilogue(MachineFunction &MF,
  183. MachineBasicBlock &MBB) const = 0;
  184. /// With basic block sections, emit callee saved frame moves for basic blocks
  185. /// that are in a different section.
  186. virtual void
  187. emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
  188. MachineBasicBlock::iterator MBBI) const {}
  189. virtual void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
  190. MachineBasicBlock::iterator MBBI,
  191. const DebugLoc &DL,
  192. bool IsPrologue) const {}
  193. /// Replace a StackProbe stub (if any) with the actual probe code inline
  194. virtual void inlineStackProbe(MachineFunction &MF,
  195. MachineBasicBlock &PrologueMBB) const {}
  196. /// Adjust the prologue to have the function use segmented stacks. This works
  197. /// by adding a check even before the "normal" function prologue.
  198. virtual void adjustForSegmentedStacks(MachineFunction &MF,
  199. MachineBasicBlock &PrologueMBB) const {}
  200. /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
  201. /// the assembly prologue to explicitly handle the stack.
  202. virtual void adjustForHiPEPrologue(MachineFunction &MF,
  203. MachineBasicBlock &PrologueMBB) const {}
  204. /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
  205. /// saved registers and returns true if it isn't possible / profitable to do
  206. /// so by issuing a series of store instructions via
  207. /// storeRegToStackSlot(). Returns false otherwise.
  208. virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
  209. MachineBasicBlock::iterator MI,
  210. ArrayRef<CalleeSavedInfo> CSI,
  211. const TargetRegisterInfo *TRI) const {
  212. return false;
  213. }
  214. /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
  215. /// saved registers and returns true if it isn't possible / profitable to do
  216. /// so by issuing a series of load instructions via loadRegToStackSlot().
  217. /// If it returns true, and any of the registers in CSI is not restored,
  218. /// it sets the corresponding Restored flag in CSI to false.
  219. /// Returns false otherwise.
  220. virtual bool
  221. restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
  222. MachineBasicBlock::iterator MI,
  223. MutableArrayRef<CalleeSavedInfo> CSI,
  224. const TargetRegisterInfo *TRI) const {
  225. return false;
  226. }
  227. /// Return true if the target wants to keep the frame pointer regardless of
  228. /// the function attribute "frame-pointer".
  229. virtual bool keepFramePointer(const MachineFunction &MF) const {
  230. return false;
  231. }
  232. /// hasFP - Return true if the specified function should have a dedicated
  233. /// frame pointer register. For most targets this is true only if the function
  234. /// has variable sized allocas or if frame pointer elimination is disabled.
  235. virtual bool hasFP(const MachineFunction &MF) const = 0;
  236. /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
  237. /// not required, we reserve argument space for call sites in the function
  238. /// immediately on entry to the current function. This eliminates the need for
  239. /// add/sub sp brackets around call sites. Returns true if the call frame is
  240. /// included as part of the stack frame.
  241. virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
  242. return !hasFP(MF);
  243. }
  244. /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
  245. /// call frame pseudo ops before doing frame index elimination. This is
  246. /// possible only when frame index references between the pseudos won't
  247. /// need adjusting for the call frame adjustments. Normally, that's true
  248. /// if the function has a reserved call frame or a frame pointer. Some
  249. /// targets (Thumb2, for example) may have more complicated criteria,
  250. /// however, and can override this behavior.
  251. virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
  252. return hasReservedCallFrame(MF) || hasFP(MF);
  253. }
  254. // needsFrameIndexResolution - Do we need to perform FI resolution for
  255. // this function. Normally, this is required only when the function
  256. // has any stack objects. However, targets may want to override this.
  257. virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
  258. /// getFrameIndexReference - This method should return the base register
  259. /// and offset used to reference a frame index location. The offset is
  260. /// returned directly, and the base register is returned via FrameReg.
  261. virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
  262. Register &FrameReg) const;
  263. /// Same as \c getFrameIndexReference, except that the stack pointer (as
  264. /// opposed to the frame pointer) will be the preferred value for \p
  265. /// FrameReg. This is generally used for emitting statepoint or EH tables that
  266. /// use offsets from RSP. If \p IgnoreSPUpdates is true, the returned
  267. /// offset is only guaranteed to be valid with respect to the value of SP at
  268. /// the end of the prologue.
  269. virtual StackOffset
  270. getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
  271. Register &FrameReg,
  272. bool IgnoreSPUpdates) const {
  273. // Always safe to dispatch to getFrameIndexReference.
  274. return getFrameIndexReference(MF, FI, FrameReg);
  275. }
  276. /// getNonLocalFrameIndexReference - This method returns the offset used to
  277. /// reference a frame index location. The offset can be from either FP/BP/SP
  278. /// based on which base register is returned by llvm.localaddress.
  279. virtual StackOffset getNonLocalFrameIndexReference(const MachineFunction &MF,
  280. int FI) const {
  281. // By default, dispatch to getFrameIndexReference. Interested targets can
  282. // override this.
  283. Register FrameReg;
  284. return getFrameIndexReference(MF, FI, FrameReg);
  285. }
  286. /// Returns the callee-saved registers as computed by determineCalleeSaves
  287. /// in the BitVector \p SavedRegs.
  288. virtual void getCalleeSaves(const MachineFunction &MF,
  289. BitVector &SavedRegs) const;
  290. /// This method determines which of the registers reported by
  291. /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
  292. /// The default implementation checks populates the \p SavedRegs bitset with
  293. /// all registers which are modified in the function, targets may override
  294. /// this function to save additional registers.
  295. /// This method also sets up the register scavenger ensuring there is a free
  296. /// register or a frameindex available.
  297. /// This method should not be called by any passes outside of PEI, because
  298. /// it may change state passed in by \p MF and \p RS. The preferred
  299. /// interface outside PEI is getCalleeSaves.
  300. virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
  301. RegScavenger *RS = nullptr) const;
  302. /// processFunctionBeforeFrameFinalized - This method is called immediately
  303. /// before the specified function's frame layout (MF.getFrameInfo()) is
  304. /// finalized. Once the frame is finalized, MO_FrameIndex operands are
  305. /// replaced with direct constants. This method is optional.
  306. ///
  307. virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
  308. RegScavenger *RS = nullptr) const {
  309. }
  310. /// processFunctionBeforeFrameIndicesReplaced - This method is called
  311. /// immediately before MO_FrameIndex operands are eliminated, but after the
  312. /// frame is finalized. This method is optional.
  313. virtual void
  314. processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF,
  315. RegScavenger *RS = nullptr) const {}
  316. virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const {
  317. report_fatal_error("WinEH not implemented for this target");
  318. }
  319. /// This method is called during prolog/epilog code insertion to eliminate
  320. /// call frame setup and destroy pseudo instructions (but only if the Target
  321. /// is using them). It is responsible for eliminating these instructions,
  322. /// replacing them with concrete instructions. This method need only be
  323. /// implemented if using call frame setup/destroy pseudo instructions.
  324. /// Returns an iterator pointing to the instruction after the replaced one.
  325. virtual MachineBasicBlock::iterator
  326. eliminateCallFramePseudoInstr(MachineFunction &MF,
  327. MachineBasicBlock &MBB,
  328. MachineBasicBlock::iterator MI) const {
  329. llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
  330. "target!");
  331. }
  332. /// Order the symbols in the local stack frame.
  333. /// The list of objects that we want to order is in \p objectsToAllocate as
  334. /// indices into the MachineFrameInfo. The array can be reordered in any way
  335. /// upon return. The contents of the array, however, may not be modified (i.e.
  336. /// only their order may be changed).
  337. /// By default, just maintain the original order.
  338. virtual void
  339. orderFrameObjects(const MachineFunction &MF,
  340. SmallVectorImpl<int> &objectsToAllocate) const {
  341. }
  342. /// Check whether or not the given \p MBB can be used as a prologue
  343. /// for the target.
  344. /// The prologue will be inserted first in this basic block.
  345. /// This method is used by the shrink-wrapping pass to decide if
  346. /// \p MBB will be correctly handled by the target.
  347. /// As soon as the target enable shrink-wrapping without overriding
  348. /// this method, we assume that each basic block is a valid
  349. /// prologue.
  350. virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
  351. return true;
  352. }
  353. /// Check whether or not the given \p MBB can be used as a epilogue
  354. /// for the target.
  355. /// The epilogue will be inserted before the first terminator of that block.
  356. /// This method is used by the shrink-wrapping pass to decide if
  357. /// \p MBB will be correctly handled by the target.
  358. /// As soon as the target enable shrink-wrapping without overriding
  359. /// this method, we assume that each basic block is a valid
  360. /// epilogue.
  361. virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
  362. return true;
  363. }
  364. /// Returns the StackID that scalable vectors should be associated with.
  365. virtual TargetStackID::Value getStackIDForScalableVectors() const {
  366. return TargetStackID::Default;
  367. }
  368. virtual bool isSupportedStackID(TargetStackID::Value ID) const {
  369. switch (ID) {
  370. default:
  371. return false;
  372. case TargetStackID::Default:
  373. case TargetStackID::NoAlloc:
  374. return true;
  375. }
  376. }
  377. /// Check if given function is safe for not having callee saved registers.
  378. /// This is used when interprocedural register allocation is enabled.
  379. static bool isSafeForNoCSROpt(const Function &F);
  380. /// Check if the no-CSR optimisation is profitable for the given function.
  381. virtual bool isProfitableForNoCSROpt(const Function &F) const {
  382. return true;
  383. }
  384. /// Return initial CFA offset value i.e. the one valid at the beginning of the
  385. /// function (before any stack operations).
  386. virtual int getInitialCFAOffset(const MachineFunction &MF) const;
  387. /// Return initial CFA register value i.e. the one valid at the beginning of
  388. /// the function (before any stack operations).
  389. virtual Register getInitialCFARegister(const MachineFunction &MF) const;
  390. /// Return the frame base information to be encoded in the DWARF subprogram
  391. /// debug info.
  392. virtual DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const;
  393. };
  394. } // End llvm namespace
  395. #endif