MachineFrameInfo.h revision 344779
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
22namespace llvm {
23class raw_ostream;
24class MachineFunction;
25class MachineBasicBlock;
26class BitVector;
27class AllocaInst;
28
29/// The CalleeSavedInfo class tracks the information need to locate where a
30/// callee saved register is in the current frame.
31/// Callee saved reg can also be saved to a different register rather than
32/// on the stack by setting DstReg instead of FrameIdx.
33class CalleeSavedInfo {
34  unsigned Reg;
35  union {
36    int FrameIdx;
37    unsigned DstReg;
38  };
39  /// Flag indicating whether the register is actually restored in the epilog.
40  /// In most cases, if a register is saved, it is also restored. There are
41  /// some situations, though, when this is not the case. For example, the
42  /// LR register on ARM is usually saved, but on exit from the function its
43  /// saved value may be loaded directly into PC. Since liveness tracking of
44  /// physical registers treats callee-saved registers are live outside of
45  /// the function, LR would be treated as live-on-exit, even though in these
46  /// scenarios it is not. This flag is added to indicate that the saved
47  /// register described by this object is not restored in the epilog.
48  /// The long-term solution is to model the liveness of callee-saved registers
49  /// by implicit uses on the return instructions, however, the required
50  /// changes in the ARM backend would be quite extensive.
51  bool Restored;
52  /// Flag indicating whether the register is spilled to stack or another
53  /// register.
54  bool SpilledToReg;
55
56public:
57  explicit CalleeSavedInfo(unsigned R, int FI = 0)
58  : Reg(R), FrameIdx(FI), Restored(true), SpilledToReg(false) {}
59
60  // Accessors.
61  unsigned getReg()                        const { return Reg; }
62  int getFrameIdx()                        const { return FrameIdx; }
63  unsigned getDstReg()                     const { return DstReg; }
64  void setFrameIdx(int FI) {
65    FrameIdx = FI;
66    SpilledToReg = false;
67  }
68  void setDstReg(unsigned SpillReg) {
69    DstReg = SpillReg;
70    SpilledToReg = true;
71  }
72  bool isRestored()                        const { return Restored; }
73  void setRestored(bool R)                       { Restored = R; }
74  bool isSpilledToReg()                    const { return SpilledToReg; }
75};
76
77/// The MachineFrameInfo class represents an abstract stack frame until
78/// prolog/epilog code is inserted.  This class is key to allowing stack frame
79/// representation optimizations, such as frame pointer elimination.  It also
80/// allows more mundane (but still important) optimizations, such as reordering
81/// of abstract objects on the stack frame.
82///
83/// To support this, the class assigns unique integer identifiers to stack
84/// objects requested clients.  These identifiers are negative integers for
85/// fixed stack objects (such as arguments passed on the stack) or nonnegative
86/// for objects that may be reordered.  Instructions which refer to stack
87/// objects use a special MO_FrameIndex operand to represent these frame
88/// indexes.
89///
90/// Because this class keeps track of all references to the stack frame, it
91/// knows when a variable sized object is allocated on the stack.  This is the
92/// sole condition which prevents frame pointer elimination, which is an
93/// important optimization on register-poor architectures.  Because original
94/// variable sized alloca's in the source program are the only source of
95/// variable sized stack objects, it is safe to decide whether there will be
96/// any variable sized objects before all stack objects are known (for
97/// example, register allocator spill code never needs variable sized
98/// objects).
99///
100/// When prolog/epilog code emission is performed, the final stack frame is
101/// built and the machine instructions are modified to refer to the actual
102/// stack offsets of the object, eliminating all MO_FrameIndex operands from
103/// the program.
104///
105/// Abstract Stack Frame Information
106class MachineFrameInfo {
107public:
108  /// Stack Smashing Protection (SSP) rules require that vulnerable stack
109  /// allocations are located close the stack protector.
110  enum SSPLayoutKind {
111    SSPLK_None,       ///< Did not trigger a stack protector.  No effect on data
112                      ///< layout.
113    SSPLK_LargeArray, ///< Array or nested array >= SSP-buffer-size.  Closest
114                      ///< to the stack protector.
115    SSPLK_SmallArray, ///< Array or nested array < SSP-buffer-size. 2nd closest
116                      ///< to the stack protector.
117    SSPLK_AddrOf      ///< The address of this allocation is exposed and
118                      ///< triggered protection.  3rd closest to the protector.
119  };
120
121private:
122  // Represent a single object allocated on the stack.
123  struct StackObject {
124    // The offset of this object from the stack pointer on entry to
125    // the function.  This field has no meaning for a variable sized element.
126    int64_t SPOffset;
127
128    // The size of this object on the stack. 0 means a variable sized object,
129    // ~0ULL means a dead object.
130    uint64_t Size;
131
132    // The required alignment of this stack slot.
133    unsigned Alignment;
134
135    // If true, the value of the stack object is set before
136    // entering the function and is not modified inside the function. By
137    // default, fixed objects are immutable unless marked otherwise.
138    bool isImmutable;
139
140    // If true the stack object is used as spill slot. It
141    // cannot alias any other memory objects.
142    bool isSpillSlot;
143
144    /// If true, this stack slot is used to spill a value (could be deopt
145    /// and/or GC related) over a statepoint. We know that the address of the
146    /// slot can't alias any LLVM IR value.  This is very similar to a Spill
147    /// Slot, but is created by statepoint lowering is SelectionDAG, not the
148    /// register allocator.
149    bool isStatepointSpillSlot = false;
150
151    /// Identifier for stack memory type analagous to address space. If this is
152    /// non-0, the meaning is target defined. Offsets cannot be directly
153    /// compared between objects with different stack IDs. The object may not
154    /// necessarily reside in the same contiguous memory block as other stack
155    /// objects. Objects with differing stack IDs should not be merged or
156    /// replaced substituted for each other.
157    //
158    /// It is assumed a target uses consecutive, increasing stack IDs starting
159    /// from 1.
160    uint8_t StackID;
161
162    /// If this stack object is originated from an Alloca instruction
163    /// this value saves the original IR allocation. Can be NULL.
164    const AllocaInst *Alloca;
165
166    // If true, the object was mapped into the local frame
167    // block and doesn't need additional handling for allocation beyond that.
168    bool PreAllocated = false;
169
170    // If true, an LLVM IR value might point to this object.
171    // Normally, spill slots and fixed-offset objects don't alias IR-accessible
172    // objects, but there are exceptions (on PowerPC, for example, some byval
173    // arguments have ABI-prescribed offsets).
174    bool isAliased;
175
176    /// If true, the object has been zero-extended.
177    bool isZExt = false;
178
179    /// If true, the object has been zero-extended.
180    bool isSExt = false;
181
182    uint8_t SSPLayout;
183
184    StackObject(uint64_t Size, unsigned Alignment, int64_t SPOffset,
185                bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
186                bool IsAliased, uint8_t StackID = 0)
187      : SPOffset(SPOffset), Size(Size), Alignment(Alignment),
188        isImmutable(IsImmutable), isSpillSlot(IsSpillSlot),
189        StackID(StackID), Alloca(Alloca), isAliased(IsAliased),
190        SSPLayout(SSPLK_None) {}
191  };
192
193  /// The alignment of the stack.
194  unsigned 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  unsigned MaxAlignment = 0;
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  unsigned LocalFrameMaxAlign = 0;
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
339public:
340  explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
341                            bool ForcedRealign)
342      : StackAlignment(StackAlignment), StackRealignable(StackRealignable),
343        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(unsigned Align) { LocalFrameMaxAlign = Align; }
424
425  /// Return the required alignment of the local object blob.
426  unsigned getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
427
428  /// Get whether the local allocation blob should be allocated together or
429  /// let PEI allocate the locals in it directly.
430  bool getUseLocalStackAllocationBlock() const {
431    return UseLocalStackAllocationBlock;
432  }
433
434  /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
435  /// should be allocated together or let PEI allocate the locals in it
436  /// directly.
437  void setUseLocalStackAllocationBlock(bool v) {
438    UseLocalStackAllocationBlock = v;
439  }
440
441  /// Return true if the object was pre-allocated into the local block.
442  bool isObjectPreAllocated(int ObjectIdx) const {
443    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
444           "Invalid Object Idx!");
445    return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
446  }
447
448  /// Return the size of the specified object.
449  int64_t getObjectSize(int ObjectIdx) const {
450    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
451           "Invalid Object Idx!");
452    return Objects[ObjectIdx+NumFixedObjects].Size;
453  }
454
455  /// Change the size of the specified stack object.
456  void setObjectSize(int ObjectIdx, int64_t Size) {
457    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
458           "Invalid Object Idx!");
459    Objects[ObjectIdx+NumFixedObjects].Size = Size;
460  }
461
462  /// Return the alignment of the specified stack object.
463  unsigned getObjectAlignment(int ObjectIdx) const {
464    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
465           "Invalid Object Idx!");
466    return Objects[ObjectIdx+NumFixedObjects].Alignment;
467  }
468
469  /// setObjectAlignment - Change the alignment of the specified stack object.
470  void setObjectAlignment(int ObjectIdx, unsigned Align) {
471    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
472           "Invalid Object Idx!");
473    Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
474    ensureMaxAlignment(Align);
475  }
476
477  /// Return the underlying Alloca of the specified
478  /// stack object if it exists. Returns 0 if none exists.
479  const AllocaInst* getObjectAllocation(int ObjectIdx) const {
480    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
481           "Invalid Object Idx!");
482    return Objects[ObjectIdx+NumFixedObjects].Alloca;
483  }
484
485  /// Return the assigned stack offset of the specified object
486  /// from the incoming stack pointer.
487  int64_t getObjectOffset(int ObjectIdx) const {
488    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
489           "Invalid Object Idx!");
490    assert(!isDeadObjectIndex(ObjectIdx) &&
491           "Getting frame offset for a dead object?");
492    return Objects[ObjectIdx+NumFixedObjects].SPOffset;
493  }
494
495  bool isObjectZExt(int ObjectIdx) const {
496    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
497           "Invalid Object Idx!");
498    return Objects[ObjectIdx+NumFixedObjects].isZExt;
499  }
500
501  void setObjectZExt(int ObjectIdx, bool IsZExt) {
502    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
503           "Invalid Object Idx!");
504    Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
505  }
506
507  bool isObjectSExt(int ObjectIdx) const {
508    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
509           "Invalid Object Idx!");
510    return Objects[ObjectIdx+NumFixedObjects].isSExt;
511  }
512
513  void setObjectSExt(int ObjectIdx, bool IsSExt) {
514    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
515           "Invalid Object Idx!");
516    Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
517  }
518
519  /// Set the stack frame offset of the specified object. The
520  /// offset is relative to the stack pointer on entry to the function.
521  void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
522    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
523           "Invalid Object Idx!");
524    assert(!isDeadObjectIndex(ObjectIdx) &&
525           "Setting frame offset for a dead object?");
526    Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
527  }
528
529  SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
530    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
531           "Invalid Object Idx!");
532    return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
533  }
534
535  void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
536    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
537           "Invalid Object Idx!");
538    assert(!isDeadObjectIndex(ObjectIdx) &&
539           "Setting SSP layout for a dead object?");
540    Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
541  }
542
543  /// Return the number of bytes that must be allocated to hold
544  /// all of the fixed size frame objects.  This is only valid after
545  /// Prolog/Epilog code insertion has finalized the stack frame layout.
546  uint64_t getStackSize() const { return StackSize; }
547
548  /// Set the size of the stack.
549  void setStackSize(uint64_t Size) { StackSize = Size; }
550
551  /// Estimate and return the size of the stack frame.
552  unsigned estimateStackSize(const MachineFunction &MF) const;
553
554  /// Return the correction for frame offsets.
555  int getOffsetAdjustment() const { return OffsetAdjustment; }
556
557  /// Set the correction for frame offsets.
558  void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
559
560  /// Return the alignment in bytes that this function must be aligned to,
561  /// which is greater than the default stack alignment provided by the target.
562  unsigned getMaxAlignment() const { return MaxAlignment; }
563
564  /// Make sure the function is at least Align bytes aligned.
565  void ensureMaxAlignment(unsigned Align);
566
567  /// Return true if this function adjusts the stack -- e.g.,
568  /// when calling another function. This is only valid during and after
569  /// prolog/epilog code insertion.
570  bool adjustsStack() const { return AdjustsStack; }
571  void setAdjustsStack(bool V) { AdjustsStack = V; }
572
573  /// Return true if the current function has any function calls.
574  bool hasCalls() const { return HasCalls; }
575  void setHasCalls(bool V) { HasCalls = V; }
576
577  /// Returns true if the function contains opaque dynamic stack adjustments.
578  bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
579  void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
580
581  /// Returns true if the function contains operations which will lower down to
582  /// instructions which manipulate the stack pointer.
583  bool hasCopyImplyingStackAdjustment() const {
584    return HasCopyImplyingStackAdjustment;
585  }
586  void setHasCopyImplyingStackAdjustment(bool B) {
587    HasCopyImplyingStackAdjustment = B;
588  }
589
590  /// Returns true if the function calls the llvm.va_start intrinsic.
591  bool hasVAStart() const { return HasVAStart; }
592  void setHasVAStart(bool B) { HasVAStart = B; }
593
594  /// Returns true if the function is variadic and contains a musttail call.
595  bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
596  void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
597
598  /// Returns true if the function contains a tail call.
599  bool hasTailCall() const { return HasTailCall; }
600  void setHasTailCall() { HasTailCall = true; }
601
602  /// Computes the maximum size of a callframe and the AdjustsStack property.
603  /// This only works for targets defining
604  /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
605  /// and getFrameSize().
606  /// This is usually computed by the prologue epilogue inserter but some
607  /// targets may call this to compute it earlier.
608  void computeMaxCallFrameSize(const MachineFunction &MF);
609
610  /// Return the maximum size of a call frame that must be
611  /// allocated for an outgoing function call.  This is only available if
612  /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
613  /// then only during or after prolog/epilog code insertion.
614  ///
615  unsigned getMaxCallFrameSize() const {
616    // TODO: Enable this assert when targets are fixed.
617    //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
618    if (!isMaxCallFrameSizeComputed())
619      return 0;
620    return MaxCallFrameSize;
621  }
622  bool isMaxCallFrameSizeComputed() const {
623    return MaxCallFrameSize != ~0u;
624  }
625  void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
626
627  /// Returns how many bytes of callee-saved registers the target pushed in the
628  /// prologue. Only used for debug info.
629  unsigned getCVBytesOfCalleeSavedRegisters() const {
630    return CVBytesOfCalleeSavedRegisters;
631  }
632  void setCVBytesOfCalleeSavedRegisters(unsigned S) {
633    CVBytesOfCalleeSavedRegisters = S;
634  }
635
636  /// Create a new object at a fixed location on the stack.
637  /// All fixed objects should be created before other objects are created for
638  /// efficiency. By default, fixed objects are not pointed to by LLVM IR
639  /// values. This returns an index with a negative value.
640  int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
641                        bool isAliased = false);
642
643  /// Create a spill slot at a fixed location on the stack.
644  /// Returns an index with a negative value.
645  int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
646                                  bool IsImmutable = false);
647
648  /// Returns true if the specified index corresponds to a fixed stack object.
649  bool isFixedObjectIndex(int ObjectIdx) const {
650    return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
651  }
652
653  /// Returns true if the specified index corresponds
654  /// to an object that might be pointed to by an LLVM IR value.
655  bool isAliasedObjectIndex(int ObjectIdx) const {
656    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
657           "Invalid Object Idx!");
658    return Objects[ObjectIdx+NumFixedObjects].isAliased;
659  }
660
661  /// Returns true if the specified index corresponds to an immutable object.
662  bool isImmutableObjectIndex(int ObjectIdx) const {
663    // Tail calling functions can clobber their function arguments.
664    if (HasTailCall)
665      return false;
666    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
667           "Invalid Object Idx!");
668    return Objects[ObjectIdx+NumFixedObjects].isImmutable;
669  }
670
671  /// Marks the immutability of an object.
672  void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
673    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
674           "Invalid Object Idx!");
675    Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
676  }
677
678  /// Returns true if the specified index corresponds to a spill slot.
679  bool isSpillSlotObjectIndex(int ObjectIdx) const {
680    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
681           "Invalid Object Idx!");
682    return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
683  }
684
685  bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
686    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
687           "Invalid Object Idx!");
688    return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
689  }
690
691  /// \see StackID
692  uint8_t getStackID(int ObjectIdx) const {
693    return Objects[ObjectIdx+NumFixedObjects].StackID;
694  }
695
696  /// \see StackID
697  void setStackID(int ObjectIdx, uint8_t ID) {
698    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
699           "Invalid Object Idx!");
700    Objects[ObjectIdx+NumFixedObjects].StackID = ID;
701  }
702
703  /// Returns true if the specified index corresponds to a dead object.
704  bool isDeadObjectIndex(int ObjectIdx) const {
705    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
706           "Invalid Object Idx!");
707    return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
708  }
709
710  /// Returns true if the specified index corresponds to a variable sized
711  /// object.
712  bool isVariableSizedObjectIndex(int ObjectIdx) const {
713    assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
714           "Invalid Object Idx!");
715    return Objects[ObjectIdx + NumFixedObjects].Size == 0;
716  }
717
718  void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) {
719    assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
720           "Invalid Object Idx!");
721    Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
722    assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
723  }
724
725  /// Create a new statically sized stack object, returning
726  /// a nonnegative identifier to represent it.
727  int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot,
728                        const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
729
730  /// Create a new statically sized stack object that represents a spill slot,
731  /// returning a nonnegative identifier to represent it.
732  int CreateSpillStackObject(uint64_t Size, unsigned Alignment);
733
734  /// Remove or mark dead a statically sized stack object.
735  void RemoveStackObject(int ObjectIdx) {
736    // Mark it dead.
737    Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
738  }
739
740  /// Notify the MachineFrameInfo object that a variable sized object has been
741  /// created.  This must be created whenever a variable sized object is
742  /// created, whether or not the index returned is actually used.
743  int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
744
745  /// Returns a reference to call saved info vector for the current function.
746  const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
747    return CSInfo;
748  }
749  /// \copydoc getCalleeSavedInfo()
750  std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
751
752  /// Used by prolog/epilog inserter to set the function's callee saved
753  /// information.
754  void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
755    CSInfo = CSI;
756  }
757
758  /// Has the callee saved info been calculated yet?
759  bool isCalleeSavedInfoValid() const { return CSIValid; }
760
761  void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
762
763  MachineBasicBlock *getSavePoint() const { return Save; }
764  void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
765  MachineBasicBlock *getRestorePoint() const { return Restore; }
766  void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
767
768  /// Return a set of physical registers that are pristine.
769  ///
770  /// Pristine registers hold a value that is useless to the current function,
771  /// but that must be preserved - they are callee saved registers that are not
772  /// saved.
773  ///
774  /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
775  /// method always returns an empty set.
776  BitVector getPristineRegs(const MachineFunction &MF) const;
777
778  /// Used by the MachineFunction printer to print information about
779  /// stack objects. Implemented in MachineFunction.cpp.
780  void print(const MachineFunction &MF, raw_ostream &OS) const;
781
782  /// dump - Print the function to stderr.
783  void dump(const MachineFunction &MF) const;
784};
785
786} // End llvm namespace
787
788#endif
789