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