10b57cec5SDimitry Andric //===-- lib/CodeGen/GlobalISel/CallLowering.cpp - Call lowering -----------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This file implements some simple delegations needed for call lowering. 110b57cec5SDimitry Andric /// 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 1481ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/CallLowering.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h" 16349cc55cSDimitry Andric #include "llvm/CodeGen/CallingConvLower.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 1881ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/Utils.h" 1981ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 230b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 24*0fca6ea1SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 258bcb0991SDimitry Andric #include "llvm/IR/LLVMContext.h" 260b57cec5SDimitry Andric #include "llvm/IR/Module.h" 275ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h" 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric #define DEBUG_TYPE "call-lowering" 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric using namespace llvm; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric void CallLowering::anchor() {} 340b57cec5SDimitry Andric 35e8d8bef9SDimitry Andric /// Helper function which updates \p Flags when \p AttrFn returns true. 36e8d8bef9SDimitry Andric static void 37e8d8bef9SDimitry Andric addFlagsUsingAttrFn(ISD::ArgFlagsTy &Flags, 38e8d8bef9SDimitry Andric const std::function<bool(Attribute::AttrKind)> &AttrFn) { 39*0fca6ea1SDimitry Andric // TODO: There are missing flags. Add them here. 40e8d8bef9SDimitry Andric if (AttrFn(Attribute::SExt)) 41e8d8bef9SDimitry Andric Flags.setSExt(); 42e8d8bef9SDimitry Andric if (AttrFn(Attribute::ZExt)) 43e8d8bef9SDimitry Andric Flags.setZExt(); 44e8d8bef9SDimitry Andric if (AttrFn(Attribute::InReg)) 45e8d8bef9SDimitry Andric Flags.setInReg(); 46e8d8bef9SDimitry Andric if (AttrFn(Attribute::StructRet)) 47e8d8bef9SDimitry Andric Flags.setSRet(); 48e8d8bef9SDimitry Andric if (AttrFn(Attribute::Nest)) 49e8d8bef9SDimitry Andric Flags.setNest(); 50e8d8bef9SDimitry Andric if (AttrFn(Attribute::ByVal)) 51e8d8bef9SDimitry Andric Flags.setByVal(); 52*0fca6ea1SDimitry Andric if (AttrFn(Attribute::ByRef)) 53*0fca6ea1SDimitry Andric Flags.setByRef(); 54e8d8bef9SDimitry Andric if (AttrFn(Attribute::Preallocated)) 55e8d8bef9SDimitry Andric Flags.setPreallocated(); 56e8d8bef9SDimitry Andric if (AttrFn(Attribute::InAlloca)) 57e8d8bef9SDimitry Andric Flags.setInAlloca(); 58e8d8bef9SDimitry Andric if (AttrFn(Attribute::Returned)) 59e8d8bef9SDimitry Andric Flags.setReturned(); 60e8d8bef9SDimitry Andric if (AttrFn(Attribute::SwiftSelf)) 61e8d8bef9SDimitry Andric Flags.setSwiftSelf(); 62fe6060f1SDimitry Andric if (AttrFn(Attribute::SwiftAsync)) 63fe6060f1SDimitry Andric Flags.setSwiftAsync(); 64e8d8bef9SDimitry Andric if (AttrFn(Attribute::SwiftError)) 65e8d8bef9SDimitry Andric Flags.setSwiftError(); 66e8d8bef9SDimitry Andric } 67e8d8bef9SDimitry Andric 68e8d8bef9SDimitry Andric ISD::ArgFlagsTy CallLowering::getAttributesForArgIdx(const CallBase &Call, 69e8d8bef9SDimitry Andric unsigned ArgIdx) const { 70e8d8bef9SDimitry Andric ISD::ArgFlagsTy Flags; 71e8d8bef9SDimitry Andric addFlagsUsingAttrFn(Flags, [&Call, &ArgIdx](Attribute::AttrKind Attr) { 72e8d8bef9SDimitry Andric return Call.paramHasAttr(ArgIdx, Attr); 73e8d8bef9SDimitry Andric }); 74e8d8bef9SDimitry Andric return Flags; 75e8d8bef9SDimitry Andric } 76e8d8bef9SDimitry Andric 77bdd1243dSDimitry Andric ISD::ArgFlagsTy 78bdd1243dSDimitry Andric CallLowering::getAttributesForReturn(const CallBase &Call) const { 79bdd1243dSDimitry Andric ISD::ArgFlagsTy Flags; 80bdd1243dSDimitry Andric addFlagsUsingAttrFn(Flags, [&Call](Attribute::AttrKind Attr) { 81bdd1243dSDimitry Andric return Call.hasRetAttr(Attr); 82bdd1243dSDimitry Andric }); 83bdd1243dSDimitry Andric return Flags; 84bdd1243dSDimitry Andric } 85bdd1243dSDimitry Andric 86e8d8bef9SDimitry Andric void CallLowering::addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags, 87e8d8bef9SDimitry Andric const AttributeList &Attrs, 88e8d8bef9SDimitry Andric unsigned OpIdx) const { 89e8d8bef9SDimitry Andric addFlagsUsingAttrFn(Flags, [&Attrs, &OpIdx](Attribute::AttrKind Attr) { 90349cc55cSDimitry Andric return Attrs.hasAttributeAtIndex(OpIdx, Attr); 91e8d8bef9SDimitry Andric }); 92e8d8bef9SDimitry Andric } 93e8d8bef9SDimitry Andric 945ffd83dbSDimitry Andric bool CallLowering::lowerCall(MachineIRBuilder &MIRBuilder, const CallBase &CB, 950b57cec5SDimitry Andric ArrayRef<Register> ResRegs, 960b57cec5SDimitry Andric ArrayRef<ArrayRef<Register>> ArgRegs, 970b57cec5SDimitry Andric Register SwiftErrorVReg, 98*0fca6ea1SDimitry Andric std::optional<PtrAuthInfo> PAI, 99*0fca6ea1SDimitry Andric Register ConvergenceCtrlToken, 1000b57cec5SDimitry Andric std::function<unsigned()> GetCalleeReg) const { 1018bcb0991SDimitry Andric CallLoweringInfo Info; 1025ffd83dbSDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 103e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 10404eeddc0SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 105e8d8bef9SDimitry Andric bool CanBeTailCalled = CB.isTailCall() && 106e8d8bef9SDimitry Andric isInTailCallPosition(CB, MF.getTarget()) && 107e8d8bef9SDimitry Andric (MF.getFunction() 108e8d8bef9SDimitry Andric .getFnAttribute("disable-tail-calls") 109e8d8bef9SDimitry Andric .getValueAsString() != "true"); 110e8d8bef9SDimitry Andric 111e8d8bef9SDimitry Andric CallingConv::ID CallConv = CB.getCallingConv(); 112e8d8bef9SDimitry Andric Type *RetTy = CB.getType(); 113e8d8bef9SDimitry Andric bool IsVarArg = CB.getFunctionType()->isVarArg(); 114e8d8bef9SDimitry Andric 115e8d8bef9SDimitry Andric SmallVector<BaseArgInfo, 4> SplitArgs; 116e8d8bef9SDimitry Andric getReturnInfo(CallConv, RetTy, CB.getAttributes(), SplitArgs, DL); 117e8d8bef9SDimitry Andric Info.CanLowerReturn = canLowerReturn(MF, CallConv, SplitArgs, IsVarArg); 118e8d8bef9SDimitry Andric 1195f757f3fSDimitry Andric Info.IsConvergent = CB.isConvergent(); 1205f757f3fSDimitry Andric 121e8d8bef9SDimitry Andric if (!Info.CanLowerReturn) { 122e8d8bef9SDimitry Andric // Callee requires sret demotion. 123e8d8bef9SDimitry Andric insertSRetOutgoingArgument(MIRBuilder, CB, Info); 124e8d8bef9SDimitry Andric 125e8d8bef9SDimitry Andric // The sret demotion isn't compatible with tail-calls, since the sret 126e8d8bef9SDimitry Andric // argument points into the caller's stack frame. 127e8d8bef9SDimitry Andric CanBeTailCalled = false; 128e8d8bef9SDimitry Andric } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric // First step is to marshall all the function's parameters into the correct 1310b57cec5SDimitry Andric // physregs and memory locations. Gather the sequence of argument types that 1320b57cec5SDimitry Andric // we'll pass to the assigner function. 1330b57cec5SDimitry Andric unsigned i = 0; 1345ffd83dbSDimitry Andric unsigned NumFixedArgs = CB.getFunctionType()->getNumParams(); 135fcaf7f86SDimitry Andric for (const auto &Arg : CB.args()) { 136fe6060f1SDimitry Andric ArgInfo OrigArg{ArgRegs[i], *Arg.get(), i, getAttributesForArgIdx(CB, i), 1370b57cec5SDimitry Andric i < NumFixedArgs}; 1385ffd83dbSDimitry Andric setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CB); 139e8d8bef9SDimitry Andric 140e8d8bef9SDimitry Andric // If we have an explicit sret argument that is an Instruction, (i.e., it 141e8d8bef9SDimitry Andric // might point to function-local memory), we can't meaningfully tail-call. 142e8d8bef9SDimitry Andric if (OrigArg.Flags[0].isSRet() && isa<Instruction>(&Arg)) 143e8d8bef9SDimitry Andric CanBeTailCalled = false; 144e8d8bef9SDimitry Andric 1458bcb0991SDimitry Andric Info.OrigArgs.push_back(OrigArg); 1460b57cec5SDimitry Andric ++i; 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 1495ffd83dbSDimitry Andric // Try looking through a bitcast from one function type to another. 1505ffd83dbSDimitry Andric // Commonly happens with calls to objc_msgSend(). 1515ffd83dbSDimitry Andric const Value *CalleeV = CB.getCalledOperand()->stripPointerCasts(); 152*0fca6ea1SDimitry Andric 153*0fca6ea1SDimitry Andric // If IRTranslator chose to drop the ptrauth info, we can turn this into 154*0fca6ea1SDimitry Andric // a direct call. 155*0fca6ea1SDimitry Andric if (!PAI && CB.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) { 156*0fca6ea1SDimitry Andric CalleeV = cast<ConstantPtrAuth>(CalleeV)->getPointer(); 157*0fca6ea1SDimitry Andric assert(isa<Function>(CalleeV)); 158*0fca6ea1SDimitry Andric } 159*0fca6ea1SDimitry Andric 160*0fca6ea1SDimitry Andric if (const Function *F = dyn_cast<Function>(CalleeV)) { 161*0fca6ea1SDimitry Andric if (F->hasFnAttribute(Attribute::NonLazyBind)) { 162*0fca6ea1SDimitry Andric LLT Ty = getLLTForType(*F->getType(), DL); 163*0fca6ea1SDimitry Andric Register Reg = MIRBuilder.buildGlobalValue(Ty, F).getReg(0); 164*0fca6ea1SDimitry Andric Info.Callee = MachineOperand::CreateReg(Reg, false); 165*0fca6ea1SDimitry Andric } else { 1668bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateGA(F, 0); 167*0fca6ea1SDimitry Andric } 168*0fca6ea1SDimitry Andric } else if (isa<GlobalIFunc>(CalleeV) || isa<GlobalAlias>(CalleeV)) { 1695f757f3fSDimitry Andric // IR IFuncs and Aliases can't be forward declared (only defined), so the 1705f757f3fSDimitry Andric // callee must be in the same TU and therefore we can direct-call it without 1715f757f3fSDimitry Andric // worrying about it being out of range. 1725f757f3fSDimitry Andric Info.Callee = MachineOperand::CreateGA(cast<GlobalValue>(CalleeV), 0); 1735f757f3fSDimitry Andric } else 1748bcb0991SDimitry Andric Info.Callee = MachineOperand::CreateReg(GetCalleeReg(), false); 1750b57cec5SDimitry Andric 17604eeddc0SDimitry Andric Register ReturnHintAlignReg; 17704eeddc0SDimitry Andric Align ReturnHintAlign; 17804eeddc0SDimitry Andric 179bdd1243dSDimitry Andric Info.OrigRet = ArgInfo{ResRegs, RetTy, 0, getAttributesForReturn(CB)}; 18004eeddc0SDimitry Andric 18104eeddc0SDimitry Andric if (!Info.OrigRet.Ty->isVoidTy()) { 1825ffd83dbSDimitry Andric setArgFlags(Info.OrigRet, AttributeList::ReturnIndex, DL, CB); 1830b57cec5SDimitry Andric 18404eeddc0SDimitry Andric if (MaybeAlign Alignment = CB.getRetAlign()) { 18504eeddc0SDimitry Andric if (*Alignment > Align(1)) { 18604eeddc0SDimitry Andric ReturnHintAlignReg = MRI.cloneVirtualRegister(ResRegs[0]); 18704eeddc0SDimitry Andric Info.OrigRet.Regs[0] = ReturnHintAlignReg; 18804eeddc0SDimitry Andric ReturnHintAlign = *Alignment; 18904eeddc0SDimitry Andric } 19004eeddc0SDimitry Andric } 19104eeddc0SDimitry Andric } 19204eeddc0SDimitry Andric 193bdd1243dSDimitry Andric auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi); 194bdd1243dSDimitry Andric if (Bundle && CB.isIndirectCall()) { 195bdd1243dSDimitry Andric Info.CFIType = cast<ConstantInt>(Bundle->Inputs[0]); 196bdd1243dSDimitry Andric assert(Info.CFIType->getType()->isIntegerTy(32) && "Invalid CFI type"); 197bdd1243dSDimitry Andric } 198bdd1243dSDimitry Andric 199349cc55cSDimitry Andric Info.CB = &CB; 2005ffd83dbSDimitry Andric Info.KnownCallees = CB.getMetadata(LLVMContext::MD_callees); 201e8d8bef9SDimitry Andric Info.CallConv = CallConv; 2028bcb0991SDimitry Andric Info.SwiftErrorVReg = SwiftErrorVReg; 203*0fca6ea1SDimitry Andric Info.PAI = PAI; 204*0fca6ea1SDimitry Andric Info.ConvergenceCtrlToken = ConvergenceCtrlToken; 2055ffd83dbSDimitry Andric Info.IsMustTailCall = CB.isMustTailCall(); 206e8d8bef9SDimitry Andric Info.IsTailCall = CanBeTailCalled; 207e8d8bef9SDimitry Andric Info.IsVarArg = IsVarArg; 20804eeddc0SDimitry Andric if (!lowerCall(MIRBuilder, Info)) 20904eeddc0SDimitry Andric return false; 21004eeddc0SDimitry Andric 211*0fca6ea1SDimitry Andric if (ReturnHintAlignReg && !Info.LoweredTailCall) { 21204eeddc0SDimitry Andric MIRBuilder.buildAssertAlign(ResRegs[0], ReturnHintAlignReg, 21304eeddc0SDimitry Andric ReturnHintAlign); 21404eeddc0SDimitry Andric } 21504eeddc0SDimitry Andric 21604eeddc0SDimitry Andric return true; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric template <typename FuncInfoTy> 2200b57cec5SDimitry Andric void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx, 2210b57cec5SDimitry Andric const DataLayout &DL, 2220b57cec5SDimitry Andric const FuncInfoTy &FuncInfo) const { 2238bcb0991SDimitry Andric auto &Flags = Arg.Flags[0]; 2240b57cec5SDimitry Andric const AttributeList &Attrs = FuncInfo.getAttributes(); 225e8d8bef9SDimitry Andric addArgFlagsFromAttributes(Flags, Attrs, OpIdx); 2260b57cec5SDimitry Andric 227fe6060f1SDimitry Andric PointerType *PtrTy = dyn_cast<PointerType>(Arg.Ty->getScalarType()); 228fe6060f1SDimitry Andric if (PtrTy) { 229fe6060f1SDimitry Andric Flags.setPointer(); 230fe6060f1SDimitry Andric Flags.setPointerAddrSpace(PtrTy->getPointerAddressSpace()); 231fe6060f1SDimitry Andric } 232fe6060f1SDimitry Andric 233fe6060f1SDimitry Andric Align MemAlign = DL.getABITypeAlign(Arg.Ty); 234*0fca6ea1SDimitry Andric if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() || 235*0fca6ea1SDimitry Andric Flags.isByRef()) { 236fe6060f1SDimitry Andric assert(OpIdx >= AttributeList::FirstArgIndex); 237349cc55cSDimitry Andric unsigned ParamIdx = OpIdx - AttributeList::FirstArgIndex; 2380b57cec5SDimitry Andric 239349cc55cSDimitry Andric Type *ElementTy = FuncInfo.getParamByValType(ParamIdx); 240349cc55cSDimitry Andric if (!ElementTy) 241*0fca6ea1SDimitry Andric ElementTy = FuncInfo.getParamByRefType(ParamIdx); 242*0fca6ea1SDimitry Andric if (!ElementTy) 243349cc55cSDimitry Andric ElementTy = FuncInfo.getParamInAllocaType(ParamIdx); 244349cc55cSDimitry Andric if (!ElementTy) 245349cc55cSDimitry Andric ElementTy = FuncInfo.getParamPreallocatedType(ParamIdx); 246*0fca6ea1SDimitry Andric 247349cc55cSDimitry Andric assert(ElementTy && "Must have byval, inalloca or preallocated type"); 248*0fca6ea1SDimitry Andric 249*0fca6ea1SDimitry Andric uint64_t MemSize = DL.getTypeAllocSize(ElementTy); 250*0fca6ea1SDimitry Andric if (Flags.isByRef()) 251*0fca6ea1SDimitry Andric Flags.setByRefSize(MemSize); 252*0fca6ea1SDimitry Andric else 253*0fca6ea1SDimitry Andric Flags.setByValSize(MemSize); 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric // For ByVal, alignment should be passed from FE. BE will guess if 2560b57cec5SDimitry Andric // this info is not there but there are cases it cannot get right. 257349cc55cSDimitry Andric if (auto ParamAlign = FuncInfo.getParamStackAlign(ParamIdx)) 258fe6060f1SDimitry Andric MemAlign = *ParamAlign; 259349cc55cSDimitry Andric else if ((ParamAlign = FuncInfo.getParamAlign(ParamIdx))) 260fe6060f1SDimitry Andric MemAlign = *ParamAlign; 2610b57cec5SDimitry Andric else 262fe6060f1SDimitry Andric MemAlign = Align(getTLI()->getByValTypeAlignment(ElementTy, DL)); 263fe6060f1SDimitry Andric } else if (OpIdx >= AttributeList::FirstArgIndex) { 264fe6060f1SDimitry Andric if (auto ParamAlign = 265fe6060f1SDimitry Andric FuncInfo.getParamStackAlign(OpIdx - AttributeList::FirstArgIndex)) 266fe6060f1SDimitry Andric MemAlign = *ParamAlign; 2670b57cec5SDimitry Andric } 268fe6060f1SDimitry Andric Flags.setMemAlign(MemAlign); 2695ffd83dbSDimitry Andric Flags.setOrigAlign(DL.getABITypeAlign(Arg.Ty)); 270fe6060f1SDimitry Andric 271fe6060f1SDimitry Andric // Don't try to use the returned attribute if the argument is marked as 272fe6060f1SDimitry Andric // swiftself, since it won't be passed in x0. 273fe6060f1SDimitry Andric if (Flags.isSwiftSelf()) 274fe6060f1SDimitry Andric Flags.setReturned(false); 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric template void 2780b57cec5SDimitry Andric CallLowering::setArgFlags<Function>(CallLowering::ArgInfo &Arg, unsigned OpIdx, 2790b57cec5SDimitry Andric const DataLayout &DL, 2800b57cec5SDimitry Andric const Function &FuncInfo) const; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric template void 2835ffd83dbSDimitry Andric CallLowering::setArgFlags<CallBase>(CallLowering::ArgInfo &Arg, unsigned OpIdx, 2840b57cec5SDimitry Andric const DataLayout &DL, 2855ffd83dbSDimitry Andric const CallBase &FuncInfo) const; 2860b57cec5SDimitry Andric 287fe6060f1SDimitry Andric void CallLowering::splitToValueTypes(const ArgInfo &OrigArg, 288fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &SplitArgs, 289fe6060f1SDimitry Andric const DataLayout &DL, 290fe6060f1SDimitry Andric CallingConv::ID CallConv, 291fe6060f1SDimitry Andric SmallVectorImpl<uint64_t> *Offsets) const { 292fe6060f1SDimitry Andric LLVMContext &Ctx = OrigArg.Ty->getContext(); 2930b57cec5SDimitry Andric 294fe6060f1SDimitry Andric SmallVector<EVT, 4> SplitVTs; 295fe6060f1SDimitry Andric ComputeValueVTs(*TLI, DL, OrigArg.Ty, SplitVTs, Offsets, 0); 2960b57cec5SDimitry Andric 297fe6060f1SDimitry Andric if (SplitVTs.size() == 0) 298fe6060f1SDimitry Andric return; 2990b57cec5SDimitry Andric 300fe6060f1SDimitry Andric if (SplitVTs.size() == 1) { 301fe6060f1SDimitry Andric // No splitting to do, but we want to replace the original type (e.g. [1 x 302fe6060f1SDimitry Andric // double] -> double). 303fe6060f1SDimitry Andric SplitArgs.emplace_back(OrigArg.Regs[0], SplitVTs[0].getTypeForEVT(Ctx), 304fe6060f1SDimitry Andric OrigArg.OrigArgIndex, OrigArg.Flags[0], 305fe6060f1SDimitry Andric OrigArg.IsFixed, OrigArg.OrigValue); 306fe6060f1SDimitry Andric return; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 309fe6060f1SDimitry Andric // Create one ArgInfo for each virtual register in the original ArgInfo. 310fe6060f1SDimitry Andric assert(OrigArg.Regs.size() == SplitVTs.size() && "Regs / types mismatch"); 311fe6060f1SDimitry Andric 312fe6060f1SDimitry Andric bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters( 313fe6060f1SDimitry Andric OrigArg.Ty, CallConv, false, DL); 314fe6060f1SDimitry Andric for (unsigned i = 0, e = SplitVTs.size(); i < e; ++i) { 315fe6060f1SDimitry Andric Type *SplitTy = SplitVTs[i].getTypeForEVT(Ctx); 316fe6060f1SDimitry Andric SplitArgs.emplace_back(OrigArg.Regs[i], SplitTy, OrigArg.OrigArgIndex, 317fe6060f1SDimitry Andric OrigArg.Flags[0], OrigArg.IsFixed); 318fe6060f1SDimitry Andric if (NeedsRegBlock) 319fe6060f1SDimitry Andric SplitArgs.back().Flags[0].setInConsecutiveRegs(); 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 322fe6060f1SDimitry Andric SplitArgs.back().Flags[0].setInConsecutiveRegsLast(); 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric 325fe6060f1SDimitry Andric /// Pack values \p SrcRegs to cover the vector type result \p DstRegs. 326fe6060f1SDimitry Andric static MachineInstrBuilder 327fe6060f1SDimitry Andric mergeVectorRegsToResultRegs(MachineIRBuilder &B, ArrayRef<Register> DstRegs, 328fe6060f1SDimitry Andric ArrayRef<Register> SrcRegs) { 329fe6060f1SDimitry Andric MachineRegisterInfo &MRI = *B.getMRI(); 330fe6060f1SDimitry Andric LLT LLTy = MRI.getType(DstRegs[0]); 331fe6060f1SDimitry Andric LLT PartLLT = MRI.getType(SrcRegs[0]); 332fe6060f1SDimitry Andric 333fe6060f1SDimitry Andric // Deal with v3s16 split into v2s16 3340eae32dcSDimitry Andric LLT LCMTy = getCoverTy(LLTy, PartLLT); 335fe6060f1SDimitry Andric if (LCMTy == LLTy) { 336fe6060f1SDimitry Andric // Common case where no padding is needed. 337fe6060f1SDimitry Andric assert(DstRegs.size() == 1); 338fe6060f1SDimitry Andric return B.buildConcatVectors(DstRegs[0], SrcRegs); 339fe6060f1SDimitry Andric } 340fe6060f1SDimitry Andric 341fe6060f1SDimitry Andric // We need to create an unmerge to the result registers, which may require 342fe6060f1SDimitry Andric // widening the original value. 343fe6060f1SDimitry Andric Register UnmergeSrcReg; 344fe6060f1SDimitry Andric if (LCMTy != PartLLT) { 3450eae32dcSDimitry Andric assert(DstRegs.size() == 1); 346bdd1243dSDimitry Andric return B.buildDeleteTrailingVectorElements( 347bdd1243dSDimitry Andric DstRegs[0], B.buildMergeLikeInstr(LCMTy, SrcRegs)); 348fe6060f1SDimitry Andric } else { 349fe6060f1SDimitry Andric // We don't need to widen anything if we're extracting a scalar which was 350fe6060f1SDimitry Andric // promoted to a vector e.g. s8 -> v4s8 -> s8 351fe6060f1SDimitry Andric assert(SrcRegs.size() == 1); 352fe6060f1SDimitry Andric UnmergeSrcReg = SrcRegs[0]; 353fe6060f1SDimitry Andric } 354fe6060f1SDimitry Andric 355fe6060f1SDimitry Andric int NumDst = LCMTy.getSizeInBits() / LLTy.getSizeInBits(); 356fe6060f1SDimitry Andric 357fe6060f1SDimitry Andric SmallVector<Register, 8> PadDstRegs(NumDst); 358fe6060f1SDimitry Andric std::copy(DstRegs.begin(), DstRegs.end(), PadDstRegs.begin()); 359fe6060f1SDimitry Andric 360fe6060f1SDimitry Andric // Create the excess dead defs for the unmerge. 361fe6060f1SDimitry Andric for (int I = DstRegs.size(); I != NumDst; ++I) 362fe6060f1SDimitry Andric PadDstRegs[I] = MRI.createGenericVirtualRegister(LLTy); 363fe6060f1SDimitry Andric 3640eae32dcSDimitry Andric if (PadDstRegs.size() == 1) 3650eae32dcSDimitry Andric return B.buildDeleteTrailingVectorElements(DstRegs[0], UnmergeSrcReg); 366fe6060f1SDimitry Andric return B.buildUnmerge(PadDstRegs, UnmergeSrcReg); 367fe6060f1SDimitry Andric } 368fe6060f1SDimitry Andric 369fe6060f1SDimitry Andric /// Create a sequence of instructions to combine pieces split into register 370fe6060f1SDimitry Andric /// typed values to the original IR value. \p OrigRegs contains the destination 371fe6060f1SDimitry Andric /// value registers of type \p LLTy, and \p Regs contains the legalized pieces 372fe6060f1SDimitry Andric /// with type \p PartLLT. This is used for incoming values (physregs to vregs). 373fe6060f1SDimitry Andric static void buildCopyFromRegs(MachineIRBuilder &B, ArrayRef<Register> OrigRegs, 374fe6060f1SDimitry Andric ArrayRef<Register> Regs, LLT LLTy, LLT PartLLT, 375fe6060f1SDimitry Andric const ISD::ArgFlagsTy Flags) { 376fe6060f1SDimitry Andric MachineRegisterInfo &MRI = *B.getMRI(); 377fe6060f1SDimitry Andric 378fe6060f1SDimitry Andric if (PartLLT == LLTy) { 379fe6060f1SDimitry Andric // We should have avoided introducing a new virtual register, and just 380fe6060f1SDimitry Andric // directly assigned here. 381fe6060f1SDimitry Andric assert(OrigRegs[0] == Regs[0]); 382fe6060f1SDimitry Andric return; 383fe6060f1SDimitry Andric } 384fe6060f1SDimitry Andric 385fe6060f1SDimitry Andric if (PartLLT.getSizeInBits() == LLTy.getSizeInBits() && OrigRegs.size() == 1 && 386fe6060f1SDimitry Andric Regs.size() == 1) { 387fe6060f1SDimitry Andric B.buildBitcast(OrigRegs[0], Regs[0]); 388fe6060f1SDimitry Andric return; 389fe6060f1SDimitry Andric } 390fe6060f1SDimitry Andric 391fe6060f1SDimitry Andric // A vector PartLLT needs extending to LLTy's element size. 392fe6060f1SDimitry Andric // E.g. <2 x s64> = G_SEXT <2 x s32>. 393fe6060f1SDimitry Andric if (PartLLT.isVector() == LLTy.isVector() && 394fe6060f1SDimitry Andric PartLLT.getScalarSizeInBits() > LLTy.getScalarSizeInBits() && 395fe6060f1SDimitry Andric (!PartLLT.isVector() || 3965f757f3fSDimitry Andric PartLLT.getElementCount() == LLTy.getElementCount()) && 397fe6060f1SDimitry Andric OrigRegs.size() == 1 && Regs.size() == 1) { 398fe6060f1SDimitry Andric Register SrcReg = Regs[0]; 399fe6060f1SDimitry Andric 400fe6060f1SDimitry Andric LLT LocTy = MRI.getType(SrcReg); 401fe6060f1SDimitry Andric 402fe6060f1SDimitry Andric if (Flags.isSExt()) { 403fe6060f1SDimitry Andric SrcReg = B.buildAssertSExt(LocTy, SrcReg, LLTy.getScalarSizeInBits()) 404fe6060f1SDimitry Andric .getReg(0); 405fe6060f1SDimitry Andric } else if (Flags.isZExt()) { 406fe6060f1SDimitry Andric SrcReg = B.buildAssertZExt(LocTy, SrcReg, LLTy.getScalarSizeInBits()) 407fe6060f1SDimitry Andric .getReg(0); 408fe6060f1SDimitry Andric } 409fe6060f1SDimitry Andric 410fe6060f1SDimitry Andric // Sometimes pointers are passed zero extended. 411fe6060f1SDimitry Andric LLT OrigTy = MRI.getType(OrigRegs[0]); 412fe6060f1SDimitry Andric if (OrigTy.isPointer()) { 413fe6060f1SDimitry Andric LLT IntPtrTy = LLT::scalar(OrigTy.getSizeInBits()); 414fe6060f1SDimitry Andric B.buildIntToPtr(OrigRegs[0], B.buildTrunc(IntPtrTy, SrcReg)); 415fe6060f1SDimitry Andric return; 416fe6060f1SDimitry Andric } 417fe6060f1SDimitry Andric 418fe6060f1SDimitry Andric B.buildTrunc(OrigRegs[0], SrcReg); 419fe6060f1SDimitry Andric return; 420fe6060f1SDimitry Andric } 421fe6060f1SDimitry Andric 422fe6060f1SDimitry Andric if (!LLTy.isVector() && !PartLLT.isVector()) { 423fe6060f1SDimitry Andric assert(OrigRegs.size() == 1); 424fe6060f1SDimitry Andric LLT OrigTy = MRI.getType(OrigRegs[0]); 425fe6060f1SDimitry Andric 426bdd1243dSDimitry Andric unsigned SrcSize = PartLLT.getSizeInBits().getFixedValue() * Regs.size(); 427fe6060f1SDimitry Andric if (SrcSize == OrigTy.getSizeInBits()) 428bdd1243dSDimitry Andric B.buildMergeValues(OrigRegs[0], Regs); 429fe6060f1SDimitry Andric else { 430bdd1243dSDimitry Andric auto Widened = B.buildMergeLikeInstr(LLT::scalar(SrcSize), Regs); 431fe6060f1SDimitry Andric B.buildTrunc(OrigRegs[0], Widened); 432fe6060f1SDimitry Andric } 433fe6060f1SDimitry Andric 434fe6060f1SDimitry Andric return; 435fe6060f1SDimitry Andric } 436fe6060f1SDimitry Andric 437fe6060f1SDimitry Andric if (PartLLT.isVector()) { 438fe6060f1SDimitry Andric assert(OrigRegs.size() == 1); 439fe6060f1SDimitry Andric SmallVector<Register> CastRegs(Regs.begin(), Regs.end()); 440fe6060f1SDimitry Andric 441fe6060f1SDimitry Andric // If PartLLT is a mismatched vector in both number of elements and element 442fe6060f1SDimitry Andric // size, e.g. PartLLT == v2s64 and LLTy is v3s32, then first coerce it to 443fe6060f1SDimitry Andric // have the same elt type, i.e. v4s32. 4445f757f3fSDimitry Andric // TODO: Extend this coersion to element multiples other than just 2. 445*0fca6ea1SDimitry Andric if (TypeSize::isKnownGT(PartLLT.getSizeInBits(), LLTy.getSizeInBits()) && 446fe6060f1SDimitry Andric PartLLT.getScalarSizeInBits() == LLTy.getScalarSizeInBits() * 2 && 447fe6060f1SDimitry Andric Regs.size() == 1) { 448fe6060f1SDimitry Andric LLT NewTy = PartLLT.changeElementType(LLTy.getElementType()) 449fe6060f1SDimitry Andric .changeElementCount(PartLLT.getElementCount() * 2); 450fe6060f1SDimitry Andric CastRegs[0] = B.buildBitcast(NewTy, Regs[0]).getReg(0); 451fe6060f1SDimitry Andric PartLLT = NewTy; 452fe6060f1SDimitry Andric } 453fe6060f1SDimitry Andric 454fe6060f1SDimitry Andric if (LLTy.getScalarType() == PartLLT.getElementType()) { 455fe6060f1SDimitry Andric mergeVectorRegsToResultRegs(B, OrigRegs, CastRegs); 456fe6060f1SDimitry Andric } else { 457fe6060f1SDimitry Andric unsigned I = 0; 458fe6060f1SDimitry Andric LLT GCDTy = getGCDType(LLTy, PartLLT); 459fe6060f1SDimitry Andric 460fe6060f1SDimitry Andric // We are both splitting a vector, and bitcasting its element types. Cast 461fe6060f1SDimitry Andric // the source pieces into the appropriate number of pieces with the result 462fe6060f1SDimitry Andric // element type. 463fe6060f1SDimitry Andric for (Register SrcReg : CastRegs) 464fe6060f1SDimitry Andric CastRegs[I++] = B.buildBitcast(GCDTy, SrcReg).getReg(0); 465fe6060f1SDimitry Andric mergeVectorRegsToResultRegs(B, OrigRegs, CastRegs); 466fe6060f1SDimitry Andric } 467fe6060f1SDimitry Andric 468fe6060f1SDimitry Andric return; 469fe6060f1SDimitry Andric } 470fe6060f1SDimitry Andric 471fe6060f1SDimitry Andric assert(LLTy.isVector() && !PartLLT.isVector()); 472fe6060f1SDimitry Andric 473fe6060f1SDimitry Andric LLT DstEltTy = LLTy.getElementType(); 474fe6060f1SDimitry Andric 475fe6060f1SDimitry Andric // Pointer information was discarded. We'll need to coerce some register types 476fe6060f1SDimitry Andric // to avoid violating type constraints. 477fe6060f1SDimitry Andric LLT RealDstEltTy = MRI.getType(OrigRegs[0]).getElementType(); 478fe6060f1SDimitry Andric 479fe6060f1SDimitry Andric assert(DstEltTy.getSizeInBits() == RealDstEltTy.getSizeInBits()); 480fe6060f1SDimitry Andric 481fe6060f1SDimitry Andric if (DstEltTy == PartLLT) { 482fe6060f1SDimitry Andric // Vector was trivially scalarized. 483fe6060f1SDimitry Andric 484fe6060f1SDimitry Andric if (RealDstEltTy.isPointer()) { 485fe6060f1SDimitry Andric for (Register Reg : Regs) 486fe6060f1SDimitry Andric MRI.setType(Reg, RealDstEltTy); 487fe6060f1SDimitry Andric } 488fe6060f1SDimitry Andric 489fe6060f1SDimitry Andric B.buildBuildVector(OrigRegs[0], Regs); 490fe6060f1SDimitry Andric } else if (DstEltTy.getSizeInBits() > PartLLT.getSizeInBits()) { 491fe6060f1SDimitry Andric // Deal with vector with 64-bit elements decomposed to 32-bit 492fe6060f1SDimitry Andric // registers. Need to create intermediate 64-bit elements. 493fe6060f1SDimitry Andric SmallVector<Register, 8> EltMerges; 494*0fca6ea1SDimitry Andric int PartsPerElt = 495*0fca6ea1SDimitry Andric divideCeil(DstEltTy.getSizeInBits(), PartLLT.getSizeInBits()); 496*0fca6ea1SDimitry Andric LLT ExtendedPartTy = LLT::scalar(PartLLT.getSizeInBits() * PartsPerElt); 497fe6060f1SDimitry Andric 498fe6060f1SDimitry Andric for (int I = 0, NumElts = LLTy.getNumElements(); I != NumElts; ++I) { 499bdd1243dSDimitry Andric auto Merge = 500*0fca6ea1SDimitry Andric B.buildMergeLikeInstr(ExtendedPartTy, Regs.take_front(PartsPerElt)); 501*0fca6ea1SDimitry Andric if (ExtendedPartTy.getSizeInBits() > RealDstEltTy.getSizeInBits()) 502*0fca6ea1SDimitry Andric Merge = B.buildTrunc(RealDstEltTy, Merge); 503fe6060f1SDimitry Andric // Fix the type in case this is really a vector of pointers. 504fe6060f1SDimitry Andric MRI.setType(Merge.getReg(0), RealDstEltTy); 505fe6060f1SDimitry Andric EltMerges.push_back(Merge.getReg(0)); 506fe6060f1SDimitry Andric Regs = Regs.drop_front(PartsPerElt); 507fe6060f1SDimitry Andric } 508fe6060f1SDimitry Andric 509fe6060f1SDimitry Andric B.buildBuildVector(OrigRegs[0], EltMerges); 510fe6060f1SDimitry Andric } else { 511fe6060f1SDimitry Andric // Vector was split, and elements promoted to a wider type. 512fe6060f1SDimitry Andric // FIXME: Should handle floating point promotions. 5137a6dacacSDimitry Andric unsigned NumElts = LLTy.getNumElements(); 5147a6dacacSDimitry Andric LLT BVType = LLT::fixed_vector(NumElts, PartLLT); 5157a6dacacSDimitry Andric 5167a6dacacSDimitry Andric Register BuildVec; 5177a6dacacSDimitry Andric if (NumElts == Regs.size()) 5187a6dacacSDimitry Andric BuildVec = B.buildBuildVector(BVType, Regs).getReg(0); 5197a6dacacSDimitry Andric else { 5207a6dacacSDimitry Andric // Vector elements are packed in the inputs. 5217a6dacacSDimitry Andric // e.g. we have a <4 x s16> but 2 x s32 in regs. 5227a6dacacSDimitry Andric assert(NumElts > Regs.size()); 5237a6dacacSDimitry Andric LLT SrcEltTy = MRI.getType(Regs[0]); 5247a6dacacSDimitry Andric 5257a6dacacSDimitry Andric LLT OriginalEltTy = MRI.getType(OrigRegs[0]).getElementType(); 5267a6dacacSDimitry Andric 5277a6dacacSDimitry Andric // Input registers contain packed elements. 5287a6dacacSDimitry Andric // Determine how many elements per reg. 5297a6dacacSDimitry Andric assert((SrcEltTy.getSizeInBits() % OriginalEltTy.getSizeInBits()) == 0); 5307a6dacacSDimitry Andric unsigned EltPerReg = 5317a6dacacSDimitry Andric (SrcEltTy.getSizeInBits() / OriginalEltTy.getSizeInBits()); 5327a6dacacSDimitry Andric 5337a6dacacSDimitry Andric SmallVector<Register, 0> BVRegs; 5347a6dacacSDimitry Andric BVRegs.reserve(Regs.size() * EltPerReg); 5357a6dacacSDimitry Andric for (Register R : Regs) { 5367a6dacacSDimitry Andric auto Unmerge = B.buildUnmerge(OriginalEltTy, R); 5377a6dacacSDimitry Andric for (unsigned K = 0; K < EltPerReg; ++K) 5387a6dacacSDimitry Andric BVRegs.push_back(B.buildAnyExt(PartLLT, Unmerge.getReg(K)).getReg(0)); 5397a6dacacSDimitry Andric } 5407a6dacacSDimitry Andric 5417a6dacacSDimitry Andric // We may have some more elements in BVRegs, e.g. if we have 2 s32 pieces 5427a6dacacSDimitry Andric // for a <3 x s16> vector. We should have less than EltPerReg extra items. 5437a6dacacSDimitry Andric if (BVRegs.size() > NumElts) { 5447a6dacacSDimitry Andric assert((BVRegs.size() - NumElts) < EltPerReg); 5457a6dacacSDimitry Andric BVRegs.truncate(NumElts); 5467a6dacacSDimitry Andric } 5477a6dacacSDimitry Andric BuildVec = B.buildBuildVector(BVType, BVRegs).getReg(0); 5487a6dacacSDimitry Andric } 5497a6dacacSDimitry Andric B.buildTrunc(OrigRegs[0], BuildVec); 550fe6060f1SDimitry Andric } 551fe6060f1SDimitry Andric } 552fe6060f1SDimitry Andric 553fe6060f1SDimitry Andric /// Create a sequence of instructions to expand the value in \p SrcReg (of type 554fe6060f1SDimitry Andric /// \p SrcTy) to the types in \p DstRegs (of type \p PartTy). \p ExtendOp should 555fe6060f1SDimitry Andric /// contain the type of scalar value extension if necessary. 556fe6060f1SDimitry Andric /// 557fe6060f1SDimitry Andric /// This is used for outgoing values (vregs to physregs) 558fe6060f1SDimitry Andric static void buildCopyToRegs(MachineIRBuilder &B, ArrayRef<Register> DstRegs, 559fe6060f1SDimitry Andric Register SrcReg, LLT SrcTy, LLT PartTy, 560fe6060f1SDimitry Andric unsigned ExtendOp = TargetOpcode::G_ANYEXT) { 561fe6060f1SDimitry Andric // We could just insert a regular copy, but this is unreachable at the moment. 562fe6060f1SDimitry Andric assert(SrcTy != PartTy && "identical part types shouldn't reach here"); 563fe6060f1SDimitry Andric 564*0fca6ea1SDimitry Andric const TypeSize PartSize = PartTy.getSizeInBits(); 565fe6060f1SDimitry Andric 566fe6060f1SDimitry Andric if (PartTy.isVector() == SrcTy.isVector() && 567fe6060f1SDimitry Andric PartTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits()) { 568fe6060f1SDimitry Andric assert(DstRegs.size() == 1); 569fe6060f1SDimitry Andric B.buildInstr(ExtendOp, {DstRegs[0]}, {SrcReg}); 570fe6060f1SDimitry Andric return; 571fe6060f1SDimitry Andric } 572fe6060f1SDimitry Andric 573fe6060f1SDimitry Andric if (SrcTy.isVector() && !PartTy.isVector() && 574*0fca6ea1SDimitry Andric TypeSize::isKnownGT(PartSize, SrcTy.getElementType().getSizeInBits())) { 575fe6060f1SDimitry Andric // Vector was scalarized, and the elements extended. 576fe6060f1SDimitry Andric auto UnmergeToEltTy = B.buildUnmerge(SrcTy.getElementType(), SrcReg); 577fe6060f1SDimitry Andric for (int i = 0, e = DstRegs.size(); i != e; ++i) 578fe6060f1SDimitry Andric B.buildAnyExt(DstRegs[i], UnmergeToEltTy.getReg(i)); 579fe6060f1SDimitry Andric return; 580fe6060f1SDimitry Andric } 581fe6060f1SDimitry Andric 582bdd1243dSDimitry Andric if (SrcTy.isVector() && PartTy.isVector() && 583*0fca6ea1SDimitry Andric PartTy.getSizeInBits() == SrcTy.getSizeInBits() && 584*0fca6ea1SDimitry Andric ElementCount::isKnownLT(SrcTy.getElementCount(), 585*0fca6ea1SDimitry Andric PartTy.getElementCount())) { 586*0fca6ea1SDimitry Andric // A coercion like: v2f32 -> v4f32 or nxv2f32 -> nxv4f32 587bdd1243dSDimitry Andric Register DstReg = DstRegs.front(); 588bdd1243dSDimitry Andric B.buildPadVectorWithUndefElements(DstReg, SrcReg); 589bdd1243dSDimitry Andric return; 590bdd1243dSDimitry Andric } 591bdd1243dSDimitry Andric 592fe6060f1SDimitry Andric LLT GCDTy = getGCDType(SrcTy, PartTy); 593fe6060f1SDimitry Andric if (GCDTy == PartTy) { 594fe6060f1SDimitry Andric // If this already evenly divisible, we can create a simple unmerge. 595fe6060f1SDimitry Andric B.buildUnmerge(DstRegs, SrcReg); 596fe6060f1SDimitry Andric return; 597fe6060f1SDimitry Andric } 598fe6060f1SDimitry Andric 599*0fca6ea1SDimitry Andric if (SrcTy.isVector() && !PartTy.isVector() && 600*0fca6ea1SDimitry Andric SrcTy.getScalarSizeInBits() > PartTy.getSizeInBits()) { 601*0fca6ea1SDimitry Andric LLT ExtTy = 602*0fca6ea1SDimitry Andric LLT::vector(SrcTy.getElementCount(), 603*0fca6ea1SDimitry Andric LLT::scalar(PartTy.getScalarSizeInBits() * DstRegs.size() / 604*0fca6ea1SDimitry Andric SrcTy.getNumElements())); 605*0fca6ea1SDimitry Andric auto Ext = B.buildAnyExt(ExtTy, SrcReg); 606*0fca6ea1SDimitry Andric B.buildUnmerge(DstRegs, Ext); 607*0fca6ea1SDimitry Andric return; 608*0fca6ea1SDimitry Andric } 609*0fca6ea1SDimitry Andric 610fe6060f1SDimitry Andric MachineRegisterInfo &MRI = *B.getMRI(); 611fe6060f1SDimitry Andric LLT DstTy = MRI.getType(DstRegs[0]); 6120eae32dcSDimitry Andric LLT LCMTy = getCoverTy(SrcTy, PartTy); 613fe6060f1SDimitry Andric 614753f127fSDimitry Andric if (PartTy.isVector() && LCMTy == PartTy) { 615753f127fSDimitry Andric assert(DstRegs.size() == 1); 616753f127fSDimitry Andric B.buildPadVectorWithUndefElements(DstRegs[0], SrcReg); 617753f127fSDimitry Andric return; 618753f127fSDimitry Andric } 619753f127fSDimitry Andric 620fe6060f1SDimitry Andric const unsigned DstSize = DstTy.getSizeInBits(); 621fe6060f1SDimitry Andric const unsigned SrcSize = SrcTy.getSizeInBits(); 622fe6060f1SDimitry Andric unsigned CoveringSize = LCMTy.getSizeInBits(); 623fe6060f1SDimitry Andric 624fe6060f1SDimitry Andric Register UnmergeSrc = SrcReg; 625fe6060f1SDimitry Andric 6260eae32dcSDimitry Andric if (!LCMTy.isVector() && CoveringSize != SrcSize) { 627fe6060f1SDimitry Andric // For scalars, it's common to be able to use a simple extension. 628fe6060f1SDimitry Andric if (SrcTy.isScalar() && DstTy.isScalar()) { 629fe6060f1SDimitry Andric CoveringSize = alignTo(SrcSize, DstSize); 630fe6060f1SDimitry Andric LLT CoverTy = LLT::scalar(CoveringSize); 631fe6060f1SDimitry Andric UnmergeSrc = B.buildInstr(ExtendOp, {CoverTy}, {SrcReg}).getReg(0); 632fe6060f1SDimitry Andric } else { 633fe6060f1SDimitry Andric // Widen to the common type. 634fe6060f1SDimitry Andric // FIXME: This should respect the extend type 635fe6060f1SDimitry Andric Register Undef = B.buildUndef(SrcTy).getReg(0); 636fe6060f1SDimitry Andric SmallVector<Register, 8> MergeParts(1, SrcReg); 637fe6060f1SDimitry Andric for (unsigned Size = SrcSize; Size != CoveringSize; Size += SrcSize) 638fe6060f1SDimitry Andric MergeParts.push_back(Undef); 639bdd1243dSDimitry Andric UnmergeSrc = B.buildMergeLikeInstr(LCMTy, MergeParts).getReg(0); 640fe6060f1SDimitry Andric } 641fe6060f1SDimitry Andric } 642fe6060f1SDimitry Andric 6430eae32dcSDimitry Andric if (LCMTy.isVector() && CoveringSize != SrcSize) 6440eae32dcSDimitry Andric UnmergeSrc = B.buildPadVectorWithUndefElements(LCMTy, SrcReg).getReg(0); 645fe6060f1SDimitry Andric 6460eae32dcSDimitry Andric B.buildUnmerge(DstRegs, UnmergeSrc); 647fe6060f1SDimitry Andric } 648fe6060f1SDimitry Andric 649fe6060f1SDimitry Andric bool CallLowering::determineAndHandleAssignments( 650fe6060f1SDimitry Andric ValueHandler &Handler, ValueAssigner &Assigner, 651fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &Args, MachineIRBuilder &MIRBuilder, 65204eeddc0SDimitry Andric CallingConv::ID CallConv, bool IsVarArg, 65304eeddc0SDimitry Andric ArrayRef<Register> ThisReturnRegs) const { 6540b57cec5SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 6550b57cec5SDimitry Andric const Function &F = MF.getFunction(); 6560b57cec5SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 657fe6060f1SDimitry Andric 658fe6060f1SDimitry Andric CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, F.getContext()); 659fe6060f1SDimitry Andric if (!determineAssignments(Assigner, Args, CCInfo)) 660fe6060f1SDimitry Andric return false; 661fe6060f1SDimitry Andric 662fe6060f1SDimitry Andric return handleAssignments(Handler, Args, CCInfo, ArgLocs, MIRBuilder, 66304eeddc0SDimitry Andric ThisReturnRegs); 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric 666fe6060f1SDimitry Andric static unsigned extendOpFromFlags(llvm::ISD::ArgFlagsTy Flags) { 667fe6060f1SDimitry Andric if (Flags.isSExt()) 668fe6060f1SDimitry Andric return TargetOpcode::G_SEXT; 669fe6060f1SDimitry Andric if (Flags.isZExt()) 670fe6060f1SDimitry Andric return TargetOpcode::G_ZEXT; 671fe6060f1SDimitry Andric return TargetOpcode::G_ANYEXT; 672fe6060f1SDimitry Andric } 673fe6060f1SDimitry Andric 674fe6060f1SDimitry Andric bool CallLowering::determineAssignments(ValueAssigner &Assigner, 6758bcb0991SDimitry Andric SmallVectorImpl<ArgInfo> &Args, 676fe6060f1SDimitry Andric CCState &CCInfo) const { 677fe6060f1SDimitry Andric LLVMContext &Ctx = CCInfo.getContext(); 678fe6060f1SDimitry Andric const CallingConv::ID CallConv = CCInfo.getCallingConv(); 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric unsigned NumArgs = Args.size(); 6810b57cec5SDimitry Andric for (unsigned i = 0; i != NumArgs; ++i) { 6825ffd83dbSDimitry Andric EVT CurVT = EVT::getEVT(Args[i].Ty); 683e8d8bef9SDimitry Andric 684fe6060f1SDimitry Andric MVT NewVT = TLI->getRegisterTypeForCallingConv(Ctx, CallConv, CurVT); 6858bcb0991SDimitry Andric 6868bcb0991SDimitry Andric // If we need to split the type over multiple regs, check it's a scenario 6878bcb0991SDimitry Andric // we currently support. 688fe6060f1SDimitry Andric unsigned NumParts = 689fe6060f1SDimitry Andric TLI->getNumRegistersForCallingConv(Ctx, CallConv, CurVT); 690e8d8bef9SDimitry Andric 691e8d8bef9SDimitry Andric if (NumParts == 1) { 692e8d8bef9SDimitry Andric // Try to use the register type if we couldn't assign the VT. 693fe6060f1SDimitry Andric if (Assigner.assignArg(i, CurVT, NewVT, NewVT, CCValAssign::Full, Args[i], 694e8d8bef9SDimitry Andric Args[i].Flags[0], CCInfo)) 695e8d8bef9SDimitry Andric return false; 696e8d8bef9SDimitry Andric continue; 697e8d8bef9SDimitry Andric } 698e8d8bef9SDimitry Andric 6998bcb0991SDimitry Andric // For incoming arguments (physregs to vregs), we could have values in 7008bcb0991SDimitry Andric // physregs (or memlocs) which we want to extract and copy to vregs. 7018bcb0991SDimitry Andric // During this, we might have to deal with the LLT being split across 7028bcb0991SDimitry Andric // multiple regs, so we have to record this information for later. 7038bcb0991SDimitry Andric // 7048bcb0991SDimitry Andric // If we have outgoing args, then we have the opposite case. We have a 7058bcb0991SDimitry Andric // vreg with an LLT which we want to assign to a physical location, and 7068bcb0991SDimitry Andric // we might have to record that the value has to be split later. 707fe6060f1SDimitry Andric 7088bcb0991SDimitry Andric // We're handling an incoming arg which is split over multiple regs. 7098bcb0991SDimitry Andric // E.g. passing an s128 on AArch64. 7108bcb0991SDimitry Andric ISD::ArgFlagsTy OrigFlags = Args[i].Flags[0]; 7118bcb0991SDimitry Andric Args[i].Flags.clear(); 712fe6060f1SDimitry Andric 7138bcb0991SDimitry Andric for (unsigned Part = 0; Part < NumParts; ++Part) { 7148bcb0991SDimitry Andric ISD::ArgFlagsTy Flags = OrigFlags; 7158bcb0991SDimitry Andric if (Part == 0) { 7168bcb0991SDimitry Andric Flags.setSplit(); 7178bcb0991SDimitry Andric } else { 7185ffd83dbSDimitry Andric Flags.setOrigAlign(Align(1)); 7198bcb0991SDimitry Andric if (Part == NumParts - 1) 7208bcb0991SDimitry Andric Flags.setSplitEnd(); 7218bcb0991SDimitry Andric } 722fe6060f1SDimitry Andric 7238bcb0991SDimitry Andric Args[i].Flags.push_back(Flags); 724fe6060f1SDimitry Andric if (Assigner.assignArg(i, CurVT, NewVT, NewVT, CCValAssign::Full, Args[i], 725e8d8bef9SDimitry Andric Args[i].Flags[Part], CCInfo)) { 7268bcb0991SDimitry Andric // Still couldn't assign this smaller part type for some reason. 7278bcb0991SDimitry Andric return false; 7288bcb0991SDimitry Andric } 7298bcb0991SDimitry Andric } 7308bcb0991SDimitry Andric } 7310b57cec5SDimitry Andric 732fe6060f1SDimitry Andric return true; 733fe6060f1SDimitry Andric } 734fe6060f1SDimitry Andric 735fe6060f1SDimitry Andric bool CallLowering::handleAssignments(ValueHandler &Handler, 736fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &Args, 737fe6060f1SDimitry Andric CCState &CCInfo, 738fe6060f1SDimitry Andric SmallVectorImpl<CCValAssign> &ArgLocs, 739fe6060f1SDimitry Andric MachineIRBuilder &MIRBuilder, 74004eeddc0SDimitry Andric ArrayRef<Register> ThisReturnRegs) const { 741fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 742fe6060f1SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 743fe6060f1SDimitry Andric const Function &F = MF.getFunction(); 744*0fca6ea1SDimitry Andric const DataLayout &DL = F.getDataLayout(); 745fe6060f1SDimitry Andric 746fe6060f1SDimitry Andric const unsigned NumArgs = Args.size(); 747fe6060f1SDimitry Andric 748349cc55cSDimitry Andric // Stores thunks for outgoing register assignments. This is used so we delay 749349cc55cSDimitry Andric // generating register copies until mem loc assignments are done. We do this 750349cc55cSDimitry Andric // so that if the target is using the delayed stack protector feature, we can 751349cc55cSDimitry Andric // find the split point of the block accurately. E.g. if we have: 752349cc55cSDimitry Andric // G_STORE %val, %memloc 753349cc55cSDimitry Andric // $x0 = COPY %foo 754349cc55cSDimitry Andric // $x1 = COPY %bar 755349cc55cSDimitry Andric // CALL func 756349cc55cSDimitry Andric // ... then the split point for the block will correctly be at, and including, 757349cc55cSDimitry Andric // the copy to $x0. If instead the G_STORE instruction immediately precedes 758349cc55cSDimitry Andric // the CALL, then we'd prematurely choose the CALL as the split point, thus 759349cc55cSDimitry Andric // generating a split block with a CALL that uses undefined physregs. 760349cc55cSDimitry Andric SmallVector<std::function<void()>> DelayedOutgoingRegAssignments; 761349cc55cSDimitry Andric 762fe6060f1SDimitry Andric for (unsigned i = 0, j = 0; i != NumArgs; ++i, ++j) { 7630b57cec5SDimitry Andric assert(j < ArgLocs.size() && "Skipped too many arg locs"); 7640b57cec5SDimitry Andric CCValAssign &VA = ArgLocs[j]; 7650b57cec5SDimitry Andric assert(VA.getValNo() == i && "Location doesn't correspond to current arg"); 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric if (VA.needsCustom()) { 768349cc55cSDimitry Andric std::function<void()> Thunk; 769349cc55cSDimitry Andric unsigned NumArgRegs = Handler.assignCustomValue( 770bdd1243dSDimitry Andric Args[i], ArrayRef(ArgLocs).slice(j), &Thunk); 771349cc55cSDimitry Andric if (Thunk) 772349cc55cSDimitry Andric DelayedOutgoingRegAssignments.emplace_back(Thunk); 7735ffd83dbSDimitry Andric if (!NumArgRegs) 7745ffd83dbSDimitry Andric return false; 7757a6dacacSDimitry Andric j += (NumArgRegs - 1); 7760b57cec5SDimitry Andric continue; 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric 779*0fca6ea1SDimitry Andric auto AllocaAddressSpace = MF.getDataLayout().getAllocaAddrSpace(); 780*0fca6ea1SDimitry Andric 781fe6060f1SDimitry Andric const MVT ValVT = VA.getValVT(); 782fe6060f1SDimitry Andric const MVT LocVT = VA.getLocVT(); 7830b57cec5SDimitry Andric 784fe6060f1SDimitry Andric const LLT LocTy(LocVT); 785fe6060f1SDimitry Andric const LLT ValTy(ValVT); 786fe6060f1SDimitry Andric const LLT NewLLT = Handler.isIncomingArgumentHandler() ? LocTy : ValTy; 787fe6060f1SDimitry Andric const EVT OrigVT = EVT::getEVT(Args[i].Ty); 7885ffd83dbSDimitry Andric const LLT OrigTy = getLLTForType(*Args[i].Ty, DL); 789*0fca6ea1SDimitry Andric const LLT PointerTy = LLT::pointer( 790*0fca6ea1SDimitry Andric AllocaAddressSpace, DL.getPointerSizeInBits(AllocaAddressSpace)); 7915ffd83dbSDimitry Andric 7928bcb0991SDimitry Andric // Expected to be multiple regs for a single incoming arg. 793e8d8bef9SDimitry Andric // There should be Regs.size() ArgLocs per argument. 794fe6060f1SDimitry Andric // This should be the same as getNumRegistersForCallingConv 795fe6060f1SDimitry Andric const unsigned NumParts = Args[i].Flags.size(); 7968bcb0991SDimitry Andric 797fe6060f1SDimitry Andric // Now split the registers into the assigned types. 798fe6060f1SDimitry Andric Args[i].OrigRegs.assign(Args[i].Regs.begin(), Args[i].Regs.end()); 799fe6060f1SDimitry Andric 800fe6060f1SDimitry Andric if (NumParts != 1 || NewLLT != OrigTy) { 801fe6060f1SDimitry Andric // If we can't directly assign the register, we need one or more 802fe6060f1SDimitry Andric // intermediate values. 803fe6060f1SDimitry Andric Args[i].Regs.resize(NumParts); 804fe6060f1SDimitry Andric 805*0fca6ea1SDimitry Andric // When we have indirect parameter passing we are receiving a pointer, 806*0fca6ea1SDimitry Andric // that points to the actual value, so we need one "temporary" pointer. 807*0fca6ea1SDimitry Andric if (VA.getLocInfo() == CCValAssign::Indirect) { 808*0fca6ea1SDimitry Andric if (Handler.isIncomingArgumentHandler()) 809*0fca6ea1SDimitry Andric Args[i].Regs[0] = MRI.createGenericVirtualRegister(PointerTy); 810*0fca6ea1SDimitry Andric } else { 811fe6060f1SDimitry Andric // For each split register, create and assign a vreg that will store 812fe6060f1SDimitry Andric // the incoming component of the larger value. These will later be 813fe6060f1SDimitry Andric // merged to form the final vreg. 814fe6060f1SDimitry Andric for (unsigned Part = 0; Part < NumParts; ++Part) 815fe6060f1SDimitry Andric Args[i].Regs[Part] = MRI.createGenericVirtualRegister(NewLLT); 816fe6060f1SDimitry Andric } 817*0fca6ea1SDimitry Andric } 818fe6060f1SDimitry Andric 819fe6060f1SDimitry Andric assert((j + (NumParts - 1)) < ArgLocs.size() && 8208bcb0991SDimitry Andric "Too many regs for number of args"); 821fe6060f1SDimitry Andric 822fe6060f1SDimitry Andric // Coerce into outgoing value types before register assignment. 823*0fca6ea1SDimitry Andric if (!Handler.isIncomingArgumentHandler() && OrigTy != ValTy && 824*0fca6ea1SDimitry Andric VA.getLocInfo() != CCValAssign::Indirect) { 825fe6060f1SDimitry Andric assert(Args[i].OrigRegs.size() == 1); 826fe6060f1SDimitry Andric buildCopyToRegs(MIRBuilder, Args[i].Regs, Args[i].OrigRegs[0], OrigTy, 827fe6060f1SDimitry Andric ValTy, extendOpFromFlags(Args[i].Flags[0])); 828fe6060f1SDimitry Andric } 829fe6060f1SDimitry Andric 830*0fca6ea1SDimitry Andric bool IndirectParameterPassingHandled = false; 83181ad6265SDimitry Andric bool BigEndianPartOrdering = TLI->hasBigEndianPartOrdering(OrigVT, DL); 832fe6060f1SDimitry Andric for (unsigned Part = 0; Part < NumParts; ++Part) { 833*0fca6ea1SDimitry Andric assert((VA.getLocInfo() != CCValAssign::Indirect || Part == 0) && 834*0fca6ea1SDimitry Andric "Only the first parameter should be processed when " 835*0fca6ea1SDimitry Andric "handling indirect passing!"); 836fe6060f1SDimitry Andric Register ArgReg = Args[i].Regs[Part]; 8378bcb0991SDimitry Andric // There should be Regs.size() ArgLocs per argument. 83881ad6265SDimitry Andric unsigned Idx = BigEndianPartOrdering ? NumParts - 1 - Part : Part; 83981ad6265SDimitry Andric CCValAssign &VA = ArgLocs[j + Idx]; 840fe6060f1SDimitry Andric const ISD::ArgFlagsTy Flags = Args[i].Flags[Part]; 841e8d8bef9SDimitry Andric 842*0fca6ea1SDimitry Andric // We found an indirect parameter passing, and we have an 843*0fca6ea1SDimitry Andric // OutgoingValueHandler as our handler (so we are at the call site or the 844*0fca6ea1SDimitry Andric // return value). In this case, start the construction of the following 845*0fca6ea1SDimitry Andric // GMIR, that is responsible for the preparation of indirect parameter 846*0fca6ea1SDimitry Andric // passing: 847*0fca6ea1SDimitry Andric // 848*0fca6ea1SDimitry Andric // %1(indirectly passed type) = The value to pass 849*0fca6ea1SDimitry Andric // %3(pointer) = G_FRAME_INDEX %stack.0 850*0fca6ea1SDimitry Andric // G_STORE %1, %3 :: (store (s128), align 8) 851*0fca6ea1SDimitry Andric // 852*0fca6ea1SDimitry Andric // After this GMIR, the remaining part of the loop body will decide how 853*0fca6ea1SDimitry Andric // to get the value to the caller and we break out of the loop. 854*0fca6ea1SDimitry Andric if (VA.getLocInfo() == CCValAssign::Indirect && 855*0fca6ea1SDimitry Andric !Handler.isIncomingArgumentHandler()) { 856*0fca6ea1SDimitry Andric Align AlignmentForStored = DL.getPrefTypeAlign(Args[i].Ty); 857*0fca6ea1SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 858*0fca6ea1SDimitry Andric // Get some space on the stack for the value, so later we can pass it 859*0fca6ea1SDimitry Andric // as a reference. 860*0fca6ea1SDimitry Andric int FrameIdx = MFI.CreateStackObject(OrigTy.getScalarSizeInBits(), 861*0fca6ea1SDimitry Andric AlignmentForStored, false); 862*0fca6ea1SDimitry Andric Register PointerToStackReg = 863*0fca6ea1SDimitry Andric MIRBuilder.buildFrameIndex(PointerTy, FrameIdx).getReg(0); 864*0fca6ea1SDimitry Andric MachinePointerInfo StackPointerMPO = 865*0fca6ea1SDimitry Andric MachinePointerInfo::getFixedStack(MF, FrameIdx); 866*0fca6ea1SDimitry Andric // Store the value in the previously created stack space. 867*0fca6ea1SDimitry Andric MIRBuilder.buildStore(Args[i].OrigRegs[Part], PointerToStackReg, 868*0fca6ea1SDimitry Andric StackPointerMPO, 869*0fca6ea1SDimitry Andric inferAlignFromPtrInfo(MF, StackPointerMPO)); 870*0fca6ea1SDimitry Andric 871*0fca6ea1SDimitry Andric ArgReg = PointerToStackReg; 872*0fca6ea1SDimitry Andric IndirectParameterPassingHandled = true; 873*0fca6ea1SDimitry Andric } 874*0fca6ea1SDimitry Andric 875fe6060f1SDimitry Andric if (VA.isMemLoc() && !Flags.isByVal()) { 876fe6060f1SDimitry Andric // Individual pieces may have been spilled to the stack and others 877fe6060f1SDimitry Andric // passed in registers. 878fe6060f1SDimitry Andric 879fe6060f1SDimitry Andric // TODO: The memory size may be larger than the value we need to 880fe6060f1SDimitry Andric // store. We may need to adjust the offset for big endian targets. 881fe6060f1SDimitry Andric LLT MemTy = Handler.getStackValueStoreType(DL, VA, Flags); 882fe6060f1SDimitry Andric 883e8d8bef9SDimitry Andric MachinePointerInfo MPO; 884*0fca6ea1SDimitry Andric Register StackAddr = 885*0fca6ea1SDimitry Andric Handler.getStackAddress(VA.getLocInfo() == CCValAssign::Indirect 886*0fca6ea1SDimitry Andric ? PointerTy.getSizeInBytes() 887*0fca6ea1SDimitry Andric : MemTy.getSizeInBytes(), 888*0fca6ea1SDimitry Andric VA.getLocMemOffset(), MPO, Flags); 889fe6060f1SDimitry Andric 890*0fca6ea1SDimitry Andric // Finish the handling of indirect passing from the passers 891*0fca6ea1SDimitry Andric // (OutgoingParameterHandler) side. 892*0fca6ea1SDimitry Andric // This branch is needed, so the pointer to the value is loaded onto the 893*0fca6ea1SDimitry Andric // stack. 894*0fca6ea1SDimitry Andric if (VA.getLocInfo() == CCValAssign::Indirect) 895*0fca6ea1SDimitry Andric Handler.assignValueToAddress(ArgReg, StackAddr, PointerTy, MPO, VA); 896*0fca6ea1SDimitry Andric else 897*0fca6ea1SDimitry Andric Handler.assignValueToAddress(Args[i], Part, StackAddr, MemTy, MPO, 898*0fca6ea1SDimitry Andric VA); 899*0fca6ea1SDimitry Andric } else if (VA.isMemLoc() && Flags.isByVal()) { 900*0fca6ea1SDimitry Andric assert(Args[i].Regs.size() == 1 && "didn't expect split byval pointer"); 901e8d8bef9SDimitry Andric 902e8d8bef9SDimitry Andric if (Handler.isIncomingArgumentHandler()) { 903fe6060f1SDimitry Andric // We just need to copy the frame index value to the pointer. 904fe6060f1SDimitry Andric MachinePointerInfo MPO; 905fe6060f1SDimitry Andric Register StackAddr = Handler.getStackAddress( 906fe6060f1SDimitry Andric Flags.getByValSize(), VA.getLocMemOffset(), MPO, Flags); 907fe6060f1SDimitry Andric MIRBuilder.buildCopy(Args[i].Regs[0], StackAddr); 908fe6060f1SDimitry Andric } else { 909fe6060f1SDimitry Andric // For outgoing byval arguments, insert the implicit copy byval 910fe6060f1SDimitry Andric // implies, such that writes in the callee do not modify the caller's 911fe6060f1SDimitry Andric // value. 912fe6060f1SDimitry Andric uint64_t MemSize = Flags.getByValSize(); 913fe6060f1SDimitry Andric int64_t Offset = VA.getLocMemOffset(); 9148833aad7SDimitry Andric 915fe6060f1SDimitry Andric MachinePointerInfo DstMPO; 916fe6060f1SDimitry Andric Register StackAddr = 917fe6060f1SDimitry Andric Handler.getStackAddress(MemSize, Offset, DstMPO, Flags); 918fe6060f1SDimitry Andric 919fe6060f1SDimitry Andric MachinePointerInfo SrcMPO(Args[i].OrigValue); 920fe6060f1SDimitry Andric if (!Args[i].OrigValue) { 921fe6060f1SDimitry Andric // We still need to accurately track the stack address space if we 922fe6060f1SDimitry Andric // don't know the underlying value. 923fe6060f1SDimitry Andric const LLT PtrTy = MRI.getType(StackAddr); 924fe6060f1SDimitry Andric SrcMPO = MachinePointerInfo(PtrTy.getAddressSpace()); 9250b57cec5SDimitry Andric } 926e8d8bef9SDimitry Andric 927fe6060f1SDimitry Andric Align DstAlign = std::max(Flags.getNonZeroByValAlign(), 928fe6060f1SDimitry Andric inferAlignFromPtrInfo(MF, DstMPO)); 929fe6060f1SDimitry Andric 930fe6060f1SDimitry Andric Align SrcAlign = std::max(Flags.getNonZeroByValAlign(), 931fe6060f1SDimitry Andric inferAlignFromPtrInfo(MF, SrcMPO)); 932fe6060f1SDimitry Andric 933fe6060f1SDimitry Andric Handler.copyArgumentMemory(Args[i], StackAddr, Args[i].Regs[0], 934fe6060f1SDimitry Andric DstMPO, DstAlign, SrcMPO, SrcAlign, 935fe6060f1SDimitry Andric MemSize, VA); 936fe6060f1SDimitry Andric } 937*0fca6ea1SDimitry Andric } else if (i == 0 && !ThisReturnRegs.empty() && 938fe6060f1SDimitry Andric Handler.isIncomingArgumentHandler() && 939fe6060f1SDimitry Andric isTypeIsValidForThisReturn(ValVT)) { 94004eeddc0SDimitry Andric Handler.assignValueToReg(ArgReg, ThisReturnRegs[Part], VA); 941*0fca6ea1SDimitry Andric } else if (Handler.isIncomingArgumentHandler()) { 942fe6060f1SDimitry Andric Handler.assignValueToReg(ArgReg, VA.getLocReg(), VA); 943*0fca6ea1SDimitry Andric } else { 944349cc55cSDimitry Andric DelayedOutgoingRegAssignments.emplace_back([=, &Handler]() { 945349cc55cSDimitry Andric Handler.assignValueToReg(ArgReg, VA.getLocReg(), VA); 946349cc55cSDimitry Andric }); 947349cc55cSDimitry Andric } 948*0fca6ea1SDimitry Andric 949*0fca6ea1SDimitry Andric // Finish the handling of indirect parameter passing when receiving 950*0fca6ea1SDimitry Andric // the value (we are in the called function or the caller when receiving 951*0fca6ea1SDimitry Andric // the return value). 952*0fca6ea1SDimitry Andric if (VA.getLocInfo() == CCValAssign::Indirect && 953*0fca6ea1SDimitry Andric Handler.isIncomingArgumentHandler()) { 954*0fca6ea1SDimitry Andric Align Alignment = DL.getABITypeAlign(Args[i].Ty); 955*0fca6ea1SDimitry Andric MachinePointerInfo MPO = MachinePointerInfo::getUnknownStack(MF); 956*0fca6ea1SDimitry Andric 957*0fca6ea1SDimitry Andric // Since we are doing indirect parameter passing, we know that the value 958*0fca6ea1SDimitry Andric // in the temporary register is not the value passed to the function, 959*0fca6ea1SDimitry Andric // but rather a pointer to that value. Let's load that value into the 960*0fca6ea1SDimitry Andric // virtual register where the parameter should go. 961*0fca6ea1SDimitry Andric MIRBuilder.buildLoad(Args[i].OrigRegs[0], Args[i].Regs[0], MPO, 962*0fca6ea1SDimitry Andric Alignment); 963*0fca6ea1SDimitry Andric 964*0fca6ea1SDimitry Andric IndirectParameterPassingHandled = true; 965*0fca6ea1SDimitry Andric } 966*0fca6ea1SDimitry Andric 967*0fca6ea1SDimitry Andric if (IndirectParameterPassingHandled) 968*0fca6ea1SDimitry Andric break; 969fe6060f1SDimitry Andric } 970fe6060f1SDimitry Andric 971fe6060f1SDimitry Andric // Now that all pieces have been assigned, re-pack the register typed values 972*0fca6ea1SDimitry Andric // into the original value typed registers. This is only necessary, when 973*0fca6ea1SDimitry Andric // the value was passed in multiple registers, not indirectly. 974*0fca6ea1SDimitry Andric if (Handler.isIncomingArgumentHandler() && OrigVT != LocVT && 975*0fca6ea1SDimitry Andric !IndirectParameterPassingHandled) { 976fe6060f1SDimitry Andric // Merge the split registers into the expected larger result vregs of 977fe6060f1SDimitry Andric // the original call. 978fe6060f1SDimitry Andric buildCopyFromRegs(MIRBuilder, Args[i].OrigRegs, Args[i].Regs, OrigTy, 979fe6060f1SDimitry Andric LocTy, Args[i].Flags[0]); 980fe6060f1SDimitry Andric } 981fe6060f1SDimitry Andric 982fe6060f1SDimitry Andric j += NumParts - 1; 983e8d8bef9SDimitry Andric } 984349cc55cSDimitry Andric for (auto &Fn : DelayedOutgoingRegAssignments) 985349cc55cSDimitry Andric Fn(); 986e8d8bef9SDimitry Andric 987e8d8bef9SDimitry Andric return true; 988e8d8bef9SDimitry Andric } 989e8d8bef9SDimitry Andric 990e8d8bef9SDimitry Andric void CallLowering::insertSRetLoads(MachineIRBuilder &MIRBuilder, Type *RetTy, 991e8d8bef9SDimitry Andric ArrayRef<Register> VRegs, Register DemoteReg, 992e8d8bef9SDimitry Andric int FI) const { 993e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 994e8d8bef9SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 995e8d8bef9SDimitry Andric const DataLayout &DL = MF.getDataLayout(); 996e8d8bef9SDimitry Andric 997e8d8bef9SDimitry Andric SmallVector<EVT, 4> SplitVTs; 998e8d8bef9SDimitry Andric SmallVector<uint64_t, 4> Offsets; 999e8d8bef9SDimitry Andric ComputeValueVTs(*TLI, DL, RetTy, SplitVTs, &Offsets, 0); 1000e8d8bef9SDimitry Andric 1001e8d8bef9SDimitry Andric assert(VRegs.size() == SplitVTs.size()); 1002e8d8bef9SDimitry Andric 1003e8d8bef9SDimitry Andric unsigned NumValues = SplitVTs.size(); 1004e8d8bef9SDimitry Andric Align BaseAlign = DL.getPrefTypeAlign(RetTy); 10055f757f3fSDimitry Andric Type *RetPtrTy = 10065f757f3fSDimitry Andric PointerType::get(RetTy->getContext(), DL.getAllocaAddrSpace()); 100706c3fb27SDimitry Andric LLT OffsetLLTy = getLLTForType(*DL.getIndexType(RetPtrTy), DL); 1008e8d8bef9SDimitry Andric 1009e8d8bef9SDimitry Andric MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI); 1010e8d8bef9SDimitry Andric 1011e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumValues; ++I) { 1012e8d8bef9SDimitry Andric Register Addr; 1013e8d8bef9SDimitry Andric MIRBuilder.materializePtrAdd(Addr, DemoteReg, OffsetLLTy, Offsets[I]); 1014e8d8bef9SDimitry Andric auto *MMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad, 1015fe6060f1SDimitry Andric MRI.getType(VRegs[I]), 1016e8d8bef9SDimitry Andric commonAlignment(BaseAlign, Offsets[I])); 1017e8d8bef9SDimitry Andric MIRBuilder.buildLoad(VRegs[I], Addr, *MMO); 1018e8d8bef9SDimitry Andric } 1019e8d8bef9SDimitry Andric } 1020e8d8bef9SDimitry Andric 1021e8d8bef9SDimitry Andric void CallLowering::insertSRetStores(MachineIRBuilder &MIRBuilder, Type *RetTy, 1022e8d8bef9SDimitry Andric ArrayRef<Register> VRegs, 1023e8d8bef9SDimitry Andric Register DemoteReg) const { 1024e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 1025e8d8bef9SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1026e8d8bef9SDimitry Andric const DataLayout &DL = MF.getDataLayout(); 1027e8d8bef9SDimitry Andric 1028e8d8bef9SDimitry Andric SmallVector<EVT, 4> SplitVTs; 1029e8d8bef9SDimitry Andric SmallVector<uint64_t, 4> Offsets; 1030e8d8bef9SDimitry Andric ComputeValueVTs(*TLI, DL, RetTy, SplitVTs, &Offsets, 0); 1031e8d8bef9SDimitry Andric 1032e8d8bef9SDimitry Andric assert(VRegs.size() == SplitVTs.size()); 1033e8d8bef9SDimitry Andric 1034e8d8bef9SDimitry Andric unsigned NumValues = SplitVTs.size(); 1035e8d8bef9SDimitry Andric Align BaseAlign = DL.getPrefTypeAlign(RetTy); 1036e8d8bef9SDimitry Andric unsigned AS = DL.getAllocaAddrSpace(); 103706c3fb27SDimitry Andric LLT OffsetLLTy = getLLTForType(*DL.getIndexType(RetTy->getPointerTo(AS)), DL); 1038e8d8bef9SDimitry Andric 1039e8d8bef9SDimitry Andric MachinePointerInfo PtrInfo(AS); 1040e8d8bef9SDimitry Andric 1041e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumValues; ++I) { 1042e8d8bef9SDimitry Andric Register Addr; 1043e8d8bef9SDimitry Andric MIRBuilder.materializePtrAdd(Addr, DemoteReg, OffsetLLTy, Offsets[I]); 1044e8d8bef9SDimitry Andric auto *MMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore, 1045fe6060f1SDimitry Andric MRI.getType(VRegs[I]), 1046e8d8bef9SDimitry Andric commonAlignment(BaseAlign, Offsets[I])); 1047e8d8bef9SDimitry Andric MIRBuilder.buildStore(VRegs[I], Addr, *MMO); 1048e8d8bef9SDimitry Andric } 1049e8d8bef9SDimitry Andric } 1050e8d8bef9SDimitry Andric 1051e8d8bef9SDimitry Andric void CallLowering::insertSRetIncomingArgument( 1052e8d8bef9SDimitry Andric const Function &F, SmallVectorImpl<ArgInfo> &SplitArgs, Register &DemoteReg, 1053e8d8bef9SDimitry Andric MachineRegisterInfo &MRI, const DataLayout &DL) const { 1054e8d8bef9SDimitry Andric unsigned AS = DL.getAllocaAddrSpace(); 1055e8d8bef9SDimitry Andric DemoteReg = MRI.createGenericVirtualRegister( 1056e8d8bef9SDimitry Andric LLT::pointer(AS, DL.getPointerSizeInBits(AS))); 1057e8d8bef9SDimitry Andric 1058e8d8bef9SDimitry Andric Type *PtrTy = PointerType::get(F.getReturnType(), AS); 1059e8d8bef9SDimitry Andric 1060e8d8bef9SDimitry Andric SmallVector<EVT, 1> ValueVTs; 1061e8d8bef9SDimitry Andric ComputeValueVTs(*TLI, DL, PtrTy, ValueVTs); 1062e8d8bef9SDimitry Andric 1063e8d8bef9SDimitry Andric // NOTE: Assume that a pointer won't get split into more than one VT. 1064e8d8bef9SDimitry Andric assert(ValueVTs.size() == 1); 1065e8d8bef9SDimitry Andric 1066fe6060f1SDimitry Andric ArgInfo DemoteArg(DemoteReg, ValueVTs[0].getTypeForEVT(PtrTy->getContext()), 1067fe6060f1SDimitry Andric ArgInfo::NoArgIndex); 1068e8d8bef9SDimitry Andric setArgFlags(DemoteArg, AttributeList::ReturnIndex, DL, F); 1069e8d8bef9SDimitry Andric DemoteArg.Flags[0].setSRet(); 1070e8d8bef9SDimitry Andric SplitArgs.insert(SplitArgs.begin(), DemoteArg); 1071e8d8bef9SDimitry Andric } 1072e8d8bef9SDimitry Andric 1073e8d8bef9SDimitry Andric void CallLowering::insertSRetOutgoingArgument(MachineIRBuilder &MIRBuilder, 1074e8d8bef9SDimitry Andric const CallBase &CB, 1075e8d8bef9SDimitry Andric CallLoweringInfo &Info) const { 1076e8d8bef9SDimitry Andric const DataLayout &DL = MIRBuilder.getDataLayout(); 1077e8d8bef9SDimitry Andric Type *RetTy = CB.getType(); 1078e8d8bef9SDimitry Andric unsigned AS = DL.getAllocaAddrSpace(); 1079e8d8bef9SDimitry Andric LLT FramePtrTy = LLT::pointer(AS, DL.getPointerSizeInBits(AS)); 1080e8d8bef9SDimitry Andric 1081e8d8bef9SDimitry Andric int FI = MIRBuilder.getMF().getFrameInfo().CreateStackObject( 1082e8d8bef9SDimitry Andric DL.getTypeAllocSize(RetTy), DL.getPrefTypeAlign(RetTy), false); 1083e8d8bef9SDimitry Andric 1084e8d8bef9SDimitry Andric Register DemoteReg = MIRBuilder.buildFrameIndex(FramePtrTy, FI).getReg(0); 1085fe6060f1SDimitry Andric ArgInfo DemoteArg(DemoteReg, PointerType::get(RetTy, AS), 1086fe6060f1SDimitry Andric ArgInfo::NoArgIndex); 1087e8d8bef9SDimitry Andric setArgFlags(DemoteArg, AttributeList::ReturnIndex, DL, CB); 1088e8d8bef9SDimitry Andric DemoteArg.Flags[0].setSRet(); 1089e8d8bef9SDimitry Andric 1090e8d8bef9SDimitry Andric Info.OrigArgs.insert(Info.OrigArgs.begin(), DemoteArg); 1091e8d8bef9SDimitry Andric Info.DemoteStackIndex = FI; 1092e8d8bef9SDimitry Andric Info.DemoteRegister = DemoteReg; 1093e8d8bef9SDimitry Andric } 1094e8d8bef9SDimitry Andric 1095e8d8bef9SDimitry Andric bool CallLowering::checkReturn(CCState &CCInfo, 1096e8d8bef9SDimitry Andric SmallVectorImpl<BaseArgInfo> &Outs, 1097e8d8bef9SDimitry Andric CCAssignFn *Fn) const { 1098e8d8bef9SDimitry Andric for (unsigned I = 0, E = Outs.size(); I < E; ++I) { 1099e8d8bef9SDimitry Andric MVT VT = MVT::getVT(Outs[I].Ty); 1100e8d8bef9SDimitry Andric if (Fn(I, VT, VT, CCValAssign::Full, Outs[I].Flags[0], CCInfo)) 1101e8d8bef9SDimitry Andric return false; 1102e8d8bef9SDimitry Andric } 11030b57cec5SDimitry Andric return true; 11040b57cec5SDimitry Andric } 11050b57cec5SDimitry Andric 1106e8d8bef9SDimitry Andric void CallLowering::getReturnInfo(CallingConv::ID CallConv, Type *RetTy, 1107e8d8bef9SDimitry Andric AttributeList Attrs, 1108e8d8bef9SDimitry Andric SmallVectorImpl<BaseArgInfo> &Outs, 1109e8d8bef9SDimitry Andric const DataLayout &DL) const { 1110e8d8bef9SDimitry Andric LLVMContext &Context = RetTy->getContext(); 1111e8d8bef9SDimitry Andric ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); 1112e8d8bef9SDimitry Andric 1113e8d8bef9SDimitry Andric SmallVector<EVT, 4> SplitVTs; 1114e8d8bef9SDimitry Andric ComputeValueVTs(*TLI, DL, RetTy, SplitVTs); 1115e8d8bef9SDimitry Andric addArgFlagsFromAttributes(Flags, Attrs, AttributeList::ReturnIndex); 1116e8d8bef9SDimitry Andric 1117e8d8bef9SDimitry Andric for (EVT VT : SplitVTs) { 1118e8d8bef9SDimitry Andric unsigned NumParts = 1119e8d8bef9SDimitry Andric TLI->getNumRegistersForCallingConv(Context, CallConv, VT); 1120e8d8bef9SDimitry Andric MVT RegVT = TLI->getRegisterTypeForCallingConv(Context, CallConv, VT); 1121e8d8bef9SDimitry Andric Type *PartTy = EVT(RegVT).getTypeForEVT(Context); 1122e8d8bef9SDimitry Andric 1123e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumParts; ++I) { 1124e8d8bef9SDimitry Andric Outs.emplace_back(PartTy, Flags); 1125e8d8bef9SDimitry Andric } 1126e8d8bef9SDimitry Andric } 1127e8d8bef9SDimitry Andric } 1128e8d8bef9SDimitry Andric 1129e8d8bef9SDimitry Andric bool CallLowering::checkReturnTypeForCallConv(MachineFunction &MF) const { 1130e8d8bef9SDimitry Andric const auto &F = MF.getFunction(); 1131e8d8bef9SDimitry Andric Type *ReturnType = F.getReturnType(); 1132e8d8bef9SDimitry Andric CallingConv::ID CallConv = F.getCallingConv(); 1133e8d8bef9SDimitry Andric 1134e8d8bef9SDimitry Andric SmallVector<BaseArgInfo, 4> SplitArgs; 1135e8d8bef9SDimitry Andric getReturnInfo(CallConv, ReturnType, F.getAttributes(), SplitArgs, 1136e8d8bef9SDimitry Andric MF.getDataLayout()); 1137e8d8bef9SDimitry Andric return canLowerReturn(MF, CallConv, SplitArgs, F.isVarArg()); 1138e8d8bef9SDimitry Andric } 1139e8d8bef9SDimitry Andric 1140e8d8bef9SDimitry Andric bool CallLowering::parametersInCSRMatch( 1141e8d8bef9SDimitry Andric const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, 1142e8d8bef9SDimitry Andric const SmallVectorImpl<CCValAssign> &OutLocs, 1143e8d8bef9SDimitry Andric const SmallVectorImpl<ArgInfo> &OutArgs) const { 1144e8d8bef9SDimitry Andric for (unsigned i = 0; i < OutLocs.size(); ++i) { 1145fcaf7f86SDimitry Andric const auto &ArgLoc = OutLocs[i]; 1146e8d8bef9SDimitry Andric // If it's not a register, it's fine. 1147e8d8bef9SDimitry Andric if (!ArgLoc.isRegLoc()) 1148e8d8bef9SDimitry Andric continue; 1149e8d8bef9SDimitry Andric 1150e8d8bef9SDimitry Andric MCRegister PhysReg = ArgLoc.getLocReg(); 1151e8d8bef9SDimitry Andric 1152e8d8bef9SDimitry Andric // Only look at callee-saved registers. 1153e8d8bef9SDimitry Andric if (MachineOperand::clobbersPhysReg(CallerPreservedMask, PhysReg)) 1154e8d8bef9SDimitry Andric continue; 1155e8d8bef9SDimitry Andric 1156e8d8bef9SDimitry Andric LLVM_DEBUG( 1157e8d8bef9SDimitry Andric dbgs() 1158e8d8bef9SDimitry Andric << "... Call has an argument passed in a callee-saved register.\n"); 1159e8d8bef9SDimitry Andric 1160e8d8bef9SDimitry Andric // Check if it was copied from. 1161e8d8bef9SDimitry Andric const ArgInfo &OutInfo = OutArgs[i]; 1162e8d8bef9SDimitry Andric 1163e8d8bef9SDimitry Andric if (OutInfo.Regs.size() > 1) { 1164e8d8bef9SDimitry Andric LLVM_DEBUG( 1165e8d8bef9SDimitry Andric dbgs() << "... Cannot handle arguments in multiple registers.\n"); 1166e8d8bef9SDimitry Andric return false; 1167e8d8bef9SDimitry Andric } 1168e8d8bef9SDimitry Andric 1169e8d8bef9SDimitry Andric // Check if we copy the register, walking through copies from virtual 1170e8d8bef9SDimitry Andric // registers. Note that getDefIgnoringCopies does not ignore copies from 1171e8d8bef9SDimitry Andric // physical registers. 1172e8d8bef9SDimitry Andric MachineInstr *RegDef = getDefIgnoringCopies(OutInfo.Regs[0], MRI); 1173e8d8bef9SDimitry Andric if (!RegDef || RegDef->getOpcode() != TargetOpcode::COPY) { 1174e8d8bef9SDimitry Andric LLVM_DEBUG( 1175e8d8bef9SDimitry Andric dbgs() 1176e8d8bef9SDimitry Andric << "... Parameter was not copied into a VReg, cannot tail call.\n"); 1177e8d8bef9SDimitry Andric return false; 1178e8d8bef9SDimitry Andric } 1179e8d8bef9SDimitry Andric 1180e8d8bef9SDimitry Andric // Got a copy. Verify that it's the same as the register we want. 1181e8d8bef9SDimitry Andric Register CopyRHS = RegDef->getOperand(1).getReg(); 1182e8d8bef9SDimitry Andric if (CopyRHS != PhysReg) { 1183e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "... Callee-saved register was not copied into " 1184e8d8bef9SDimitry Andric "VReg, cannot tail call.\n"); 1185e8d8bef9SDimitry Andric return false; 1186e8d8bef9SDimitry Andric } 1187e8d8bef9SDimitry Andric } 1188e8d8bef9SDimitry Andric 1189e8d8bef9SDimitry Andric return true; 1190e8d8bef9SDimitry Andric } 1191e8d8bef9SDimitry Andric 11928bcb0991SDimitry Andric bool CallLowering::resultsCompatible(CallLoweringInfo &Info, 11938bcb0991SDimitry Andric MachineFunction &MF, 11948bcb0991SDimitry Andric SmallVectorImpl<ArgInfo> &InArgs, 1195fe6060f1SDimitry Andric ValueAssigner &CalleeAssigner, 1196fe6060f1SDimitry Andric ValueAssigner &CallerAssigner) const { 11978bcb0991SDimitry Andric const Function &F = MF.getFunction(); 11988bcb0991SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv; 11998bcb0991SDimitry Andric CallingConv::ID CallerCC = F.getCallingConv(); 12008bcb0991SDimitry Andric 12018bcb0991SDimitry Andric if (CallerCC == CalleeCC) 12028bcb0991SDimitry Andric return true; 12038bcb0991SDimitry Andric 12048bcb0991SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs1; 1205fe6060f1SDimitry Andric CCState CCInfo1(CalleeCC, Info.IsVarArg, MF, ArgLocs1, F.getContext()); 1206fe6060f1SDimitry Andric if (!determineAssignments(CalleeAssigner, InArgs, CCInfo1)) 12078bcb0991SDimitry Andric return false; 12088bcb0991SDimitry Andric 12098bcb0991SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs2; 1210fe6060f1SDimitry Andric CCState CCInfo2(CallerCC, F.isVarArg(), MF, ArgLocs2, F.getContext()); 1211fe6060f1SDimitry Andric if (!determineAssignments(CallerAssigner, InArgs, CCInfo2)) 12128bcb0991SDimitry Andric return false; 12138bcb0991SDimitry Andric 12148bcb0991SDimitry Andric // We need the argument locations to match up exactly. If there's more in 12158bcb0991SDimitry Andric // one than the other, then we are done. 12168bcb0991SDimitry Andric if (ArgLocs1.size() != ArgLocs2.size()) 12178bcb0991SDimitry Andric return false; 12188bcb0991SDimitry Andric 12198bcb0991SDimitry Andric // Make sure that each location is passed in exactly the same way. 12208bcb0991SDimitry Andric for (unsigned i = 0, e = ArgLocs1.size(); i < e; ++i) { 12218bcb0991SDimitry Andric const CCValAssign &Loc1 = ArgLocs1[i]; 12228bcb0991SDimitry Andric const CCValAssign &Loc2 = ArgLocs2[i]; 12238bcb0991SDimitry Andric 12248bcb0991SDimitry Andric // We need both of them to be the same. So if one is a register and one 12258bcb0991SDimitry Andric // isn't, we're done. 12268bcb0991SDimitry Andric if (Loc1.isRegLoc() != Loc2.isRegLoc()) 12278bcb0991SDimitry Andric return false; 12288bcb0991SDimitry Andric 12298bcb0991SDimitry Andric if (Loc1.isRegLoc()) { 12308bcb0991SDimitry Andric // If they don't have the same register location, we're done. 12318bcb0991SDimitry Andric if (Loc1.getLocReg() != Loc2.getLocReg()) 12328bcb0991SDimitry Andric return false; 12338bcb0991SDimitry Andric 12348bcb0991SDimitry Andric // They matched, so we can move to the next ArgLoc. 12358bcb0991SDimitry Andric continue; 12368bcb0991SDimitry Andric } 12378bcb0991SDimitry Andric 12388bcb0991SDimitry Andric // Loc1 wasn't a RegLoc, so they both must be MemLocs. Check if they match. 12398bcb0991SDimitry Andric if (Loc1.getLocMemOffset() != Loc2.getLocMemOffset()) 12408bcb0991SDimitry Andric return false; 12418bcb0991SDimitry Andric } 12428bcb0991SDimitry Andric 12438bcb0991SDimitry Andric return true; 12448bcb0991SDimitry Andric } 12458bcb0991SDimitry Andric 1246fe6060f1SDimitry Andric LLT CallLowering::ValueHandler::getStackValueStoreType( 1247fe6060f1SDimitry Andric const DataLayout &DL, const CCValAssign &VA, ISD::ArgFlagsTy Flags) const { 1248fe6060f1SDimitry Andric const MVT ValVT = VA.getValVT(); 1249fe6060f1SDimitry Andric if (ValVT != MVT::iPTR) { 1250fe6060f1SDimitry Andric LLT ValTy(ValVT); 1251fe6060f1SDimitry Andric 1252fe6060f1SDimitry Andric // We lost the pointeriness going through CCValAssign, so try to restore it 1253fe6060f1SDimitry Andric // based on the flags. 1254fe6060f1SDimitry Andric if (Flags.isPointer()) { 1255fe6060f1SDimitry Andric LLT PtrTy = LLT::pointer(Flags.getPointerAddrSpace(), 1256fe6060f1SDimitry Andric ValTy.getScalarSizeInBits()); 1257fe6060f1SDimitry Andric if (ValVT.isVector()) 1258fe6060f1SDimitry Andric return LLT::vector(ValTy.getElementCount(), PtrTy); 1259fe6060f1SDimitry Andric return PtrTy; 1260fe6060f1SDimitry Andric } 1261fe6060f1SDimitry Andric 1262fe6060f1SDimitry Andric return ValTy; 1263fe6060f1SDimitry Andric } 1264fe6060f1SDimitry Andric 1265fe6060f1SDimitry Andric unsigned AddrSpace = Flags.getPointerAddrSpace(); 1266fe6060f1SDimitry Andric return LLT::pointer(AddrSpace, DL.getPointerSize(AddrSpace)); 1267fe6060f1SDimitry Andric } 1268fe6060f1SDimitry Andric 1269fe6060f1SDimitry Andric void CallLowering::ValueHandler::copyArgumentMemory( 1270fe6060f1SDimitry Andric const ArgInfo &Arg, Register DstPtr, Register SrcPtr, 1271fe6060f1SDimitry Andric const MachinePointerInfo &DstPtrInfo, Align DstAlign, 1272fe6060f1SDimitry Andric const MachinePointerInfo &SrcPtrInfo, Align SrcAlign, uint64_t MemSize, 1273fe6060f1SDimitry Andric CCValAssign &VA) const { 1274fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 1275fe6060f1SDimitry Andric MachineMemOperand *SrcMMO = MF.getMachineMemOperand( 1276fe6060f1SDimitry Andric SrcPtrInfo, 1277fe6060f1SDimitry Andric MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable, MemSize, 1278fe6060f1SDimitry Andric SrcAlign); 1279fe6060f1SDimitry Andric 1280fe6060f1SDimitry Andric MachineMemOperand *DstMMO = MF.getMachineMemOperand( 1281fe6060f1SDimitry Andric DstPtrInfo, 1282fe6060f1SDimitry Andric MachineMemOperand::MOStore | MachineMemOperand::MODereferenceable, 1283fe6060f1SDimitry Andric MemSize, DstAlign); 1284fe6060f1SDimitry Andric 1285fe6060f1SDimitry Andric const LLT PtrTy = MRI.getType(DstPtr); 1286fe6060f1SDimitry Andric const LLT SizeTy = LLT::scalar(PtrTy.getSizeInBits()); 1287fe6060f1SDimitry Andric 1288fe6060f1SDimitry Andric auto SizeConst = MIRBuilder.buildConstant(SizeTy, MemSize); 1289fe6060f1SDimitry Andric MIRBuilder.buildMemCpy(DstPtr, SrcPtr, SizeConst, *DstMMO, *SrcMMO); 1290fe6060f1SDimitry Andric } 1291fe6060f1SDimitry Andric 12920b57cec5SDimitry Andric Register CallLowering::ValueHandler::extendRegister(Register ValReg, 12935f757f3fSDimitry Andric const CCValAssign &VA, 12945ffd83dbSDimitry Andric unsigned MaxSizeBits) { 12950b57cec5SDimitry Andric LLT LocTy{VA.getLocVT()}; 1296fe6060f1SDimitry Andric LLT ValTy{VA.getValVT()}; 1297fe6060f1SDimitry Andric 12985ffd83dbSDimitry Andric if (LocTy.getSizeInBits() == ValTy.getSizeInBits()) 12990b57cec5SDimitry Andric return ValReg; 13005ffd83dbSDimitry Andric 13015ffd83dbSDimitry Andric if (LocTy.isScalar() && MaxSizeBits && MaxSizeBits < LocTy.getSizeInBits()) { 13025ffd83dbSDimitry Andric if (MaxSizeBits <= ValTy.getSizeInBits()) 13035ffd83dbSDimitry Andric return ValReg; 13045ffd83dbSDimitry Andric LocTy = LLT::scalar(MaxSizeBits); 13055ffd83dbSDimitry Andric } 13065ffd83dbSDimitry Andric 1307fe6060f1SDimitry Andric const LLT ValRegTy = MRI.getType(ValReg); 1308fe6060f1SDimitry Andric if (ValRegTy.isPointer()) { 1309fe6060f1SDimitry Andric // The x32 ABI wants to zero extend 32-bit pointers to 64-bit registers, so 1310fe6060f1SDimitry Andric // we have to cast to do the extension. 1311fe6060f1SDimitry Andric LLT IntPtrTy = LLT::scalar(ValRegTy.getSizeInBits()); 1312fe6060f1SDimitry Andric ValReg = MIRBuilder.buildPtrToInt(IntPtrTy, ValReg).getReg(0); 1313fe6060f1SDimitry Andric } 1314fe6060f1SDimitry Andric 13150b57cec5SDimitry Andric switch (VA.getLocInfo()) { 1316*0fca6ea1SDimitry Andric default: 1317*0fca6ea1SDimitry Andric break; 13180b57cec5SDimitry Andric case CCValAssign::Full: 13190b57cec5SDimitry Andric case CCValAssign::BCvt: 13200b57cec5SDimitry Andric // FIXME: bitconverting between vector types may or may not be a 13210b57cec5SDimitry Andric // nop in big-endian situations. 13220b57cec5SDimitry Andric return ValReg; 13230b57cec5SDimitry Andric case CCValAssign::AExt: { 13240b57cec5SDimitry Andric auto MIB = MIRBuilder.buildAnyExt(LocTy, ValReg); 13255ffd83dbSDimitry Andric return MIB.getReg(0); 13260b57cec5SDimitry Andric } 13270b57cec5SDimitry Andric case CCValAssign::SExt: { 13280b57cec5SDimitry Andric Register NewReg = MRI.createGenericVirtualRegister(LocTy); 13290b57cec5SDimitry Andric MIRBuilder.buildSExt(NewReg, ValReg); 13300b57cec5SDimitry Andric return NewReg; 13310b57cec5SDimitry Andric } 13320b57cec5SDimitry Andric case CCValAssign::ZExt: { 13330b57cec5SDimitry Andric Register NewReg = MRI.createGenericVirtualRegister(LocTy); 13340b57cec5SDimitry Andric MIRBuilder.buildZExt(NewReg, ValReg); 13350b57cec5SDimitry Andric return NewReg; 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric } 13380b57cec5SDimitry Andric llvm_unreachable("unable to extend register"); 13390b57cec5SDimitry Andric } 13400b57cec5SDimitry Andric 1341fe6060f1SDimitry Andric void CallLowering::ValueAssigner::anchor() {} 1342fe6060f1SDimitry Andric 13435f757f3fSDimitry Andric Register CallLowering::IncomingValueHandler::buildExtensionHint( 13445f757f3fSDimitry Andric const CCValAssign &VA, Register SrcReg, LLT NarrowTy) { 1345fe6060f1SDimitry Andric switch (VA.getLocInfo()) { 1346fe6060f1SDimitry Andric case CCValAssign::LocInfo::ZExt: { 1347fe6060f1SDimitry Andric return MIRBuilder 1348fe6060f1SDimitry Andric .buildAssertZExt(MRI.cloneVirtualRegister(SrcReg), SrcReg, 1349fe6060f1SDimitry Andric NarrowTy.getScalarSizeInBits()) 1350fe6060f1SDimitry Andric .getReg(0); 1351fe6060f1SDimitry Andric } 1352fe6060f1SDimitry Andric case CCValAssign::LocInfo::SExt: { 1353fe6060f1SDimitry Andric return MIRBuilder 1354fe6060f1SDimitry Andric .buildAssertSExt(MRI.cloneVirtualRegister(SrcReg), SrcReg, 1355fe6060f1SDimitry Andric NarrowTy.getScalarSizeInBits()) 1356fe6060f1SDimitry Andric .getReg(0); 1357fe6060f1SDimitry Andric break; 1358fe6060f1SDimitry Andric } 1359fe6060f1SDimitry Andric default: 1360fe6060f1SDimitry Andric return SrcReg; 1361fe6060f1SDimitry Andric } 1362fe6060f1SDimitry Andric } 1363fe6060f1SDimitry Andric 1364fe6060f1SDimitry Andric /// Check if we can use a basic COPY instruction between the two types. 1365fe6060f1SDimitry Andric /// 1366fe6060f1SDimitry Andric /// We're currently building on top of the infrastructure using MVT, which loses 1367fe6060f1SDimitry Andric /// pointer information in the CCValAssign. We accept copies from physical 1368fe6060f1SDimitry Andric /// registers that have been reported as integers if it's to an equivalent sized 1369fe6060f1SDimitry Andric /// pointer LLT. 1370fe6060f1SDimitry Andric static bool isCopyCompatibleType(LLT SrcTy, LLT DstTy) { 1371fe6060f1SDimitry Andric if (SrcTy == DstTy) 1372fe6060f1SDimitry Andric return true; 1373fe6060f1SDimitry Andric 1374fe6060f1SDimitry Andric if (SrcTy.getSizeInBits() != DstTy.getSizeInBits()) 1375fe6060f1SDimitry Andric return false; 1376fe6060f1SDimitry Andric 1377fe6060f1SDimitry Andric SrcTy = SrcTy.getScalarType(); 1378fe6060f1SDimitry Andric DstTy = DstTy.getScalarType(); 1379fe6060f1SDimitry Andric 1380fe6060f1SDimitry Andric return (SrcTy.isPointer() && DstTy.isScalar()) || 1381bdd1243dSDimitry Andric (DstTy.isPointer() && SrcTy.isScalar()); 1382fe6060f1SDimitry Andric } 1383fe6060f1SDimitry Andric 13845f757f3fSDimitry Andric void CallLowering::IncomingValueHandler::assignValueToReg( 13855f757f3fSDimitry Andric Register ValVReg, Register PhysReg, const CCValAssign &VA) { 1386fe6060f1SDimitry Andric const MVT LocVT = VA.getLocVT(); 1387fe6060f1SDimitry Andric const LLT LocTy(LocVT); 1388fe6060f1SDimitry Andric const LLT RegTy = MRI.getType(ValVReg); 1389fe6060f1SDimitry Andric 1390fe6060f1SDimitry Andric if (isCopyCompatibleType(RegTy, LocTy)) { 1391fe6060f1SDimitry Andric MIRBuilder.buildCopy(ValVReg, PhysReg); 1392fe6060f1SDimitry Andric return; 1393fe6060f1SDimitry Andric } 1394fe6060f1SDimitry Andric 1395fe6060f1SDimitry Andric auto Copy = MIRBuilder.buildCopy(LocTy, PhysReg); 1396fe6060f1SDimitry Andric auto Hint = buildExtensionHint(VA, Copy.getReg(0), RegTy); 1397fe6060f1SDimitry Andric MIRBuilder.buildTrunc(ValVReg, Hint); 1398fe6060f1SDimitry Andric } 1399