1b915e9e0SDimitry Andric //===-- llvm/lib/Target/AMDGPU/AMDGPUCallLowering.cpp - Call lowering -----===//
201095a5dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601095a5dSDimitry Andric //
701095a5dSDimitry Andric //===----------------------------------------------------------------------===//
801095a5dSDimitry Andric ///
901095a5dSDimitry Andric /// \file
1001095a5dSDimitry Andric /// This file implements the lowering of LLVM calls to machine code calls for
1101095a5dSDimitry Andric /// GlobalISel.
1201095a5dSDimitry Andric ///
1301095a5dSDimitry Andric //===----------------------------------------------------------------------===//
1401095a5dSDimitry Andric
1501095a5dSDimitry Andric #include "AMDGPUCallLowering.h"
1671d5a254SDimitry Andric #include "AMDGPU.h"
17b60736ecSDimitry Andric #include "AMDGPULegalizerInfo.h"
18cfca06d7SDimitry Andric #include "AMDGPUTargetMachine.h"
1971d5a254SDimitry Andric #include "SIMachineFunctionInfo.h"
207ab83427SDimitry Andric #include "SIRegisterInfo.h"
21e6d15924SDimitry Andric #include "llvm/CodeGen/Analysis.h"
22b60736ecSDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h"
2301095a5dSDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
24145449b1SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
25b60736ecSDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h"
26b60736ecSDimitry Andric
27b60736ecSDimitry Andric #define DEBUG_TYPE "amdgpu-call-lowering"
2801095a5dSDimitry Andric
2901095a5dSDimitry Andric using namespace llvm;
3001095a5dSDimitry Andric
31e6d15924SDimitry Andric namespace {
32e6d15924SDimitry Andric
33344a3780SDimitry Andric /// Wrapper around extendRegister to ensure we extend to a full 32-bit register.
extendRegisterMin32(CallLowering::ValueHandler & Handler,Register ValVReg,const CCValAssign & VA)34344a3780SDimitry Andric static Register extendRegisterMin32(CallLowering::ValueHandler &Handler,
35b1c73532SDimitry Andric Register ValVReg, const CCValAssign &VA) {
36b60736ecSDimitry Andric if (VA.getLocVT().getSizeInBits() < 32) {
37b60736ecSDimitry Andric // 16-bit types are reported as legal for 32-bit registers. We need to
38b60736ecSDimitry Andric // extend and do a 32-bit copy to avoid the verifier complaining about it.
39344a3780SDimitry Andric return Handler.MIRBuilder.buildAnyExt(LLT::scalar(32), ValVReg).getReg(0);
40b60736ecSDimitry Andric }
41b60736ecSDimitry Andric
42344a3780SDimitry Andric return Handler.extendRegister(ValVReg, VA);
43b60736ecSDimitry Andric }
44b60736ecSDimitry Andric
45344a3780SDimitry Andric struct AMDGPUOutgoingValueHandler : public CallLowering::OutgoingValueHandler {
AMDGPUOutgoingValueHandler__anona8a9f0490111::AMDGPUOutgoingValueHandler46b60736ecSDimitry Andric AMDGPUOutgoingValueHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI,
47344a3780SDimitry Andric MachineInstrBuilder MIB)
48344a3780SDimitry Andric : OutgoingValueHandler(B, MRI), MIB(MIB) {}
49e6d15924SDimitry Andric
50e6d15924SDimitry Andric MachineInstrBuilder MIB;
51e6d15924SDimitry Andric
getStackAddress__anona8a9f0490111::AMDGPUOutgoingValueHandler52e6d15924SDimitry Andric Register getStackAddress(uint64_t Size, int64_t Offset,
53344a3780SDimitry Andric MachinePointerInfo &MPO,
54344a3780SDimitry Andric ISD::ArgFlagsTy Flags) override {
55e6d15924SDimitry Andric llvm_unreachable("not implemented");
56e6d15924SDimitry Andric }
57e6d15924SDimitry Andric
assignValueToAddress__anona8a9f0490111::AMDGPUOutgoingValueHandler58344a3780SDimitry Andric void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
59b1c73532SDimitry Andric const MachinePointerInfo &MPO,
60b1c73532SDimitry Andric const CCValAssign &VA) override {
61e6d15924SDimitry Andric llvm_unreachable("not implemented");
62e6d15924SDimitry Andric }
63e6d15924SDimitry Andric
assignValueToReg__anona8a9f0490111::AMDGPUOutgoingValueHandler64e6d15924SDimitry Andric void assignValueToReg(Register ValVReg, Register PhysReg,
65b1c73532SDimitry Andric const CCValAssign &VA) override {
66344a3780SDimitry Andric Register ExtReg = extendRegisterMin32(*this, ValVReg, VA);
671d5ae102SDimitry Andric
68cfca06d7SDimitry Andric // If this is a scalar return, insert a readfirstlane just in case the value
69cfca06d7SDimitry Andric // ends up in a VGPR.
70cfca06d7SDimitry Andric // FIXME: Assert this is a shader return.
71cfca06d7SDimitry Andric const SIRegisterInfo *TRI
72cfca06d7SDimitry Andric = static_cast<const SIRegisterInfo *>(MRI.getTargetRegisterInfo());
73cfca06d7SDimitry Andric if (TRI->isSGPRReg(MRI, PhysReg)) {
74e3b55780SDimitry Andric LLT Ty = MRI.getType(ExtReg);
75e3b55780SDimitry Andric LLT S32 = LLT::scalar(32);
76e3b55780SDimitry Andric if (Ty != S32) {
77e3b55780SDimitry Andric // FIXME: We should probably support readfirstlane intrinsics with all
78e3b55780SDimitry Andric // legal 32-bit types.
79e3b55780SDimitry Andric assert(Ty.getSizeInBits() == 32);
80e3b55780SDimitry Andric if (Ty.isPointer())
81e3b55780SDimitry Andric ExtReg = MIRBuilder.buildPtrToInt(S32, ExtReg).getReg(0);
82e3b55780SDimitry Andric else
83e3b55780SDimitry Andric ExtReg = MIRBuilder.buildBitcast(S32, ExtReg).getReg(0);
84e3b55780SDimitry Andric }
85e3b55780SDimitry Andric
86b1c73532SDimitry Andric auto ToSGPR = MIRBuilder
87b1c73532SDimitry Andric .buildIntrinsic(Intrinsic::amdgcn_readfirstlane,
88b1c73532SDimitry Andric {MRI.getType(ExtReg)})
89cfca06d7SDimitry Andric .addReg(ExtReg);
90cfca06d7SDimitry Andric ExtReg = ToSGPR.getReg(0);
91cfca06d7SDimitry Andric }
92cfca06d7SDimitry Andric
931d5ae102SDimitry Andric MIRBuilder.buildCopy(PhysReg, ExtReg);
941d5ae102SDimitry Andric MIB.addUse(PhysReg, RegState::Implicit);
95e6d15924SDimitry Andric }
961d5ae102SDimitry Andric };
971d5ae102SDimitry Andric
98344a3780SDimitry Andric struct AMDGPUIncomingArgHandler : public CallLowering::IncomingValueHandler {
991d5ae102SDimitry Andric uint64_t StackUsed = 0;
1001d5ae102SDimitry Andric
AMDGPUIncomingArgHandler__anona8a9f0490111::AMDGPUIncomingArgHandler101344a3780SDimitry Andric AMDGPUIncomingArgHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI)
102344a3780SDimitry Andric : IncomingValueHandler(B, MRI) {}
1031d5ae102SDimitry Andric
getStackAddress__anona8a9f0490111::AMDGPUIncomingArgHandler1041d5ae102SDimitry Andric Register getStackAddress(uint64_t Size, int64_t Offset,
105344a3780SDimitry Andric MachinePointerInfo &MPO,
106344a3780SDimitry Andric ISD::ArgFlagsTy Flags) override {
1071d5ae102SDimitry Andric auto &MFI = MIRBuilder.getMF().getFrameInfo();
108344a3780SDimitry Andric
109344a3780SDimitry Andric // Byval is assumed to be writable memory, but other stack passed arguments
110344a3780SDimitry Andric // are not.
111344a3780SDimitry Andric const bool IsImmutable = !Flags.isByVal();
112344a3780SDimitry Andric int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable);
1131d5ae102SDimitry Andric MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
114cfca06d7SDimitry Andric auto AddrReg = MIRBuilder.buildFrameIndex(
115cfca06d7SDimitry Andric LLT::pointer(AMDGPUAS::PRIVATE_ADDRESS, 32), FI);
1161d5ae102SDimitry Andric StackUsed = std::max(StackUsed, Size + Offset);
117cfca06d7SDimitry Andric return AddrReg.getReg(0);
1181d5ae102SDimitry Andric }
1191d5ae102SDimitry Andric
assignValueToReg__anona8a9f0490111::AMDGPUIncomingArgHandler1201d5ae102SDimitry Andric void assignValueToReg(Register ValVReg, Register PhysReg,
121b1c73532SDimitry Andric const CCValAssign &VA) override {
1221d5ae102SDimitry Andric markPhysRegUsed(PhysReg);
1231d5ae102SDimitry Andric
1241d5ae102SDimitry Andric if (VA.getLocVT().getSizeInBits() < 32) {
1251d5ae102SDimitry Andric // 16-bit types are reported as legal for 32-bit registers. We need to do
1261d5ae102SDimitry Andric // a 32-bit copy, and truncate to avoid the verifier complaining about it.
1271d5ae102SDimitry Andric auto Copy = MIRBuilder.buildCopy(LLT::scalar(32), PhysReg);
128344a3780SDimitry Andric
129344a3780SDimitry Andric // If we have signext/zeroext, it applies to the whole 32-bit register
130344a3780SDimitry Andric // before truncation.
131344a3780SDimitry Andric auto Extended =
132344a3780SDimitry Andric buildExtensionHint(VA, Copy.getReg(0), LLT(VA.getLocVT()));
133344a3780SDimitry Andric MIRBuilder.buildTrunc(ValVReg, Extended);
1341d5ae102SDimitry Andric return;
1351d5ae102SDimitry Andric }
1361d5ae102SDimitry Andric
137344a3780SDimitry Andric IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA);
1381d5ae102SDimitry Andric }
1391d5ae102SDimitry Andric
assignValueToAddress__anona8a9f0490111::AMDGPUIncomingArgHandler140344a3780SDimitry Andric void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
141b1c73532SDimitry Andric const MachinePointerInfo &MPO,
142b1c73532SDimitry Andric const CCValAssign &VA) override {
143cfca06d7SDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
144cfca06d7SDimitry Andric
145cfca06d7SDimitry Andric auto MMO = MF.getMachineMemOperand(
146344a3780SDimitry Andric MPO, MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, MemTy,
147cfca06d7SDimitry Andric inferAlignFromPtrInfo(MF, MPO));
1481d5ae102SDimitry Andric MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
1491d5ae102SDimitry Andric }
1501d5ae102SDimitry Andric
1511d5ae102SDimitry Andric /// How the physical register gets marked varies between formal
1521d5ae102SDimitry Andric /// parameters (it's a basic-block live-in), and a call instruction
1531d5ae102SDimitry Andric /// (it's an implicit-def of the BL).
1541d5ae102SDimitry Andric virtual void markPhysRegUsed(unsigned PhysReg) = 0;
1551d5ae102SDimitry Andric };
1561d5ae102SDimitry Andric
157b60736ecSDimitry Andric struct FormalArgHandler : public AMDGPUIncomingArgHandler {
FormalArgHandler__anona8a9f0490111::FormalArgHandler158344a3780SDimitry Andric FormalArgHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI)
159344a3780SDimitry Andric : AMDGPUIncomingArgHandler(B, MRI) {}
1601d5ae102SDimitry Andric
markPhysRegUsed__anona8a9f0490111::FormalArgHandler1611d5ae102SDimitry Andric void markPhysRegUsed(unsigned PhysReg) override {
1621d5ae102SDimitry Andric MIRBuilder.getMBB().addLiveIn(PhysReg);
163e6d15924SDimitry Andric }
164e6d15924SDimitry Andric };
165e6d15924SDimitry Andric
166b60736ecSDimitry Andric struct CallReturnHandler : public AMDGPUIncomingArgHandler {
CallReturnHandler__anona8a9f0490111::CallReturnHandler167b60736ecSDimitry Andric CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
168344a3780SDimitry Andric MachineInstrBuilder MIB)
169344a3780SDimitry Andric : AMDGPUIncomingArgHandler(MIRBuilder, MRI), MIB(MIB) {}
170b60736ecSDimitry Andric
markPhysRegUsed__anona8a9f0490111::CallReturnHandler171b60736ecSDimitry Andric void markPhysRegUsed(unsigned PhysReg) override {
172b60736ecSDimitry Andric MIB.addDef(PhysReg, RegState::Implicit);
173b60736ecSDimitry Andric }
174b60736ecSDimitry Andric
175b60736ecSDimitry Andric MachineInstrBuilder MIB;
176b60736ecSDimitry Andric };
177b60736ecSDimitry Andric
178344a3780SDimitry Andric struct AMDGPUOutgoingArgHandler : public AMDGPUOutgoingValueHandler {
179b60736ecSDimitry Andric /// For tail calls, the byte offset of the call's argument area from the
180b60736ecSDimitry Andric /// callee's. Unused elsewhere.
181b60736ecSDimitry Andric int FPDiff;
182b60736ecSDimitry Andric
183b60736ecSDimitry Andric // Cache the SP register vreg if we need it more than once in this call site.
184b60736ecSDimitry Andric Register SPReg;
185b60736ecSDimitry Andric
186b60736ecSDimitry Andric bool IsTailCall;
187b60736ecSDimitry Andric
AMDGPUOutgoingArgHandler__anona8a9f0490111::AMDGPUOutgoingArgHandler188b60736ecSDimitry Andric AMDGPUOutgoingArgHandler(MachineIRBuilder &MIRBuilder,
189b60736ecSDimitry Andric MachineRegisterInfo &MRI, MachineInstrBuilder MIB,
190b60736ecSDimitry Andric bool IsTailCall = false, int FPDiff = 0)
191344a3780SDimitry Andric : AMDGPUOutgoingValueHandler(MIRBuilder, MRI, MIB), FPDiff(FPDiff),
192344a3780SDimitry Andric IsTailCall(IsTailCall) {}
193b60736ecSDimitry Andric
getStackAddress__anona8a9f0490111::AMDGPUOutgoingArgHandler194b60736ecSDimitry Andric Register getStackAddress(uint64_t Size, int64_t Offset,
195344a3780SDimitry Andric MachinePointerInfo &MPO,
196344a3780SDimitry Andric ISD::ArgFlagsTy Flags) override {
197b60736ecSDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
198b60736ecSDimitry Andric const LLT PtrTy = LLT::pointer(AMDGPUAS::PRIVATE_ADDRESS, 32);
199b60736ecSDimitry Andric const LLT S32 = LLT::scalar(32);
200b60736ecSDimitry Andric
201b60736ecSDimitry Andric if (IsTailCall) {
202344a3780SDimitry Andric Offset += FPDiff;
203344a3780SDimitry Andric int FI = MF.getFrameInfo().CreateFixedObject(Size, Offset, true);
204344a3780SDimitry Andric auto FIReg = MIRBuilder.buildFrameIndex(PtrTy, FI);
205344a3780SDimitry Andric MPO = MachinePointerInfo::getFixedStack(MF, FI);
206344a3780SDimitry Andric return FIReg.getReg(0);
207b60736ecSDimitry Andric }
208b60736ecSDimitry Andric
209b60736ecSDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
210b60736ecSDimitry Andric
2116f8fc217SDimitry Andric if (!SPReg) {
2126f8fc217SDimitry Andric const GCNSubtarget &ST = MIRBuilder.getMF().getSubtarget<GCNSubtarget>();
2136f8fc217SDimitry Andric if (ST.enableFlatScratch()) {
2146f8fc217SDimitry Andric // The stack is accessed unswizzled, so we can use a regular copy.
2156f8fc217SDimitry Andric SPReg = MIRBuilder.buildCopy(PtrTy,
2166f8fc217SDimitry Andric MFI->getStackPtrOffsetReg()).getReg(0);
2176f8fc217SDimitry Andric } else {
2186f8fc217SDimitry Andric // The address we produce here, without knowing the use context, is going
2196f8fc217SDimitry Andric // to be interpreted as a vector address, so we need to convert to a
2206f8fc217SDimitry Andric // swizzled address.
2216f8fc217SDimitry Andric SPReg = MIRBuilder.buildInstr(AMDGPU::G_AMDGPU_WAVE_ADDRESS, {PtrTy},
2226f8fc217SDimitry Andric {MFI->getStackPtrOffsetReg()}).getReg(0);
2236f8fc217SDimitry Andric }
2246f8fc217SDimitry Andric }
225b60736ecSDimitry Andric
226b60736ecSDimitry Andric auto OffsetReg = MIRBuilder.buildConstant(S32, Offset);
227b60736ecSDimitry Andric
228b60736ecSDimitry Andric auto AddrReg = MIRBuilder.buildPtrAdd(PtrTy, SPReg, OffsetReg);
229b60736ecSDimitry Andric MPO = MachinePointerInfo::getStack(MF, Offset);
230b60736ecSDimitry Andric return AddrReg.getReg(0);
231b60736ecSDimitry Andric }
232b60736ecSDimitry Andric
assignValueToReg__anona8a9f0490111::AMDGPUOutgoingArgHandler233b60736ecSDimitry Andric void assignValueToReg(Register ValVReg, Register PhysReg,
234b1c73532SDimitry Andric const CCValAssign &VA) override {
235b60736ecSDimitry Andric MIB.addUse(PhysReg, RegState::Implicit);
236344a3780SDimitry Andric Register ExtReg = extendRegisterMin32(*this, ValVReg, VA);
237b60736ecSDimitry Andric MIRBuilder.buildCopy(PhysReg, ExtReg);
238b60736ecSDimitry Andric }
239b60736ecSDimitry Andric
assignValueToAddress__anona8a9f0490111::AMDGPUOutgoingArgHandler240344a3780SDimitry Andric void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
241b1c73532SDimitry Andric const MachinePointerInfo &MPO,
242b1c73532SDimitry Andric const CCValAssign &VA) override {
243b60736ecSDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
244b60736ecSDimitry Andric uint64_t LocMemOffset = VA.getLocMemOffset();
245b60736ecSDimitry Andric const auto &ST = MF.getSubtarget<GCNSubtarget>();
246b60736ecSDimitry Andric
247b60736ecSDimitry Andric auto MMO = MF.getMachineMemOperand(
248344a3780SDimitry Andric MPO, MachineMemOperand::MOStore, MemTy,
249b60736ecSDimitry Andric commonAlignment(ST.getStackAlignment(), LocMemOffset));
250b60736ecSDimitry Andric MIRBuilder.buildStore(ValVReg, Addr, *MMO);
251b60736ecSDimitry Andric }
252b60736ecSDimitry Andric
assignValueToAddress__anona8a9f0490111::AMDGPUOutgoingArgHandler253344a3780SDimitry Andric void assignValueToAddress(const CallLowering::ArgInfo &Arg,
254344a3780SDimitry Andric unsigned ValRegIndex, Register Addr, LLT MemTy,
255b1c73532SDimitry Andric const MachinePointerInfo &MPO,
256b1c73532SDimitry Andric const CCValAssign &VA) override {
257b60736ecSDimitry Andric Register ValVReg = VA.getLocInfo() != CCValAssign::LocInfo::FPExt
258344a3780SDimitry Andric ? extendRegister(Arg.Regs[ValRegIndex], VA)
259344a3780SDimitry Andric : Arg.Regs[ValRegIndex];
260344a3780SDimitry Andric assignValueToAddress(ValVReg, Addr, MemTy, MPO, VA);
261b60736ecSDimitry Andric }
262b60736ecSDimitry Andric };
263ac9a064cSDimitry Andric } // anonymous namespace
264e6d15924SDimitry Andric
AMDGPUCallLowering(const AMDGPUTargetLowering & TLI)26501095a5dSDimitry Andric AMDGPUCallLowering::AMDGPUCallLowering(const AMDGPUTargetLowering &TLI)
266d8e91e46SDimitry Andric : CallLowering(&TLI) {
26701095a5dSDimitry Andric }
26801095a5dSDimitry Andric
269c0981da4SDimitry Andric // FIXME: Compatibility shim
extOpcodeToISDExtOpcode(unsigned MIOpc)270cfca06d7SDimitry Andric static ISD::NodeType extOpcodeToISDExtOpcode(unsigned MIOpc) {
271cfca06d7SDimitry Andric switch (MIOpc) {
272cfca06d7SDimitry Andric case TargetOpcode::G_SEXT:
273cfca06d7SDimitry Andric return ISD::SIGN_EXTEND;
274cfca06d7SDimitry Andric case TargetOpcode::G_ZEXT:
275cfca06d7SDimitry Andric return ISD::ZERO_EXTEND;
276cfca06d7SDimitry Andric case TargetOpcode::G_ANYEXT:
277cfca06d7SDimitry Andric return ISD::ANY_EXTEND;
278cfca06d7SDimitry Andric default:
279cfca06d7SDimitry Andric llvm_unreachable("not an extend opcode");
280cfca06d7SDimitry Andric }
281cfca06d7SDimitry Andric }
282cfca06d7SDimitry Andric
canLowerReturn(MachineFunction & MF,CallingConv::ID CallConv,SmallVectorImpl<BaseArgInfo> & Outs,bool IsVarArg) const283b60736ecSDimitry Andric bool AMDGPUCallLowering::canLowerReturn(MachineFunction &MF,
284b60736ecSDimitry Andric CallingConv::ID CallConv,
285b60736ecSDimitry Andric SmallVectorImpl<BaseArgInfo> &Outs,
286b60736ecSDimitry Andric bool IsVarArg) const {
287b60736ecSDimitry Andric // For shaders. Vector types should be explicitly handled by CC.
288b60736ecSDimitry Andric if (AMDGPU::isEntryFunctionCC(CallConv))
289b60736ecSDimitry Andric return true;
290b60736ecSDimitry Andric
291b60736ecSDimitry Andric SmallVector<CCValAssign, 16> ArgLocs;
292b60736ecSDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
293b60736ecSDimitry Andric CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs,
294b60736ecSDimitry Andric MF.getFunction().getContext());
295b60736ecSDimitry Andric
296b60736ecSDimitry Andric return checkReturn(CCInfo, Outs, TLI.CCAssignFnForReturn(CallConv, IsVarArg));
2971d5ae102SDimitry Andric }
2981d5ae102SDimitry Andric
2991d5ae102SDimitry Andric /// Lower the return value for the already existing \p Ret. This assumes that
3001d5ae102SDimitry Andric /// \p B's insertion point is correct.
lowerReturnVal(MachineIRBuilder & B,const Value * Val,ArrayRef<Register> VRegs,MachineInstrBuilder & Ret) const3011d5ae102SDimitry Andric bool AMDGPUCallLowering::lowerReturnVal(MachineIRBuilder &B,
3021d5ae102SDimitry Andric const Value *Val, ArrayRef<Register> VRegs,
3031d5ae102SDimitry Andric MachineInstrBuilder &Ret) const {
3041d5ae102SDimitry Andric if (!Val)
3051d5ae102SDimitry Andric return true;
3061d5ae102SDimitry Andric
3071d5ae102SDimitry Andric auto &MF = B.getMF();
3081d5ae102SDimitry Andric const auto &F = MF.getFunction();
3091d5ae102SDimitry Andric const DataLayout &DL = MF.getDataLayout();
310cfca06d7SDimitry Andric MachineRegisterInfo *MRI = B.getMRI();
311b60736ecSDimitry Andric LLVMContext &Ctx = F.getContext();
3121d5ae102SDimitry Andric
3131d5ae102SDimitry Andric CallingConv::ID CC = F.getCallingConv();
3141d5ae102SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
3151d5ae102SDimitry Andric
316b60736ecSDimitry Andric SmallVector<EVT, 8> SplitEVTs;
317b60736ecSDimitry Andric ComputeValueVTs(TLI, DL, Val->getType(), SplitEVTs);
318b60736ecSDimitry Andric assert(VRegs.size() == SplitEVTs.size() &&
319b60736ecSDimitry Andric "For each split Type there should be exactly one VReg.");
3201d5ae102SDimitry Andric
321b60736ecSDimitry Andric SmallVector<ArgInfo, 8> SplitRetInfos;
322b60736ecSDimitry Andric
323b60736ecSDimitry Andric for (unsigned i = 0; i < SplitEVTs.size(); ++i) {
324b60736ecSDimitry Andric EVT VT = SplitEVTs[i];
325b60736ecSDimitry Andric Register Reg = VRegs[i];
326344a3780SDimitry Andric ArgInfo RetInfo(Reg, VT.getTypeForEVT(Ctx), 0);
327b60736ecSDimitry Andric setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F);
328b60736ecSDimitry Andric
329b60736ecSDimitry Andric if (VT.isScalarInteger()) {
330b60736ecSDimitry Andric unsigned ExtendOp = TargetOpcode::G_ANYEXT;
331b60736ecSDimitry Andric if (RetInfo.Flags[0].isSExt()) {
332b60736ecSDimitry Andric assert(RetInfo.Regs.size() == 1 && "expect only simple return values");
333b60736ecSDimitry Andric ExtendOp = TargetOpcode::G_SEXT;
334b60736ecSDimitry Andric } else if (RetInfo.Flags[0].isZExt()) {
335b60736ecSDimitry Andric assert(RetInfo.Regs.size() == 1 && "expect only simple return values");
336b60736ecSDimitry Andric ExtendOp = TargetOpcode::G_ZEXT;
337b60736ecSDimitry Andric }
338b60736ecSDimitry Andric
339b60736ecSDimitry Andric EVT ExtVT = TLI.getTypeForExtReturn(Ctx, VT,
340b60736ecSDimitry Andric extOpcodeToISDExtOpcode(ExtendOp));
341b60736ecSDimitry Andric if (ExtVT != VT) {
342b60736ecSDimitry Andric RetInfo.Ty = ExtVT.getTypeForEVT(Ctx);
343b60736ecSDimitry Andric LLT ExtTy = getLLTForType(*RetInfo.Ty, DL);
344b60736ecSDimitry Andric Reg = B.buildInstr(ExtendOp, {ExtTy}, {Reg}).getReg(0);
345b60736ecSDimitry Andric }
346b60736ecSDimitry Andric }
347b60736ecSDimitry Andric
348b60736ecSDimitry Andric if (Reg != RetInfo.Regs[0]) {
349b60736ecSDimitry Andric RetInfo.Regs[0] = Reg;
350b60736ecSDimitry Andric // Reset the arg flags after modifying Reg.
351b60736ecSDimitry Andric setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F);
352b60736ecSDimitry Andric }
353b60736ecSDimitry Andric
354344a3780SDimitry Andric splitToValueTypes(RetInfo, SplitRetInfos, DL, CC);
355b60736ecSDimitry Andric }
3561d5ae102SDimitry Andric
3571d5ae102SDimitry Andric CCAssignFn *AssignFn = TLI.CCAssignFnForReturn(CC, F.isVarArg());
358344a3780SDimitry Andric
359344a3780SDimitry Andric OutgoingValueAssigner Assigner(AssignFn);
360344a3780SDimitry Andric AMDGPUOutgoingValueHandler RetHandler(B, *MRI, Ret);
361344a3780SDimitry Andric return determineAndHandleAssignments(RetHandler, Assigner, SplitRetInfos, B,
362344a3780SDimitry Andric CC, F.isVarArg());
3631d5ae102SDimitry Andric }
3641d5ae102SDimitry Andric
lowerReturn(MachineIRBuilder & B,const Value * Val,ArrayRef<Register> VRegs,FunctionLoweringInfo & FLI) const365b60736ecSDimitry Andric bool AMDGPUCallLowering::lowerReturn(MachineIRBuilder &B, const Value *Val,
366b60736ecSDimitry Andric ArrayRef<Register> VRegs,
367b60736ecSDimitry Andric FunctionLoweringInfo &FLI) const {
368eb11fae6SDimitry Andric
3691d5ae102SDimitry Andric MachineFunction &MF = B.getMF();
370e6d15924SDimitry Andric SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
371e6d15924SDimitry Andric MFI->setIfReturnsVoid(!Val);
372e6d15924SDimitry Andric
3731d5ae102SDimitry Andric assert(!Val == VRegs.empty() && "Return value without a vreg");
3741d5ae102SDimitry Andric
3751d5ae102SDimitry Andric CallingConv::ID CC = B.getMF().getFunction().getCallingConv();
3761d5ae102SDimitry Andric const bool IsShader = AMDGPU::isShader(CC);
377b60736ecSDimitry Andric const bool IsWaveEnd =
378b60736ecSDimitry Andric (IsShader && MFI->returnsVoid()) || AMDGPU::isKernel(CC);
3791d5ae102SDimitry Andric if (IsWaveEnd) {
3801d5ae102SDimitry Andric B.buildInstr(AMDGPU::S_ENDPGM)
3811d5ae102SDimitry Andric .addImm(0);
38201095a5dSDimitry Andric return true;
38301095a5dSDimitry Andric }
38401095a5dSDimitry Andric
385145449b1SDimitry Andric unsigned ReturnOpc =
386145449b1SDimitry Andric IsShader ? AMDGPU::SI_RETURN_TO_EPILOG : AMDGPU::SI_RETURN;
3871d5ae102SDimitry Andric auto Ret = B.buildInstrNoInsert(ReturnOpc);
388e6d15924SDimitry Andric
389b60736ecSDimitry Andric if (!FLI.CanLowerReturn)
390b60736ecSDimitry Andric insertSRetStores(B, Val->getType(), VRegs, FLI.DemoteRegister);
391b60736ecSDimitry Andric else if (!lowerReturnVal(B, Val, VRegs, Ret))
3921d5ae102SDimitry Andric return false;
3931d5ae102SDimitry Andric
3941d5ae102SDimitry Andric // TODO: Handle CalleeSavedRegsViaCopy.
3951d5ae102SDimitry Andric
3961d5ae102SDimitry Andric B.insertInstr(Ret);
397e6d15924SDimitry Andric return true;
398e6d15924SDimitry Andric }
399e6d15924SDimitry Andric
lowerParameterPtr(Register DstReg,MachineIRBuilder & B,uint64_t Offset) const400b60736ecSDimitry Andric void AMDGPUCallLowering::lowerParameterPtr(Register DstReg, MachineIRBuilder &B,
401eb11fae6SDimitry Andric uint64_t Offset) const {
4021d5ae102SDimitry Andric MachineFunction &MF = B.getMF();
403044eb2f6SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
40471d5a254SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
405e6d15924SDimitry Andric Register KernArgSegmentPtr =
406044eb2f6SDimitry Andric MFI->getPreloadedReg(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR);
407e6d15924SDimitry Andric Register KernArgSegmentVReg = MRI.getLiveInVirtReg(KernArgSegmentPtr);
40871d5a254SDimitry Andric
409cfca06d7SDimitry Andric auto OffsetReg = B.buildConstant(LLT::scalar(64), Offset);
41071d5a254SDimitry Andric
411b60736ecSDimitry Andric B.buildPtrAdd(DstReg, KernArgSegmentVReg, OffsetReg);
41271d5a254SDimitry Andric }
41371d5a254SDimitry Andric
lowerParameter(MachineIRBuilder & B,ArgInfo & OrigArg,uint64_t Offset,Align Alignment) const414344a3780SDimitry Andric void AMDGPUCallLowering::lowerParameter(MachineIRBuilder &B, ArgInfo &OrigArg,
415344a3780SDimitry Andric uint64_t Offset,
416344a3780SDimitry Andric Align Alignment) const {
4171d5ae102SDimitry Andric MachineFunction &MF = B.getMF();
418044eb2f6SDimitry Andric const Function &F = MF.getFunction();
419ac9a064cSDimitry Andric const DataLayout &DL = F.getDataLayout();
420706b4fc4SDimitry Andric MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
421b60736ecSDimitry Andric
422b60736ecSDimitry Andric LLT PtrTy = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64);
423344a3780SDimitry Andric
424344a3780SDimitry Andric SmallVector<ArgInfo, 32> SplitArgs;
425344a3780SDimitry Andric SmallVector<uint64_t> FieldOffsets;
426344a3780SDimitry Andric splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv(), &FieldOffsets);
427344a3780SDimitry Andric
428344a3780SDimitry Andric unsigned Idx = 0;
429344a3780SDimitry Andric for (ArgInfo &SplitArg : SplitArgs) {
430b60736ecSDimitry Andric Register PtrReg = B.getMRI()->createGenericVirtualRegister(PtrTy);
431344a3780SDimitry Andric lowerParameterPtr(PtrReg, B, Offset + FieldOffsets[Idx]);
432344a3780SDimitry Andric
433344a3780SDimitry Andric LLT ArgTy = getLLTForType(*SplitArg.Ty, DL);
434344a3780SDimitry Andric if (SplitArg.Flags[0].isPointer()) {
435344a3780SDimitry Andric // Compensate for losing pointeriness in splitValueTypes.
436344a3780SDimitry Andric LLT PtrTy = LLT::pointer(SplitArg.Flags[0].getPointerAddrSpace(),
437344a3780SDimitry Andric ArgTy.getScalarSizeInBits());
438344a3780SDimitry Andric ArgTy = ArgTy.isVector() ? LLT::vector(ArgTy.getElementCount(), PtrTy)
439344a3780SDimitry Andric : PtrTy;
440344a3780SDimitry Andric }
44171d5a254SDimitry Andric
442cfca06d7SDimitry Andric MachineMemOperand *MMO = MF.getMachineMemOperand(
443cfca06d7SDimitry Andric PtrInfo,
444cfca06d7SDimitry Andric MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
44571d5a254SDimitry Andric MachineMemOperand::MOInvariant,
446344a3780SDimitry Andric ArgTy, commonAlignment(Alignment, FieldOffsets[Idx]));
44771d5a254SDimitry Andric
448344a3780SDimitry Andric assert(SplitArg.Regs.size() == 1);
449344a3780SDimitry Andric
450344a3780SDimitry Andric B.buildLoad(SplitArg.Regs[0], PtrReg, *MMO);
451344a3780SDimitry Andric ++Idx;
452344a3780SDimitry Andric }
453e6d15924SDimitry Andric }
454e6d15924SDimitry Andric
455e6d15924SDimitry Andric // Allocate special inputs passed in user SGPRs.
allocateHSAUserSGPRs(CCState & CCInfo,MachineIRBuilder & B,MachineFunction & MF,const SIRegisterInfo & TRI,SIMachineFunctionInfo & Info)456e6d15924SDimitry Andric static void allocateHSAUserSGPRs(CCState &CCInfo,
4571d5ae102SDimitry Andric MachineIRBuilder &B,
458e6d15924SDimitry Andric MachineFunction &MF,
459e6d15924SDimitry Andric const SIRegisterInfo &TRI,
460e6d15924SDimitry Andric SIMachineFunctionInfo &Info) {
461e6d15924SDimitry Andric // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
462b1c73532SDimitry Andric const GCNUserSGPRUsageInfo &UserSGPRInfo = Info.getUserSGPRInfo();
463b1c73532SDimitry Andric if (UserSGPRInfo.hasPrivateSegmentBuffer()) {
464cfca06d7SDimitry Andric Register PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI);
465e6d15924SDimitry Andric MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass);
466e6d15924SDimitry Andric CCInfo.AllocateReg(PrivateSegmentBufferReg);
467e6d15924SDimitry Andric }
468e6d15924SDimitry Andric
469b1c73532SDimitry Andric if (UserSGPRInfo.hasDispatchPtr()) {
470cfca06d7SDimitry Andric Register DispatchPtrReg = Info.addDispatchPtr(TRI);
471e6d15924SDimitry Andric MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
472e6d15924SDimitry Andric CCInfo.AllocateReg(DispatchPtrReg);
473e6d15924SDimitry Andric }
474e6d15924SDimitry Andric
4757fa27ce4SDimitry Andric const Module *M = MF.getFunction().getParent();
476b1c73532SDimitry Andric if (UserSGPRInfo.hasQueuePtr() &&
4774df029ccSDimitry Andric AMDGPU::getAMDHSACodeObjectVersion(*M) < AMDGPU::AMDHSA_COV5) {
478cfca06d7SDimitry Andric Register QueuePtrReg = Info.addQueuePtr(TRI);
479e6d15924SDimitry Andric MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
480e6d15924SDimitry Andric CCInfo.AllocateReg(QueuePtrReg);
481e6d15924SDimitry Andric }
482e6d15924SDimitry Andric
483b1c73532SDimitry Andric if (UserSGPRInfo.hasKernargSegmentPtr()) {
484e6d15924SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
485e6d15924SDimitry Andric Register InputPtrReg = Info.addKernargSegmentPtr(TRI);
486e6d15924SDimitry Andric const LLT P4 = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64);
487e6d15924SDimitry Andric Register VReg = MRI.createGenericVirtualRegister(P4);
488e6d15924SDimitry Andric MRI.addLiveIn(InputPtrReg, VReg);
4891d5ae102SDimitry Andric B.getMBB().addLiveIn(InputPtrReg);
4901d5ae102SDimitry Andric B.buildCopy(VReg, InputPtrReg);
491e6d15924SDimitry Andric CCInfo.AllocateReg(InputPtrReg);
492e6d15924SDimitry Andric }
493e6d15924SDimitry Andric
494b1c73532SDimitry Andric if (UserSGPRInfo.hasDispatchID()) {
495cfca06d7SDimitry Andric Register DispatchIDReg = Info.addDispatchID(TRI);
496e6d15924SDimitry Andric MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
497e6d15924SDimitry Andric CCInfo.AllocateReg(DispatchIDReg);
498e6d15924SDimitry Andric }
499e6d15924SDimitry Andric
500b1c73532SDimitry Andric if (UserSGPRInfo.hasFlatScratchInit()) {
501cfca06d7SDimitry Andric Register FlatScratchInitReg = Info.addFlatScratchInit(TRI);
502e6d15924SDimitry Andric MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
503e6d15924SDimitry Andric CCInfo.AllocateReg(FlatScratchInitReg);
504e6d15924SDimitry Andric }
505e6d15924SDimitry Andric
506e6d15924SDimitry Andric // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
507e6d15924SDimitry Andric // these from the dispatch pointer.
508e6d15924SDimitry Andric }
509e6d15924SDimitry Andric
lowerFormalArgumentsKernel(MachineIRBuilder & B,const Function & F,ArrayRef<ArrayRef<Register>> VRegs) const510e6d15924SDimitry Andric bool AMDGPUCallLowering::lowerFormalArgumentsKernel(
5111d5ae102SDimitry Andric MachineIRBuilder &B, const Function &F,
512e6d15924SDimitry Andric ArrayRef<ArrayRef<Register>> VRegs) const {
5131d5ae102SDimitry Andric MachineFunction &MF = B.getMF();
514eb11fae6SDimitry Andric const GCNSubtarget *Subtarget = &MF.getSubtarget<GCNSubtarget>();
51571d5a254SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
51671d5a254SDimitry Andric SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
5171d5ae102SDimitry Andric const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
5181d5ae102SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
519ac9a064cSDimitry Andric const DataLayout &DL = F.getDataLayout();
52071d5a254SDimitry Andric
52171d5a254SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs;
52271d5a254SDimitry Andric CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
52371d5a254SDimitry Andric
5241d5ae102SDimitry Andric allocateHSAUserSGPRs(CCInfo, B, MF, *TRI, *Info);
52571d5a254SDimitry Andric
526eb11fae6SDimitry Andric unsigned i = 0;
527cfca06d7SDimitry Andric const Align KernArgBaseAlign(16);
5287fa27ce4SDimitry Andric const unsigned BaseOffset = Subtarget->getExplicitKernelArgOffset();
529eb11fae6SDimitry Andric uint64_t ExplicitArgOffset = 0;
530eb11fae6SDimitry Andric
531eb11fae6SDimitry Andric // TODO: Align down to dword alignment and extract bits for extending loads.
532eb11fae6SDimitry Andric for (auto &Arg : F.args()) {
533b60736ecSDimitry Andric const bool IsByRef = Arg.hasByRefAttr();
534b60736ecSDimitry Andric Type *ArgTy = IsByRef ? Arg.getParamByRefType() : Arg.getType();
535eb11fae6SDimitry Andric unsigned AllocSize = DL.getTypeAllocSize(ArgTy);
536eb11fae6SDimitry Andric if (AllocSize == 0)
537eb11fae6SDimitry Andric continue;
538eb11fae6SDimitry Andric
539e3b55780SDimitry Andric MaybeAlign ParamAlign = IsByRef ? Arg.getParamAlign() : std::nullopt;
540145449b1SDimitry Andric Align ABIAlign = DL.getValueOrABITypeAlignment(ParamAlign, ArgTy);
541eb11fae6SDimitry Andric
542eb11fae6SDimitry Andric uint64_t ArgOffset = alignTo(ExplicitArgOffset, ABIAlign) + BaseOffset;
543eb11fae6SDimitry Andric ExplicitArgOffset = alignTo(ExplicitArgOffset, ABIAlign) + AllocSize;
544eb11fae6SDimitry Andric
545cfca06d7SDimitry Andric if (Arg.use_empty()) {
546cfca06d7SDimitry Andric ++i;
547cfca06d7SDimitry Andric continue;
548cfca06d7SDimitry Andric }
549cfca06d7SDimitry Andric
550b60736ecSDimitry Andric Align Alignment = commonAlignment(KernArgBaseAlign, ArgOffset);
551b60736ecSDimitry Andric
552b60736ecSDimitry Andric if (IsByRef) {
553b60736ecSDimitry Andric unsigned ByRefAS = cast<PointerType>(Arg.getType())->getAddressSpace();
554b60736ecSDimitry Andric
555b60736ecSDimitry Andric assert(VRegs[i].size() == 1 &&
556b60736ecSDimitry Andric "expected only one register for byval pointers");
557b60736ecSDimitry Andric if (ByRefAS == AMDGPUAS::CONSTANT_ADDRESS) {
558344a3780SDimitry Andric lowerParameterPtr(VRegs[i][0], B, ArgOffset);
559b60736ecSDimitry Andric } else {
560b60736ecSDimitry Andric const LLT ConstPtrTy = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64);
561b60736ecSDimitry Andric Register PtrReg = MRI.createGenericVirtualRegister(ConstPtrTy);
562344a3780SDimitry Andric lowerParameterPtr(PtrReg, B, ArgOffset);
563b60736ecSDimitry Andric
564b60736ecSDimitry Andric B.buildAddrSpaceCast(VRegs[i][0], PtrReg);
565b60736ecSDimitry Andric }
566b60736ecSDimitry Andric } else {
567344a3780SDimitry Andric ArgInfo OrigArg(VRegs[i], Arg, i);
568344a3780SDimitry Andric const unsigned OrigArgIdx = i + AttributeList::FirstArgIndex;
569344a3780SDimitry Andric setArgFlags(OrigArg, OrigArgIdx, DL, F);
570344a3780SDimitry Andric lowerParameter(B, OrigArg, ArgOffset, Alignment);
571b60736ecSDimitry Andric }
572b60736ecSDimitry Andric
573eb11fae6SDimitry Andric ++i;
574eb11fae6SDimitry Andric }
575eb11fae6SDimitry Andric
5761d5ae102SDimitry Andric TLI.allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info);
5771d5ae102SDimitry Andric TLI.allocateSystemSGPRs(CCInfo, MF, *Info, F.getCallingConv(), false);
578eb11fae6SDimitry Andric return true;
579eb11fae6SDimitry Andric }
580eb11fae6SDimitry Andric
lowerFormalArguments(MachineIRBuilder & B,const Function & F,ArrayRef<ArrayRef<Register>> VRegs,FunctionLoweringInfo & FLI) const581e6d15924SDimitry Andric bool AMDGPUCallLowering::lowerFormalArguments(
582b60736ecSDimitry Andric MachineIRBuilder &B, const Function &F, ArrayRef<ArrayRef<Register>> VRegs,
583b60736ecSDimitry Andric FunctionLoweringInfo &FLI) const {
5841d5ae102SDimitry Andric CallingConv::ID CC = F.getCallingConv();
5851d5ae102SDimitry Andric
586e6d15924SDimitry Andric // The infrastructure for normal calling convention lowering is essentially
587e6d15924SDimitry Andric // useless for kernels. We want to avoid any kind of legalization or argument
588e6d15924SDimitry Andric // splitting.
5891d5ae102SDimitry Andric if (CC == CallingConv::AMDGPU_KERNEL)
5901d5ae102SDimitry Andric return lowerFormalArgumentsKernel(B, F, VRegs);
591e6d15924SDimitry Andric
592b60736ecSDimitry Andric const bool IsGraphics = AMDGPU::isGraphics(CC);
5931d5ae102SDimitry Andric const bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CC);
594e6d15924SDimitry Andric
5951d5ae102SDimitry Andric MachineFunction &MF = B.getMF();
5961d5ae102SDimitry Andric MachineBasicBlock &MBB = B.getMBB();
597e6d15924SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
598e6d15924SDimitry Andric SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
5991d5ae102SDimitry Andric const GCNSubtarget &Subtarget = MF.getSubtarget<GCNSubtarget>();
6001d5ae102SDimitry Andric const SIRegisterInfo *TRI = Subtarget.getRegisterInfo();
601ac9a064cSDimitry Andric const DataLayout &DL = F.getDataLayout();
602e6d15924SDimitry Andric
603e6d15924SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs;
6041d5ae102SDimitry Andric CCState CCInfo(CC, F.isVarArg(), MF, ArgLocs, F.getContext());
605b1c73532SDimitry Andric const GCNUserSGPRUsageInfo &UserSGPRInfo = Info->getUserSGPRInfo();
6061d5ae102SDimitry Andric
607b1c73532SDimitry Andric if (UserSGPRInfo.hasImplicitBufferPtr()) {
6081d5ae102SDimitry Andric Register ImplicitBufferPtrReg = Info->addImplicitBufferPtr(*TRI);
609e6d15924SDimitry Andric MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass);
610e6d15924SDimitry Andric CCInfo.AllocateReg(ImplicitBufferPtrReg);
611e6d15924SDimitry Andric }
612e6d15924SDimitry Andric
6136f8fc217SDimitry Andric // FIXME: This probably isn't defined for mesa
614b1c73532SDimitry Andric if (UserSGPRInfo.hasFlatScratchInit() && !Subtarget.isAmdPalOS()) {
6156f8fc217SDimitry Andric Register FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
6166f8fc217SDimitry Andric MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
6176f8fc217SDimitry Andric CCInfo.AllocateReg(FlatScratchInitReg);
6186f8fc217SDimitry Andric }
6196f8fc217SDimitry Andric
6201d5ae102SDimitry Andric SmallVector<ArgInfo, 32> SplitArgs;
6211d5ae102SDimitry Andric unsigned Idx = 0;
622eb11fae6SDimitry Andric unsigned PSInputNum = 0;
623044eb2f6SDimitry Andric
624b60736ecSDimitry Andric // Insert the hidden sret parameter if the return value won't fit in the
625b60736ecSDimitry Andric // return registers.
626b60736ecSDimitry Andric if (!FLI.CanLowerReturn)
627b60736ecSDimitry Andric insertSRetIncomingArgument(F, SplitArgs, FLI.DemoteRegister, MRI, DL);
628b60736ecSDimitry Andric
6291d5ae102SDimitry Andric for (auto &Arg : F.args()) {
6301d5ae102SDimitry Andric if (DL.getTypeStoreSize(Arg.getType()) == 0)
631eb11fae6SDimitry Andric continue;
632eb11fae6SDimitry Andric
6331d5ae102SDimitry Andric const bool InReg = Arg.hasAttribute(Attribute::InReg);
6341d5ae102SDimitry Andric
6351d5ae102SDimitry Andric if (Arg.hasAttribute(Attribute::SwiftSelf) ||
6361d5ae102SDimitry Andric Arg.hasAttribute(Attribute::SwiftError) ||
6371d5ae102SDimitry Andric Arg.hasAttribute(Attribute::Nest))
6381d5ae102SDimitry Andric return false;
6391d5ae102SDimitry Andric
6401d5ae102SDimitry Andric if (CC == CallingConv::AMDGPU_PS && !InReg && PSInputNum <= 15) {
6411d5ae102SDimitry Andric const bool ArgUsed = !Arg.use_empty();
6421d5ae102SDimitry Andric bool SkipArg = !ArgUsed && !Info->isPSInputAllocated(PSInputNum);
6431d5ae102SDimitry Andric
6441d5ae102SDimitry Andric if (!SkipArg) {
645eb11fae6SDimitry Andric Info->markPSInputAllocated(PSInputNum);
6461d5ae102SDimitry Andric if (ArgUsed)
647eb11fae6SDimitry Andric Info->markPSInputEnabled(PSInputNum);
6481d5ae102SDimitry Andric }
649eb11fae6SDimitry Andric
650eb11fae6SDimitry Andric ++PSInputNum;
651eb11fae6SDimitry Andric
6521d5ae102SDimitry Andric if (SkipArg) {
65377fc4c14SDimitry Andric for (Register R : VRegs[Idx])
65477fc4c14SDimitry Andric B.buildUndef(R);
655eb11fae6SDimitry Andric
6561d5ae102SDimitry Andric ++Idx;
657eb11fae6SDimitry Andric continue;
6581d5ae102SDimitry Andric }
659044eb2f6SDimitry Andric }
660e6d15924SDimitry Andric
661344a3780SDimitry Andric ArgInfo OrigArg(VRegs[Idx], Arg, Idx);
662cfca06d7SDimitry Andric const unsigned OrigArgIdx = Idx + AttributeList::FirstArgIndex;
663cfca06d7SDimitry Andric setArgFlags(OrigArg, OrigArgIdx, DL, F);
6641d5ae102SDimitry Andric
665344a3780SDimitry Andric splitToValueTypes(OrigArg, SplitArgs, DL, CC);
6661d5ae102SDimitry Andric ++Idx;
667044eb2f6SDimitry Andric }
668044eb2f6SDimitry Andric
6691d5ae102SDimitry Andric // At least one interpolation mode must be enabled or else the GPU will
6701d5ae102SDimitry Andric // hang.
6711d5ae102SDimitry Andric //
6721d5ae102SDimitry Andric // Check PSInputAddr instead of PSInputEnable. The idea is that if the user
6731d5ae102SDimitry Andric // set PSInputAddr, the user wants to enable some bits after the compilation
6741d5ae102SDimitry Andric // based on run-time states. Since we can't know what the final PSInputEna
6751d5ae102SDimitry Andric // will look like, so we shouldn't do anything here and the user should take
6761d5ae102SDimitry Andric // responsibility for the correct programming.
6771d5ae102SDimitry Andric //
6781d5ae102SDimitry Andric // Otherwise, the following restrictions apply:
6791d5ae102SDimitry Andric // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
6801d5ae102SDimitry Andric // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
6811d5ae102SDimitry Andric // enabled too.
6821d5ae102SDimitry Andric if (CC == CallingConv::AMDGPU_PS) {
6831d5ae102SDimitry Andric if ((Info->getPSInputAddr() & 0x7F) == 0 ||
6841d5ae102SDimitry Andric ((Info->getPSInputAddr() & 0xF) == 0 &&
6851d5ae102SDimitry Andric Info->isPSInputAllocated(11))) {
6861d5ae102SDimitry Andric CCInfo.AllocateReg(AMDGPU::VGPR0);
6871d5ae102SDimitry Andric CCInfo.AllocateReg(AMDGPU::VGPR1);
6881d5ae102SDimitry Andric Info->markPSInputAllocated(0);
6891d5ae102SDimitry Andric Info->markPSInputEnabled(0);
6901d5ae102SDimitry Andric }
6911d5ae102SDimitry Andric
6921d5ae102SDimitry Andric if (Subtarget.isAmdPalOS()) {
6931d5ae102SDimitry Andric // For isAmdPalOS, the user does not enable some bits after compilation
6941d5ae102SDimitry Andric // based on run-time states; the register values being generated here are
6951d5ae102SDimitry Andric // the final ones set in hardware. Therefore we need to apply the
6961d5ae102SDimitry Andric // workaround to PSInputAddr and PSInputEnable together. (The case where
6971d5ae102SDimitry Andric // a bit is set in PSInputAddr but not PSInputEnable is where the frontend
6981d5ae102SDimitry Andric // set up an input arg for a particular interpolation mode, but nothing
6991d5ae102SDimitry Andric // uses that input arg. Really we should have an earlier pass that removes
7001d5ae102SDimitry Andric // such an arg.)
7011d5ae102SDimitry Andric unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable();
7021d5ae102SDimitry Andric if ((PsInputBits & 0x7F) == 0 ||
7031d5ae102SDimitry Andric ((PsInputBits & 0xF) == 0 &&
7041d5ae102SDimitry Andric (PsInputBits >> 11 & 1)))
7057fa27ce4SDimitry Andric Info->markPSInputEnabled(llvm::countr_zero(Info->getPSInputAddr()));
7061d5ae102SDimitry Andric }
7071d5ae102SDimitry Andric }
7081d5ae102SDimitry Andric
7091d5ae102SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
7101d5ae102SDimitry Andric CCAssignFn *AssignFn = TLI.CCAssignFnForCall(CC, F.isVarArg());
7111d5ae102SDimitry Andric
7121d5ae102SDimitry Andric if (!MBB.empty())
7131d5ae102SDimitry Andric B.setInstr(*MBB.begin());
7141d5ae102SDimitry Andric
71577fc4c14SDimitry Andric if (!IsEntryFunc && !IsGraphics) {
716cfca06d7SDimitry Andric // For the fixed ABI, pass workitem IDs in the last argument register.
717cfca06d7SDimitry Andric TLI.allocateSpecialInputVGPRsFixed(CCInfo, MF, *TRI, *Info);
7184df029ccSDimitry Andric
7194df029ccSDimitry Andric if (!Subtarget.enableFlatScratch())
7204df029ccSDimitry Andric CCInfo.AllocateReg(Info->getScratchRSrcReg());
7214df029ccSDimitry Andric TLI.allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info);
722cfca06d7SDimitry Andric }
723cfca06d7SDimitry Andric
724344a3780SDimitry Andric IncomingValueAssigner Assigner(AssignFn);
725344a3780SDimitry Andric if (!determineAssignments(Assigner, SplitArgs, CCInfo))
726eb11fae6SDimitry Andric return false;
7271d5ae102SDimitry Andric
728344a3780SDimitry Andric FormalArgHandler Handler(B, MRI);
729344a3780SDimitry Andric if (!handleAssignments(Handler, SplitArgs, CCInfo, ArgLocs, B))
730344a3780SDimitry Andric return false;
731344a3780SDimitry Andric
7327fa27ce4SDimitry Andric uint64_t StackSize = Assigner.StackSize;
733344a3780SDimitry Andric
7341d5ae102SDimitry Andric // Start adding system SGPRs.
7354df029ccSDimitry Andric if (IsEntryFunc)
736b60736ecSDimitry Andric TLI.allocateSystemSGPRs(CCInfo, MF, *Info, CC, IsGraphics);
7371d5ae102SDimitry Andric
738344a3780SDimitry Andric // When we tail call, we need to check if the callee's arguments will fit on
739344a3780SDimitry Andric // the caller's stack. So, whenever we lower formal arguments, we should keep
740344a3780SDimitry Andric // track of this information, since we might lower a tail call in this
741344a3780SDimitry Andric // function later.
7427fa27ce4SDimitry Andric Info->setBytesInStackArgArea(StackSize);
743344a3780SDimitry Andric
7441d5ae102SDimitry Andric // Move back to the end of the basic block.
7451d5ae102SDimitry Andric B.setMBB(MBB);
7461d5ae102SDimitry Andric
7471d5ae102SDimitry Andric return true;
74801095a5dSDimitry Andric }
749b60736ecSDimitry Andric
passSpecialInputs(MachineIRBuilder & MIRBuilder,CCState & CCInfo,SmallVectorImpl<std::pair<MCRegister,Register>> & ArgRegs,CallLoweringInfo & Info) const750b60736ecSDimitry Andric bool AMDGPUCallLowering::passSpecialInputs(MachineIRBuilder &MIRBuilder,
751b60736ecSDimitry Andric CCState &CCInfo,
752b60736ecSDimitry Andric SmallVectorImpl<std::pair<MCRegister, Register>> &ArgRegs,
753b60736ecSDimitry Andric CallLoweringInfo &Info) const {
754b60736ecSDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
755b60736ecSDimitry Andric
756c0981da4SDimitry Andric // If there's no call site, this doesn't correspond to a call from the IR and
757c0981da4SDimitry Andric // doesn't need implicit inputs.
758c0981da4SDimitry Andric if (!Info.CB)
759c0981da4SDimitry Andric return true;
760c0981da4SDimitry Andric
761b60736ecSDimitry Andric const AMDGPUFunctionArgInfo *CalleeArgInfo
762b60736ecSDimitry Andric = &AMDGPUArgumentUsageInfo::FixedABIFunctionInfo;
763b60736ecSDimitry Andric
764b60736ecSDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
765b60736ecSDimitry Andric const AMDGPUFunctionArgInfo &CallerArgInfo = MFI->getArgInfo();
766b60736ecSDimitry Andric
767b60736ecSDimitry Andric
768b60736ecSDimitry Andric // TODO: Unify with private memory register handling. This is complicated by
769b60736ecSDimitry Andric // the fact that at least in kernels, the input argument is not necessarily
770b60736ecSDimitry Andric // in the same location as the input.
771b60736ecSDimitry Andric AMDGPUFunctionArgInfo::PreloadedValue InputRegs[] = {
772b60736ecSDimitry Andric AMDGPUFunctionArgInfo::DISPATCH_PTR,
773b60736ecSDimitry Andric AMDGPUFunctionArgInfo::QUEUE_PTR,
774b60736ecSDimitry Andric AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR,
775b60736ecSDimitry Andric AMDGPUFunctionArgInfo::DISPATCH_ID,
776b60736ecSDimitry Andric AMDGPUFunctionArgInfo::WORKGROUP_ID_X,
777b60736ecSDimitry Andric AMDGPUFunctionArgInfo::WORKGROUP_ID_Y,
7784b4fe385SDimitry Andric AMDGPUFunctionArgInfo::WORKGROUP_ID_Z,
7794b4fe385SDimitry Andric AMDGPUFunctionArgInfo::LDS_KERNEL_ID,
780b60736ecSDimitry Andric };
781b60736ecSDimitry Andric
782c0981da4SDimitry Andric static constexpr StringLiteral ImplicitAttrNames[] = {
783c0981da4SDimitry Andric "amdgpu-no-dispatch-ptr",
784c0981da4SDimitry Andric "amdgpu-no-queue-ptr",
785c0981da4SDimitry Andric "amdgpu-no-implicitarg-ptr",
786c0981da4SDimitry Andric "amdgpu-no-dispatch-id",
787c0981da4SDimitry Andric "amdgpu-no-workgroup-id-x",
788c0981da4SDimitry Andric "amdgpu-no-workgroup-id-y",
7894b4fe385SDimitry Andric "amdgpu-no-workgroup-id-z",
7904b4fe385SDimitry Andric "amdgpu-no-lds-kernel-id",
791c0981da4SDimitry Andric };
792c0981da4SDimitry Andric
793b60736ecSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
794b60736ecSDimitry Andric
795b60736ecSDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
796b60736ecSDimitry Andric const AMDGPULegalizerInfo *LI
797b60736ecSDimitry Andric = static_cast<const AMDGPULegalizerInfo*>(ST.getLegalizerInfo());
798b60736ecSDimitry Andric
799c0981da4SDimitry Andric unsigned I = 0;
800b60736ecSDimitry Andric for (auto InputID : InputRegs) {
801b60736ecSDimitry Andric const ArgDescriptor *OutgoingArg;
802b60736ecSDimitry Andric const TargetRegisterClass *ArgRC;
803b60736ecSDimitry Andric LLT ArgTy;
804b60736ecSDimitry Andric
805c0981da4SDimitry Andric // If the callee does not use the attribute value, skip copying the value.
806c0981da4SDimitry Andric if (Info.CB->hasFnAttr(ImplicitAttrNames[I++]))
807c0981da4SDimitry Andric continue;
808c0981da4SDimitry Andric
809b60736ecSDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) =
810b60736ecSDimitry Andric CalleeArgInfo->getPreloadedValue(InputID);
811b60736ecSDimitry Andric if (!OutgoingArg)
812b60736ecSDimitry Andric continue;
813b60736ecSDimitry Andric
814b60736ecSDimitry Andric const ArgDescriptor *IncomingArg;
815b60736ecSDimitry Andric const TargetRegisterClass *IncomingArgRC;
816b60736ecSDimitry Andric std::tie(IncomingArg, IncomingArgRC, ArgTy) =
817b60736ecSDimitry Andric CallerArgInfo.getPreloadedValue(InputID);
818b60736ecSDimitry Andric assert(IncomingArgRC == ArgRC);
819b60736ecSDimitry Andric
820b60736ecSDimitry Andric Register InputReg = MRI.createGenericVirtualRegister(ArgTy);
821b60736ecSDimitry Andric
822b60736ecSDimitry Andric if (IncomingArg) {
823b60736ecSDimitry Andric LI->loadInputValue(InputReg, MIRBuilder, IncomingArg, ArgRC, ArgTy);
82477fc4c14SDimitry Andric } else if (InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR) {
825b60736ecSDimitry Andric LI->getImplicitArgPtr(InputReg, MRI, MIRBuilder);
8264b4fe385SDimitry Andric } else if (InputID == AMDGPUFunctionArgInfo::LDS_KERNEL_ID) {
827e3b55780SDimitry Andric std::optional<uint32_t> Id =
8284b4fe385SDimitry Andric AMDGPUMachineFunction::getLDSKernelIdMetadata(MF.getFunction());
829e3b55780SDimitry Andric if (Id) {
830e3b55780SDimitry Andric MIRBuilder.buildConstant(InputReg, *Id);
8314b4fe385SDimitry Andric } else {
8324b4fe385SDimitry Andric MIRBuilder.buildUndef(InputReg);
8334b4fe385SDimitry Andric }
83477fc4c14SDimitry Andric } else {
83577fc4c14SDimitry Andric // We may have proven the input wasn't needed, although the ABI is
83677fc4c14SDimitry Andric // requiring it. We just need to allocate the register appropriately.
83777fc4c14SDimitry Andric MIRBuilder.buildUndef(InputReg);
838b60736ecSDimitry Andric }
839b60736ecSDimitry Andric
840b60736ecSDimitry Andric if (OutgoingArg->isRegister()) {
841b60736ecSDimitry Andric ArgRegs.emplace_back(OutgoingArg->getRegister(), InputReg);
842b60736ecSDimitry Andric if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
843b60736ecSDimitry Andric report_fatal_error("failed to allocate implicit input argument");
844b60736ecSDimitry Andric } else {
845b60736ecSDimitry Andric LLVM_DEBUG(dbgs() << "Unhandled stack passed implicit input argument\n");
846b60736ecSDimitry Andric return false;
847b60736ecSDimitry Andric }
848b60736ecSDimitry Andric }
849b60736ecSDimitry Andric
850b60736ecSDimitry Andric // Pack workitem IDs into a single register or pass it as is if already
851b60736ecSDimitry Andric // packed.
852b60736ecSDimitry Andric const ArgDescriptor *OutgoingArg;
853b60736ecSDimitry Andric const TargetRegisterClass *ArgRC;
854b60736ecSDimitry Andric LLT ArgTy;
855b60736ecSDimitry Andric
856b60736ecSDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) =
857b60736ecSDimitry Andric CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X);
858b60736ecSDimitry Andric if (!OutgoingArg)
859b60736ecSDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) =
860b60736ecSDimitry Andric CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y);
861b60736ecSDimitry Andric if (!OutgoingArg)
862b60736ecSDimitry Andric std::tie(OutgoingArg, ArgRC, ArgTy) =
863b60736ecSDimitry Andric CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z);
864b60736ecSDimitry Andric if (!OutgoingArg)
865b60736ecSDimitry Andric return false;
866b60736ecSDimitry Andric
867b60736ecSDimitry Andric auto WorkitemIDX =
868b60736ecSDimitry Andric CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X);
869b60736ecSDimitry Andric auto WorkitemIDY =
870b60736ecSDimitry Andric CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y);
871b60736ecSDimitry Andric auto WorkitemIDZ =
872b60736ecSDimitry Andric CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z);
873b60736ecSDimitry Andric
874b60736ecSDimitry Andric const ArgDescriptor *IncomingArgX = std::get<0>(WorkitemIDX);
875b60736ecSDimitry Andric const ArgDescriptor *IncomingArgY = std::get<0>(WorkitemIDY);
876b60736ecSDimitry Andric const ArgDescriptor *IncomingArgZ = std::get<0>(WorkitemIDZ);
877b60736ecSDimitry Andric const LLT S32 = LLT::scalar(32);
878b60736ecSDimitry Andric
879c0981da4SDimitry Andric const bool NeedWorkItemIDX = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-x");
880c0981da4SDimitry Andric const bool NeedWorkItemIDY = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-y");
881c0981da4SDimitry Andric const bool NeedWorkItemIDZ = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-z");
882c0981da4SDimitry Andric
883b60736ecSDimitry Andric // If incoming ids are not packed we need to pack them.
884b60736ecSDimitry Andric // FIXME: Should consider known workgroup size to eliminate known 0 cases.
885b60736ecSDimitry Andric Register InputReg;
886c0981da4SDimitry Andric if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo->WorkItemIDX &&
887c0981da4SDimitry Andric NeedWorkItemIDX) {
8886f8fc217SDimitry Andric if (ST.getMaxWorkitemID(MF.getFunction(), 0) != 0) {
889b60736ecSDimitry Andric InputReg = MRI.createGenericVirtualRegister(S32);
890b60736ecSDimitry Andric LI->loadInputValue(InputReg, MIRBuilder, IncomingArgX,
891b60736ecSDimitry Andric std::get<1>(WorkitemIDX), std::get<2>(WorkitemIDX));
8926f8fc217SDimitry Andric } else {
8936f8fc217SDimitry Andric InputReg = MIRBuilder.buildConstant(S32, 0).getReg(0);
8946f8fc217SDimitry Andric }
895b60736ecSDimitry Andric }
896b60736ecSDimitry Andric
897c0981da4SDimitry Andric if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo->WorkItemIDY &&
8986f8fc217SDimitry Andric NeedWorkItemIDY && ST.getMaxWorkitemID(MF.getFunction(), 1) != 0) {
899b60736ecSDimitry Andric Register Y = MRI.createGenericVirtualRegister(S32);
900b60736ecSDimitry Andric LI->loadInputValue(Y, MIRBuilder, IncomingArgY, std::get<1>(WorkitemIDY),
901b60736ecSDimitry Andric std::get<2>(WorkitemIDY));
902b60736ecSDimitry Andric
903b60736ecSDimitry Andric Y = MIRBuilder.buildShl(S32, Y, MIRBuilder.buildConstant(S32, 10)).getReg(0);
904b60736ecSDimitry Andric InputReg = InputReg ? MIRBuilder.buildOr(S32, InputReg, Y).getReg(0) : Y;
905b60736ecSDimitry Andric }
906b60736ecSDimitry Andric
907c0981da4SDimitry Andric if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo->WorkItemIDZ &&
9086f8fc217SDimitry Andric NeedWorkItemIDZ && ST.getMaxWorkitemID(MF.getFunction(), 2) != 0) {
909b60736ecSDimitry Andric Register Z = MRI.createGenericVirtualRegister(S32);
910b60736ecSDimitry Andric LI->loadInputValue(Z, MIRBuilder, IncomingArgZ, std::get<1>(WorkitemIDZ),
911b60736ecSDimitry Andric std::get<2>(WorkitemIDZ));
912b60736ecSDimitry Andric
913b60736ecSDimitry Andric Z = MIRBuilder.buildShl(S32, Z, MIRBuilder.buildConstant(S32, 20)).getReg(0);
914b60736ecSDimitry Andric InputReg = InputReg ? MIRBuilder.buildOr(S32, InputReg, Z).getReg(0) : Z;
915b60736ecSDimitry Andric }
916b60736ecSDimitry Andric
9176f8fc217SDimitry Andric if (!InputReg &&
9186f8fc217SDimitry Andric (NeedWorkItemIDX || NeedWorkItemIDY || NeedWorkItemIDZ)) {
919b60736ecSDimitry Andric InputReg = MRI.createGenericVirtualRegister(S32);
9206f8fc217SDimitry Andric if (!IncomingArgX && !IncomingArgY && !IncomingArgZ) {
9216f8fc217SDimitry Andric // We're in a situation where the outgoing function requires the workitem
9226f8fc217SDimitry Andric // ID, but the calling function does not have it (e.g a graphics function
9236f8fc217SDimitry Andric // calling a C calling convention function). This is illegal, but we need
9246f8fc217SDimitry Andric // to produce something.
9256f8fc217SDimitry Andric MIRBuilder.buildUndef(InputReg);
9266f8fc217SDimitry Andric } else {
927b60736ecSDimitry Andric // Workitem ids are already packed, any of present incoming arguments will
928b60736ecSDimitry Andric // carry all required fields.
929b60736ecSDimitry Andric ArgDescriptor IncomingArg = ArgDescriptor::createArg(
930b60736ecSDimitry Andric IncomingArgX ? *IncomingArgX :
931b60736ecSDimitry Andric IncomingArgY ? *IncomingArgY : *IncomingArgZ, ~0u);
932b60736ecSDimitry Andric LI->loadInputValue(InputReg, MIRBuilder, &IncomingArg,
933b60736ecSDimitry Andric &AMDGPU::VGPR_32RegClass, S32);
934b60736ecSDimitry Andric }
9356f8fc217SDimitry Andric }
936b60736ecSDimitry Andric
937b60736ecSDimitry Andric if (OutgoingArg->isRegister()) {
938c0981da4SDimitry Andric if (InputReg)
939b60736ecSDimitry Andric ArgRegs.emplace_back(OutgoingArg->getRegister(), InputReg);
940c0981da4SDimitry Andric
941b60736ecSDimitry Andric if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
942b60736ecSDimitry Andric report_fatal_error("failed to allocate implicit input argument");
943b60736ecSDimitry Andric } else {
944b60736ecSDimitry Andric LLVM_DEBUG(dbgs() << "Unhandled stack passed implicit input argument\n");
945b60736ecSDimitry Andric return false;
946b60736ecSDimitry Andric }
947b60736ecSDimitry Andric
948b60736ecSDimitry Andric return true;
949b60736ecSDimitry Andric }
950b60736ecSDimitry Andric
951b60736ecSDimitry Andric /// Returns a pair containing the fixed CCAssignFn and the vararg CCAssignFn for
952b60736ecSDimitry Andric /// CC.
953b60736ecSDimitry Andric static std::pair<CCAssignFn *, CCAssignFn *>
getAssignFnsForCC(CallingConv::ID CC,const SITargetLowering & TLI)954b60736ecSDimitry Andric getAssignFnsForCC(CallingConv::ID CC, const SITargetLowering &TLI) {
955b60736ecSDimitry Andric return {TLI.CCAssignFnForCall(CC, false), TLI.CCAssignFnForCall(CC, true)};
956b60736ecSDimitry Andric }
957b60736ecSDimitry Andric
getCallOpcode(const MachineFunction & CallerF,bool IsIndirect,bool IsTailCall,bool isWave32,CallingConv::ID CC)958b60736ecSDimitry Andric static unsigned getCallOpcode(const MachineFunction &CallerF, bool IsIndirect,
959b1c73532SDimitry Andric bool IsTailCall, bool isWave32,
960b1c73532SDimitry Andric CallingConv::ID CC) {
961b1c73532SDimitry Andric // For calls to amdgpu_cs_chain functions, the address is known to be uniform.
962b1c73532SDimitry Andric assert((AMDGPU::isChainCC(CC) || !IsIndirect || !IsTailCall) &&
963b1c73532SDimitry Andric "Indirect calls can't be tail calls, "
964c0981da4SDimitry Andric "because the address can be divergent");
9657fa27ce4SDimitry Andric if (!IsTailCall)
9667fa27ce4SDimitry Andric return AMDGPU::G_SI_CALL;
9677fa27ce4SDimitry Andric
968b1c73532SDimitry Andric if (AMDGPU::isChainCC(CC))
969b1c73532SDimitry Andric return isWave32 ? AMDGPU::SI_CS_CHAIN_TC_W32 : AMDGPU::SI_CS_CHAIN_TC_W64;
970b1c73532SDimitry Andric
9717fa27ce4SDimitry Andric return CC == CallingConv::AMDGPU_Gfx ? AMDGPU::SI_TCRETURN_GFX :
9727fa27ce4SDimitry Andric AMDGPU::SI_TCRETURN;
973b60736ecSDimitry Andric }
974b60736ecSDimitry Andric
975b60736ecSDimitry Andric // Add operands to call instruction to track the callee.
addCallTargetOperands(MachineInstrBuilder & CallInst,MachineIRBuilder & MIRBuilder,AMDGPUCallLowering::CallLoweringInfo & Info)976b60736ecSDimitry Andric static bool addCallTargetOperands(MachineInstrBuilder &CallInst,
977b60736ecSDimitry Andric MachineIRBuilder &MIRBuilder,
978b60736ecSDimitry Andric AMDGPUCallLowering::CallLoweringInfo &Info) {
979b60736ecSDimitry Andric if (Info.Callee.isReg()) {
980b60736ecSDimitry Andric CallInst.addReg(Info.Callee.getReg());
981b60736ecSDimitry Andric CallInst.addImm(0);
982b60736ecSDimitry Andric } else if (Info.Callee.isGlobal() && Info.Callee.getOffset() == 0) {
983b60736ecSDimitry Andric // The call lowering lightly assumed we can directly encode a call target in
984b60736ecSDimitry Andric // the instruction, which is not the case. Materialize the address here.
985b60736ecSDimitry Andric const GlobalValue *GV = Info.Callee.getGlobal();
986b60736ecSDimitry Andric auto Ptr = MIRBuilder.buildGlobalValue(
987b60736ecSDimitry Andric LLT::pointer(GV->getAddressSpace(), 64), GV);
988b60736ecSDimitry Andric CallInst.addReg(Ptr.getReg(0));
989b60736ecSDimitry Andric CallInst.add(Info.Callee);
990b60736ecSDimitry Andric } else
991b60736ecSDimitry Andric return false;
992b60736ecSDimitry Andric
993b60736ecSDimitry Andric return true;
994b60736ecSDimitry Andric }
995b60736ecSDimitry Andric
doCallerAndCalleePassArgsTheSameWay(CallLoweringInfo & Info,MachineFunction & MF,SmallVectorImpl<ArgInfo> & InArgs) const996344a3780SDimitry Andric bool AMDGPUCallLowering::doCallerAndCalleePassArgsTheSameWay(
997344a3780SDimitry Andric CallLoweringInfo &Info, MachineFunction &MF,
998344a3780SDimitry Andric SmallVectorImpl<ArgInfo> &InArgs) const {
999344a3780SDimitry Andric const Function &CallerF = MF.getFunction();
1000344a3780SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv;
1001344a3780SDimitry Andric CallingConv::ID CallerCC = CallerF.getCallingConv();
1002344a3780SDimitry Andric
1003344a3780SDimitry Andric // If the calling conventions match, then everything must be the same.
1004344a3780SDimitry Andric if (CalleeCC == CallerCC)
1005344a3780SDimitry Andric return true;
1006344a3780SDimitry Andric
1007344a3780SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1008344a3780SDimitry Andric
1009344a3780SDimitry Andric // Make sure that the caller and callee preserve all of the same registers.
1010344a3780SDimitry Andric auto TRI = ST.getRegisterInfo();
1011344a3780SDimitry Andric
1012344a3780SDimitry Andric const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1013344a3780SDimitry Andric const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
1014344a3780SDimitry Andric if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
1015344a3780SDimitry Andric return false;
1016344a3780SDimitry Andric
1017344a3780SDimitry Andric // Check if the caller and callee will handle arguments in the same way.
1018344a3780SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
1019344a3780SDimitry Andric CCAssignFn *CalleeAssignFnFixed;
1020344a3780SDimitry Andric CCAssignFn *CalleeAssignFnVarArg;
1021344a3780SDimitry Andric std::tie(CalleeAssignFnFixed, CalleeAssignFnVarArg) =
1022344a3780SDimitry Andric getAssignFnsForCC(CalleeCC, TLI);
1023344a3780SDimitry Andric
1024344a3780SDimitry Andric CCAssignFn *CallerAssignFnFixed;
1025344a3780SDimitry Andric CCAssignFn *CallerAssignFnVarArg;
1026344a3780SDimitry Andric std::tie(CallerAssignFnFixed, CallerAssignFnVarArg) =
1027344a3780SDimitry Andric getAssignFnsForCC(CallerCC, TLI);
1028344a3780SDimitry Andric
1029344a3780SDimitry Andric // FIXME: We are not accounting for potential differences in implicitly passed
1030344a3780SDimitry Andric // inputs, but only the fixed ABI is supported now anyway.
1031344a3780SDimitry Andric IncomingValueAssigner CalleeAssigner(CalleeAssignFnFixed,
1032344a3780SDimitry Andric CalleeAssignFnVarArg);
1033344a3780SDimitry Andric IncomingValueAssigner CallerAssigner(CallerAssignFnFixed,
1034344a3780SDimitry Andric CallerAssignFnVarArg);
1035344a3780SDimitry Andric return resultsCompatible(Info, MF, InArgs, CalleeAssigner, CallerAssigner);
1036344a3780SDimitry Andric }
1037344a3780SDimitry Andric
areCalleeOutgoingArgsTailCallable(CallLoweringInfo & Info,MachineFunction & MF,SmallVectorImpl<ArgInfo> & OutArgs) const1038344a3780SDimitry Andric bool AMDGPUCallLowering::areCalleeOutgoingArgsTailCallable(
1039344a3780SDimitry Andric CallLoweringInfo &Info, MachineFunction &MF,
1040344a3780SDimitry Andric SmallVectorImpl<ArgInfo> &OutArgs) const {
1041344a3780SDimitry Andric // If there are no outgoing arguments, then we are done.
1042344a3780SDimitry Andric if (OutArgs.empty())
1043344a3780SDimitry Andric return true;
1044344a3780SDimitry Andric
1045344a3780SDimitry Andric const Function &CallerF = MF.getFunction();
1046344a3780SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv;
1047344a3780SDimitry Andric CallingConv::ID CallerCC = CallerF.getCallingConv();
1048344a3780SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
1049344a3780SDimitry Andric
1050344a3780SDimitry Andric CCAssignFn *AssignFnFixed;
1051344a3780SDimitry Andric CCAssignFn *AssignFnVarArg;
1052344a3780SDimitry Andric std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI);
1053344a3780SDimitry Andric
1054344a3780SDimitry Andric // We have outgoing arguments. Make sure that we can tail call with them.
1055344a3780SDimitry Andric SmallVector<CCValAssign, 16> OutLocs;
1056344a3780SDimitry Andric CCState OutInfo(CalleeCC, false, MF, OutLocs, CallerF.getContext());
1057344a3780SDimitry Andric OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg);
1058344a3780SDimitry Andric
1059344a3780SDimitry Andric if (!determineAssignments(Assigner, OutArgs, OutInfo)) {
1060344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "... Could not analyze call operands.\n");
1061344a3780SDimitry Andric return false;
1062344a3780SDimitry Andric }
1063344a3780SDimitry Andric
1064344a3780SDimitry Andric // Make sure that they can fit on the caller's stack.
1065344a3780SDimitry Andric const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
10667fa27ce4SDimitry Andric if (OutInfo.getStackSize() > FuncInfo->getBytesInStackArgArea()) {
1067344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n");
1068344a3780SDimitry Andric return false;
1069344a3780SDimitry Andric }
1070344a3780SDimitry Andric
1071344a3780SDimitry Andric // Verify that the parameters in callee-saved registers match.
1072344a3780SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1073344a3780SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo();
1074344a3780SDimitry Andric const uint32_t *CallerPreservedMask = TRI->getCallPreservedMask(MF, CallerCC);
1075344a3780SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
1076344a3780SDimitry Andric return parametersInCSRMatch(MRI, CallerPreservedMask, OutLocs, OutArgs);
1077344a3780SDimitry Andric }
1078344a3780SDimitry Andric
1079344a3780SDimitry Andric /// Return true if the calling convention is one that we can guarantee TCO for.
canGuaranteeTCO(CallingConv::ID CC)1080344a3780SDimitry Andric static bool canGuaranteeTCO(CallingConv::ID CC) {
1081344a3780SDimitry Andric return CC == CallingConv::Fast;
1082344a3780SDimitry Andric }
1083344a3780SDimitry Andric
1084344a3780SDimitry Andric /// Return true if we might ever do TCO for calls with this calling convention.
mayTailCallThisCC(CallingConv::ID CC)1085344a3780SDimitry Andric static bool mayTailCallThisCC(CallingConv::ID CC) {
1086344a3780SDimitry Andric switch (CC) {
1087344a3780SDimitry Andric case CallingConv::C:
1088344a3780SDimitry Andric case CallingConv::AMDGPU_Gfx:
1089344a3780SDimitry Andric return true;
1090344a3780SDimitry Andric default:
1091344a3780SDimitry Andric return canGuaranteeTCO(CC);
1092344a3780SDimitry Andric }
1093344a3780SDimitry Andric }
1094344a3780SDimitry Andric
isEligibleForTailCallOptimization(MachineIRBuilder & B,CallLoweringInfo & Info,SmallVectorImpl<ArgInfo> & InArgs,SmallVectorImpl<ArgInfo> & OutArgs) const1095344a3780SDimitry Andric bool AMDGPUCallLowering::isEligibleForTailCallOptimization(
1096344a3780SDimitry Andric MachineIRBuilder &B, CallLoweringInfo &Info,
1097344a3780SDimitry Andric SmallVectorImpl<ArgInfo> &InArgs, SmallVectorImpl<ArgInfo> &OutArgs) const {
1098344a3780SDimitry Andric // Must pass all target-independent checks in order to tail call optimize.
1099344a3780SDimitry Andric if (!Info.IsTailCall)
1100344a3780SDimitry Andric return false;
1101344a3780SDimitry Andric
1102c0981da4SDimitry Andric // Indirect calls can't be tail calls, because the address can be divergent.
1103c0981da4SDimitry Andric // TODO Check divergence info if the call really is divergent.
1104c0981da4SDimitry Andric if (Info.Callee.isReg())
1105c0981da4SDimitry Andric return false;
1106c0981da4SDimitry Andric
1107344a3780SDimitry Andric MachineFunction &MF = B.getMF();
1108344a3780SDimitry Andric const Function &CallerF = MF.getFunction();
1109344a3780SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv;
1110344a3780SDimitry Andric CallingConv::ID CallerCC = CallerF.getCallingConv();
1111344a3780SDimitry Andric
1112344a3780SDimitry Andric const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
1113344a3780SDimitry Andric const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1114344a3780SDimitry Andric // Kernels aren't callable, and don't have a live in return address so it
1115344a3780SDimitry Andric // doesn't make sense to do a tail call with entry functions.
1116344a3780SDimitry Andric if (!CallerPreserved)
1117344a3780SDimitry Andric return false;
1118344a3780SDimitry Andric
1119344a3780SDimitry Andric if (!mayTailCallThisCC(CalleeCC)) {
1120344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "... Calling convention cannot be tail called.\n");
1121344a3780SDimitry Andric return false;
1122344a3780SDimitry Andric }
1123344a3780SDimitry Andric
1124344a3780SDimitry Andric if (any_of(CallerF.args(), [](const Argument &A) {
1125344a3780SDimitry Andric return A.hasByValAttr() || A.hasSwiftErrorAttr();
1126344a3780SDimitry Andric })) {
1127344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "... Cannot tail call from callers with byval "
1128344a3780SDimitry Andric "or swifterror arguments\n");
1129344a3780SDimitry Andric return false;
1130344a3780SDimitry Andric }
1131344a3780SDimitry Andric
1132344a3780SDimitry Andric // If we have -tailcallopt, then we're done.
1133344a3780SDimitry Andric if (MF.getTarget().Options.GuaranteedTailCallOpt)
1134344a3780SDimitry Andric return canGuaranteeTCO(CalleeCC) && CalleeCC == CallerF.getCallingConv();
1135344a3780SDimitry Andric
1136344a3780SDimitry Andric // Verify that the incoming and outgoing arguments from the callee are
1137344a3780SDimitry Andric // safe to tail call.
1138344a3780SDimitry Andric if (!doCallerAndCalleePassArgsTheSameWay(Info, MF, InArgs)) {
1139344a3780SDimitry Andric LLVM_DEBUG(
1140344a3780SDimitry Andric dbgs()
1141344a3780SDimitry Andric << "... Caller and callee have incompatible calling conventions.\n");
1142344a3780SDimitry Andric return false;
1143344a3780SDimitry Andric }
1144344a3780SDimitry Andric
1145344a3780SDimitry Andric if (!areCalleeOutgoingArgsTailCallable(Info, MF, OutArgs))
1146344a3780SDimitry Andric return false;
1147344a3780SDimitry Andric
1148344a3780SDimitry Andric LLVM_DEBUG(dbgs() << "... Call is eligible for tail call optimization.\n");
1149344a3780SDimitry Andric return true;
1150344a3780SDimitry Andric }
1151344a3780SDimitry Andric
1152344a3780SDimitry Andric // Insert outgoing implicit arguments for a call, by inserting copies to the
1153344a3780SDimitry Andric // implicit argument registers and adding the necessary implicit uses to the
1154344a3780SDimitry Andric // call instruction.
handleImplicitCallArguments(MachineIRBuilder & MIRBuilder,MachineInstrBuilder & CallInst,const GCNSubtarget & ST,const SIMachineFunctionInfo & FuncInfo,CallingConv::ID CalleeCC,ArrayRef<std::pair<MCRegister,Register>> ImplicitArgRegs) const1155344a3780SDimitry Andric void AMDGPUCallLowering::handleImplicitCallArguments(
1156344a3780SDimitry Andric MachineIRBuilder &MIRBuilder, MachineInstrBuilder &CallInst,
1157344a3780SDimitry Andric const GCNSubtarget &ST, const SIMachineFunctionInfo &FuncInfo,
1158b1c73532SDimitry Andric CallingConv::ID CalleeCC,
1159344a3780SDimitry Andric ArrayRef<std::pair<MCRegister, Register>> ImplicitArgRegs) const {
1160344a3780SDimitry Andric if (!ST.enableFlatScratch()) {
1161344a3780SDimitry Andric // Insert copies for the SRD. In the HSA case, this should be an identity
1162344a3780SDimitry Andric // copy.
1163344a3780SDimitry Andric auto ScratchRSrcReg = MIRBuilder.buildCopy(LLT::fixed_vector(4, 32),
1164344a3780SDimitry Andric FuncInfo.getScratchRSrcReg());
1165b1c73532SDimitry Andric
1166b1c73532SDimitry Andric auto CalleeRSrcReg = AMDGPU::isChainCC(CalleeCC)
1167b1c73532SDimitry Andric ? AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51
1168b1c73532SDimitry Andric : AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3;
1169b1c73532SDimitry Andric
1170b1c73532SDimitry Andric MIRBuilder.buildCopy(CalleeRSrcReg, ScratchRSrcReg);
1171b1c73532SDimitry Andric CallInst.addReg(CalleeRSrcReg, RegState::Implicit);
1172344a3780SDimitry Andric }
1173344a3780SDimitry Andric
1174344a3780SDimitry Andric for (std::pair<MCRegister, Register> ArgReg : ImplicitArgRegs) {
1175344a3780SDimitry Andric MIRBuilder.buildCopy((Register)ArgReg.first, ArgReg.second);
1176344a3780SDimitry Andric CallInst.addReg(ArgReg.first, RegState::Implicit);
1177344a3780SDimitry Andric }
1178344a3780SDimitry Andric }
1179344a3780SDimitry Andric
lowerTailCall(MachineIRBuilder & MIRBuilder,CallLoweringInfo & Info,SmallVectorImpl<ArgInfo> & OutArgs) const1180344a3780SDimitry Andric bool AMDGPUCallLowering::lowerTailCall(
1181344a3780SDimitry Andric MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info,
1182344a3780SDimitry Andric SmallVectorImpl<ArgInfo> &OutArgs) const {
1183344a3780SDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
1184344a3780SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1185344a3780SDimitry Andric SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
1186344a3780SDimitry Andric const Function &F = MF.getFunction();
1187344a3780SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
1188344a3780SDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
1189344a3780SDimitry Andric
1190344a3780SDimitry Andric // True when we're tail calling, but without -tailcallopt.
1191344a3780SDimitry Andric bool IsSibCall = !MF.getTarget().Options.GuaranteedTailCallOpt;
1192344a3780SDimitry Andric
1193344a3780SDimitry Andric // Find out which ABI gets to decide where things go.
1194344a3780SDimitry Andric CallingConv::ID CalleeCC = Info.CallConv;
1195344a3780SDimitry Andric CCAssignFn *AssignFnFixed;
1196344a3780SDimitry Andric CCAssignFn *AssignFnVarArg;
1197344a3780SDimitry Andric std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI);
1198344a3780SDimitry Andric
1199344a3780SDimitry Andric MachineInstrBuilder CallSeqStart;
1200344a3780SDimitry Andric if (!IsSibCall)
1201344a3780SDimitry Andric CallSeqStart = MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKUP);
1202344a3780SDimitry Andric
1203b1c73532SDimitry Andric unsigned Opc =
1204b1c73532SDimitry Andric getCallOpcode(MF, Info.Callee.isReg(), true, ST.isWave32(), CalleeCC);
1205344a3780SDimitry Andric auto MIB = MIRBuilder.buildInstrNoInsert(Opc);
1206344a3780SDimitry Andric if (!addCallTargetOperands(MIB, MIRBuilder, Info))
1207344a3780SDimitry Andric return false;
1208344a3780SDimitry Andric
1209344a3780SDimitry Andric // Byte offset for the tail call. When we are sibcalling, this will always
1210344a3780SDimitry Andric // be 0.
1211344a3780SDimitry Andric MIB.addImm(0);
1212344a3780SDimitry Andric
1213b1c73532SDimitry Andric // If this is a chain call, we need to pass in the EXEC mask.
1214344a3780SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo();
1215b1c73532SDimitry Andric if (AMDGPU::isChainCC(Info.CallConv)) {
1216b1c73532SDimitry Andric ArgInfo ExecArg = Info.OrigArgs[1];
1217b1c73532SDimitry Andric assert(ExecArg.Regs.size() == 1 && "Too many regs for EXEC");
1218b1c73532SDimitry Andric
1219b1c73532SDimitry Andric if (!ExecArg.Ty->isIntegerTy(ST.getWavefrontSize()))
1220b1c73532SDimitry Andric return false;
1221b1c73532SDimitry Andric
1222b1c73532SDimitry Andric if (auto CI = dyn_cast<ConstantInt>(ExecArg.OrigValue)) {
1223b1c73532SDimitry Andric MIB.addImm(CI->getSExtValue());
1224b1c73532SDimitry Andric } else {
1225b1c73532SDimitry Andric MIB.addReg(ExecArg.Regs[0]);
1226b1c73532SDimitry Andric unsigned Idx = MIB->getNumOperands() - 1;
1227b1c73532SDimitry Andric MIB->getOperand(Idx).setReg(constrainOperandRegClass(
1228b1c73532SDimitry Andric MF, *TRI, MRI, *ST.getInstrInfo(), *ST.getRegBankInfo(), *MIB,
1229b1c73532SDimitry Andric MIB->getDesc(), MIB->getOperand(Idx), Idx));
1230b1c73532SDimitry Andric }
1231b1c73532SDimitry Andric }
1232b1c73532SDimitry Andric
1233b1c73532SDimitry Andric // Tell the call which registers are clobbered.
1234344a3780SDimitry Andric const uint32_t *Mask = TRI->getCallPreservedMask(MF, CalleeCC);
1235344a3780SDimitry Andric MIB.addRegMask(Mask);
1236344a3780SDimitry Andric
1237344a3780SDimitry Andric // FPDiff is the byte offset of the call's argument area from the callee's.
1238344a3780SDimitry Andric // Stores to callee stack arguments will be placed in FixedStackSlots offset
1239344a3780SDimitry Andric // by this amount for a tail call. In a sibling call it must be 0 because the
1240344a3780SDimitry Andric // caller will deallocate the entire stack and the callee still expects its
1241344a3780SDimitry Andric // arguments to begin at SP+0.
1242344a3780SDimitry Andric int FPDiff = 0;
1243344a3780SDimitry Andric
1244344a3780SDimitry Andric // This will be 0 for sibcalls, potentially nonzero for tail calls produced
1245344a3780SDimitry Andric // by -tailcallopt. For sibcalls, the memory operands for the call are
1246344a3780SDimitry Andric // already available in the caller's incoming argument space.
1247344a3780SDimitry Andric unsigned NumBytes = 0;
1248344a3780SDimitry Andric if (!IsSibCall) {
1249344a3780SDimitry Andric // We aren't sibcalling, so we need to compute FPDiff. We need to do this
1250344a3780SDimitry Andric // before handling assignments, because FPDiff must be known for memory
1251344a3780SDimitry Andric // arguments.
1252344a3780SDimitry Andric unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
1253344a3780SDimitry Andric SmallVector<CCValAssign, 16> OutLocs;
1254344a3780SDimitry Andric CCState OutInfo(CalleeCC, false, MF, OutLocs, F.getContext());
1255344a3780SDimitry Andric
1256344a3780SDimitry Andric // FIXME: Not accounting for callee implicit inputs
1257344a3780SDimitry Andric OutgoingValueAssigner CalleeAssigner(AssignFnFixed, AssignFnVarArg);
1258344a3780SDimitry Andric if (!determineAssignments(CalleeAssigner, OutArgs, OutInfo))
1259344a3780SDimitry Andric return false;
1260344a3780SDimitry Andric
1261344a3780SDimitry Andric // The callee will pop the argument stack as a tail call. Thus, we must
1262344a3780SDimitry Andric // keep it 16-byte aligned.
12637fa27ce4SDimitry Andric NumBytes = alignTo(OutInfo.getStackSize(), ST.getStackAlignment());
1264344a3780SDimitry Andric
1265344a3780SDimitry Andric // FPDiff will be negative if this tail call requires more space than we
1266344a3780SDimitry Andric // would automatically have in our incoming argument space. Positive if we
1267344a3780SDimitry Andric // actually shrink the stack.
1268344a3780SDimitry Andric FPDiff = NumReusableBytes - NumBytes;
1269344a3780SDimitry Andric
1270344a3780SDimitry Andric // The stack pointer must be 16-byte aligned at all times it's used for a
1271344a3780SDimitry Andric // memory operation, which in practice means at *all* times and in
1272344a3780SDimitry Andric // particular across call boundaries. Therefore our own arguments started at
1273344a3780SDimitry Andric // a 16-byte aligned SP and the delta applied for the tail call should
1274344a3780SDimitry Andric // satisfy the same constraint.
1275344a3780SDimitry Andric assert(isAligned(ST.getStackAlignment(), FPDiff) &&
1276344a3780SDimitry Andric "unaligned stack on tail call");
1277344a3780SDimitry Andric }
1278344a3780SDimitry Andric
1279344a3780SDimitry Andric SmallVector<CCValAssign, 16> ArgLocs;
1280344a3780SDimitry Andric CCState CCInfo(Info.CallConv, Info.IsVarArg, MF, ArgLocs, F.getContext());
1281344a3780SDimitry Andric
1282344a3780SDimitry Andric // We could pass MIB and directly add the implicit uses to the call
1283344a3780SDimitry Andric // now. However, as an aesthetic choice, place implicit argument operands
1284344a3780SDimitry Andric // after the ordinary user argument registers.
1285344a3780SDimitry Andric SmallVector<std::pair<MCRegister, Register>, 12> ImplicitArgRegs;
1286344a3780SDimitry Andric
1287b1c73532SDimitry Andric if (Info.CallConv != CallingConv::AMDGPU_Gfx &&
1288b1c73532SDimitry Andric !AMDGPU::isChainCC(Info.CallConv)) {
1289344a3780SDimitry Andric // With a fixed ABI, allocate fixed registers before user arguments.
1290344a3780SDimitry Andric if (!passSpecialInputs(MIRBuilder, CCInfo, ImplicitArgRegs, Info))
1291344a3780SDimitry Andric return false;
1292344a3780SDimitry Andric }
1293344a3780SDimitry Andric
1294344a3780SDimitry Andric OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg);
1295344a3780SDimitry Andric
1296344a3780SDimitry Andric if (!determineAssignments(Assigner, OutArgs, CCInfo))
1297344a3780SDimitry Andric return false;
1298344a3780SDimitry Andric
1299344a3780SDimitry Andric // Do the actual argument marshalling.
1300344a3780SDimitry Andric AMDGPUOutgoingArgHandler Handler(MIRBuilder, MRI, MIB, true, FPDiff);
1301344a3780SDimitry Andric if (!handleAssignments(Handler, OutArgs, CCInfo, ArgLocs, MIRBuilder))
1302344a3780SDimitry Andric return false;
1303344a3780SDimitry Andric
1304ac9a064cSDimitry Andric if (Info.ConvergenceCtrlToken) {
1305ac9a064cSDimitry Andric MIB.addUse(Info.ConvergenceCtrlToken, RegState::Implicit);
1306ac9a064cSDimitry Andric }
1307b1c73532SDimitry Andric handleImplicitCallArguments(MIRBuilder, MIB, ST, *FuncInfo, CalleeCC,
1308b1c73532SDimitry Andric ImplicitArgRegs);
1309344a3780SDimitry Andric
1310344a3780SDimitry Andric // If we have -tailcallopt, we need to adjust the stack. We'll do the call
1311344a3780SDimitry Andric // sequence start and end here.
1312344a3780SDimitry Andric if (!IsSibCall) {
1313344a3780SDimitry Andric MIB->getOperand(1).setImm(FPDiff);
1314344a3780SDimitry Andric CallSeqStart.addImm(NumBytes).addImm(0);
1315344a3780SDimitry Andric // End the call sequence *before* emitting the call. Normally, we would
1316344a3780SDimitry Andric // tidy the frame up after the call. However, here, we've laid out the
1317344a3780SDimitry Andric // parameters so that when SP is reset, they will be in the correct
1318344a3780SDimitry Andric // location.
1319344a3780SDimitry Andric MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKDOWN).addImm(NumBytes).addImm(0);
1320344a3780SDimitry Andric }
1321344a3780SDimitry Andric
1322344a3780SDimitry Andric // Now we can add the actual call instruction to the correct basic block.
1323344a3780SDimitry Andric MIRBuilder.insertInstr(MIB);
1324344a3780SDimitry Andric
1325344a3780SDimitry Andric // If Callee is a reg, since it is used by a target specific
1326344a3780SDimitry Andric // instruction, it must have a register class matching the
1327344a3780SDimitry Andric // constraint of that instruction.
1328344a3780SDimitry Andric
1329344a3780SDimitry Andric // FIXME: We should define regbankselectable call instructions to handle
1330344a3780SDimitry Andric // divergent call targets.
1331344a3780SDimitry Andric if (MIB->getOperand(0).isReg()) {
1332344a3780SDimitry Andric MIB->getOperand(0).setReg(constrainOperandRegClass(
1333344a3780SDimitry Andric MF, *TRI, MRI, *ST.getInstrInfo(), *ST.getRegBankInfo(), *MIB,
1334344a3780SDimitry Andric MIB->getDesc(), MIB->getOperand(0), 0));
1335344a3780SDimitry Andric }
1336344a3780SDimitry Andric
1337344a3780SDimitry Andric MF.getFrameInfo().setHasTailCall();
1338344a3780SDimitry Andric Info.LoweredTailCall = true;
1339344a3780SDimitry Andric return true;
1340344a3780SDimitry Andric }
1341344a3780SDimitry Andric
1342b1c73532SDimitry Andric /// Lower a call to the @llvm.amdgcn.cs.chain intrinsic.
lowerChainCall(MachineIRBuilder & MIRBuilder,CallLoweringInfo & Info) const1343b1c73532SDimitry Andric bool AMDGPUCallLowering::lowerChainCall(MachineIRBuilder &MIRBuilder,
1344b1c73532SDimitry Andric CallLoweringInfo &Info) const {
1345b1c73532SDimitry Andric ArgInfo Callee = Info.OrigArgs[0];
1346b1c73532SDimitry Andric ArgInfo SGPRArgs = Info.OrigArgs[2];
1347b1c73532SDimitry Andric ArgInfo VGPRArgs = Info.OrigArgs[3];
1348b1c73532SDimitry Andric ArgInfo Flags = Info.OrigArgs[4];
1349b1c73532SDimitry Andric
1350b1c73532SDimitry Andric assert(cast<ConstantInt>(Flags.OrigValue)->isZero() &&
1351b1c73532SDimitry Andric "Non-zero flags aren't supported yet.");
1352b1c73532SDimitry Andric assert(Info.OrigArgs.size() == 5 && "Additional args aren't supported yet.");
1353b1c73532SDimitry Andric
1354b1c73532SDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
1355b1c73532SDimitry Andric const Function &F = MF.getFunction();
1356ac9a064cSDimitry Andric const DataLayout &DL = F.getDataLayout();
1357b1c73532SDimitry Andric
1358b1c73532SDimitry Andric // The function to jump to is actually the first argument, so we'll change the
1359b1c73532SDimitry Andric // Callee and other info to match that before using our existing helper.
1360b1c73532SDimitry Andric const Value *CalleeV = Callee.OrigValue->stripPointerCasts();
1361b1c73532SDimitry Andric if (const Function *F = dyn_cast<Function>(CalleeV)) {
1362b1c73532SDimitry Andric Info.Callee = MachineOperand::CreateGA(F, 0);
1363b1c73532SDimitry Andric Info.CallConv = F->getCallingConv();
1364b1c73532SDimitry Andric } else {
1365b1c73532SDimitry Andric assert(Callee.Regs.size() == 1 && "Too many regs for the callee");
1366b1c73532SDimitry Andric Info.Callee = MachineOperand::CreateReg(Callee.Regs[0], false);
1367b1c73532SDimitry Andric Info.CallConv = CallingConv::AMDGPU_CS_Chain; // amdgpu_cs_chain_preserve
1368b1c73532SDimitry Andric // behaves the same here.
1369b1c73532SDimitry Andric }
1370b1c73532SDimitry Andric
1371b1c73532SDimitry Andric // The function that we're calling cannot be vararg (only the intrinsic is).
1372b1c73532SDimitry Andric Info.IsVarArg = false;
1373b1c73532SDimitry Andric
1374b1c73532SDimitry Andric assert(std::all_of(SGPRArgs.Flags.begin(), SGPRArgs.Flags.end(),
1375b1c73532SDimitry Andric [](ISD::ArgFlagsTy F) { return F.isInReg(); }) &&
1376b1c73532SDimitry Andric "SGPR arguments should be marked inreg");
1377b1c73532SDimitry Andric assert(std::none_of(VGPRArgs.Flags.begin(), VGPRArgs.Flags.end(),
1378b1c73532SDimitry Andric [](ISD::ArgFlagsTy F) { return F.isInReg(); }) &&
1379b1c73532SDimitry Andric "VGPR arguments should not be marked inreg");
1380b1c73532SDimitry Andric
1381b1c73532SDimitry Andric SmallVector<ArgInfo, 8> OutArgs;
1382b1c73532SDimitry Andric splitToValueTypes(SGPRArgs, OutArgs, DL, Info.CallConv);
1383b1c73532SDimitry Andric splitToValueTypes(VGPRArgs, OutArgs, DL, Info.CallConv);
1384b1c73532SDimitry Andric
1385b1c73532SDimitry Andric Info.IsMustTailCall = true;
1386b1c73532SDimitry Andric return lowerTailCall(MIRBuilder, Info, OutArgs);
1387b1c73532SDimitry Andric }
1388b1c73532SDimitry Andric
lowerCall(MachineIRBuilder & MIRBuilder,CallLoweringInfo & Info) const1389b60736ecSDimitry Andric bool AMDGPUCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
1390b60736ecSDimitry Andric CallLoweringInfo &Info) const {
1391b1c73532SDimitry Andric if (Function *F = Info.CB->getCalledFunction())
1392b1c73532SDimitry Andric if (F->isIntrinsic()) {
1393b1c73532SDimitry Andric assert(F->getIntrinsicID() == Intrinsic::amdgcn_cs_chain &&
1394b1c73532SDimitry Andric "Unexpected intrinsic");
1395b1c73532SDimitry Andric return lowerChainCall(MIRBuilder, Info);
1396b1c73532SDimitry Andric }
1397b1c73532SDimitry Andric
1398b60736ecSDimitry Andric if (Info.IsVarArg) {
1399b60736ecSDimitry Andric LLVM_DEBUG(dbgs() << "Variadic functions not implemented\n");
1400b60736ecSDimitry Andric return false;
1401b60736ecSDimitry Andric }
1402b60736ecSDimitry Andric
1403b60736ecSDimitry Andric MachineFunction &MF = MIRBuilder.getMF();
1404b60736ecSDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1405b60736ecSDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo();
1406b60736ecSDimitry Andric
1407b60736ecSDimitry Andric const Function &F = MF.getFunction();
1408b60736ecSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
1409b60736ecSDimitry Andric const SITargetLowering &TLI = *getTLI<SITargetLowering>();
1410ac9a064cSDimitry Andric const DataLayout &DL = F.getDataLayout();
1411b60736ecSDimitry Andric
1412b60736ecSDimitry Andric SmallVector<ArgInfo, 8> OutArgs;
1413344a3780SDimitry Andric for (auto &OrigArg : Info.OrigArgs)
1414344a3780SDimitry Andric splitToValueTypes(OrigArg, OutArgs, DL, Info.CallConv);
1415b60736ecSDimitry Andric
1416344a3780SDimitry Andric SmallVector<ArgInfo, 8> InArgs;
1417344a3780SDimitry Andric if (Info.CanLowerReturn && !Info.OrigRet.Ty->isVoidTy())
1418344a3780SDimitry Andric splitToValueTypes(Info.OrigRet, InArgs, DL, Info.CallConv);
1419b60736ecSDimitry Andric
1420b60736ecSDimitry Andric // If we can lower as a tail call, do that instead.
1421344a3780SDimitry Andric bool CanTailCallOpt =
1422344a3780SDimitry Andric isEligibleForTailCallOptimization(MIRBuilder, Info, InArgs, OutArgs);
1423b60736ecSDimitry Andric
1424b60736ecSDimitry Andric // We must emit a tail call if we have musttail.
1425b60736ecSDimitry Andric if (Info.IsMustTailCall && !CanTailCallOpt) {
1426b60736ecSDimitry Andric LLVM_DEBUG(dbgs() << "Failed to lower musttail call as tail call\n");
1427b60736ecSDimitry Andric return false;
1428b60736ecSDimitry Andric }
1429b60736ecSDimitry Andric
14306f8fc217SDimitry Andric Info.IsTailCall = CanTailCallOpt;
1431344a3780SDimitry Andric if (CanTailCallOpt)
1432344a3780SDimitry Andric return lowerTailCall(MIRBuilder, Info, OutArgs);
1433344a3780SDimitry Andric
1434b60736ecSDimitry Andric // Find out which ABI gets to decide where things go.
1435b60736ecSDimitry Andric CCAssignFn *AssignFnFixed;
1436b60736ecSDimitry Andric CCAssignFn *AssignFnVarArg;
1437b60736ecSDimitry Andric std::tie(AssignFnFixed, AssignFnVarArg) =
1438b60736ecSDimitry Andric getAssignFnsForCC(Info.CallConv, TLI);
1439b60736ecSDimitry Andric
1440b60736ecSDimitry Andric MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKUP)
1441b60736ecSDimitry Andric .addImm(0)
1442b60736ecSDimitry Andric .addImm(0);
1443b60736ecSDimitry Andric
1444b60736ecSDimitry Andric // Create a temporarily-floating call instruction so we can add the implicit
1445b60736ecSDimitry Andric // uses of arg registers.
1446b1c73532SDimitry Andric unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), false, ST.isWave32(),
1447b1c73532SDimitry Andric Info.CallConv);
1448b60736ecSDimitry Andric
1449b60736ecSDimitry Andric auto MIB = MIRBuilder.buildInstrNoInsert(Opc);
1450b60736ecSDimitry Andric MIB.addDef(TRI->getReturnAddressReg(MF));
1451b60736ecSDimitry Andric
1452b1c73532SDimitry Andric if (!Info.IsConvergent)
1453b1c73532SDimitry Andric MIB.setMIFlag(MachineInstr::NoConvergent);
1454b1c73532SDimitry Andric
1455b60736ecSDimitry Andric if (!addCallTargetOperands(MIB, MIRBuilder, Info))
1456b60736ecSDimitry Andric return false;
1457b60736ecSDimitry Andric
1458b60736ecSDimitry Andric // Tell the call which registers are clobbered.
1459b60736ecSDimitry Andric const uint32_t *Mask = TRI->getCallPreservedMask(MF, Info.CallConv);
1460b60736ecSDimitry Andric MIB.addRegMask(Mask);
1461b60736ecSDimitry Andric
1462b60736ecSDimitry Andric SmallVector<CCValAssign, 16> ArgLocs;
1463b60736ecSDimitry Andric CCState CCInfo(Info.CallConv, Info.IsVarArg, MF, ArgLocs, F.getContext());
1464b60736ecSDimitry Andric
1465b60736ecSDimitry Andric // We could pass MIB and directly add the implicit uses to the call
1466b60736ecSDimitry Andric // now. However, as an aesthetic choice, place implicit argument operands
1467b60736ecSDimitry Andric // after the ordinary user argument registers.
1468b60736ecSDimitry Andric SmallVector<std::pair<MCRegister, Register>, 12> ImplicitArgRegs;
1469b60736ecSDimitry Andric
147077fc4c14SDimitry Andric if (Info.CallConv != CallingConv::AMDGPU_Gfx) {
1471b60736ecSDimitry Andric // With a fixed ABI, allocate fixed registers before user arguments.
1472b60736ecSDimitry Andric if (!passSpecialInputs(MIRBuilder, CCInfo, ImplicitArgRegs, Info))
1473b60736ecSDimitry Andric return false;
1474b60736ecSDimitry Andric }
1475b60736ecSDimitry Andric
1476b60736ecSDimitry Andric // Do the actual argument marshalling.
1477b60736ecSDimitry Andric SmallVector<Register, 8> PhysRegs;
1478344a3780SDimitry Andric
1479344a3780SDimitry Andric OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg);
1480344a3780SDimitry Andric if (!determineAssignments(Assigner, OutArgs, CCInfo))
1481344a3780SDimitry Andric return false;
1482344a3780SDimitry Andric
1483344a3780SDimitry Andric AMDGPUOutgoingArgHandler Handler(MIRBuilder, MRI, MIB, false);
1484344a3780SDimitry Andric if (!handleAssignments(Handler, OutArgs, CCInfo, ArgLocs, MIRBuilder))
1485b60736ecSDimitry Andric return false;
1486b60736ecSDimitry Andric
1487b60736ecSDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1488b60736ecSDimitry Andric
1489ac9a064cSDimitry Andric if (Info.ConvergenceCtrlToken) {
1490ac9a064cSDimitry Andric MIB.addUse(Info.ConvergenceCtrlToken, RegState::Implicit);
1491ac9a064cSDimitry Andric }
1492b1c73532SDimitry Andric handleImplicitCallArguments(MIRBuilder, MIB, ST, *MFI, Info.CallConv,
1493b1c73532SDimitry Andric ImplicitArgRegs);
1494b60736ecSDimitry Andric
1495b60736ecSDimitry Andric // Get a count of how many bytes are to be pushed on the stack.
14967fa27ce4SDimitry Andric unsigned NumBytes = CCInfo.getStackSize();
1497b60736ecSDimitry Andric
1498b60736ecSDimitry Andric // If Callee is a reg, since it is used by a target specific
1499b60736ecSDimitry Andric // instruction, it must have a register class matching the
1500b60736ecSDimitry Andric // constraint of that instruction.
1501b60736ecSDimitry Andric
1502b60736ecSDimitry Andric // FIXME: We should define regbankselectable call instructions to handle
1503b60736ecSDimitry Andric // divergent call targets.
1504b60736ecSDimitry Andric if (MIB->getOperand(1).isReg()) {
1505b60736ecSDimitry Andric MIB->getOperand(1).setReg(constrainOperandRegClass(
1506b60736ecSDimitry Andric MF, *TRI, MRI, *ST.getInstrInfo(),
1507b60736ecSDimitry Andric *ST.getRegBankInfo(), *MIB, MIB->getDesc(), MIB->getOperand(1),
1508b60736ecSDimitry Andric 1));
1509b60736ecSDimitry Andric }
1510b60736ecSDimitry Andric
1511b60736ecSDimitry Andric // Now we can add the actual call instruction to the correct position.
1512b60736ecSDimitry Andric MIRBuilder.insertInstr(MIB);
1513b60736ecSDimitry Andric
1514b60736ecSDimitry Andric // Finally we can copy the returned value back into its virtual-register. In
1515b60736ecSDimitry Andric // symmetry with the arguments, the physical register must be an
1516b60736ecSDimitry Andric // implicit-define of the call instruction.
1517b60736ecSDimitry Andric if (Info.CanLowerReturn && !Info.OrigRet.Ty->isVoidTy()) {
1518b60736ecSDimitry Andric CCAssignFn *RetAssignFn = TLI.CCAssignFnForReturn(Info.CallConv,
1519b60736ecSDimitry Andric Info.IsVarArg);
1520344a3780SDimitry Andric IncomingValueAssigner Assigner(RetAssignFn);
1521344a3780SDimitry Andric CallReturnHandler Handler(MIRBuilder, MRI, MIB);
1522344a3780SDimitry Andric if (!determineAndHandleAssignments(Handler, Assigner, InArgs, MIRBuilder,
1523344a3780SDimitry Andric Info.CallConv, Info.IsVarArg))
1524b60736ecSDimitry Andric return false;
1525b60736ecSDimitry Andric }
1526b60736ecSDimitry Andric
1527b60736ecSDimitry Andric uint64_t CalleePopBytes = NumBytes;
1528344a3780SDimitry Andric
1529344a3780SDimitry Andric MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKDOWN)
1530344a3780SDimitry Andric .addImm(0)
1531b60736ecSDimitry Andric .addImm(CalleePopBytes);
1532b60736ecSDimitry Andric
1533344a3780SDimitry Andric if (!Info.CanLowerReturn) {
1534344a3780SDimitry Andric insertSRetLoads(MIRBuilder, Info.OrigRet.Ty, Info.OrigRet.Regs,
1535344a3780SDimitry Andric Info.DemoteRegister, Info.DemoteStackIndex);
1536344a3780SDimitry Andric }
1537344a3780SDimitry Andric
1538b60736ecSDimitry Andric return true;
1539b60736ecSDimitry Andric }
1540