LLVM  3.7.0
CallingConvLower.cpp
Go to the documentation of this file.
1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
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 // This file implements the CCState class, used for lowering and implementing
11 // calling conventions.
12 //
13 //===----------------------------------------------------------------------===//
14 
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/Support/Debug.h"
26 using namespace llvm;
27 
30  : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
31  TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C),
32  CallOrPrologue(Unknown) {
33  // No stack is used.
34  StackOffset = 0;
35 
37  UsedRegs.resize((TRI.getNumRegs()+31)/32);
38 }
39 
40 /// Allocate space on the stack large enough to pass an argument by value.
41 /// The size and alignment information of the argument is encoded in
42 /// its parameter attribute.
43 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
44  MVT LocVT, CCValAssign::LocInfo LocInfo,
45  int MinSize, int MinAlign,
46  ISD::ArgFlagsTy ArgFlags) {
47  unsigned Align = ArgFlags.getByValAlign();
48  unsigned Size = ArgFlags.getByValSize();
49  if (MinSize > (int)Size)
50  Size = MinSize;
51  if (MinAlign > (int)Align)
52  Align = MinAlign;
53  MF.getFrameInfo()->ensureMaxAlignment(Align);
54  MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
55  Size = unsigned(RoundUpToAlignment(Size, MinAlign));
56  unsigned Offset = AllocateStack(Size, Align);
57  addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
58 }
59 
60 /// Mark a register and all of its aliases as allocated.
61 void CCState::MarkAllocated(unsigned Reg) {
62  for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
63  UsedRegs[*AI/32] |= 1 << (*AI&31);
64 }
65 
66 /// Analyze an array of argument values,
67 /// incorporating info about the formals into this state.
68 void
70  CCAssignFn Fn) {
71  unsigned NumArgs = Ins.size();
72 
73  for (unsigned i = 0; i != NumArgs; ++i) {
74  MVT ArgVT = Ins[i].VT;
75  ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
76  if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
77 #ifndef NDEBUG
78  dbgs() << "Formal argument #" << i << " has unhandled type "
79  << EVT(ArgVT).getEVTString() << '\n';
80 #endif
81  llvm_unreachable(nullptr);
82  }
83  }
84 }
85 
86 /// Analyze the return values of a function, returning true if the return can
87 /// be performed without sret-demotion and false otherwise.
89  CCAssignFn Fn) {
90  // Determine which register each value should be copied into.
91  for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
92  MVT VT = Outs[i].VT;
93  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
94  if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
95  return false;
96  }
97  return true;
98 }
99 
100 /// Analyze the returned values of a return,
101 /// incorporating info about the result values into this state.
103  CCAssignFn Fn) {
104  // Determine which register each value should be copied into.
105  for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
106  MVT VT = Outs[i].VT;
107  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
108  if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
109 #ifndef NDEBUG
110  dbgs() << "Return operand #" << i << " has unhandled type "
111  << EVT(VT).getEVTString() << '\n';
112 #endif
113  llvm_unreachable(nullptr);
114  }
115  }
116 }
117 
118 /// Analyze the outgoing arguments to a call,
119 /// incorporating info about the passed values into this state.
121  CCAssignFn Fn) {
122  unsigned NumOps = Outs.size();
123  for (unsigned i = 0; i != NumOps; ++i) {
124  MVT ArgVT = Outs[i].VT;
125  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
126  if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
127 #ifndef NDEBUG
128  dbgs() << "Call operand #" << i << " has unhandled type "
129  << EVT(ArgVT).getEVTString() << '\n';
130 #endif
131  llvm_unreachable(nullptr);
132  }
133  }
134 }
135 
136 /// Same as above except it takes vectors of types and argument flags.
139  CCAssignFn Fn) {
140  unsigned NumOps = ArgVTs.size();
141  for (unsigned i = 0; i != NumOps; ++i) {
142  MVT ArgVT = ArgVTs[i];
143  ISD::ArgFlagsTy ArgFlags = Flags[i];
144  if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
145 #ifndef NDEBUG
146  dbgs() << "Call operand #" << i << " has unhandled type "
147  << EVT(ArgVT).getEVTString() << '\n';
148 #endif
149  llvm_unreachable(nullptr);
150  }
151  }
152 }
153 
154 /// Analyze the return values of a call, incorporating info about the passed
155 /// values into this state.
157  CCAssignFn Fn) {
158  for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
159  MVT VT = Ins[i].VT;
160  ISD::ArgFlagsTy Flags = Ins[i].Flags;
161  if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
162 #ifndef NDEBUG
163  dbgs() << "Call result #" << i << " has unhandled type "
164  << EVT(VT).getEVTString() << '\n';
165 #endif
166  llvm_unreachable(nullptr);
167  }
168  }
169 }
170 
171 /// Same as above except it's specialized for calls that produce a single value.
173  if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
174 #ifndef NDEBUG
175  dbgs() << "Call result has unhandled type "
176  << EVT(VT).getEVTString() << '\n';
177 #endif
178  llvm_unreachable(nullptr);
179  }
180 }
181 
183  if (VT.isVector())
184  return true; // Assume -msse-regparm might be in effect.
185  if (!VT.isInteger())
186  return false;
188  return true;
189  return false;
190 }
191 
193  MVT VT, CCAssignFn Fn) {
194  unsigned SavedStackOffset = StackOffset;
195  unsigned NumLocs = Locs.size();
196 
197  // Set the 'inreg' flag if it is used for this calling convention.
199  if (isValueTypeInRegForCC(CallingConv, VT))
200  Flags.setInReg();
201 
202  // Allocate something of this value type repeatedly until we get assigned a
203  // location in memory.
204  bool HaveRegParm = true;
205  while (HaveRegParm) {
206  if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
207 #ifndef NDEBUG
208  dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
209  << " while computing remaining regparms\n";
210 #endif
211  llvm_unreachable(nullptr);
212  }
213  HaveRegParm = Locs.back().isRegLoc();
214  }
215 
216  // Copy all the registers from the value locations we added.
217  assert(NumLocs < Locs.size() && "CC assignment failed to add location");
218  for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
219  if (Locs[I].isRegLoc())
220  Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
221 
222  // Clear the assigned values and stack memory. We leave the registers marked
223  // as allocated so that future queries don't return the same registers, i.e.
224  // when i64 and f64 are both passed in GPRs.
225  StackOffset = SavedStackOffset;
226  Locs.resize(NumLocs);
227 }
228 
230  SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
231  CCAssignFn Fn) {
232  // Oftentimes calling conventions will not user register parameters for
233  // variadic functions, so we need to assume we're not variadic so that we get
234  // all the registers that might be used in a non-variadic call.
235  SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
236 
237  for (MVT RegVT : RegParmTypes) {
238  SmallVector<MCPhysReg, 8> RemainingRegs;
239  getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
240  const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
241  const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
242  for (MCPhysReg PReg : RemainingRegs) {
243  unsigned VReg = MF.addLiveIn(PReg, RC);
244  Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
245  }
246  }
247 }
void AnalyzeCallResult(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeCallResult - Analyze the return values of a call, incorporating info about the passed values i...
void push_back(const T &Elt)
Definition: SmallVector.h:222
Describes a register that needs to be forwarded from the prologue to a musttail call.
unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
void AnalyzeFormalArguments(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeFormalArguments - Analyze an array of argument values, incorporating info about the formals in...
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change...
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
unsigned getByValSize() const
bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &ArgsFlags, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
virtual void HandleByVal(CCState *, unsigned &, unsigned) const
Target-specific cleanup for formal ByVal parameters.
std::string getEVTString() const
getEVTString - This function returns value type as a string, e.g.
Definition: ValueTypes.cpp:106
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
void addLoc(const CCValAssign &V)
Reg
All possible values of the reg field in the ModR/M byte.
static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
bool isInteger() const
isInteger - Return true if this is an integer, or a vector integer type.
MVT - Machine Value Type.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
bool isVector() const
isVector - Return true if this is a vector value type.
MCRegAliasIterator enumerates all registers aliasing Reg.
EVT - Extended Value Type.
Definition: ValueTypes.h:31
void HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags)
Allocate space on the stack large enough to pass an argument by value.
void getRemainingRegParmsForType(SmallVectorImpl< MCPhysReg > &Regs, MVT VT, CCAssignFn Fn)
Compute the remaining unused register parameters that would be used for the given value type...
unsigned getByValAlign() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
virtual const TargetLowering * getTargetLowering() const
MachineFrameInfo * getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
X86_FastCall - 'fast' analog of X86_StdCall.
Definition: CallingConv.h:85
CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, SmallVectorImpl< CCValAssign > &locs, LLVMContext &C)
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
virtual const TargetRegisterClass * getRegClassFor(MVT VT) const
Return the register class that should be used for the specified value type.
void AnalyzeCallOperands(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeCallOperands - Analyze the outgoing arguments to a call, incorporating info about the passed v...
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:609
A utility class that uses RAII to save and restore the value of a variable.
void analyzeMustTailForwardedRegisters(SmallVectorImpl< ForwardedRegister > &Forwards, ArrayRef< MVT > RegParmTypes, CCAssignFn Fn)
Compute the set of registers that need to be preserved and forwarded to any musttail calls...
uint64_t MinAlign(uint64_t A, uint64_t B)
MinAlign - A and B are either alignments or offsets.
Definition: MathExtras.h:552
void ensureMaxAlignment(unsigned Align)
Make sure the function is at least Align bytes aligned.
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:147
#define I(x, y, z)
Definition: MD5.cpp:54
void AnalyzeReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeReturn - Analyze the returned values of a return, incorporating info about the result values i...
static CCValAssign getMem(unsigned ValNo, MVT ValVT, unsigned Offset, MVT LocVT, LocInfo HTP)
void clearByValRegsInfo()
This file provides utility classes that use RAII to save and restore values.
unsigned AllocateStack(unsigned Size, unsigned Align)
AllocateStack - Allocate a chunk of stack space with the specified size and alignment.
This file describes how to lower LLVM code to machine code.
void resize(size_type N)
Definition: SmallVector.h:376