TargetFrameLowering.h 20 KB

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