MachineFrameInfo.h 33 KB

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