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