LLVM  9.0.0svn
Go to the documentation of this file.
1 //===-- lib/CodeGen/GlobalISel/CallLowering.cpp - Call lowering -----------===//
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 /// \file
10 /// This file implements some simple delegations needed for call lowering.
11 ///
12 //===----------------------------------------------------------------------===//
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
23 #define DEBUG_TYPE "call-lowering"
25 using namespace llvm;
27 void CallLowering::anchor() {}
30  unsigned ResReg, ArrayRef<unsigned> ArgRegs,
31  unsigned SwiftErrorVReg,
32  std::function<unsigned()> GetCalleeReg) const {
33  auto &DL = CS.getParent()->getParent()->getParent()->getDataLayout();
35  // First step is to marshall all the function's parameters into the correct
36  // physregs and memory locations. Gather the sequence of argument types that
37  // we'll pass to the assigner function.
38  SmallVector<ArgInfo, 8> OrigArgs;
39  unsigned i = 0;
40  unsigned NumFixedArgs = CS.getFunctionType()->getNumParams();
41  for (auto &Arg : CS.args()) {
42  ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{},
43  i < NumFixedArgs};
44  setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CS);
45  // We don't currently support swiftself args.
46  if (OrigArg.Flags.isSwiftSelf())
47  return false;
48  OrigArgs.push_back(OrigArg);
49  ++i;
50  }
53  if (const Function *F = CS.getCalledFunction())
54  Callee = MachineOperand::CreateGA(F, 0);
55  else
56  Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
58  ArgInfo OrigRet{ResReg, CS.getType(), ISD::ArgFlagsTy{}};
59  if (!OrigRet.Ty->isVoidTy())
60  setArgFlags(OrigRet, AttributeList::ReturnIndex, DL, CS);
62  return lowerCall(MIRBuilder, CS.getCallingConv(), Callee, OrigRet, OrigArgs,
63  SwiftErrorVReg);
64 }
66 template <typename FuncInfoTy>
68  const DataLayout &DL,
69  const FuncInfoTy &FuncInfo) const {
70  const AttributeList &Attrs = FuncInfo.getAttributes();
71  if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
72  Arg.Flags.setZExt();
73  if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
74  Arg.Flags.setSExt();
75  if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
76  Arg.Flags.setInReg();
77  if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
78  Arg.Flags.setSRet();
79  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
80  Arg.Flags.setSwiftSelf();
81  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
82  Arg.Flags.setSwiftError();
83  if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
84  Arg.Flags.setByVal();
85  if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
86  Arg.Flags.setInAlloca();
88  if (Arg.Flags.isByVal() || Arg.Flags.isInAlloca()) {
89  Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
91  auto Ty = Attrs.getAttribute(OpIdx, Attribute::ByVal).getValueAsType();
92  Arg.Flags.setByValSize(DL.getTypeAllocSize(Ty ? Ty : ElementTy));
94  // For ByVal, alignment should be passed from FE. BE will guess if
95  // this info is not there but there are cases it cannot get right.
96  unsigned FrameAlign;
97  if (FuncInfo.getParamAlignment(OpIdx - 2))
98  FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
99  else
100  FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
101  Arg.Flags.setByValAlign(FrameAlign);
102  }
103  if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
104  Arg.Flags.setNest();
106 }
108 template void
109 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
110  const DataLayout &DL,
111  const Function &FuncInfo) const;
113 template void
114 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
115  const DataLayout &DL,
116  const CallInst &FuncInfo) const;
120  ValueHandler &Handler) const {
121  MachineFunction &MF = MIRBuilder.getMF();
122  const Function &F = MF.getFunction();
123  const DataLayout &DL = F.getParent()->getDataLayout();
126  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
128  unsigned NumArgs = Args.size();
129  for (unsigned i = 0; i != NumArgs; ++i) {
130  MVT CurVT = MVT::getVT(Args[i].Ty);
131  if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i], CCInfo)) {
132  // Try to use the register type if we couldn't assign the VT.
133  if (!Handler.isArgumentHandler() || !CurVT.isValid())
134  return false;
135  CurVT = TLI->getRegisterTypeForCallingConv(
136  F.getContext(), F.getCallingConv(), EVT(CurVT));
137  if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i], CCInfo))
138  return false;
139  }
140  }
142  for (unsigned i = 0, e = Args.size(), j = 0; i != e; ++i, ++j) {
143  assert(j < ArgLocs.size() && "Skipped too many arg locs");
145  CCValAssign &VA = ArgLocs[j];
146  assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
148  if (VA.needsCustom()) {
149  j += Handler.assignCustomValue(Args[i], makeArrayRef(ArgLocs).slice(j));
150  continue;
151  }
153  if (VA.isRegLoc()) {
154  MVT OrigVT = MVT::getVT(Args[i].Ty);
155  MVT VAVT = VA.getValVT();
156  if (Handler.isArgumentHandler() && VAVT != OrigVT) {
157  if (VAVT.getSizeInBits() < OrigVT.getSizeInBits())
158  return false; // Can't handle this type of arg yet.
159  const LLT VATy(VAVT);
160  unsigned NewReg =
161  MIRBuilder.getMRI()->createGenericVirtualRegister(VATy);
162  Handler.assignValueToReg(NewReg, VA.getLocReg(), VA);
163  // If it's a vector type, we either need to truncate the elements
164  // or do an unmerge to get the lower block of elements.
165  if (VATy.isVector() &&
166  VATy.getNumElements() > OrigVT.getVectorNumElements()) {
167  const LLT OrigTy(OrigVT);
168  // Just handle the case where the VA type is 2 * original type.
169  if (VATy.getNumElements() != OrigVT.getVectorNumElements() * 2) {
170  LLVM_DEBUG(dbgs()
171  << "Incoming promoted vector arg has too many elts");
172  return false;
173  }
174  auto Unmerge = MIRBuilder.buildUnmerge({OrigTy, OrigTy}, {NewReg});
175  MIRBuilder.buildCopy(Args[i].Reg, Unmerge.getReg(0));
176  } else {
177  MIRBuilder.buildTrunc(Args[i].Reg, {NewReg}).getReg(0);
178  }
179  } else {
180  Handler.assignValueToReg(Args[i].Reg, VA.getLocReg(), VA);
181  }
182  } else if (VA.isMemLoc()) {
183  MVT VT = MVT::getVT(Args[i].Ty);
184  unsigned Size = VT == MVT::iPTR ? DL.getPointerSize()
185  : alignTo(VT.getSizeInBits(), 8) / 8;
186  unsigned Offset = VA.getLocMemOffset();
187  MachinePointerInfo MPO;
188  unsigned StackAddr = Handler.getStackAddress(Size, Offset, MPO);
189  Handler.assignValueToAddress(Args[i].Reg, StackAddr, Size, MPO, VA);
190  } else {
191  // FIXME: Support byvals and other weirdness
192  return false;
193  }
194  }
195  return true;
196 }
199  CCValAssign &VA) {
200  LLT LocTy{VA.getLocVT()};
201  if (LocTy.getSizeInBits() == MRI.getType(ValReg).getSizeInBits())
202  return ValReg;
203  switch (VA.getLocInfo()) {
204  default: break;
205  case CCValAssign::Full:
206  case CCValAssign::BCvt:
207  // FIXME: bitconverting between vector types may or may not be a
208  // nop in big-endian situations.
209  return ValReg;
210  case CCValAssign::AExt: {
211  auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
212  return MIB->getOperand(0).getReg();
213  }
214  case CCValAssign::SExt: {
215  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
216  MIRBuilder.buildSExt(NewReg, ValReg);
217  return NewReg;
218  }
219  case CCValAssign::ZExt: {
220  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
221  MIRBuilder.buildZExt(NewReg, ValReg);
222  return NewReg;
223  }
224  }
225  llvm_unreachable("unable to extend register");
226 }
228 void CallLowering::ValueHandler::anchor() {}
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:176
void setByValAlign(unsigned A)
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
MachineInstrBuilder buildUnmerge(ArrayRef< LLT > Res, const SrcOp &Op)
Build and insert Res0, ...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned getReg() const
getReg - Returns the register number.
This class represents a function call, abstracting a target machine&#39;s calling convention.
unsigned Reg
unsigned getVectorNumElements() const
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:684
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:111
virtual unsigned getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Return the desired alignment for ByVal or InAlloca aggregate function arguments in the caller paramet...
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
bool isValid() const
Return true if this is a valid simple valuetype.
FunctionType * getFunctionType() const
Definition: CallSite.h:328
static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
CallingConv::ID getCallingConv() const
Get the calling convention of the call.
Definition: CallSite.h:320
bool isVector() const
bool handleAssignments(MachineIRBuilder &MIRBuilder, ArrayRef< ArgInfo > Args, ValueHandler &Handler) const
Invoke Handler::assignArg on each of the given Args and then use Callback to move them to the assigne...
const TargetLowering * getTLI() const
Getter for generic TargetLowering class.
Definition: CallLowering.h:125
MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ANYEXT Op0.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
void setByValSize(unsigned S)
Type * getType() const
Return the type of the instruction that generated this call site.
Definition: CallSite.h:272
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, const MachineOperand &Callee, const ArgInfo &OrigRet, ArrayRef< ArgInfo > OrigArgs, unsigned SwiftErrorVReg) const
This hook must be implemented to lower the given call instruction, including argument and return valu...
Definition: CallLowering.h:216
LocInfo getLocInfo() const
unsigned getSizeInBits() const
Type * getValueAsType() const
Return the attribute&#39;s value as a Type.
Definition: Attributes.cpp:230
MachineFunction & getMF()
Getter for the function we currently build.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
void setOrigAlign(unsigned A)
MachineInstrBuilder buildSExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_SEXT Op.
MachineRegisterInfo * getMRI()
Getter for MRI.
unsigned const MachineRegisterInfo * MRI
Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
virtual void assignValueToReg(unsigned ValVReg, unsigned PhysReg, CCValAssign &VA)=0
The specified value has been assigned to a physical register, handle the appropriate COPY (either to ...
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ZEXT Op.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:648
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
Helper class to build MachineInstr.
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned char TargetFlags=0)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:288
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:205
static unsigned NumFixedArgs
Extended Value Type.
Definition: ValueTypes.h:33
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_TRUNC Op.
Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return the attribute object that exists at the given index.
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:64
This class contains a discriminated union of information about pointers in memory operands...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
iterator_range< IterTy > args() const
Definition: CallSite.h:222
virtual unsigned assignCustomValue(const ArgInfo &Arg, ArrayRef< CCValAssign > VAs)
Handle custom values, which may be passed into one or more of VAs.
Definition: CallLowering.h:100
unsigned createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
CCState - This class holds information needed while lowering arguments and return values...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
CCValAssign - Represent assignment of one arg/retval to a location.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:749
const Function & getFunction() const
Return the LLVM function that this machine code represents.
This file declares the MachineIRBuilder class.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
amdgpu Simplify well known AMD library false FunctionCallee Callee
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
BBTy * getParent() const
Get the basic block containing the call site.
Definition: CallSite.h:101
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:469
virtual unsigned getStackAddress(uint64_t Size, int64_t Offset, MachinePointerInfo &MPO)=0
Materialize a VReg containing the address of the specified stack-based object.
virtual void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size, MachinePointerInfo &MPO, CCValAssign &VA)=0
The specified value has been assigned to a stack location.
Establish a view to a call site for examination.
Definition: CallSite.h:897
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
static MachineOperand CreateImm(int64_t Val)
virtual bool isArgumentHandler() const
Returns true if the handler is dealing with formal arguments, not with return values etc...
Definition: CallLowering.h:73
virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, const ArgInfo &Info, CCState &State)
Definition: CallLowering.h:109
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
This file describes how to lower LLVM calls to machine code calls.
print Print MemDeps of function
unsigned extendRegister(unsigned ValReg, CCValAssign &VA)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:415
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file describes how to lower LLVM code to machine code.