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