LLVM  4.0.0
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 
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
23 
24 using namespace llvm;
25 
27  MachineIRBuilder &MIRBuilder, const CallInst &CI, unsigned ResReg,
28  ArrayRef<unsigned> ArgRegs, std::function<unsigned()> GetCalleeReg) const {
29  auto &DL = CI.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  for (auto &Arg : CI.arg_operands()) {
37  ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{}};
38  setArgFlags(OrigArg, i + 1, DL, CI);
39  OrigArgs.push_back(OrigArg);
40  ++i;
41  }
42 
44  if (Function *F = CI.getCalledFunction())
45  Callee = MachineOperand::CreateGA(F, 0);
46  else
47  Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
48 
49  ArgInfo OrigRet{ResReg, CI.getType(), ISD::ArgFlagsTy{}};
50  if (!OrigRet.Ty->isVoidTy())
51  setArgFlags(OrigRet, AttributeSet::ReturnIndex, DL, CI);
52 
53  return lowerCall(MIRBuilder, Callee, OrigRet, OrigArgs);
54 }
55 
56 template <typename FuncInfoTy>
58  const DataLayout &DL,
59  const FuncInfoTy &FuncInfo) const {
60  const AttributeSet &Attrs = FuncInfo.getAttributes();
61  if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
62  Arg.Flags.setZExt();
63  if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
64  Arg.Flags.setSExt();
65  if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
66  Arg.Flags.setInReg();
67  if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
68  Arg.Flags.setSRet();
69  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
70  Arg.Flags.setSwiftSelf();
71  if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
72  Arg.Flags.setSwiftError();
73  if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
74  Arg.Flags.setByVal();
75  if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
76  Arg.Flags.setInAlloca();
77 
78  if (Arg.Flags.isByVal() || Arg.Flags.isInAlloca()) {
79  Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
80  Arg.Flags.setByValSize(DL.getTypeAllocSize(ElementTy));
81  // For ByVal, alignment should be passed from FE. BE will guess if
82  // this info is not there but there are cases it cannot get right.
83  unsigned FrameAlign;
84  if (FuncInfo.getParamAlignment(OpIdx))
85  FrameAlign = FuncInfo.getParamAlignment(OpIdx);
86  else
87  FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
88  Arg.Flags.setByValAlign(FrameAlign);
89  }
90  if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
91  Arg.Flags.setNest();
93 }
94 
95 template void
96 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
97  const DataLayout &DL,
98  const Function &FuncInfo) const;
99 
100 template void
101 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
102  const DataLayout &DL,
103  const CallInst &FuncInfo) const;
104 
106  CCAssignFn *AssignFn,
108  ValueHandler &Handler) const {
109  MachineFunction &MF = MIRBuilder.getMF();
110  const Function &F = *MF.getFunction();
111  const DataLayout &DL = F.getParent()->getDataLayout();
112 
114  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
115 
116  unsigned NumArgs = Args.size();
117  for (unsigned i = 0; i != NumArgs; ++i) {
118  MVT CurVT = MVT::getVT(Args[i].Ty);
119  if (AssignFn(i, CurVT, CurVT, CCValAssign::Full, Args[i].Flags, CCInfo))
120  return false;
121  }
122 
123  for (unsigned i = 0, e = Args.size(); i != e; ++i) {
124  CCValAssign &VA = ArgLocs[i];
125 
126  if (VA.isRegLoc())
127  Handler.assignValueToReg(Args[i].Reg, VA.getLocReg(), VA);
128  else if (VA.isMemLoc()) {
129  unsigned Size = VA.getValVT() == MVT::iPTR
130  ? DL.getPointerSize()
131  : alignTo(VA.getValVT().getSizeInBits(), 8) / 8;
132  unsigned Offset = VA.getLocMemOffset();
133  MachinePointerInfo MPO;
134  unsigned StackAddr = Handler.getStackAddress(Size, Offset, MPO);
135  Handler.assignValueToAddress(Args[i].Reg, StackAddr, Size, MPO, VA);
136  } else {
137  // FIXME: Support byvals and other weirdness
138  return false;
139  }
140  }
141  return true;
142 }
143 
145  CCValAssign &VA) {
146  LLT LocTy{VA.getLocVT()};
147  switch (VA.getLocInfo()) {
148  default: break;
149  case CCValAssign::Full:
150  case CCValAssign::BCvt:
151  // FIXME: bitconverting between vector types may or may not be a
152  // nop in big-endian situations.
153  return ValReg;
154  case CCValAssign::AExt:
155  assert(!VA.getLocVT().isVector() && "unexpected vector extend");
156  // Otherwise, it's a nop.
157  return ValReg;
158  case CCValAssign::SExt: {
159  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
160  MIRBuilder.buildSExt(NewReg, ValReg);
161  return NewReg;
162  }
163  case CCValAssign::ZExt: {
164  unsigned NewReg = MRI.createGenericVirtualRegister(LocTy);
165  MIRBuilder.buildZExt(NewReg, ValReg);
166  return NewReg;
167  }
168  }
169  llvm_unreachable("unable to extend register");
170 }
void setByValAlign(unsigned A)
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
MVT getValVT() const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:226
size_t i
LocInfo getLocInfo() const
MachineRegisterInfo & MRI
Definition: CallLowering.h:79
MachineInstrBuilder buildZExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_ZEXT Op.
This class represents a function call, abstracting a target machine's calling convention.
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change...
unsigned getSizeInBits() const
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:100
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
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:664
unsigned createGenericVirtualRegister(LLT Ty)
Create and return a new generic virtual register with low-level type Ty.
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Definition: Attributes.cpp:994
bool isRegLoc() const
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:165
struct fuzzer::@269 Flags
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)
Reg
All possible values of the reg field in the ModR/M byte.
void setByValSize(unsigned S)
unsigned getLocReg() const
#define F(x, y, z)
Definition: MD5.cpp:51
MachineFunction & getMF()
Getter for the function we currently build.
void setOrigAlign(unsigned A)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
MVT - 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 ...
MVT getLocVT() const
bool isVector() const
isVector - Return true if this is a vector value type.
Helper class to build MachineInstr.
uint32_t Offset
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
virtual unsigned getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Return the desired alignment for ByVal or InAlloca aggregate function arguments in the caller paramet...
Argument handling is mostly uniform between the four places that make these decisions: function forma...
Definition: CallLowering.h:49
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.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:689
CCState - This class holds information needed while lowering arguments and return values...
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:408
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
CCValAssign - Represent assignment of one arg/retval to a location.
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, 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:153
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
This file declares the MachineIRBuilder class.
bool isMemLoc() const
MachineInstrBuilder buildSExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_SEXT Op.
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.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
static MachineOperand CreateImm(int64_t Val)
void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
iterator_range< op_iterator > arg_operands()
Iteration adapter for range-for loops.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
This file describes how to lower LLVM calls to machine code calls.
print Print MemDeps of function
unsigned extendRegister(unsigned ValReg, CCValAssign &VA)
bool handleAssignments(MachineIRBuilder &MIRBuilder, CCAssignFn *AssignFn, ArrayRef< ArgInfo > Args, ValueHandler &Callback) const
Invoke the AssignFn on each of the given Args and then use Callback to move them to the assigned loca...
unsigned getLocMemOffset() const
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:608
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.cpp:234
const TargetLowering * getTLI() const
Getter for generic TargetLowering class.
Definition: CallLowering.h:84
const BasicBlock * getParent() const
Definition: Instruction.h:62
This file describes how to lower LLVM code to machine code.