LLVM  8.0.0svn
MachineFrameInfo.h
Go to the documentation of this file.
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.
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)
50  : Reg(R), FrameIdx(FI), Restored(true) {}
51 
52  // Accessors.
53  unsigned getReg() const { return Reg; }
54  int getFrameIdx() const { return FrameIdx; }
55  void setFrameIdx(int FI) { FrameIdx = FI; }
56  bool isRestored() const { return Restored; }
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
90 public:
91  /// Stack Smashing Protection (SSP) rules require that vulnerable stack
92  /// allocations are located close the stack protector.
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)
170  : 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 number of bytes of callee saved registers that the target wants to
270  /// report for the current function in the CodeView S_FRAMEPROC record.
271  unsigned CVBytesOfCalleeSavedRegisters = 0;
272 
273  /// The prolog/epilog code inserter fills in this vector with each
274  /// callee saved register saved in the frame. Beyond its use by the prolog/
275  /// epilog code inserter, this data used for debug info and exception
276  /// handling.
277  std::vector<CalleeSavedInfo> CSInfo;
278 
279  /// Has CSInfo been set yet?
280  bool CSIValid = false;
281 
282  /// References to frame indices which are mapped
283  /// into the local frame allocation block. <FrameIdx, LocalOffset>
284  SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
285 
286  /// Size of the pre-allocated local frame block.
287  int64_t LocalFrameSize = 0;
288 
289  /// Required alignment of the local object blob, which is the strictest
290  /// alignment of any object in it.
291  unsigned LocalFrameMaxAlign = 0;
292 
293  /// Whether the local object blob needs to be allocated together. If not,
294  /// PEI should ignore the isPreAllocated flags on the stack objects and
295  /// just allocate them normally.
296  bool UseLocalStackAllocationBlock = false;
297 
298  /// True if the function dynamically adjusts the stack pointer through some
299  /// opaque mechanism like inline assembly or Win32 EH.
300  bool HasOpaqueSPAdjustment = false;
301 
302  /// True if the function contains operations which will lower down to
303  /// instructions which manipulate the stack pointer.
304  bool HasCopyImplyingStackAdjustment = false;
305 
306  /// True if the function contains a call to the llvm.vastart intrinsic.
307  bool HasVAStart = false;
308 
309  /// True if this is a varargs function that contains a musttail call.
310  bool HasMustTailInVarArgFunc = false;
311 
312  /// True if this function contains a tail call. If so immutable objects like
313  /// function arguments are no longer so. A tail call *can* override fixed
314  /// stack objects like arguments so we can't treat them as immutable.
315  bool HasTailCall = false;
316 
317  /// Not null, if shrink-wrapping found a better place for the prologue.
318  MachineBasicBlock *Save = nullptr;
319  /// Not null, if shrink-wrapping found a better place for the epilogue.
320  MachineBasicBlock *Restore = nullptr;
321 
322 public:
323  explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
324  bool ForcedRealign)
325  : StackAlignment(StackAlignment), StackRealignable(StackRealignable),
326  ForcedRealign(ForcedRealign) {}
327 
328  /// Return true if there are any stack objects in this function.
329  bool hasStackObjects() const { return !Objects.empty(); }
330 
331  /// This method may be called any time after instruction
332  /// selection is complete to determine if the stack frame for this function
333  /// contains any variable sized objects.
334  bool hasVarSizedObjects() const { return HasVarSizedObjects; }
335 
336  /// Return the index for the stack protector object.
337  int getStackProtectorIndex() const { return StackProtectorIdx; }
338  void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
339  bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
340 
341  /// Return the index for the function context object.
342  /// This object is used for SjLj exceptions.
343  int getFunctionContextIndex() const { return FunctionContextIdx; }
344  void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
345 
346  /// This method may be called any time after instruction
347  /// selection is complete to determine if there is a call to
348  /// \@llvm.frameaddress in this function.
349  bool isFrameAddressTaken() const { return FrameAddressTaken; }
350  void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
351 
352  /// This method may be called any time after
353  /// instruction selection is complete to determine if there is a call to
354  /// \@llvm.returnaddress in this function.
355  bool isReturnAddressTaken() const { return ReturnAddressTaken; }
356  void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
357 
358  /// This method may be called any time after instruction
359  /// selection is complete to determine if there is a call to builtin
360  /// \@llvm.experimental.stackmap.
361  bool hasStackMap() const { return HasStackMap; }
362  void setHasStackMap(bool s = true) { HasStackMap = s; }
363 
364  /// This method may be called any time after instruction
365  /// selection is complete to determine if there is a call to builtin
366  /// \@llvm.experimental.patchpoint.
367  bool hasPatchPoint() const { return HasPatchPoint; }
368  void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
369 
370  /// Return the minimum frame object index.
371  int getObjectIndexBegin() const { return -NumFixedObjects; }
372 
373  /// Return one past the maximum frame object index.
374  int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
375 
376  /// Return the number of fixed objects.
377  unsigned getNumFixedObjects() const { return NumFixedObjects; }
378 
379  /// Return the number of objects.
380  unsigned getNumObjects() const { return Objects.size(); }
381 
382  /// Map a frame index into the local object block
383  void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
384  LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
385  Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
386  }
387 
388  /// Get the local offset mapping for a for an object.
389  std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
390  assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
391  "Invalid local object reference!");
392  return LocalFrameObjects[i];
393  }
394 
395  /// Return the number of objects allocated into the local object block.
396  int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
397 
398  /// Set the size of the local object blob.
399  void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
400 
401  /// Get the size of the local object blob.
402  int64_t getLocalFrameSize() const { return LocalFrameSize; }
403 
404  /// Required alignment of the local object blob,
405  /// which is the strictest alignment of any object in it.
406  void setLocalFrameMaxAlign(unsigned Align) { LocalFrameMaxAlign = Align; }
407 
408  /// Return the required alignment of the local object blob.
409  unsigned getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
410 
411  /// Get whether the local allocation blob should be allocated together or
412  /// let PEI allocate the locals in it directly.
414  return UseLocalStackAllocationBlock;
415  }
416 
417  /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
418  /// should be allocated together or let PEI allocate the locals in it
419  /// directly.
421  UseLocalStackAllocationBlock = v;
422  }
423 
424  /// Return true if the object was pre-allocated into the local block.
425  bool isObjectPreAllocated(int ObjectIdx) const {
426  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
427  "Invalid Object Idx!");
428  return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
429  }
430 
431  /// Return the size of the specified object.
432  int64_t getObjectSize(int ObjectIdx) const {
433  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
434  "Invalid Object Idx!");
435  return Objects[ObjectIdx+NumFixedObjects].Size;
436  }
437 
438  /// Change the size of the specified stack object.
439  void setObjectSize(int ObjectIdx, int64_t Size) {
440  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
441  "Invalid Object Idx!");
442  Objects[ObjectIdx+NumFixedObjects].Size = Size;
443  }
444 
445  /// Return the alignment of the specified stack object.
446  unsigned getObjectAlignment(int ObjectIdx) const {
447  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
448  "Invalid Object Idx!");
449  return Objects[ObjectIdx+NumFixedObjects].Alignment;
450  }
451 
452  /// setObjectAlignment - Change the alignment of the specified stack object.
453  void setObjectAlignment(int ObjectIdx, unsigned Align) {
454  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
455  "Invalid Object Idx!");
456  Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
457  ensureMaxAlignment(Align);
458  }
459 
460  /// Return the underlying Alloca of the specified
461  /// stack object if it exists. Returns 0 if none exists.
462  const AllocaInst* getObjectAllocation(int ObjectIdx) const {
463  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
464  "Invalid Object Idx!");
465  return Objects[ObjectIdx+NumFixedObjects].Alloca;
466  }
467 
468  /// Return the assigned stack offset of the specified object
469  /// from the incoming stack pointer.
470  int64_t getObjectOffset(int ObjectIdx) const {
471  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
472  "Invalid Object Idx!");
473  assert(!isDeadObjectIndex(ObjectIdx) &&
474  "Getting frame offset for a dead object?");
475  return Objects[ObjectIdx+NumFixedObjects].SPOffset;
476  }
477 
478  bool isObjectZExt(int ObjectIdx) const {
479  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
480  "Invalid Object Idx!");
481  return Objects[ObjectIdx+NumFixedObjects].isZExt;
482  }
483 
484  void setObjectZExt(int ObjectIdx, bool IsZExt) {
485  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
486  "Invalid Object Idx!");
487  Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
488  }
489 
490  bool isObjectSExt(int ObjectIdx) const {
491  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
492  "Invalid Object Idx!");
493  return Objects[ObjectIdx+NumFixedObjects].isSExt;
494  }
495 
496  void setObjectSExt(int ObjectIdx, bool IsSExt) {
497  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
498  "Invalid Object Idx!");
499  Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
500  }
501 
502  /// Set the stack frame offset of the specified object. The
503  /// offset is relative to the stack pointer on entry to the function.
504  void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
505  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
506  "Invalid Object Idx!");
507  assert(!isDeadObjectIndex(ObjectIdx) &&
508  "Setting frame offset for a dead object?");
509  Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
510  }
511 
512  SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
513  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
514  "Invalid Object Idx!");
515  return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
516  }
517 
518  void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
519  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
520  "Invalid Object Idx!");
521  assert(!isDeadObjectIndex(ObjectIdx) &&
522  "Setting SSP layout for a dead object?");
523  Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
524  }
525 
526  /// Return the number of bytes that must be allocated to hold
527  /// all of the fixed size frame objects. This is only valid after
528  /// Prolog/Epilog code insertion has finalized the stack frame layout.
529  uint64_t getStackSize() const { return StackSize; }
530 
531  /// Set the size of the stack.
532  void setStackSize(uint64_t Size) { StackSize = Size; }
533 
534  /// Estimate and return the size of the stack frame.
535  unsigned estimateStackSize(const MachineFunction &MF) const;
536 
537  /// Return the correction for frame offsets.
538  int getOffsetAdjustment() const { return OffsetAdjustment; }
539 
540  /// Set the correction for frame offsets.
541  void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
542 
543  /// Return the alignment in bytes that this function must be aligned to,
544  /// which is greater than the default stack alignment provided by the target.
545  unsigned getMaxAlignment() const { return MaxAlignment; }
546 
547  /// Make sure the function is at least Align bytes aligned.
548  void ensureMaxAlignment(unsigned Align);
549 
550  /// Return true if this function adjusts the stack -- e.g.,
551  /// when calling another function. This is only valid during and after
552  /// prolog/epilog code insertion.
553  bool adjustsStack() const { return AdjustsStack; }
554  void setAdjustsStack(bool V) { AdjustsStack = V; }
555 
556  /// Return true if the current function has any function calls.
557  bool hasCalls() const { return HasCalls; }
558  void setHasCalls(bool V) { HasCalls = V; }
559 
560  /// Returns true if the function contains opaque dynamic stack adjustments.
561  bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
562  void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
563 
564  /// Returns true if the function contains operations which will lower down to
565  /// instructions which manipulate the stack pointer.
567  return HasCopyImplyingStackAdjustment;
568  }
570  HasCopyImplyingStackAdjustment = B;
571  }
572 
573  /// Returns true if the function calls the llvm.va_start intrinsic.
574  bool hasVAStart() const { return HasVAStart; }
575  void setHasVAStart(bool B) { HasVAStart = B; }
576 
577  /// Returns true if the function is variadic and contains a musttail call.
578  bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
579  void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
580 
581  /// Returns true if the function contains a tail call.
582  bool hasTailCall() const { return HasTailCall; }
583  void setHasTailCall() { HasTailCall = true; }
584 
585  /// Computes the maximum size of a callframe and the AdjustsStack property.
586  /// This only works for targets defining
587  /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
588  /// and getFrameSize().
589  /// This is usually computed by the prologue epilogue inserter but some
590  /// targets may call this to compute it earlier.
591  void computeMaxCallFrameSize(const MachineFunction &MF);
592 
593  /// Return the maximum size of a call frame that must be
594  /// allocated for an outgoing function call. This is only available if
595  /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
596  /// then only during or after prolog/epilog code insertion.
597  ///
598  unsigned getMaxCallFrameSize() const {
599  // TODO: Enable this assert when targets are fixed.
600  //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
601  if (!isMaxCallFrameSizeComputed())
602  return 0;
603  return MaxCallFrameSize;
604  }
606  return MaxCallFrameSize != ~0u;
607  }
608  void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
609 
610  /// Returns how many bytes of callee-saved registers the target pushed in the
611  /// prologue. Only used for debug info.
613  return CVBytesOfCalleeSavedRegisters;
614  }
616  CVBytesOfCalleeSavedRegisters = S;
617  }
618 
619  /// Create a new object at a fixed location on the stack.
620  /// All fixed objects should be created before other objects are created for
621  /// efficiency. By default, fixed objects are not pointed to by LLVM IR
622  /// values. This returns an index with a negative value.
623  int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
624  bool isAliased = false);
625 
626  /// Create a spill slot at a fixed location on the stack.
627  /// Returns an index with a negative value.
628  int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
629  bool IsImmutable = false);
630 
631  /// Returns true if the specified index corresponds to a fixed stack object.
632  bool isFixedObjectIndex(int ObjectIdx) const {
633  return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
634  }
635 
636  /// Returns true if the specified index corresponds
637  /// to an object that might be pointed to by an LLVM IR value.
638  bool isAliasedObjectIndex(int ObjectIdx) const {
639  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
640  "Invalid Object Idx!");
641  return Objects[ObjectIdx+NumFixedObjects].isAliased;
642  }
643 
644  /// Returns true if the specified index corresponds to an immutable object.
645  bool isImmutableObjectIndex(int ObjectIdx) const {
646  // Tail calling functions can clobber their function arguments.
647  if (HasTailCall)
648  return false;
649  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
650  "Invalid Object Idx!");
651  return Objects[ObjectIdx+NumFixedObjects].isImmutable;
652  }
653 
654  /// Marks the immutability of an object.
655  void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
656  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
657  "Invalid Object Idx!");
658  Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
659  }
660 
661  /// Returns true if the specified index corresponds to a spill slot.
662  bool isSpillSlotObjectIndex(int ObjectIdx) const {
663  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
664  "Invalid Object Idx!");
665  return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
666  }
667 
668  bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
669  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
670  "Invalid Object Idx!");
671  return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
672  }
673 
674  /// \see StackID
675  uint8_t getStackID(int ObjectIdx) const {
676  return Objects[ObjectIdx+NumFixedObjects].StackID;
677  }
678 
679  /// \see StackID
680  void setStackID(int ObjectIdx, uint8_t ID) {
681  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
682  "Invalid Object Idx!");
683  Objects[ObjectIdx+NumFixedObjects].StackID = ID;
684  }
685 
686  /// Returns true if the specified index corresponds to a dead object.
687  bool isDeadObjectIndex(int ObjectIdx) const {
688  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
689  "Invalid Object Idx!");
690  return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
691  }
692 
693  /// Returns true if the specified index corresponds to a variable sized
694  /// object.
695  bool isVariableSizedObjectIndex(int ObjectIdx) const {
696  assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
697  "Invalid Object Idx!");
698  return Objects[ObjectIdx + NumFixedObjects].Size == 0;
699  }
700 
702  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
703  "Invalid Object Idx!");
704  Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
705  assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
706  }
707 
708  /// Create a new statically sized stack object, returning
709  /// a nonnegative identifier to represent it.
710  int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot,
711  const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
712 
713  /// Create a new statically sized stack object that represents a spill slot,
714  /// returning a nonnegative identifier to represent it.
715  int CreateSpillStackObject(uint64_t Size, unsigned Alignment);
716 
717  /// Remove or mark dead a statically sized stack object.
718  void RemoveStackObject(int ObjectIdx) {
719  // Mark it dead.
720  Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
721  }
722 
723  /// Notify the MachineFrameInfo object that a variable sized object has been
724  /// created. This must be created whenever a variable sized object is
725  /// created, whether or not the index returned is actually used.
726  int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
727 
728  /// Returns a reference to call saved info vector for the current function.
729  const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
730  return CSInfo;
731  }
732  /// \copydoc getCalleeSavedInfo()
733  std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
734 
735  /// Used by prolog/epilog inserter to set the function's callee saved
736  /// information.
737  void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
738  CSInfo = CSI;
739  }
740 
741  /// Has the callee saved info been calculated yet?
742  bool isCalleeSavedInfoValid() const { return CSIValid; }
743 
744  void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
745 
746  MachineBasicBlock *getSavePoint() const { return Save; }
747  void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
748  MachineBasicBlock *getRestorePoint() const { return Restore; }
749  void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
750 
751  /// Return a set of physical registers that are pristine.
752  ///
753  /// Pristine registers hold a value that is useless to the current function,
754  /// but that must be preserved - they are callee saved registers that are not
755  /// saved.
756  ///
757  /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
758  /// method always returns an empty set.
759  BitVector getPristineRegs(const MachineFunction &MF) const;
760 
761  /// Used by the MachineFunction printer to print information about
762  /// stack objects. Implemented in MachineFunction.cpp.
763  void print(const MachineFunction &MF, raw_ostream &OS) const;
764 
765  /// dump - Print the function to stderr.
766  void dump(const MachineFunction &MF) const;
767 };
768 
769 } // End llvm namespace
770 
771 #endif
void setHasStackMap(bool s=true)
void setFrameAddressIsTaken(bool T)
unsigned getNumFixedObjects() const
Return the number of fixed objects.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
void setSavePoint(MachineBasicBlock *NewSave)
void setObjectZExt(int ObjectIdx, bool IsZExt)
bool hasStackMap() const
This method may be called any time after instruction selection is complete to determine if there is a...
void setRestorePoint(MachineBasicBlock *NewRestore)
void mapLocalFrameObject(int ObjectIndex, int64_t Offset)
Map a frame index into the local object block.
int getFunctionContextIndex() const
Return the index for the function context object.
void setCalleeSavedInfoValid(bool v)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
unsigned getNumObjects() const
Return the number of objects.
bool hasVAStart() const
Returns true if the function calls the llvm.va_start intrinsic.
bool hasStackProtectorIndex() const
bool hasStackObjects() const
Return true if there are any stack objects in this function.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int64_t getLocalFrameSize() const
Get the size of the local object blob.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
block Block Frequency true
bool isVariableSizedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a variable sized object.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
std::pair< int, int64_t > getLocalFrameObjectMap(int i) const
Get the local offset mapping for a for an object.
void setHasPatchPoint(bool s=true)
void setLocalFrameSize(int64_t sz)
Set the size of the local object blob.
void setUseLocalStackAllocationBlock(bool v)
setUseLocalStackAllocationBlock - Set whether the local allocation blob should be allocated together ...
MachineBasicBlock * getRestorePoint() const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool isObjectSExt(int ObjectIdx) const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
void setFunctionContextIndex(int I)
bool isImmutableObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to an immutable object.
bool isObjectZExt(int ObjectIdx) const
bool getUseLocalStackAllocationBlock() const
Get whether the local allocation blob should be allocated together or let PEI allocate the locals in ...
void setHasMustTailInVarArgFunc(bool B)
#define T
int getObjectIndexBegin() const
Return the minimum frame object index.
bool isObjectPreAllocated(int ObjectIdx) const
Return true if the object was pre-allocated into the local block.
void setLocalFrameMaxAlign(unsigned Align)
Required alignment of the local object blob, which is the strictest alignment of any object in it...
int getObjectIndexEnd() const
Return one past the maximum frame object index.
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const
void setHasCopyImplyingStackAdjustment(bool B)
bool hasCopyImplyingStackAdjustment() const
Returns true if the function contains operations which will lower down to instructions which manipula...
void setObjectSExt(int ObjectIdx, bool IsSExt)
void setStackProtectorIndex(int I)
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void setHasOpaqueSPAdjustment(bool B)
void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind)
unsigned getCVBytesOfCalleeSavedRegisters() const
Returns how many bytes of callee-saved registers the target pushed in the prologue.
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
int getStackProtectorIndex() const
Return the index for the stack protector object.
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
MachineFrameInfo(unsigned StackAlignment, bool StackRealignable, bool ForcedRealign)
void setObjectSize(int ObjectIdx, int64_t Size)
Change the size of the specified stack object.
void setStackID(int ObjectIdx, uint8_t ID)
size_t size() const
Definition: SmallVector.h:53
void markAsStatepointSpillSlotObjectIndex(int ObjectIdx)
CalleeSavedInfo(unsigned R, int FI=0)
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
SSPLayoutKind
Stack Smashing Protection (SSP) rules require that vulnerable stack allocations are located close the...
void setOffsetAdjustment(int Adj)
Set the correction for frame offsets.
bool hasMustTailInVarArgFunc() const
Returns true if the function is variadic and contains a musttail call.
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
void setCalleeSavedInfo(const std::vector< CalleeSavedInfo > &CSI)
Used by prolog/epilog inserter to set the function&#39;s callee saved information.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool isAliasedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to an object that might be pointed to by an LLVM IR v...
bool isReturnAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
#define I(x, y, z)
Definition: MD5.cpp:58
void setMaxCallFrameSize(unsigned S)
unsigned getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
int getOffsetAdjustment() const
Return the correction for frame offsets.
void setCVBytesOfCalleeSavedRegisters(unsigned S)
uint32_t Size
Definition: Profile.cpp:47
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
const unsigned Kind
uint8_t getStackID(int ObjectIdx) const
MachineBasicBlock * getSavePoint() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void setReturnAddressIsTaken(bool s)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
bool isMaxCallFrameSizeComputed() const
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
bool hasPatchPoint() const
This method may be called any time after instruction selection is complete to determine if there is a...
int64_t getLocalFrameObjectCount() const
Return the number of objects allocated into the local object block.
unsigned getReg() const
bool hasTailCall() const
Returns true if the function contains a tail call.
void setObjectAlignment(int ObjectIdx, unsigned Align)
setObjectAlignment - Change the alignment of the specified stack object.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
std::vector< CalleeSavedInfo > & getCalleeSavedInfo()
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
bool hasCalls() const
Return true if the current function has any function calls.
an instruction to allocate memory on the stack
Definition: Instructions.h:60