LLVM  10.0.0svn
MachineFrameInfo.h
Go to the documentation of this file.
1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The file defines the MachineFrameInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
14 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
15 
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Alignment.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 /// Callee saved reg can also be saved to a different register rather than
32 /// on the stack by setting DstReg instead of FrameIdx.
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 
56 public:
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
107 public:
108  /// Stack Smashing Protection (SSP) rules require that vulnerable stack
109  /// allocations are located close the stack protector.
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 
121 private:
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  Align 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, Align 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), StackID(StackID),
189  Alloca(Alloca), isAliased(IsAliased), SSPLayout(SSPLK_None) {}
190  };
191 
192  /// The alignment of the stack.
193  Align StackAlignment;
194 
195  /// Can the stack be realigned. This can be false if the target does not
196  /// support stack realignment, or if the user asks us not to realign the
197  /// stack. In this situation, overaligned allocas are all treated as dynamic
198  /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC
199  /// lowering. All non-alloca stack objects have their alignment clamped to the
200  /// base ABI stack alignment.
201  /// FIXME: There is room for improvement in this case, in terms of
202  /// grouping overaligned allocas into a "secondary stack frame" and
203  /// then only use a single alloca to allocate this frame and only a
204  /// single virtual register to access it. Currently, without such an
205  /// optimization, each such alloca gets its own dynamic realignment.
206  bool StackRealignable;
207 
208  /// Whether the function has the \c alignstack attribute.
209  bool ForcedRealign;
210 
211  /// The list of stack objects allocated.
212  std::vector<StackObject> Objects;
213 
214  /// This contains the number of fixed objects contained on
215  /// the stack. Because fixed objects are stored at a negative index in the
216  /// Objects list, this is also the index to the 0th object in the list.
217  unsigned NumFixedObjects = 0;
218 
219  /// This boolean keeps track of whether any variable
220  /// sized objects have been allocated yet.
221  bool HasVarSizedObjects = false;
222 
223  /// This boolean keeps track of whether there is a call
224  /// to builtin \@llvm.frameaddress.
225  bool FrameAddressTaken = false;
226 
227  /// This boolean keeps track of whether there is a call
228  /// to builtin \@llvm.returnaddress.
229  bool ReturnAddressTaken = false;
230 
231  /// This boolean keeps track of whether there is a call
232  /// to builtin \@llvm.experimental.stackmap.
233  bool HasStackMap = false;
234 
235  /// This boolean keeps track of whether there is a call
236  /// to builtin \@llvm.experimental.patchpoint.
237  bool HasPatchPoint = false;
238 
239  /// The prolog/epilog code inserter calculates the final stack
240  /// offsets for all of the fixed size objects, updating the Objects list
241  /// above. It then updates StackSize to contain the number of bytes that need
242  /// to be allocated on entry to the function.
243  uint64_t StackSize = 0;
244 
245  /// The amount that a frame offset needs to be adjusted to
246  /// have the actual offset from the stack/frame pointer. The exact usage of
247  /// this is target-dependent, but it is typically used to adjust between
248  /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
249  /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
250  /// to the distance between the initial SP and the value in FP. For many
251  /// targets, this value is only used when generating debug info (via
252  /// TargetRegisterInfo::getFrameIndexReference); when generating code, the
253  /// corresponding adjustments are performed directly.
254  int OffsetAdjustment = 0;
255 
256  /// The prolog/epilog code inserter may process objects that require greater
257  /// alignment than the default alignment the target provides.
258  /// To handle this, MaxAlignment is set to the maximum alignment
259  /// needed by the objects on the current frame. If this is greater than the
260  /// native alignment maintained by the compiler, dynamic alignment code will
261  /// be needed.
262  ///
263  Align MaxAlignment;
264 
265  /// Set to true if this function adjusts the stack -- e.g.,
266  /// when calling another function. This is only valid during and after
267  /// prolog/epilog code insertion.
268  bool AdjustsStack = false;
269 
270  /// Set to true if this function has any function calls.
271  bool HasCalls = false;
272 
273  /// The frame index for the stack protector.
274  int StackProtectorIdx = -1;
275 
276  /// The frame index for the function context. Used for SjLj exceptions.
277  int FunctionContextIdx = -1;
278 
279  /// This contains the size of the largest call frame if the target uses frame
280  /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
281  /// class). This information is important for frame pointer elimination.
282  /// It is only valid during and after prolog/epilog code insertion.
283  unsigned MaxCallFrameSize = ~0u;
284 
285  /// The number of bytes of callee saved registers that the target wants to
286  /// report for the current function in the CodeView S_FRAMEPROC record.
287  unsigned CVBytesOfCalleeSavedRegisters = 0;
288 
289  /// The prolog/epilog code inserter fills in this vector with each
290  /// callee saved register saved in either the frame or a different
291  /// register. Beyond its use by the prolog/ epilog code inserter,
292  /// this data is used for debug info and exception handling.
293  std::vector<CalleeSavedInfo> CSInfo;
294 
295  /// Has CSInfo been set yet?
296  bool CSIValid = false;
297 
298  /// References to frame indices which are mapped
299  /// into the local frame allocation block. <FrameIdx, LocalOffset>
300  SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
301 
302  /// Size of the pre-allocated local frame block.
303  int64_t LocalFrameSize = 0;
304 
305  /// Required alignment of the local object blob, which is the strictest
306  /// alignment of any object in it.
307  Align LocalFrameMaxAlign;
308 
309  /// Whether the local object blob needs to be allocated together. If not,
310  /// PEI should ignore the isPreAllocated flags on the stack objects and
311  /// just allocate them normally.
312  bool UseLocalStackAllocationBlock = false;
313 
314  /// True if the function dynamically adjusts the stack pointer through some
315  /// opaque mechanism like inline assembly or Win32 EH.
316  bool HasOpaqueSPAdjustment = false;
317 
318  /// True if the function contains operations which will lower down to
319  /// instructions which manipulate the stack pointer.
320  bool HasCopyImplyingStackAdjustment = false;
321 
322  /// True if the function contains a call to the llvm.vastart intrinsic.
323  bool HasVAStart = false;
324 
325  /// True if this is a varargs function that contains a musttail call.
326  bool HasMustTailInVarArgFunc = false;
327 
328  /// True if this function contains a tail call. If so immutable objects like
329  /// function arguments are no longer so. A tail call *can* override fixed
330  /// stack objects like arguments so we can't treat them as immutable.
331  bool HasTailCall = false;
332 
333  /// Not null, if shrink-wrapping found a better place for the prologue.
334  MachineBasicBlock *Save = nullptr;
335  /// Not null, if shrink-wrapping found a better place for the epilogue.
336  MachineBasicBlock *Restore = nullptr;
337 
338 public:
339  explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
340  bool ForcedRealign)
341  : StackAlignment(assumeAligned(StackAlignment)),
342  StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
343 
344  /// Return true if there are any stack objects in this function.
345  bool hasStackObjects() const { return !Objects.empty(); }
346 
347  /// This method may be called any time after instruction
348  /// selection is complete to determine if the stack frame for this function
349  /// contains any variable sized objects.
350  bool hasVarSizedObjects() const { return HasVarSizedObjects; }
351 
352  /// Return the index for the stack protector object.
353  int getStackProtectorIndex() const { return StackProtectorIdx; }
354  void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
355  bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
356 
357  /// Return the index for the function context object.
358  /// This object is used for SjLj exceptions.
359  int getFunctionContextIndex() const { return FunctionContextIdx; }
360  void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
361 
362  /// This method may be called any time after instruction
363  /// selection is complete to determine if there is a call to
364  /// \@llvm.frameaddress in this function.
365  bool isFrameAddressTaken() const { return FrameAddressTaken; }
366  void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
367 
368  /// This method may be called any time after
369  /// instruction selection is complete to determine if there is a call to
370  /// \@llvm.returnaddress in this function.
371  bool isReturnAddressTaken() const { return ReturnAddressTaken; }
372  void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
373 
374  /// This method may be called any time after instruction
375  /// selection is complete to determine if there is a call to builtin
376  /// \@llvm.experimental.stackmap.
377  bool hasStackMap() const { return HasStackMap; }
378  void setHasStackMap(bool s = true) { HasStackMap = s; }
379 
380  /// This method may be called any time after instruction
381  /// selection is complete to determine if there is a call to builtin
382  /// \@llvm.experimental.patchpoint.
383  bool hasPatchPoint() const { return HasPatchPoint; }
384  void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
385 
386  /// Return the minimum frame object index.
387  int getObjectIndexBegin() const { return -NumFixedObjects; }
388 
389  /// Return one past the maximum frame object index.
390  int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
391 
392  /// Return the number of fixed objects.
393  unsigned getNumFixedObjects() const { return NumFixedObjects; }
394 
395  /// Return the number of objects.
396  unsigned getNumObjects() const { return Objects.size(); }
397 
398  /// Map a frame index into the local object block
399  void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
400  LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
401  Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
402  }
403 
404  /// Get the local offset mapping for a for an object.
405  std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
406  assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
407  "Invalid local object reference!");
408  return LocalFrameObjects[i];
409  }
410 
411  /// Return the number of objects allocated into the local object block.
412  int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
413 
414  /// Set the size of the local object blob.
415  void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
416 
417  /// Get the size of the local object blob.
418  int64_t getLocalFrameSize() const { return LocalFrameSize; }
419 
420  /// Required alignment of the local object blob,
421  /// which is the strictest alignment of any object in it.
422  void setLocalFrameMaxAlign(Align Alignment) {
423  LocalFrameMaxAlign = Alignment;
424  }
425 
426  /// Return the required alignment of the local object blob.
427  Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
428 
429  /// Get whether the local allocation blob should be allocated together or
430  /// let PEI allocate the locals in it directly.
432  return UseLocalStackAllocationBlock;
433  }
434 
435  /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
436  /// should be allocated together or let PEI allocate the locals in it
437  /// directly.
439  UseLocalStackAllocationBlock = v;
440  }
441 
442  /// Return true if the object was pre-allocated into the local block.
443  bool isObjectPreAllocated(int ObjectIdx) const {
444  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
445  "Invalid Object Idx!");
446  return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
447  }
448 
449  /// Return the size of the specified object.
450  int64_t getObjectSize(int ObjectIdx) const {
451  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
452  "Invalid Object Idx!");
453  return Objects[ObjectIdx+NumFixedObjects].Size;
454  }
455 
456  /// Change the size of the specified stack object.
457  void setObjectSize(int ObjectIdx, int64_t Size) {
458  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
459  "Invalid Object Idx!");
460  Objects[ObjectIdx+NumFixedObjects].Size = Size;
461  }
462 
463  /// Return the alignment of the specified stack object.
464  unsigned getObjectAlignment(int ObjectIdx) const {
465  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
466  "Invalid Object Idx!");
467  return Objects[ObjectIdx + NumFixedObjects].Alignment.value();
468  }
469 
470  /// setObjectAlignment - Change the alignment of the specified stack object.
471  void setObjectAlignment(int ObjectIdx, unsigned Align) {
472  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
473  "Invalid Object Idx!");
474  Objects[ObjectIdx + NumFixedObjects].Alignment = assumeAligned(Align);
475 
476  // Only ensure max alignment for the default stack.
477  if (getStackID(ObjectIdx) == 0)
478  ensureMaxAlignment(Align);
479  }
480 
481  /// Return the underlying Alloca of the specified
482  /// stack object if it exists. Returns 0 if none exists.
483  const AllocaInst* getObjectAllocation(int ObjectIdx) const {
484  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
485  "Invalid Object Idx!");
486  return Objects[ObjectIdx+NumFixedObjects].Alloca;
487  }
488 
489  /// Return the assigned stack offset of the specified object
490  /// from the incoming stack pointer.
491  int64_t getObjectOffset(int ObjectIdx) const {
492  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
493  "Invalid Object Idx!");
494  assert(!isDeadObjectIndex(ObjectIdx) &&
495  "Getting frame offset for a dead object?");
496  return Objects[ObjectIdx+NumFixedObjects].SPOffset;
497  }
498 
499  bool isObjectZExt(int ObjectIdx) const {
500  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
501  "Invalid Object Idx!");
502  return Objects[ObjectIdx+NumFixedObjects].isZExt;
503  }
504 
505  void setObjectZExt(int ObjectIdx, bool IsZExt) {
506  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
507  "Invalid Object Idx!");
508  Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
509  }
510 
511  bool isObjectSExt(int ObjectIdx) const {
512  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
513  "Invalid Object Idx!");
514  return Objects[ObjectIdx+NumFixedObjects].isSExt;
515  }
516 
517  void setObjectSExt(int ObjectIdx, bool IsSExt) {
518  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
519  "Invalid Object Idx!");
520  Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
521  }
522 
523  /// Set the stack frame offset of the specified object. The
524  /// offset is relative to the stack pointer on entry to the function.
525  void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
526  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
527  "Invalid Object Idx!");
528  assert(!isDeadObjectIndex(ObjectIdx) &&
529  "Setting frame offset for a dead object?");
530  Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
531  }
532 
533  SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
534  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
535  "Invalid Object Idx!");
536  return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
537  }
538 
539  void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
540  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
541  "Invalid Object Idx!");
542  assert(!isDeadObjectIndex(ObjectIdx) &&
543  "Setting SSP layout for a dead object?");
544  Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
545  }
546 
547  /// Return the number of bytes that must be allocated to hold
548  /// all of the fixed size frame objects. This is only valid after
549  /// Prolog/Epilog code insertion has finalized the stack frame layout.
550  uint64_t getStackSize() const { return StackSize; }
551 
552  /// Set the size of the stack.
553  void setStackSize(uint64_t Size) { StackSize = Size; }
554 
555  /// Estimate and return the size of the stack frame.
556  unsigned estimateStackSize(const MachineFunction &MF) const;
557 
558  /// Return the correction for frame offsets.
559  int getOffsetAdjustment() const { return OffsetAdjustment; }
560 
561  /// Set the correction for frame offsets.
562  void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
563 
564  /// Return the alignment in bytes that this function must be aligned to,
565  /// which is greater than the default stack alignment provided by the target.
566  unsigned getMaxAlignment() const { return MaxAlignment.value(); }
567 
568  /// Make sure the function is at least Align bytes aligned.
569  void ensureMaxAlignment(Align Alignment);
570  /// FIXME: Remove this once transition to Align is over.
571  inline void ensureMaxAlignment(unsigned Align) {
572  ensureMaxAlignment(assumeAligned(Align));
573  }
574 
575  /// Return true if this function adjusts the stack -- e.g.,
576  /// when calling another function. This is only valid during and after
577  /// prolog/epilog code insertion.
578  bool adjustsStack() const { return AdjustsStack; }
579  void setAdjustsStack(bool V) { AdjustsStack = V; }
580 
581  /// Return true if the current function has any function calls.
582  bool hasCalls() const { return HasCalls; }
583  void setHasCalls(bool V) { HasCalls = V; }
584 
585  /// Returns true if the function contains opaque dynamic stack adjustments.
586  bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
587  void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
588 
589  /// Returns true if the function contains operations which will lower down to
590  /// instructions which manipulate the stack pointer.
592  return HasCopyImplyingStackAdjustment;
593  }
595  HasCopyImplyingStackAdjustment = B;
596  }
597 
598  /// Returns true if the function calls the llvm.va_start intrinsic.
599  bool hasVAStart() const { return HasVAStart; }
600  void setHasVAStart(bool B) { HasVAStart = B; }
601 
602  /// Returns true if the function is variadic and contains a musttail call.
603  bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
604  void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
605 
606  /// Returns true if the function contains a tail call.
607  bool hasTailCall() const { return HasTailCall; }
608  void setHasTailCall() { HasTailCall = true; }
609 
610  /// Computes the maximum size of a callframe and the AdjustsStack property.
611  /// This only works for targets defining
612  /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
613  /// and getFrameSize().
614  /// This is usually computed by the prologue epilogue inserter but some
615  /// targets may call this to compute it earlier.
616  void computeMaxCallFrameSize(const MachineFunction &MF);
617 
618  /// Return the maximum size of a call frame that must be
619  /// allocated for an outgoing function call. This is only available if
620  /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
621  /// then only during or after prolog/epilog code insertion.
622  ///
623  unsigned getMaxCallFrameSize() const {
624  // TODO: Enable this assert when targets are fixed.
625  //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
626  if (!isMaxCallFrameSizeComputed())
627  return 0;
628  return MaxCallFrameSize;
629  }
631  return MaxCallFrameSize != ~0u;
632  }
633  void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
634 
635  /// Returns how many bytes of callee-saved registers the target pushed in the
636  /// prologue. Only used for debug info.
638  return CVBytesOfCalleeSavedRegisters;
639  }
641  CVBytesOfCalleeSavedRegisters = S;
642  }
643 
644  /// Create a new object at a fixed location on the stack.
645  /// All fixed objects should be created before other objects are created for
646  /// efficiency. By default, fixed objects are not pointed to by LLVM IR
647  /// values. This returns an index with a negative value.
648  int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
649  bool isAliased = false);
650 
651  /// Create a spill slot at a fixed location on the stack.
652  /// Returns an index with a negative value.
653  int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
654  bool IsImmutable = false);
655 
656  /// Returns true if the specified index corresponds to a fixed stack object.
657  bool isFixedObjectIndex(int ObjectIdx) const {
658  return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
659  }
660 
661  /// Returns true if the specified index corresponds
662  /// to an object that might be pointed to by an LLVM IR value.
663  bool isAliasedObjectIndex(int ObjectIdx) const {
664  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
665  "Invalid Object Idx!");
666  return Objects[ObjectIdx+NumFixedObjects].isAliased;
667  }
668 
669  /// Returns true if the specified index corresponds to an immutable object.
670  bool isImmutableObjectIndex(int ObjectIdx) const {
671  // Tail calling functions can clobber their function arguments.
672  if (HasTailCall)
673  return false;
674  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
675  "Invalid Object Idx!");
676  return Objects[ObjectIdx+NumFixedObjects].isImmutable;
677  }
678 
679  /// Marks the immutability of an object.
680  void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
681  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
682  "Invalid Object Idx!");
683  Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
684  }
685 
686  /// Returns true if the specified index corresponds to a spill slot.
687  bool isSpillSlotObjectIndex(int ObjectIdx) const {
688  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
689  "Invalid Object Idx!");
690  return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
691  }
692 
693  bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
694  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
695  "Invalid Object Idx!");
696  return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
697  }
698 
699  /// \see StackID
700  uint8_t getStackID(int ObjectIdx) const {
701  return Objects[ObjectIdx+NumFixedObjects].StackID;
702  }
703 
704  /// \see StackID
705  void setStackID(int ObjectIdx, uint8_t ID) {
706  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
707  "Invalid Object Idx!");
708  Objects[ObjectIdx+NumFixedObjects].StackID = ID;
709  // If ID > 0, MaxAlignment may now be overly conservative.
710  // If ID == 0, MaxAlignment will need to be updated separately.
711  }
712 
713  /// Returns true if the specified index corresponds to a dead object.
714  bool isDeadObjectIndex(int ObjectIdx) const {
715  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
716  "Invalid Object Idx!");
717  return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
718  }
719 
720  /// Returns true if the specified index corresponds to a variable sized
721  /// object.
722  bool isVariableSizedObjectIndex(int ObjectIdx) const {
723  assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
724  "Invalid Object Idx!");
725  return Objects[ObjectIdx + NumFixedObjects].Size == 0;
726  }
727 
729  assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
730  "Invalid Object Idx!");
731  Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
732  assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
733  }
734 
735  /// Create a new statically sized stack object, returning
736  /// a nonnegative identifier to represent it.
737  int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot,
738  const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
739  /// FIXME: Remove this function when transition to Align is over.
740  inline int CreateStackObject(uint64_t Size, unsigned Alignment,
741  bool isSpillSlot,
742  const AllocaInst *Alloca = nullptr,
743  uint8_t ID = 0) {
744  return CreateStackObject(Size, assumeAligned(Alignment), isSpillSlot,
745  Alloca, ID);
746  }
747 
748  /// Create a new statically sized stack object that represents a spill slot,
749  /// returning a nonnegative identifier to represent it.
750  int CreateSpillStackObject(uint64_t Size, Align Alignment);
751  /// FIXME: Remove this function when transition to Align is over.
752  inline int CreateSpillStackObject(uint64_t Size, unsigned Alignment) {
753  return CreateSpillStackObject(Size, assumeAligned(Alignment));
754  }
755 
756  /// Remove or mark dead a statically sized stack object.
757  void RemoveStackObject(int ObjectIdx) {
758  // Mark it dead.
759  Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
760  }
761 
762  /// Notify the MachineFrameInfo object that a variable sized object has been
763  /// created. This must be created whenever a variable sized object is
764  /// created, whether or not the index returned is actually used.
765  int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca);
766  /// FIXME: Remove this function when transition to Align is over.
767  int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca) {
768  return CreateVariableSizedObject(assumeAligned(Alignment), Alloca);
769  }
770 
771  /// Returns a reference to call saved info vector for the current function.
772  const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
773  return CSInfo;
774  }
775  /// \copydoc getCalleeSavedInfo()
776  std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
777 
778  /// Used by prolog/epilog inserter to set the function's callee saved
779  /// information.
780  void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
781  CSInfo = CSI;
782  }
783 
784  /// Has the callee saved info been calculated yet?
785  bool isCalleeSavedInfoValid() const { return CSIValid; }
786 
787  void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
788 
789  MachineBasicBlock *getSavePoint() const { return Save; }
790  void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
791  MachineBasicBlock *getRestorePoint() const { return Restore; }
792  void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
793 
794  /// Return a set of physical registers that are pristine.
795  ///
796  /// Pristine registers hold a value that is useless to the current function,
797  /// but that must be preserved - they are callee saved registers that are not
798  /// saved.
799  ///
800  /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
801  /// method always returns an empty set.
802  BitVector getPristineRegs(const MachineFunction &MF) const;
803 
804  /// Used by the MachineFunction printer to print information about
805  /// stack objects. Implemented in MachineFunction.cpp.
806  void print(const MachineFunction &MF, raw_ostream &OS) const;
807 
808  /// dump - Print the function to stderr.
809  void dump(const MachineFunction &MF) const;
810 };
811 
812 } // End llvm namespace
813 
814 #endif
void setHasStackMap(bool s=true)
void setFrameAddressIsTaken(bool T)
unsigned getNumFixedObjects() const
Return the number of fixed objects.
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)
bool isSpilledToReg() const
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)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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.
void setDstReg(unsigned SpillReg)
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
block Block Frequency true
void ensureMaxAlignment(unsigned Align)
FIXME: Remove this once transition to Align is over.
unsigned getDstReg() const
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.
void setLocalFrameMaxAlign(Align Alignment)
Required alignment of the local object blob, which is the strictest alignment of any object in it...
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)
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.
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)
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:86
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.
int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
FIXME: Remove this function when transition to Align is over.
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:52
int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca)
FIXME: Remove this function when transition to Align is over.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
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:837
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)
int getOffsetAdjustment() const
Return the correction for frame offsets.
void setCVBytesOfCalleeSavedRegisters(unsigned S)
uint32_t Size
Definition: Profile.cpp:46
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
int CreateSpillStackObject(uint64_t Size, unsigned Alignment)
FIXME: Remove this function when transition to Align is over.
uint8_t getStackID(int ObjectIdx) const
Align getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
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:45
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...
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition: Alignment.h:111
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:59