LLVM  5.0.0
X86CallLowering.cpp
Go to the documentation of this file.
1 //===-- llvm/lib/Target/X86/X86CallLowering.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 the lowering of LLVM calls to machine code calls for
12 /// GlobalISel.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "X86CallLowering.h"
17 #include "X86CallingConv.h"
18 #include "X86ISelLowering.h"
19 #include "X86InstrInfo.h"
20 #include "X86TargetMachine.h"
21 
22 #include "llvm/CodeGen/Analysis.h"
27 
28 using namespace llvm;
29 
30 #include "X86GenCallingConv.inc"
31 
32 #ifndef LLVM_BUILD_GLOBAL_ISEL
33 #error "This shouldn't be built without GISel"
34 #endif
35 
37  : CallLowering(&TLI) {}
38 
39 bool X86CallLowering::splitToValueTypes(const ArgInfo &OrigArg,
40  SmallVectorImpl<ArgInfo> &SplitArgs,
41  const DataLayout &DL,
43  SplitArgTy PerformArgSplit) const {
44 
45  const X86TargetLowering &TLI = *getTLI<X86TargetLowering>();
46  LLVMContext &Context = OrigArg.Ty->getContext();
47 
48  SmallVector<EVT, 4> SplitVTs;
50  ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0);
51 
52  if (SplitVTs.size() != 1) {
53  // TODO: support struct/array split
54  return false;
55  }
56 
57  EVT VT = SplitVTs[0];
58  unsigned NumParts = TLI.getNumRegisters(Context, VT);
59 
60  if (NumParts == 1) {
61  // replace the original type ( pointer -> GPR ).
62  SplitArgs.emplace_back(OrigArg.Reg, VT.getTypeForEVT(Context),
63  OrigArg.Flags, OrigArg.IsFixed);
64  return true;
65  }
66 
67  SmallVector<unsigned, 8> SplitRegs;
68 
69  EVT PartVT = TLI.getRegisterType(Context, VT);
70  Type *PartTy = PartVT.getTypeForEVT(Context);
71 
72  for (unsigned i = 0; i < NumParts; ++i) {
73  ArgInfo Info =
74  ArgInfo{MRI.createGenericVirtualRegister(getLLTForType(*PartTy, DL)),
75  PartTy, OrigArg.Flags};
76  SplitArgs.push_back(Info);
77  SplitRegs.push_back(Info.Reg);
78  }
79 
80  PerformArgSplit(SplitRegs);
81  return true;
82 }
83 
84 namespace {
85 struct FuncReturnHandler : public CallLowering::ValueHandler {
86  FuncReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
87  MachineInstrBuilder &MIB, CCAssignFn *AssignFn)
88  : ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {}
89 
90  unsigned getStackAddress(uint64_t Size, int64_t Offset,
91  MachinePointerInfo &MPO) override {
92  llvm_unreachable("Don't know how to get a stack address yet");
93  }
94 
95  void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
96  CCValAssign &VA) override {
97  MIB.addUse(PhysReg, RegState::Implicit);
98  unsigned ExtReg = extendRegister(ValVReg, VA);
99  MIRBuilder.buildCopy(PhysReg, ExtReg);
100  }
101 
102  void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
103  MachinePointerInfo &MPO, CCValAssign &VA) override {
104  llvm_unreachable("Don't know how to assign a value to an address yet");
105  }
106 
107  MachineInstrBuilder &MIB;
108 };
109 } // End anonymous namespace.
110 
112  const Value *Val, unsigned VReg) const {
113 
114  assert(((Val && VReg) || (!Val && !VReg)) && "Return value without a vreg");
115 
116  auto MIB = MIRBuilder.buildInstrNoInsert(X86::RET).addImm(0);
117 
118  if (VReg) {
119  MachineFunction &MF = MIRBuilder.getMF();
120  MachineRegisterInfo &MRI = MF.getRegInfo();
121  auto &DL = MF.getDataLayout();
122  const Function &F = *MF.getFunction();
123 
124  ArgInfo OrigArg{VReg, Val->getType()};
125  setArgFlags(OrigArg, AttributeList::ReturnIndex, DL, F);
126 
127  SmallVector<ArgInfo, 8> SplitArgs;
128  if (!splitToValueTypes(OrigArg, SplitArgs, DL, MRI,
129  [&](ArrayRef<unsigned> Regs) {
130  MIRBuilder.buildUnmerge(Regs, VReg);
131  }))
132  return false;
133 
134  FuncReturnHandler Handler(MIRBuilder, MRI, MIB, RetCC_X86);
135  if (!handleAssignments(MIRBuilder, SplitArgs, Handler))
136  return false;
137  }
138 
139  MIRBuilder.insertInstr(MIB);
140  return true;
141 }
142 
143 namespace {
145  FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
146  CCAssignFn *AssignFn, const DataLayout &DL)
147  : ValueHandler(MIRBuilder, MRI, AssignFn), DL(DL) {}
148 
149  unsigned getStackAddress(uint64_t Size, int64_t Offset,
150  MachinePointerInfo &MPO) override {
151 
152  auto &MFI = MIRBuilder.getMF().getFrameInfo();
153  int FI = MFI.CreateFixedObject(Size, Offset, true);
154  MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
155 
156  unsigned AddrReg = MRI.createGenericVirtualRegister(
158  MIRBuilder.buildFrameIndex(AddrReg, FI);
159  return AddrReg;
160  }
161 
162  void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
163  MachinePointerInfo &MPO, CCValAssign &VA) override {
164 
165  auto MMO = MIRBuilder.getMF().getMachineMemOperand(
167  0);
168  MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
169  }
170 
171  void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
172  CCValAssign &VA) override {
173  MIRBuilder.getMBB().addLiveIn(PhysReg);
174  MIRBuilder.buildCopy(ValVReg, PhysReg);
175  }
176 
177  const DataLayout &DL;
178 };
179 } // namespace
180 
182  const Function &F,
183  ArrayRef<unsigned> VRegs) const {
184  if (F.arg_empty())
185  return true;
186 
187  // TODO: handle variadic function
188  if (F.isVarArg())
189  return false;
190 
191  MachineFunction &MF = MIRBuilder.getMF();
192  MachineRegisterInfo &MRI = MF.getRegInfo();
193  auto DL = MF.getDataLayout();
194 
195  SmallVector<ArgInfo, 8> SplitArgs;
196  unsigned Idx = 0;
197  for (auto &Arg : F.args()) {
198 
199  // TODO: handle not simple cases.
200  if (Arg.hasAttribute(Attribute::ByVal) ||
201  Arg.hasAttribute(Attribute::InReg) ||
202  Arg.hasAttribute(Attribute::StructRet) ||
203  Arg.hasAttribute(Attribute::SwiftSelf) ||
204  Arg.hasAttribute(Attribute::SwiftError) ||
205  Arg.hasAttribute(Attribute::Nest))
206  return false;
207 
208  ArgInfo OrigArg(VRegs[Idx], Arg.getType());
209  setArgFlags(OrigArg, Idx + AttributeList::FirstArgIndex, DL, F);
210  if (!splitToValueTypes(OrigArg, SplitArgs, DL, MRI,
211  [&](ArrayRef<unsigned> Regs) {
212  MIRBuilder.buildMerge(VRegs[Idx], Regs);
213  }))
214  return false;
215  Idx++;
216  }
217 
218  MachineBasicBlock &MBB = MIRBuilder.getMBB();
219  if (!MBB.empty())
220  MIRBuilder.setInstr(*MBB.begin());
221 
222  FormalArgHandler Handler(MIRBuilder, MRI, CC_X86, DL);
223  if (!handleAssignments(MIRBuilder, SplitArgs, Handler))
224  return false;
225 
226  // Move back to the end of the basic block.
227  MIRBuilder.setMBB(MBB);
228 
229  return true;
230 }
MachineBasicBlock & getMBB()
Getter for the basic block we currently build.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
bool arg_empty() const
Definition: Function.h:623
LLVMContext & Context
unsigned getNumRegisters(LLVMContext &Context, EVT VT) const
Return the number of registers that this ValueType will eventually require.
DataLayout DL
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...
Offsets
Offsets in bytes from the start of the input buffer.
Definition: SIInstrInfo.h:864
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:205
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, ArrayRef< unsigned > VRegs) const override
This hook must be implemented to lower the incoming (formal) arguments, described by Args...
static const MCPhysReg VRegs[32]
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
unsigned createGenericVirtualRegister(LLT Ty)
Create and return a new generic virtual register with low-level type Ty.
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:150
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
#define F(x, y, z)
Definition: MD5.cpp:55
const RegList & Regs
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don't insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< uint64_t > *Offsets=nullptr, uint64_t StartingOffset=0)
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition: Analysis.cpp:85
bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val, unsigned VReg) const override
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
unsigned const MachineRegisterInfo * MRI
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Helper class to build MachineInstr.
void setInstr(MachineInstr &MI)
Set the insertion point to before MI.
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
Extended Value Type.
Definition: ValueTypes.h:34
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...
LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
CCValAssign - Represent assignment of one arg/retval to a location.
This file declares the MachineIRBuilder class.
This file describes how to lower LLVM calls to machine code calls.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
The memory access reads data.
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
void emplace_back(ArgTypes &&...Args)
Definition: SmallVector.h:653
bool handleAssignments(MachineIRBuilder &MIRBuilder, ArrayRef< ArgInfo > Args, ValueHandler &Callback) const
Invoke Handler::assignArg on each of the given Args and then use Callback to move them to the assigne...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setMBB(MachineBasicBlock &MBB)
Set the insertion point to the end of MBB.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
void setArgFlags(ArgInfo &Arg, unsigned OpNum, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:346
The memory access always returns the same value (or traps).
MachineInstrBuilder buildUnmerge(ArrayRef< unsigned > Res, unsigned Op)
Build and insert Res0<def>, ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineInstrBuilder insertInstr(MachineInstrBuilder MIB)
Insert an existing instruction at the insertion point.
LLVM Value Representation.
Definition: Value.h:73
static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space (defaulting to 0).
X86CallLowering(const X86TargetLowering &TLI)
MachineInstrBuilder buildMerge(unsigned Res, ArrayRef< unsigned > Ops)
Build and insert Res<def> = G_MERGE_VALUES Op0, ...
FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, CCAssignFn *AssignFn)
iterator_range< arg_iterator > args()
Definition: Function.h:613