10b57cec5SDimitry Andric //===-- llvm/lib/Target/AMDGPU/AMDGPUCallLowering.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 the lowering of LLVM calls to machine code calls for 110b57cec5SDimitry Andric /// GlobalISel. 120b57cec5SDimitry Andric /// 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "AMDGPUCallLowering.h" 160b57cec5SDimitry Andric #include "AMDGPU.h" 17e8d8bef9SDimitry Andric #include "AMDGPULegalizerInfo.h" 185ffd83dbSDimitry Andric #include "AMDGPUTargetMachine.h" 190b57cec5SDimitry Andric #include "SIMachineFunctionInfo.h" 200b57cec5SDimitry Andric #include "SIRegisterInfo.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h" 22e8d8bef9SDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 24e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h" 25e8d8bef9SDimitry Andric 26e8d8bef9SDimitry Andric #define DEBUG_TYPE "amdgpu-call-lowering" 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace llvm; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric namespace { 310b57cec5SDimitry Andric 32fe6060f1SDimitry Andric /// Wrapper around extendRegister to ensure we extend to a full 32-bit register. 33fe6060f1SDimitry Andric static Register extendRegisterMin32(CallLowering::ValueHandler &Handler, 34fe6060f1SDimitry Andric Register ValVReg, CCValAssign &VA) { 35e8d8bef9SDimitry Andric if (VA.getLocVT().getSizeInBits() < 32) { 36e8d8bef9SDimitry Andric // 16-bit types are reported as legal for 32-bit registers. We need to 37e8d8bef9SDimitry Andric // extend and do a 32-bit copy to avoid the verifier complaining about it. 38fe6060f1SDimitry Andric return Handler.MIRBuilder.buildAnyExt(LLT::scalar(32), ValVReg).getReg(0); 39e8d8bef9SDimitry Andric } 40e8d8bef9SDimitry Andric 41fe6060f1SDimitry Andric return Handler.extendRegister(ValVReg, VA); 42e8d8bef9SDimitry Andric } 43e8d8bef9SDimitry Andric 44fe6060f1SDimitry Andric struct AMDGPUOutgoingValueHandler : public CallLowering::OutgoingValueHandler { 45e8d8bef9SDimitry Andric AMDGPUOutgoingValueHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI, 46fe6060f1SDimitry Andric MachineInstrBuilder MIB) 47fe6060f1SDimitry Andric : OutgoingValueHandler(B, MRI), MIB(MIB) {} 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric MachineInstrBuilder MIB; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric Register getStackAddress(uint64_t Size, int64_t Offset, 52fe6060f1SDimitry Andric MachinePointerInfo &MPO, 53fe6060f1SDimitry Andric ISD::ArgFlagsTy Flags) override { 540b57cec5SDimitry Andric llvm_unreachable("not implemented"); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 57fe6060f1SDimitry Andric void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy, 580b57cec5SDimitry Andric MachinePointerInfo &MPO, CCValAssign &VA) override { 590b57cec5SDimitry Andric llvm_unreachable("not implemented"); 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric void assignValueToReg(Register ValVReg, Register PhysReg, 63349cc55cSDimitry Andric CCValAssign VA) override { 64fe6060f1SDimitry Andric Register ExtReg = extendRegisterMin32(*this, ValVReg, VA); 658bcb0991SDimitry Andric 665ffd83dbSDimitry Andric // If this is a scalar return, insert a readfirstlane just in case the value 675ffd83dbSDimitry Andric // ends up in a VGPR. 685ffd83dbSDimitry Andric // FIXME: Assert this is a shader return. 695ffd83dbSDimitry Andric const SIRegisterInfo *TRI 705ffd83dbSDimitry Andric = static_cast<const SIRegisterInfo *>(MRI.getTargetRegisterInfo()); 715ffd83dbSDimitry Andric if (TRI->isSGPRReg(MRI, PhysReg)) { 725ffd83dbSDimitry Andric auto ToSGPR = MIRBuilder.buildIntrinsic(Intrinsic::amdgcn_readfirstlane, 735ffd83dbSDimitry Andric {MRI.getType(ExtReg)}, false) 745ffd83dbSDimitry Andric .addReg(ExtReg); 755ffd83dbSDimitry Andric ExtReg = ToSGPR.getReg(0); 765ffd83dbSDimitry Andric } 775ffd83dbSDimitry Andric 788bcb0991SDimitry Andric MIRBuilder.buildCopy(PhysReg, ExtReg); 798bcb0991SDimitry Andric MIB.addUse(PhysReg, RegState::Implicit); 800b57cec5SDimitry Andric } 818bcb0991SDimitry Andric }; 828bcb0991SDimitry Andric 83fe6060f1SDimitry Andric struct AMDGPUIncomingArgHandler : public CallLowering::IncomingValueHandler { 848bcb0991SDimitry Andric uint64_t StackUsed = 0; 858bcb0991SDimitry Andric 86fe6060f1SDimitry Andric AMDGPUIncomingArgHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI) 87fe6060f1SDimitry Andric : IncomingValueHandler(B, MRI) {} 888bcb0991SDimitry Andric 898bcb0991SDimitry Andric Register getStackAddress(uint64_t Size, int64_t Offset, 90fe6060f1SDimitry Andric MachinePointerInfo &MPO, 91fe6060f1SDimitry Andric ISD::ArgFlagsTy Flags) override { 928bcb0991SDimitry Andric auto &MFI = MIRBuilder.getMF().getFrameInfo(); 93fe6060f1SDimitry Andric 94fe6060f1SDimitry Andric // Byval is assumed to be writable memory, but other stack passed arguments 95fe6060f1SDimitry Andric // are not. 96fe6060f1SDimitry Andric const bool IsImmutable = !Flags.isByVal(); 97fe6060f1SDimitry Andric int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable); 988bcb0991SDimitry Andric MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI); 995ffd83dbSDimitry Andric auto AddrReg = MIRBuilder.buildFrameIndex( 1005ffd83dbSDimitry Andric LLT::pointer(AMDGPUAS::PRIVATE_ADDRESS, 32), FI); 1018bcb0991SDimitry Andric StackUsed = std::max(StackUsed, Size + Offset); 1025ffd83dbSDimitry Andric return AddrReg.getReg(0); 1038bcb0991SDimitry Andric } 1048bcb0991SDimitry Andric 1058bcb0991SDimitry Andric void assignValueToReg(Register ValVReg, Register PhysReg, 106349cc55cSDimitry Andric CCValAssign VA) override { 1078bcb0991SDimitry Andric markPhysRegUsed(PhysReg); 1088bcb0991SDimitry Andric 1098bcb0991SDimitry Andric if (VA.getLocVT().getSizeInBits() < 32) { 1108bcb0991SDimitry Andric // 16-bit types are reported as legal for 32-bit registers. We need to do 1118bcb0991SDimitry Andric // a 32-bit copy, and truncate to avoid the verifier complaining about it. 1128bcb0991SDimitry Andric auto Copy = MIRBuilder.buildCopy(LLT::scalar(32), PhysReg); 113fe6060f1SDimitry Andric 114fe6060f1SDimitry Andric // If we have signext/zeroext, it applies to the whole 32-bit register 115fe6060f1SDimitry Andric // before truncation. 116fe6060f1SDimitry Andric auto Extended = 117fe6060f1SDimitry Andric buildExtensionHint(VA, Copy.getReg(0), LLT(VA.getLocVT())); 118fe6060f1SDimitry Andric MIRBuilder.buildTrunc(ValVReg, Extended); 1198bcb0991SDimitry Andric return; 1208bcb0991SDimitry Andric } 1218bcb0991SDimitry Andric 122fe6060f1SDimitry Andric IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA); 1238bcb0991SDimitry Andric } 1248bcb0991SDimitry Andric 125fe6060f1SDimitry Andric void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy, 1268bcb0991SDimitry Andric MachinePointerInfo &MPO, CCValAssign &VA) override { 1275ffd83dbSDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 1285ffd83dbSDimitry Andric 1295ffd83dbSDimitry Andric auto MMO = MF.getMachineMemOperand( 130fe6060f1SDimitry Andric MPO, MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, MemTy, 1315ffd83dbSDimitry Andric inferAlignFromPtrInfo(MF, MPO)); 1328bcb0991SDimitry Andric MIRBuilder.buildLoad(ValVReg, Addr, *MMO); 1338bcb0991SDimitry Andric } 1348bcb0991SDimitry Andric 1358bcb0991SDimitry Andric /// How the physical register gets marked varies between formal 1368bcb0991SDimitry Andric /// parameters (it's a basic-block live-in), and a call instruction 1378bcb0991SDimitry Andric /// (it's an implicit-def of the BL). 1388bcb0991SDimitry Andric virtual void markPhysRegUsed(unsigned PhysReg) = 0; 1398bcb0991SDimitry Andric }; 1408bcb0991SDimitry Andric 141e8d8bef9SDimitry Andric struct FormalArgHandler : public AMDGPUIncomingArgHandler { 142fe6060f1SDimitry Andric FormalArgHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI) 143fe6060f1SDimitry Andric : AMDGPUIncomingArgHandler(B, MRI) {} 1448bcb0991SDimitry Andric 1458bcb0991SDimitry Andric void markPhysRegUsed(unsigned PhysReg) override { 1468bcb0991SDimitry Andric MIRBuilder.getMBB().addLiveIn(PhysReg); 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric }; 1490b57cec5SDimitry Andric 150e8d8bef9SDimitry Andric struct CallReturnHandler : public AMDGPUIncomingArgHandler { 151e8d8bef9SDimitry Andric CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI, 152fe6060f1SDimitry Andric MachineInstrBuilder MIB) 153fe6060f1SDimitry Andric : AMDGPUIncomingArgHandler(MIRBuilder, MRI), MIB(MIB) {} 154e8d8bef9SDimitry Andric 155e8d8bef9SDimitry Andric void markPhysRegUsed(unsigned PhysReg) override { 156e8d8bef9SDimitry Andric MIB.addDef(PhysReg, RegState::Implicit); 157e8d8bef9SDimitry Andric } 158e8d8bef9SDimitry Andric 159e8d8bef9SDimitry Andric MachineInstrBuilder MIB; 160e8d8bef9SDimitry Andric }; 161e8d8bef9SDimitry Andric 162fe6060f1SDimitry Andric struct AMDGPUOutgoingArgHandler : public AMDGPUOutgoingValueHandler { 163e8d8bef9SDimitry Andric /// For tail calls, the byte offset of the call's argument area from the 164e8d8bef9SDimitry Andric /// callee's. Unused elsewhere. 165e8d8bef9SDimitry Andric int FPDiff; 166e8d8bef9SDimitry Andric 167e8d8bef9SDimitry Andric // Cache the SP register vreg if we need it more than once in this call site. 168e8d8bef9SDimitry Andric Register SPReg; 169e8d8bef9SDimitry Andric 170e8d8bef9SDimitry Andric bool IsTailCall; 171e8d8bef9SDimitry Andric 172e8d8bef9SDimitry Andric AMDGPUOutgoingArgHandler(MachineIRBuilder &MIRBuilder, 173e8d8bef9SDimitry Andric MachineRegisterInfo &MRI, MachineInstrBuilder MIB, 174e8d8bef9SDimitry Andric bool IsTailCall = false, int FPDiff = 0) 175fe6060f1SDimitry Andric : AMDGPUOutgoingValueHandler(MIRBuilder, MRI, MIB), FPDiff(FPDiff), 176fe6060f1SDimitry Andric IsTailCall(IsTailCall) {} 177e8d8bef9SDimitry Andric 178e8d8bef9SDimitry Andric Register getStackAddress(uint64_t Size, int64_t Offset, 179fe6060f1SDimitry Andric MachinePointerInfo &MPO, 180fe6060f1SDimitry Andric ISD::ArgFlagsTy Flags) override { 181e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 182e8d8bef9SDimitry Andric const LLT PtrTy = LLT::pointer(AMDGPUAS::PRIVATE_ADDRESS, 32); 183e8d8bef9SDimitry Andric const LLT S32 = LLT::scalar(32); 184e8d8bef9SDimitry Andric 185e8d8bef9SDimitry Andric if (IsTailCall) { 186fe6060f1SDimitry Andric Offset += FPDiff; 187fe6060f1SDimitry Andric int FI = MF.getFrameInfo().CreateFixedObject(Size, Offset, true); 188fe6060f1SDimitry Andric auto FIReg = MIRBuilder.buildFrameIndex(PtrTy, FI); 189fe6060f1SDimitry Andric MPO = MachinePointerInfo::getFixedStack(MF, FI); 190fe6060f1SDimitry Andric return FIReg.getReg(0); 191e8d8bef9SDimitry Andric } 192e8d8bef9SDimitry Andric 193e8d8bef9SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 194e8d8bef9SDimitry Andric 195e8d8bef9SDimitry Andric if (!SPReg) 196e8d8bef9SDimitry Andric SPReg = MIRBuilder.buildCopy(PtrTy, MFI->getStackPtrOffsetReg()).getReg(0); 197e8d8bef9SDimitry Andric 198e8d8bef9SDimitry Andric auto OffsetReg = MIRBuilder.buildConstant(S32, Offset); 199e8d8bef9SDimitry Andric 200e8d8bef9SDimitry Andric auto AddrReg = MIRBuilder.buildPtrAdd(PtrTy, SPReg, OffsetReg); 201e8d8bef9SDimitry Andric MPO = MachinePointerInfo::getStack(MF, Offset); 202e8d8bef9SDimitry Andric return AddrReg.getReg(0); 203e8d8bef9SDimitry Andric } 204e8d8bef9SDimitry Andric 205e8d8bef9SDimitry Andric void assignValueToReg(Register ValVReg, Register PhysReg, 206349cc55cSDimitry Andric CCValAssign VA) override { 207e8d8bef9SDimitry Andric MIB.addUse(PhysReg, RegState::Implicit); 208fe6060f1SDimitry Andric Register ExtReg = extendRegisterMin32(*this, ValVReg, VA); 209e8d8bef9SDimitry Andric MIRBuilder.buildCopy(PhysReg, ExtReg); 210e8d8bef9SDimitry Andric } 211e8d8bef9SDimitry Andric 212fe6060f1SDimitry Andric void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy, 213e8d8bef9SDimitry Andric MachinePointerInfo &MPO, CCValAssign &VA) override { 214e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 215e8d8bef9SDimitry Andric uint64_t LocMemOffset = VA.getLocMemOffset(); 216e8d8bef9SDimitry Andric const auto &ST = MF.getSubtarget<GCNSubtarget>(); 217e8d8bef9SDimitry Andric 218e8d8bef9SDimitry Andric auto MMO = MF.getMachineMemOperand( 219fe6060f1SDimitry Andric MPO, MachineMemOperand::MOStore, MemTy, 220e8d8bef9SDimitry Andric commonAlignment(ST.getStackAlignment(), LocMemOffset)); 221e8d8bef9SDimitry Andric MIRBuilder.buildStore(ValVReg, Addr, *MMO); 222e8d8bef9SDimitry Andric } 223e8d8bef9SDimitry Andric 224fe6060f1SDimitry Andric void assignValueToAddress(const CallLowering::ArgInfo &Arg, 225fe6060f1SDimitry Andric unsigned ValRegIndex, Register Addr, LLT MemTy, 226fe6060f1SDimitry Andric MachinePointerInfo &MPO, CCValAssign &VA) override { 227e8d8bef9SDimitry Andric Register ValVReg = VA.getLocInfo() != CCValAssign::LocInfo::FPExt 228fe6060f1SDimitry Andric ? extendRegister(Arg.Regs[ValRegIndex], VA) 229fe6060f1SDimitry Andric : Arg.Regs[ValRegIndex]; 230fe6060f1SDimitry Andric assignValueToAddress(ValVReg, Addr, MemTy, MPO, VA); 231e8d8bef9SDimitry Andric } 232e8d8bef9SDimitry Andric }; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric AMDGPUCallLowering::AMDGPUCallLowering(const AMDGPUTargetLowering &TLI) 2360b57cec5SDimitry Andric : CallLowering(&TLI) { 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 239349cc55cSDimitry Andric // FIXME: Compatibility shim 2405ffd83dbSDimitry Andric static ISD::NodeType extOpcodeToISDExtOpcode(unsigned MIOpc) { 2415ffd83dbSDimitry Andric switch (MIOpc) { 2425ffd83dbSDimitry Andric case TargetOpcode::G_SEXT: 2435ffd83dbSDimitry Andric return ISD::SIGN_EXTEND; 2445ffd83dbSDimitry Andric case TargetOpcode::G_ZEXT: 2455ffd83dbSDimitry Andric return ISD::ZERO_EXTEND; 2465ffd83dbSDimitry Andric case TargetOpcode::G_ANYEXT: 2475ffd83dbSDimitry Andric return ISD::ANY_EXTEND; 2485ffd83dbSDimitry Andric default: 2495ffd83dbSDimitry Andric llvm_unreachable("not an extend opcode"); 2505ffd83dbSDimitry Andric } 2515ffd83dbSDimitry Andric } 2525ffd83dbSDimitry Andric 253e8d8bef9SDimitry Andric bool AMDGPUCallLowering::canLowerReturn(MachineFunction &MF, 254e8d8bef9SDimitry Andric CallingConv::ID CallConv, 255e8d8bef9SDimitry Andric SmallVectorImpl<BaseArgInfo> &Outs, 256e8d8bef9SDimitry Andric bool IsVarArg) const { 257e8d8bef9SDimitry Andric // For shaders. Vector types should be explicitly handled by CC. 258e8d8bef9SDimitry Andric if (AMDGPU::isEntryFunctionCC(CallConv)) 259e8d8bef9SDimitry Andric return true; 260e8d8bef9SDimitry Andric 261e8d8bef9SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 262e8d8bef9SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 263e8d8bef9SDimitry Andric CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, 264e8d8bef9SDimitry Andric MF.getFunction().getContext()); 265e8d8bef9SDimitry Andric 266e8d8bef9SDimitry Andric return checkReturn(CCInfo, Outs, TLI.CCAssignFnForReturn(CallConv, IsVarArg)); 2678bcb0991SDimitry Andric } 2688bcb0991SDimitry Andric 2698bcb0991SDimitry Andric /// Lower the return value for the already existing \p Ret. This assumes that 2708bcb0991SDimitry Andric /// \p B's insertion point is correct. 2718bcb0991SDimitry Andric bool AMDGPUCallLowering::lowerReturnVal(MachineIRBuilder &B, 2728bcb0991SDimitry Andric const Value *Val, ArrayRef<Register> VRegs, 2738bcb0991SDimitry Andric MachineInstrBuilder &Ret) const { 2748bcb0991SDimitry Andric if (!Val) 2758bcb0991SDimitry Andric return true; 2768bcb0991SDimitry Andric 2778bcb0991SDimitry Andric auto &MF = B.getMF(); 2788bcb0991SDimitry Andric const auto &F = MF.getFunction(); 2798bcb0991SDimitry Andric const DataLayout &DL = MF.getDataLayout(); 2805ffd83dbSDimitry Andric MachineRegisterInfo *MRI = B.getMRI(); 281e8d8bef9SDimitry Andric LLVMContext &Ctx = F.getContext(); 2828bcb0991SDimitry Andric 2838bcb0991SDimitry Andric CallingConv::ID CC = F.getCallingConv(); 2848bcb0991SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 2858bcb0991SDimitry Andric 286e8d8bef9SDimitry Andric SmallVector<EVT, 8> SplitEVTs; 287e8d8bef9SDimitry Andric ComputeValueVTs(TLI, DL, Val->getType(), SplitEVTs); 288e8d8bef9SDimitry Andric assert(VRegs.size() == SplitEVTs.size() && 289e8d8bef9SDimitry Andric "For each split Type there should be exactly one VReg."); 2908bcb0991SDimitry Andric 291e8d8bef9SDimitry Andric SmallVector<ArgInfo, 8> SplitRetInfos; 292e8d8bef9SDimitry Andric 293e8d8bef9SDimitry Andric for (unsigned i = 0; i < SplitEVTs.size(); ++i) { 294e8d8bef9SDimitry Andric EVT VT = SplitEVTs[i]; 295e8d8bef9SDimitry Andric Register Reg = VRegs[i]; 296fe6060f1SDimitry Andric ArgInfo RetInfo(Reg, VT.getTypeForEVT(Ctx), 0); 297e8d8bef9SDimitry Andric setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F); 298e8d8bef9SDimitry Andric 299e8d8bef9SDimitry Andric if (VT.isScalarInteger()) { 300e8d8bef9SDimitry Andric unsigned ExtendOp = TargetOpcode::G_ANYEXT; 301e8d8bef9SDimitry Andric if (RetInfo.Flags[0].isSExt()) { 302e8d8bef9SDimitry Andric assert(RetInfo.Regs.size() == 1 && "expect only simple return values"); 303e8d8bef9SDimitry Andric ExtendOp = TargetOpcode::G_SEXT; 304e8d8bef9SDimitry Andric } else if (RetInfo.Flags[0].isZExt()) { 305e8d8bef9SDimitry Andric assert(RetInfo.Regs.size() == 1 && "expect only simple return values"); 306e8d8bef9SDimitry Andric ExtendOp = TargetOpcode::G_ZEXT; 307e8d8bef9SDimitry Andric } 308e8d8bef9SDimitry Andric 309e8d8bef9SDimitry Andric EVT ExtVT = TLI.getTypeForExtReturn(Ctx, VT, 310e8d8bef9SDimitry Andric extOpcodeToISDExtOpcode(ExtendOp)); 311e8d8bef9SDimitry Andric if (ExtVT != VT) { 312e8d8bef9SDimitry Andric RetInfo.Ty = ExtVT.getTypeForEVT(Ctx); 313e8d8bef9SDimitry Andric LLT ExtTy = getLLTForType(*RetInfo.Ty, DL); 314e8d8bef9SDimitry Andric Reg = B.buildInstr(ExtendOp, {ExtTy}, {Reg}).getReg(0); 315e8d8bef9SDimitry Andric } 316e8d8bef9SDimitry Andric } 317e8d8bef9SDimitry Andric 318e8d8bef9SDimitry Andric if (Reg != RetInfo.Regs[0]) { 319e8d8bef9SDimitry Andric RetInfo.Regs[0] = Reg; 320e8d8bef9SDimitry Andric // Reset the arg flags after modifying Reg. 321e8d8bef9SDimitry Andric setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F); 322e8d8bef9SDimitry Andric } 323e8d8bef9SDimitry Andric 324fe6060f1SDimitry Andric splitToValueTypes(RetInfo, SplitRetInfos, DL, CC); 325e8d8bef9SDimitry Andric } 3268bcb0991SDimitry Andric 3278bcb0991SDimitry Andric CCAssignFn *AssignFn = TLI.CCAssignFnForReturn(CC, F.isVarArg()); 328fe6060f1SDimitry Andric 329fe6060f1SDimitry Andric OutgoingValueAssigner Assigner(AssignFn); 330fe6060f1SDimitry Andric AMDGPUOutgoingValueHandler RetHandler(B, *MRI, Ret); 331fe6060f1SDimitry Andric return determineAndHandleAssignments(RetHandler, Assigner, SplitRetInfos, B, 332fe6060f1SDimitry Andric CC, F.isVarArg()); 3338bcb0991SDimitry Andric } 3348bcb0991SDimitry Andric 335e8d8bef9SDimitry Andric bool AMDGPUCallLowering::lowerReturn(MachineIRBuilder &B, const Value *Val, 336e8d8bef9SDimitry Andric ArrayRef<Register> VRegs, 337e8d8bef9SDimitry Andric FunctionLoweringInfo &FLI) const { 3380b57cec5SDimitry Andric 3398bcb0991SDimitry Andric MachineFunction &MF = B.getMF(); 3400b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 3410b57cec5SDimitry Andric SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 3420b57cec5SDimitry Andric MFI->setIfReturnsVoid(!Val); 3430b57cec5SDimitry Andric 3448bcb0991SDimitry Andric assert(!Val == VRegs.empty() && "Return value without a vreg"); 3458bcb0991SDimitry Andric 3468bcb0991SDimitry Andric CallingConv::ID CC = B.getMF().getFunction().getCallingConv(); 3478bcb0991SDimitry Andric const bool IsShader = AMDGPU::isShader(CC); 348e8d8bef9SDimitry Andric const bool IsWaveEnd = 349e8d8bef9SDimitry Andric (IsShader && MFI->returnsVoid()) || AMDGPU::isKernel(CC); 3508bcb0991SDimitry Andric if (IsWaveEnd) { 3518bcb0991SDimitry Andric B.buildInstr(AMDGPU::S_ENDPGM) 3528bcb0991SDimitry Andric .addImm(0); 3530b57cec5SDimitry Andric return true; 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3565ffd83dbSDimitry Andric auto const &ST = MF.getSubtarget<GCNSubtarget>(); 3570b57cec5SDimitry Andric 358349cc55cSDimitry Andric unsigned ReturnOpc = 0; 359349cc55cSDimitry Andric if (IsShader) 360349cc55cSDimitry Andric ReturnOpc = AMDGPU::SI_RETURN_TO_EPILOG; 361349cc55cSDimitry Andric else if (CC == CallingConv::AMDGPU_Gfx) 362349cc55cSDimitry Andric ReturnOpc = AMDGPU::S_SETPC_B64_return_gfx; 363349cc55cSDimitry Andric else 364349cc55cSDimitry Andric ReturnOpc = AMDGPU::S_SETPC_B64_return; 3650b57cec5SDimitry Andric 3668bcb0991SDimitry Andric auto Ret = B.buildInstrNoInsert(ReturnOpc); 3678bcb0991SDimitry Andric Register ReturnAddrVReg; 3688bcb0991SDimitry Andric if (ReturnOpc == AMDGPU::S_SETPC_B64_return) { 3698bcb0991SDimitry Andric ReturnAddrVReg = MRI.createVirtualRegister(&AMDGPU::CCR_SGPR_64RegClass); 3708bcb0991SDimitry Andric Ret.addUse(ReturnAddrVReg); 371349cc55cSDimitry Andric } else if (ReturnOpc == AMDGPU::S_SETPC_B64_return_gfx) { 372349cc55cSDimitry Andric ReturnAddrVReg = 373349cc55cSDimitry Andric MRI.createVirtualRegister(&AMDGPU::Gfx_CCR_SGPR_64RegClass); 374349cc55cSDimitry Andric Ret.addUse(ReturnAddrVReg); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 377e8d8bef9SDimitry Andric if (!FLI.CanLowerReturn) 378e8d8bef9SDimitry Andric insertSRetStores(B, Val->getType(), VRegs, FLI.DemoteRegister); 379e8d8bef9SDimitry Andric else if (!lowerReturnVal(B, Val, VRegs, Ret)) 3808bcb0991SDimitry Andric return false; 3818bcb0991SDimitry Andric 382349cc55cSDimitry Andric if (ReturnOpc == AMDGPU::S_SETPC_B64_return || 383349cc55cSDimitry Andric ReturnOpc == AMDGPU::S_SETPC_B64_return_gfx) { 3848bcb0991SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 3858bcb0991SDimitry Andric Register LiveInReturn = MF.addLiveIn(TRI->getReturnAddressReg(MF), 3868bcb0991SDimitry Andric &AMDGPU::SGPR_64RegClass); 3878bcb0991SDimitry Andric B.buildCopy(ReturnAddrVReg, LiveInReturn); 3888bcb0991SDimitry Andric } 3898bcb0991SDimitry Andric 3908bcb0991SDimitry Andric // TODO: Handle CalleeSavedRegsViaCopy. 3918bcb0991SDimitry Andric 3928bcb0991SDimitry Andric B.insertInstr(Ret); 3930b57cec5SDimitry Andric return true; 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 396e8d8bef9SDimitry Andric void AMDGPUCallLowering::lowerParameterPtr(Register DstReg, MachineIRBuilder &B, 3970b57cec5SDimitry Andric uint64_t Offset) const { 3988bcb0991SDimitry Andric MachineFunction &MF = B.getMF(); 3990b57cec5SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 4000b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 4010b57cec5SDimitry Andric Register KernArgSegmentPtr = 4020b57cec5SDimitry Andric MFI->getPreloadedReg(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR); 4030b57cec5SDimitry Andric Register KernArgSegmentVReg = MRI.getLiveInVirtReg(KernArgSegmentPtr); 4040b57cec5SDimitry Andric 4055ffd83dbSDimitry Andric auto OffsetReg = B.buildConstant(LLT::scalar(64), Offset); 4060b57cec5SDimitry Andric 407e8d8bef9SDimitry Andric B.buildPtrAdd(DstReg, KernArgSegmentVReg, OffsetReg); 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric 410fe6060f1SDimitry Andric void AMDGPUCallLowering::lowerParameter(MachineIRBuilder &B, ArgInfo &OrigArg, 411fe6060f1SDimitry Andric uint64_t Offset, 412fe6060f1SDimitry Andric Align Alignment) const { 4138bcb0991SDimitry Andric MachineFunction &MF = B.getMF(); 4140b57cec5SDimitry Andric const Function &F = MF.getFunction(); 4150b57cec5SDimitry Andric const DataLayout &DL = F.getParent()->getDataLayout(); 416480093f4SDimitry Andric MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS); 417e8d8bef9SDimitry Andric 418e8d8bef9SDimitry Andric LLT PtrTy = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64); 419fe6060f1SDimitry Andric 420fe6060f1SDimitry Andric SmallVector<ArgInfo, 32> SplitArgs; 421fe6060f1SDimitry Andric SmallVector<uint64_t> FieldOffsets; 422fe6060f1SDimitry Andric splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv(), &FieldOffsets); 423fe6060f1SDimitry Andric 424fe6060f1SDimitry Andric unsigned Idx = 0; 425fe6060f1SDimitry Andric for (ArgInfo &SplitArg : SplitArgs) { 426e8d8bef9SDimitry Andric Register PtrReg = B.getMRI()->createGenericVirtualRegister(PtrTy); 427fe6060f1SDimitry Andric lowerParameterPtr(PtrReg, B, Offset + FieldOffsets[Idx]); 428fe6060f1SDimitry Andric 429fe6060f1SDimitry Andric LLT ArgTy = getLLTForType(*SplitArg.Ty, DL); 430fe6060f1SDimitry Andric if (SplitArg.Flags[0].isPointer()) { 431fe6060f1SDimitry Andric // Compensate for losing pointeriness in splitValueTypes. 432fe6060f1SDimitry Andric LLT PtrTy = LLT::pointer(SplitArg.Flags[0].getPointerAddrSpace(), 433fe6060f1SDimitry Andric ArgTy.getScalarSizeInBits()); 434fe6060f1SDimitry Andric ArgTy = ArgTy.isVector() ? LLT::vector(ArgTy.getElementCount(), PtrTy) 435fe6060f1SDimitry Andric : PtrTy; 436fe6060f1SDimitry Andric } 4370b57cec5SDimitry Andric 4385ffd83dbSDimitry Andric MachineMemOperand *MMO = MF.getMachineMemOperand( 4395ffd83dbSDimitry Andric PtrInfo, 4405ffd83dbSDimitry Andric MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 4410b57cec5SDimitry Andric MachineMemOperand::MOInvariant, 442fe6060f1SDimitry Andric ArgTy, commonAlignment(Alignment, FieldOffsets[Idx])); 4430b57cec5SDimitry Andric 444fe6060f1SDimitry Andric assert(SplitArg.Regs.size() == 1); 445fe6060f1SDimitry Andric 446fe6060f1SDimitry Andric B.buildLoad(SplitArg.Regs[0], PtrReg, *MMO); 447fe6060f1SDimitry Andric ++Idx; 448fe6060f1SDimitry Andric } 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric // Allocate special inputs passed in user SGPRs. 4520b57cec5SDimitry Andric static void allocateHSAUserSGPRs(CCState &CCInfo, 4538bcb0991SDimitry Andric MachineIRBuilder &B, 4540b57cec5SDimitry Andric MachineFunction &MF, 4550b57cec5SDimitry Andric const SIRegisterInfo &TRI, 4560b57cec5SDimitry Andric SIMachineFunctionInfo &Info) { 4570b57cec5SDimitry Andric // FIXME: How should these inputs interact with inreg / custom SGPR inputs? 4580b57cec5SDimitry Andric if (Info.hasPrivateSegmentBuffer()) { 4595ffd83dbSDimitry Andric Register PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI); 4600b57cec5SDimitry Andric MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass); 4610b57cec5SDimitry Andric CCInfo.AllocateReg(PrivateSegmentBufferReg); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric if (Info.hasDispatchPtr()) { 4655ffd83dbSDimitry Andric Register DispatchPtrReg = Info.addDispatchPtr(TRI); 4660b57cec5SDimitry Andric MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass); 4670b57cec5SDimitry Andric CCInfo.AllocateReg(DispatchPtrReg); 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric if (Info.hasQueuePtr()) { 4715ffd83dbSDimitry Andric Register QueuePtrReg = Info.addQueuePtr(TRI); 4720b57cec5SDimitry Andric MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass); 4730b57cec5SDimitry Andric CCInfo.AllocateReg(QueuePtrReg); 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric if (Info.hasKernargSegmentPtr()) { 4770b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 4780b57cec5SDimitry Andric Register InputPtrReg = Info.addKernargSegmentPtr(TRI); 4790b57cec5SDimitry Andric const LLT P4 = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64); 4800b57cec5SDimitry Andric Register VReg = MRI.createGenericVirtualRegister(P4); 4810b57cec5SDimitry Andric MRI.addLiveIn(InputPtrReg, VReg); 4828bcb0991SDimitry Andric B.getMBB().addLiveIn(InputPtrReg); 4838bcb0991SDimitry Andric B.buildCopy(VReg, InputPtrReg); 4840b57cec5SDimitry Andric CCInfo.AllocateReg(InputPtrReg); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric if (Info.hasDispatchID()) { 4885ffd83dbSDimitry Andric Register DispatchIDReg = Info.addDispatchID(TRI); 4890b57cec5SDimitry Andric MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass); 4900b57cec5SDimitry Andric CCInfo.AllocateReg(DispatchIDReg); 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric if (Info.hasFlatScratchInit()) { 4945ffd83dbSDimitry Andric Register FlatScratchInitReg = Info.addFlatScratchInit(TRI); 4950b57cec5SDimitry Andric MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass); 4960b57cec5SDimitry Andric CCInfo.AllocateReg(FlatScratchInitReg); 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read 5000b57cec5SDimitry Andric // these from the dispatch pointer. 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric bool AMDGPUCallLowering::lowerFormalArgumentsKernel( 5048bcb0991SDimitry Andric MachineIRBuilder &B, const Function &F, 5050b57cec5SDimitry Andric ArrayRef<ArrayRef<Register>> VRegs) const { 5068bcb0991SDimitry Andric MachineFunction &MF = B.getMF(); 5070b57cec5SDimitry Andric const GCNSubtarget *Subtarget = &MF.getSubtarget<GCNSubtarget>(); 5080b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 5090b57cec5SDimitry Andric SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 5108bcb0991SDimitry Andric const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 5118bcb0991SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 5120b57cec5SDimitry Andric const DataLayout &DL = F.getParent()->getDataLayout(); 5130b57cec5SDimitry Andric 514fe6060f1SDimitry Andric Info->allocateModuleLDSGlobal(F.getParent()); 515fe6060f1SDimitry Andric 5160b57cec5SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 5170b57cec5SDimitry Andric CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext()); 5180b57cec5SDimitry Andric 5198bcb0991SDimitry Andric allocateHSAUserSGPRs(CCInfo, B, MF, *TRI, *Info); 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric unsigned i = 0; 5225ffd83dbSDimitry Andric const Align KernArgBaseAlign(16); 5230b57cec5SDimitry Andric const unsigned BaseOffset = Subtarget->getExplicitKernelArgOffset(F); 5240b57cec5SDimitry Andric uint64_t ExplicitArgOffset = 0; 5250b57cec5SDimitry Andric 5260b57cec5SDimitry Andric // TODO: Align down to dword alignment and extract bits for extending loads. 5270b57cec5SDimitry Andric for (auto &Arg : F.args()) { 528e8d8bef9SDimitry Andric const bool IsByRef = Arg.hasByRefAttr(); 529e8d8bef9SDimitry Andric Type *ArgTy = IsByRef ? Arg.getParamByRefType() : Arg.getType(); 5300b57cec5SDimitry Andric unsigned AllocSize = DL.getTypeAllocSize(ArgTy); 5310b57cec5SDimitry Andric if (AllocSize == 0) 5320b57cec5SDimitry Andric continue; 5330b57cec5SDimitry Andric 534e8d8bef9SDimitry Andric MaybeAlign ABIAlign = IsByRef ? Arg.getParamAlign() : None; 535e8d8bef9SDimitry Andric if (!ABIAlign) 536e8d8bef9SDimitry Andric ABIAlign = DL.getABITypeAlign(ArgTy); 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric uint64_t ArgOffset = alignTo(ExplicitArgOffset, ABIAlign) + BaseOffset; 5390b57cec5SDimitry Andric ExplicitArgOffset = alignTo(ExplicitArgOffset, ABIAlign) + AllocSize; 5400b57cec5SDimitry Andric 5415ffd83dbSDimitry Andric if (Arg.use_empty()) { 5425ffd83dbSDimitry Andric ++i; 5435ffd83dbSDimitry Andric continue; 5445ffd83dbSDimitry Andric } 5455ffd83dbSDimitry Andric 546e8d8bef9SDimitry Andric Align Alignment = commonAlignment(KernArgBaseAlign, ArgOffset); 547e8d8bef9SDimitry Andric 548e8d8bef9SDimitry Andric if (IsByRef) { 549e8d8bef9SDimitry Andric unsigned ByRefAS = cast<PointerType>(Arg.getType())->getAddressSpace(); 550e8d8bef9SDimitry Andric 551e8d8bef9SDimitry Andric assert(VRegs[i].size() == 1 && 552e8d8bef9SDimitry Andric "expected only one register for byval pointers"); 553e8d8bef9SDimitry Andric if (ByRefAS == AMDGPUAS::CONSTANT_ADDRESS) { 554fe6060f1SDimitry Andric lowerParameterPtr(VRegs[i][0], B, ArgOffset); 555e8d8bef9SDimitry Andric } else { 556e8d8bef9SDimitry Andric const LLT ConstPtrTy = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64); 557e8d8bef9SDimitry Andric Register PtrReg = MRI.createGenericVirtualRegister(ConstPtrTy); 558fe6060f1SDimitry Andric lowerParameterPtr(PtrReg, B, ArgOffset); 559e8d8bef9SDimitry Andric 560e8d8bef9SDimitry Andric B.buildAddrSpaceCast(VRegs[i][0], PtrReg); 561e8d8bef9SDimitry Andric } 562e8d8bef9SDimitry Andric } else { 563fe6060f1SDimitry Andric ArgInfo OrigArg(VRegs[i], Arg, i); 564fe6060f1SDimitry Andric const unsigned OrigArgIdx = i + AttributeList::FirstArgIndex; 565fe6060f1SDimitry Andric setArgFlags(OrigArg, OrigArgIdx, DL, F); 566fe6060f1SDimitry Andric lowerParameter(B, OrigArg, ArgOffset, Alignment); 567e8d8bef9SDimitry Andric } 568e8d8bef9SDimitry Andric 5690b57cec5SDimitry Andric ++i; 5700b57cec5SDimitry Andric } 5710b57cec5SDimitry Andric 5728bcb0991SDimitry Andric TLI.allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info); 5738bcb0991SDimitry Andric TLI.allocateSystemSGPRs(CCInfo, MF, *Info, F.getCallingConv(), false); 5740b57cec5SDimitry Andric return true; 5750b57cec5SDimitry Andric } 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric bool AMDGPUCallLowering::lowerFormalArguments( 578e8d8bef9SDimitry Andric MachineIRBuilder &B, const Function &F, ArrayRef<ArrayRef<Register>> VRegs, 579e8d8bef9SDimitry Andric FunctionLoweringInfo &FLI) const { 5808bcb0991SDimitry Andric CallingConv::ID CC = F.getCallingConv(); 5818bcb0991SDimitry Andric 5820b57cec5SDimitry Andric // The infrastructure for normal calling convention lowering is essentially 5830b57cec5SDimitry Andric // useless for kernels. We want to avoid any kind of legalization or argument 5840b57cec5SDimitry Andric // splitting. 5858bcb0991SDimitry Andric if (CC == CallingConv::AMDGPU_KERNEL) 5868bcb0991SDimitry Andric return lowerFormalArgumentsKernel(B, F, VRegs); 5870b57cec5SDimitry Andric 588e8d8bef9SDimitry Andric const bool IsGraphics = AMDGPU::isGraphics(CC); 5898bcb0991SDimitry Andric const bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CC); 5900b57cec5SDimitry Andric 5918bcb0991SDimitry Andric MachineFunction &MF = B.getMF(); 5928bcb0991SDimitry Andric MachineBasicBlock &MBB = B.getMBB(); 5930b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 5940b57cec5SDimitry Andric SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 5958bcb0991SDimitry Andric const GCNSubtarget &Subtarget = MF.getSubtarget<GCNSubtarget>(); 5968bcb0991SDimitry Andric const SIRegisterInfo *TRI = Subtarget.getRegisterInfo(); 5970b57cec5SDimitry Andric const DataLayout &DL = F.getParent()->getDataLayout(); 5980b57cec5SDimitry Andric 599fe6060f1SDimitry Andric Info->allocateModuleLDSGlobal(F.getParent()); 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 6028bcb0991SDimitry Andric CCState CCInfo(CC, F.isVarArg(), MF, ArgLocs, F.getContext()); 6038bcb0991SDimitry Andric 6048bcb0991SDimitry Andric if (!IsEntryFunc) { 6058bcb0991SDimitry Andric Register ReturnAddrReg = TRI->getReturnAddressReg(MF); 6068bcb0991SDimitry Andric Register LiveInReturn = MF.addLiveIn(ReturnAddrReg, 6078bcb0991SDimitry Andric &AMDGPU::SGPR_64RegClass); 6088bcb0991SDimitry Andric MBB.addLiveIn(ReturnAddrReg); 6098bcb0991SDimitry Andric B.buildCopy(LiveInReturn, ReturnAddrReg); 6108bcb0991SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric if (Info->hasImplicitBufferPtr()) { 6138bcb0991SDimitry Andric Register ImplicitBufferPtrReg = Info->addImplicitBufferPtr(*TRI); 6140b57cec5SDimitry Andric MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass); 6150b57cec5SDimitry Andric CCInfo.AllocateReg(ImplicitBufferPtrReg); 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric 6188bcb0991SDimitry Andric SmallVector<ArgInfo, 32> SplitArgs; 6198bcb0991SDimitry Andric unsigned Idx = 0; 6200b57cec5SDimitry Andric unsigned PSInputNum = 0; 6210b57cec5SDimitry Andric 622e8d8bef9SDimitry Andric // Insert the hidden sret parameter if the return value won't fit in the 623e8d8bef9SDimitry Andric // return registers. 624e8d8bef9SDimitry Andric if (!FLI.CanLowerReturn) 625e8d8bef9SDimitry Andric insertSRetIncomingArgument(F, SplitArgs, FLI.DemoteRegister, MRI, DL); 626e8d8bef9SDimitry Andric 6278bcb0991SDimitry Andric for (auto &Arg : F.args()) { 6288bcb0991SDimitry Andric if (DL.getTypeStoreSize(Arg.getType()) == 0) 6290b57cec5SDimitry Andric continue; 6300b57cec5SDimitry Andric 6318bcb0991SDimitry Andric const bool InReg = Arg.hasAttribute(Attribute::InReg); 6328bcb0991SDimitry Andric 6338bcb0991SDimitry Andric // SGPR arguments to functions not implemented. 634e8d8bef9SDimitry Andric if (!IsGraphics && InReg) 6358bcb0991SDimitry Andric return false; 6368bcb0991SDimitry Andric 6378bcb0991SDimitry Andric if (Arg.hasAttribute(Attribute::SwiftSelf) || 6388bcb0991SDimitry Andric Arg.hasAttribute(Attribute::SwiftError) || 6398bcb0991SDimitry Andric Arg.hasAttribute(Attribute::Nest)) 6408bcb0991SDimitry Andric return false; 6418bcb0991SDimitry Andric 6428bcb0991SDimitry Andric if (CC == CallingConv::AMDGPU_PS && !InReg && PSInputNum <= 15) { 6438bcb0991SDimitry Andric const bool ArgUsed = !Arg.use_empty(); 6448bcb0991SDimitry Andric bool SkipArg = !ArgUsed && !Info->isPSInputAllocated(PSInputNum); 6458bcb0991SDimitry Andric 6468bcb0991SDimitry Andric if (!SkipArg) { 6470b57cec5SDimitry Andric Info->markPSInputAllocated(PSInputNum); 6488bcb0991SDimitry Andric if (ArgUsed) 6490b57cec5SDimitry Andric Info->markPSInputEnabled(PSInputNum); 6508bcb0991SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric ++PSInputNum; 6530b57cec5SDimitry Andric 6548bcb0991SDimitry Andric if (SkipArg) { 655*0eae32dcSDimitry Andric for (Register R : VRegs[Idx]) 656*0eae32dcSDimitry Andric B.buildUndef(R); 6570b57cec5SDimitry Andric 6588bcb0991SDimitry Andric ++Idx; 6590b57cec5SDimitry Andric continue; 6608bcb0991SDimitry Andric } 6610b57cec5SDimitry Andric } 6620b57cec5SDimitry Andric 663fe6060f1SDimitry Andric ArgInfo OrigArg(VRegs[Idx], Arg, Idx); 6645ffd83dbSDimitry Andric const unsigned OrigArgIdx = Idx + AttributeList::FirstArgIndex; 6655ffd83dbSDimitry Andric setArgFlags(OrigArg, OrigArgIdx, DL, F); 6668bcb0991SDimitry Andric 667fe6060f1SDimitry Andric splitToValueTypes(OrigArg, SplitArgs, DL, CC); 6688bcb0991SDimitry Andric ++Idx; 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric 6718bcb0991SDimitry Andric // At least one interpolation mode must be enabled or else the GPU will 6728bcb0991SDimitry Andric // hang. 6738bcb0991SDimitry Andric // 6748bcb0991SDimitry Andric // Check PSInputAddr instead of PSInputEnable. The idea is that if the user 6758bcb0991SDimitry Andric // set PSInputAddr, the user wants to enable some bits after the compilation 6768bcb0991SDimitry Andric // based on run-time states. Since we can't know what the final PSInputEna 6778bcb0991SDimitry Andric // will look like, so we shouldn't do anything here and the user should take 6788bcb0991SDimitry Andric // responsibility for the correct programming. 6798bcb0991SDimitry Andric // 6808bcb0991SDimitry Andric // Otherwise, the following restrictions apply: 6818bcb0991SDimitry Andric // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. 6828bcb0991SDimitry Andric // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be 6838bcb0991SDimitry Andric // enabled too. 6848bcb0991SDimitry Andric if (CC == CallingConv::AMDGPU_PS) { 6858bcb0991SDimitry Andric if ((Info->getPSInputAddr() & 0x7F) == 0 || 6868bcb0991SDimitry Andric ((Info->getPSInputAddr() & 0xF) == 0 && 6878bcb0991SDimitry Andric Info->isPSInputAllocated(11))) { 6888bcb0991SDimitry Andric CCInfo.AllocateReg(AMDGPU::VGPR0); 6898bcb0991SDimitry Andric CCInfo.AllocateReg(AMDGPU::VGPR1); 6908bcb0991SDimitry Andric Info->markPSInputAllocated(0); 6918bcb0991SDimitry Andric Info->markPSInputEnabled(0); 6928bcb0991SDimitry Andric } 6938bcb0991SDimitry Andric 6948bcb0991SDimitry Andric if (Subtarget.isAmdPalOS()) { 6958bcb0991SDimitry Andric // For isAmdPalOS, the user does not enable some bits after compilation 6968bcb0991SDimitry Andric // based on run-time states; the register values being generated here are 6978bcb0991SDimitry Andric // the final ones set in hardware. Therefore we need to apply the 6988bcb0991SDimitry Andric // workaround to PSInputAddr and PSInputEnable together. (The case where 6998bcb0991SDimitry Andric // a bit is set in PSInputAddr but not PSInputEnable is where the frontend 7008bcb0991SDimitry Andric // set up an input arg for a particular interpolation mode, but nothing 7018bcb0991SDimitry Andric // uses that input arg. Really we should have an earlier pass that removes 7028bcb0991SDimitry Andric // such an arg.) 7038bcb0991SDimitry Andric unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable(); 7048bcb0991SDimitry Andric if ((PsInputBits & 0x7F) == 0 || 7058bcb0991SDimitry Andric ((PsInputBits & 0xF) == 0 && 7068bcb0991SDimitry Andric (PsInputBits >> 11 & 1))) 7078bcb0991SDimitry Andric Info->markPSInputEnabled( 7088bcb0991SDimitry Andric countTrailingZeros(Info->getPSInputAddr(), ZB_Undefined)); 7098bcb0991SDimitry Andric } 7108bcb0991SDimitry Andric } 7118bcb0991SDimitry Andric 7128bcb0991SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 7138bcb0991SDimitry Andric CCAssignFn *AssignFn = TLI.CCAssignFnForCall(CC, F.isVarArg()); 7148bcb0991SDimitry Andric 7158bcb0991SDimitry Andric if (!MBB.empty()) 7168bcb0991SDimitry Andric B.setInstr(*MBB.begin()); 7178bcb0991SDimitry Andric 718*0eae32dcSDimitry Andric if (!IsEntryFunc && !IsGraphics) { 7195ffd83dbSDimitry Andric // For the fixed ABI, pass workitem IDs in the last argument register. 7205ffd83dbSDimitry Andric TLI.allocateSpecialInputVGPRsFixed(CCInfo, MF, *TRI, *Info); 7215ffd83dbSDimitry Andric } 7225ffd83dbSDimitry Andric 723fe6060f1SDimitry Andric IncomingValueAssigner Assigner(AssignFn); 724fe6060f1SDimitry Andric if (!determineAssignments(Assigner, SplitArgs, CCInfo)) 7250b57cec5SDimitry Andric return false; 7268bcb0991SDimitry Andric 727fe6060f1SDimitry Andric FormalArgHandler Handler(B, MRI); 728fe6060f1SDimitry Andric if (!handleAssignments(Handler, SplitArgs, CCInfo, ArgLocs, B)) 729fe6060f1SDimitry Andric return false; 730fe6060f1SDimitry Andric 731fe6060f1SDimitry Andric uint64_t StackOffset = Assigner.StackOffset; 732fe6060f1SDimitry Andric 7338bcb0991SDimitry Andric // Start adding system SGPRs. 7348bcb0991SDimitry Andric if (IsEntryFunc) { 735e8d8bef9SDimitry Andric TLI.allocateSystemSGPRs(CCInfo, MF, *Info, CC, IsGraphics); 7368bcb0991SDimitry Andric } else { 737e8d8bef9SDimitry Andric if (!Subtarget.enableFlatScratch()) 7388bcb0991SDimitry Andric CCInfo.AllocateReg(Info->getScratchRSrcReg()); 7398bcb0991SDimitry Andric TLI.allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info); 7408bcb0991SDimitry Andric } 7418bcb0991SDimitry Andric 742fe6060f1SDimitry Andric // When we tail call, we need to check if the callee's arguments will fit on 743fe6060f1SDimitry Andric // the caller's stack. So, whenever we lower formal arguments, we should keep 744fe6060f1SDimitry Andric // track of this information, since we might lower a tail call in this 745fe6060f1SDimitry Andric // function later. 746fe6060f1SDimitry Andric Info->setBytesInStackArgArea(StackOffset); 747fe6060f1SDimitry Andric 7488bcb0991SDimitry Andric // Move back to the end of the basic block. 7498bcb0991SDimitry Andric B.setMBB(MBB); 7508bcb0991SDimitry Andric 7518bcb0991SDimitry Andric return true; 7520b57cec5SDimitry Andric } 753e8d8bef9SDimitry Andric 754e8d8bef9SDimitry Andric bool AMDGPUCallLowering::passSpecialInputs(MachineIRBuilder &MIRBuilder, 755e8d8bef9SDimitry Andric CCState &CCInfo, 756e8d8bef9SDimitry Andric SmallVectorImpl<std::pair<MCRegister, Register>> &ArgRegs, 757e8d8bef9SDimitry Andric CallLoweringInfo &Info) const { 758e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 759e8d8bef9SDimitry Andric 760349cc55cSDimitry Andric // If there's no call site, this doesn't correspond to a call from the IR and 761349cc55cSDimitry Andric // doesn't need implicit inputs. 762349cc55cSDimitry Andric if (!Info.CB) 763349cc55cSDimitry Andric return true; 764349cc55cSDimitry Andric 765e8d8bef9SDimitry Andric const AMDGPUFunctionArgInfo *CalleeArgInfo 766e8d8bef9SDimitry Andric = &AMDGPUArgumentUsageInfo::FixedABIFunctionInfo; 767e8d8bef9SDimitry Andric 768e8d8bef9SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 769e8d8bef9SDimitry Andric const AMDGPUFunctionArgInfo &CallerArgInfo = MFI->getArgInfo(); 770e8d8bef9SDimitry Andric 771e8d8bef9SDimitry Andric 772e8d8bef9SDimitry Andric // TODO: Unify with private memory register handling. This is complicated by 773e8d8bef9SDimitry Andric // the fact that at least in kernels, the input argument is not necessarily 774e8d8bef9SDimitry Andric // in the same location as the input. 775e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::PreloadedValue InputRegs[] = { 776e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::DISPATCH_PTR, 777e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::QUEUE_PTR, 778e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR, 779e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::DISPATCH_ID, 780e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::WORKGROUP_ID_X, 781e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::WORKGROUP_ID_Y, 782e8d8bef9SDimitry Andric AMDGPUFunctionArgInfo::WORKGROUP_ID_Z 783e8d8bef9SDimitry Andric }; 784e8d8bef9SDimitry Andric 785349cc55cSDimitry Andric static constexpr StringLiteral ImplicitAttrNames[] = { 786349cc55cSDimitry Andric "amdgpu-no-dispatch-ptr", 787349cc55cSDimitry Andric "amdgpu-no-queue-ptr", 788349cc55cSDimitry Andric "amdgpu-no-implicitarg-ptr", 789349cc55cSDimitry Andric "amdgpu-no-dispatch-id", 790349cc55cSDimitry Andric "amdgpu-no-workgroup-id-x", 791349cc55cSDimitry Andric "amdgpu-no-workgroup-id-y", 792349cc55cSDimitry Andric "amdgpu-no-workgroup-id-z" 793349cc55cSDimitry Andric }; 794349cc55cSDimitry Andric 795e8d8bef9SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 796e8d8bef9SDimitry Andric 797e8d8bef9SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 798e8d8bef9SDimitry Andric const AMDGPULegalizerInfo *LI 799e8d8bef9SDimitry Andric = static_cast<const AMDGPULegalizerInfo*>(ST.getLegalizerInfo()); 800e8d8bef9SDimitry Andric 801349cc55cSDimitry Andric unsigned I = 0; 802e8d8bef9SDimitry Andric for (auto InputID : InputRegs) { 803e8d8bef9SDimitry Andric const ArgDescriptor *OutgoingArg; 804e8d8bef9SDimitry Andric const TargetRegisterClass *ArgRC; 805e8d8bef9SDimitry Andric LLT ArgTy; 806e8d8bef9SDimitry Andric 807349cc55cSDimitry Andric // If the callee does not use the attribute value, skip copying the value. 808349cc55cSDimitry Andric if (Info.CB->hasFnAttr(ImplicitAttrNames[I++])) 809349cc55cSDimitry Andric continue; 810349cc55cSDimitry Andric 811e8d8bef9SDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) = 812e8d8bef9SDimitry Andric CalleeArgInfo->getPreloadedValue(InputID); 813e8d8bef9SDimitry Andric if (!OutgoingArg) 814e8d8bef9SDimitry Andric continue; 815e8d8bef9SDimitry Andric 816e8d8bef9SDimitry Andric const ArgDescriptor *IncomingArg; 817e8d8bef9SDimitry Andric const TargetRegisterClass *IncomingArgRC; 818e8d8bef9SDimitry Andric std::tie(IncomingArg, IncomingArgRC, ArgTy) = 819e8d8bef9SDimitry Andric CallerArgInfo.getPreloadedValue(InputID); 820e8d8bef9SDimitry Andric assert(IncomingArgRC == ArgRC); 821e8d8bef9SDimitry Andric 822e8d8bef9SDimitry Andric Register InputReg = MRI.createGenericVirtualRegister(ArgTy); 823e8d8bef9SDimitry Andric 824e8d8bef9SDimitry Andric if (IncomingArg) { 825e8d8bef9SDimitry Andric LI->loadInputValue(InputReg, MIRBuilder, IncomingArg, ArgRC, ArgTy); 826*0eae32dcSDimitry Andric } else if (InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR) { 827e8d8bef9SDimitry Andric LI->getImplicitArgPtr(InputReg, MRI, MIRBuilder); 828*0eae32dcSDimitry Andric } else { 829*0eae32dcSDimitry Andric // We may have proven the input wasn't needed, although the ABI is 830*0eae32dcSDimitry Andric // requiring it. We just need to allocate the register appropriately. 831*0eae32dcSDimitry Andric MIRBuilder.buildUndef(InputReg); 832e8d8bef9SDimitry Andric } 833e8d8bef9SDimitry Andric 834e8d8bef9SDimitry Andric if (OutgoingArg->isRegister()) { 835e8d8bef9SDimitry Andric ArgRegs.emplace_back(OutgoingArg->getRegister(), InputReg); 836e8d8bef9SDimitry Andric if (!CCInfo.AllocateReg(OutgoingArg->getRegister())) 837e8d8bef9SDimitry Andric report_fatal_error("failed to allocate implicit input argument"); 838e8d8bef9SDimitry Andric } else { 839e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Unhandled stack passed implicit input argument\n"); 840e8d8bef9SDimitry Andric return false; 841e8d8bef9SDimitry Andric } 842e8d8bef9SDimitry Andric } 843e8d8bef9SDimitry Andric 844e8d8bef9SDimitry Andric // Pack workitem IDs into a single register or pass it as is if already 845e8d8bef9SDimitry Andric // packed. 846e8d8bef9SDimitry Andric const ArgDescriptor *OutgoingArg; 847e8d8bef9SDimitry Andric const TargetRegisterClass *ArgRC; 848e8d8bef9SDimitry Andric LLT ArgTy; 849e8d8bef9SDimitry Andric 850e8d8bef9SDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) = 851e8d8bef9SDimitry Andric CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X); 852e8d8bef9SDimitry Andric if (!OutgoingArg) 853e8d8bef9SDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) = 854e8d8bef9SDimitry Andric CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y); 855e8d8bef9SDimitry Andric if (!OutgoingArg) 856e8d8bef9SDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) = 857e8d8bef9SDimitry Andric CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z); 858e8d8bef9SDimitry Andric if (!OutgoingArg) 859e8d8bef9SDimitry Andric return false; 860e8d8bef9SDimitry Andric 861e8d8bef9SDimitry Andric auto WorkitemIDX = 862e8d8bef9SDimitry Andric CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X); 863e8d8bef9SDimitry Andric auto WorkitemIDY = 864e8d8bef9SDimitry Andric CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y); 865e8d8bef9SDimitry Andric auto WorkitemIDZ = 866e8d8bef9SDimitry Andric CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z); 867e8d8bef9SDimitry Andric 868e8d8bef9SDimitry Andric const ArgDescriptor *IncomingArgX = std::get<0>(WorkitemIDX); 869e8d8bef9SDimitry Andric const ArgDescriptor *IncomingArgY = std::get<0>(WorkitemIDY); 870e8d8bef9SDimitry Andric const ArgDescriptor *IncomingArgZ = std::get<0>(WorkitemIDZ); 871e8d8bef9SDimitry Andric const LLT S32 = LLT::scalar(32); 872e8d8bef9SDimitry Andric 873349cc55cSDimitry Andric const bool NeedWorkItemIDX = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-x"); 874349cc55cSDimitry Andric const bool NeedWorkItemIDY = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-y"); 875349cc55cSDimitry Andric const bool NeedWorkItemIDZ = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-z"); 876349cc55cSDimitry Andric 877e8d8bef9SDimitry Andric // If incoming ids are not packed we need to pack them. 878e8d8bef9SDimitry Andric // FIXME: Should consider known workgroup size to eliminate known 0 cases. 879e8d8bef9SDimitry Andric Register InputReg; 880349cc55cSDimitry Andric if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo->WorkItemIDX && 881349cc55cSDimitry Andric NeedWorkItemIDX) { 882e8d8bef9SDimitry Andric InputReg = MRI.createGenericVirtualRegister(S32); 883e8d8bef9SDimitry Andric LI->loadInputValue(InputReg, MIRBuilder, IncomingArgX, 884e8d8bef9SDimitry Andric std::get<1>(WorkitemIDX), std::get<2>(WorkitemIDX)); 885e8d8bef9SDimitry Andric } 886e8d8bef9SDimitry Andric 887349cc55cSDimitry Andric if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo->WorkItemIDY && 888349cc55cSDimitry Andric NeedWorkItemIDY) { 889e8d8bef9SDimitry Andric Register Y = MRI.createGenericVirtualRegister(S32); 890e8d8bef9SDimitry Andric LI->loadInputValue(Y, MIRBuilder, IncomingArgY, std::get<1>(WorkitemIDY), 891e8d8bef9SDimitry Andric std::get<2>(WorkitemIDY)); 892e8d8bef9SDimitry Andric 893e8d8bef9SDimitry Andric Y = MIRBuilder.buildShl(S32, Y, MIRBuilder.buildConstant(S32, 10)).getReg(0); 894e8d8bef9SDimitry Andric InputReg = InputReg ? MIRBuilder.buildOr(S32, InputReg, Y).getReg(0) : Y; 895e8d8bef9SDimitry Andric } 896e8d8bef9SDimitry Andric 897349cc55cSDimitry Andric if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo->WorkItemIDZ && 898349cc55cSDimitry Andric NeedWorkItemIDZ) { 899e8d8bef9SDimitry Andric Register Z = MRI.createGenericVirtualRegister(S32); 900e8d8bef9SDimitry Andric LI->loadInputValue(Z, MIRBuilder, IncomingArgZ, std::get<1>(WorkitemIDZ), 901e8d8bef9SDimitry Andric std::get<2>(WorkitemIDZ)); 902e8d8bef9SDimitry Andric 903e8d8bef9SDimitry Andric Z = MIRBuilder.buildShl(S32, Z, MIRBuilder.buildConstant(S32, 20)).getReg(0); 904e8d8bef9SDimitry Andric InputReg = InputReg ? MIRBuilder.buildOr(S32, InputReg, Z).getReg(0) : Z; 905e8d8bef9SDimitry Andric } 906e8d8bef9SDimitry Andric 907349cc55cSDimitry Andric if (!InputReg && (NeedWorkItemIDX || NeedWorkItemIDY || NeedWorkItemIDZ)) { 908e8d8bef9SDimitry Andric InputReg = MRI.createGenericVirtualRegister(S32); 909e8d8bef9SDimitry Andric 910e8d8bef9SDimitry Andric // Workitem ids are already packed, any of present incoming arguments will 911e8d8bef9SDimitry Andric // carry all required fields. 912e8d8bef9SDimitry Andric ArgDescriptor IncomingArg = ArgDescriptor::createArg( 913e8d8bef9SDimitry Andric IncomingArgX ? *IncomingArgX : 914e8d8bef9SDimitry Andric IncomingArgY ? *IncomingArgY : *IncomingArgZ, ~0u); 915e8d8bef9SDimitry Andric LI->loadInputValue(InputReg, MIRBuilder, &IncomingArg, 916e8d8bef9SDimitry Andric &AMDGPU::VGPR_32RegClass, S32); 917e8d8bef9SDimitry Andric } 918e8d8bef9SDimitry Andric 919e8d8bef9SDimitry Andric if (OutgoingArg->isRegister()) { 920349cc55cSDimitry Andric if (InputReg) 921e8d8bef9SDimitry Andric ArgRegs.emplace_back(OutgoingArg->getRegister(), InputReg); 922349cc55cSDimitry Andric 923e8d8bef9SDimitry Andric if (!CCInfo.AllocateReg(OutgoingArg->getRegister())) 924e8d8bef9SDimitry Andric report_fatal_error("failed to allocate implicit input argument"); 925e8d8bef9SDimitry Andric } else { 926e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Unhandled stack passed implicit input argument\n"); 927e8d8bef9SDimitry Andric return false; 928e8d8bef9SDimitry Andric } 929e8d8bef9SDimitry Andric 930e8d8bef9SDimitry Andric return true; 931e8d8bef9SDimitry Andric } 932e8d8bef9SDimitry Andric 933e8d8bef9SDimitry Andric /// Returns a pair containing the fixed CCAssignFn and the vararg CCAssignFn for 934e8d8bef9SDimitry Andric /// CC. 935e8d8bef9SDimitry Andric static std::pair<CCAssignFn *, CCAssignFn *> 936e8d8bef9SDimitry Andric getAssignFnsForCC(CallingConv::ID CC, const SITargetLowering &TLI) { 937e8d8bef9SDimitry Andric return {TLI.CCAssignFnForCall(CC, false), TLI.CCAssignFnForCall(CC, true)}; 938e8d8bef9SDimitry Andric } 939e8d8bef9SDimitry Andric 940e8d8bef9SDimitry Andric static unsigned getCallOpcode(const MachineFunction &CallerF, bool IsIndirect, 941e8d8bef9SDimitry Andric bool IsTailCall) { 942349cc55cSDimitry Andric assert(!(IsIndirect && IsTailCall) && "Indirect calls can't be tail calls, " 943349cc55cSDimitry Andric "because the address can be divergent"); 944349cc55cSDimitry Andric return IsTailCall ? AMDGPU::SI_TCRETURN : AMDGPU::G_SI_CALL; 945e8d8bef9SDimitry Andric } 946e8d8bef9SDimitry Andric 947e8d8bef9SDimitry Andric // Add operands to call instruction to track the callee. 948e8d8bef9SDimitry Andric static bool addCallTargetOperands(MachineInstrBuilder &CallInst, 949e8d8bef9SDimitry Andric MachineIRBuilder &MIRBuilder, 950e8d8bef9SDimitry Andric AMDGPUCallLowering::CallLoweringInfo &Info) { 951e8d8bef9SDimitry Andric if (Info.Callee.isReg()) { 952e8d8bef9SDimitry Andric CallInst.addReg(Info.Callee.getReg()); 953e8d8bef9SDimitry Andric CallInst.addImm(0); 954e8d8bef9SDimitry Andric } else if (Info.Callee.isGlobal() && Info.Callee.getOffset() == 0) { 955e8d8bef9SDimitry Andric // The call lowering lightly assumed we can directly encode a call target in 956e8d8bef9SDimitry Andric // the instruction, which is not the case. Materialize the address here. 957e8d8bef9SDimitry Andric const GlobalValue *GV = Info.Callee.getGlobal(); 958e8d8bef9SDimitry Andric auto Ptr = MIRBuilder.buildGlobalValue( 959e8d8bef9SDimitry Andric LLT::pointer(GV->getAddressSpace(), 64), GV); 960e8d8bef9SDimitry Andric CallInst.addReg(Ptr.getReg(0)); 961e8d8bef9SDimitry Andric CallInst.add(Info.Callee); 962e8d8bef9SDimitry Andric } else 963e8d8bef9SDimitry Andric return false; 964e8d8bef9SDimitry Andric 965e8d8bef9SDimitry Andric return true; 966e8d8bef9SDimitry Andric } 967e8d8bef9SDimitry Andric 968fe6060f1SDimitry Andric bool AMDGPUCallLowering::doCallerAndCalleePassArgsTheSameWay( 969fe6060f1SDimitry Andric CallLoweringInfo &Info, MachineFunction &MF, 970fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &InArgs) const { 971fe6060f1SDimitry Andric const Function &CallerF = MF.getFunction(); 972fe6060f1SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv; 973fe6060f1SDimitry Andric CallingConv::ID CallerCC = CallerF.getCallingConv(); 974fe6060f1SDimitry Andric 975fe6060f1SDimitry Andric // If the calling conventions match, then everything must be the same. 976fe6060f1SDimitry Andric if (CalleeCC == CallerCC) 977fe6060f1SDimitry Andric return true; 978fe6060f1SDimitry Andric 979fe6060f1SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 980fe6060f1SDimitry Andric 981fe6060f1SDimitry Andric // Make sure that the caller and callee preserve all of the same registers. 982fe6060f1SDimitry Andric auto TRI = ST.getRegisterInfo(); 983fe6060f1SDimitry Andric 984fe6060f1SDimitry Andric const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 985fe6060f1SDimitry Andric const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 986fe6060f1SDimitry Andric if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 987fe6060f1SDimitry Andric return false; 988fe6060f1SDimitry Andric 989fe6060f1SDimitry Andric // Check if the caller and callee will handle arguments in the same way. 990fe6060f1SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 991fe6060f1SDimitry Andric CCAssignFn *CalleeAssignFnFixed; 992fe6060f1SDimitry Andric CCAssignFn *CalleeAssignFnVarArg; 993fe6060f1SDimitry Andric std::tie(CalleeAssignFnFixed, CalleeAssignFnVarArg) = 994fe6060f1SDimitry Andric getAssignFnsForCC(CalleeCC, TLI); 995fe6060f1SDimitry Andric 996fe6060f1SDimitry Andric CCAssignFn *CallerAssignFnFixed; 997fe6060f1SDimitry Andric CCAssignFn *CallerAssignFnVarArg; 998fe6060f1SDimitry Andric std::tie(CallerAssignFnFixed, CallerAssignFnVarArg) = 999fe6060f1SDimitry Andric getAssignFnsForCC(CallerCC, TLI); 1000fe6060f1SDimitry Andric 1001fe6060f1SDimitry Andric // FIXME: We are not accounting for potential differences in implicitly passed 1002fe6060f1SDimitry Andric // inputs, but only the fixed ABI is supported now anyway. 1003fe6060f1SDimitry Andric IncomingValueAssigner CalleeAssigner(CalleeAssignFnFixed, 1004fe6060f1SDimitry Andric CalleeAssignFnVarArg); 1005fe6060f1SDimitry Andric IncomingValueAssigner CallerAssigner(CallerAssignFnFixed, 1006fe6060f1SDimitry Andric CallerAssignFnVarArg); 1007fe6060f1SDimitry Andric return resultsCompatible(Info, MF, InArgs, CalleeAssigner, CallerAssigner); 1008fe6060f1SDimitry Andric } 1009fe6060f1SDimitry Andric 1010fe6060f1SDimitry Andric bool AMDGPUCallLowering::areCalleeOutgoingArgsTailCallable( 1011fe6060f1SDimitry Andric CallLoweringInfo &Info, MachineFunction &MF, 1012fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &OutArgs) const { 1013fe6060f1SDimitry Andric // If there are no outgoing arguments, then we are done. 1014fe6060f1SDimitry Andric if (OutArgs.empty()) 1015fe6060f1SDimitry Andric return true; 1016fe6060f1SDimitry Andric 1017fe6060f1SDimitry Andric const Function &CallerF = MF.getFunction(); 1018fe6060f1SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv; 1019fe6060f1SDimitry Andric CallingConv::ID CallerCC = CallerF.getCallingConv(); 1020fe6060f1SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 1021fe6060f1SDimitry Andric 1022fe6060f1SDimitry Andric CCAssignFn *AssignFnFixed; 1023fe6060f1SDimitry Andric CCAssignFn *AssignFnVarArg; 1024fe6060f1SDimitry Andric std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI); 1025fe6060f1SDimitry Andric 1026fe6060f1SDimitry Andric // We have outgoing arguments. Make sure that we can tail call with them. 1027fe6060f1SDimitry Andric SmallVector<CCValAssign, 16> OutLocs; 1028fe6060f1SDimitry Andric CCState OutInfo(CalleeCC, false, MF, OutLocs, CallerF.getContext()); 1029fe6060f1SDimitry Andric OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg); 1030fe6060f1SDimitry Andric 1031fe6060f1SDimitry Andric if (!determineAssignments(Assigner, OutArgs, OutInfo)) { 1032fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "... Could not analyze call operands.\n"); 1033fe6060f1SDimitry Andric return false; 1034fe6060f1SDimitry Andric } 1035fe6060f1SDimitry Andric 1036fe6060f1SDimitry Andric // Make sure that they can fit on the caller's stack. 1037fe6060f1SDimitry Andric const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); 1038fe6060f1SDimitry Andric if (OutInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea()) { 1039fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n"); 1040fe6060f1SDimitry Andric return false; 1041fe6060f1SDimitry Andric } 1042fe6060f1SDimitry Andric 1043fe6060f1SDimitry Andric // Verify that the parameters in callee-saved registers match. 1044fe6060f1SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 1045fe6060f1SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1046fe6060f1SDimitry Andric const uint32_t *CallerPreservedMask = TRI->getCallPreservedMask(MF, CallerCC); 1047fe6060f1SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1048fe6060f1SDimitry Andric return parametersInCSRMatch(MRI, CallerPreservedMask, OutLocs, OutArgs); 1049fe6060f1SDimitry Andric } 1050fe6060f1SDimitry Andric 1051fe6060f1SDimitry Andric /// Return true if the calling convention is one that we can guarantee TCO for. 1052fe6060f1SDimitry Andric static bool canGuaranteeTCO(CallingConv::ID CC) { 1053fe6060f1SDimitry Andric return CC == CallingConv::Fast; 1054fe6060f1SDimitry Andric } 1055fe6060f1SDimitry Andric 1056fe6060f1SDimitry Andric /// Return true if we might ever do TCO for calls with this calling convention. 1057fe6060f1SDimitry Andric static bool mayTailCallThisCC(CallingConv::ID CC) { 1058fe6060f1SDimitry Andric switch (CC) { 1059fe6060f1SDimitry Andric case CallingConv::C: 1060fe6060f1SDimitry Andric case CallingConv::AMDGPU_Gfx: 1061fe6060f1SDimitry Andric return true; 1062fe6060f1SDimitry Andric default: 1063fe6060f1SDimitry Andric return canGuaranteeTCO(CC); 1064fe6060f1SDimitry Andric } 1065fe6060f1SDimitry Andric } 1066fe6060f1SDimitry Andric 1067fe6060f1SDimitry Andric bool AMDGPUCallLowering::isEligibleForTailCallOptimization( 1068fe6060f1SDimitry Andric MachineIRBuilder &B, CallLoweringInfo &Info, 1069fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &InArgs, SmallVectorImpl<ArgInfo> &OutArgs) const { 1070fe6060f1SDimitry Andric // Must pass all target-independent checks in order to tail call optimize. 1071fe6060f1SDimitry Andric if (!Info.IsTailCall) 1072fe6060f1SDimitry Andric return false; 1073fe6060f1SDimitry Andric 1074349cc55cSDimitry Andric // Indirect calls can't be tail calls, because the address can be divergent. 1075349cc55cSDimitry Andric // TODO Check divergence info if the call really is divergent. 1076349cc55cSDimitry Andric if (Info.Callee.isReg()) 1077349cc55cSDimitry Andric return false; 1078349cc55cSDimitry Andric 1079fe6060f1SDimitry Andric MachineFunction &MF = B.getMF(); 1080fe6060f1SDimitry Andric const Function &CallerF = MF.getFunction(); 1081fe6060f1SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv; 1082fe6060f1SDimitry Andric CallingConv::ID CallerCC = CallerF.getCallingConv(); 1083fe6060f1SDimitry Andric 1084fe6060f1SDimitry Andric const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo(); 1085fe6060f1SDimitry Andric const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 1086fe6060f1SDimitry Andric // Kernels aren't callable, and don't have a live in return address so it 1087fe6060f1SDimitry Andric // doesn't make sense to do a tail call with entry functions. 1088fe6060f1SDimitry Andric if (!CallerPreserved) 1089fe6060f1SDimitry Andric return false; 1090fe6060f1SDimitry Andric 1091fe6060f1SDimitry Andric if (!mayTailCallThisCC(CalleeCC)) { 1092fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "... Calling convention cannot be tail called.\n"); 1093fe6060f1SDimitry Andric return false; 1094fe6060f1SDimitry Andric } 1095fe6060f1SDimitry Andric 1096fe6060f1SDimitry Andric if (any_of(CallerF.args(), [](const Argument &A) { 1097fe6060f1SDimitry Andric return A.hasByValAttr() || A.hasSwiftErrorAttr(); 1098fe6060f1SDimitry Andric })) { 1099fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "... Cannot tail call from callers with byval " 1100fe6060f1SDimitry Andric "or swifterror arguments\n"); 1101fe6060f1SDimitry Andric return false; 1102fe6060f1SDimitry Andric } 1103fe6060f1SDimitry Andric 1104fe6060f1SDimitry Andric // If we have -tailcallopt, then we're done. 1105fe6060f1SDimitry Andric if (MF.getTarget().Options.GuaranteedTailCallOpt) 1106fe6060f1SDimitry Andric return canGuaranteeTCO(CalleeCC) && CalleeCC == CallerF.getCallingConv(); 1107fe6060f1SDimitry Andric 1108fe6060f1SDimitry Andric // Verify that the incoming and outgoing arguments from the callee are 1109fe6060f1SDimitry Andric // safe to tail call. 1110fe6060f1SDimitry Andric if (!doCallerAndCalleePassArgsTheSameWay(Info, MF, InArgs)) { 1111fe6060f1SDimitry Andric LLVM_DEBUG( 1112fe6060f1SDimitry Andric dbgs() 1113fe6060f1SDimitry Andric << "... Caller and callee have incompatible calling conventions.\n"); 1114fe6060f1SDimitry Andric return false; 1115fe6060f1SDimitry Andric } 1116fe6060f1SDimitry Andric 1117fe6060f1SDimitry Andric if (!areCalleeOutgoingArgsTailCallable(Info, MF, OutArgs)) 1118fe6060f1SDimitry Andric return false; 1119fe6060f1SDimitry Andric 1120fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "... Call is eligible for tail call optimization.\n"); 1121fe6060f1SDimitry Andric return true; 1122fe6060f1SDimitry Andric } 1123fe6060f1SDimitry Andric 1124fe6060f1SDimitry Andric // Insert outgoing implicit arguments for a call, by inserting copies to the 1125fe6060f1SDimitry Andric // implicit argument registers and adding the necessary implicit uses to the 1126fe6060f1SDimitry Andric // call instruction. 1127fe6060f1SDimitry Andric void AMDGPUCallLowering::handleImplicitCallArguments( 1128fe6060f1SDimitry Andric MachineIRBuilder &MIRBuilder, MachineInstrBuilder &CallInst, 1129fe6060f1SDimitry Andric const GCNSubtarget &ST, const SIMachineFunctionInfo &FuncInfo, 1130fe6060f1SDimitry Andric ArrayRef<std::pair<MCRegister, Register>> ImplicitArgRegs) const { 1131fe6060f1SDimitry Andric if (!ST.enableFlatScratch()) { 1132fe6060f1SDimitry Andric // Insert copies for the SRD. In the HSA case, this should be an identity 1133fe6060f1SDimitry Andric // copy. 1134fe6060f1SDimitry Andric auto ScratchRSrcReg = MIRBuilder.buildCopy(LLT::fixed_vector(4, 32), 1135fe6060f1SDimitry Andric FuncInfo.getScratchRSrcReg()); 1136fe6060f1SDimitry Andric MIRBuilder.buildCopy(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, ScratchRSrcReg); 1137fe6060f1SDimitry Andric CallInst.addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Implicit); 1138fe6060f1SDimitry Andric } 1139fe6060f1SDimitry Andric 1140fe6060f1SDimitry Andric for (std::pair<MCRegister, Register> ArgReg : ImplicitArgRegs) { 1141fe6060f1SDimitry Andric MIRBuilder.buildCopy((Register)ArgReg.first, ArgReg.second); 1142fe6060f1SDimitry Andric CallInst.addReg(ArgReg.first, RegState::Implicit); 1143fe6060f1SDimitry Andric } 1144fe6060f1SDimitry Andric } 1145fe6060f1SDimitry Andric 1146fe6060f1SDimitry Andric bool AMDGPUCallLowering::lowerTailCall( 1147fe6060f1SDimitry Andric MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info, 1148fe6060f1SDimitry Andric SmallVectorImpl<ArgInfo> &OutArgs) const { 1149fe6060f1SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 1150fe6060f1SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 1151fe6060f1SDimitry Andric SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); 1152fe6060f1SDimitry Andric const Function &F = MF.getFunction(); 1153fe6060f1SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1154fe6060f1SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 1155fe6060f1SDimitry Andric 1156fe6060f1SDimitry Andric // True when we're tail calling, but without -tailcallopt. 1157fe6060f1SDimitry Andric bool IsSibCall = !MF.getTarget().Options.GuaranteedTailCallOpt; 1158fe6060f1SDimitry Andric 1159fe6060f1SDimitry Andric // Find out which ABI gets to decide where things go. 1160fe6060f1SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv; 1161fe6060f1SDimitry Andric CCAssignFn *AssignFnFixed; 1162fe6060f1SDimitry Andric CCAssignFn *AssignFnVarArg; 1163fe6060f1SDimitry Andric std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI); 1164fe6060f1SDimitry Andric 1165fe6060f1SDimitry Andric MachineInstrBuilder CallSeqStart; 1166fe6060f1SDimitry Andric if (!IsSibCall) 1167fe6060f1SDimitry Andric CallSeqStart = MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKUP); 1168fe6060f1SDimitry Andric 1169fe6060f1SDimitry Andric unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), true); 1170fe6060f1SDimitry Andric auto MIB = MIRBuilder.buildInstrNoInsert(Opc); 1171fe6060f1SDimitry Andric if (!addCallTargetOperands(MIB, MIRBuilder, Info)) 1172fe6060f1SDimitry Andric return false; 1173fe6060f1SDimitry Andric 1174fe6060f1SDimitry Andric // Byte offset for the tail call. When we are sibcalling, this will always 1175fe6060f1SDimitry Andric // be 0. 1176fe6060f1SDimitry Andric MIB.addImm(0); 1177fe6060f1SDimitry Andric 1178fe6060f1SDimitry Andric // Tell the call which registers are clobbered. 1179fe6060f1SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1180fe6060f1SDimitry Andric const uint32_t *Mask = TRI->getCallPreservedMask(MF, CalleeCC); 1181fe6060f1SDimitry Andric MIB.addRegMask(Mask); 1182fe6060f1SDimitry Andric 1183fe6060f1SDimitry Andric // FPDiff is the byte offset of the call's argument area from the callee's. 1184fe6060f1SDimitry Andric // Stores to callee stack arguments will be placed in FixedStackSlots offset 1185fe6060f1SDimitry Andric // by this amount for a tail call. In a sibling call it must be 0 because the 1186fe6060f1SDimitry Andric // caller will deallocate the entire stack and the callee still expects its 1187fe6060f1SDimitry Andric // arguments to begin at SP+0. 1188fe6060f1SDimitry Andric int FPDiff = 0; 1189fe6060f1SDimitry Andric 1190fe6060f1SDimitry Andric // This will be 0 for sibcalls, potentially nonzero for tail calls produced 1191fe6060f1SDimitry Andric // by -tailcallopt. For sibcalls, the memory operands for the call are 1192fe6060f1SDimitry Andric // already available in the caller's incoming argument space. 1193fe6060f1SDimitry Andric unsigned NumBytes = 0; 1194fe6060f1SDimitry Andric if (!IsSibCall) { 1195fe6060f1SDimitry Andric // We aren't sibcalling, so we need to compute FPDiff. We need to do this 1196fe6060f1SDimitry Andric // before handling assignments, because FPDiff must be known for memory 1197fe6060f1SDimitry Andric // arguments. 1198fe6060f1SDimitry Andric unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea(); 1199fe6060f1SDimitry Andric SmallVector<CCValAssign, 16> OutLocs; 1200fe6060f1SDimitry Andric CCState OutInfo(CalleeCC, false, MF, OutLocs, F.getContext()); 1201fe6060f1SDimitry Andric 1202fe6060f1SDimitry Andric // FIXME: Not accounting for callee implicit inputs 1203fe6060f1SDimitry Andric OutgoingValueAssigner CalleeAssigner(AssignFnFixed, AssignFnVarArg); 1204fe6060f1SDimitry Andric if (!determineAssignments(CalleeAssigner, OutArgs, OutInfo)) 1205fe6060f1SDimitry Andric return false; 1206fe6060f1SDimitry Andric 1207fe6060f1SDimitry Andric // The callee will pop the argument stack as a tail call. Thus, we must 1208fe6060f1SDimitry Andric // keep it 16-byte aligned. 1209fe6060f1SDimitry Andric NumBytes = alignTo(OutInfo.getNextStackOffset(), ST.getStackAlignment()); 1210fe6060f1SDimitry Andric 1211fe6060f1SDimitry Andric // FPDiff will be negative if this tail call requires more space than we 1212fe6060f1SDimitry Andric // would automatically have in our incoming argument space. Positive if we 1213fe6060f1SDimitry Andric // actually shrink the stack. 1214fe6060f1SDimitry Andric FPDiff = NumReusableBytes - NumBytes; 1215fe6060f1SDimitry Andric 1216fe6060f1SDimitry Andric // The stack pointer must be 16-byte aligned at all times it's used for a 1217fe6060f1SDimitry Andric // memory operation, which in practice means at *all* times and in 1218fe6060f1SDimitry Andric // particular across call boundaries. Therefore our own arguments started at 1219fe6060f1SDimitry Andric // a 16-byte aligned SP and the delta applied for the tail call should 1220fe6060f1SDimitry Andric // satisfy the same constraint. 1221fe6060f1SDimitry Andric assert(isAligned(ST.getStackAlignment(), FPDiff) && 1222fe6060f1SDimitry Andric "unaligned stack on tail call"); 1223fe6060f1SDimitry Andric } 1224fe6060f1SDimitry Andric 1225fe6060f1SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 1226fe6060f1SDimitry Andric CCState CCInfo(Info.CallConv, Info.IsVarArg, MF, ArgLocs, F.getContext()); 1227fe6060f1SDimitry Andric 1228fe6060f1SDimitry Andric // We could pass MIB and directly add the implicit uses to the call 1229fe6060f1SDimitry Andric // now. However, as an aesthetic choice, place implicit argument operands 1230fe6060f1SDimitry Andric // after the ordinary user argument registers. 1231fe6060f1SDimitry Andric SmallVector<std::pair<MCRegister, Register>, 12> ImplicitArgRegs; 1232fe6060f1SDimitry Andric 1233*0eae32dcSDimitry Andric if (Info.CallConv != CallingConv::AMDGPU_Gfx) { 1234fe6060f1SDimitry Andric // With a fixed ABI, allocate fixed registers before user arguments. 1235fe6060f1SDimitry Andric if (!passSpecialInputs(MIRBuilder, CCInfo, ImplicitArgRegs, Info)) 1236fe6060f1SDimitry Andric return false; 1237fe6060f1SDimitry Andric } 1238fe6060f1SDimitry Andric 1239fe6060f1SDimitry Andric OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg); 1240fe6060f1SDimitry Andric 1241fe6060f1SDimitry Andric if (!determineAssignments(Assigner, OutArgs, CCInfo)) 1242fe6060f1SDimitry Andric return false; 1243fe6060f1SDimitry Andric 1244fe6060f1SDimitry Andric // Do the actual argument marshalling. 1245fe6060f1SDimitry Andric AMDGPUOutgoingArgHandler Handler(MIRBuilder, MRI, MIB, true, FPDiff); 1246fe6060f1SDimitry Andric if (!handleAssignments(Handler, OutArgs, CCInfo, ArgLocs, MIRBuilder)) 1247fe6060f1SDimitry Andric return false; 1248fe6060f1SDimitry Andric 1249fe6060f1SDimitry Andric handleImplicitCallArguments(MIRBuilder, MIB, ST, *FuncInfo, ImplicitArgRegs); 1250fe6060f1SDimitry Andric 1251fe6060f1SDimitry Andric // If we have -tailcallopt, we need to adjust the stack. We'll do the call 1252fe6060f1SDimitry Andric // sequence start and end here. 1253fe6060f1SDimitry Andric if (!IsSibCall) { 1254fe6060f1SDimitry Andric MIB->getOperand(1).setImm(FPDiff); 1255fe6060f1SDimitry Andric CallSeqStart.addImm(NumBytes).addImm(0); 1256fe6060f1SDimitry Andric // End the call sequence *before* emitting the call. Normally, we would 1257fe6060f1SDimitry Andric // tidy the frame up after the call. However, here, we've laid out the 1258fe6060f1SDimitry Andric // parameters so that when SP is reset, they will be in the correct 1259fe6060f1SDimitry Andric // location. 1260fe6060f1SDimitry Andric MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKDOWN).addImm(NumBytes).addImm(0); 1261fe6060f1SDimitry Andric } 1262fe6060f1SDimitry Andric 1263fe6060f1SDimitry Andric // Now we can add the actual call instruction to the correct basic block. 1264fe6060f1SDimitry Andric MIRBuilder.insertInstr(MIB); 1265fe6060f1SDimitry Andric 1266fe6060f1SDimitry Andric // If Callee is a reg, since it is used by a target specific 1267fe6060f1SDimitry Andric // instruction, it must have a register class matching the 1268fe6060f1SDimitry Andric // constraint of that instruction. 1269fe6060f1SDimitry Andric 1270fe6060f1SDimitry Andric // FIXME: We should define regbankselectable call instructions to handle 1271fe6060f1SDimitry Andric // divergent call targets. 1272fe6060f1SDimitry Andric if (MIB->getOperand(0).isReg()) { 1273fe6060f1SDimitry Andric MIB->getOperand(0).setReg(constrainOperandRegClass( 1274fe6060f1SDimitry Andric MF, *TRI, MRI, *ST.getInstrInfo(), *ST.getRegBankInfo(), *MIB, 1275fe6060f1SDimitry Andric MIB->getDesc(), MIB->getOperand(0), 0)); 1276fe6060f1SDimitry Andric } 1277fe6060f1SDimitry Andric 1278fe6060f1SDimitry Andric MF.getFrameInfo().setHasTailCall(); 1279fe6060f1SDimitry Andric Info.LoweredTailCall = true; 1280fe6060f1SDimitry Andric return true; 1281fe6060f1SDimitry Andric } 1282fe6060f1SDimitry Andric 1283e8d8bef9SDimitry Andric bool AMDGPUCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, 1284e8d8bef9SDimitry Andric CallLoweringInfo &Info) const { 1285e8d8bef9SDimitry Andric if (Info.IsVarArg) { 1286e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Variadic functions not implemented\n"); 1287e8d8bef9SDimitry Andric return false; 1288e8d8bef9SDimitry Andric } 1289e8d8bef9SDimitry Andric 1290e8d8bef9SDimitry Andric MachineFunction &MF = MIRBuilder.getMF(); 1291e8d8bef9SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 1292e8d8bef9SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1293e8d8bef9SDimitry Andric 1294e8d8bef9SDimitry Andric const Function &F = MF.getFunction(); 1295e8d8bef9SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1296e8d8bef9SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>(); 1297e8d8bef9SDimitry Andric const DataLayout &DL = F.getParent()->getDataLayout(); 1298e8d8bef9SDimitry Andric 1299e8d8bef9SDimitry Andric SmallVector<ArgInfo, 8> OutArgs; 1300fe6060f1SDimitry Andric for (auto &OrigArg : Info.OrigArgs) 1301fe6060f1SDimitry Andric splitToValueTypes(OrigArg, OutArgs, DL, Info.CallConv); 1302e8d8bef9SDimitry Andric 1303fe6060f1SDimitry Andric SmallVector<ArgInfo, 8> InArgs; 1304fe6060f1SDimitry Andric if (Info.CanLowerReturn && !Info.OrigRet.Ty->isVoidTy()) 1305fe6060f1SDimitry Andric splitToValueTypes(Info.OrigRet, InArgs, DL, Info.CallConv); 1306e8d8bef9SDimitry Andric 1307e8d8bef9SDimitry Andric // If we can lower as a tail call, do that instead. 1308fe6060f1SDimitry Andric bool CanTailCallOpt = 1309fe6060f1SDimitry Andric isEligibleForTailCallOptimization(MIRBuilder, Info, InArgs, OutArgs); 1310e8d8bef9SDimitry Andric 1311e8d8bef9SDimitry Andric // We must emit a tail call if we have musttail. 1312e8d8bef9SDimitry Andric if (Info.IsMustTailCall && !CanTailCallOpt) { 1313e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Failed to lower musttail call as tail call\n"); 1314e8d8bef9SDimitry Andric return false; 1315e8d8bef9SDimitry Andric } 1316e8d8bef9SDimitry Andric 1317fe6060f1SDimitry Andric if (CanTailCallOpt) 1318fe6060f1SDimitry Andric return lowerTailCall(MIRBuilder, Info, OutArgs); 1319fe6060f1SDimitry Andric 1320e8d8bef9SDimitry Andric // Find out which ABI gets to decide where things go. 1321e8d8bef9SDimitry Andric CCAssignFn *AssignFnFixed; 1322e8d8bef9SDimitry Andric CCAssignFn *AssignFnVarArg; 1323e8d8bef9SDimitry Andric std::tie(AssignFnFixed, AssignFnVarArg) = 1324e8d8bef9SDimitry Andric getAssignFnsForCC(Info.CallConv, TLI); 1325e8d8bef9SDimitry Andric 1326e8d8bef9SDimitry Andric MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKUP) 1327e8d8bef9SDimitry Andric .addImm(0) 1328e8d8bef9SDimitry Andric .addImm(0); 1329e8d8bef9SDimitry Andric 1330e8d8bef9SDimitry Andric // Create a temporarily-floating call instruction so we can add the implicit 1331e8d8bef9SDimitry Andric // uses of arg registers. 1332e8d8bef9SDimitry Andric unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), false); 1333e8d8bef9SDimitry Andric 1334e8d8bef9SDimitry Andric auto MIB = MIRBuilder.buildInstrNoInsert(Opc); 1335e8d8bef9SDimitry Andric MIB.addDef(TRI->getReturnAddressReg(MF)); 1336e8d8bef9SDimitry Andric 1337e8d8bef9SDimitry Andric if (!addCallTargetOperands(MIB, MIRBuilder, Info)) 1338e8d8bef9SDimitry Andric return false; 1339e8d8bef9SDimitry Andric 1340e8d8bef9SDimitry Andric // Tell the call which registers are clobbered. 1341e8d8bef9SDimitry Andric const uint32_t *Mask = TRI->getCallPreservedMask(MF, Info.CallConv); 1342e8d8bef9SDimitry Andric MIB.addRegMask(Mask); 1343e8d8bef9SDimitry Andric 1344e8d8bef9SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs; 1345e8d8bef9SDimitry Andric CCState CCInfo(Info.CallConv, Info.IsVarArg, MF, ArgLocs, F.getContext()); 1346e8d8bef9SDimitry Andric 1347e8d8bef9SDimitry Andric // We could pass MIB and directly add the implicit uses to the call 1348e8d8bef9SDimitry Andric // now. However, as an aesthetic choice, place implicit argument operands 1349e8d8bef9SDimitry Andric // after the ordinary user argument registers. 1350e8d8bef9SDimitry Andric SmallVector<std::pair<MCRegister, Register>, 12> ImplicitArgRegs; 1351e8d8bef9SDimitry Andric 1352*0eae32dcSDimitry Andric if (Info.CallConv != CallingConv::AMDGPU_Gfx) { 1353e8d8bef9SDimitry Andric // With a fixed ABI, allocate fixed registers before user arguments. 1354e8d8bef9SDimitry Andric if (!passSpecialInputs(MIRBuilder, CCInfo, ImplicitArgRegs, Info)) 1355e8d8bef9SDimitry Andric return false; 1356e8d8bef9SDimitry Andric } 1357e8d8bef9SDimitry Andric 1358e8d8bef9SDimitry Andric // Do the actual argument marshalling. 1359e8d8bef9SDimitry Andric SmallVector<Register, 8> PhysRegs; 1360fe6060f1SDimitry Andric 1361fe6060f1SDimitry Andric OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg); 1362fe6060f1SDimitry Andric if (!determineAssignments(Assigner, OutArgs, CCInfo)) 1363fe6060f1SDimitry Andric return false; 1364fe6060f1SDimitry Andric 1365fe6060f1SDimitry Andric AMDGPUOutgoingArgHandler Handler(MIRBuilder, MRI, MIB, false); 1366fe6060f1SDimitry Andric if (!handleAssignments(Handler, OutArgs, CCInfo, ArgLocs, MIRBuilder)) 1367e8d8bef9SDimitry Andric return false; 1368e8d8bef9SDimitry Andric 1369e8d8bef9SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1370e8d8bef9SDimitry Andric 1371fe6060f1SDimitry Andric handleImplicitCallArguments(MIRBuilder, MIB, ST, *MFI, ImplicitArgRegs); 1372e8d8bef9SDimitry Andric 1373e8d8bef9SDimitry Andric // Get a count of how many bytes are to be pushed on the stack. 1374e8d8bef9SDimitry Andric unsigned NumBytes = CCInfo.getNextStackOffset(); 1375e8d8bef9SDimitry Andric 1376e8d8bef9SDimitry Andric // If Callee is a reg, since it is used by a target specific 1377e8d8bef9SDimitry Andric // instruction, it must have a register class matching the 1378e8d8bef9SDimitry Andric // constraint of that instruction. 1379e8d8bef9SDimitry Andric 1380e8d8bef9SDimitry Andric // FIXME: We should define regbankselectable call instructions to handle 1381e8d8bef9SDimitry Andric // divergent call targets. 1382e8d8bef9SDimitry Andric if (MIB->getOperand(1).isReg()) { 1383e8d8bef9SDimitry Andric MIB->getOperand(1).setReg(constrainOperandRegClass( 1384e8d8bef9SDimitry Andric MF, *TRI, MRI, *ST.getInstrInfo(), 1385e8d8bef9SDimitry Andric *ST.getRegBankInfo(), *MIB, MIB->getDesc(), MIB->getOperand(1), 1386e8d8bef9SDimitry Andric 1)); 1387e8d8bef9SDimitry Andric } 1388e8d8bef9SDimitry Andric 1389e8d8bef9SDimitry Andric // Now we can add the actual call instruction to the correct position. 1390e8d8bef9SDimitry Andric MIRBuilder.insertInstr(MIB); 1391e8d8bef9SDimitry Andric 1392e8d8bef9SDimitry Andric // Finally we can copy the returned value back into its virtual-register. In 1393e8d8bef9SDimitry Andric // symmetry with the arguments, the physical register must be an 1394e8d8bef9SDimitry Andric // implicit-define of the call instruction. 1395e8d8bef9SDimitry Andric if (Info.CanLowerReturn && !Info.OrigRet.Ty->isVoidTy()) { 1396e8d8bef9SDimitry Andric CCAssignFn *RetAssignFn = TLI.CCAssignFnForReturn(Info.CallConv, 1397e8d8bef9SDimitry Andric Info.IsVarArg); 1398fe6060f1SDimitry Andric IncomingValueAssigner Assigner(RetAssignFn); 1399fe6060f1SDimitry Andric CallReturnHandler Handler(MIRBuilder, MRI, MIB); 1400fe6060f1SDimitry Andric if (!determineAndHandleAssignments(Handler, Assigner, InArgs, MIRBuilder, 1401fe6060f1SDimitry Andric Info.CallConv, Info.IsVarArg)) 1402e8d8bef9SDimitry Andric return false; 1403e8d8bef9SDimitry Andric } 1404e8d8bef9SDimitry Andric 1405e8d8bef9SDimitry Andric uint64_t CalleePopBytes = NumBytes; 1406fe6060f1SDimitry Andric 1407fe6060f1SDimitry Andric MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKDOWN) 1408fe6060f1SDimitry Andric .addImm(0) 1409e8d8bef9SDimitry Andric .addImm(CalleePopBytes); 1410e8d8bef9SDimitry Andric 1411fe6060f1SDimitry Andric if (!Info.CanLowerReturn) { 1412fe6060f1SDimitry Andric insertSRetLoads(MIRBuilder, Info.OrigRet.Ty, Info.OrigRet.Regs, 1413fe6060f1SDimitry Andric Info.DemoteRegister, Info.DemoteStackIndex); 1414fe6060f1SDimitry Andric } 1415fe6060f1SDimitry Andric 1416e8d8bef9SDimitry Andric return true; 1417e8d8bef9SDimitry Andric } 1418