Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:30

0001 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // The file defines the MachineFrameInfo class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
0014 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
0015 
0016 #include "llvm/ADT/SmallVector.h"
0017 #include "llvm/CodeGen/Register.h"
0018 #include "llvm/CodeGen/TargetFrameLowering.h"
0019 #include "llvm/Support/Alignment.h"
0020 #include <cassert>
0021 #include <vector>
0022 
0023 namespace llvm {
0024 class raw_ostream;
0025 class MachineFunction;
0026 class MachineBasicBlock;
0027 class BitVector;
0028 class AllocaInst;
0029 
0030 /// The CalleeSavedInfo class tracks the information need to locate where a
0031 /// callee saved register is in the current frame.
0032 /// Callee saved reg can also be saved to a different register rather than
0033 /// on the stack by setting DstReg instead of FrameIdx.
0034 class CalleeSavedInfo {
0035   Register Reg;
0036   union {
0037     int FrameIdx;
0038     unsigned DstReg;
0039   };
0040   /// Flag indicating whether the register is actually restored in the epilog.
0041   /// In most cases, if a register is saved, it is also restored. There are
0042   /// some situations, though, when this is not the case. For example, the
0043   /// LR register on ARM is usually saved, but on exit from the function its
0044   /// saved value may be loaded directly into PC. Since liveness tracking of
0045   /// physical registers treats callee-saved registers are live outside of
0046   /// the function, LR would be treated as live-on-exit, even though in these
0047   /// scenarios it is not. This flag is added to indicate that the saved
0048   /// register described by this object is not restored in the epilog.
0049   /// The long-term solution is to model the liveness of callee-saved registers
0050   /// by implicit uses on the return instructions, however, the required
0051   /// changes in the ARM backend would be quite extensive.
0052   bool Restored = true;
0053   /// Flag indicating whether the register is spilled to stack or another
0054   /// register.
0055   bool SpilledToReg = false;
0056 
0057 public:
0058   explicit CalleeSavedInfo(unsigned R, int FI = 0) : Reg(R), FrameIdx(FI) {}
0059 
0060   // Accessors.
0061   Register getReg()                        const { return Reg; }
0062   int getFrameIdx()                        const { return FrameIdx; }
0063   unsigned getDstReg()                     const { return DstReg; }
0064   void setFrameIdx(int FI) {
0065     FrameIdx = FI;
0066     SpilledToReg = false;
0067   }
0068   void setDstReg(Register SpillReg) {
0069     DstReg = SpillReg;
0070     SpilledToReg = true;
0071   }
0072   bool isRestored()                        const { return Restored; }
0073   void setRestored(bool R)                       { Restored = R; }
0074   bool isSpilledToReg()                    const { return SpilledToReg; }
0075 };
0076 
0077 /// The MachineFrameInfo class represents an abstract stack frame until
0078 /// prolog/epilog code is inserted.  This class is key to allowing stack frame
0079 /// representation optimizations, such as frame pointer elimination.  It also
0080 /// allows more mundane (but still important) optimizations, such as reordering
0081 /// of abstract objects on the stack frame.
0082 ///
0083 /// To support this, the class assigns unique integer identifiers to stack
0084 /// objects requested clients.  These identifiers are negative integers for
0085 /// fixed stack objects (such as arguments passed on the stack) or nonnegative
0086 /// for objects that may be reordered.  Instructions which refer to stack
0087 /// objects use a special MO_FrameIndex operand to represent these frame
0088 /// indexes.
0089 ///
0090 /// Because this class keeps track of all references to the stack frame, it
0091 /// knows when a variable sized object is allocated on the stack.  This is the
0092 /// sole condition which prevents frame pointer elimination, which is an
0093 /// important optimization on register-poor architectures.  Because original
0094 /// variable sized alloca's in the source program are the only source of
0095 /// variable sized stack objects, it is safe to decide whether there will be
0096 /// any variable sized objects before all stack objects are known (for
0097 /// example, register allocator spill code never needs variable sized
0098 /// objects).
0099 ///
0100 /// When prolog/epilog code emission is performed, the final stack frame is
0101 /// built and the machine instructions are modified to refer to the actual
0102 /// stack offsets of the object, eliminating all MO_FrameIndex operands from
0103 /// the program.
0104 ///
0105 /// Abstract Stack Frame Information
0106 class MachineFrameInfo {
0107 public:
0108   /// Stack Smashing Protection (SSP) rules require that vulnerable stack
0109   /// allocations are located close the stack protector.
0110   enum SSPLayoutKind {
0111     SSPLK_None,       ///< Did not trigger a stack protector.  No effect on data
0112                       ///< layout.
0113     SSPLK_LargeArray, ///< Array or nested array >= SSP-buffer-size.  Closest
0114                       ///< to the stack protector.
0115     SSPLK_SmallArray, ///< Array or nested array < SSP-buffer-size. 2nd closest
0116                       ///< to the stack protector.
0117     SSPLK_AddrOf      ///< The address of this allocation is exposed and
0118                       ///< triggered protection.  3rd closest to the protector.
0119   };
0120 
0121 private:
0122   // Represent a single object allocated on the stack.
0123   struct StackObject {
0124     // The offset of this object from the stack pointer on entry to
0125     // the function.  This field has no meaning for a variable sized element.
0126     int64_t SPOffset;
0127 
0128     // The size of this object on the stack. 0 means a variable sized object,
0129     // ~0ULL means a dead object.
0130     uint64_t Size;
0131 
0132     // The required alignment of this stack slot.
0133     Align Alignment;
0134 
0135     // If true, the value of the stack object is set before
0136     // entering the function and is not modified inside the function. By
0137     // default, fixed objects are immutable unless marked otherwise.
0138     bool isImmutable;
0139 
0140     // If true the stack object is used as spill slot. It
0141     // cannot alias any other memory objects.
0142     bool isSpillSlot;
0143 
0144     /// If true, this stack slot is used to spill a value (could be deopt
0145     /// and/or GC related) over a statepoint. We know that the address of the
0146     /// slot can't alias any LLVM IR value.  This is very similar to a Spill
0147     /// Slot, but is created by statepoint lowering is SelectionDAG, not the
0148     /// register allocator.
0149     bool isStatepointSpillSlot = false;
0150 
0151     /// Identifier for stack memory type analagous to address space. If this is
0152     /// non-0, the meaning is target defined. Offsets cannot be directly
0153     /// compared between objects with different stack IDs. The object may not
0154     /// necessarily reside in the same contiguous memory block as other stack
0155     /// objects. Objects with differing stack IDs should not be merged or
0156     /// replaced substituted for each other.
0157     //
0158     /// It is assumed a target uses consecutive, increasing stack IDs starting
0159     /// from 1.
0160     uint8_t StackID;
0161 
0162     /// If this stack object is originated from an Alloca instruction
0163     /// this value saves the original IR allocation. Can be NULL.
0164     const AllocaInst *Alloca;
0165 
0166     // If true, the object was mapped into the local frame
0167     // block and doesn't need additional handling for allocation beyond that.
0168     bool PreAllocated = false;
0169 
0170     // If true, an LLVM IR value might point to this object.
0171     // Normally, spill slots and fixed-offset objects don't alias IR-accessible
0172     // objects, but there are exceptions (on PowerPC, for example, some byval
0173     // arguments have ABI-prescribed offsets).
0174     bool isAliased;
0175 
0176     /// If true, the object has been zero-extended.
0177     bool isZExt = false;
0178 
0179     /// If true, the object has been sign-extended.
0180     bool isSExt = false;
0181 
0182     uint8_t SSPLayout = SSPLK_None;
0183 
0184     StackObject(uint64_t Size, Align Alignment, int64_t SPOffset,
0185                 bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
0186                 bool IsAliased, uint8_t StackID = 0)
0187         : SPOffset(SPOffset), Size(Size), Alignment(Alignment),
0188           isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID),
0189           Alloca(Alloca), isAliased(IsAliased) {}
0190   };
0191 
0192   /// The alignment of the stack.
0193   Align StackAlignment;
0194 
0195   /// Can the stack be realigned. This can be false if the target does not
0196   /// support stack realignment, or if the user asks us not to realign the
0197   /// stack. In this situation, overaligned allocas are all treated as dynamic
0198   /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC
0199   /// lowering. All non-alloca stack objects have their alignment clamped to the
0200   /// base ABI stack alignment.
0201   /// FIXME: There is room for improvement in this case, in terms of
0202   /// grouping overaligned allocas into a "secondary stack frame" and
0203   /// then only use a single alloca to allocate this frame and only a
0204   /// single virtual register to access it. Currently, without such an
0205   /// optimization, each such alloca gets its own dynamic realignment.
0206   bool StackRealignable;
0207 
0208   /// Whether the function has the \c alignstack attribute.
0209   bool ForcedRealign;
0210 
0211   /// The list of stack objects allocated.
0212   std::vector<StackObject> Objects;
0213 
0214   /// This contains the number of fixed objects contained on
0215   /// the stack.  Because fixed objects are stored at a negative index in the
0216   /// Objects list, this is also the index to the 0th object in the list.
0217   unsigned NumFixedObjects = 0;
0218 
0219   /// This boolean keeps track of whether any variable
0220   /// sized objects have been allocated yet.
0221   bool HasVarSizedObjects = false;
0222 
0223   /// This boolean keeps track of whether there is a call
0224   /// to builtin \@llvm.frameaddress.
0225   bool FrameAddressTaken = false;
0226 
0227   /// This boolean keeps track of whether there is a call
0228   /// to builtin \@llvm.returnaddress.
0229   bool ReturnAddressTaken = false;
0230 
0231   /// This boolean keeps track of whether there is a call
0232   /// to builtin \@llvm.experimental.stackmap.
0233   bool HasStackMap = false;
0234 
0235   /// This boolean keeps track of whether there is a call
0236   /// to builtin \@llvm.experimental.patchpoint.
0237   bool HasPatchPoint = false;
0238 
0239   /// The prolog/epilog code inserter calculates the final stack
0240   /// offsets for all of the fixed size objects, updating the Objects list
0241   /// above.  It then updates StackSize to contain the number of bytes that need
0242   /// to be allocated on entry to the function.
0243   uint64_t StackSize = 0;
0244 
0245   /// The amount that a frame offset needs to be adjusted to
0246   /// have the actual offset from the stack/frame pointer.  The exact usage of
0247   /// this is target-dependent, but it is typically used to adjust between
0248   /// SP-relative and FP-relative offsets.  E.G., if objects are accessed via
0249   /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
0250   /// to the distance between the initial SP and the value in FP.  For many
0251   /// targets, this value is only used when generating debug info (via
0252   /// TargetRegisterInfo::getFrameIndexReference); when generating code, the
0253   /// corresponding adjustments are performed directly.
0254   int64_t OffsetAdjustment = 0;
0255 
0256   /// The prolog/epilog code inserter may process objects that require greater
0257   /// alignment than the default alignment the target provides.
0258   /// To handle this, MaxAlignment is set to the maximum alignment
0259   /// needed by the objects on the current frame.  If this is greater than the
0260   /// native alignment maintained by the compiler, dynamic alignment code will
0261   /// be needed.
0262   ///
0263   Align MaxAlignment;
0264 
0265   /// Set to true if this function adjusts the stack -- e.g.,
0266   /// when calling another function. This is only valid during and after
0267   /// prolog/epilog code insertion.
0268   bool AdjustsStack = false;
0269 
0270   /// Set to true if this function has any function calls.
0271   bool HasCalls = false;
0272 
0273   /// The frame index for the stack protector.
0274   int StackProtectorIdx = -1;
0275 
0276   /// The frame index for the function context. Used for SjLj exceptions.
0277   int FunctionContextIdx = -1;
0278 
0279   /// This contains the size of the largest call frame if the target uses frame
0280   /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
0281   /// class).  This information is important for frame pointer elimination.
0282   /// It is only valid during and after prolog/epilog code insertion.
0283   uint64_t MaxCallFrameSize = ~UINT64_C(0);
0284 
0285   /// The number of bytes of callee saved registers that the target wants to
0286   /// report for the current function in the CodeView S_FRAMEPROC record.
0287   unsigned CVBytesOfCalleeSavedRegisters = 0;
0288 
0289   /// The prolog/epilog code inserter fills in this vector with each
0290   /// callee saved register saved in either the frame or a different
0291   /// register.  Beyond its use by the prolog/ epilog code inserter,
0292   /// this data is used for debug info and exception handling.
0293   std::vector<CalleeSavedInfo> CSInfo;
0294 
0295   /// Has CSInfo been set yet?
0296   bool CSIValid = false;
0297 
0298   /// References to frame indices which are mapped
0299   /// into the local frame allocation block. <FrameIdx, LocalOffset>
0300   SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
0301 
0302   /// Size of the pre-allocated local frame block.
0303   int64_t LocalFrameSize = 0;
0304 
0305   /// Required alignment of the local object blob, which is the strictest
0306   /// alignment of any object in it.
0307   Align LocalFrameMaxAlign;
0308 
0309   /// Whether the local object blob needs to be allocated together. If not,
0310   /// PEI should ignore the isPreAllocated flags on the stack objects and
0311   /// just allocate them normally.
0312   bool UseLocalStackAllocationBlock = false;
0313 
0314   /// True if the function dynamically adjusts the stack pointer through some
0315   /// opaque mechanism like inline assembly or Win32 EH.
0316   bool HasOpaqueSPAdjustment = false;
0317 
0318   /// True if the function contains operations which will lower down to
0319   /// instructions which manipulate the stack pointer.
0320   bool HasCopyImplyingStackAdjustment = false;
0321 
0322   /// True if the function contains a call to the llvm.vastart intrinsic.
0323   bool HasVAStart = false;
0324 
0325   /// True if this is a varargs function that contains a musttail call.
0326   bool HasMustTailInVarArgFunc = false;
0327 
0328   /// True if this function contains a tail call. If so immutable objects like
0329   /// function arguments are no longer so. A tail call *can* override fixed
0330   /// stack objects like arguments so we can't treat them as immutable.
0331   bool HasTailCall = false;
0332 
0333   /// Not null, if shrink-wrapping found a better place for the prologue.
0334   MachineBasicBlock *Save = nullptr;
0335   /// Not null, if shrink-wrapping found a better place for the epilogue.
0336   MachineBasicBlock *Restore = nullptr;
0337 
0338   /// Size of the UnsafeStack Frame
0339   uint64_t UnsafeStackSize = 0;
0340 
0341 public:
0342   explicit MachineFrameInfo(Align StackAlignment, bool StackRealignable,
0343                             bool ForcedRealign)
0344       : StackAlignment(StackAlignment),
0345         StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
0346 
0347   MachineFrameInfo(const MachineFrameInfo &) = delete;
0348 
0349   bool isStackRealignable() const { return StackRealignable; }
0350 
0351   /// Return true if there are any stack objects in this function.
0352   bool hasStackObjects() const { return !Objects.empty(); }
0353 
0354   /// This method may be called any time after instruction
0355   /// selection is complete to determine if the stack frame for this function
0356   /// contains any variable sized objects.
0357   bool hasVarSizedObjects() const { return HasVarSizedObjects; }
0358 
0359   /// Return the index for the stack protector object.
0360   int getStackProtectorIndex() const { return StackProtectorIdx; }
0361   void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
0362   bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
0363 
0364   /// Return the index for the function context object.
0365   /// This object is used for SjLj exceptions.
0366   int getFunctionContextIndex() const { return FunctionContextIdx; }
0367   void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
0368   bool hasFunctionContextIndex() const { return FunctionContextIdx != -1; }
0369 
0370   /// This method may be called any time after instruction
0371   /// selection is complete to determine if there is a call to
0372   /// \@llvm.frameaddress in this function.
0373   bool isFrameAddressTaken() const { return FrameAddressTaken; }
0374   void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
0375 
0376   /// This method may be called any time after
0377   /// instruction selection is complete to determine if there is a call to
0378   /// \@llvm.returnaddress in this function.
0379   bool isReturnAddressTaken() const { return ReturnAddressTaken; }
0380   void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
0381 
0382   /// This method may be called any time after instruction
0383   /// selection is complete to determine if there is a call to builtin
0384   /// \@llvm.experimental.stackmap.
0385   bool hasStackMap() const { return HasStackMap; }
0386   void setHasStackMap(bool s = true) { HasStackMap = s; }
0387 
0388   /// This method may be called any time after instruction
0389   /// selection is complete to determine if there is a call to builtin
0390   /// \@llvm.experimental.patchpoint.
0391   bool hasPatchPoint() const { return HasPatchPoint; }
0392   void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
0393 
0394   /// Return true if this function requires a split stack prolog, even if it
0395   /// uses no stack space. This is only meaningful for functions where
0396   /// MachineFunction::shouldSplitStack() returns true.
0397   //
0398   // For non-leaf functions we have to allow for the possibility that the call
0399   // is to a non-split function, as in PR37807. This function could also take
0400   // the address of a non-split function. When the linker tries to adjust its
0401   // non-existent prologue, it would fail with an error. Mark the object file so
0402   // that such failures are not errors. See this Go language bug-report
0403   // https://go-review.googlesource.com/c/go/+/148819/
0404   bool needsSplitStackProlog() const {
0405     return getStackSize() != 0 || hasTailCall();
0406   }
0407 
0408   /// Return the minimum frame object index.
0409   int getObjectIndexBegin() const { return -NumFixedObjects; }
0410 
0411   /// Return one past the maximum frame object index.
0412   int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
0413 
0414   /// Return the number of fixed objects.
0415   unsigned getNumFixedObjects() const { return NumFixedObjects; }
0416 
0417   /// Return the number of objects.
0418   unsigned getNumObjects() const { return Objects.size(); }
0419 
0420   /// Map a frame index into the local object block
0421   void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
0422     LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
0423     Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
0424   }
0425 
0426   /// Get the local offset mapping for a for an object.
0427   std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
0428     assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
0429             "Invalid local object reference!");
0430     return LocalFrameObjects[i];
0431   }
0432 
0433   /// Return the number of objects allocated into the local object block.
0434   int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
0435 
0436   /// Set the size of the local object blob.
0437   void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
0438 
0439   /// Get the size of the local object blob.
0440   int64_t getLocalFrameSize() const { return LocalFrameSize; }
0441 
0442   /// Required alignment of the local object blob,
0443   /// which is the strictest alignment of any object in it.
0444   void setLocalFrameMaxAlign(Align Alignment) {
0445     LocalFrameMaxAlign = Alignment;
0446   }
0447 
0448   /// Return the required alignment of the local object blob.
0449   Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
0450 
0451   /// Get whether the local allocation blob should be allocated together or
0452   /// let PEI allocate the locals in it directly.
0453   bool getUseLocalStackAllocationBlock() const {
0454     return UseLocalStackAllocationBlock;
0455   }
0456 
0457   /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
0458   /// should be allocated together or let PEI allocate the locals in it
0459   /// directly.
0460   void setUseLocalStackAllocationBlock(bool v) {
0461     UseLocalStackAllocationBlock = v;
0462   }
0463 
0464   /// Return true if the object was pre-allocated into the local block.
0465   bool isObjectPreAllocated(int ObjectIdx) const {
0466     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0467            "Invalid Object Idx!");
0468     return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
0469   }
0470 
0471   /// Return the size of the specified object.
0472   int64_t getObjectSize(int ObjectIdx) const {
0473     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0474            "Invalid Object Idx!");
0475     return Objects[ObjectIdx+NumFixedObjects].Size;
0476   }
0477 
0478   /// Change the size of the specified stack object.
0479   void setObjectSize(int ObjectIdx, int64_t Size) {
0480     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0481            "Invalid Object Idx!");
0482     Objects[ObjectIdx+NumFixedObjects].Size = Size;
0483   }
0484 
0485   /// Return the alignment of the specified stack object.
0486   Align getObjectAlign(int ObjectIdx) const {
0487     assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
0488            "Invalid Object Idx!");
0489     return Objects[ObjectIdx + NumFixedObjects].Alignment;
0490   }
0491 
0492   /// Should this stack ID be considered in MaxAlignment.
0493   bool contributesToMaxAlignment(uint8_t StackID) {
0494     return StackID == TargetStackID::Default ||
0495            StackID == TargetStackID::ScalableVector;
0496   }
0497 
0498   /// setObjectAlignment - Change the alignment of the specified stack object.
0499   void setObjectAlignment(int ObjectIdx, Align Alignment) {
0500     assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
0501            "Invalid Object Idx!");
0502     Objects[ObjectIdx + NumFixedObjects].Alignment = Alignment;
0503 
0504     // Only ensure max alignment for the default and scalable vector stack.
0505     uint8_t StackID = getStackID(ObjectIdx);
0506     if (contributesToMaxAlignment(StackID))
0507       ensureMaxAlignment(Alignment);
0508   }
0509 
0510   /// Return the underlying Alloca of the specified
0511   /// stack object if it exists. Returns 0 if none exists.
0512   const AllocaInst* getObjectAllocation(int ObjectIdx) const {
0513     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0514            "Invalid Object Idx!");
0515     return Objects[ObjectIdx+NumFixedObjects].Alloca;
0516   }
0517 
0518   /// Remove the underlying Alloca of the specified stack object if it
0519   /// exists. This generally should not be used and is for reduction tooling.
0520   void clearObjectAllocation(int ObjectIdx) {
0521     assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
0522            "Invalid Object Idx!");
0523     Objects[ObjectIdx + NumFixedObjects].Alloca = nullptr;
0524   }
0525 
0526   /// Return the assigned stack offset of the specified object
0527   /// from the incoming stack pointer.
0528   int64_t getObjectOffset(int ObjectIdx) const {
0529     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0530            "Invalid Object Idx!");
0531     assert(!isDeadObjectIndex(ObjectIdx) &&
0532            "Getting frame offset for a dead object?");
0533     return Objects[ObjectIdx+NumFixedObjects].SPOffset;
0534   }
0535 
0536   bool isObjectZExt(int ObjectIdx) const {
0537     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0538            "Invalid Object Idx!");
0539     return Objects[ObjectIdx+NumFixedObjects].isZExt;
0540   }
0541 
0542   void setObjectZExt(int ObjectIdx, bool IsZExt) {
0543     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0544            "Invalid Object Idx!");
0545     Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
0546   }
0547 
0548   bool isObjectSExt(int ObjectIdx) const {
0549     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0550            "Invalid Object Idx!");
0551     return Objects[ObjectIdx+NumFixedObjects].isSExt;
0552   }
0553 
0554   void setObjectSExt(int ObjectIdx, bool IsSExt) {
0555     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0556            "Invalid Object Idx!");
0557     Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
0558   }
0559 
0560   /// Set the stack frame offset of the specified object. The
0561   /// offset is relative to the stack pointer on entry to the function.
0562   void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
0563     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0564            "Invalid Object Idx!");
0565     assert(!isDeadObjectIndex(ObjectIdx) &&
0566            "Setting frame offset for a dead object?");
0567     Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
0568   }
0569 
0570   SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
0571     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0572            "Invalid Object Idx!");
0573     return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
0574   }
0575 
0576   void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
0577     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0578            "Invalid Object Idx!");
0579     assert(!isDeadObjectIndex(ObjectIdx) &&
0580            "Setting SSP layout for a dead object?");
0581     Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
0582   }
0583 
0584   /// Return the number of bytes that must be allocated to hold
0585   /// all of the fixed size frame objects.  This is only valid after
0586   /// Prolog/Epilog code insertion has finalized the stack frame layout.
0587   uint64_t getStackSize() const { return StackSize; }
0588 
0589   /// Set the size of the stack.
0590   void setStackSize(uint64_t Size) { StackSize = Size; }
0591 
0592   /// Estimate and return the size of the stack frame.
0593   uint64_t estimateStackSize(const MachineFunction &MF) const;
0594 
0595   /// Return the correction for frame offsets.
0596   int64_t getOffsetAdjustment() const { return OffsetAdjustment; }
0597 
0598   /// Set the correction for frame offsets.
0599   void setOffsetAdjustment(int64_t Adj) { OffsetAdjustment = Adj; }
0600 
0601   /// Return the alignment in bytes that this function must be aligned to,
0602   /// which is greater than the default stack alignment provided by the target.
0603   Align getMaxAlign() const { return MaxAlignment; }
0604 
0605   /// Make sure the function is at least Align bytes aligned.
0606   void ensureMaxAlignment(Align Alignment);
0607 
0608   /// Return true if stack realignment is forced by function attributes or if
0609   /// the stack alignment.
0610   bool shouldRealignStack() const {
0611     return ForcedRealign || MaxAlignment > StackAlignment;
0612   }
0613 
0614   /// Return true if this function adjusts the stack -- e.g.,
0615   /// when calling another function. This is only valid during and after
0616   /// prolog/epilog code insertion.
0617   bool adjustsStack() const { return AdjustsStack; }
0618   void setAdjustsStack(bool V) { AdjustsStack = V; }
0619 
0620   /// Return true if the current function has any function calls.
0621   bool hasCalls() const { return HasCalls; }
0622   void setHasCalls(bool V) { HasCalls = V; }
0623 
0624   /// Returns true if the function contains opaque dynamic stack adjustments.
0625   bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
0626   void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
0627 
0628   /// Returns true if the function contains operations which will lower down to
0629   /// instructions which manipulate the stack pointer.
0630   bool hasCopyImplyingStackAdjustment() const {
0631     return HasCopyImplyingStackAdjustment;
0632   }
0633   void setHasCopyImplyingStackAdjustment(bool B) {
0634     HasCopyImplyingStackAdjustment = B;
0635   }
0636 
0637   /// Returns true if the function calls the llvm.va_start intrinsic.
0638   bool hasVAStart() const { return HasVAStart; }
0639   void setHasVAStart(bool B) { HasVAStart = B; }
0640 
0641   /// Returns true if the function is variadic and contains a musttail call.
0642   bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
0643   void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
0644 
0645   /// Returns true if the function contains a tail call.
0646   bool hasTailCall() const { return HasTailCall; }
0647   void setHasTailCall(bool V = true) { HasTailCall = V; }
0648 
0649   /// Computes the maximum size of a callframe.
0650   /// This only works for targets defining
0651   /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
0652   /// and getFrameSize().
0653   /// This is usually computed by the prologue epilogue inserter but some
0654   /// targets may call this to compute it earlier.
0655   /// If FrameSDOps is passed, the frame instructions in the MF will be
0656   /// inserted into it.
0657   void computeMaxCallFrameSize(
0658       MachineFunction &MF,
0659       std::vector<MachineBasicBlock::iterator> *FrameSDOps = nullptr);
0660 
0661   /// Return the maximum size of a call frame that must be
0662   /// allocated for an outgoing function call.  This is only available if
0663   /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
0664   /// then only during or after prolog/epilog code insertion.
0665   ///
0666   uint64_t getMaxCallFrameSize() const {
0667     // TODO: Enable this assert when targets are fixed.
0668     //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
0669     if (!isMaxCallFrameSizeComputed())
0670       return 0;
0671     return MaxCallFrameSize;
0672   }
0673   bool isMaxCallFrameSizeComputed() const {
0674     return MaxCallFrameSize != ~UINT64_C(0);
0675   }
0676   void setMaxCallFrameSize(uint64_t S) { MaxCallFrameSize = S; }
0677 
0678   /// Returns how many bytes of callee-saved registers the target pushed in the
0679   /// prologue. Only used for debug info.
0680   unsigned getCVBytesOfCalleeSavedRegisters() const {
0681     return CVBytesOfCalleeSavedRegisters;
0682   }
0683   void setCVBytesOfCalleeSavedRegisters(unsigned S) {
0684     CVBytesOfCalleeSavedRegisters = S;
0685   }
0686 
0687   /// Create a new object at a fixed location on the stack.
0688   /// All fixed objects should be created before other objects are created for
0689   /// efficiency. By default, fixed objects are not pointed to by LLVM IR
0690   /// values. This returns an index with a negative value.
0691   int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
0692                         bool isAliased = false);
0693 
0694   /// Create a spill slot at a fixed location on the stack.
0695   /// Returns an index with a negative value.
0696   int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
0697                                   bool IsImmutable = false);
0698 
0699   /// Returns true if the specified index corresponds to a fixed stack object.
0700   bool isFixedObjectIndex(int ObjectIdx) const {
0701     return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
0702   }
0703 
0704   /// Returns true if the specified index corresponds
0705   /// to an object that might be pointed to by an LLVM IR value.
0706   bool isAliasedObjectIndex(int ObjectIdx) const {
0707     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0708            "Invalid Object Idx!");
0709     return Objects[ObjectIdx+NumFixedObjects].isAliased;
0710   }
0711 
0712   /// Set "maybe pointed to by an LLVM IR value" for an object.
0713   void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased) {
0714     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0715            "Invalid Object Idx!");
0716     Objects[ObjectIdx+NumFixedObjects].isAliased = IsAliased;
0717   }
0718 
0719   /// Returns true if the specified index corresponds to an immutable object.
0720   bool isImmutableObjectIndex(int ObjectIdx) const {
0721     // Tail calling functions can clobber their function arguments.
0722     if (HasTailCall)
0723       return false;
0724     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0725            "Invalid Object Idx!");
0726     return Objects[ObjectIdx+NumFixedObjects].isImmutable;
0727   }
0728 
0729   /// Marks the immutability of an object.
0730   void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
0731     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0732            "Invalid Object Idx!");
0733     Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
0734   }
0735 
0736   /// Returns true if the specified index corresponds to a spill slot.
0737   bool isSpillSlotObjectIndex(int ObjectIdx) const {
0738     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0739            "Invalid Object Idx!");
0740     return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
0741   }
0742 
0743   bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
0744     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0745            "Invalid Object Idx!");
0746     return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
0747   }
0748 
0749   /// \see StackID
0750   uint8_t getStackID(int ObjectIdx) const {
0751     return Objects[ObjectIdx+NumFixedObjects].StackID;
0752   }
0753 
0754   /// \see StackID
0755   void setStackID(int ObjectIdx, uint8_t ID) {
0756     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0757            "Invalid Object Idx!");
0758     Objects[ObjectIdx+NumFixedObjects].StackID = ID;
0759     // If ID > 0, MaxAlignment may now be overly conservative.
0760     // If ID == 0, MaxAlignment will need to be updated separately.
0761   }
0762 
0763   /// Returns true if the specified index corresponds to a dead object.
0764   bool isDeadObjectIndex(int ObjectIdx) const {
0765     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0766            "Invalid Object Idx!");
0767     return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
0768   }
0769 
0770   /// Returns true if the specified index corresponds to a variable sized
0771   /// object.
0772   bool isVariableSizedObjectIndex(int ObjectIdx) const {
0773     assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
0774            "Invalid Object Idx!");
0775     return Objects[ObjectIdx + NumFixedObjects].Size == 0;
0776   }
0777 
0778   void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) {
0779     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
0780            "Invalid Object Idx!");
0781     Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
0782     assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
0783   }
0784 
0785   /// Create a new statically sized stack object, returning
0786   /// a nonnegative identifier to represent it.
0787   int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot,
0788                         const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
0789 
0790   /// Create a new statically sized stack object that represents a spill slot,
0791   /// returning a nonnegative identifier to represent it.
0792   int CreateSpillStackObject(uint64_t Size, Align Alignment);
0793 
0794   /// Remove or mark dead a statically sized stack object.
0795   void RemoveStackObject(int ObjectIdx) {
0796     // Mark it dead.
0797     Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
0798   }
0799 
0800   /// Notify the MachineFrameInfo object that a variable sized object has been
0801   /// created.  This must be created whenever a variable sized object is
0802   /// created, whether or not the index returned is actually used.
0803   int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca);
0804 
0805   /// Returns a reference to call saved info vector for the current function.
0806   const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
0807     return CSInfo;
0808   }
0809   /// \copydoc getCalleeSavedInfo()
0810   std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
0811 
0812   /// Used by prolog/epilog inserter to set the function's callee saved
0813   /// information.
0814   void setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI) {
0815     CSInfo = std::move(CSI);
0816   }
0817 
0818   /// Has the callee saved info been calculated yet?
0819   bool isCalleeSavedInfoValid() const { return CSIValid; }
0820 
0821   void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
0822 
0823   MachineBasicBlock *getSavePoint() const { return Save; }
0824   void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
0825   MachineBasicBlock *getRestorePoint() const { return Restore; }
0826   void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
0827 
0828   uint64_t getUnsafeStackSize() const { return UnsafeStackSize; }
0829   void setUnsafeStackSize(uint64_t Size) { UnsafeStackSize = Size; }
0830 
0831   /// Return a set of physical registers that are pristine.
0832   ///
0833   /// Pristine registers hold a value that is useless to the current function,
0834   /// but that must be preserved - they are callee saved registers that are not
0835   /// saved.
0836   ///
0837   /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
0838   /// method always returns an empty set.
0839   BitVector getPristineRegs(const MachineFunction &MF) const;
0840 
0841   /// Used by the MachineFunction printer to print information about
0842   /// stack objects. Implemented in MachineFunction.cpp.
0843   void print(const MachineFunction &MF, raw_ostream &OS) const;
0844 
0845   /// dump - Print the function to stderr.
0846   void dump(const MachineFunction &MF) const;
0847 };
0848 
0849 } // End llvm namespace
0850 
0851 #endif