xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/CallLowering.cpp (revision 9068bccbae340c4b281ad92ba041a934de39fac3)
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 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/Analysis.h"
15 #include "llvm/CodeGen/GlobalISel/CallLowering.h"
16 #include "llvm/CodeGen/GlobalISel/Utils.h"
17 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
18 #include "llvm/CodeGen/MachineOperand.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 
26 #define DEBUG_TYPE "call-lowering"
27 
28 using namespace llvm;
29 
30 void CallLowering::anchor() {}
31 
32 bool CallLowering::lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
33                              ArrayRef<Register> ResRegs,
34                              ArrayRef<ArrayRef<Register>> ArgRegs,
35                              Register SwiftErrorVReg,
36                              std::function<unsigned()> GetCalleeReg) const {
37   CallLoweringInfo Info;
38   const DataLayout &DL = MIRBuilder.getDataLayout();
39 
40   // First step is to marshall all the function's parameters into the correct
41   // physregs and memory locations. Gather the sequence of argument types that
42   // we'll pass to the assigner function.
43   unsigned i = 0;
44   unsigned NumFixedArgs = CS.getFunctionType()->getNumParams();
45   for (auto &Arg : CS.args()) {
46     ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{},
47                     i < NumFixedArgs};
48     setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CS);
49     Info.OrigArgs.push_back(OrigArg);
50     ++i;
51   }
52 
53   // Try looking through a bitcast from one function type to another.
54   // Commonly happens with calls to objc_msgSend().
55   const Value *CalleeV = CS.getCalledValue()->stripPointerCasts();
56   if (const Function *F = dyn_cast<Function>(CalleeV))
57     Info.Callee = MachineOperand::CreateGA(F, 0);
58   else
59     Info.Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
60 
61   Info.OrigRet = ArgInfo{ResRegs, CS.getType(), ISD::ArgFlagsTy{}};
62   if (!Info.OrigRet.Ty->isVoidTy())
63     setArgFlags(Info.OrigRet, AttributeList::ReturnIndex, DL, CS);
64 
65   MachineFunction &MF = MIRBuilder.getMF();
66   Info.KnownCallees =
67       CS.getInstruction()->getMetadata(LLVMContext::MD_callees);
68   Info.CallConv = CS.getCallingConv();
69   Info.SwiftErrorVReg = SwiftErrorVReg;
70   Info.IsMustTailCall = CS.isMustTailCall();
71   Info.IsTailCall = CS.isTailCall() &&
72                     isInTailCallPosition(CS, MF.getTarget()) &&
73                     (MF.getFunction()
74                        .getFnAttribute("disable-tail-calls")
75                        .getValueAsString() != "true");
76   Info.IsVarArg = CS.getFunctionType()->isVarArg();
77   return lowerCall(MIRBuilder, Info);
78 }
79 
80 template <typename FuncInfoTy>
81 void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
82                                const DataLayout &DL,
83                                const FuncInfoTy &FuncInfo) const {
84   auto &Flags = Arg.Flags[0];
85   const AttributeList &Attrs = FuncInfo.getAttributes();
86   if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
87     Flags.setZExt();
88   if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
89     Flags.setSExt();
90   if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
91     Flags.setInReg();
92   if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
93     Flags.setSRet();
94   if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
95     Flags.setSwiftSelf();
96   if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
97     Flags.setSwiftError();
98   if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
99     Flags.setByVal();
100   if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
101     Flags.setInAlloca();
102 
103   if (Flags.isByVal() || Flags.isInAlloca()) {
104     Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
105 
106     auto Ty = Attrs.getAttribute(OpIdx, Attribute::ByVal).getValueAsType();
107     Flags.setByValSize(DL.getTypeAllocSize(Ty ? Ty : ElementTy));
108 
109     // For ByVal, alignment should be passed from FE.  BE will guess if
110     // this info is not there but there are cases it cannot get right.
111     Align FrameAlign;
112     if (auto ParamAlign = FuncInfo.getParamAlign(OpIdx - 2))
113       FrameAlign = *ParamAlign;
114     else
115       FrameAlign = Align(getTLI()->getByValTypeAlignment(ElementTy, DL));
116     Flags.setByValAlign(FrameAlign);
117   }
118   if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
119     Flags.setNest();
120   Flags.setOrigAlign(Align(DL.getABITypeAlignment(Arg.Ty)));
121 }
122 
123 template void
124 CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
125                                     const DataLayout &DL,
126                                     const Function &FuncInfo) const;
127 
128 template void
129 CallLowering::setArgFlags<CallInst>(CallLowering::ArgInfo &Arg, unsigned OpIdx,
130                                     const DataLayout &DL,
131                                     const CallInst &FuncInfo) const;
132 
133 Register CallLowering::packRegs(ArrayRef<Register> SrcRegs, Type *PackedTy,
134                                 MachineIRBuilder &MIRBuilder) const {
135   assert(SrcRegs.size() > 1 && "Nothing to pack");
136 
137   const DataLayout &DL = MIRBuilder.getMF().getDataLayout();
138   MachineRegisterInfo *MRI = MIRBuilder.getMRI();
139 
140   LLT PackedLLT = getLLTForType(*PackedTy, DL);
141 
142   SmallVector<LLT, 8> LLTs;
143   SmallVector<uint64_t, 8> Offsets;
144   computeValueLLTs(DL, *PackedTy, LLTs, &Offsets);
145   assert(LLTs.size() == SrcRegs.size() && "Regs / types mismatch");
146 
147   Register Dst = MRI->createGenericVirtualRegister(PackedLLT);
148   MIRBuilder.buildUndef(Dst);
149   for (unsigned i = 0; i < SrcRegs.size(); ++i) {
150     Register NewDst = MRI->createGenericVirtualRegister(PackedLLT);
151     MIRBuilder.buildInsert(NewDst, Dst, SrcRegs[i], Offsets[i]);
152     Dst = NewDst;
153   }
154 
155   return Dst;
156 }
157 
158 void CallLowering::unpackRegs(ArrayRef<Register> DstRegs, Register SrcReg,
159                               Type *PackedTy,
160                               MachineIRBuilder &MIRBuilder) const {
161   assert(DstRegs.size() > 1 && "Nothing to unpack");
162 
163   const DataLayout &DL = MIRBuilder.getDataLayout();
164 
165   SmallVector<LLT, 8> LLTs;
166   SmallVector<uint64_t, 8> Offsets;
167   computeValueLLTs(DL, *PackedTy, LLTs, &Offsets);
168   assert(LLTs.size() == DstRegs.size() && "Regs / types mismatch");
169 
170   for (unsigned i = 0; i < DstRegs.size(); ++i)
171     MIRBuilder.buildExtract(DstRegs[i], SrcReg, Offsets[i]);
172 }
173 
174 bool CallLowering::handleAssignments(MachineIRBuilder &MIRBuilder,
175                                      SmallVectorImpl<ArgInfo> &Args,
176                                      ValueHandler &Handler) const {
177   MachineFunction &MF = MIRBuilder.getMF();
178   const Function &F = MF.getFunction();
179   SmallVector<CCValAssign, 16> ArgLocs;
180   CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
181   return handleAssignments(CCInfo, ArgLocs, MIRBuilder, Args, Handler);
182 }
183 
184 bool CallLowering::handleAssignments(CCState &CCInfo,
185                                      SmallVectorImpl<CCValAssign> &ArgLocs,
186                                      MachineIRBuilder &MIRBuilder,
187                                      SmallVectorImpl<ArgInfo> &Args,
188                                      ValueHandler &Handler) const {
189   MachineFunction &MF = MIRBuilder.getMF();
190   const Function &F = MF.getFunction();
191   const DataLayout &DL = F.getParent()->getDataLayout();
192 
193   unsigned NumArgs = Args.size();
194   for (unsigned i = 0; i != NumArgs; ++i) {
195     MVT CurVT = MVT::getVT(Args[i].Ty);
196     if (Handler.assignArg(i, CurVT, CurVT, CCValAssign::Full, Args[i],
197                           Args[i].Flags[0], CCInfo)) {
198       if (!CurVT.isValid())
199         return false;
200       MVT NewVT = TLI->getRegisterTypeForCallingConv(
201           F.getContext(), F.getCallingConv(), EVT(CurVT));
202 
203       // If we need to split the type over multiple regs, check it's a scenario
204       // we currently support.
205       unsigned NumParts = TLI->getNumRegistersForCallingConv(
206           F.getContext(), F.getCallingConv(), CurVT);
207       if (NumParts > 1) {
208         // For now only handle exact splits.
209         if (NewVT.getSizeInBits() * NumParts != CurVT.getSizeInBits())
210           return false;
211       }
212 
213       // For incoming arguments (physregs to vregs), we could have values in
214       // physregs (or memlocs) which we want to extract and copy to vregs.
215       // During this, we might have to deal with the LLT being split across
216       // multiple regs, so we have to record this information for later.
217       //
218       // If we have outgoing args, then we have the opposite case. We have a
219       // vreg with an LLT which we want to assign to a physical location, and
220       // we might have to record that the value has to be split later.
221       if (Handler.isIncomingArgumentHandler()) {
222         if (NumParts == 1) {
223           // Try to use the register type if we couldn't assign the VT.
224           if (Handler.assignArg(i, NewVT, NewVT, CCValAssign::Full, Args[i],
225                                 Args[i].Flags[0], CCInfo))
226             return false;
227         } else {
228           // We're handling an incoming arg which is split over multiple regs.
229           // E.g. passing an s128 on AArch64.
230           ISD::ArgFlagsTy OrigFlags = Args[i].Flags[0];
231           Args[i].OrigRegs.push_back(Args[i].Regs[0]);
232           Args[i].Regs.clear();
233           Args[i].Flags.clear();
234           LLT NewLLT = getLLTForMVT(NewVT);
235           // For each split register, create and assign a vreg that will store
236           // the incoming component of the larger value. These will later be
237           // merged to form the final vreg.
238           for (unsigned Part = 0; Part < NumParts; ++Part) {
239             Register Reg =
240                 MIRBuilder.getMRI()->createGenericVirtualRegister(NewLLT);
241             ISD::ArgFlagsTy Flags = OrigFlags;
242             if (Part == 0) {
243               Flags.setSplit();
244             } else {
245               Flags.setOrigAlign(Align(1));
246               if (Part == NumParts - 1)
247                 Flags.setSplitEnd();
248             }
249             Args[i].Regs.push_back(Reg);
250             Args[i].Flags.push_back(Flags);
251             if (Handler.assignArg(i + Part, NewVT, NewVT, CCValAssign::Full,
252                                   Args[i], Args[i].Flags[Part], CCInfo)) {
253               // Still couldn't assign this smaller part type for some reason.
254               return false;
255             }
256           }
257         }
258       } else {
259         // Handling an outgoing arg that might need to be split.
260         if (NumParts < 2)
261           return false; // Don't know how to deal with this type combination.
262 
263         // This type is passed via multiple registers in the calling convention.
264         // We need to extract the individual parts.
265         Register LargeReg = Args[i].Regs[0];
266         LLT SmallTy = LLT::scalar(NewVT.getSizeInBits());
267         auto Unmerge = MIRBuilder.buildUnmerge(SmallTy, LargeReg);
268         assert(Unmerge->getNumOperands() == NumParts + 1);
269         ISD::ArgFlagsTy OrigFlags = Args[i].Flags[0];
270         // We're going to replace the regs and flags with the split ones.
271         Args[i].Regs.clear();
272         Args[i].Flags.clear();
273         for (unsigned PartIdx = 0; PartIdx < NumParts; ++PartIdx) {
274           ISD::ArgFlagsTy Flags = OrigFlags;
275           if (PartIdx == 0) {
276             Flags.setSplit();
277           } else {
278             Flags.setOrigAlign(Align(1));
279             if (PartIdx == NumParts - 1)
280               Flags.setSplitEnd();
281           }
282           Args[i].Regs.push_back(Unmerge.getReg(PartIdx));
283           Args[i].Flags.push_back(Flags);
284           if (Handler.assignArg(i + PartIdx, NewVT, NewVT, CCValAssign::Full,
285                                 Args[i], Args[i].Flags[PartIdx], CCInfo))
286             return false;
287         }
288       }
289     }
290   }
291 
292   for (unsigned i = 0, e = Args.size(), j = 0; i != e; ++i, ++j) {
293     assert(j < ArgLocs.size() && "Skipped too many arg locs");
294 
295     CCValAssign &VA = ArgLocs[j];
296     assert(VA.getValNo() == i && "Location doesn't correspond to current arg");
297 
298     if (VA.needsCustom()) {
299       j += Handler.assignCustomValue(Args[i], makeArrayRef(ArgLocs).slice(j));
300       continue;
301     }
302 
303     // FIXME: Pack registers if we have more than one.
304     Register ArgReg = Args[i].Regs[0];
305 
306     MVT OrigVT = MVT::getVT(Args[i].Ty);
307     MVT VAVT = VA.getValVT();
308     if (VA.isRegLoc()) {
309       if (Handler.isIncomingArgumentHandler() && VAVT != OrigVT) {
310         if (VAVT.getSizeInBits() < OrigVT.getSizeInBits()) {
311           // Expected to be multiple regs for a single incoming arg.
312           unsigned NumArgRegs = Args[i].Regs.size();
313           if (NumArgRegs < 2)
314             return false;
315 
316           assert((j + (NumArgRegs - 1)) < ArgLocs.size() &&
317                  "Too many regs for number of args");
318           for (unsigned Part = 0; Part < NumArgRegs; ++Part) {
319             // There should be Regs.size() ArgLocs per argument.
320             VA = ArgLocs[j + Part];
321             Handler.assignValueToReg(Args[i].Regs[Part], VA.getLocReg(), VA);
322           }
323           j += NumArgRegs - 1;
324           // Merge the split registers into the expected larger result vreg
325           // of the original call.
326           MIRBuilder.buildMerge(Args[i].OrigRegs[0], Args[i].Regs);
327           continue;
328         }
329         const LLT VATy(VAVT);
330         Register NewReg =
331             MIRBuilder.getMRI()->createGenericVirtualRegister(VATy);
332         Handler.assignValueToReg(NewReg, VA.getLocReg(), VA);
333         // If it's a vector type, we either need to truncate the elements
334         // or do an unmerge to get the lower block of elements.
335         if (VATy.isVector() &&
336             VATy.getNumElements() > OrigVT.getVectorNumElements()) {
337           const LLT OrigTy(OrigVT);
338           // Just handle the case where the VA type is 2 * original type.
339           if (VATy.getNumElements() != OrigVT.getVectorNumElements() * 2) {
340             LLVM_DEBUG(dbgs()
341                        << "Incoming promoted vector arg has too many elts");
342             return false;
343           }
344           auto Unmerge = MIRBuilder.buildUnmerge({OrigTy, OrigTy}, {NewReg});
345           MIRBuilder.buildCopy(ArgReg, Unmerge.getReg(0));
346         } else {
347           MIRBuilder.buildTrunc(ArgReg, {NewReg}).getReg(0);
348         }
349       } else if (!Handler.isIncomingArgumentHandler()) {
350         assert((j + (Args[i].Regs.size() - 1)) < ArgLocs.size() &&
351                "Too many regs for number of args");
352         // This is an outgoing argument that might have been split.
353         for (unsigned Part = 0; Part < Args[i].Regs.size(); ++Part) {
354           // There should be Regs.size() ArgLocs per argument.
355           VA = ArgLocs[j + Part];
356           Handler.assignValueToReg(Args[i].Regs[Part], VA.getLocReg(), VA);
357         }
358         j += Args[i].Regs.size() - 1;
359       } else {
360         Handler.assignValueToReg(ArgReg, VA.getLocReg(), VA);
361       }
362     } else if (VA.isMemLoc()) {
363       // Don't currently support loading/storing a type that needs to be split
364       // to the stack. Should be easy, just not implemented yet.
365       if (Args[i].Regs.size() > 1) {
366         LLVM_DEBUG(
367             dbgs()
368             << "Load/store a split arg to/from the stack not implemented yet");
369         return false;
370       }
371       MVT VT = MVT::getVT(Args[i].Ty);
372       unsigned Size = VT == MVT::iPTR ? DL.getPointerSize()
373                                       : alignTo(VT.getSizeInBits(), 8) / 8;
374       unsigned Offset = VA.getLocMemOffset();
375       MachinePointerInfo MPO;
376       Register StackAddr = Handler.getStackAddress(Size, Offset, MPO);
377       Handler.assignValueToAddress(ArgReg, StackAddr, Size, MPO, VA);
378     } else {
379       // FIXME: Support byvals and other weirdness
380       return false;
381     }
382   }
383   return true;
384 }
385 
386 bool CallLowering::analyzeArgInfo(CCState &CCState,
387                                   SmallVectorImpl<ArgInfo> &Args,
388                                   CCAssignFn &AssignFnFixed,
389                                   CCAssignFn &AssignFnVarArg) const {
390   for (unsigned i = 0, e = Args.size(); i < e; ++i) {
391     MVT VT = MVT::getVT(Args[i].Ty);
392     CCAssignFn &Fn = Args[i].IsFixed ? AssignFnFixed : AssignFnVarArg;
393     if (Fn(i, VT, VT, CCValAssign::Full, Args[i].Flags[0], CCState)) {
394       // Bail out on anything we can't handle.
395       LLVM_DEBUG(dbgs() << "Cannot analyze " << EVT(VT).getEVTString()
396                         << " (arg number = " << i << "\n");
397       return false;
398     }
399   }
400   return true;
401 }
402 
403 bool CallLowering::resultsCompatible(CallLoweringInfo &Info,
404                                      MachineFunction &MF,
405                                      SmallVectorImpl<ArgInfo> &InArgs,
406                                      CCAssignFn &CalleeAssignFnFixed,
407                                      CCAssignFn &CalleeAssignFnVarArg,
408                                      CCAssignFn &CallerAssignFnFixed,
409                                      CCAssignFn &CallerAssignFnVarArg) const {
410   const Function &F = MF.getFunction();
411   CallingConv::ID CalleeCC = Info.CallConv;
412   CallingConv::ID CallerCC = F.getCallingConv();
413 
414   if (CallerCC == CalleeCC)
415     return true;
416 
417   SmallVector<CCValAssign, 16> ArgLocs1;
418   CCState CCInfo1(CalleeCC, false, MF, ArgLocs1, F.getContext());
419   if (!analyzeArgInfo(CCInfo1, InArgs, CalleeAssignFnFixed,
420                       CalleeAssignFnVarArg))
421     return false;
422 
423   SmallVector<CCValAssign, 16> ArgLocs2;
424   CCState CCInfo2(CallerCC, false, MF, ArgLocs2, F.getContext());
425   if (!analyzeArgInfo(CCInfo2, InArgs, CallerAssignFnFixed,
426                       CalleeAssignFnVarArg))
427     return false;
428 
429   // We need the argument locations to match up exactly. If there's more in
430   // one than the other, then we are done.
431   if (ArgLocs1.size() != ArgLocs2.size())
432     return false;
433 
434   // Make sure that each location is passed in exactly the same way.
435   for (unsigned i = 0, e = ArgLocs1.size(); i < e; ++i) {
436     const CCValAssign &Loc1 = ArgLocs1[i];
437     const CCValAssign &Loc2 = ArgLocs2[i];
438 
439     // We need both of them to be the same. So if one is a register and one
440     // isn't, we're done.
441     if (Loc1.isRegLoc() != Loc2.isRegLoc())
442       return false;
443 
444     if (Loc1.isRegLoc()) {
445       // If they don't have the same register location, we're done.
446       if (Loc1.getLocReg() != Loc2.getLocReg())
447         return false;
448 
449       // They matched, so we can move to the next ArgLoc.
450       continue;
451     }
452 
453     // Loc1 wasn't a RegLoc, so they both must be MemLocs. Check if they match.
454     if (Loc1.getLocMemOffset() != Loc2.getLocMemOffset())
455       return false;
456   }
457 
458   return true;
459 }
460 
461 Register CallLowering::ValueHandler::extendRegister(Register ValReg,
462                                                     CCValAssign &VA) {
463   LLT LocTy{VA.getLocVT()};
464   if (LocTy.getSizeInBits() == MRI.getType(ValReg).getSizeInBits())
465     return ValReg;
466   switch (VA.getLocInfo()) {
467   default: break;
468   case CCValAssign::Full:
469   case CCValAssign::BCvt:
470     // FIXME: bitconverting between vector types may or may not be a
471     // nop in big-endian situations.
472     return ValReg;
473   case CCValAssign::AExt: {
474     auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg);
475     return MIB.getReg(0);
476   }
477   case CCValAssign::SExt: {
478     Register NewReg = MRI.createGenericVirtualRegister(LocTy);
479     MIRBuilder.buildSExt(NewReg, ValReg);
480     return NewReg;
481   }
482   case CCValAssign::ZExt: {
483     Register NewReg = MRI.createGenericVirtualRegister(LocTy);
484     MIRBuilder.buildZExt(NewReg, ValReg);
485     return NewReg;
486   }
487   }
488   llvm_unreachable("unable to extend register");
489 }
490 
491 void CallLowering::ValueHandler::anchor() {}
492