MachineFrameInfo.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- 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. // The file defines the MachineFrameInfo class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
  18. #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/CodeGen/Register.h"
  21. #include "llvm/Support/Alignment.h"
  22. #include "llvm/Support/DataTypes.h"
  23. #include <cassert>
  24. #include <vector>
  25. namespace llvm {
  26. class raw_ostream;
  27. class MachineFunction;
  28. class MachineBasicBlock;
  29. class BitVector;
  30. class AllocaInst;
  31. /// The CalleeSavedInfo class tracks the information need to locate where a
  32. /// callee saved register is in the current frame.
  33. /// Callee saved reg can also be saved to a different register rather than
  34. /// on the stack by setting DstReg instead of FrameIdx.
  35. class CalleeSavedInfo {
  36. Register Reg;
  37. union {
  38. int FrameIdx;
  39. unsigned DstReg;
  40. };
  41. /// Flag indicating whether the register is actually restored in the epilog.
  42. /// In most cases, if a register is saved, it is also restored. There are
  43. /// some situations, though, when this is not the case. For example, the
  44. /// LR register on ARM is usually saved, but on exit from the function its
  45. /// saved value may be loaded directly into PC. Since liveness tracking of
  46. /// physical registers treats callee-saved registers are live outside of
  47. /// the function, LR would be treated as live-on-exit, even though in these
  48. /// scenarios it is not. This flag is added to indicate that the saved
  49. /// register described by this object is not restored in the epilog.
  50. /// The long-term solution is to model the liveness of callee-saved registers
  51. /// by implicit uses on the return instructions, however, the required
  52. /// changes in the ARM backend would be quite extensive.
  53. bool Restored = true;
  54. /// Flag indicating whether the register is spilled to stack or another
  55. /// register.
  56. bool SpilledToReg = false;
  57. public:
  58. explicit CalleeSavedInfo(unsigned R, int FI = 0) : Reg(R), FrameIdx(FI) {}
  59. // Accessors.
  60. Register getReg() const { return Reg; }
  61. int getFrameIdx() const { return FrameIdx; }
  62. unsigned getDstReg() const { return DstReg; }
  63. void setFrameIdx(int FI) {
  64. FrameIdx = FI;
  65. SpilledToReg = false;
  66. }
  67. void setDstReg(Register SpillReg) {
  68. DstReg = SpillReg;
  69. SpilledToReg = true;
  70. }
  71. bool isRestored() const { return Restored; }
  72. void setRestored(bool R) { Restored = R; }
  73. bool isSpilledToReg() const { return SpilledToReg; }
  74. };
  75. /// The MachineFrameInfo class represents an abstract stack frame until
  76. /// prolog/epilog code is inserted. This class is key to allowing stack frame
  77. /// representation optimizations, such as frame pointer elimination. It also
  78. /// allows more mundane (but still important) optimizations, such as reordering
  79. /// of abstract objects on the stack frame.
  80. ///
  81. /// To support this, the class assigns unique integer identifiers to stack
  82. /// objects requested clients. These identifiers are negative integers for
  83. /// fixed stack objects (such as arguments passed on the stack) or nonnegative
  84. /// for objects that may be reordered. Instructions which refer to stack
  85. /// objects use a special MO_FrameIndex operand to represent these frame
  86. /// indexes.
  87. ///
  88. /// Because this class keeps track of all references to the stack frame, it
  89. /// knows when a variable sized object is allocated on the stack. This is the
  90. /// sole condition which prevents frame pointer elimination, which is an
  91. /// important optimization on register-poor architectures. Because original
  92. /// variable sized alloca's in the source program are the only source of
  93. /// variable sized stack objects, it is safe to decide whether there will be
  94. /// any variable sized objects before all stack objects are known (for
  95. /// example, register allocator spill code never needs variable sized
  96. /// objects).
  97. ///
  98. /// When prolog/epilog code emission is performed, the final stack frame is
  99. /// built and the machine instructions are modified to refer to the actual
  100. /// stack offsets of the object, eliminating all MO_FrameIndex operands from
  101. /// the program.
  102. ///
  103. /// Abstract Stack Frame Information
  104. class MachineFrameInfo {
  105. public:
  106. /// Stack Smashing Protection (SSP) rules require that vulnerable stack
  107. /// allocations are located close the stack protector.
  108. enum SSPLayoutKind {
  109. SSPLK_None, ///< Did not trigger a stack protector. No effect on data
  110. ///< layout.
  111. SSPLK_LargeArray, ///< Array or nested array >= SSP-buffer-size. Closest
  112. ///< to the stack protector.
  113. SSPLK_SmallArray, ///< Array or nested array < SSP-buffer-size. 2nd closest
  114. ///< to the stack protector.
  115. SSPLK_AddrOf ///< The address of this allocation is exposed and
  116. ///< triggered protection. 3rd closest to the protector.
  117. };
  118. private:
  119. // Represent a single object allocated on the stack.
  120. struct StackObject {
  121. // The offset of this object from the stack pointer on entry to
  122. // the function. This field has no meaning for a variable sized element.
  123. int64_t SPOffset;
  124. // The size of this object on the stack. 0 means a variable sized object,
  125. // ~0ULL means a dead object.
  126. uint64_t Size;
  127. // The required alignment of this stack slot.
  128. Align Alignment;
  129. // If true, the value of the stack object is set before
  130. // entering the function and is not modified inside the function. By
  131. // default, fixed objects are immutable unless marked otherwise.
  132. bool isImmutable;
  133. // If true the stack object is used as spill slot. It
  134. // cannot alias any other memory objects.
  135. bool isSpillSlot;
  136. /// If true, this stack slot is used to spill a value (could be deopt
  137. /// and/or GC related) over a statepoint. We know that the address of the
  138. /// slot can't alias any LLVM IR value. This is very similar to a Spill
  139. /// Slot, but is created by statepoint lowering is SelectionDAG, not the
  140. /// register allocator.
  141. bool isStatepointSpillSlot = false;
  142. /// Identifier for stack memory type analagous to address space. If this is
  143. /// non-0, the meaning is target defined. Offsets cannot be directly
  144. /// compared between objects with different stack IDs. The object may not
  145. /// necessarily reside in the same contiguous memory block as other stack
  146. /// objects. Objects with differing stack IDs should not be merged or
  147. /// replaced substituted for each other.
  148. //
  149. /// It is assumed a target uses consecutive, increasing stack IDs starting
  150. /// from 1.
  151. uint8_t StackID;
  152. /// If this stack object is originated from an Alloca instruction
  153. /// this value saves the original IR allocation. Can be NULL.
  154. const AllocaInst *Alloca;
  155. // If true, the object was mapped into the local frame
  156. // block and doesn't need additional handling for allocation beyond that.
  157. bool PreAllocated = false;
  158. // If true, an LLVM IR value might point to this object.
  159. // Normally, spill slots and fixed-offset objects don't alias IR-accessible
  160. // objects, but there are exceptions (on PowerPC, for example, some byval
  161. // arguments have ABI-prescribed offsets).
  162. bool isAliased;
  163. /// If true, the object has been zero-extended.
  164. bool isZExt = false;
  165. /// If true, the object has been sign-extended.
  166. bool isSExt = false;
  167. uint8_t SSPLayout = SSPLK_None;
  168. StackObject(uint64_t Size, Align Alignment, int64_t SPOffset,
  169. bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
  170. bool IsAliased, uint8_t StackID = 0)
  171. : SPOffset(SPOffset), Size(Size), Alignment(Alignment),
  172. isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID),
  173. Alloca(Alloca), isAliased(IsAliased) {}
  174. };
  175. /// The alignment of the stack.
  176. Align StackAlignment;
  177. /// Can the stack be realigned. This can be false if the target does not
  178. /// support stack realignment, or if the user asks us not to realign the
  179. /// stack. In this situation, overaligned allocas are all treated as dynamic
  180. /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC
  181. /// lowering. All non-alloca stack objects have their alignment clamped to the
  182. /// base ABI stack alignment.
  183. /// FIXME: There is room for improvement in this case, in terms of
  184. /// grouping overaligned allocas into a "secondary stack frame" and
  185. /// then only use a single alloca to allocate this frame and only a
  186. /// single virtual register to access it. Currently, without such an
  187. /// optimization, each such alloca gets its own dynamic realignment.
  188. bool StackRealignable;
  189. /// Whether the function has the \c alignstack attribute.
  190. bool ForcedRealign;
  191. /// The list of stack objects allocated.
  192. std::vector<StackObject> Objects;
  193. /// This contains the number of fixed objects contained on
  194. /// the stack. Because fixed objects are stored at a negative index in the
  195. /// Objects list, this is also the index to the 0th object in the list.
  196. unsigned NumFixedObjects = 0;
  197. /// This boolean keeps track of whether any variable
  198. /// sized objects have been allocated yet.
  199. bool HasVarSizedObjects = false;
  200. /// This boolean keeps track of whether there is a call
  201. /// to builtin \@llvm.frameaddress.
  202. bool FrameAddressTaken = false;
  203. /// This boolean keeps track of whether there is a call
  204. /// to builtin \@llvm.returnaddress.
  205. bool ReturnAddressTaken = false;
  206. /// This boolean keeps track of whether there is a call
  207. /// to builtin \@llvm.experimental.stackmap.
  208. bool HasStackMap = false;
  209. /// This boolean keeps track of whether there is a call
  210. /// to builtin \@llvm.experimental.patchpoint.
  211. bool HasPatchPoint = false;
  212. /// The prolog/epilog code inserter calculates the final stack
  213. /// offsets for all of the fixed size objects, updating the Objects list
  214. /// above. It then updates StackSize to contain the number of bytes that need
  215. /// to be allocated on entry to the function.
  216. uint64_t StackSize = 0;
  217. /// The amount that a frame offset needs to be adjusted to
  218. /// have the actual offset from the stack/frame pointer. The exact usage of
  219. /// this is target-dependent, but it is typically used to adjust between
  220. /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
  221. /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
  222. /// to the distance between the initial SP and the value in FP. For many
  223. /// targets, this value is only used when generating debug info (via
  224. /// TargetRegisterInfo::getFrameIndexReference); when generating code, the
  225. /// corresponding adjustments are performed directly.
  226. int OffsetAdjustment = 0;
  227. /// The prolog/epilog code inserter may process objects that require greater
  228. /// alignment than the default alignment the target provides.
  229. /// To handle this, MaxAlignment is set to the maximum alignment
  230. /// needed by the objects on the current frame. If this is greater than the
  231. /// native alignment maintained by the compiler, dynamic alignment code will
  232. /// be needed.
  233. ///
  234. Align MaxAlignment;
  235. /// Set to true if this function adjusts the stack -- e.g.,
  236. /// when calling another function. This is only valid during and after
  237. /// prolog/epilog code insertion.
  238. bool AdjustsStack = false;
  239. /// Set to true if this function has any function calls.
  240. bool HasCalls = false;
  241. /// The frame index for the stack protector.
  242. int StackProtectorIdx = -1;
  243. /// The frame index for the function context. Used for SjLj exceptions.
  244. int FunctionContextIdx = -1;
  245. /// This contains the size of the largest call frame if the target uses frame
  246. /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
  247. /// class). This information is important for frame pointer elimination.
  248. /// It is only valid during and after prolog/epilog code insertion.
  249. unsigned MaxCallFrameSize = ~0u;
  250. /// The number of bytes of callee saved registers that the target wants to
  251. /// report for the current function in the CodeView S_FRAMEPROC record.
  252. unsigned CVBytesOfCalleeSavedRegisters = 0;
  253. /// The prolog/epilog code inserter fills in this vector with each
  254. /// callee saved register saved in either the frame or a different
  255. /// register. Beyond its use by the prolog/ epilog code inserter,
  256. /// this data is used for debug info and exception handling.
  257. std::vector<CalleeSavedInfo> CSInfo;
  258. /// Has CSInfo been set yet?
  259. bool CSIValid = false;
  260. /// References to frame indices which are mapped
  261. /// into the local frame allocation block. <FrameIdx, LocalOffset>
  262. SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
  263. /// Size of the pre-allocated local frame block.
  264. int64_t LocalFrameSize = 0;
  265. /// Required alignment of the local object blob, which is the strictest
  266. /// alignment of any object in it.
  267. Align LocalFrameMaxAlign;
  268. /// Whether the local object blob needs to be allocated together. If not,
  269. /// PEI should ignore the isPreAllocated flags on the stack objects and
  270. /// just allocate them normally.
  271. bool UseLocalStackAllocationBlock = false;
  272. /// True if the function dynamically adjusts the stack pointer through some
  273. /// opaque mechanism like inline assembly or Win32 EH.
  274. bool HasOpaqueSPAdjustment = false;
  275. /// True if the function contains operations which will lower down to
  276. /// instructions which manipulate the stack pointer.
  277. bool HasCopyImplyingStackAdjustment = false;
  278. /// True if the function contains a call to the llvm.vastart intrinsic.
  279. bool HasVAStart = false;
  280. /// True if this is a varargs function that contains a musttail call.
  281. bool HasMustTailInVarArgFunc = false;
  282. /// True if this function contains a tail call. If so immutable objects like
  283. /// function arguments are no longer so. A tail call *can* override fixed
  284. /// stack objects like arguments so we can't treat them as immutable.
  285. bool HasTailCall = false;
  286. /// Not null, if shrink-wrapping found a better place for the prologue.
  287. MachineBasicBlock *Save = nullptr;
  288. /// Not null, if shrink-wrapping found a better place for the epilogue.
  289. MachineBasicBlock *Restore = nullptr;
  290. public:
  291. explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
  292. bool ForcedRealign)
  293. : StackAlignment(assumeAligned(StackAlignment)),
  294. StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
  295. MachineFrameInfo(const MachineFrameInfo &) = delete;
  296. /// Return true if there are any stack objects in this function.
  297. bool hasStackObjects() const { return !Objects.empty(); }
  298. /// This method may be called any time after instruction
  299. /// selection is complete to determine if the stack frame for this function
  300. /// contains any variable sized objects.
  301. bool hasVarSizedObjects() const { return HasVarSizedObjects; }
  302. /// Return the index for the stack protector object.
  303. int getStackProtectorIndex() const { return StackProtectorIdx; }
  304. void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
  305. bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
  306. /// Return the index for the function context object.
  307. /// This object is used for SjLj exceptions.
  308. int getFunctionContextIndex() const { return FunctionContextIdx; }
  309. void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
  310. /// This method may be called any time after instruction
  311. /// selection is complete to determine if there is a call to
  312. /// \@llvm.frameaddress in this function.
  313. bool isFrameAddressTaken() const { return FrameAddressTaken; }
  314. void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
  315. /// This method may be called any time after
  316. /// instruction selection is complete to determine if there is a call to
  317. /// \@llvm.returnaddress in this function.
  318. bool isReturnAddressTaken() const { return ReturnAddressTaken; }
  319. void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
  320. /// This method may be called any time after instruction
  321. /// selection is complete to determine if there is a call to builtin
  322. /// \@llvm.experimental.stackmap.
  323. bool hasStackMap() const { return HasStackMap; }
  324. void setHasStackMap(bool s = true) { HasStackMap = s; }
  325. /// This method may be called any time after instruction
  326. /// selection is complete to determine if there is a call to builtin
  327. /// \@llvm.experimental.patchpoint.
  328. bool hasPatchPoint() const { return HasPatchPoint; }
  329. void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
  330. /// Return the minimum frame object index.
  331. int getObjectIndexBegin() const { return -NumFixedObjects; }
  332. /// Return one past the maximum frame object index.
  333. int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
  334. /// Return the number of fixed objects.
  335. unsigned getNumFixedObjects() const { return NumFixedObjects; }
  336. /// Return the number of objects.
  337. unsigned getNumObjects() const { return Objects.size(); }
  338. /// Map a frame index into the local object block
  339. void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
  340. LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
  341. Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
  342. }
  343. /// Get the local offset mapping for a for an object.
  344. std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
  345. assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
  346. "Invalid local object reference!");
  347. return LocalFrameObjects[i];
  348. }
  349. /// Return the number of objects allocated into the local object block.
  350. int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
  351. /// Set the size of the local object blob.
  352. void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
  353. /// Get the size of the local object blob.
  354. int64_t getLocalFrameSize() const { return LocalFrameSize; }
  355. /// Required alignment of the local object blob,
  356. /// which is the strictest alignment of any object in it.
  357. void setLocalFrameMaxAlign(Align Alignment) {
  358. LocalFrameMaxAlign = Alignment;
  359. }
  360. /// Return the required alignment of the local object blob.
  361. Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
  362. /// Get whether the local allocation blob should be allocated together or
  363. /// let PEI allocate the locals in it directly.
  364. bool getUseLocalStackAllocationBlock() const {
  365. return UseLocalStackAllocationBlock;
  366. }
  367. /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
  368. /// should be allocated together or let PEI allocate the locals in it
  369. /// directly.
  370. void setUseLocalStackAllocationBlock(bool v) {
  371. UseLocalStackAllocationBlock = v;
  372. }
  373. /// Return true if the object was pre-allocated into the local block.
  374. bool isObjectPreAllocated(int ObjectIdx) const {
  375. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  376. "Invalid Object Idx!");
  377. return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
  378. }
  379. /// Return the size of the specified object.
  380. int64_t getObjectSize(int ObjectIdx) const {
  381. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  382. "Invalid Object Idx!");
  383. return Objects[ObjectIdx+NumFixedObjects].Size;
  384. }
  385. /// Change the size of the specified stack object.
  386. void setObjectSize(int ObjectIdx, int64_t Size) {
  387. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  388. "Invalid Object Idx!");
  389. Objects[ObjectIdx+NumFixedObjects].Size = Size;
  390. }
  391. /// Return the alignment of the specified stack object.
  392. Align getObjectAlign(int ObjectIdx) const {
  393. assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
  394. "Invalid Object Idx!");
  395. return Objects[ObjectIdx + NumFixedObjects].Alignment;
  396. }
  397. /// setObjectAlignment - Change the alignment of the specified stack object.
  398. void setObjectAlignment(int ObjectIdx, Align Alignment) {
  399. assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
  400. "Invalid Object Idx!");
  401. Objects[ObjectIdx + NumFixedObjects].Alignment = Alignment;
  402. // Only ensure max alignment for the default stack.
  403. if (getStackID(ObjectIdx) == 0)
  404. ensureMaxAlignment(Alignment);
  405. }
  406. /// Return the underlying Alloca of the specified
  407. /// stack object if it exists. Returns 0 if none exists.
  408. const AllocaInst* getObjectAllocation(int ObjectIdx) const {
  409. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  410. "Invalid Object Idx!");
  411. return Objects[ObjectIdx+NumFixedObjects].Alloca;
  412. }
  413. /// Return the assigned stack offset of the specified object
  414. /// from the incoming stack pointer.
  415. int64_t getObjectOffset(int ObjectIdx) const {
  416. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  417. "Invalid Object Idx!");
  418. assert(!isDeadObjectIndex(ObjectIdx) &&
  419. "Getting frame offset for a dead object?");
  420. return Objects[ObjectIdx+NumFixedObjects].SPOffset;
  421. }
  422. bool isObjectZExt(int ObjectIdx) const {
  423. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  424. "Invalid Object Idx!");
  425. return Objects[ObjectIdx+NumFixedObjects].isZExt;
  426. }
  427. void setObjectZExt(int ObjectIdx, bool IsZExt) {
  428. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  429. "Invalid Object Idx!");
  430. Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
  431. }
  432. bool isObjectSExt(int ObjectIdx) const {
  433. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  434. "Invalid Object Idx!");
  435. return Objects[ObjectIdx+NumFixedObjects].isSExt;
  436. }
  437. void setObjectSExt(int ObjectIdx, bool IsSExt) {
  438. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  439. "Invalid Object Idx!");
  440. Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
  441. }
  442. /// Set the stack frame offset of the specified object. The
  443. /// offset is relative to the stack pointer on entry to the function.
  444. void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
  445. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  446. "Invalid Object Idx!");
  447. assert(!isDeadObjectIndex(ObjectIdx) &&
  448. "Setting frame offset for a dead object?");
  449. Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
  450. }
  451. SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
  452. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  453. "Invalid Object Idx!");
  454. return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
  455. }
  456. void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
  457. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  458. "Invalid Object Idx!");
  459. assert(!isDeadObjectIndex(ObjectIdx) &&
  460. "Setting SSP layout for a dead object?");
  461. Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
  462. }
  463. /// Return the number of bytes that must be allocated to hold
  464. /// all of the fixed size frame objects. This is only valid after
  465. /// Prolog/Epilog code insertion has finalized the stack frame layout.
  466. uint64_t getStackSize() const { return StackSize; }
  467. /// Set the size of the stack.
  468. void setStackSize(uint64_t Size) { StackSize = Size; }
  469. /// Estimate and return the size of the stack frame.
  470. uint64_t estimateStackSize(const MachineFunction &MF) const;
  471. /// Return the correction for frame offsets.
  472. int getOffsetAdjustment() const { return OffsetAdjustment; }
  473. /// Set the correction for frame offsets.
  474. void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
  475. /// Return the alignment in bytes that this function must be aligned to,
  476. /// which is greater than the default stack alignment provided by the target.
  477. Align getMaxAlign() const { return MaxAlignment; }
  478. /// Make sure the function is at least Align bytes aligned.
  479. void ensureMaxAlignment(Align Alignment);
  480. /// Return true if this function adjusts the stack -- e.g.,
  481. /// when calling another function. This is only valid during and after
  482. /// prolog/epilog code insertion.
  483. bool adjustsStack() const { return AdjustsStack; }
  484. void setAdjustsStack(bool V) { AdjustsStack = V; }
  485. /// Return true if the current function has any function calls.
  486. bool hasCalls() const { return HasCalls; }
  487. void setHasCalls(bool V) { HasCalls = V; }
  488. /// Returns true if the function contains opaque dynamic stack adjustments.
  489. bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
  490. void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
  491. /// Returns true if the function contains operations which will lower down to
  492. /// instructions which manipulate the stack pointer.
  493. bool hasCopyImplyingStackAdjustment() const {
  494. return HasCopyImplyingStackAdjustment;
  495. }
  496. void setHasCopyImplyingStackAdjustment(bool B) {
  497. HasCopyImplyingStackAdjustment = B;
  498. }
  499. /// Returns true if the function calls the llvm.va_start intrinsic.
  500. bool hasVAStart() const { return HasVAStart; }
  501. void setHasVAStart(bool B) { HasVAStart = B; }
  502. /// Returns true if the function is variadic and contains a musttail call.
  503. bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
  504. void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
  505. /// Returns true if the function contains a tail call.
  506. bool hasTailCall() const { return HasTailCall; }
  507. void setHasTailCall(bool V = true) { HasTailCall = V; }
  508. /// Computes the maximum size of a callframe and the AdjustsStack property.
  509. /// This only works for targets defining
  510. /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
  511. /// and getFrameSize().
  512. /// This is usually computed by the prologue epilogue inserter but some
  513. /// targets may call this to compute it earlier.
  514. void computeMaxCallFrameSize(const MachineFunction &MF);
  515. /// Return the maximum size of a call frame that must be
  516. /// allocated for an outgoing function call. This is only available if
  517. /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
  518. /// then only during or after prolog/epilog code insertion.
  519. ///
  520. unsigned getMaxCallFrameSize() const {
  521. // TODO: Enable this assert when targets are fixed.
  522. //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
  523. if (!isMaxCallFrameSizeComputed())
  524. return 0;
  525. return MaxCallFrameSize;
  526. }
  527. bool isMaxCallFrameSizeComputed() const {
  528. return MaxCallFrameSize != ~0u;
  529. }
  530. void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
  531. /// Returns how many bytes of callee-saved registers the target pushed in the
  532. /// prologue. Only used for debug info.
  533. unsigned getCVBytesOfCalleeSavedRegisters() const {
  534. return CVBytesOfCalleeSavedRegisters;
  535. }
  536. void setCVBytesOfCalleeSavedRegisters(unsigned S) {
  537. CVBytesOfCalleeSavedRegisters = S;
  538. }
  539. /// Create a new object at a fixed location on the stack.
  540. /// All fixed objects should be created before other objects are created for
  541. /// efficiency. By default, fixed objects are not pointed to by LLVM IR
  542. /// values. This returns an index with a negative value.
  543. int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
  544. bool isAliased = false);
  545. /// Create a spill slot at a fixed location on the stack.
  546. /// Returns an index with a negative value.
  547. int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
  548. bool IsImmutable = false);
  549. /// Returns true if the specified index corresponds to a fixed stack object.
  550. bool isFixedObjectIndex(int ObjectIdx) const {
  551. return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
  552. }
  553. /// Returns true if the specified index corresponds
  554. /// to an object that might be pointed to by an LLVM IR value.
  555. bool isAliasedObjectIndex(int ObjectIdx) const {
  556. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  557. "Invalid Object Idx!");
  558. return Objects[ObjectIdx+NumFixedObjects].isAliased;
  559. }
  560. /// Returns true if the specified index corresponds to an immutable object.
  561. bool isImmutableObjectIndex(int ObjectIdx) const {
  562. // Tail calling functions can clobber their function arguments.
  563. if (HasTailCall)
  564. return false;
  565. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  566. "Invalid Object Idx!");
  567. return Objects[ObjectIdx+NumFixedObjects].isImmutable;
  568. }
  569. /// Marks the immutability of an object.
  570. void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
  571. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  572. "Invalid Object Idx!");
  573. Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
  574. }
  575. /// Returns true if the specified index corresponds to a spill slot.
  576. bool isSpillSlotObjectIndex(int ObjectIdx) const {
  577. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  578. "Invalid Object Idx!");
  579. return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
  580. }
  581. bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
  582. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  583. "Invalid Object Idx!");
  584. return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
  585. }
  586. /// \see StackID
  587. uint8_t getStackID(int ObjectIdx) const {
  588. return Objects[ObjectIdx+NumFixedObjects].StackID;
  589. }
  590. /// \see StackID
  591. void setStackID(int ObjectIdx, uint8_t ID) {
  592. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  593. "Invalid Object Idx!");
  594. Objects[ObjectIdx+NumFixedObjects].StackID = ID;
  595. // If ID > 0, MaxAlignment may now be overly conservative.
  596. // If ID == 0, MaxAlignment will need to be updated separately.
  597. }
  598. /// Returns true if the specified index corresponds to a dead object.
  599. bool isDeadObjectIndex(int ObjectIdx) const {
  600. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  601. "Invalid Object Idx!");
  602. return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
  603. }
  604. /// Returns true if the specified index corresponds to a variable sized
  605. /// object.
  606. bool isVariableSizedObjectIndex(int ObjectIdx) const {
  607. assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
  608. "Invalid Object Idx!");
  609. return Objects[ObjectIdx + NumFixedObjects].Size == 0;
  610. }
  611. void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) {
  612. assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
  613. "Invalid Object Idx!");
  614. Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
  615. assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
  616. }
  617. /// Create a new statically sized stack object, returning
  618. /// a nonnegative identifier to represent it.
  619. int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot,
  620. const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
  621. /// Create a new statically sized stack object that represents a spill slot,
  622. /// returning a nonnegative identifier to represent it.
  623. int CreateSpillStackObject(uint64_t Size, Align Alignment);
  624. /// Remove or mark dead a statically sized stack object.
  625. void RemoveStackObject(int ObjectIdx) {
  626. // Mark it dead.
  627. Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
  628. }
  629. /// Notify the MachineFrameInfo object that a variable sized object has been
  630. /// created. This must be created whenever a variable sized object is
  631. /// created, whether or not the index returned is actually used.
  632. int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca);
  633. /// Returns a reference to call saved info vector for the current function.
  634. const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
  635. return CSInfo;
  636. }
  637. /// \copydoc getCalleeSavedInfo()
  638. std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
  639. /// Used by prolog/epilog inserter to set the function's callee saved
  640. /// information.
  641. void setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI) {
  642. CSInfo = std::move(CSI);
  643. }
  644. /// Has the callee saved info been calculated yet?
  645. bool isCalleeSavedInfoValid() const { return CSIValid; }
  646. void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
  647. MachineBasicBlock *getSavePoint() const { return Save; }
  648. void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
  649. MachineBasicBlock *getRestorePoint() const { return Restore; }
  650. void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
  651. /// Return a set of physical registers that are pristine.
  652. ///
  653. /// Pristine registers hold a value that is useless to the current function,
  654. /// but that must be preserved - they are callee saved registers that are not
  655. /// saved.
  656. ///
  657. /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
  658. /// method always returns an empty set.
  659. BitVector getPristineRegs(const MachineFunction &MF) const;
  660. /// Used by the MachineFunction printer to print information about
  661. /// stack objects. Implemented in MachineFunction.cpp.
  662. void print(const MachineFunction &MF, raw_ostream &OS) const;
  663. /// dump - Print the function to stderr.
  664. void dump(const MachineFunction &MF) const;
  665. };
  666. } // End llvm namespace
  667. #endif
  668. #ifdef __GNUC__
  669. #pragma GCC diagnostic pop
  670. #endif