TargetFrameLowering.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /// emitZeroCallUsedRegs - Zeros out call used registers.
  194. virtual void emitZeroCallUsedRegs(BitVector RegsToZero,
  195. MachineBasicBlock &MBB) const {}
  196. /// With basic block sections, emit callee saved frame moves for basic blocks
  197. /// that are in a different section.
  198. virtual void
  199. emitCalleeSavedFrameMovesFullCFA(MachineBasicBlock &MBB,
  200. MachineBasicBlock::iterator MBBI) const {}
  201. /// Returns true if we may need to fix the unwind information for the
  202. /// function.
  203. virtual bool enableCFIFixup(MachineFunction &MF) const;
  204. /// Emit CFI instructions that recreate the state of the unwind information
  205. /// upon fucntion entry.
  206. virtual void resetCFIToInitialState(MachineBasicBlock &MBB) const {}
  207. /// Replace a StackProbe stub (if any) with the actual probe code inline
  208. virtual void inlineStackProbe(MachineFunction &MF,
  209. MachineBasicBlock &PrologueMBB) const {}
  210. /// Does the stack probe function call return with a modified stack pointer?
  211. virtual bool stackProbeFunctionModifiesSP() const { return false; }
  212. /// Adjust the prologue to have the function use segmented stacks. This works
  213. /// by adding a check even before the "normal" function prologue.
  214. virtual void adjustForSegmentedStacks(MachineFunction &MF,
  215. MachineBasicBlock &PrologueMBB) const {}
  216. /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
  217. /// the assembly prologue to explicitly handle the stack.
  218. virtual void adjustForHiPEPrologue(MachineFunction &MF,
  219. MachineBasicBlock &PrologueMBB) const {}
  220. /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
  221. /// saved registers and returns true if it isn't possible / profitable to do
  222. /// so by issuing a series of store instructions via
  223. /// storeRegToStackSlot(). Returns false otherwise.
  224. virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
  225. MachineBasicBlock::iterator MI,
  226. ArrayRef<CalleeSavedInfo> CSI,
  227. const TargetRegisterInfo *TRI) const {
  228. return false;
  229. }
  230. /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
  231. /// saved registers and returns true if it isn't possible / profitable to do
  232. /// so by issuing a series of load instructions via loadRegToStackSlot().
  233. /// If it returns true, and any of the registers in CSI is not restored,
  234. /// it sets the corresponding Restored flag in CSI to false.
  235. /// Returns false otherwise.
  236. virtual bool
  237. restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
  238. MachineBasicBlock::iterator MI,
  239. MutableArrayRef<CalleeSavedInfo> CSI,
  240. const TargetRegisterInfo *TRI) const {
  241. return false;
  242. }
  243. /// Return true if the target wants to keep the frame pointer regardless of
  244. /// the function attribute "frame-pointer".
  245. virtual bool keepFramePointer(const MachineFunction &MF) const {
  246. return false;
  247. }
  248. /// hasFP - Return true if the specified function should have a dedicated
  249. /// frame pointer register. For most targets this is true only if the function
  250. /// has variable sized allocas or if frame pointer elimination is disabled.
  251. virtual bool hasFP(const MachineFunction &MF) const = 0;
  252. /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
  253. /// not required, we reserve argument space for call sites in the function
  254. /// immediately on entry to the current function. This eliminates the need for
  255. /// add/sub sp brackets around call sites. Returns true if the call frame is
  256. /// included as part of the stack frame.
  257. virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
  258. return !hasFP(MF);
  259. }
  260. /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
  261. /// call frame pseudo ops before doing frame index elimination. This is
  262. /// possible only when frame index references between the pseudos won't
  263. /// need adjusting for the call frame adjustments. Normally, that's true
  264. /// if the function has a reserved call frame or a frame pointer. Some
  265. /// targets (Thumb2, for example) may have more complicated criteria,
  266. /// however, and can override this behavior.
  267. virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
  268. return hasReservedCallFrame(MF) || hasFP(MF);
  269. }
  270. // needsFrameIndexResolution - Do we need to perform FI resolution for
  271. // this function. Normally, this is required only when the function
  272. // has any stack objects. However, targets may want to override this.
  273. virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
  274. /// getFrameIndexReference - This method should return the base register
  275. /// and offset used to reference a frame index location. The offset is
  276. /// returned directly, and the base register is returned via FrameReg.
  277. virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
  278. Register &FrameReg) const;
  279. /// Same as \c getFrameIndexReference, except that the stack pointer (as
  280. /// opposed to the frame pointer) will be the preferred value for \p
  281. /// FrameReg. This is generally used for emitting statepoint or EH tables that
  282. /// use offsets from RSP. If \p IgnoreSPUpdates is true, the returned
  283. /// offset is only guaranteed to be valid with respect to the value of SP at
  284. /// the end of the prologue.
  285. virtual StackOffset
  286. getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
  287. Register &FrameReg,
  288. bool IgnoreSPUpdates) const {
  289. // Always safe to dispatch to getFrameIndexReference.
  290. return getFrameIndexReference(MF, FI, FrameReg);
  291. }
  292. /// getNonLocalFrameIndexReference - This method returns the offset used to
  293. /// reference a frame index location. The offset can be from either FP/BP/SP
  294. /// based on which base register is returned by llvm.localaddress.
  295. virtual StackOffset getNonLocalFrameIndexReference(const MachineFunction &MF,
  296. int FI) const {
  297. // By default, dispatch to getFrameIndexReference. Interested targets can
  298. // override this.
  299. Register FrameReg;
  300. return getFrameIndexReference(MF, FI, FrameReg);
  301. }
  302. /// Returns the callee-saved registers as computed by determineCalleeSaves
  303. /// in the BitVector \p SavedRegs.
  304. virtual void getCalleeSaves(const MachineFunction &MF,
  305. BitVector &SavedRegs) const;
  306. /// This method determines which of the registers reported by
  307. /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
  308. /// The default implementation checks populates the \p SavedRegs bitset with
  309. /// all registers which are modified in the function, targets may override
  310. /// this function to save additional registers.
  311. /// This method also sets up the register scavenger ensuring there is a free
  312. /// register or a frameindex available.
  313. /// This method should not be called by any passes outside of PEI, because
  314. /// it may change state passed in by \p MF and \p RS. The preferred
  315. /// interface outside PEI is getCalleeSaves.
  316. virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
  317. RegScavenger *RS = nullptr) const;
  318. /// processFunctionBeforeFrameFinalized - This method is called immediately
  319. /// before the specified function's frame layout (MF.getFrameInfo()) is
  320. /// finalized. Once the frame is finalized, MO_FrameIndex operands are
  321. /// replaced with direct constants. This method is optional.
  322. ///
  323. virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
  324. RegScavenger *RS = nullptr) const {
  325. }
  326. /// processFunctionBeforeFrameIndicesReplaced - This method is called
  327. /// immediately before MO_FrameIndex operands are eliminated, but after the
  328. /// frame is finalized. This method is optional.
  329. virtual void
  330. processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF,
  331. RegScavenger *RS = nullptr) const {}
  332. virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const {
  333. report_fatal_error("WinEH not implemented for this target");
  334. }
  335. /// This method is called during prolog/epilog code insertion to eliminate
  336. /// call frame setup and destroy pseudo instructions (but only if the Target
  337. /// is using them). It is responsible for eliminating these instructions,
  338. /// replacing them with concrete instructions. This method need only be
  339. /// implemented if using call frame setup/destroy pseudo instructions.
  340. /// Returns an iterator pointing to the instruction after the replaced one.
  341. virtual MachineBasicBlock::iterator
  342. eliminateCallFramePseudoInstr(MachineFunction &MF,
  343. MachineBasicBlock &MBB,
  344. MachineBasicBlock::iterator MI) const {
  345. llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
  346. "target!");
  347. }
  348. /// Order the symbols in the local stack frame.
  349. /// The list of objects that we want to order is in \p objectsToAllocate as
  350. /// indices into the MachineFrameInfo. The array can be reordered in any way
  351. /// upon return. The contents of the array, however, may not be modified (i.e.
  352. /// only their order may be changed).
  353. /// By default, just maintain the original order.
  354. virtual void
  355. orderFrameObjects(const MachineFunction &MF,
  356. SmallVectorImpl<int> &objectsToAllocate) const {
  357. }
  358. /// Check whether or not the given \p MBB can be used as a prologue
  359. /// for the target.
  360. /// The prologue will be inserted first in this basic block.
  361. /// This method is used by the shrink-wrapping pass to decide if
  362. /// \p MBB will be correctly handled by the target.
  363. /// As soon as the target enable shrink-wrapping without overriding
  364. /// this method, we assume that each basic block is a valid
  365. /// prologue.
  366. virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
  367. return true;
  368. }
  369. /// Check whether or not the given \p MBB can be used as a epilogue
  370. /// for the target.
  371. /// The epilogue will be inserted before the first terminator of that block.
  372. /// This method is used by the shrink-wrapping pass to decide if
  373. /// \p MBB will be correctly handled by the target.
  374. /// As soon as the target enable shrink-wrapping without overriding
  375. /// this method, we assume that each basic block is a valid
  376. /// epilogue.
  377. virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
  378. return true;
  379. }
  380. /// Returns the StackID that scalable vectors should be associated with.
  381. virtual TargetStackID::Value getStackIDForScalableVectors() const {
  382. return TargetStackID::Default;
  383. }
  384. virtual bool isSupportedStackID(TargetStackID::Value ID) const {
  385. switch (ID) {
  386. default:
  387. return false;
  388. case TargetStackID::Default:
  389. case TargetStackID::NoAlloc:
  390. return true;
  391. }
  392. }
  393. /// Check if given function is safe for not having callee saved registers.
  394. /// This is used when interprocedural register allocation is enabled.
  395. static bool isSafeForNoCSROpt(const Function &F);
  396. /// Check if the no-CSR optimisation is profitable for the given function.
  397. virtual bool isProfitableForNoCSROpt(const Function &F) const {
  398. return true;
  399. }
  400. /// Return initial CFA offset value i.e. the one valid at the beginning of the
  401. /// function (before any stack operations).
  402. virtual int getInitialCFAOffset(const MachineFunction &MF) const;
  403. /// Return initial CFA register value i.e. the one valid at the beginning of
  404. /// the function (before any stack operations).
  405. virtual Register getInitialCFARegister(const MachineFunction &MF) const;
  406. /// Return the frame base information to be encoded in the DWARF subprogram
  407. /// debug info.
  408. virtual DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const;
  409. };
  410. } // End llvm namespace
  411. #endif
  412. #ifdef __GNUC__
  413. #pragma GCC diagnostic pop
  414. #endif