LLVM  8.0.0svn
CallLowering.cpp
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 //===----------------------------------------------------------------------===//
14 
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 
24 using namespace llvm;
25 
27  MachineIRBuilder &MIRBuilder, ImmutableCallSite CS, unsigned ResReg,
28  ArrayRef<unsigned> ArgRegs, std::function<unsigned()> GetCalleeReg) const {
29  auto &DL = CS.getParent()->getParent()->getParent()->getDataLayout();
30 
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  // We don't currently support swifterror or swiftself args.
42  if (OrigArg.Flags.isSwiftError() || OrigArg.Flags.isSwiftSelf())
43  return false;
44  OrigArgs.push_back(OrigArg);
45  ++i;
46  }
47 
49  if (const Function *F = CS.getCalledFunction())
50  Callee = MachineOperand::CreateGA(F, 0);
51  else
52  Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
53 
54  ArgInfo OrigRet{ResReg, CS.getType(), ISD::ArgFlagsTy{}};
55  if (!OrigRet.Ty->isVoidTy())
56  setArgFlags(OrigRet, AttributeList::ReturnIndex, DL, CS);
57 
58  return lowerCall(MIRBuilder, CS.getCallingConv(), Callee, OrigRet, OrigArgs);
59 }
60 
61 template <typename FuncInfoTy>
63  const DataLayout &DL,
64  const FuncInfoTy &FuncInfo) const {
65  const AttributeList &Attrs = FuncInfo.getAttributes();
66  if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
67  Arg.Flags.setZExt();
68  if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
69  Arg.Flags.setSExt();
70  if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
71  Arg.Flags.setInReg();
72  if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
73  Arg.Flags.setSRet();
74  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
75  Arg.Flags.setSwiftSelf();
76  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
77  Arg.Flags.setSwiftError();
78  if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
79  Arg.Flags.setByVal();
80  if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
81  Arg.Flags.setInAlloca();
82 
83  if (Arg.Flags.isByVal() || Arg.Flags.isInAlloca()) {
84  Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
85  Arg.Flags.setByValSize(DL.getTypeAllocSize(ElementTy));
86  // For ByVal, alignment should be passed from FE. BE will guess if
87  // this info is not there but there are cases it cannot get right.
88  unsigned FrameAlign;
89  if (FuncInfo.getParamAlignment(OpIdx - 2))
90  FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
91  else
92  FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
93  Arg.Flags.setByValAlign(FrameAlign);
94  }
95  if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
96  Arg.Flags.setNest();
98 }
99 
100 template void
101 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
102  const DataLayout &DL,
103  const Function &FuncInfo) const;
104 
105 template void
106 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
107  const DataLayout &DL,
108  const CallInst &FuncInfo) const;
109 
112  ValueHandler &Handler) const {
113  MachineFunction &MF = MIRBuilder.getMF();
114  const Function &F = MF.getFunction();
115  const DataLayout &DL = F.getParent()->getDataLayout();
116 
118  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
119 
120  unsigned NumArgs = Args.size();
121  for (unsigned i = 0; i != NumArgs; ++i) {
122  MVT CurVT = MVT::getVT(Args[i].Ty);
123  if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i], CCInfo))
124  return false;
125  }
126 
127  for (unsigned i = 0, e = Args.size(), j = 0; i != e; ++i, ++j) {
128  assert(j < ArgLocs.size() && "Skipped too many arg locs");
129 
130  CCValAssign &VA = ArgLocs[j];
131  assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
132 
133  if (VA.needsCustom()) {
134  j += Handler.assignCustomValue(Args[i], makeArrayRef(ArgLocs).slice(j));
135  continue;
136  }
137 
138  if (VA.isRegLoc())
139  Handler.assignValueToReg(Args[i].Reg, VA.getLocReg(), VA);
140  else if (VA.isMemLoc()) {
141  unsigned Size = VA.getValVT() == MVT::iPTR
142  ? DL.getPointerSize()
143  : alignTo(VA.getValVT().getSizeInBits(), 8) / 8;
144  unsigned Offset = VA.getLocMemOffset();
145  MachinePointerInfo MPO;
146  unsigned StackAddr = Handler.getStackAddress(Size, Offset, MPO);
147  Handler.assignValueToAddress(Args[i].Reg, StackAddr, Size, MPO, VA);
148  } else {
149  // FIXME: Support byvals and other weirdness
150  return false;
151  }
152  }
153  return true;
154 }
155 
157  CCValAssign &VA) {
158  LLT LocTy{VA.getLocVT()};
159  switch (VA.getLocInfo()) {
160  default: break;
161  case CCValAssign::Full:
162  case CCValAssign::BCvt:
163  // FIXME: bitconverting between vector types may or may not be a
164  // nop in big-endian situations.
165  return ValReg;
166  case CCValAssign::AExt: {
167  auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
168  return MIB->getOperand(0).getReg();
169  }
170  case CCValAssign::SExt: {
171  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
172  MIRBuilder.buildSExt(NewReg, ValReg);
173  return NewReg;
174  }
175  case CCValAssign::ZExt: {
176  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
177  MIRBuilder.buildZExt(NewReg, ValReg);
178  return NewReg;
179  }
180  }
181  llvm_unreachable("unable to extend register");
182 }
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:177
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
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.
F(f)
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:364
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:213
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:847
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:107
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.
uint32_t Size
Definition: Profile.cpp:47
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:566
FunctionType * getFunctionType() const
Definition: CallSite.h:320
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:414
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file describes how to lower LLVM code to machine code.