LLVM  8.0.1
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 
26 void CallLowering::anchor() {}
27 
29  MachineIRBuilder &MIRBuilder, ImmutableCallSite CS, unsigned ResReg,
30  ArrayRef<unsigned> ArgRegs, std::function<unsigned()> GetCalleeReg) const {
31  auto &DL = CS.getParent()->getParent()->getParent()->getDataLayout();
32 
33  // First step is to marshall all the function's parameters into the correct
34  // physregs and memory locations. Gather the sequence of argument types that
35  // we'll pass to the assigner function.
36  SmallVector<ArgInfo, 8> OrigArgs;
37  unsigned i = 0;
38  unsigned NumFixedArgs = CS.getFunctionType()->getNumParams();
39  for (auto &Arg : CS.args()) {
40  ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{},
41  i < NumFixedArgs};
42  setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CS);
43  // We don't currently support swifterror or swiftself args.
44  if (OrigArg.Flags.isSwiftError() || OrigArg.Flags.isSwiftSelf())
45  return false;
46  OrigArgs.push_back(OrigArg);
47  ++i;
48  }
49 
51  if (const Function *F = CS.getCalledFunction())
52  Callee = MachineOperand::CreateGA(F, 0);
53  else
54  Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
55 
56  ArgInfo OrigRet{ResReg, CS.getType(), ISD::ArgFlagsTy{}};
57  if (!OrigRet.Ty->isVoidTy())
58  setArgFlags(OrigRet, AttributeList::ReturnIndex, DL, CS);
59 
60  return lowerCall(MIRBuilder, CS.getCallingConv(), Callee, OrigRet, OrigArgs);
61 }
62 
63 template <typename FuncInfoTy>
65  const DataLayout &DL,
66  const FuncInfoTy &FuncInfo) const {
67  const AttributeList &Attrs = FuncInfo.getAttributes();
68  if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
69  Arg.Flags.setZExt();
70  if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
71  Arg.Flags.setSExt();
72  if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
73  Arg.Flags.setInReg();
74  if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
75  Arg.Flags.setSRet();
76  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
77  Arg.Flags.setSwiftSelf();
78  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
79  Arg.Flags.setSwiftError();
80  if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
81  Arg.Flags.setByVal();
82  if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
83  Arg.Flags.setInAlloca();
84 
85  if (Arg.Flags.isByVal() || Arg.Flags.isInAlloca()) {
86  Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
87  Arg.Flags.setByValSize(DL.getTypeAllocSize(ElementTy));
88  // For ByVal, alignment should be passed from FE. BE will guess if
89  // this info is not there but there are cases it cannot get right.
90  unsigned FrameAlign;
91  if (FuncInfo.getParamAlignment(OpIdx - 2))
92  FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
93  else
94  FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
95  Arg.Flags.setByValAlign(FrameAlign);
96  }
97  if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
98  Arg.Flags.setNest();
100 }
101 
102 template void
103 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
104  const DataLayout &DL,
105  const Function &FuncInfo) const;
106 
107 template void
108 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
109  const DataLayout &DL,
110  const CallInst &FuncInfo) const;
111 
114  ValueHandler &Handler) const {
115  MachineFunction &MF = MIRBuilder.getMF();
116  const Function &F = MF.getFunction();
117  const DataLayout &DL = F.getParent()->getDataLayout();
118 
120  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
121 
122  unsigned NumArgs = Args.size();
123  for (unsigned i = 0; i != NumArgs; ++i) {
124  MVT CurVT = MVT::getVT(Args[i].Ty);
125  if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i], CCInfo))
126  return false;
127  }
128 
129  for (unsigned i = 0, e = Args.size(), j = 0; i != e; ++i, ++j) {
130  assert(j < ArgLocs.size() && "Skipped too many arg locs");
131 
132  CCValAssign &VA = ArgLocs[j];
133  assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
134 
135  if (VA.needsCustom()) {
136  j += Handler.assignCustomValue(Args[i], makeArrayRef(ArgLocs).slice(j));
137  continue;
138  }
139 
140  if (VA.isRegLoc())
141  Handler.assignValueToReg(Args[i].Reg, VA.getLocReg(), VA);
142  else if (VA.isMemLoc()) {
143  unsigned Size = VA.getValVT() == MVT::iPTR
144  ? DL.getPointerSize()
145  : alignTo(VA.getValVT().getSizeInBits(), 8) / 8;
146  unsigned Offset = VA.getLocMemOffset();
147  MachinePointerInfo MPO;
148  unsigned StackAddr = Handler.getStackAddress(Size, Offset, MPO);
149  Handler.assignValueToAddress(Args[i].Reg, StackAddr, Size, MPO, VA);
150  } else {
151  // FIXME: Support byvals and other weirdness
152  return false;
153  }
154  }
155  return true;
156 }
157 
159  CCValAssign &VA) {
160  LLT LocTy{VA.getLocVT()};
161  switch (VA.getLocInfo()) {
162  default: break;
163  case CCValAssign::Full:
164  case CCValAssign::BCvt:
165  // FIXME: bitconverting between vector types may or may not be a
166  // nop in big-endian situations.
167  return ValReg;
168  case CCValAssign::AExt: {
169  auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
170  return MIB->getOperand(0).getReg();
171  }
172  case CCValAssign::SExt: {
173  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
174  MIRBuilder.buildSExt(NewReg, ValReg);
175  return NewReg;
176  }
177  case CCValAssign::ZExt: {
178  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
179  MIRBuilder.buildZExt(NewReg, ValReg);
180  return NewReg;
181  }
182  }
183  llvm_unreachable("unable to extend register");
184 }
185 
186 void CallLowering::ValueHandler::anchor() {}
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
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned getReg() const
getReg - Returns the register number.
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
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:119
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:371
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
MachineFunction & getMF()
Getter for the function we currently build.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
void setOrigAlign(unsigned A)
amdgpu Simplify well known AMD library false Value * Callee
MachineInstrBuilder buildSExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_SEXT Op.
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:189
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: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
Helper class to build MachineInstr.
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:193
static unsigned NumFixedArgs
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:62
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:94
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:730
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:436
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:711
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:103
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.