LLVM  14.0.0git
CallingConvLower.cpp
Go to the documentation of this file.
1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
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 // This file implements the CCState class, used for lowering and implementing
10 // calling conventions.
11 //
12 //===----------------------------------------------------------------------===//
13 
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/Support/Debug.h"
26 #include <algorithm>
27 
28 using namespace llvm;
29 
32  : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
33  TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C) {
34  // No stack is used.
35  StackOffset = 0;
36 
38  UsedRegs.resize((TRI.getNumRegs()+31)/32);
39 }
40 
41 /// Allocate space on the stack large enough to pass an argument by value.
42 /// The size and alignment information of the argument is encoded in
43 /// its parameter attribute.
44 void CCState::HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT,
45  CCValAssign::LocInfo LocInfo, int MinSize,
46  Align MinAlign, ISD::ArgFlagsTy ArgFlags) {
47  Align Alignment = ArgFlags.getNonZeroByValAlign();
48  unsigned Size = ArgFlags.getByValSize();
49  if (MinSize > (int)Size)
50  Size = MinSize;
51  if (MinAlign > Alignment)
52  Alignment = MinAlign;
53  ensureMaxAlignment(Alignment);
54  MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Alignment);
55  Size = unsigned(alignTo(Size, MinAlign));
56  unsigned Offset = AllocateStack(Size, Alignment);
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(MCPhysReg Reg) {
62  for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
63  UsedRegs[*AI / 32] |= 1 << (*AI & 31);
64 }
65 
66 void CCState::MarkUnallocated(MCPhysReg Reg) {
67  for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
68  UsedRegs[*AI / 32] &= ~(1 << (*AI & 31));
69 }
70 
72  if (!isAllocated(Reg))
73  return false;
74 
75  for (auto const &ValAssign : Locs) {
76  if (ValAssign.isRegLoc()) {
77  for (MCRegAliasIterator AI(ValAssign.getLocReg(), &TRI, true);
78  AI.isValid(); ++AI) {
79  if (*AI == Reg)
80  return false;
81  }
82  }
83  }
84  return true;
85 }
86 
87 /// Analyze an array of argument values,
88 /// incorporating info about the formals into this state.
89 void
91  CCAssignFn Fn) {
92  unsigned NumArgs = Ins.size();
93 
94  for (unsigned i = 0; i != NumArgs; ++i) {
95  MVT ArgVT = Ins[i].VT;
96  ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
97  if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this))
98  report_fatal_error("unable to allocate function argument #" + Twine(i));
99  }
100 }
101 
102 /// Analyze the return values of a function, returning true if the return can
103 /// be performed without sret-demotion and false otherwise.
105  CCAssignFn Fn) {
106  // Determine which register each value should be copied into.
107  for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
108  MVT VT = Outs[i].VT;
109  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
110  if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
111  return false;
112  }
113  return true;
114 }
115 
116 /// Analyze the returned values of a return,
117 /// incorporating info about the result values into this state.
119  CCAssignFn Fn) {
120  // Determine which register each value should be copied into.
121  for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
122  MVT VT = Outs[i].VT;
123  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
124  if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
125  report_fatal_error("unable to allocate function return #" + Twine(i));
126  }
127 }
128 
129 /// Analyze the outgoing arguments to a call,
130 /// incorporating info about the passed values into this state.
132  CCAssignFn Fn) {
133  unsigned NumOps = Outs.size();
134  for (unsigned i = 0; i != NumOps; ++i) {
135  MVT ArgVT = Outs[i].VT;
136  ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
137  if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
138 #ifndef NDEBUG
139  dbgs() << "Call operand #" << i << " has unhandled type "
140  << EVT(ArgVT).getEVTString() << '\n';
141 #endif
142  llvm_unreachable(nullptr);
143  }
144  }
145 }
146 
147 /// Same as above except it takes vectors of types and argument flags.
150  CCAssignFn Fn) {
151  unsigned NumOps = ArgVTs.size();
152  for (unsigned i = 0; i != NumOps; ++i) {
153  MVT ArgVT = ArgVTs[i];
154  ISD::ArgFlagsTy ArgFlags = Flags[i];
155  if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
156 #ifndef NDEBUG
157  dbgs() << "Call operand #" << i << " has unhandled type "
158  << EVT(ArgVT).getEVTString() << '\n';
159 #endif
160  llvm_unreachable(nullptr);
161  }
162  }
163 }
164 
165 /// Analyze the return values of a call, incorporating info about the passed
166 /// values into this state.
168  CCAssignFn Fn) {
169  for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
170  MVT VT = Ins[i].VT;
171  ISD::ArgFlagsTy Flags = Ins[i].Flags;
172  if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
173 #ifndef NDEBUG
174  dbgs() << "Call result #" << i << " has unhandled type "
175  << EVT(VT).getEVTString() << '\n';
176 #endif
177  llvm_unreachable(nullptr);
178  }
179  }
180 }
181 
182 /// Same as above except it's specialized for calls that produce a single value.
184  if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
185 #ifndef NDEBUG
186  dbgs() << "Call result has unhandled type "
187  << EVT(VT).getEVTString() << '\n';
188 #endif
189  llvm_unreachable(nullptr);
190  }
191 }
192 
194  if (!AnalyzingMustTailForwardedRegs)
195  MF.getFrameInfo().ensureMaxAlignment(Alignment);
196 }
197 
199  if (VT.isVector())
200  return true; // Assume -msse-regparm might be in effect.
201  if (!VT.isInteger())
202  return false;
204 }
205 
207  MVT VT, CCAssignFn Fn) {
208  unsigned SavedStackOffset = StackOffset;
209  Align SavedMaxStackArgAlign = MaxStackArgAlign;
210  unsigned NumLocs = Locs.size();
211 
212  // Set the 'inreg' flag if it is used for this calling convention.
213  ISD::ArgFlagsTy Flags;
214  if (isValueTypeInRegForCC(CallingConv, VT))
215  Flags.setInReg();
216 
217  // Allocate something of this value type repeatedly until we get assigned a
218  // location in memory.
219  bool HaveRegParm;
220  do {
221  if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
222 #ifndef NDEBUG
223  dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
224  << " while computing remaining regparms\n";
225 #endif
226  llvm_unreachable(nullptr);
227  }
228  HaveRegParm = Locs.back().isRegLoc();
229  } while (HaveRegParm);
230 
231  // Copy all the registers from the value locations we added.
232  assert(NumLocs < Locs.size() && "CC assignment failed to add location");
233  for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
234  if (Locs[I].isRegLoc())
235  Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
236 
237  // Clear the assigned values and stack memory. We leave the registers marked
238  // as allocated so that future queries don't return the same registers, i.e.
239  // when i64 and f64 are both passed in GPRs.
240  StackOffset = SavedStackOffset;
241  MaxStackArgAlign = SavedMaxStackArgAlign;
242  Locs.resize(NumLocs);
243 }
244 
246  SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
247  CCAssignFn Fn) {
248  // Oftentimes calling conventions will not user register parameters for
249  // variadic functions, so we need to assume we're not variadic so that we get
250  // all the registers that might be used in a non-variadic call.
251  SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
252  SaveAndRestore<bool> SavedMustTail(AnalyzingMustTailForwardedRegs, true);
253 
254  for (MVT RegVT : RegParmTypes) {
255  SmallVector<MCPhysReg, 8> RemainingRegs;
256  getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
257  const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
258  const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
259  for (MCPhysReg PReg : RemainingRegs) {
260  Register VReg = MF.addLiveIn(PReg, RC);
261  Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
262  }
263  }
264 }
265 
267  CallingConv::ID CallerCC, MachineFunction &MF,
268  LLVMContext &C,
270  CCAssignFn CalleeFn, CCAssignFn CallerFn) {
271  if (CalleeCC == CallerCC)
272  return true;
274  CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
275  CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
276 
278  CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
279  CCInfo2.AnalyzeCallResult(Ins, CallerFn);
280 
281  if (RVLocs1.size() != RVLocs2.size())
282  return false;
283  for (unsigned I = 0, E = RVLocs1.size(); I != E; ++I) {
284  const CCValAssign &Loc1 = RVLocs1[I];
285  const CCValAssign &Loc2 = RVLocs2[I];
286 
287  if ( // Must both be in registers, or both in memory
288  Loc1.isRegLoc() != Loc2.isRegLoc() ||
289  // Must fill the same part of their locations
290  Loc1.getLocInfo() != Loc2.getLocInfo() ||
291  // Memory offset/register number must be the same
292  Loc1.getExtraInfo() != Loc2.getExtraInfo())
293  return false;
294  }
295  return true;
296 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::TargetLowering::HandleByVal
virtual void HandleByVal(CCState *, unsigned &, Align) const
Target-specific cleanup for formal ByVal parameters.
Definition: TargetLowering.h:3969
llvm::CCValAssign::Full
@ Full
Definition: CallingConvLower.h:36
llvm::MVT::isInteger
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: MachineValueType.h:350
llvm::CCValAssign::getExtraInfo
unsigned getExtraInfo() const
Definition: CallingConvLower.h:152
llvm::CCState
CCState - This class holds information needed while lowering arguments and return values.
Definition: CallingConvLower.h:191
llvm::CCState::addLoc
void addLoc(const CCValAssign &V)
Definition: CallingConvLower.h:253
llvm::SmallVector< MCPhysReg, 8 >
llvm::MVT::isVector
bool isVector() const
Return true if this is a vector value type.
Definition: MachineValueType.h:366
ErrorHandling.h
llvm::MCRegisterInfo::getNumRegs
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
Definition: MCRegisterInfo.h:491
isValueTypeInRegForCC
static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT)
Definition: CallingConvLower.cpp:198
llvm::CCState::AnalyzeFormalArguments
void AnalyzeFormalArguments(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeFormalArguments - Analyze an array of argument values, incorporating info about the formals in...
Definition: CallingConvLower.cpp:90
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::CCState::clearByValRegsInfo
void clearByValRegsInfo()
Definition: CallingConvLower.h:487
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::CCState::AnalyzeCallOperands
void AnalyzeCallOperands(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeCallOperands - Analyze the outgoing arguments to a call, incorporating info about the passed v...
Definition: CallingConvLower.cpp:131
MachineRegisterInfo.h
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::CCState::analyzeMustTailForwardedRegisters
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.
Definition: CallingConvLower.cpp:245
TargetLowering.h
llvm::MinAlign
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:672
llvm::CCValAssign
CCValAssign - Represent assignment of one arg/retval to a location.
Definition: CallingConvLower.h:33
llvm::ms_demangle::CallingConv
CallingConv
Definition: MicrosoftDemangleNodes.h:59
llvm::CCState::IsShadowAllocatedReg
bool IsShadowAllocatedReg(MCRegister Reg) const
A shadow allocated register is a register that was allocated but wasn't added to the location list (L...
Definition: CallingConvLower.cpp:71
llvm::MachineFrameInfo::ensureMaxAlignment
void ensureMaxAlignment(Align Alignment)
Make sure the function is at least Align bytes aligned.
Definition: MachineFrameInfo.cpp:31
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::EVT
Extended Value Type.
Definition: ValueTypes.h:35
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::TargetLowering
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Definition: TargetLowering.h:3189
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:46
llvm::CCState::AnalyzeReturn
void AnalyzeReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeReturn - Analyze the returned values of a return, incorporating info about the result values i...
Definition: CallingConvLower.cpp:118
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:606
llvm::ISD::ArgFlagsTy::setInReg
void setInReg()
Definition: TargetCallingConv.h:80
llvm::CCValAssign::getLocInfo
LocInfo getLocInfo() const
Definition: CallingConvLower.h:155
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::CCValAssign::isRegLoc
bool isRegLoc() const
Definition: CallingConvLower.h:145
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::CCState::ensureMaxAlignment
void ensureMaxAlignment(Align Alignment)
Definition: CallingConvLower.cpp:193
llvm::CCAssignFn
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.
Definition: CallingConvLower.h:177
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:626
llvm::CCValAssign::LocInfo
LocInfo
Definition: CallingConvLower.h:35
SaveAndRestore.h
llvm::ForwardedRegister
Describes a register that needs to be forwarded from the prologue to a musttail call.
Definition: CallingConvLower.h:167
llvm::CCState::CCState
CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, SmallVectorImpl< CCValAssign > &locs, LLVMContext &C)
Definition: CallingConvLower.cpp:30
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::MCPhysReg
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition: MCRegister.h:20
llvm::CCValAssign::getMem
static CCValAssign getMem(unsigned ValNo, MVT ValVT, unsigned Offset, MVT LocVT, LocInfo HTP)
Definition: CallingConvLower.h:102
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:642
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:31
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::CCState::resultsCompatible
static bool resultsCompatible(CallingConv::ID CalleeCC, CallingConv::ID CallerCC, MachineFunction &MF, LLVMContext &C, const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn CalleeFn, CCAssignFn CallerFn)
Returns true if the results of the two calling conventions are compatible.
Definition: CallingConvLower.cpp:266
llvm::MachineFunction::addLiveIn
Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
Definition: MachineFunction.cpp:653
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
DataLayout.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::StackOffset
StackOffset is a class to represent an offset with 2 dimensions, named fixed and scalable,...
Definition: TypeSize.h:134
llvm::ISD::ArgFlagsTy
Definition: TargetCallingConv.h:27
TargetSubtargetInfo.h
llvm::CallingConv::X86_VectorCall
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:173
llvm::SaveAndRestore
A utility class that uses RAII to save and restore the value of a variable.
Definition: SaveAndRestore.h:21
llvm::CCState::CheckReturn
bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
Definition: CallingConvLower.cpp:104
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::EVT::getEVTString
std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
Definition: ValueTypes.cpp:151
llvm::CCState::AnalyzeCallResult
void AnalyzeCallResult(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeCallResult - Analyze the return values of a call, incorporating info about the passed values i...
Definition: CallingConvLower.cpp:167
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::ISD::ArgFlagsTy::getByValSize
unsigned getByValSize() const
Definition: TargetCallingConv.h:169
uint16_t
CallingConvLower.h
MachineFrameInfo.h
llvm::CCState::AllocateStack
unsigned AllocateStack(unsigned Size, Align Alignment)
AllocateStack - Allocate a chunk of stack space with the specified size and alignment.
Definition: CallingConvLower.h:425
llvm::CCState::getRemainingRegParmsForType
void getRemainingRegParmsForType(SmallVectorImpl< MCPhysReg > &Regs, MVT VT, CCAssignFn Fn)
Compute the remaining unused register parameters that would be used for the given value type.
Definition: CallingConvLower.cpp:206
llvm::CCState::HandleByVal
void HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, int MinSize, Align MinAlign, ISD::ArgFlagsTy ArgFlags)
Allocate space on the stack large enough to pass an argument by value.
Definition: CallingConvLower.cpp:44
llvm::MCRegAliasIterator::isValid
bool isValid() const
Definition: MCRegisterInfo.h:805
llvm::CallingConv::X86_FastCall
@ X86_FastCall
X86_FastCall - 'fast' analog of X86_StdCall.
Definition: CallingConv.h:107
llvm::TargetSubtargetInfo::getTargetLowering
virtual const TargetLowering * getTargetLowering() const
Definition: TargetSubtargetInfo.h:96
llvm::ISD::ArgFlagsTy::getNonZeroByValAlign
Align getNonZeroByValAlign() const
Definition: TargetCallingConv.h:153
llvm::TargetLoweringBase::getRegClassFor
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
Definition: TargetLowering.h:852
llvm::MipsISD::Ins
@ Ins
Definition: MipsISelLowering.h:157
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
raw_ostream.h
MachineFunction.h
TargetRegisterInfo.h
Debug.h
llvm::CCState::isAllocated
bool isAllocated(MCRegister Reg) const
isAllocated - Return true if the specified register (or an alias) is allocated.
Definition: CallingConvLower.h:277
llvm::MCRegAliasIterator
MCRegAliasIterator enumerates all registers aliasing Reg.
Definition: MCRegisterInfo.h:780
llvm::MCRegister
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:23