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 using namespace llvm;
25 void CallLowering::anchor() {}
28  MachineIRBuilder &MIRBuilder, ImmutableCallSite CS, unsigned ResReg,
29  ArrayRef<unsigned> ArgRegs, std::function<unsigned()> GetCalleeReg) const {
30  auto &DL = CS.getParent()->getParent()->getParent()->getDataLayout();
32  // First step is to marshall all the function's parameters into the correct
33  // physregs and memory locations. Gather the sequence of argument types that
34  // we'll pass to the assigner function.
35  SmallVector<ArgInfo, 8> OrigArgs;
36  unsigned i = 0;
37  unsigned NumFixedArgs = CS.getFunctionType()->getNumParams();
38  for (auto &Arg : CS.args()) {
39  ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{},
40  i < NumFixedArgs};
41  setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CS);
42  // We don't currently support swifterror or swiftself args.
43  if (OrigArg.Flags.isSwiftError() || OrigArg.Flags.isSwiftSelf())
44  return false;
45  OrigArgs.push_back(OrigArg);
46  ++i;
47  }
50  if (const Function *F = CS.getCalledFunction())
51  Callee = MachineOperand::CreateGA(F, 0);
52  else
53  Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
55  ArgInfo OrigRet{ResReg, CS.getType(), ISD::ArgFlagsTy{}};
56  if (!OrigRet.Ty->isVoidTy())
57  setArgFlags(OrigRet, AttributeList::ReturnIndex, DL, CS);
59  return lowerCall(MIRBuilder, CS.getCallingConv(), Callee, OrigRet, OrigArgs);
60 }
62 template <typename FuncInfoTy>
64  const DataLayout &DL,
65  const FuncInfoTy &FuncInfo) const {
66  const AttributeList &Attrs = FuncInfo.getAttributes();
67  if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
68  Arg.Flags.setZExt();
69  if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
70  Arg.Flags.setSExt();
71  if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
72  Arg.Flags.setInReg();
73  if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
74  Arg.Flags.setSRet();
75  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
76  Arg.Flags.setSwiftSelf();
77  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
78  Arg.Flags.setSwiftError();
79  if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
80  Arg.Flags.setByVal();
81  if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
82  Arg.Flags.setInAlloca();
84  if (Arg.Flags.isByVal() || Arg.Flags.isInAlloca()) {
85  Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
86  Arg.Flags.setByValSize(DL.getTypeAllocSize(ElementTy));
87  // For ByVal, alignment should be passed from FE. BE will guess if
88  // this info is not there but there are cases it cannot get right.
89  unsigned FrameAlign;
90  if (FuncInfo.getParamAlignment(OpIdx - 2))
91  FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
92  else
93  FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
94  Arg.Flags.setByValAlign(FrameAlign);
95  }
96  if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
97  Arg.Flags.setNest();
99 }
101 template void
102 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
103  const DataLayout &DL,
104  const Function &FuncInfo) const;
106 template void
107 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
108  const DataLayout &DL,
109  const CallInst &FuncInfo) const;
113  ValueHandler &Handler) const {
114  MachineFunction &MF = MIRBuilder.getMF();
115  const Function &F = MF.getFunction();
116  const DataLayout &DL = F.getParent()->getDataLayout();
119  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
121  unsigned NumArgs = Args.size();
122  for (unsigned i = 0; i != NumArgs; ++i) {
123  MVT CurVT = MVT::getVT(Args[i].Ty);
124  if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i], CCInfo))
125  return false;
126  }
128  for (unsigned i = 0, e = Args.size(), j = 0; i != e; ++i, ++j) {
129  assert(j < ArgLocs.size() && "Skipped too many arg locs");
131  CCValAssign &VA = ArgLocs[j];
132  assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
134  if (VA.needsCustom()) {
135  j += Handler.assignCustomValue(Args[i], makeArrayRef(ArgLocs).slice(j));
136  continue;
137  }
139  if (VA.isRegLoc())
140  Handler.assignValueToReg(Args[i].Reg, VA.getLocReg(), VA);
141  else if (VA.isMemLoc()) {
142  unsigned Size = VA.getValVT() == MVT::iPTR
143  ? DL.getPointerSize()
144  : alignTo(VA.getValVT().getSizeInBits(), 8) / 8;
145  unsigned Offset = VA.getLocMemOffset();
146  MachinePointerInfo MPO;
147  unsigned StackAddr = Handler.getStackAddress(Size, Offset, MPO);
148  Handler.assignValueToAddress(Args[i].Reg, StackAddr, Size, MPO, VA);
149  } else {
150  // FIXME: Support byvals and other weirdness
151  return false;
152  }
153  }
154  return true;
155 }
158  CCValAssign &VA) {
159  LLT LocTy{VA.getLocVT()};
160  switch (VA.getLocInfo()) {
161  default: break;
162  case CCValAssign::Full:
163  case CCValAssign::BCvt:
164  // FIXME: bitconverting between vector types may or may not be a
165  // nop in big-endian situations.
166  return ValReg;
167  case CCValAssign::AExt: {
168  auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
169  return MIB->getOperand(0).getReg();
170  }
171  case CCValAssign::SExt: {
172  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
173  MIRBuilder.buildSExt(NewReg, ValReg);
174  return NewReg;
175  }
176  case CCValAssign::ZExt: {
177  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
178  MIRBuilder.buildZExt(NewReg, ValReg);
179  return NewReg;
180  }
181  }
182  llvm_unreachable("unable to extend register");
183 }
185 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
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
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.
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 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:118
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
void setByValSize(unsigned S)
Type * getType() const
Return the type of the instruction that generated this call site.
Definition: CallSite.h:272
LocInfo getLocInfo() const
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.
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 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:188
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:628
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:138
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:280
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:192
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.
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: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: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:839
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:729
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 FunctionCallee Callee
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:435
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:892
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
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
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:565
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:413
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file describes how to lower LLVM code to machine code.