LLVM 23.0.0git
AMDGPUCallLowering.cpp
Go to the documentation of this file.
1//===-- llvm/lib/Target/AMDGPU/AMDGPUCallLowering.cpp - Call lowering -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the lowering of LLVM calls to machine code calls for
11/// GlobalISel.
12///
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPUCallLowering.h"
16#include "AMDGPU.h"
17#include "AMDGPULegalizerInfo.h"
19#include "SIRegisterInfo.h"
25#include "llvm/IR/IntrinsicsAMDGPU.h"
26
27#define DEBUG_TYPE "amdgpu-call-lowering"
28
29using namespace llvm;
30
31namespace {
32
33/// Wrapper around extendRegister to ensure we extend to a full 32-bit register.
34static Register extendRegisterMin32(CallLowering::ValueHandler &Handler,
35 Register ValVReg, const CCValAssign &VA) {
36 if (VA.getLocVT().getSizeInBits() < 32) {
37 // 16-bit types are reported as legal for 32-bit registers. We need to
38 // extend and do a 32-bit copy to avoid the verifier complaining about it.
39 return Handler.MIRBuilder.buildAnyExt(LLT::integer(32), ValVReg).getReg(0);
40 }
41
42 return Handler.extendRegister(ValVReg, VA);
43}
44
45struct AMDGPUOutgoingValueHandler : public CallLowering::OutgoingValueHandler {
46 AMDGPUOutgoingValueHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI,
48 : OutgoingValueHandler(B, MRI), MIB(MIB) {}
49
51
52 Register getStackAddress(uint64_t Size, int64_t Offset,
54 ISD::ArgFlagsTy Flags) override {
55 llvm_unreachable("not implemented");
56 }
57
58 void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
59 const MachinePointerInfo &MPO,
60 const CCValAssign &VA) override {
61 llvm_unreachable("not implemented");
62 }
63
64 void assignValueToReg(Register ValVReg, Register PhysReg,
65 const CCValAssign &VA,
66 ISD::ArgFlagsTy Flags = {}) override {
67 Register ExtReg = extendRegisterMin32(*this, ValVReg, VA);
68
69 // If this is a scalar return, insert a readfirstlane just in case the value
70 // ends up in a VGPR.
71 // FIXME: Assert this is a shader return.
72 const SIRegisterInfo *TRI
73 = static_cast<const SIRegisterInfo *>(MRI.getTargetRegisterInfo());
74 if (TRI->isSGPRReg(MRI, PhysReg)) {
75 LLT Ty = MRI.getType(ExtReg);
76 LLT I32 = LLT::integer(32);
77 if (Ty != I32 && Ty != LLT::float32()) {
78 // FIXME: We should probably support readfirstlane intrinsics with all
79 // legal 32-bit types.
80 assert(Ty.getSizeInBits() == 32);
81 if (Ty.isPointer())
82 ExtReg = MIRBuilder.buildPtrToInt(I32, ExtReg).getReg(0);
83 else
84 ExtReg = MIRBuilder.buildBitcast(I32, ExtReg).getReg(0);
85 }
86
87 auto ToSGPR = MIRBuilder
88 .buildIntrinsic(Intrinsic::amdgcn_readfirstlane,
89 {MRI.getType(ExtReg)})
90 .addReg(ExtReg);
91 ExtReg = ToSGPR.getReg(0);
92 }
93
94 MIRBuilder.buildCopy(PhysReg, ExtReg);
95 MIB.addUse(PhysReg, RegState::Implicit);
96 }
97};
98
99struct AMDGPUIncomingArgHandler : public CallLowering::IncomingValueHandler {
100 uint64_t StackUsed = 0;
101
102 AMDGPUIncomingArgHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI)
103 : IncomingValueHandler(B, MRI) {}
104
105 Register getStackAddress(uint64_t Size, int64_t Offset,
107 ISD::ArgFlagsTy Flags) override {
108 auto &MFI = MIRBuilder.getMF().getFrameInfo();
109
110 // Byval is assumed to be writable memory, but other stack passed arguments
111 // are not.
112 const bool IsImmutable = !Flags.isByVal();
113 int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable);
114 MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
115 auto AddrReg = MIRBuilder.buildFrameIndex(
117 StackUsed = std::max(StackUsed, Size + Offset);
118 return AddrReg.getReg(0);
119 }
120
121 void copyToReg(Register ValVReg, Register PhysReg, const CCValAssign &VA) {
122 if (VA.getLocVT().getSizeInBits() < 32) {
123 // 16-bit types are reported as legal for 32-bit registers. We need to
124 // do a 32-bit copy, and truncate to avoid the verifier complaining
125 // about it.
126 auto Copy = MIRBuilder.buildCopy(LLT::integer(32), PhysReg);
127
128 // If we have signext/zeroext, it applies to the whole 32-bit register
129 // before truncation.
130 auto Extended =
131 buildExtensionHint(VA, Copy.getReg(0), LLT(VA.getLocVT()));
132 LLT ValTy = MRI.getType(ValVReg);
133 if (ValTy.isInteger()) {
134 MIRBuilder.buildTrunc(ValVReg, Extended);
135 } else {
136 auto Trunc = MIRBuilder.buildTrunc(LLT::integer(ValTy.getSizeInBits()),
137 Extended);
138 MIRBuilder.buildBitcast(ValVReg, Trunc);
139 }
140 return;
141 }
142
143 IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA);
144 }
145
146 void readLaneToSGPR(Register ValVReg, Register PhysReg,
147 const CCValAssign &VA) {
148 // Handle inreg parameters passed through VGPRs due to SGPR exhaustion.
149 // When SGPRs are exhausted, the calling convention may allocate inreg
150 // parameters to VGPRs. We insert readfirstlane to move the value from
151 // VGPR to SGPR, as required by the inreg ABI.
152 //
153 // FIXME: This may increase instruction count in some cases. If the
154 // readfirstlane result is subsequently copied back to a VGPR, we cannot
155 // optimize away the unnecessary VGPR->SGPR->VGPR sequence in later passes
156 // because the inreg attribute information is not preserved in MIR. We could
157 // use WWM_COPY (or similar instructions) and mark it as foldable to enable
158 // later optimization passes to eliminate the redundant readfirstlane.
159 if (VA.getLocVT().getSizeInBits() < 32) {
160 auto Copy = MIRBuilder.buildCopy(LLT::integer(32), PhysReg);
161 auto ToSGPR = MIRBuilder
162 .buildIntrinsic(Intrinsic::amdgcn_readfirstlane,
163 {MRI.getType(Copy.getReg(0))})
164 .addReg(Copy.getReg(0));
165 auto Extended =
166 buildExtensionHint(VA, ToSGPR.getReg(0), LLT(VA.getLocVT()));
167 LLT ValTy = MRI.getType(ValVReg);
168 if (ValTy.isInteger()) {
169 MIRBuilder.buildTrunc(ValVReg, Extended);
170 } else {
171 auto Trunc = MIRBuilder.buildTrunc(LLT::integer(ValTy.getSizeInBits()),
172 Extended);
173 MIRBuilder.buildBitcast(ValVReg, Trunc);
174 }
175 return;
176 }
177
178 auto Copy = MIRBuilder.buildCopy(MRI.getType(ValVReg), PhysReg);
179 MIRBuilder.buildIntrinsic(Intrinsic::amdgcn_readfirstlane, ValVReg)
180 .addReg(Copy.getReg(0));
181 }
182
183 void assignValueToReg(Register ValVReg, Register PhysReg,
184 const CCValAssign &VA,
185 ISD::ArgFlagsTy Flags = {}) override {
186 markPhysRegUsed(PhysReg);
187
188 const SIRegisterInfo *TRI =
189 static_cast<const SIRegisterInfo *>(MRI.getTargetRegisterInfo());
190
191 // Inreg flag should be the same across SplitArg[i]
192 if (Flags.isInReg() && TRI->isVGPR(MRI, PhysReg))
193 readLaneToSGPR(ValVReg, PhysReg, VA);
194 else
195 copyToReg(ValVReg, PhysReg, VA);
196 }
197
198 void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
199 const MachinePointerInfo &MPO,
200 const CCValAssign &VA) override {
201 MachineFunction &MF = MIRBuilder.getMF();
202
203 auto *MMO = MF.getMachineMemOperand(
205 inferAlignFromPtrInfo(MF, MPO));
206 MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
207 }
208
209 /// How the physical register gets marked varies between formal
210 /// parameters (it's a basic-block live-in), and a call instruction
211 /// (it's an implicit-def of the BL).
212 virtual void markPhysRegUsed(unsigned PhysReg) = 0;
213};
214
215struct FormalArgHandler : public AMDGPUIncomingArgHandler {
216 FormalArgHandler(MachineIRBuilder &B, MachineRegisterInfo &MRI)
217 : AMDGPUIncomingArgHandler(B, MRI) {}
218
219 void markPhysRegUsed(unsigned PhysReg) override {
220 MIRBuilder.getMBB().addLiveIn(PhysReg);
221 }
222};
223
224struct CallReturnHandler : public AMDGPUIncomingArgHandler {
225 CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
227 : AMDGPUIncomingArgHandler(MIRBuilder, MRI), MIB(MIB) {}
228
229 void markPhysRegUsed(unsigned PhysReg) override {
230 MIB.addDef(PhysReg, RegState::Implicit);
231 }
232
234};
235
236struct AMDGPUOutgoingArgHandler : public AMDGPUOutgoingValueHandler {
237 /// For tail calls, the byte offset of the call's argument area from the
238 /// callee's. Unused elsewhere.
239 int FPDiff;
240
241 // Cache the SP register vreg if we need it more than once in this call site.
243
244 bool IsTailCall;
245
246 AMDGPUOutgoingArgHandler(MachineIRBuilder &MIRBuilder,
248 bool IsTailCall = false, int FPDiff = 0)
249 : AMDGPUOutgoingValueHandler(MIRBuilder, MRI, MIB), FPDiff(FPDiff),
250 IsTailCall(IsTailCall) {}
251
252 Register getStackAddress(uint64_t Size, int64_t Offset,
254 ISD::ArgFlagsTy Flags) override {
255 MachineFunction &MF = MIRBuilder.getMF();
256 const LLT PtrTy = LLT::pointer(AMDGPUAS::PRIVATE_ADDRESS, 32);
257 const LLT I32 = LLT::integer(32);
258
259 if (IsTailCall) {
260 Offset += FPDiff;
261 int FI = MF.getFrameInfo().CreateFixedObject(Size, Offset, true);
262 auto FIReg = MIRBuilder.buildFrameIndex(PtrTy, FI);
264 return FIReg.getReg(0);
265 }
266
268
269 if (!SPReg) {
270 const GCNSubtarget &ST = MIRBuilder.getMF().getSubtarget<GCNSubtarget>();
271 if (ST.hasFlatScratchEnabled()) {
272 // The stack is accessed unswizzled, so we can use a regular copy.
273 SPReg = MIRBuilder.buildCopy(PtrTy,
274 MFI->getStackPtrOffsetReg()).getReg(0);
275 } else {
276 // The address we produce here, without knowing the use context, is going
277 // to be interpreted as a vector address, so we need to convert to a
278 // swizzled address.
279 SPReg = MIRBuilder.buildInstr(AMDGPU::G_AMDGPU_WAVE_ADDRESS, {PtrTy},
280 {MFI->getStackPtrOffsetReg()}).getReg(0);
281 }
282 }
283
284 auto OffsetReg = MIRBuilder.buildConstant(I32, Offset);
285
286 auto AddrReg = MIRBuilder.buildPtrAdd(PtrTy, SPReg, OffsetReg);
288 return AddrReg.getReg(0);
289 }
290
291 void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
292 const MachinePointerInfo &MPO,
293 const CCValAssign &VA) override {
294 MachineFunction &MF = MIRBuilder.getMF();
295 uint64_t LocMemOffset = VA.getLocMemOffset();
296 const auto &ST = MF.getSubtarget<GCNSubtarget>();
297
298 auto *MMO = MF.getMachineMemOperand(
299 MPO, MachineMemOperand::MOStore, MemTy,
300 commonAlignment(ST.getStackAlignment(), LocMemOffset));
301 MIRBuilder.buildStore(ValVReg, Addr, *MMO);
302 }
303
304 void assignValueToAddress(const CallLowering::ArgInfo &Arg,
305 unsigned ValRegIndex, Register Addr, LLT MemTy,
306 const MachinePointerInfo &MPO,
307 const CCValAssign &VA) override {
309 ? extendRegister(Arg.Regs[ValRegIndex], VA)
310 : Arg.Regs[ValRegIndex];
311 assignValueToAddress(ValVReg, Addr, MemTy, MPO, VA);
312 }
313};
314} // anonymous namespace
315
318
319// FIXME: Compatibility shim
321 switch (MIOpc) {
322 case TargetOpcode::G_SEXT:
323 return ISD::SIGN_EXTEND;
324 case TargetOpcode::G_ZEXT:
325 return ISD::ZERO_EXTEND;
326 case TargetOpcode::G_ANYEXT:
327 return ISD::ANY_EXTEND;
328 default:
329 llvm_unreachable("not an extend opcode");
330 }
331}
332
333bool AMDGPUCallLowering::canLowerReturn(MachineFunction &MF,
334 CallingConv::ID CallConv,
336 bool IsVarArg) const {
337 // For shaders. Vector types should be explicitly handled by CC.
338 if (AMDGPU::isEntryFunctionCC(CallConv))
339 return true;
340
342 const SITargetLowering &TLI = *getTLI<SITargetLowering>();
343 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs,
344 MF.getFunction().getContext());
345
346 return checkReturn(CCInfo, Outs, TLI.CCAssignFnForReturn(CallConv, IsVarArg));
347}
348
349/// Lower the return value for the already existing \p Ret. This assumes that
350/// \p B's insertion point is correct.
351bool AMDGPUCallLowering::lowerReturnVal(MachineIRBuilder &B,
352 const Value *Val, ArrayRef<Register> VRegs,
353 MachineInstrBuilder &Ret) const {
354 if (!Val)
355 return true;
356
357 auto &MF = B.getMF();
358 const auto &F = MF.getFunction();
359 const DataLayout &DL = MF.getDataLayout();
360 MachineRegisterInfo *MRI = B.getMRI();
361 LLVMContext &Ctx = F.getContext();
362
363 CallingConv::ID CC = F.getCallingConv();
364 const SITargetLowering &TLI = *getTLI<SITargetLowering>();
365
366 SmallVector<EVT, 8> SplitEVTs;
367 ComputeValueVTs(TLI, DL, Val->getType(), SplitEVTs);
368 assert(VRegs.size() == SplitEVTs.size() &&
369 "For each split Type there should be exactly one VReg.");
370
371 SmallVector<ArgInfo, 8> SplitRetInfos;
372
373 for (unsigned i = 0; i < SplitEVTs.size(); ++i) {
374 EVT VT = SplitEVTs[i];
375 Register Reg = VRegs[i];
376 ArgInfo RetInfo(Reg, VT.getTypeForEVT(Ctx), 0);
377 setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F);
378
379 if (VT.isScalarInteger()) {
380 unsigned ExtendOp = TargetOpcode::G_ANYEXT;
381 if (RetInfo.Flags[0].isSExt()) {
382 assert(RetInfo.Regs.size() == 1 && "expect only simple return values");
383 ExtendOp = TargetOpcode::G_SEXT;
384 } else if (RetInfo.Flags[0].isZExt()) {
385 assert(RetInfo.Regs.size() == 1 && "expect only simple return values");
386 ExtendOp = TargetOpcode::G_ZEXT;
387 }
388
389 EVT ExtVT = TLI.getTypeForExtReturn(Ctx, VT,
390 extOpcodeToISDExtOpcode(ExtendOp));
391 if (ExtVT != VT) {
392 RetInfo.Ty = ExtVT.getTypeForEVT(Ctx);
393 LLT ExtTy = getLLTForType(*RetInfo.Ty, DL);
394 Reg = B.buildInstr(ExtendOp, {ExtTy}, {Reg}).getReg(0);
395 }
396 }
397
398 if (Reg != RetInfo.Regs[0]) {
399 RetInfo.Regs[0] = Reg;
400 // Reset the arg flags after modifying Reg.
401 setArgFlags(RetInfo, AttributeList::ReturnIndex, DL, F);
402 }
403
404 splitToValueTypes(RetInfo, SplitRetInfos, DL, CC);
405 }
406
407 CCAssignFn *AssignFn = TLI.CCAssignFnForReturn(CC, F.isVarArg());
408
409 OutgoingValueAssigner Assigner(AssignFn);
410 AMDGPUOutgoingValueHandler RetHandler(B, *MRI, Ret);
411 return determineAndHandleAssignments(RetHandler, Assigner, SplitRetInfos, B,
412 CC, F.isVarArg());
413}
414
416 ArrayRef<Register> VRegs,
417 FunctionLoweringInfo &FLI) const {
418
419 MachineFunction &MF = B.getMF();
421 MFI->setIfReturnsVoid(!Val);
422
423 assert(!Val == VRegs.empty() && "Return value without a vreg");
424
425 CallingConv::ID CC = B.getMF().getFunction().getCallingConv();
426 const bool IsShader = AMDGPU::isShader(CC);
427 const bool IsWaveEnd =
428 (IsShader && MFI->returnsVoid()) || AMDGPU::isKernel(CC);
429 if (IsWaveEnd) {
430 B.buildInstr(AMDGPU::S_ENDPGM)
431 .addImm(0);
432 return true;
433 }
434
435 const bool IsWholeWave = MFI->isWholeWaveFunction();
436 unsigned ReturnOpc = IsWholeWave ? AMDGPU::G_AMDGPU_WHOLE_WAVE_FUNC_RETURN
437 : IsShader ? AMDGPU::SI_RETURN_TO_EPILOG
438 : AMDGPU::SI_RETURN;
439 auto Ret = B.buildInstrNoInsert(ReturnOpc);
440
441 if (!FLI.CanLowerReturn)
442 insertSRetStores(B, Val->getType(), VRegs, FLI.DemoteRegister);
443 else if (!lowerReturnVal(B, Val, VRegs, Ret))
444 return false;
445
446 if (IsWholeWave)
447 addOriginalExecToReturn(B.getMF(), Ret);
448
449 // TODO: Handle CalleeSavedRegsViaCopy.
450
451 B.insertInstr(Ret);
452 return true;
453}
454
455void AMDGPUCallLowering::lowerParameterPtr(Register DstReg, MachineIRBuilder &B,
456 uint64_t Offset) const {
457 MachineFunction &MF = B.getMF();
460 Register KernArgSegmentPtr =
462 Register KernArgSegmentVReg = MRI.getLiveInVirtReg(KernArgSegmentPtr);
463
464 auto OffsetReg = B.buildConstant(LLT::integer(64), Offset);
465
466 B.buildPtrAdd(DstReg, KernArgSegmentVReg, OffsetReg);
467}
468
469void AMDGPUCallLowering::lowerParameter(MachineIRBuilder &B, ArgInfo &OrigArg,
471 Align Alignment) const {
472 MachineFunction &MF = B.getMF();
473 const Function &F = MF.getFunction();
474 const DataLayout &DL = F.getDataLayout();
477
479
480 SmallVector<ArgInfo, 32> SplitArgs;
481 SmallVector<TypeSize> FieldOffsets;
482 splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv(), &FieldOffsets);
483
484 unsigned Idx = 0;
485 for (ArgInfo &SplitArg : SplitArgs) {
486 Register PtrReg = B.getMRI()->createGenericVirtualRegister(PtrTy);
487 lowerParameterPtr(PtrReg, B, Offset + FieldOffsets[Idx]);
488
489 LLT ArgTy = getLLTForType(*SplitArg.Ty, DL);
490 if (SplitArg.Flags[0].isPointer()) {
491 // Compensate for losing pointeriness in splitValueTypes.
492 LLT PtrTy = LLT::pointer(SplitArg.Flags[0].getPointerAddrSpace(),
493 ArgTy.getScalarSizeInBits());
494 ArgTy = ArgTy.isVector() ? LLT::vector(ArgTy.getElementCount(), PtrTy)
495 : PtrTy;
496 }
497
498 MachineMemOperand *MMO = MF.getMachineMemOperand(
499 PtrInfo,
502 ArgTy, commonAlignment(Alignment, FieldOffsets[Idx]));
503
504 assert(SplitArg.Regs.size() == 1);
505
506 B.buildLoad(SplitArg.Regs[0], PtrReg, *MMO);
507 ++Idx;
508 }
509}
510
511// Allocate special inputs passed in user SGPRs.
512static void allocateHSAUserSGPRs(CCState &CCInfo,
514 MachineFunction &MF,
515 const SIRegisterInfo &TRI,
516 SIMachineFunctionInfo &Info) {
517 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
518 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info.getUserSGPRInfo();
519 if (UserSGPRInfo.hasPrivateSegmentBuffer()) {
520 Register PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI);
521 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass);
522 CCInfo.AllocateReg(PrivateSegmentBufferReg);
523 }
524
525 if (UserSGPRInfo.hasDispatchPtr()) {
526 Register DispatchPtrReg = Info.addDispatchPtr(TRI);
527 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
528 CCInfo.AllocateReg(DispatchPtrReg);
529 }
530
531 if (UserSGPRInfo.hasQueuePtr()) {
532 Register QueuePtrReg = Info.addQueuePtr(TRI);
533 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
534 CCInfo.AllocateReg(QueuePtrReg);
535 }
536
537 if (UserSGPRInfo.hasKernargSegmentPtr()) {
539 Register InputPtrReg = Info.addKernargSegmentPtr(TRI);
542 MRI.addLiveIn(InputPtrReg, VReg);
543 B.getMBB().addLiveIn(InputPtrReg);
544 B.buildCopy(VReg, InputPtrReg);
545 CCInfo.AllocateReg(InputPtrReg);
546 }
547
548 if (UserSGPRInfo.hasDispatchID()) {
549 Register DispatchIDReg = Info.addDispatchID(TRI);
550 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
551 CCInfo.AllocateReg(DispatchIDReg);
552 }
553
554 if (UserSGPRInfo.hasFlatScratchInit()) {
555 Register FlatScratchInitReg = Info.addFlatScratchInit(TRI);
556 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
557 CCInfo.AllocateReg(FlatScratchInitReg);
558 }
559
560 if (UserSGPRInfo.hasPrivateSegmentSize()) {
561 Register PrivateSegmentSizeReg = Info.addPrivateSegmentSize(TRI);
562 MF.addLiveIn(PrivateSegmentSizeReg, &AMDGPU::SGPR_32RegClass);
563 CCInfo.AllocateReg(PrivateSegmentSizeReg);
564 }
565
566 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
567 // these from the dispatch pointer.
568}
569
571 MachineIRBuilder &B, const Function &F,
572 ArrayRef<ArrayRef<Register>> VRegs) const {
573 MachineFunction &MF = B.getMF();
574 const GCNSubtarget *Subtarget = &MF.getSubtarget<GCNSubtarget>();
577 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
579 const DataLayout &DL = F.getDataLayout();
580
582 CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
583
584 allocateHSAUserSGPRs(CCInfo, B, MF, *TRI, *Info);
585
586 unsigned i = 0;
587 const Align KernArgBaseAlign(16);
588 const unsigned BaseOffset = Subtarget->getExplicitKernelArgOffset();
589 uint64_t ExplicitArgOffset = 0;
590
591 // TODO: Align down to dword alignment and extract bits for extending loads.
592 for (auto &Arg : F.args()) {
593 // TODO: Add support for kernarg preload.
594 if (Arg.hasAttribute("amdgpu-hidden-argument")) {
595 LLVM_DEBUG(dbgs() << "Preloading hidden arguments is not supported\n");
596 return false;
597 }
598
599 const bool IsByRef = Arg.hasByRefAttr();
600 Type *ArgTy = IsByRef ? Arg.getParamByRefType() : Arg.getType();
601 unsigned AllocSize = DL.getTypeAllocSize(ArgTy);
602 if (AllocSize == 0)
603 continue;
604
605 MaybeAlign ParamAlign = IsByRef ? Arg.getParamAlign() : std::nullopt;
606 Align ABIAlign = DL.getValueOrABITypeAlignment(ParamAlign, ArgTy);
607
608 uint64_t ArgOffset = alignTo(ExplicitArgOffset, ABIAlign) + BaseOffset;
609 ExplicitArgOffset = alignTo(ExplicitArgOffset, ABIAlign) + AllocSize;
610
611 if (Arg.use_empty()) {
612 ++i;
613 continue;
614 }
615
616 Align Alignment = commonAlignment(KernArgBaseAlign, ArgOffset);
617
618 if (IsByRef) {
619 unsigned ByRefAS = cast<PointerType>(Arg.getType())->getAddressSpace();
620
621 assert(VRegs[i].size() == 1 &&
622 "expected only one register for byval pointers");
623 if (ByRefAS == AMDGPUAS::CONSTANT_ADDRESS) {
624 lowerParameterPtr(VRegs[i][0], B, ArgOffset);
625 } else {
626 const LLT ConstPtrTy = LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64);
627 Register PtrReg = MRI.createGenericVirtualRegister(ConstPtrTy);
628 lowerParameterPtr(PtrReg, B, ArgOffset);
629
630 B.buildAddrSpaceCast(VRegs[i][0], PtrReg);
631 }
632 } else {
633 ArgInfo OrigArg(VRegs[i], Arg, i);
634 const unsigned OrigArgIdx = i + AttributeList::FirstArgIndex;
635 setArgFlags(OrigArg, OrigArgIdx, DL, F);
636 lowerParameter(B, OrigArg, ArgOffset, Alignment);
637 }
638
639 ++i;
640 }
641
642 if (Info->getNumKernargPreloadedSGPRs())
643 Info->setNumWaveDispatchSGPRs(Info->getNumUserSGPRs());
644
645 TLI.allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info);
646 TLI.allocateSystemSGPRs(CCInfo, MF, *Info, F.getCallingConv(), false);
647 return true;
648}
649
652 FunctionLoweringInfo &FLI) const {
653 CallingConv::ID CC = F.getCallingConv();
654
655 // The infrastructure for normal calling convention lowering is essentially
656 // useless for kernels. We want to avoid any kind of legalization or argument
657 // splitting.
659 return lowerFormalArgumentsKernel(B, F, VRegs);
660
661 const bool IsGraphics = AMDGPU::isGraphics(CC);
662 const bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CC);
663
664 MachineFunction &MF = B.getMF();
665 MachineBasicBlock &MBB = B.getMBB();
668 const GCNSubtarget &Subtarget = MF.getSubtarget<GCNSubtarget>();
669 const SIRegisterInfo *TRI = Subtarget.getRegisterInfo();
670 const DataLayout &DL = F.getDataLayout();
671
673 CCState CCInfo(CC, F.isVarArg(), MF, ArgLocs, F.getContext());
674 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info->getUserSGPRInfo();
675
676 if (UserSGPRInfo.hasImplicitBufferPtr()) {
677 Register ImplicitBufferPtrReg = Info->addImplicitBufferPtr(*TRI);
678 MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass);
679 CCInfo.AllocateReg(ImplicitBufferPtrReg);
680 }
681
682 // FIXME: This probably isn't defined for mesa
683 if (UserSGPRInfo.hasFlatScratchInit() && !Subtarget.isAmdPalOS()) {
684 Register FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
685 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
686 CCInfo.AllocateReg(FlatScratchInitReg);
687 }
688
689 SmallVector<ArgInfo, 32> SplitArgs;
690 unsigned Idx = 0;
691 unsigned PSInputNum = 0;
692
693 // Insert the hidden sret parameter if the return value won't fit in the
694 // return registers.
695 if (!FLI.CanLowerReturn)
696 insertSRetIncomingArgument(F, SplitArgs, FLI.DemoteRegister, MRI, DL);
697
698 for (auto &Arg : F.args()) {
699 if (DL.getTypeStoreSize(Arg.getType()) == 0)
700 continue;
701
702 if (Info->isWholeWaveFunction() && Idx == 0) {
703 assert(VRegs[Idx].size() == 1 && "Expected only one register");
704
705 // The first argument for whole wave functions is the original EXEC value.
706 B.buildInstr(AMDGPU::G_AMDGPU_WHOLE_WAVE_FUNC_SETUP)
707 .addDef(VRegs[Idx][0]);
708
709 ++Idx;
710 continue;
711 }
712
713 const bool InReg = Arg.hasAttribute(Attribute::InReg);
714
715 if (Arg.hasAttribute(Attribute::SwiftSelf) ||
716 Arg.hasAttribute(Attribute::SwiftError) ||
717 Arg.hasAttribute(Attribute::Nest))
718 return false;
719
720 if (CC == CallingConv::AMDGPU_PS && !InReg && PSInputNum <= 15) {
721 const bool ArgUsed = !Arg.use_empty();
722 bool SkipArg = !ArgUsed && !Info->isPSInputAllocated(PSInputNum);
723
724 if (!SkipArg) {
725 Info->markPSInputAllocated(PSInputNum);
726 if (ArgUsed)
727 Info->markPSInputEnabled(PSInputNum);
728 }
729
730 ++PSInputNum;
731
732 if (SkipArg) {
733 for (Register R : VRegs[Idx])
734 B.buildUndef(R);
735
736 ++Idx;
737 continue;
738 }
739 }
740
741 ArgInfo OrigArg(VRegs[Idx], Arg, Idx);
742 const unsigned OrigArgIdx = Idx + AttributeList::FirstArgIndex;
743 setArgFlags(OrigArg, OrigArgIdx, DL, F);
744
745 splitToValueTypes(OrigArg, SplitArgs, DL, CC);
746 ++Idx;
747 }
748
749 // At least one interpolation mode must be enabled or else the GPU will
750 // hang.
751 //
752 // Check PSInputAddr instead of PSInputEnable. The idea is that if the user
753 // set PSInputAddr, the user wants to enable some bits after the compilation
754 // based on run-time states. Since we can't know what the final PSInputEna
755 // will look like, so we shouldn't do anything here and the user should take
756 // responsibility for the correct programming.
757 //
758 // Otherwise, the following restrictions apply:
759 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
760 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
761 // enabled too.
762 if (CC == CallingConv::AMDGPU_PS) {
763 if ((Info->getPSInputAddr() & 0x7F) == 0 ||
764 ((Info->getPSInputAddr() & 0xF) == 0 &&
765 Info->isPSInputAllocated(11))) {
766 CCInfo.AllocateReg(AMDGPU::VGPR0);
767 CCInfo.AllocateReg(AMDGPU::VGPR1);
768 Info->markPSInputAllocated(0);
769 Info->markPSInputEnabled(0);
770 }
771
772 if (Subtarget.isAmdPalOS()) {
773 // For isAmdPalOS, the user does not enable some bits after compilation
774 // based on run-time states; the register values being generated here are
775 // the final ones set in hardware. Therefore we need to apply the
776 // workaround to PSInputAddr and PSInputEnable together. (The case where
777 // a bit is set in PSInputAddr but not PSInputEnable is where the frontend
778 // set up an input arg for a particular interpolation mode, but nothing
779 // uses that input arg. Really we should have an earlier pass that removes
780 // such an arg.)
781 unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable();
782 if ((PsInputBits & 0x7F) == 0 ||
783 ((PsInputBits & 0xF) == 0 &&
784 (PsInputBits >> 11 & 1)))
785 Info->markPSInputEnabled(llvm::countr_zero(Info->getPSInputAddr()));
786 }
787 }
788
790 CCAssignFn *AssignFn = TLI.CCAssignFnForCall(CC, F.isVarArg());
791
792 if (!MBB.empty())
793 B.setInstr(*MBB.begin());
794
795 if (!IsEntryFunc && !IsGraphics) {
796 // For the fixed ABI, pass workitem IDs in the last argument register.
797 TLI.allocateSpecialInputVGPRsFixed(CCInfo, MF, *TRI, *Info);
798
799 if (!Subtarget.hasFlatScratchEnabled())
800 CCInfo.AllocateReg(Info->getScratchRSrcReg());
801 TLI.allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info);
802 }
803
804 IncomingValueAssigner Assigner(AssignFn);
805 if (!determineAssignments(Assigner, SplitArgs, CCInfo))
806 return false;
807
808 if (IsEntryFunc) {
809 // This assumes the registers are allocated by CCInfo in ascending order
810 // with no gaps.
811 Info->setNumWaveDispatchSGPRs(
812 CCInfo.getFirstUnallocated(AMDGPU::SGPR_32RegClass.getRegisters()));
813 Info->setNumWaveDispatchVGPRs(
814 CCInfo.getFirstUnallocated(AMDGPU::VGPR_32RegClass.getRegisters()));
815 }
816
817 FormalArgHandler Handler(B, MRI);
818 if (!handleAssignments(Handler, SplitArgs, CCInfo, ArgLocs, B))
819 return false;
820
821 uint64_t StackSize = Assigner.StackSize;
822
823 // Start adding system SGPRs.
824 if (IsEntryFunc)
825 TLI.allocateSystemSGPRs(CCInfo, MF, *Info, CC, IsGraphics);
826
827 // When we tail call, we need to check if the callee's arguments will fit on
828 // the caller's stack. So, whenever we lower formal arguments, we should keep
829 // track of this information, since we might lower a tail call in this
830 // function later.
831 Info->setBytesInStackArgArea(StackSize);
832
833 // Move back to the end of the basic block.
834 B.setMBB(MBB);
835
836 return true;
837}
838
840 CCState &CCInfo,
841 SmallVectorImpl<std::pair<MCRegister, Register>> &ArgRegs,
842 CallLoweringInfo &Info) const {
843 MachineFunction &MF = MIRBuilder.getMF();
844
845 // If there's no call site, this doesn't correspond to a call from the IR and
846 // doesn't need implicit inputs.
847 if (!Info.CB)
848 return true;
849
850 const AMDGPUFunctionArgInfo &CalleeArgInfo =
852
854 const AMDGPUFunctionArgInfo &CallerArgInfo = MFI->getArgInfo();
855
856
857 // TODO: Unify with private memory register handling. This is complicated by
858 // the fact that at least in kernels, the input argument is not necessarily
859 // in the same location as the input.
869 };
870
871 static constexpr StringLiteral ImplicitAttrNames[][2] = {
872 {"amdgpu-no-dispatch-ptr", ""},
873 {"amdgpu-no-queue-ptr", ""},
874 {"amdgpu-no-implicitarg-ptr", ""},
875 {"amdgpu-no-dispatch-id", ""},
876 {"amdgpu-no-workgroup-id-x", "amdgpu-no-cluster-id-x"},
877 {"amdgpu-no-workgroup-id-y", "amdgpu-no-cluster-id-y"},
878 {"amdgpu-no-workgroup-id-z", "amdgpu-no-cluster-id-z"},
879 {"amdgpu-no-lds-kernel-id", ""},
880 };
881
883
884 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
885 const AMDGPULegalizerInfo *LI
886 = static_cast<const AMDGPULegalizerInfo*>(ST.getLegalizerInfo());
887
888 unsigned I = 0;
889 for (auto InputID : InputRegs) {
890 const ArgDescriptor *OutgoingArg;
891 const TargetRegisterClass *ArgRC;
892 LLT ArgTy;
893
894 // If the callee does not use the attribute value, skip copying the value.
895 if (all_of(ImplicitAttrNames[I++], [&](StringRef AttrName) {
896 return AttrName.empty() || Info.CB->hasFnAttr(AttrName);
897 }))
898 continue;
899
900 std::tie(OutgoingArg, ArgRC, ArgTy) =
901 CalleeArgInfo.getPreloadedValue(InputID);
902 if (!OutgoingArg)
903 continue;
904
905 const ArgDescriptor *IncomingArg;
906 const TargetRegisterClass *IncomingArgRC;
907 std::tie(IncomingArg, IncomingArgRC, ArgTy) =
908 CallerArgInfo.getPreloadedValue(InputID);
909 assert(IncomingArgRC == ArgRC);
910
911 Register InputReg = MRI.createGenericVirtualRegister(ArgTy);
912
913 if (IncomingArg) {
914 LI->buildLoadInputValue(InputReg, MIRBuilder, IncomingArg, ArgRC, ArgTy);
915 } else if (InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR) {
916 LI->getImplicitArgPtr(InputReg, MRI, MIRBuilder);
917 } else if (InputID == AMDGPUFunctionArgInfo::LDS_KERNEL_ID) {
918 std::optional<uint32_t> Id =
920 if (Id) {
921 MIRBuilder.buildConstant(InputReg, *Id);
922 } else {
923 MIRBuilder.buildUndef(InputReg);
924 }
925 } else {
926 // We may have proven the input wasn't needed, although the ABI is
927 // requiring it. We just need to allocate the register appropriately.
928 MIRBuilder.buildUndef(InputReg);
929 }
930
931 if (OutgoingArg->isRegister()) {
932 ArgRegs.emplace_back(OutgoingArg->getRegister(), InputReg);
933 if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
934 report_fatal_error("failed to allocate implicit input argument");
935 } else {
936 LLVM_DEBUG(dbgs() << "Unhandled stack passed implicit input argument\n");
937 return false;
938 }
939 }
940
941 // Pack workitem IDs into a single register or pass it as is if already
942 // packed.
943 const ArgDescriptor *OutgoingArg;
944 const TargetRegisterClass *ArgRC;
945 LLT ArgTy;
946
947 std::tie(OutgoingArg, ArgRC, ArgTy) =
949 if (!OutgoingArg)
950 std::tie(OutgoingArg, ArgRC, ArgTy) =
952 if (!OutgoingArg)
953 std::tie(OutgoingArg, ArgRC, ArgTy) =
955 if (!OutgoingArg)
956 return false;
957
958 auto WorkitemIDX =
959 CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X);
960 auto WorkitemIDY =
961 CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y);
962 auto WorkitemIDZ =
963 CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z);
964
965 const ArgDescriptor *IncomingArgX = std::get<0>(WorkitemIDX);
966 const ArgDescriptor *IncomingArgY = std::get<0>(WorkitemIDY);
967 const ArgDescriptor *IncomingArgZ = std::get<0>(WorkitemIDZ);
968 const LLT I32 = LLT::integer(32);
969
970 const bool NeedWorkItemIDX = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-x");
971 const bool NeedWorkItemIDY = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-y");
972 const bool NeedWorkItemIDZ = !Info.CB->hasFnAttr("amdgpu-no-workitem-id-z");
973
974 // If incoming ids are not packed we need to pack them.
975 // FIXME: Should consider known workgroup size to eliminate known 0 cases.
976 Register InputReg;
977 if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo.WorkItemIDX &&
978 NeedWorkItemIDX) {
979 if (ST.getMaxWorkitemID(MF.getFunction(), 0) != 0) {
980 InputReg = MRI.createGenericVirtualRegister(I32);
981 LI->buildLoadInputValue(InputReg, MIRBuilder, IncomingArgX,
982 std::get<1>(WorkitemIDX),
983 std::get<2>(WorkitemIDX));
984 } else {
985 InputReg = MIRBuilder.buildConstant(I32, 0).getReg(0);
986 }
987 }
988
989 if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo.WorkItemIDY &&
990 NeedWorkItemIDY && ST.getMaxWorkitemID(MF.getFunction(), 1) != 0) {
992 LI->buildLoadInputValue(Y, MIRBuilder, IncomingArgY,
993 std::get<1>(WorkitemIDY), std::get<2>(WorkitemIDY));
994
995 Y = MIRBuilder.buildShl(I32, Y, MIRBuilder.buildConstant(I32, 10))
996 .getReg(0);
997 InputReg = InputReg ? MIRBuilder.buildOr(I32, InputReg, Y).getReg(0) : Y;
998 }
999
1000 if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo.WorkItemIDZ &&
1001 NeedWorkItemIDZ && ST.getMaxWorkitemID(MF.getFunction(), 2) != 0) {
1003 LI->buildLoadInputValue(Z, MIRBuilder, IncomingArgZ,
1004 std::get<1>(WorkitemIDZ), std::get<2>(WorkitemIDZ));
1005
1006 Z = MIRBuilder.buildShl(I32, Z, MIRBuilder.buildConstant(I32, 20))
1007 .getReg(0);
1008 InputReg = InputReg ? MIRBuilder.buildOr(I32, InputReg, Z).getReg(0) : Z;
1009 }
1010
1011 if (!InputReg &&
1012 (NeedWorkItemIDX || NeedWorkItemIDY || NeedWorkItemIDZ)) {
1013 InputReg = MRI.createGenericVirtualRegister(I32);
1014 if (!IncomingArgX && !IncomingArgY && !IncomingArgZ) {
1015 // We're in a situation where the outgoing function requires the workitem
1016 // ID, but the calling function does not have it (e.g a graphics function
1017 // calling a C calling convention function). This is illegal, but we need
1018 // to produce something.
1019 MIRBuilder.buildUndef(InputReg);
1020 } else {
1021 // Workitem ids are already packed, any of present incoming arguments will
1022 // carry all required fields.
1024 IncomingArgX ? *IncomingArgX :
1025 IncomingArgY ? *IncomingArgY : *IncomingArgZ, ~0u);
1026 LI->buildLoadInputValue(InputReg, MIRBuilder, &IncomingArg,
1027 &AMDGPU::VGPR_32RegClass, I32);
1028 }
1029 }
1030
1031 if (OutgoingArg->isRegister()) {
1032 if (InputReg)
1033 ArgRegs.emplace_back(OutgoingArg->getRegister(), InputReg);
1034
1035 if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
1036 report_fatal_error("failed to allocate implicit input argument");
1037 } else {
1038 LLVM_DEBUG(dbgs() << "Unhandled stack passed implicit input argument\n");
1039 return false;
1040 }
1041
1042 return true;
1043}
1044
1045/// Returns a pair containing the fixed CCAssignFn and the vararg CCAssignFn for
1046/// CC.
1047static std::pair<CCAssignFn *, CCAssignFn *>
1049 return {TLI.CCAssignFnForCall(CC, false), TLI.CCAssignFnForCall(CC, true)};
1050}
1051
1052static unsigned getCallOpcode(const MachineFunction &CallerF, bool IsIndirect,
1053 bool IsTailCall, bool IsWave32,
1054 CallingConv::ID CC,
1055 bool IsDynamicVGPRChainCall = false) {
1056 // For calls to amdgpu_cs_chain functions, the address is known to be uniform.
1057 assert((AMDGPU::isChainCC(CC) || !IsIndirect || !IsTailCall) &&
1058 "Indirect calls can't be tail calls, "
1059 "because the address can be divergent");
1060 if (!IsTailCall)
1061 return AMDGPU::G_SI_CALL;
1062
1063 if (AMDGPU::isChainCC(CC)) {
1064 if (IsDynamicVGPRChainCall)
1065 return IsWave32 ? AMDGPU::SI_CS_CHAIN_TC_W32_DVGPR
1066 : AMDGPU::SI_CS_CHAIN_TC_W64_DVGPR;
1067 return IsWave32 ? AMDGPU::SI_CS_CHAIN_TC_W32 : AMDGPU::SI_CS_CHAIN_TC_W64;
1068 }
1069
1070 if (CallerF.getFunction().getCallingConv() ==
1072 return AMDGPU::SI_TCRETURN_GFX_WholeWave;
1073
1075 return AMDGPU::SI_TCRETURN_GFX;
1076
1077 return AMDGPU::SI_TCRETURN;
1078}
1079
1080// Add operands to call instruction to track the callee.
1082 MachineIRBuilder &MIRBuilder,
1084 bool IsDynamicVGPRChainCall = false) {
1085 if (Info.Callee.isReg()) {
1086 CallInst.addReg(Info.Callee.getReg());
1087 CallInst.addImm(0);
1088 } else if (Info.Callee.isGlobal() && Info.Callee.getOffset() == 0) {
1089 // The call lowering lightly assumed we can directly encode a call target in
1090 // the instruction, which is not the case. Materialize the address here.
1091 const GlobalValue *GV = Info.Callee.getGlobal();
1092 auto Ptr = MIRBuilder.buildGlobalValue(
1093 LLT::pointer(GV->getAddressSpace(), 64), GV);
1094 CallInst.addReg(Ptr.getReg(0));
1095
1096 if (IsDynamicVGPRChainCall) {
1097 // DynamicVGPR chain calls are always indirect.
1098 CallInst.addImm(0);
1099 } else
1100 CallInst.add(Info.Callee);
1101 } else
1102 return false;
1103
1104 return true;
1105}
1106
1109 SmallVectorImpl<ArgInfo> &InArgs) const {
1110 const Function &CallerF = MF.getFunction();
1111 CallingConv::ID CalleeCC = Info.CallConv;
1112 CallingConv::ID CallerCC = CallerF.getCallingConv();
1113
1114 // If the calling conventions match, then everything must be the same.
1115 if (CalleeCC == CallerCC)
1116 return true;
1117
1118 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1119
1120 // Make sure that the caller and callee preserve all of the same registers.
1121 const auto *TRI = ST.getRegisterInfo();
1122
1123 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1124 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
1125 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
1126 return false;
1127
1128 // Check if the caller and callee will handle arguments in the same way.
1130 CCAssignFn *CalleeAssignFnFixed;
1131 CCAssignFn *CalleeAssignFnVarArg;
1132 std::tie(CalleeAssignFnFixed, CalleeAssignFnVarArg) =
1133 getAssignFnsForCC(CalleeCC, TLI);
1134
1135 CCAssignFn *CallerAssignFnFixed;
1136 CCAssignFn *CallerAssignFnVarArg;
1137 std::tie(CallerAssignFnFixed, CallerAssignFnVarArg) =
1138 getAssignFnsForCC(CallerCC, TLI);
1139
1140 // FIXME: We are not accounting for potential differences in implicitly passed
1141 // inputs, but only the fixed ABI is supported now anyway.
1142 IncomingValueAssigner CalleeAssigner(CalleeAssignFnFixed,
1143 CalleeAssignFnVarArg);
1144 IncomingValueAssigner CallerAssigner(CallerAssignFnFixed,
1145 CallerAssignFnVarArg);
1146 return resultsCompatible(Info, MF, InArgs, CalleeAssigner, CallerAssigner);
1147}
1148
1151 SmallVectorImpl<ArgInfo> &OutArgs) const {
1152 // If there are no outgoing arguments, then we are done.
1153 if (OutArgs.empty())
1154 return true;
1155
1156 const Function &CallerF = MF.getFunction();
1157 CallingConv::ID CalleeCC = Info.CallConv;
1158 CallingConv::ID CallerCC = CallerF.getCallingConv();
1160
1161 CCAssignFn *AssignFnFixed;
1162 CCAssignFn *AssignFnVarArg;
1163 std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI);
1164
1165 // We have outgoing arguments. Make sure that we can tail call with them.
1167 CCState OutInfo(CalleeCC, false, MF, OutLocs, CallerF.getContext());
1168 OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg);
1169
1170 if (!determineAssignments(Assigner, OutArgs, OutInfo)) {
1171 LLVM_DEBUG(dbgs() << "... Could not analyze call operands.\n");
1172 return false;
1173 }
1174
1175 // Make sure that they can fit on the caller's stack.
1176 const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
1177 if (OutInfo.getStackSize() > FuncInfo->getBytesInStackArgArea()) {
1178 LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n");
1179 return false;
1180 }
1181
1182 // Verify that the parameters in callee-saved registers match.
1183 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1184 const SIRegisterInfo *TRI = ST.getRegisterInfo();
1185 const uint32_t *CallerPreservedMask = TRI->getCallPreservedMask(MF, CallerCC);
1186 MachineRegisterInfo &MRI = MF.getRegInfo();
1187 return parametersInCSRMatch(MRI, CallerPreservedMask, OutLocs, OutArgs);
1188}
1189
1192 SmallVectorImpl<ArgInfo> &InArgs, SmallVectorImpl<ArgInfo> &OutArgs) const {
1193 // Must pass all target-independent checks in order to tail call optimize.
1194 if (!Info.IsTailCall)
1195 return false;
1196
1197 // Indirect calls can't be tail calls, because the address can be divergent.
1198 // TODO Check divergence info if the call really is divergent.
1199 if (Info.Callee.isReg())
1200 return false;
1201
1202 MachineFunction &MF = B.getMF();
1203 const Function &CallerF = MF.getFunction();
1204 CallingConv::ID CalleeCC = Info.CallConv;
1205 CallingConv::ID CallerCC = CallerF.getCallingConv();
1206
1207 const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
1208 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1209 // Kernels aren't callable, and don't have a live in return address so it
1210 // doesn't make sense to do a tail call with entry functions.
1211 if (!CallerPreserved)
1212 return false;
1213
1214 if (!AMDGPU::mayTailCallThisCC(CalleeCC)) {
1215 LLVM_DEBUG(dbgs() << "... Calling convention cannot be tail called.\n");
1216 return false;
1217 }
1218
1219 if (any_of(CallerF.args(), [](const Argument &A) {
1220 return A.hasByValAttr() || A.hasSwiftErrorAttr();
1221 })) {
1222 LLVM_DEBUG(dbgs() << "... Cannot tail call from callers with byval "
1223 "or swifterror arguments\n");
1224 return false;
1225 }
1226
1227 // If we have -tailcallopt, then we're done.
1229 return AMDGPU::canGuaranteeTCO(CalleeCC) &&
1230 CalleeCC == CallerF.getCallingConv();
1231 }
1232
1233 // Verify that the incoming and outgoing arguments from the callee are
1234 // safe to tail call.
1235 if (!doCallerAndCalleePassArgsTheSameWay(Info, MF, InArgs)) {
1236 LLVM_DEBUG(
1237 dbgs()
1238 << "... Caller and callee have incompatible calling conventions.\n");
1239 return false;
1240 }
1241
1242 // FIXME: We need to check if any arguments passed in SGPR are uniform. If
1243 // they are not, this cannot be a tail call. If they are uniform, but may be
1244 // VGPR, we need to insert readfirstlanes.
1245 if (!areCalleeOutgoingArgsTailCallable(Info, MF, OutArgs))
1246 return false;
1247
1248 LLVM_DEBUG(dbgs() << "... Call is eligible for tail call optimization.\n");
1249 return true;
1250}
1251
1252// Insert outgoing implicit arguments for a call, by inserting copies to the
1253// implicit argument registers and adding the necessary implicit uses to the
1254// call instruction.
1257 const GCNSubtarget &ST, const SIMachineFunctionInfo &FuncInfo,
1258 CallingConv::ID CalleeCC,
1259 ArrayRef<std::pair<MCRegister, Register>> ImplicitArgRegs) const {
1260 if (!ST.hasFlatScratchEnabled()) {
1261 // Insert copies for the SRD. In the HSA case, this should be an identity
1262 // copy.
1263 auto ScratchRSrcReg = MIRBuilder.buildCopy(LLT::fixed_vector(4, 32),
1264 FuncInfo.getScratchRSrcReg());
1265
1266 auto CalleeRSrcReg = AMDGPU::isChainCC(CalleeCC)
1267 ? AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51
1268 : AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3;
1269
1270 MIRBuilder.buildCopy(CalleeRSrcReg, ScratchRSrcReg);
1271 CallInst.addReg(CalleeRSrcReg, RegState::Implicit);
1272 }
1273
1274 for (std::pair<MCRegister, Register> ArgReg : ImplicitArgRegs) {
1275 MIRBuilder.buildCopy((Register)ArgReg.first, ArgReg.second);
1276 CallInst.addReg(ArgReg.first, RegState::Implicit);
1277 }
1278}
1279
1280namespace {
1281// Chain calls have special arguments that we need to handle. These have the
1282// same index as they do in the llvm.amdgcn.cs.chain intrinsic.
1283enum ChainCallArgIdx {
1284 Exec = 1,
1285 Flags = 4,
1286 NumVGPRs = 5,
1287 FallbackExec = 6,
1288 FallbackCallee = 7,
1289};
1290} // anonymous namespace
1291
1293 MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info,
1294 SmallVectorImpl<ArgInfo> &OutArgs) const {
1295 MachineFunction &MF = MIRBuilder.getMF();
1296 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1298 const Function &F = MF.getFunction();
1299 MachineRegisterInfo &MRI = MF.getRegInfo();
1300 const SIInstrInfo *TII = ST.getInstrInfo();
1301 const SIRegisterInfo *TRI = ST.getRegisterInfo();
1303
1304 // True when we're tail calling, but without -tailcallopt.
1305 bool IsSibCall = !MF.getTarget().Options.GuaranteedTailCallOpt;
1306
1307 // Find out which ABI gets to decide where things go.
1308 CallingConv::ID CalleeCC = Info.CallConv;
1309 CCAssignFn *AssignFnFixed;
1310 CCAssignFn *AssignFnVarArg;
1311 std::tie(AssignFnFixed, AssignFnVarArg) = getAssignFnsForCC(CalleeCC, TLI);
1312
1313 MachineInstrBuilder CallSeqStart;
1314 if (!IsSibCall)
1315 CallSeqStart = MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKUP);
1316
1317 bool IsChainCall = AMDGPU::isChainCC(Info.CallConv);
1318 bool IsDynamicVGPRChainCall = false;
1319
1320 if (IsChainCall) {
1321 ArgInfo FlagsArg = Info.OrigArgs[ChainCallArgIdx::Flags];
1322 const APInt &FlagsValue = cast<ConstantInt>(FlagsArg.OrigValue)->getValue();
1323 if (FlagsValue.isZero()) {
1324 if (Info.OrigArgs.size() != 5) {
1325 LLVM_DEBUG(dbgs() << "No additional args allowed if flags == 0\n");
1326 return false;
1327 }
1328 } else if (FlagsValue.isOneBitSet(0)) {
1329 IsDynamicVGPRChainCall = true;
1330
1331 if (Info.OrigArgs.size() != 8) {
1332 LLVM_DEBUG(dbgs() << "Expected 3 additional args\n");
1333 return false;
1334 }
1335
1336 // On GFX12, we can only change the VGPR allocation for wave32.
1337 if (!ST.isWave32()) {
1338 F.getContext().diagnose(DiagnosticInfoUnsupported(
1339 F, "dynamic VGPR mode is only supported for wave32"));
1340 return false;
1341 }
1342
1343 ArgInfo FallbackExecArg = Info.OrigArgs[ChainCallArgIdx::FallbackExec];
1344 assert(FallbackExecArg.Regs.size() == 1 &&
1345 "Expected single register for fallback EXEC");
1346 if (!FallbackExecArg.Ty->isIntegerTy(ST.getWavefrontSize())) {
1347 LLVM_DEBUG(dbgs() << "Bad type for fallback EXEC\n");
1348 return false;
1349 }
1350 }
1351 }
1352
1353 unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), /*IsTailCall*/ true,
1354 ST.isWave32(), CalleeCC, IsDynamicVGPRChainCall);
1355 auto MIB = MIRBuilder.buildInstrNoInsert(Opc);
1356
1357 if (FuncInfo->isWholeWaveFunction())
1358 addOriginalExecToReturn(MF, MIB);
1359
1360 // Keep track of the index of the next operand to be added to the call
1361 unsigned CalleeIdx = MIB->getNumOperands();
1362
1363 if (!addCallTargetOperands(MIB, MIRBuilder, Info, IsDynamicVGPRChainCall))
1364 return false;
1365
1366 // Byte offset for the tail call. When we are sibcalling, this will always
1367 // be 0.
1368 MIB.addImm(0);
1369
1370 // If this is a chain call, we need to pass in the EXEC mask as well as any
1371 // other special args.
1372 if (IsChainCall) {
1373 auto AddRegOrImm = [&](const ArgInfo &Arg) {
1374 if (auto CI = dyn_cast<ConstantInt>(Arg.OrigValue)) {
1375 MIB.addImm(CI->getSExtValue());
1376 } else {
1377 MIB.addReg(Arg.Regs[0]);
1378 unsigned Idx = MIB->getNumOperands() - 1;
1379 MIB->getOperand(Idx).setReg(constrainOperandRegClass(
1380 MF, *TRI, MRI, *TII, *ST.getRegBankInfo(), *MIB, MIB->getDesc(),
1381 MIB->getOperand(Idx), Idx));
1382 }
1383 };
1384
1385 ArgInfo ExecArg = Info.OrigArgs[ChainCallArgIdx::Exec];
1386 assert(ExecArg.Regs.size() == 1 && "Too many regs for EXEC");
1387
1388 if (!ExecArg.Ty->isIntegerTy(ST.getWavefrontSize())) {
1389 LLVM_DEBUG(dbgs() << "Bad type for EXEC");
1390 return false;
1391 }
1392
1393 AddRegOrImm(ExecArg);
1394 if (IsDynamicVGPRChainCall)
1395 std::for_each(Info.OrigArgs.begin() + ChainCallArgIdx::NumVGPRs,
1396 Info.OrigArgs.end(), AddRegOrImm);
1397 }
1398
1399 // Tell the call which registers are clobbered.
1400 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CalleeCC);
1401 MIB.addRegMask(Mask);
1402
1403 // FPDiff is the byte offset of the call's argument area from the callee's.
1404 // Stores to callee stack arguments will be placed in FixedStackSlots offset
1405 // by this amount for a tail call. In a sibling call it must be 0 because the
1406 // caller will deallocate the entire stack and the callee still expects its
1407 // arguments to begin at SP+0.
1408 int FPDiff = 0;
1409
1410 // This will be 0 for sibcalls, potentially nonzero for tail calls produced
1411 // by -tailcallopt. For sibcalls, the memory operands for the call are
1412 // already available in the caller's incoming argument space.
1413 unsigned NumBytes = 0;
1414 if (!IsSibCall) {
1415 // We aren't sibcalling, so we need to compute FPDiff. We need to do this
1416 // before handling assignments, because FPDiff must be known for memory
1417 // arguments.
1418 unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
1420 CCState OutInfo(CalleeCC, false, MF, OutLocs, F.getContext());
1421
1422 // FIXME: Not accounting for callee implicit inputs
1423 OutgoingValueAssigner CalleeAssigner(AssignFnFixed, AssignFnVarArg);
1424 if (!determineAssignments(CalleeAssigner, OutArgs, OutInfo))
1425 return false;
1426
1427 // The callee will pop the argument stack as a tail call. Thus, we must
1428 // keep it 16-byte aligned.
1429 NumBytes = alignTo(OutInfo.getStackSize(), ST.getStackAlignment());
1430
1431 // FPDiff will be negative if this tail call requires more space than we
1432 // would automatically have in our incoming argument space. Positive if we
1433 // actually shrink the stack.
1434 FPDiff = NumReusableBytes - NumBytes;
1435
1436 // The stack pointer must be 16-byte aligned at all times it's used for a
1437 // memory operation, which in practice means at *all* times and in
1438 // particular across call boundaries. Therefore our own arguments started at
1439 // a 16-byte aligned SP and the delta applied for the tail call should
1440 // satisfy the same constraint.
1441 assert(isAligned(ST.getStackAlignment(), FPDiff) &&
1442 "unaligned stack on tail call");
1443 }
1444
1446 CCState CCInfo(Info.CallConv, Info.IsVarArg, MF, ArgLocs, F.getContext());
1447
1448 // We could pass MIB and directly add the implicit uses to the call
1449 // now. However, as an aesthetic choice, place implicit argument operands
1450 // after the ordinary user argument registers.
1452
1453 if (Info.CallConv != CallingConv::AMDGPU_Gfx &&
1454 Info.CallConv != CallingConv::AMDGPU_Gfx_WholeWave &&
1455 !AMDGPU::isChainCC(Info.CallConv)) {
1456 // With a fixed ABI, allocate fixed registers before user arguments.
1457 if (!passSpecialInputs(MIRBuilder, CCInfo, ImplicitArgRegs, Info))
1458 return false;
1459 }
1460
1461 // Mark the scratch resource descriptor as allocated so the CC analysis
1462 // does not assign user arguments to these registers, matching the callee.
1463 if (!ST.hasFlatScratchEnabled())
1464 CCInfo.AllocateReg(FuncInfo->getScratchRSrcReg());
1465
1466 OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg);
1467
1468 if (!determineAssignments(Assigner, OutArgs, CCInfo))
1469 return false;
1470
1471 // Do the actual argument marshalling.
1472 AMDGPUOutgoingArgHandler Handler(MIRBuilder, MRI, MIB, true, FPDiff);
1473 if (!handleAssignments(Handler, OutArgs, CCInfo, ArgLocs, MIRBuilder))
1474 return false;
1475
1476 if (Info.ConvergenceCtrlToken) {
1477 MIB.addUse(Info.ConvergenceCtrlToken, RegState::Implicit);
1478 }
1479 handleImplicitCallArguments(MIRBuilder, MIB, ST, *FuncInfo, CalleeCC,
1480 ImplicitArgRegs);
1481
1482 // If we have -tailcallopt, we need to adjust the stack. We'll do the call
1483 // sequence start and end here.
1484 if (!IsSibCall) {
1485 MIB->getOperand(CalleeIdx + 1).setImm(FPDiff);
1486 CallSeqStart.addImm(NumBytes).addImm(0);
1487 // End the call sequence *before* emitting the call. Normally, we would
1488 // tidy the frame up after the call. However, here, we've laid out the
1489 // parameters so that when SP is reset, they will be in the correct
1490 // location.
1491 MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKDOWN).addImm(NumBytes).addImm(0);
1492 }
1493
1494 // Now we can add the actual call instruction to the correct basic block.
1495 MIRBuilder.insertInstr(MIB);
1496
1497 // If this is a whole wave tail call, we need to constrain the register for
1498 // the original EXEC.
1499 if (MIB->getOpcode() == AMDGPU::SI_TCRETURN_GFX_WholeWave) {
1500 MIB->getOperand(0).setReg(
1501 constrainOperandRegClass(MF, *TRI, MRI, *TII, *ST.getRegBankInfo(),
1502 *MIB, MIB->getDesc(), MIB->getOperand(0), 0));
1503 }
1504
1505 // If Callee is a reg, since it is used by a target specific
1506 // instruction, it must have a register class matching the
1507 // constraint of that instruction.
1508
1509 // FIXME: We should define regbankselectable call instructions to handle
1510 // divergent call targets.
1511 if (MIB->getOperand(CalleeIdx).isReg()) {
1512 MIB->getOperand(CalleeIdx).setReg(constrainOperandRegClass(
1513 MF, *TRI, MRI, *TII, *ST.getRegBankInfo(), *MIB, MIB->getDesc(),
1514 MIB->getOperand(CalleeIdx), CalleeIdx));
1515 }
1516
1518 Info.LoweredTailCall = true;
1519 return true;
1520}
1521
1522/// Lower a call to the @llvm.amdgcn.cs.chain intrinsic.
1524 CallLoweringInfo &Info) const {
1525 ArgInfo Callee = Info.OrigArgs[0];
1526 ArgInfo SGPRArgs = Info.OrigArgs[2];
1527 ArgInfo VGPRArgs = Info.OrigArgs[3];
1528
1529 MachineFunction &MF = MIRBuilder.getMF();
1530 const Function &F = MF.getFunction();
1531 const DataLayout &DL = F.getDataLayout();
1532
1533 // The function to jump to is actually the first argument, so we'll change the
1534 // Callee and other info to match that before using our existing helper.
1535 const Value *CalleeV = Callee.OrigValue->stripPointerCasts();
1536 if (const Function *F = dyn_cast<Function>(CalleeV)) {
1537 Info.Callee = MachineOperand::CreateGA(F, 0);
1538 Info.CallConv = F->getCallingConv();
1539 } else {
1540 assert(Callee.Regs.size() == 1 && "Too many regs for the callee");
1541 Register CalleeReg = Callee.Regs[0];
1542 LLT CalleeTy = MIRBuilder.getMRI()->getType(CalleeReg);
1543 Register UniformCallee =
1544 MIRBuilder.buildIntrinsic(Intrinsic::amdgcn_readfirstlane, {CalleeTy})
1545 .addReg(CalleeReg)
1546 .getReg(0);
1547 Info.Callee = MachineOperand::CreateReg(UniformCallee, false);
1548 Info.CallConv = CallingConv::AMDGPU_CS_Chain; // amdgpu_cs_chain_preserve
1549 // behaves the same here.
1550 }
1551
1552 // The function that we're calling cannot be vararg (only the intrinsic is).
1553 Info.IsVarArg = false;
1554
1555 assert(
1556 all_of(SGPRArgs.Flags, [](ISD::ArgFlagsTy F) { return F.isInReg(); }) &&
1557 "SGPR arguments should be marked inreg");
1558 assert(
1559 none_of(VGPRArgs.Flags, [](ISD::ArgFlagsTy F) { return F.isInReg(); }) &&
1560 "VGPR arguments should not be marked inreg");
1561
1563 splitToValueTypes(SGPRArgs, OutArgs, DL, Info.CallConv);
1564 splitToValueTypes(VGPRArgs, OutArgs, DL, Info.CallConv);
1565
1566 Info.IsMustTailCall = true;
1567 return lowerTailCall(MIRBuilder, Info, OutArgs);
1568}
1569
1571 CallLoweringInfo &Info) const {
1572 if (Function *F = Info.CB->getCalledFunction())
1573 if (F->isIntrinsic()) {
1574 switch (F->getIntrinsicID()) {
1575 case Intrinsic::amdgcn_cs_chain:
1576 return lowerChainCall(MIRBuilder, Info);
1577 case Intrinsic::amdgcn_call_whole_wave:
1578 Info.CallConv = CallingConv::AMDGPU_Gfx_WholeWave;
1579
1580 // Get the callee from the original instruction, so it doesn't look like
1581 // this is an indirect call.
1582 Info.Callee = MachineOperand::CreateGA(
1583 cast<GlobalValue>(Info.CB->getOperand(0)), /*Offset=*/0);
1584 Info.OrigArgs.erase(Info.OrigArgs.begin());
1585 Info.IsVarArg = false;
1586 break;
1587 default:
1588 llvm_unreachable("Unexpected intrinsic call");
1589 }
1590 }
1591
1592 if (Info.IsVarArg) {
1593 LLVM_DEBUG(dbgs() << "Variadic functions not implemented\n");
1594 return false;
1595 }
1596
1597 MachineFunction &MF = MIRBuilder.getMF();
1598 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1599 const SIRegisterInfo *TRI = ST.getRegisterInfo();
1600
1601 const Function &F = MF.getFunction();
1602 MachineRegisterInfo &MRI = MF.getRegInfo();
1604 const DataLayout &DL = F.getDataLayout();
1605
1607 for (auto &OrigArg : Info.OrigArgs)
1608 splitToValueTypes(OrigArg, OutArgs, DL, Info.CallConv);
1609
1611 if (Info.CanLowerReturn && !Info.OrigRet.Ty->isVoidTy())
1612 splitToValueTypes(Info.OrigRet, InArgs, DL, Info.CallConv);
1613
1614 // If we can lower as a tail call, do that instead.
1615 bool CanTailCallOpt =
1616 isEligibleForTailCallOptimization(MIRBuilder, Info, InArgs, OutArgs);
1617
1618 // We must emit a tail call if we have musttail.
1619 if (Info.IsMustTailCall && !CanTailCallOpt) {
1620 LLVM_DEBUG(dbgs() << "Failed to lower musttail call as tail call\n");
1621 return false;
1622 }
1623
1624 Info.IsTailCall = CanTailCallOpt;
1625 if (CanTailCallOpt)
1626 return lowerTailCall(MIRBuilder, Info, OutArgs);
1627
1628 // Find out which ABI gets to decide where things go.
1629 CCAssignFn *AssignFnFixed;
1630 CCAssignFn *AssignFnVarArg;
1631 std::tie(AssignFnFixed, AssignFnVarArg) =
1632 getAssignFnsForCC(Info.CallConv, TLI);
1633
1634 MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKUP)
1635 .addImm(0)
1636 .addImm(0);
1637
1638 // Create a temporarily-floating call instruction so we can add the implicit
1639 // uses of arg registers.
1640 unsigned Opc = getCallOpcode(MF, Info.Callee.isReg(), false, ST.isWave32(),
1641 Info.CallConv);
1642
1643 auto MIB = MIRBuilder.buildInstrNoInsert(Opc);
1644 MIB.addDef(TRI->getReturnAddressReg(MF));
1645
1646 if (!Info.IsConvergent)
1648
1649 if (!addCallTargetOperands(MIB, MIRBuilder, Info))
1650 return false;
1651
1652 // Tell the call which registers are clobbered.
1653 const uint32_t *Mask = TRI->getCallPreservedMask(MF, Info.CallConv);
1654 MIB.addRegMask(Mask);
1655
1657 CCState CCInfo(Info.CallConv, Info.IsVarArg, MF, ArgLocs, F.getContext());
1658
1659 // We could pass MIB and directly add the implicit uses to the call
1660 // now. However, as an aesthetic choice, place implicit argument operands
1661 // after the ordinary user argument registers.
1663
1664 if (Info.CallConv != CallingConv::AMDGPU_Gfx &&
1665 Info.CallConv != CallingConv::AMDGPU_Gfx_WholeWave) {
1666 // With a fixed ABI, allocate fixed registers before user arguments.
1667 if (!passSpecialInputs(MIRBuilder, CCInfo, ImplicitArgRegs, Info))
1668 return false;
1669 }
1670
1671 // Mark the scratch resource descriptor as allocated so the CC analysis
1672 // does not assign user arguments to these registers, matching the callee.
1673 if (!ST.hasFlatScratchEnabled()) {
1674 const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
1675 CCInfo.AllocateReg(FuncInfo->getScratchRSrcReg());
1676 }
1677
1678 // Do the actual argument marshalling.
1679 OutgoingValueAssigner Assigner(AssignFnFixed, AssignFnVarArg);
1680 if (!determineAssignments(Assigner, OutArgs, CCInfo))
1681 return false;
1682
1683 AMDGPUOutgoingArgHandler Handler(MIRBuilder, MRI, MIB, false);
1684 if (!handleAssignments(Handler, OutArgs, CCInfo, ArgLocs, MIRBuilder))
1685 return false;
1686
1688
1689 if (Info.ConvergenceCtrlToken) {
1690 MIB.addUse(Info.ConvergenceCtrlToken, RegState::Implicit);
1691 }
1692 handleImplicitCallArguments(MIRBuilder, MIB, ST, *MFI, Info.CallConv,
1693 ImplicitArgRegs);
1694
1695 // Get a count of how many bytes are to be pushed on the stack.
1696 unsigned NumBytes = CCInfo.getStackSize();
1697
1698 // If Callee is a reg, since it is used by a target specific
1699 // instruction, it must have a register class matching the
1700 // constraint of that instruction.
1701
1702 // FIXME: We should define regbankselectable call instructions to handle
1703 // divergent call targets.
1704 if (MIB->getOperand(1).isReg()) {
1705 MIB->getOperand(1).setReg(constrainOperandRegClass(
1706 MF, *TRI, MRI, *ST.getInstrInfo(),
1707 *ST.getRegBankInfo(), *MIB, MIB->getDesc(), MIB->getOperand(1),
1708 1));
1709 }
1710
1711 // Now we can add the actual call instruction to the correct position.
1712 MIRBuilder.insertInstr(MIB);
1713
1714 // Finally we can copy the returned value back into its virtual-register. In
1715 // symmetry with the arguments, the physical register must be an
1716 // implicit-define of the call instruction.
1717 if (Info.CanLowerReturn && !Info.OrigRet.Ty->isVoidTy()) {
1718 CCAssignFn *RetAssignFn = TLI.CCAssignFnForReturn(Info.CallConv,
1719 Info.IsVarArg);
1720 IncomingValueAssigner Assigner(RetAssignFn);
1721 CallReturnHandler Handler(MIRBuilder, MRI, MIB);
1722 if (!determineAndHandleAssignments(Handler, Assigner, InArgs, MIRBuilder,
1723 Info.CallConv, Info.IsVarArg))
1724 return false;
1725 }
1726
1727 uint64_t CalleePopBytes = NumBytes;
1728
1729 MIRBuilder.buildInstr(AMDGPU::ADJCALLSTACKDOWN)
1730 .addImm(0)
1731 .addImm(CalleePopBytes);
1732
1733 if (!Info.CanLowerReturn) {
1734 insertSRetLoads(MIRBuilder, Info.OrigRet.Ty, Info.OrigRet.Regs,
1735 Info.DemoteRegister, Info.DemoteStackIndex);
1736 }
1737
1738 return true;
1739}
1740
1741void AMDGPUCallLowering::addOriginalExecToReturn(
1742 MachineFunction &MF, MachineInstrBuilder &Ret) const {
1743 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
1744 const SIInstrInfo *TII = ST.getInstrInfo();
1745 const MachineInstr *Setup = TII->getWholeWaveFunctionSetup(MF);
1746 Ret.addReg(Setup->getOperand(0).getReg());
1747}
static unsigned getCallOpcode(const MachineFunction &CallerF, bool IsIndirect, bool IsTailCall, std::optional< CallLowering::PtrAuthInfo > &PAI, MachineRegisterInfo &MRI)
static std::pair< CCAssignFn *, CCAssignFn * > getAssignFnsForCC(CallingConv::ID CC, const AArch64TargetLowering &TLI)
Returns a pair containing the fixed CCAssignFn and the vararg CCAssignFn for CC.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static ISD::NodeType extOpcodeToISDExtOpcode(unsigned MIOpc)
static void allocateHSAUserSGPRs(CCState &CCInfo, MachineIRBuilder &B, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info)
static bool addCallTargetOperands(MachineInstrBuilder &CallInst, MachineIRBuilder &MIRBuilder, AMDGPUCallLowering::CallLoweringInfo &Info, bool IsDynamicVGPRChainCall=false)
This file describes how to lower LLVM calls to machine code calls.
This file declares the targeting of the Machinelegalizer class for AMDGPU.
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static constexpr MCPhysReg SPReg
Interface definition for SIRegisterInfo.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
AMDGPUCallLowering(const TargetLowering &TLI)
bool lowerTailCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info, SmallVectorImpl< ArgInfo > &OutArgs) const
bool isEligibleForTailCallOptimization(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info, SmallVectorImpl< ArgInfo > &InArgs, SmallVectorImpl< ArgInfo > &OutArgs) const
Returns true if the call can be lowered as a tail call.
bool lowerFormalArgumentsKernel(MachineIRBuilder &B, const Function &F, ArrayRef< ArrayRef< Register > > VRegs) const
bool lowerReturn(MachineIRBuilder &B, const Value *Val, ArrayRef< Register > VRegs, FunctionLoweringInfo &FLI) const override
This hook behaves as the extended lowerReturn function, but for targets that do not support swifterro...
void handleImplicitCallArguments(MachineIRBuilder &MIRBuilder, MachineInstrBuilder &CallInst, const GCNSubtarget &ST, const SIMachineFunctionInfo &MFI, CallingConv::ID CalleeCC, ArrayRef< std::pair< MCRegister, Register > > ImplicitArgRegs) const
bool areCalleeOutgoingArgsTailCallable(CallLoweringInfo &Info, MachineFunction &MF, SmallVectorImpl< ArgInfo > &OutArgs) const
bool lowerChainCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info) const
Lower a call to the @llvm.amdgcn.cs.chain intrinsic.
bool passSpecialInputs(MachineIRBuilder &MIRBuilder, CCState &CCInfo, SmallVectorImpl< std::pair< MCRegister, Register > > &ArgRegs, CallLoweringInfo &Info) const
bool lowerFormalArguments(MachineIRBuilder &B, const Function &F, ArrayRef< ArrayRef< Register > > VRegs, FunctionLoweringInfo &FLI) const override
This hook must be implemented to lower the incoming (formal) arguments, described by VRegs,...
bool lowerCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info) const override
This hook must be implemented to lower the given call instruction, including argument and return valu...
bool doCallerAndCalleePassArgsTheSameWay(CallLoweringInfo &Info, MachineFunction &MF, SmallVectorImpl< ArgInfo > &InArgs) const
static std::optional< uint32_t > getLDSKernelIdMetadata(const Function &F)
unsigned getExplicitKernelArgOffset() const
Returns the offset in bytes from the start of the input buffer of the first explicit kernel argument.
static CCAssignFn * CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg)
Selects the correct CCAssignFn for a given CallingConvention value.
Class for arbitrary precision integers.
Definition APInt.h:78
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
bool isOneBitSet(unsigned BitNo) const
Determine if this APInt Value only has the specified bit set.
Definition APInt.h:367
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
CCState - This class holds information needed while lowering arguments and return values.
unsigned getFirstUnallocated(ArrayRef< MCPhysReg > Regs) const
getFirstUnallocated - Return the index of the first unallocated register in the set,...
MCRegister AllocateReg(MCPhysReg Reg)
AllocateReg - Attempt to allocate one register.
uint64_t getStackSize() const
Returns the size of the currently allocated portion of the stack.
CCValAssign - Represent assignment of one arg/retval to a location.
LocInfo getLocInfo() const
static CCValAssign getReg(unsigned ValNo, MVT ValVT, MCRegister Reg, MVT LocVT, LocInfo HTP, bool IsCustom=false)
int64_t getLocMemOffset() const
This class represents a function call, abstracting a target machine's calling convention.
void insertSRetLoads(MachineIRBuilder &MIRBuilder, Type *RetTy, ArrayRef< Register > VRegs, Register DemoteReg, int FI) const
Load the returned value from the stack into virtual registers in VRegs.
bool handleAssignments(ValueHandler &Handler, SmallVectorImpl< ArgInfo > &Args, CCState &CCState, SmallVectorImpl< CCValAssign > &ArgLocs, MachineIRBuilder &MIRBuilder, ArrayRef< Register > ThisReturnRegs={}) const
Use Handler to insert code to handle the argument/return values represented by Args.
bool resultsCompatible(CallLoweringInfo &Info, MachineFunction &MF, SmallVectorImpl< ArgInfo > &InArgs, ValueAssigner &CalleeAssigner, ValueAssigner &CallerAssigner) const
void insertSRetIncomingArgument(const Function &F, SmallVectorImpl< ArgInfo > &SplitArgs, Register &DemoteReg, MachineRegisterInfo &MRI, const DataLayout &DL) const
Insert the hidden sret ArgInfo to the beginning of SplitArgs.
void splitToValueTypes(const ArgInfo &OrigArgInfo, SmallVectorImpl< ArgInfo > &SplitArgs, const DataLayout &DL, CallingConv::ID CallConv, SmallVectorImpl< TypeSize > *Offsets=nullptr) const
Break OrigArgInfo into one or more pieces the calling convention can process, returned in SplitArgs.
bool determineAndHandleAssignments(ValueHandler &Handler, ValueAssigner &Assigner, SmallVectorImpl< ArgInfo > &Args, MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv, bool IsVarArg, ArrayRef< Register > ThisReturnRegs={}) const
Invoke ValueAssigner::assignArg on each of the given Args and then use Handler to move them to the as...
void insertSRetStores(MachineIRBuilder &MIRBuilder, Type *RetTy, ArrayRef< Register > VRegs, Register DemoteReg) const
Store the return value given by VRegs into stack starting at the offset specified in DemoteReg.
bool parametersInCSRMatch(const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, const SmallVectorImpl< CCValAssign > &ArgLocs, const SmallVectorImpl< ArgInfo > &OutVals) const
Check whether parameters to a call that are passed in callee saved registers are the same as from the...
bool determineAssignments(ValueAssigner &Assigner, SmallVectorImpl< ArgInfo > &Args, CCState &CCInfo) const
Analyze the argument list in Args, using Assigner to populate CCInfo.
bool checkReturn(CCState &CCInfo, SmallVectorImpl< BaseArgInfo > &Outs, CCAssignFn *Fn) const
CallLowering(const TargetLowering *TLI)
const TargetLowering * getTLI() const
Getter for generic TargetLowering class.
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL, const FuncInfoTy &FuncInfo) const
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Diagnostic information for unsupported feature in backend.
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
Register DemoteRegister
DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg allocated to hold a pointer to ...
bool CanLowerReturn
CanLowerReturn - true iff the function's return value can be lowered to registers.
iterator_range< arg_iterator > args()
Definition Function.h:866
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:353
const SIRegisterInfo * getRegisterInfo() const override
bool hasPrivateSegmentBuffer() const
unsigned getAddressSpace() const
constexpr unsigned getScalarSizeInBits() const
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
constexpr bool isInteger() const
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr ElementCount getElementCount() const
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
static LLT integer(unsigned SizeInBits)
static constexpr LLT float32()
Get a 32-bit IEEE float value.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
LLVM_ABI int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
void setHasTailCall(bool V=true)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Helper class to build MachineInstr.
MachineInstrBuilder insertInstr(MachineInstrBuilder MIB)
Insert an existing instruction at the insertion point.
MachineInstrBuilder buildGlobalValue(const DstOp &Res, const GlobalValue *GV)
Build and insert Res = G_GLOBAL_VALUE GV.
MachineInstrBuilder buildUndef(const DstOp &Res)
Build and insert Res = IMPLICIT_DEF.
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef< Register > Res, bool HasSideEffects, bool isConvergent)
Build and insert a G_INTRINSIC instruction.
MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_PTR_ADD Op0, Op1.
MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ANYEXT Op0.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_OR Op0, Op1.
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don't insert <empty> = Opcode <empty>.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
const TargetRegisterInfo * getTargetRegisterInfo() const
void addLiveIn(MCRegister Reg, Register vreg=Register())
addLiveIn - Add the specified register as a live-in.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Register getScratchRSrcReg() const
Returns the physical register reserved for use as the resource descriptor for scratch accesses.
MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const
AMDGPUFunctionArgInfo & getArgInfo()
MachinePointerInfo getKernargSegmentPtrInfo(MachineFunction &MF) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:888
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
TargetOptions Options
unsigned GuaranteedTailCallOpt
GuaranteedTailCallOpt - This flag is enabled when -tailcallopt is specified on the commandline.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ CONSTANT_ADDRESS
Address space for constant memory (VTX2).
@ PRIVATE_ADDRESS
Address space for private memory.
LLVM_READNONE constexpr bool isShader(CallingConv::ID CC)
LLVM_READNONE constexpr bool mayTailCallThisCC(CallingConv::ID CC)
Return true if we might ever do TCO for calls with this calling convention.
LLVM_READNONE constexpr bool isKernel(CallingConv::ID CC)
LLVM_READNONE constexpr bool isEntryFunctionCC(CallingConv::ID CC)
LLVM_READNONE constexpr bool isChainCC(CallingConv::ID CC)
LLVM_READNONE constexpr bool canGuaranteeTCO(CallingConv::ID CC)
LLVM_READNONE constexpr bool isGraphics(CallingConv::ID CC)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
@ AMDGPU_Gfx
Used for AMD graphics targets.
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:863
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:854
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:860
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:573
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI Register constrainOperandRegClass(const MachineFunction &MF, const TargetRegisterInfo &TRI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, const TargetRegisterClass &RegClass, MachineOperand &RegMO)
Constrain the Register operand OpIdx, so that it is now constrained to the TargetRegisterClass passed...
Definition Utils.cpp:60
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
@ Implicit
Not emitted register (e.g. carry, or temporary result).
LLVM_ABI void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
LLVM_ABI Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
Definition Utils.cpp:844
std::tuple< const ArgDescriptor *, const TargetRegisterClass *, LLT > getPreloadedValue(PreloadedValue Value) const
static const AMDGPUFunctionArgInfo FixedABIFunctionInfo
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
MCRegister getRegister() const
static ArgDescriptor createArg(const ArgDescriptor &Arg, unsigned Mask)
Helper struct shared between Function Specialization and SCCP Solver.
Definition SCCPSolver.h:42
const Value * OrigValue
Optionally track the original IR value for the argument.
SmallVector< Register, 4 > Regs
SmallVector< ISD::ArgFlagsTy, 4 > Flags
Base class for ValueHandlers used for arguments coming into the current function, or for return value...
Base class for ValueHandlers used for arguments passed to a function call, or for return values.
uint64_t StackSize
The size of the currently allocated portion of the stack.
Register extendRegister(Register ValReg, const CCValAssign &VA, unsigned MaxSizeBits=0)
Extend a register to the location type given in VA, capped at extending to at most MaxSize bits.
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset, uint8_t ID=0)
Stack pointer relative access.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106