LLVM  7.0.0svn
Go to the documentation of this file.
1 //===-- lib/CodeGen/GlobalISel/CallLowering.cpp - Call lowering -----------===//
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
11 /// This file implements some simple delegations needed for call lowering.
12 ///
13 //===----------------------------------------------------------------------===//
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
24 using namespace llvm;
27  MachineIRBuilder &MIRBuilder, ImmutableCallSite CS, unsigned ResReg,
28  ArrayRef<unsigned> ArgRegs, std::function<unsigned()> GetCalleeReg) const {
29  auto &DL = CS.getParent()->getParent()->getParent()->getDataLayout();
31  // First step is to marshall all the function's parameters into the correct
32  // physregs and memory locations. Gather the sequence of argument types that
33  // we'll pass to the assigner function.
34  SmallVector<ArgInfo, 8> OrigArgs;
35  unsigned i = 0;
36  unsigned NumFixedArgs = CS.getFunctionType()->getNumParams();
37  for (auto &Arg : CS.args()) {
38  ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{},
39  i < NumFixedArgs};
40  setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CS);
41  OrigArgs.push_back(OrigArg);
42  ++i;
43  }
46  if (const Function *F = CS.getCalledFunction())
47  Callee = MachineOperand::CreateGA(F, 0);
48  else
49  Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
51  ArgInfo OrigRet{ResReg, CS.getType(), ISD::ArgFlagsTy{}};
52  if (!OrigRet.Ty->isVoidTy())
53  setArgFlags(OrigRet, AttributeList::ReturnIndex, DL, CS);
55  return lowerCall(MIRBuilder, CS.getCallingConv(), Callee, OrigRet, OrigArgs);
56 }
58 template <typename FuncInfoTy>
60  const DataLayout &DL,
61  const FuncInfoTy &FuncInfo) const {
62  const AttributeList &Attrs = FuncInfo.getAttributes();
63  if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
64  Arg.Flags.setZExt();
65  if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
66  Arg.Flags.setSExt();
67  if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
68  Arg.Flags.setInReg();
69  if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
70  Arg.Flags.setSRet();
71  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
72  Arg.Flags.setSwiftSelf();
73  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
74  Arg.Flags.setSwiftError();
75  if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
76  Arg.Flags.setByVal();
77  if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
78  Arg.Flags.setInAlloca();
80  if (Arg.Flags.isByVal() || Arg.Flags.isInAlloca()) {
81  Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
82  Arg.Flags.setByValSize(DL.getTypeAllocSize(ElementTy));
83  // For ByVal, alignment should be passed from FE. BE will guess if
84  // this info is not there but there are cases it cannot get right.
85  unsigned FrameAlign;
86  if (FuncInfo.getParamAlignment(OpIdx - 2))
87  FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
88  else
89  FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
90  Arg.Flags.setByValAlign(FrameAlign);
91  }
92  if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
93  Arg.Flags.setNest();
95 }
97 template void
98 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
99  const DataLayout &DL,
100  const Function &FuncInfo) const;
102 template void
103 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
104  const DataLayout &DL,
105  const CallInst &FuncInfo) const;
109  ValueHandler &Handler) const {
110  MachineFunction &MF = MIRBuilder.getMF();
111  const Function &F = MF.getFunction();
112  const DataLayout &DL = F.getParent()->getDataLayout();
115  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
117  unsigned NumArgs = Args.size();
118  for (unsigned i = 0; i != NumArgs; ++i) {
119  MVT CurVT = MVT::getVT(Args[i].Ty);
120  if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i], CCInfo))
121  return false;
122  }
124  for (unsigned i = 0, e = Args.size(), j = 0; i != e; ++i, ++j) {
125  assert(j < ArgLocs.size() && "Skipped too many arg locs");
127  CCValAssign &VA = ArgLocs[j];
128  assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
130  if (VA.needsCustom()) {
131  j += Handler.assignCustomValue(Args[i], makeArrayRef(ArgLocs).slice(j));
132  continue;
133  }
135  if (VA.isRegLoc())
136  Handler.assignValueToReg(Args[i].Reg, VA.getLocReg(), VA);
137  else if (VA.isMemLoc()) {
138  unsigned Size = VA.getValVT() == MVT::iPTR
139  ? DL.getPointerSize()
140  : alignTo(VA.getValVT().getSizeInBits(), 8) / 8;
141  unsigned Offset = VA.getLocMemOffset();
142  MachinePointerInfo MPO;
143  unsigned StackAddr = Handler.getStackAddress(Size, Offset, MPO);
144  Handler.assignValueToAddress(Args[i].Reg, StackAddr, Size, MPO, VA);
145  } else {
146  // FIXME: Support byvals and other weirdness
147  return false;
148  }
149  }
150  return true;
151 }
154  CCValAssign &VA) {
155  LLT LocTy{VA.getLocVT()};
156  switch (VA.getLocInfo()) {
157  default: break;
158  case CCValAssign::Full:
159  case CCValAssign::BCvt:
160  // FIXME: bitconverting between vector types may or may not be a
161  // nop in big-endian situations.
162  return ValReg;
163  case CCValAssign::AExt: {
164  assert(!VA.getLocVT().isVector() && "unexpected vector extend");
165  auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
166  return MIB->getOperand(0).getReg();
167  }
168  case CCValAssign::SExt: {
169  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
170  MIRBuilder.buildSExt(NewReg, ValReg);
171  return NewReg;
172  }
173  case CCValAssign::ZExt: {
174  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
175  MIRBuilder.buildZExt(NewReg, ValReg);
176  return NewReg;
177  }
178  }
179  llvm_unreachable("unable to extend register");
180 }
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:163
void setByValAlign(unsigned A)
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
CallingConv::ID getCallingConv() const
Get the calling convention of the call.
Definition: CallSite.h:312
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool isVector() const
Return true if this is a vector value type.
unsigned getReg() const
getReg - Returns the register number.
MachineInstrBuilder buildSExt(DstType &&Res, ArgType &&Arg)
Build and insert Res = G_SEXT Op.
This class represents a function call, abstracting a target machine&#39;s calling convention.
unsigned Reg
iterator_range< IterTy > args() const
Definition: CallSite.h:215
MachineInstrBuilder buildAnyExt(unsigned Res, unsigned Op)
Build and insert Res = G_ANYEXT Op0.
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:685
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.
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)
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:115
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
void setByValSize(unsigned S)
LocInfo getLocInfo() const
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MachineInstrBuilder buildZExt(DstType &&Res, ArgType &&Arg)
Build and insert Res = G_ZEXT Op.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
void setOrigAlign(unsigned A)
amdgpu Simplify well known AMD library false Value * Callee
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:46
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, const MachineOperand &Callee, const ArgInfo &OrigRet, ArrayRef< ArgInfo > OrigArgs) const
This hook must be implemented to lower the given call instruction, including argument and return valu...
Definition: CallLowering.h:185
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 ...
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
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:629
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned char TargetFlags=0)
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:281
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:194
static unsigned NumFixedArgs
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:61
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.
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:93
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:199
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:861
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:722
BBTy * getParent() const
Get the basic block containing the call site.
Definition: CallSite.h:97
const Function & getFunction() const
Return the LLVM function that this machine code represents.
This file declares the MachineIRBuilder class.
amdgpu Simplify well known AMD library false Value Value * Arg
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:428
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:714
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
static MachineOperand CreateImm(int64_t Val)
virtual bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, const ArgInfo &Info, CCState &State)
Definition: CallLowering.h:102
MachineFunction & getMF()
Getter for the function we currently build.
Type * getType() const
Return the type of the instruction that generated this call site.
Definition: CallSite.h:264
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:107
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
FunctionType * getFunctionType() const
Definition: CallSite.h:320
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
This file describes how to lower LLVM calls to machine code calls.
print Print MemDeps of function
unsigned extendRegister(unsigned ValReg, CCValAssign &VA)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:316
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file describes how to lower LLVM code to machine code.