LLVM  8.0.1
MachineFrameInfo.cpp
Go to the documentation of this file.
1 //===-- MachineFrameInfo.cpp ---------------------------------------------===//
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 /// \file Implements MachineFrameInfo that manages the stack frame.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
16 #include "llvm/ADT/BitVector.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/Support/Debug.h"
26 #include <cassert>
27 
28 #define DEBUG_TYPE "codegen"
29 
30 using namespace llvm;
31 
33  if (!StackRealignable)
34  assert(Align <= StackAlignment &&
35  "For targets without stack realignment, Align is out of limit!");
36  if (MaxAlignment < Align) MaxAlignment = Align;
37 }
38 
39 /// Clamp the alignment if requested and emit a warning.
40 static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
41  unsigned StackAlign) {
42  if (!ShouldClamp || Align <= StackAlign)
43  return Align;
44  LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align
45  << " exceeds the stack alignment " << StackAlign
46  << " when stack realignment is off" << '\n');
47  return StackAlign;
48 }
49 
50 int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
51  bool IsSpillSlot,
52  const AllocaInst *Alloca,
53  uint8_t StackID) {
54  assert(Size != 0 && "Cannot allocate zero size stack objects!");
55  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
56  Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
57  !IsSpillSlot, StackID));
58  int Index = (int)Objects.size() - NumFixedObjects - 1;
59  assert(Index >= 0 && "Bad frame index!");
60  ensureMaxAlignment(Alignment);
61  return Index;
62 }
63 
65  unsigned Alignment) {
66  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
67  CreateStackObject(Size, Alignment, true);
68  int Index = (int)Objects.size() - NumFixedObjects - 1;
69  ensureMaxAlignment(Alignment);
70  return Index;
71 }
72 
74  const AllocaInst *Alloca) {
75  HasVarSizedObjects = true;
76  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
77  Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
78  ensureMaxAlignment(Alignment);
79  return (int)Objects.size()-NumFixedObjects-1;
80 }
81 
82 int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
83  bool IsImmutable, bool IsAliased) {
84  assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
85  // The alignment of the frame index can be determined from its offset from
86  // the incoming frame position. If the frame object is at offset 32 and
87  // the stack is guaranteed to be 16-byte aligned, then we know that the
88  // object is 16-byte aligned. Note that unlike the non-fixed case, if the
89  // stack needs realignment, we can't assume that the stack will in fact be
90  // aligned.
91  unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
92  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
93  Objects.insert(Objects.begin(),
94  StackObject(Size, Alignment, SPOffset, IsImmutable,
95  /*isSpillSlot=*/false, /*Alloca=*/nullptr,
96  IsAliased));
97  return -++NumFixedObjects;
98 }
99 
101  int64_t SPOffset,
102  bool IsImmutable) {
103  unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
104  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
105  Objects.insert(Objects.begin(),
106  StackObject(Size, Alignment, SPOffset, IsImmutable,
107  /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
108  /*IsAliased=*/false));
109  return -++NumFixedObjects;
110 }
111 
114  BitVector BV(TRI->getNumRegs());
115 
116  // Before CSI is calculated, no registers are considered pristine. They can be
117  // freely used and PEI will make sure they are saved.
118  if (!isCalleeSavedInfoValid())
119  return BV;
120 
121  const MachineRegisterInfo &MRI = MF.getRegInfo();
122  for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
123  ++CSR)
124  BV.set(*CSR);
125 
126  // Saved CSRs are not pristine.
127  for (auto &I : getCalleeSavedInfo())
128  for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
129  BV.reset(*S);
130 
131  return BV;
132 }
133 
136  const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
137  unsigned MaxAlign = getMaxAlignment();
138  int Offset = 0;
139 
140  // This code is very, very similar to PEI::calculateFrameObjectOffsets().
141  // It really should be refactored to share code. Until then, changes
142  // should keep in mind that there's tight coupling between the two.
143 
144  for (int i = getObjectIndexBegin(); i != 0; ++i) {
145  int FixedOff = -getObjectOffset(i);
146  if (FixedOff > Offset) Offset = FixedOff;
147  }
148  for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
149  if (isDeadObjectIndex(i))
150  continue;
151  Offset += getObjectSize(i);
152  unsigned Align = getObjectAlignment(i);
153  // Adjust to alignment boundary
154  Offset = (Offset+Align-1)/Align*Align;
155 
156  MaxAlign = std::max(Align, MaxAlign);
157  }
158 
159  if (adjustsStack() && TFI->hasReservedCallFrame(MF))
160  Offset += getMaxCallFrameSize();
161 
162  // Round up the size to a multiple of the alignment. If the function has
163  // any calls or alloca's, align to the target's StackAlignment value to
164  // ensure that the callee's frame or the alloca data is suitably aligned;
165  // otherwise, for leaf functions, align to the TransientStackAlignment
166  // value.
167  unsigned StackAlign;
168  if (adjustsStack() || hasVarSizedObjects() ||
169  (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
170  StackAlign = TFI->getStackAlignment();
171  else
172  StackAlign = TFI->getTransientStackAlignment();
173 
174  // If the frame pointer is eliminated, all frame offsets will be relative to
175  // SP not FP. Align to MaxAlign so this works.
176  StackAlign = std::max(StackAlign, MaxAlign);
177  unsigned AlignMask = StackAlign - 1;
178  Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
179 
180  return (unsigned)Offset;
181 }
182 
184  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
185  unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
186  unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
187  assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
188  "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
189 
190  MaxCallFrameSize = 0;
191  for (const MachineBasicBlock &MBB : MF) {
192  for (const MachineInstr &MI : MBB) {
193  unsigned Opcode = MI.getOpcode();
194  if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
195  unsigned Size = TII.getFrameSize(MI);
196  MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
197  AdjustsStack = true;
198  } else if (MI.isInlineAsm()) {
199  // Some inline asm's need a stack frame, as indicated by operand 1.
200  unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
201  if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
202  AdjustsStack = true;
203  }
204  }
205  }
206 }
207 
209  if (Objects.empty()) return;
210 
212  int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
213 
214  OS << "Frame Objects:\n";
215 
216  for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
217  const StackObject &SO = Objects[i];
218  OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
219 
220  if (SO.StackID != 0)
221  OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
222 
223  if (SO.Size == ~0ULL) {
224  OS << "dead\n";
225  continue;
226  }
227  if (SO.Size == 0)
228  OS << "variable sized";
229  else
230  OS << "size=" << SO.Size;
231  OS << ", align=" << SO.Alignment;
232 
233  if (i < NumFixedObjects)
234  OS << ", fixed";
235  if (i < NumFixedObjects || SO.SPOffset != -1) {
236  int64_t Off = SO.SPOffset - ValOffset;
237  OS << ", at location [SP";
238  if (Off > 0)
239  OS << "+" << Off;
240  else if (Off < 0)
241  OS << Off;
242  OS << "]";
243  }
244  OS << "\n";
245  }
246 }
247 
248 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
250  print(MF, dbgs());
251 }
252 #endif
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void dump(const MachineFunction &MF) const
dump - Print the function to stderr.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
unsigned const TargetRegisterInfo * TRI
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
unsigned getCallFrameDestroyOpcode() const
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it...
const HexagonInstrInfo * TII
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
int getObjectIndexBegin() const
Return the minimum frame object index.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
virtual const TargetInstrInfo * getInstrInfo() const
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
TargetInstrInfo - Interface to description of machine instruction set.
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:610
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
unsigned const MachineRegisterInfo * MRI
void print(const MachineFunction &MF, raw_ostream &OS) const
Used by the MachineFunction printer to print information about stack objects.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned getCallFrameSetupOpcode() const
These methods return the opcode of the frame setup/destroy instructions if they exist (-1 otherwise)...
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
void computeMaxCallFrameSize(const MachineFunction &MF)
Computes the maximum size of a callframe and the AdjustsStack property.
int CreateSpillStackObject(uint64_t Size, unsigned Alignment)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSubRegIterator enumerates all sub-registers of Reg.
static unsigned clampStackAlignment(bool ShouldClamp, unsigned Align, unsigned StackAlign)
Clamp the alignment if requested and emit a warning.
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
unsigned estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
Information about stack frame layout on the target.
int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
int64_t getFrameSize(const MachineInstr &I) const
Returns size of the frame associated with the given frame instruction.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
unsigned getTransientStackAlignment() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
void ensureMaxAlignment(unsigned Align)
Make sure the function is at least Align bytes aligned.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
#define I(x, y, z)
Definition: MD5.cpp:58
virtual const TargetFrameLowering * getFrameLowering() const
uint32_t Size
Definition: Profile.cpp:47
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
#define LLVM_DEBUG(X)
Definition: Debug.h:123
int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca)
Notify the MachineFrameInfo object that a variable sized object has been created. ...
an instruction to allocate memory on the stack
Definition: Instructions.h:60