xref: /src/contrib/llvm-project/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1044eb2f6SDimitry Andric //===- SIMachineFunctionInfo.cpp - SI Machine Function Info ---------------===//
24a16efa3SDimitry 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
64a16efa3SDimitry Andric //
74a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
84a16efa3SDimitry Andric 
94a16efa3SDimitry Andric #include "SIMachineFunctionInfo.h"
10344a3780SDimitry Andric #include "AMDGPUSubtarget.h"
11b1c73532SDimitry Andric #include "AMDGPUTargetMachine.h"
12b1c73532SDimitry Andric #include "GCNSubtarget.h"
13344a3780SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
14b1c73532SDimitry Andric #include "SIRegisterInfo.h"
15344a3780SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
16344a3780SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
17b1c73532SDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h"
18344a3780SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
19344a3780SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
20344a3780SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
21344a3780SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
22344a3780SDimitry Andric #include "llvm/IR/CallingConv.h"
23344a3780SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
24344a3780SDimitry Andric #include "llvm/IR/Function.h"
25344a3780SDimitry Andric #include <cassert>
26e3b55780SDimitry Andric #include <optional>
27344a3780SDimitry Andric #include <vector>
285ca98fd9SDimitry Andric 
29ac9a064cSDimitry Andric enum { MAX_LANES = 64 };
304a16efa3SDimitry Andric 
314a16efa3SDimitry Andric using namespace llvm;
324a16efa3SDimitry Andric 
getTM(const GCNSubtarget * STI)33e3b55780SDimitry Andric const GCNTargetMachine &getTM(const GCNSubtarget *STI) {
34e3b55780SDimitry Andric   const SITargetLowering *TLI = STI->getTargetLowering();
35e3b55780SDimitry Andric   return static_cast<const GCNTargetMachine &>(TLI->getTargetMachine());
36e3b55780SDimitry Andric }
37e3b55780SDimitry Andric 
SIMachineFunctionInfo(const Function & F,const GCNSubtarget * STI)38e3b55780SDimitry Andric SIMachineFunctionInfo::SIMachineFunctionInfo(const Function &F,
39e3b55780SDimitry Andric                                              const GCNSubtarget *STI)
40312c0ed1SDimitry Andric     : AMDGPUMachineFunction(F, *STI), Mode(F, *STI), GWSResourcePSV(getTM(STI)),
41b1c73532SDimitry Andric       UserSGPRInfo(F, *STI), WorkGroupIDX(false), WorkGroupIDY(false),
42b1c73532SDimitry Andric       WorkGroupIDZ(false), WorkGroupInfo(false), LDSKernelId(false),
43b1c73532SDimitry Andric       PrivateSegmentWaveByteOffset(false), WorkItemIDX(false),
44b1c73532SDimitry Andric       WorkItemIDY(false), WorkItemIDZ(false), ImplicitArgPtr(false),
45b1c73532SDimitry Andric       GITPtrHigh(0xffffffff), HighBitsOf32BitAddress(0) {
46e3b55780SDimitry Andric   const GCNSubtarget &ST = *static_cast<const GCNSubtarget *>(STI);
47044eb2f6SDimitry Andric   FlatWorkGroupSizes = ST.getFlatWorkGroupSizes(F);
48044eb2f6SDimitry Andric   WavesPerEU = ST.getWavesPerEU(F);
49ac9a064cSDimitry Andric   MaxNumWorkGroups = ST.getMaxNumWorkGroups(F);
50ac9a064cSDimitry Andric   assert(MaxNumWorkGroups.size() == 3);
51dd58ef01SDimitry Andric 
52cfca06d7SDimitry Andric   Occupancy = ST.computeOccupancy(F, getLDSSize());
53eb11fae6SDimitry Andric   CallingConv::ID CC = F.getCallingConv();
54eb11fae6SDimitry Andric 
557fa27ce4SDimitry Andric   VRegFlags.reserve(1024);
567fa27ce4SDimitry Andric 
57c0981da4SDimitry Andric   const bool IsKernel = CC == CallingConv::AMDGPU_KERNEL ||
58c0981da4SDimitry Andric                         CC == CallingConv::SPIR_KERNEL;
59cfca06d7SDimitry Andric 
60c0981da4SDimitry Andric   if (IsKernel) {
61eb11fae6SDimitry Andric     WorkGroupIDX = true;
62eb11fae6SDimitry Andric     WorkItemIDX = true;
63eb11fae6SDimitry Andric   } else if (CC == CallingConv::AMDGPU_PS) {
64eb11fae6SDimitry Andric     PSInputAddr = AMDGPU::getInitialPSInputAddr(F);
65eb11fae6SDimitry Andric   }
66eb11fae6SDimitry Andric 
67145449b1SDimitry Andric   MayNeedAGPRs = ST.hasMAIInsts();
68145449b1SDimitry Andric 
69b1c73532SDimitry Andric   if (AMDGPU::isChainCC(CC)) {
70b1c73532SDimitry Andric     // Chain functions don't receive an SP from their caller, but are free to
71b1c73532SDimitry Andric     // set one up. For now, we can use s32 to match what amdgpu_gfx functions
72b1c73532SDimitry Andric     // would use if called, but this can be revisited.
73b1c73532SDimitry Andric     // FIXME: Only reserve this if we actually need it.
74b1c73532SDimitry Andric     StackPtrOffsetReg = AMDGPU::SGPR32;
75b1c73532SDimitry Andric 
76b1c73532SDimitry Andric     ScratchRSrcReg = AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51;
77b1c73532SDimitry Andric 
78b1c73532SDimitry Andric     ArgInfo.PrivateSegmentBuffer =
79b1c73532SDimitry Andric         ArgDescriptor::createRegister(ScratchRSrcReg);
80b1c73532SDimitry Andric 
81b1c73532SDimitry Andric     ImplicitArgPtr = false;
82b1c73532SDimitry Andric   } else if (!isEntryFunction()) {
8377fc4c14SDimitry Andric     if (CC != CallingConv::AMDGPU_Gfx)
84344a3780SDimitry Andric       ArgInfo = AMDGPUArgumentUsageInfo::FixedABIFunctionInfo;
85344a3780SDimitry Andric 
86cfca06d7SDimitry Andric     FrameOffsetReg = AMDGPU::SGPR33;
8708bbd35aSDimitry Andric     StackPtrOffsetReg = AMDGPU::SGPR32;
8893c91e39SDimitry Andric 
89b60736ecSDimitry Andric     if (!ST.enableFlatScratch()) {
90b60736ecSDimitry Andric       // Non-entry functions have no special inputs for now, other registers
91b60736ecSDimitry Andric       // required for scratch access.
92b60736ecSDimitry Andric       ScratchRSrcReg = AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3;
93b60736ecSDimitry Andric 
94044eb2f6SDimitry Andric       ArgInfo.PrivateSegmentBuffer =
95044eb2f6SDimitry Andric         ArgDescriptor::createRegister(ScratchRSrcReg);
96b60736ecSDimitry Andric     }
97044eb2f6SDimitry Andric 
98c0981da4SDimitry Andric     if (!F.hasFnAttribute("amdgpu-no-implicitarg-ptr"))
99044eb2f6SDimitry Andric       ImplicitArgPtr = true;
100044eb2f6SDimitry Andric   } else {
101c0981da4SDimitry Andric     ImplicitArgPtr = false;
102eb11fae6SDimitry Andric     MaxKernArgAlign = std::max(ST.getAlignmentForImplicitArgPtr(),
103eb11fae6SDimitry Andric                                MaxKernArgAlign);
104145449b1SDimitry Andric 
105145449b1SDimitry Andric     if (ST.hasGFX90AInsts() &&
106145449b1SDimitry Andric         ST.getMaxNumVGPRs(F) <= AMDGPU::VGPR_32RegClass.getNumRegs() &&
107e3b55780SDimitry Andric         !mayUseAGPRs(F))
108145449b1SDimitry Andric       MayNeedAGPRs = false; // We will select all MAI with VGPR operands.
109b5630dbaSDimitry Andric   }
110c0981da4SDimitry Andric 
1117fa27ce4SDimitry Andric   if (!AMDGPU::isGraphics(CC) ||
112ac9a064cSDimitry Andric       ((CC == CallingConv::AMDGPU_CS || CC == CallingConv::AMDGPU_Gfx) &&
113ac9a064cSDimitry Andric        ST.hasArchitectedSGPRs())) {
114c0981da4SDimitry Andric     if (IsKernel || !F.hasFnAttribute("amdgpu-no-workgroup-id-x"))
11593c91e39SDimitry Andric       WorkGroupIDX = true;
11693c91e39SDimitry Andric 
117c0981da4SDimitry Andric     if (!F.hasFnAttribute("amdgpu-no-workgroup-id-y"))
118dd58ef01SDimitry Andric       WorkGroupIDY = true;
119dd58ef01SDimitry Andric 
120c0981da4SDimitry Andric     if (!F.hasFnAttribute("amdgpu-no-workgroup-id-z"))
121dd58ef01SDimitry Andric       WorkGroupIDZ = true;
1227fa27ce4SDimitry Andric   }
123dd58ef01SDimitry Andric 
1247fa27ce4SDimitry Andric   if (!AMDGPU::isGraphics(CC)) {
125c0981da4SDimitry Andric     if (IsKernel || !F.hasFnAttribute("amdgpu-no-workitem-id-x"))
12693c91e39SDimitry Andric       WorkItemIDX = true;
12793c91e39SDimitry Andric 
1286f8fc217SDimitry Andric     if (!F.hasFnAttribute("amdgpu-no-workitem-id-y") &&
1296f8fc217SDimitry Andric         ST.getMaxWorkitemID(F, 1) != 0)
130dd58ef01SDimitry Andric       WorkItemIDY = true;
131dd58ef01SDimitry Andric 
1326f8fc217SDimitry Andric     if (!F.hasFnAttribute("amdgpu-no-workitem-id-z") &&
1336f8fc217SDimitry Andric         ST.getMaxWorkitemID(F, 2) != 0)
134dd58ef01SDimitry Andric       WorkItemIDZ = true;
135c0981da4SDimitry Andric 
1364b4fe385SDimitry Andric     if (!IsKernel && !F.hasFnAttribute("amdgpu-no-lds-kernel-id"))
1374b4fe385SDimitry Andric       LDSKernelId = true;
138cfca06d7SDimitry Andric   }
139dd58ef01SDimitry Andric 
14093c91e39SDimitry Andric   if (isEntryFunction()) {
14101095a5dSDimitry Andric     // X, XY, and XYZ are the only supported combinations, so make sure Y is
14201095a5dSDimitry Andric     // enabled if Z is.
14301095a5dSDimitry Andric     if (WorkItemIDZ)
14401095a5dSDimitry Andric       WorkItemIDY = true;
14501095a5dSDimitry Andric 
146344a3780SDimitry Andric     if (!ST.flatScratchIsArchitected()) {
147dd58ef01SDimitry Andric       PrivateSegmentWaveByteOffset = true;
148dd58ef01SDimitry Andric 
149c46e6a59SDimitry Andric       // HS and GS always have the scratch wave offset in SGPR5 on GFX9.
150c46e6a59SDimitry Andric       if (ST.getGeneration() >= AMDGPUSubtarget::GFX9 &&
151c46e6a59SDimitry Andric           (CC == CallingConv::AMDGPU_HS || CC == CallingConv::AMDGPU_GS))
152d8e91e46SDimitry Andric         ArgInfo.PrivateSegmentWaveByteOffset =
153d8e91e46SDimitry Andric             ArgDescriptor::createRegister(AMDGPU::SGPR5);
15493c91e39SDimitry Andric     }
155344a3780SDimitry Andric   }
156c46e6a59SDimitry Andric 
157044eb2f6SDimitry Andric   Attribute A = F.getFnAttribute("amdgpu-git-ptr-high");
158044eb2f6SDimitry Andric   StringRef S = A.getValueAsString();
159044eb2f6SDimitry Andric   if (!S.empty())
160044eb2f6SDimitry Andric     S.consumeInteger(0, GITPtrHigh);
161eb11fae6SDimitry Andric 
162eb11fae6SDimitry Andric   A = F.getFnAttribute("amdgpu-32bit-address-high-bits");
163eb11fae6SDimitry Andric   S = A.getValueAsString();
164eb11fae6SDimitry Andric   if (!S.empty())
165eb11fae6SDimitry Andric     S.consumeInteger(0, HighBitsOf32BitAddress);
166e6d15924SDimitry Andric 
167145449b1SDimitry Andric   // On GFX908, in order to guarantee copying between AGPRs, we need a scratch
168145449b1SDimitry Andric   // VGPR available at all times. For now, reserve highest available VGPR. After
169145449b1SDimitry Andric   // RA, shift it to the lowest available unused VGPR if the one exist.
170145449b1SDimitry Andric   if (ST.hasMAIInsts() && !ST.hasGFX90AInsts()) {
171145449b1SDimitry Andric     VGPRForAGPRCopy =
172145449b1SDimitry Andric         AMDGPU::VGPR_32RegClass.getRegister(ST.getMaxNumVGPRs(F) - 1);
173145449b1SDimitry Andric   }
174145449b1SDimitry Andric }
175145449b1SDimitry Andric 
clone(BumpPtrAllocator & Allocator,MachineFunction & DestMF,const DenseMap<MachineBasicBlock *,MachineBasicBlock * > & Src2DstMBB) const176145449b1SDimitry Andric MachineFunctionInfo *SIMachineFunctionInfo::clone(
177145449b1SDimitry Andric     BumpPtrAllocator &Allocator, MachineFunction &DestMF,
178145449b1SDimitry Andric     const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
179145449b1SDimitry Andric     const {
180145449b1SDimitry Andric   return DestMF.cloneInfo<SIMachineFunctionInfo>(*this);
181eb11fae6SDimitry Andric }
182eb11fae6SDimitry Andric 
limitOccupancy(const MachineFunction & MF)183eb11fae6SDimitry Andric void SIMachineFunctionInfo::limitOccupancy(const MachineFunction &MF) {
184eb11fae6SDimitry Andric   limitOccupancy(getMaxWavesPerEU());
185eb11fae6SDimitry Andric   const GCNSubtarget& ST = MF.getSubtarget<GCNSubtarget>();
186eb11fae6SDimitry Andric   limitOccupancy(ST.getOccupancyWithLocalMemSize(getLDSSize(),
187eb11fae6SDimitry Andric                  MF.getFunction()));
18893c91e39SDimitry Andric }
189dd58ef01SDimitry Andric 
addPrivateSegmentBuffer(const SIRegisterInfo & TRI)190cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addPrivateSegmentBuffer(
191dd58ef01SDimitry Andric   const SIRegisterInfo &TRI) {
192044eb2f6SDimitry Andric   ArgInfo.PrivateSegmentBuffer =
193044eb2f6SDimitry Andric     ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
1941d5ae102SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SGPR_128RegClass));
195dd58ef01SDimitry Andric   NumUserSGPRs += 4;
196044eb2f6SDimitry Andric   return ArgInfo.PrivateSegmentBuffer.getRegister();
197dd58ef01SDimitry Andric }
198dd58ef01SDimitry Andric 
addDispatchPtr(const SIRegisterInfo & TRI)199cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addDispatchPtr(const SIRegisterInfo &TRI) {
200044eb2f6SDimitry Andric   ArgInfo.DispatchPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
201044eb2f6SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
202dd58ef01SDimitry Andric   NumUserSGPRs += 2;
203044eb2f6SDimitry Andric   return ArgInfo.DispatchPtr.getRegister();
204dd58ef01SDimitry Andric }
205dd58ef01SDimitry Andric 
addQueuePtr(const SIRegisterInfo & TRI)206cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addQueuePtr(const SIRegisterInfo &TRI) {
207044eb2f6SDimitry Andric   ArgInfo.QueuePtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
208044eb2f6SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
209dd58ef01SDimitry Andric   NumUserSGPRs += 2;
210044eb2f6SDimitry Andric   return ArgInfo.QueuePtr.getRegister();
211dd58ef01SDimitry Andric }
212dd58ef01SDimitry Andric 
addKernargSegmentPtr(const SIRegisterInfo & TRI)213cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addKernargSegmentPtr(const SIRegisterInfo &TRI) {
214044eb2f6SDimitry Andric   ArgInfo.KernargSegmentPtr
215044eb2f6SDimitry Andric     = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
216044eb2f6SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
217dd58ef01SDimitry Andric   NumUserSGPRs += 2;
218044eb2f6SDimitry Andric   return ArgInfo.KernargSegmentPtr.getRegister();
219dd58ef01SDimitry Andric }
2205ca98fd9SDimitry Andric 
addDispatchID(const SIRegisterInfo & TRI)221cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addDispatchID(const SIRegisterInfo &TRI) {
222044eb2f6SDimitry Andric   ArgInfo.DispatchID = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
223044eb2f6SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
224b915e9e0SDimitry Andric   NumUserSGPRs += 2;
225044eb2f6SDimitry Andric   return ArgInfo.DispatchID.getRegister();
226b915e9e0SDimitry Andric }
227b915e9e0SDimitry Andric 
addFlatScratchInit(const SIRegisterInfo & TRI)228cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addFlatScratchInit(const SIRegisterInfo &TRI) {
229044eb2f6SDimitry Andric   ArgInfo.FlatScratchInit = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
230044eb2f6SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
23101095a5dSDimitry Andric   NumUserSGPRs += 2;
232044eb2f6SDimitry Andric   return ArgInfo.FlatScratchInit.getRegister();
23301095a5dSDimitry Andric }
23401095a5dSDimitry Andric 
addPrivateSegmentSize(const SIRegisterInfo & TRI)235ac9a064cSDimitry Andric Register SIMachineFunctionInfo::addPrivateSegmentSize(const SIRegisterInfo &TRI) {
236ac9a064cSDimitry Andric   ArgInfo.PrivateSegmentSize = ArgDescriptor::createRegister(getNextUserSGPR());
237ac9a064cSDimitry Andric   NumUserSGPRs += 1;
238ac9a064cSDimitry Andric   return ArgInfo.PrivateSegmentSize.getRegister();
239ac9a064cSDimitry Andric }
240ac9a064cSDimitry Andric 
addImplicitBufferPtr(const SIRegisterInfo & TRI)241cfca06d7SDimitry Andric Register SIMachineFunctionInfo::addImplicitBufferPtr(const SIRegisterInfo &TRI) {
242044eb2f6SDimitry Andric   ArgInfo.ImplicitBufferPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
243044eb2f6SDimitry Andric     getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
244aac4ca60SDimitry Andric   NumUserSGPRs += 2;
245044eb2f6SDimitry Andric   return ArgInfo.ImplicitBufferPtr.getRegister();
246044eb2f6SDimitry Andric }
247044eb2f6SDimitry Andric 
addLDSKernelId()2484b4fe385SDimitry Andric Register SIMachineFunctionInfo::addLDSKernelId() {
2494b4fe385SDimitry Andric   ArgInfo.LDSKernelId = ArgDescriptor::createRegister(getNextUserSGPR());
2504b4fe385SDimitry Andric   NumUserSGPRs += 1;
2514b4fe385SDimitry Andric   return ArgInfo.LDSKernelId.getRegister();
2524b4fe385SDimitry Andric }
2534b4fe385SDimitry Andric 
addPreloadedKernArg(const SIRegisterInfo & TRI,const TargetRegisterClass * RC,unsigned AllocSizeDWord,int KernArgIdx,int PaddingSGPRs)254b1c73532SDimitry Andric SmallVectorImpl<MCRegister> *SIMachineFunctionInfo::addPreloadedKernArg(
255b1c73532SDimitry Andric     const SIRegisterInfo &TRI, const TargetRegisterClass *RC,
256b1c73532SDimitry Andric     unsigned AllocSizeDWord, int KernArgIdx, int PaddingSGPRs) {
257b1c73532SDimitry Andric   assert(!ArgInfo.PreloadKernArgs.count(KernArgIdx) &&
258b1c73532SDimitry Andric          "Preload kernel argument allocated twice.");
259b1c73532SDimitry Andric   NumUserSGPRs += PaddingSGPRs;
260b1c73532SDimitry Andric   // If the available register tuples are aligned with the kernarg to be
261b1c73532SDimitry Andric   // preloaded use that register, otherwise we need to use a set of SGPRs and
262b1c73532SDimitry Andric   // merge them.
263b1c73532SDimitry Andric   Register PreloadReg =
264b1c73532SDimitry Andric       TRI.getMatchingSuperReg(getNextUserSGPR(), AMDGPU::sub0, RC);
265b1c73532SDimitry Andric   if (PreloadReg &&
266b1c73532SDimitry Andric       (RC == &AMDGPU::SReg_32RegClass || RC == &AMDGPU::SReg_64RegClass)) {
267b1c73532SDimitry Andric     ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(PreloadReg);
268b1c73532SDimitry Andric     NumUserSGPRs += AllocSizeDWord;
269b1c73532SDimitry Andric   } else {
270b1c73532SDimitry Andric     for (unsigned I = 0; I < AllocSizeDWord; ++I) {
271b1c73532SDimitry Andric       ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(getNextUserSGPR());
272b1c73532SDimitry Andric       NumUserSGPRs++;
273b1c73532SDimitry Andric     }
274b1c73532SDimitry Andric   }
275b1c73532SDimitry Andric 
276b1c73532SDimitry Andric   // Track the actual number of SGPRs that HW will preload to.
277b1c73532SDimitry Andric   UserSGPRInfo.allocKernargPreloadSGPRs(AllocSizeDWord + PaddingSGPRs);
278b1c73532SDimitry Andric   return &ArgInfo.PreloadKernArgs[KernArgIdx].Regs;
279b1c73532SDimitry Andric }
280b1c73532SDimitry Andric 
allocateWWMSpill(MachineFunction & MF,Register VGPR,uint64_t Size,Align Alignment)281e3b55780SDimitry Andric void SIMachineFunctionInfo::allocateWWMSpill(MachineFunction &MF, Register VGPR,
282e3b55780SDimitry Andric                                              uint64_t Size, Align Alignment) {
283e3b55780SDimitry Andric   // Skip if it is an entry function or the register is already added.
284e3b55780SDimitry Andric   if (isEntryFunction() || WWMSpills.count(VGPR))
285e3b55780SDimitry Andric     return;
286e3b55780SDimitry Andric 
287b1c73532SDimitry Andric   // Skip if this is a function with the amdgpu_cs_chain or
288b1c73532SDimitry Andric   // amdgpu_cs_chain_preserve calling convention and this is a scratch register.
289b1c73532SDimitry Andric   // We never need to allocate a spill for these because we don't even need to
290b1c73532SDimitry Andric   // restore the inactive lanes for them (they're scratchier than the usual
291b1c73532SDimitry Andric   // scratch registers).
292b1c73532SDimitry Andric   if (isChainFunction() && SIRegisterInfo::isChainScratchRegister(VGPR))
293b1c73532SDimitry Andric     return;
294b1c73532SDimitry Andric 
295e3b55780SDimitry Andric   WWMSpills.insert(std::make_pair(
296e3b55780SDimitry Andric       VGPR, MF.getFrameInfo().CreateSpillStackObject(Size, Alignment)));
297e3b55780SDimitry Andric }
298e3b55780SDimitry Andric 
299e3b55780SDimitry Andric // Separate out the callee-saved and scratch registers.
splitWWMSpillRegisters(MachineFunction & MF,SmallVectorImpl<std::pair<Register,int>> & CalleeSavedRegs,SmallVectorImpl<std::pair<Register,int>> & ScratchRegs) const300e3b55780SDimitry Andric void SIMachineFunctionInfo::splitWWMSpillRegisters(
301e3b55780SDimitry Andric     MachineFunction &MF,
302e3b55780SDimitry Andric     SmallVectorImpl<std::pair<Register, int>> &CalleeSavedRegs,
303e3b55780SDimitry Andric     SmallVectorImpl<std::pair<Register, int>> &ScratchRegs) const {
304e3b55780SDimitry Andric   const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
305e3b55780SDimitry Andric   for (auto &Reg : WWMSpills) {
306e3b55780SDimitry Andric     if (isCalleeSavedReg(CSRegs, Reg.first))
307e3b55780SDimitry Andric       CalleeSavedRegs.push_back(Reg);
308e3b55780SDimitry Andric     else
309e3b55780SDimitry Andric       ScratchRegs.push_back(Reg);
310e3b55780SDimitry Andric   }
311e3b55780SDimitry Andric }
312e3b55780SDimitry Andric 
isCalleeSavedReg(const MCPhysReg * CSRegs,MCPhysReg Reg) const313cfca06d7SDimitry Andric bool SIMachineFunctionInfo::isCalleeSavedReg(const MCPhysReg *CSRegs,
314e3b55780SDimitry Andric                                              MCPhysReg Reg) const {
315044eb2f6SDimitry Andric   for (unsigned I = 0; CSRegs[I]; ++I) {
316044eb2f6SDimitry Andric     if (CSRegs[I] == Reg)
317044eb2f6SDimitry Andric       return true;
318044eb2f6SDimitry Andric   }
319044eb2f6SDimitry Andric 
320044eb2f6SDimitry Andric   return false;
321aac4ca60SDimitry Andric }
322aac4ca60SDimitry Andric 
shiftSpillPhysVGPRsToLowestRange(MachineFunction & MF)3234df029ccSDimitry Andric void SIMachineFunctionInfo::shiftSpillPhysVGPRsToLowestRange(
3244df029ccSDimitry Andric     MachineFunction &MF) {
3254df029ccSDimitry Andric   const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
3264df029ccSDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
327ac9a064cSDimitry Andric   for (Register &Reg : SpillPhysVGPRs) {
3284df029ccSDimitry Andric     Register NewReg =
3294df029ccSDimitry Andric         TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF);
3304df029ccSDimitry Andric     if (!NewReg || NewReg >= Reg)
3314df029ccSDimitry Andric       break;
3324df029ccSDimitry Andric 
3334df029ccSDimitry Andric     MRI.replaceRegWith(Reg, NewReg);
3344df029ccSDimitry Andric 
3354df029ccSDimitry Andric     // Update various tables with the new VGPR.
3364df029ccSDimitry Andric     WWMReservedRegs.remove(Reg);
3374df029ccSDimitry Andric     WWMReservedRegs.insert(NewReg);
3384df029ccSDimitry Andric     WWMSpills.insert(std::make_pair(NewReg, WWMSpills[Reg]));
3394df029ccSDimitry Andric     WWMSpills.erase(Reg);
3404df029ccSDimitry Andric 
3414df029ccSDimitry Andric     for (MachineBasicBlock &MBB : MF) {
3424df029ccSDimitry Andric       MBB.removeLiveIn(Reg);
3434df029ccSDimitry Andric       MBB.sortUniqueLiveIns();
3444df029ccSDimitry Andric     }
345ac9a064cSDimitry Andric 
346ac9a064cSDimitry Andric     Reg = NewReg;
3474df029ccSDimitry Andric   }
3484df029ccSDimitry Andric }
3494df029ccSDimitry Andric 
allocateVirtualVGPRForSGPRSpills(MachineFunction & MF,int FI,unsigned LaneIndex)3507fa27ce4SDimitry Andric bool SIMachineFunctionInfo::allocateVirtualVGPRForSGPRSpills(
3517fa27ce4SDimitry Andric     MachineFunction &MF, int FI, unsigned LaneIndex) {
352e3b55780SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
353e3b55780SDimitry Andric   Register LaneVGPR;
354e3b55780SDimitry Andric   if (!LaneIndex) {
3557fa27ce4SDimitry Andric     LaneVGPR = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
356e3b55780SDimitry Andric     SpillVGPRs.push_back(LaneVGPR);
357e3b55780SDimitry Andric   } else {
358e3b55780SDimitry Andric     LaneVGPR = SpillVGPRs.back();
359e3b55780SDimitry Andric   }
360e3b55780SDimitry Andric 
361ac9a064cSDimitry Andric   SGPRSpillsToVirtualVGPRLanes[FI].emplace_back(LaneVGPR, LaneIndex);
362e3b55780SDimitry Andric   return true;
363e3b55780SDimitry Andric }
364e3b55780SDimitry Andric 
allocatePhysicalVGPRForSGPRSpills(MachineFunction & MF,int FI,unsigned LaneIndex,bool IsPrologEpilog)3657fa27ce4SDimitry Andric bool SIMachineFunctionInfo::allocatePhysicalVGPRForSGPRSpills(
3664df029ccSDimitry Andric     MachineFunction &MF, int FI, unsigned LaneIndex, bool IsPrologEpilog) {
367e3b55780SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
368e3b55780SDimitry Andric   const SIRegisterInfo *TRI = ST.getRegisterInfo();
369e3b55780SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
370e3b55780SDimitry Andric   Register LaneVGPR;
371e3b55780SDimitry Andric   if (!LaneIndex) {
3724df029ccSDimitry Andric     // Find the highest available register if called before RA to ensure the
3734df029ccSDimitry Andric     // lowest registers are available for allocation. The LaneVGPR, in that
3744df029ccSDimitry Andric     // case, will be shifted back to the lowest range after VGPR allocation.
3754df029ccSDimitry Andric     LaneVGPR = TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF,
3764df029ccSDimitry Andric                                        !IsPrologEpilog);
377e3b55780SDimitry Andric     if (LaneVGPR == AMDGPU::NoRegister) {
378e3b55780SDimitry Andric       // We have no VGPRs left for spilling SGPRs. Reset because we will not
379e3b55780SDimitry Andric       // partially spill the SGPR to VGPRs.
3807fa27ce4SDimitry Andric       SGPRSpillsToPhysicalVGPRLanes.erase(FI);
381e3b55780SDimitry Andric       return false;
382e3b55780SDimitry Andric     }
383e3b55780SDimitry Andric 
384e3b55780SDimitry Andric     allocateWWMSpill(MF, LaneVGPR);
3857fa27ce4SDimitry Andric     reserveWWMRegister(LaneVGPR);
3867fa27ce4SDimitry Andric     for (MachineBasicBlock &MBB : MF) {
3877fa27ce4SDimitry Andric       MBB.addLiveIn(LaneVGPR);
3887fa27ce4SDimitry Andric       MBB.sortUniqueLiveIns();
3897fa27ce4SDimitry Andric     }
390312c0ed1SDimitry Andric     SpillPhysVGPRs.push_back(LaneVGPR);
391e3b55780SDimitry Andric   } else {
392312c0ed1SDimitry Andric     LaneVGPR = SpillPhysVGPRs.back();
393e3b55780SDimitry Andric   }
394e3b55780SDimitry Andric 
395ac9a064cSDimitry Andric   SGPRSpillsToPhysicalVGPRLanes[FI].emplace_back(LaneVGPR, LaneIndex);
396e3b55780SDimitry Andric   return true;
397e3b55780SDimitry Andric }
398e3b55780SDimitry Andric 
allocateSGPRSpillToVGPRLane(MachineFunction & MF,int FI,bool SpillToPhysVGPRLane,bool IsPrologEpilog)3994df029ccSDimitry Andric bool SIMachineFunctionInfo::allocateSGPRSpillToVGPRLane(
4004df029ccSDimitry Andric     MachineFunction &MF, int FI, bool SpillToPhysVGPRLane,
401e3b55780SDimitry Andric     bool IsPrologEpilog) {
402e3b55780SDimitry Andric   std::vector<SIRegisterInfo::SpilledReg> &SpillLanes =
4034df029ccSDimitry Andric       SpillToPhysVGPRLane ? SGPRSpillsToPhysicalVGPRLanes[FI]
4047fa27ce4SDimitry Andric                           : SGPRSpillsToVirtualVGPRLanes[FI];
40501095a5dSDimitry Andric 
40671d5a254SDimitry Andric   // This has already been allocated.
40771d5a254SDimitry Andric   if (!SpillLanes.empty())
40871d5a254SDimitry Andric     return true;
40971d5a254SDimitry Andric 
410eb11fae6SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
41171d5a254SDimitry Andric   MachineFrameInfo &FrameInfo = MF.getFrameInfo();
41271d5a254SDimitry Andric   unsigned WaveSize = ST.getWavefrontSize();
41301095a5dSDimitry Andric 
41471d5a254SDimitry Andric   unsigned Size = FrameInfo.getObjectSize(FI);
415cfca06d7SDimitry Andric   unsigned NumLanes = Size / 4;
4165ca98fd9SDimitry Andric 
417cfca06d7SDimitry Andric   if (NumLanes > WaveSize)
418cfca06d7SDimitry Andric     return false;
419cfca06d7SDimitry Andric 
420cfca06d7SDimitry Andric   assert(Size >= 4 && "invalid sgpr spill size");
421e3b55780SDimitry Andric   assert(ST.getRegisterInfo()->spillSGPRToVGPR() &&
422e3b55780SDimitry Andric          "not spilling SGPRs to VGPRs");
4235ca98fd9SDimitry Andric 
4244df029ccSDimitry Andric   unsigned &NumSpillLanes = SpillToPhysVGPRLane ? NumPhysicalVGPRSpillLanes
4254df029ccSDimitry Andric                                                 : NumVirtualVGPRSpillLanes;
42667c32a98SDimitry Andric 
427e3b55780SDimitry Andric   for (unsigned I = 0; I < NumLanes; ++I, ++NumSpillLanes) {
428e3b55780SDimitry Andric     unsigned LaneIndex = (NumSpillLanes % WaveSize);
429344a3780SDimitry Andric 
4304df029ccSDimitry Andric     bool Allocated = SpillToPhysVGPRLane
4314df029ccSDimitry Andric                          ? allocatePhysicalVGPRForSGPRSpills(MF, FI, LaneIndex,
4324df029ccSDimitry Andric                                                              IsPrologEpilog)
4337fa27ce4SDimitry Andric                          : allocateVirtualVGPRForSGPRSpills(MF, FI, LaneIndex);
434e3b55780SDimitry Andric     if (!Allocated) {
435e3b55780SDimitry Andric       NumSpillLanes -= I;
43671d5a254SDimitry Andric       return false;
43771d5a254SDimitry Andric     }
43871d5a254SDimitry Andric   }
43971d5a254SDimitry Andric 
44071d5a254SDimitry Andric   return true;
44171d5a254SDimitry Andric }
44271d5a254SDimitry Andric 
443e6d15924SDimitry Andric /// Reserve AGPRs or VGPRs to support spilling for FrameIndex \p FI.
444e6d15924SDimitry Andric /// Either AGPR is spilled to VGPR to vice versa.
445e6d15924SDimitry Andric /// Returns true if a \p FI can be eliminated completely.
allocateVGPRSpillToAGPR(MachineFunction & MF,int FI,bool isAGPRtoVGPR)446e6d15924SDimitry Andric bool SIMachineFunctionInfo::allocateVGPRSpillToAGPR(MachineFunction &MF,
447e6d15924SDimitry Andric                                                     int FI,
448e6d15924SDimitry Andric                                                     bool isAGPRtoVGPR) {
449e6d15924SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
450e6d15924SDimitry Andric   MachineFrameInfo &FrameInfo = MF.getFrameInfo();
451e6d15924SDimitry Andric   const GCNSubtarget &ST =  MF.getSubtarget<GCNSubtarget>();
452e6d15924SDimitry Andric 
453e6d15924SDimitry Andric   assert(ST.hasMAIInsts() && FrameInfo.isSpillSlotObjectIndex(FI));
454e6d15924SDimitry Andric 
455e6d15924SDimitry Andric   auto &Spill = VGPRToAGPRSpills[FI];
456e6d15924SDimitry Andric 
457e6d15924SDimitry Andric   // This has already been allocated.
458e6d15924SDimitry Andric   if (!Spill.Lanes.empty())
459e6d15924SDimitry Andric     return Spill.FullyAllocated;
460e6d15924SDimitry Andric 
461e6d15924SDimitry Andric   unsigned Size = FrameInfo.getObjectSize(FI);
462e6d15924SDimitry Andric   unsigned NumLanes = Size / 4;
463e6d15924SDimitry Andric   Spill.Lanes.resize(NumLanes, AMDGPU::NoRegister);
464e6d15924SDimitry Andric 
465e6d15924SDimitry Andric   const TargetRegisterClass &RC =
466e6d15924SDimitry Andric       isAGPRtoVGPR ? AMDGPU::VGPR_32RegClass : AMDGPU::AGPR_32RegClass;
467e6d15924SDimitry Andric   auto Regs = RC.getRegisters();
468e6d15924SDimitry Andric 
469e6d15924SDimitry Andric   auto &SpillRegs = isAGPRtoVGPR ? SpillAGPR : SpillVGPR;
470e6d15924SDimitry Andric   const SIRegisterInfo *TRI = ST.getRegisterInfo();
471e6d15924SDimitry Andric   Spill.FullyAllocated = true;
472e6d15924SDimitry Andric 
473e6d15924SDimitry Andric   // FIXME: Move allocation logic out of MachineFunctionInfo and initialize
474e6d15924SDimitry Andric   // once.
475e6d15924SDimitry Andric   BitVector OtherUsedRegs;
476e6d15924SDimitry Andric   OtherUsedRegs.resize(TRI->getNumRegs());
477e6d15924SDimitry Andric 
478e6d15924SDimitry Andric   const uint32_t *CSRMask =
479e6d15924SDimitry Andric       TRI->getCallPreservedMask(MF, MF.getFunction().getCallingConv());
480e6d15924SDimitry Andric   if (CSRMask)
481e6d15924SDimitry Andric     OtherUsedRegs.setBitsInMask(CSRMask);
482e6d15924SDimitry Andric 
483e6d15924SDimitry Andric   // TODO: Should include register tuples, but doesn't matter with current
484e6d15924SDimitry Andric   // usage.
485e6d15924SDimitry Andric   for (MCPhysReg Reg : SpillAGPR)
486e6d15924SDimitry Andric     OtherUsedRegs.set(Reg);
487e6d15924SDimitry Andric   for (MCPhysReg Reg : SpillVGPR)
488e6d15924SDimitry Andric     OtherUsedRegs.set(Reg);
489e6d15924SDimitry Andric 
490e6d15924SDimitry Andric   SmallVectorImpl<MCPhysReg>::const_iterator NextSpillReg = Regs.begin();
491c0981da4SDimitry Andric   for (int I = NumLanes - 1; I >= 0; --I) {
492e6d15924SDimitry Andric     NextSpillReg = std::find_if(
493e6d15924SDimitry Andric         NextSpillReg, Regs.end(), [&MRI, &OtherUsedRegs](MCPhysReg Reg) {
494e6d15924SDimitry Andric           return MRI.isAllocatable(Reg) && !MRI.isPhysRegUsed(Reg) &&
495e6d15924SDimitry Andric                  !OtherUsedRegs[Reg];
496e6d15924SDimitry Andric         });
497e6d15924SDimitry Andric 
498e6d15924SDimitry Andric     if (NextSpillReg == Regs.end()) { // Registers exhausted
499e6d15924SDimitry Andric       Spill.FullyAllocated = false;
500e6d15924SDimitry Andric       break;
501e6d15924SDimitry Andric     }
502e6d15924SDimitry Andric 
503e6d15924SDimitry Andric     OtherUsedRegs.set(*NextSpillReg);
504e6d15924SDimitry Andric     SpillRegs.push_back(*NextSpillReg);
505e3b55780SDimitry Andric     MRI.reserveReg(*NextSpillReg, TRI);
506e6d15924SDimitry Andric     Spill.Lanes[I] = *NextSpillReg++;
507e6d15924SDimitry Andric   }
508e6d15924SDimitry Andric 
509e6d15924SDimitry Andric   return Spill.FullyAllocated;
510e6d15924SDimitry Andric }
511e6d15924SDimitry Andric 
removeDeadFrameIndices(MachineFrameInfo & MFI,bool ResetSGPRSpillStackIDs)512145449b1SDimitry Andric bool SIMachineFunctionInfo::removeDeadFrameIndices(
513145449b1SDimitry Andric     MachineFrameInfo &MFI, bool ResetSGPRSpillStackIDs) {
5147fa27ce4SDimitry Andric   // Remove dead frame indices from function frame, however keep FP & BP since
5157fa27ce4SDimitry Andric   // spills for them haven't been inserted yet. And also make sure to remove the
5167fa27ce4SDimitry Andric   // frame indices from `SGPRSpillsToVirtualVGPRLanes` data structure,
5177fa27ce4SDimitry Andric   // otherwise, it could result in an unexpected side effect and bug, in case of
5187fa27ce4SDimitry Andric   // any re-mapping of freed frame indices by later pass(es) like "stack slot
519e3b55780SDimitry Andric   // coloring".
5207fa27ce4SDimitry Andric   for (auto &R : make_early_inc_range(SGPRSpillsToVirtualVGPRLanes)) {
52171d5a254SDimitry Andric     MFI.RemoveStackObject(R.first);
5227fa27ce4SDimitry Andric     SGPRSpillsToVirtualVGPRLanes.erase(R.first);
5235ca98fd9SDimitry Andric   }
524eb11fae6SDimitry Andric 
5257fa27ce4SDimitry Andric   // Remove the dead frame indices of CSR SGPRs which are spilled to physical
5267fa27ce4SDimitry Andric   // VGPR lanes during SILowerSGPRSpills pass.
5277fa27ce4SDimitry Andric   if (!ResetSGPRSpillStackIDs) {
5287fa27ce4SDimitry Andric     for (auto &R : make_early_inc_range(SGPRSpillsToPhysicalVGPRLanes)) {
5297fa27ce4SDimitry Andric       MFI.RemoveStackObject(R.first);
5307fa27ce4SDimitry Andric       SGPRSpillsToPhysicalVGPRLanes.erase(R.first);
5317fa27ce4SDimitry Andric     }
5327fa27ce4SDimitry Andric   }
533145449b1SDimitry Andric   bool HaveSGPRToMemory = false;
534145449b1SDimitry Andric 
535145449b1SDimitry Andric   if (ResetSGPRSpillStackIDs) {
536e3b55780SDimitry Andric     // All other SGPRs must be allocated on the default stack, so reset the
537145449b1SDimitry Andric     // stack ID.
538e3b55780SDimitry Andric     for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); I != E;
539e3b55780SDimitry Andric          ++I) {
540e3b55780SDimitry Andric       if (!checkIndexInPrologEpilogSGPRSpills(I)) {
541e3b55780SDimitry Andric         if (MFI.getStackID(I) == TargetStackID::SGPRSpill) {
542e3b55780SDimitry Andric           MFI.setStackID(I, TargetStackID::Default);
543145449b1SDimitry Andric           HaveSGPRToMemory = true;
544145449b1SDimitry Andric         }
545145449b1SDimitry Andric       }
546145449b1SDimitry Andric     }
547145449b1SDimitry Andric   }
548eb11fae6SDimitry Andric 
549e6d15924SDimitry Andric   for (auto &R : VGPRToAGPRSpills) {
55077fc4c14SDimitry Andric     if (R.second.IsDead)
551e6d15924SDimitry Andric       MFI.RemoveStackObject(R.first);
552eb11fae6SDimitry Andric   }
553145449b1SDimitry Andric 
554145449b1SDimitry Andric   return HaveSGPRToMemory;
555145449b1SDimitry Andric }
556145449b1SDimitry Andric 
getScavengeFI(MachineFrameInfo & MFI,const SIRegisterInfo & TRI)557344a3780SDimitry Andric int SIMachineFunctionInfo::getScavengeFI(MachineFrameInfo &MFI,
558344a3780SDimitry Andric                                          const SIRegisterInfo &TRI) {
559344a3780SDimitry Andric   if (ScavengeFI)
560344a3780SDimitry Andric     return *ScavengeFI;
561ac9a064cSDimitry Andric 
562ac9a064cSDimitry Andric   ScavengeFI =
563ac9a064cSDimitry Andric       MFI.CreateStackObject(TRI.getSpillSize(AMDGPU::SGPR_32RegClass),
564344a3780SDimitry Andric                             TRI.getSpillAlign(AMDGPU::SGPR_32RegClass), false);
565344a3780SDimitry Andric   return *ScavengeFI;
566344a3780SDimitry Andric }
567344a3780SDimitry Andric 
getNextUserSGPR() const568eb11fae6SDimitry Andric MCPhysReg SIMachineFunctionInfo::getNextUserSGPR() const {
569eb11fae6SDimitry Andric   assert(NumSystemSGPRs == 0 && "System SGPRs must be added after user SGPRs");
570eb11fae6SDimitry Andric   return AMDGPU::SGPR0 + NumUserSGPRs;
571eb11fae6SDimitry Andric }
572eb11fae6SDimitry Andric 
getNextSystemSGPR() const573eb11fae6SDimitry Andric MCPhysReg SIMachineFunctionInfo::getNextSystemSGPR() const {
574eb11fae6SDimitry Andric   return AMDGPU::SGPR0 + NumUserSGPRs + NumSystemSGPRs;
575eb11fae6SDimitry Andric }
576e6d15924SDimitry Andric 
MRI_NoteNewVirtualRegister(Register Reg)5777fa27ce4SDimitry Andric void SIMachineFunctionInfo::MRI_NoteNewVirtualRegister(Register Reg) {
5787fa27ce4SDimitry Andric   VRegFlags.grow(Reg);
5797fa27ce4SDimitry Andric }
5807fa27ce4SDimitry Andric 
MRI_NoteCloneVirtualRegister(Register NewReg,Register SrcReg)5817fa27ce4SDimitry Andric void SIMachineFunctionInfo::MRI_NoteCloneVirtualRegister(Register NewReg,
5827fa27ce4SDimitry Andric                                                          Register SrcReg) {
5837fa27ce4SDimitry Andric   VRegFlags.grow(NewReg);
5847fa27ce4SDimitry Andric   VRegFlags[NewReg] = VRegFlags[SrcReg];
5857fa27ce4SDimitry Andric }
5867fa27ce4SDimitry Andric 
587cfca06d7SDimitry Andric Register
getGITPtrLoReg(const MachineFunction & MF) const588cfca06d7SDimitry Andric SIMachineFunctionInfo::getGITPtrLoReg(const MachineFunction &MF) const {
589cfca06d7SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
590cfca06d7SDimitry Andric   if (!ST.isAmdPalOS())
591cfca06d7SDimitry Andric     return Register();
592cfca06d7SDimitry Andric   Register GitPtrLo = AMDGPU::SGPR0; // Low GIT address passed in
593cfca06d7SDimitry Andric   if (ST.hasMergedShaders()) {
594cfca06d7SDimitry Andric     switch (MF.getFunction().getCallingConv()) {
595cfca06d7SDimitry Andric     case CallingConv::AMDGPU_HS:
596cfca06d7SDimitry Andric     case CallingConv::AMDGPU_GS:
597cfca06d7SDimitry Andric       // Low GIT address is passed in s8 rather than s0 for an LS+HS or
598cfca06d7SDimitry Andric       // ES+GS merged shader on gfx9+.
599cfca06d7SDimitry Andric       GitPtrLo = AMDGPU::SGPR8;
600cfca06d7SDimitry Andric       return GitPtrLo;
601cfca06d7SDimitry Andric     default:
602cfca06d7SDimitry Andric       return GitPtrLo;
603cfca06d7SDimitry Andric     }
604cfca06d7SDimitry Andric   }
605cfca06d7SDimitry Andric   return GitPtrLo;
606cfca06d7SDimitry Andric }
607cfca06d7SDimitry Andric 
regToString(Register Reg,const TargetRegisterInfo & TRI)608cfca06d7SDimitry Andric static yaml::StringValue regToString(Register Reg,
609e6d15924SDimitry Andric                                      const TargetRegisterInfo &TRI) {
610e6d15924SDimitry Andric   yaml::StringValue Dest;
611e6d15924SDimitry Andric   {
612e6d15924SDimitry Andric     raw_string_ostream OS(Dest.Value);
613e6d15924SDimitry Andric     OS << printReg(Reg, &TRI);
614e6d15924SDimitry Andric   }
615e6d15924SDimitry Andric   return Dest;
616e6d15924SDimitry Andric }
617e6d15924SDimitry Andric 
618e3b55780SDimitry Andric static std::optional<yaml::SIArgumentInfo>
convertArgumentInfo(const AMDGPUFunctionArgInfo & ArgInfo,const TargetRegisterInfo & TRI)619e6d15924SDimitry Andric convertArgumentInfo(const AMDGPUFunctionArgInfo &ArgInfo,
620e6d15924SDimitry Andric                     const TargetRegisterInfo &TRI) {
621e6d15924SDimitry Andric   yaml::SIArgumentInfo AI;
622e6d15924SDimitry Andric 
623e3b55780SDimitry Andric   auto convertArg = [&](std::optional<yaml::SIArgument> &A,
624e6d15924SDimitry Andric                         const ArgDescriptor &Arg) {
625e6d15924SDimitry Andric     if (!Arg)
626e6d15924SDimitry Andric       return false;
627e6d15924SDimitry Andric 
628e6d15924SDimitry Andric     // Create a register or stack argument.
629e6d15924SDimitry Andric     yaml::SIArgument SA = yaml::SIArgument::createArgument(Arg.isRegister());
630e6d15924SDimitry Andric     if (Arg.isRegister()) {
631e6d15924SDimitry Andric       raw_string_ostream OS(SA.RegisterName.Value);
632e6d15924SDimitry Andric       OS << printReg(Arg.getRegister(), &TRI);
633e6d15924SDimitry Andric     } else
634e6d15924SDimitry Andric       SA.StackOffset = Arg.getStackOffset();
635e6d15924SDimitry Andric     // Check and update the optional mask.
636e6d15924SDimitry Andric     if (Arg.isMasked())
637e6d15924SDimitry Andric       SA.Mask = Arg.getMask();
638e6d15924SDimitry Andric 
639e6d15924SDimitry Andric     A = SA;
640e6d15924SDimitry Andric     return true;
641e6d15924SDimitry Andric   };
642e6d15924SDimitry Andric 
643b1c73532SDimitry Andric   // TODO: Need to serialize kernarg preloads.
644e6d15924SDimitry Andric   bool Any = false;
645e6d15924SDimitry Andric   Any |= convertArg(AI.PrivateSegmentBuffer, ArgInfo.PrivateSegmentBuffer);
646e6d15924SDimitry Andric   Any |= convertArg(AI.DispatchPtr, ArgInfo.DispatchPtr);
647e6d15924SDimitry Andric   Any |= convertArg(AI.QueuePtr, ArgInfo.QueuePtr);
648e6d15924SDimitry Andric   Any |= convertArg(AI.KernargSegmentPtr, ArgInfo.KernargSegmentPtr);
649e6d15924SDimitry Andric   Any |= convertArg(AI.DispatchID, ArgInfo.DispatchID);
650e6d15924SDimitry Andric   Any |= convertArg(AI.FlatScratchInit, ArgInfo.FlatScratchInit);
6514b4fe385SDimitry Andric   Any |= convertArg(AI.LDSKernelId, ArgInfo.LDSKernelId);
652e6d15924SDimitry Andric   Any |= convertArg(AI.PrivateSegmentSize, ArgInfo.PrivateSegmentSize);
653e6d15924SDimitry Andric   Any |= convertArg(AI.WorkGroupIDX, ArgInfo.WorkGroupIDX);
654e6d15924SDimitry Andric   Any |= convertArg(AI.WorkGroupIDY, ArgInfo.WorkGroupIDY);
655e6d15924SDimitry Andric   Any |= convertArg(AI.WorkGroupIDZ, ArgInfo.WorkGroupIDZ);
656e6d15924SDimitry Andric   Any |= convertArg(AI.WorkGroupInfo, ArgInfo.WorkGroupInfo);
657e6d15924SDimitry Andric   Any |= convertArg(AI.PrivateSegmentWaveByteOffset,
658e6d15924SDimitry Andric                     ArgInfo.PrivateSegmentWaveByteOffset);
659e6d15924SDimitry Andric   Any |= convertArg(AI.ImplicitArgPtr, ArgInfo.ImplicitArgPtr);
660e6d15924SDimitry Andric   Any |= convertArg(AI.ImplicitBufferPtr, ArgInfo.ImplicitBufferPtr);
661e6d15924SDimitry Andric   Any |= convertArg(AI.WorkItemIDX, ArgInfo.WorkItemIDX);
662e6d15924SDimitry Andric   Any |= convertArg(AI.WorkItemIDY, ArgInfo.WorkItemIDY);
663e6d15924SDimitry Andric   Any |= convertArg(AI.WorkItemIDZ, ArgInfo.WorkItemIDZ);
664e6d15924SDimitry Andric 
665e6d15924SDimitry Andric   if (Any)
666e6d15924SDimitry Andric     return AI;
667e6d15924SDimitry Andric 
668e3b55780SDimitry Andric   return std::nullopt;
669e6d15924SDimitry Andric }
670e6d15924SDimitry Andric 
SIMachineFunctionInfo(const llvm::SIMachineFunctionInfo & MFI,const TargetRegisterInfo & TRI,const llvm::MachineFunction & MF)671e6d15924SDimitry Andric yaml::SIMachineFunctionInfo::SIMachineFunctionInfo(
672344a3780SDimitry Andric     const llvm::SIMachineFunctionInfo &MFI, const TargetRegisterInfo &TRI,
673344a3780SDimitry Andric     const llvm::MachineFunction &MF)
674e6d15924SDimitry Andric     : ExplicitKernArgSize(MFI.getExplicitKernArgSize()),
675b60736ecSDimitry Andric       MaxKernArgAlign(MFI.getMaxKernArgAlign()), LDSSize(MFI.getLDSSize()),
676145449b1SDimitry Andric       GDSSize(MFI.getGDSSize()),
677b60736ecSDimitry Andric       DynLDSAlign(MFI.getDynLDSAlign()), IsEntryFunction(MFI.isEntryFunction()),
678e6d15924SDimitry Andric       NoSignedZerosFPMath(MFI.hasNoSignedZerosFPMath()),
679b60736ecSDimitry Andric       MemoryBound(MFI.isMemoryBound()), WaveLimiter(MFI.needsWaveLimiter()),
680b60736ecSDimitry Andric       HasSpilledSGPRs(MFI.hasSpilledSGPRs()),
681b60736ecSDimitry Andric       HasSpilledVGPRs(MFI.hasSpilledVGPRs()),
6821d5ae102SDimitry Andric       HighBitsOf32BitAddress(MFI.get32BitAddressHighBits()),
683b60736ecSDimitry Andric       Occupancy(MFI.getOccupancy()),
684e6d15924SDimitry Andric       ScratchRSrcReg(regToString(MFI.getScratchRSrcReg(), TRI)),
685e6d15924SDimitry Andric       FrameOffsetReg(regToString(MFI.getFrameOffsetReg(), TRI)),
686e6d15924SDimitry Andric       StackPtrOffsetReg(regToString(MFI.getStackPtrOffsetReg(), TRI)),
687145449b1SDimitry Andric       BytesInStackArgArea(MFI.getBytesInStackArgArea()),
688145449b1SDimitry Andric       ReturnsVoid(MFI.returnsVoid()),
6897fa27ce4SDimitry Andric       ArgInfo(convertArgumentInfo(MFI.getArgInfo(), TRI)),
6907fa27ce4SDimitry Andric       PSInputAddr(MFI.getPSInputAddr()),
6917fa27ce4SDimitry Andric       PSInputEnable(MFI.getPSInputEnable()),
6927fa27ce4SDimitry Andric       Mode(MFI.getMode()) {
693e3b55780SDimitry Andric   for (Register Reg : MFI.getWWMReservedRegs())
694145449b1SDimitry Andric     WWMReservedRegs.push_back(regToString(Reg, TRI));
695145449b1SDimitry Andric 
6967fa27ce4SDimitry Andric   if (MFI.getLongBranchReservedReg())
6977fa27ce4SDimitry Andric     LongBranchReservedReg = regToString(MFI.getLongBranchReservedReg(), TRI);
698145449b1SDimitry Andric   if (MFI.getVGPRForAGPRCopy())
699145449b1SDimitry Andric     VGPRForAGPRCopy = regToString(MFI.getVGPRForAGPRCopy(), TRI);
7007fa27ce4SDimitry Andric 
7017fa27ce4SDimitry Andric   if (MFI.getSGPRForEXECCopy())
7027fa27ce4SDimitry Andric     SGPRForEXECCopy = regToString(MFI.getSGPRForEXECCopy(), TRI);
7037fa27ce4SDimitry Andric 
704344a3780SDimitry Andric   auto SFI = MFI.getOptionalScavengeFI();
705344a3780SDimitry Andric   if (SFI)
706344a3780SDimitry Andric     ScavengeFI = yaml::FrameIndex(*SFI, MF.getFrameInfo());
707b60736ecSDimitry Andric }
708e6d15924SDimitry Andric 
mappingImpl(yaml::IO & YamlIO)709e6d15924SDimitry Andric void yaml::SIMachineFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
710e6d15924SDimitry Andric   MappingTraits<SIMachineFunctionInfo>::mapping(YamlIO, *this);
711e6d15924SDimitry Andric }
712e6d15924SDimitry Andric 
initializeBaseYamlFields(const yaml::SIMachineFunctionInfo & YamlMFI,const MachineFunction & MF,PerFunctionMIParsingState & PFS,SMDiagnostic & Error,SMRange & SourceRange)713e6d15924SDimitry Andric bool SIMachineFunctionInfo::initializeBaseYamlFields(
714344a3780SDimitry Andric     const yaml::SIMachineFunctionInfo &YamlMFI, const MachineFunction &MF,
715344a3780SDimitry Andric     PerFunctionMIParsingState &PFS, SMDiagnostic &Error, SMRange &SourceRange) {
716e6d15924SDimitry Andric   ExplicitKernArgSize = YamlMFI.ExplicitKernArgSize;
717145449b1SDimitry Andric   MaxKernArgAlign = YamlMFI.MaxKernArgAlign;
718e6d15924SDimitry Andric   LDSSize = YamlMFI.LDSSize;
719145449b1SDimitry Andric   GDSSize = YamlMFI.GDSSize;
720b60736ecSDimitry Andric   DynLDSAlign = YamlMFI.DynLDSAlign;
7217fa27ce4SDimitry Andric   PSInputAddr = YamlMFI.PSInputAddr;
7227fa27ce4SDimitry Andric   PSInputEnable = YamlMFI.PSInputEnable;
7231d5ae102SDimitry Andric   HighBitsOf32BitAddress = YamlMFI.HighBitsOf32BitAddress;
724b60736ecSDimitry Andric   Occupancy = YamlMFI.Occupancy;
725e6d15924SDimitry Andric   IsEntryFunction = YamlMFI.IsEntryFunction;
726e6d15924SDimitry Andric   NoSignedZerosFPMath = YamlMFI.NoSignedZerosFPMath;
727e6d15924SDimitry Andric   MemoryBound = YamlMFI.MemoryBound;
728e6d15924SDimitry Andric   WaveLimiter = YamlMFI.WaveLimiter;
729b60736ecSDimitry Andric   HasSpilledSGPRs = YamlMFI.HasSpilledSGPRs;
730b60736ecSDimitry Andric   HasSpilledVGPRs = YamlMFI.HasSpilledVGPRs;
731145449b1SDimitry Andric   BytesInStackArgArea = YamlMFI.BytesInStackArgArea;
732145449b1SDimitry Andric   ReturnsVoid = YamlMFI.ReturnsVoid;
733344a3780SDimitry Andric 
734344a3780SDimitry Andric   if (YamlMFI.ScavengeFI) {
735344a3780SDimitry Andric     auto FIOrErr = YamlMFI.ScavengeFI->getFI(MF.getFrameInfo());
736344a3780SDimitry Andric     if (!FIOrErr) {
737344a3780SDimitry Andric       // Create a diagnostic for a the frame index.
738344a3780SDimitry Andric       const MemoryBuffer &Buffer =
739344a3780SDimitry Andric           *PFS.SM->getMemoryBuffer(PFS.SM->getMainFileID());
740344a3780SDimitry Andric 
741344a3780SDimitry Andric       Error = SMDiagnostic(*PFS.SM, SMLoc(), Buffer.getBufferIdentifier(), 1, 1,
742344a3780SDimitry Andric                            SourceMgr::DK_Error, toString(FIOrErr.takeError()),
743e3b55780SDimitry Andric                            "", std::nullopt, std::nullopt);
744344a3780SDimitry Andric       SourceRange = YamlMFI.ScavengeFI->SourceRange;
745344a3780SDimitry Andric       return true;
746344a3780SDimitry Andric     }
747344a3780SDimitry Andric     ScavengeFI = *FIOrErr;
748344a3780SDimitry Andric   } else {
749e3b55780SDimitry Andric     ScavengeFI = std::nullopt;
750344a3780SDimitry Andric   }
751e6d15924SDimitry Andric   return false;
752e6d15924SDimitry Andric }
753cfca06d7SDimitry Andric 
mayUseAGPRs(const Function & F) const754e3b55780SDimitry Andric bool SIMachineFunctionInfo::mayUseAGPRs(const Function &F) const {
755ac9a064cSDimitry Andric   return !F.hasFnAttribute("amdgpu-no-agpr");
756145449b1SDimitry Andric }
757145449b1SDimitry Andric 
usesAGPRs(const MachineFunction & MF) const758c0981da4SDimitry Andric bool SIMachineFunctionInfo::usesAGPRs(const MachineFunction &MF) const {
759c0981da4SDimitry Andric   if (UsesAGPRs)
760c0981da4SDimitry Andric     return *UsesAGPRs;
761c0981da4SDimitry Andric 
762145449b1SDimitry Andric   if (!mayNeedAGPRs()) {
763145449b1SDimitry Andric     UsesAGPRs = false;
764145449b1SDimitry Andric     return false;
765145449b1SDimitry Andric   }
766145449b1SDimitry Andric 
767c0981da4SDimitry Andric   if (!AMDGPU::isEntryFunctionCC(MF.getFunction().getCallingConv()) ||
768c0981da4SDimitry Andric       MF.getFrameInfo().hasCalls()) {
769c0981da4SDimitry Andric     UsesAGPRs = true;
770c0981da4SDimitry Andric     return true;
771c0981da4SDimitry Andric   }
772c0981da4SDimitry Andric 
773c0981da4SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
774c0981da4SDimitry Andric 
775c0981da4SDimitry Andric   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
776c0981da4SDimitry Andric     const Register Reg = Register::index2VirtReg(I);
777c0981da4SDimitry Andric     const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
778c0981da4SDimitry Andric     if (RC && SIRegisterInfo::isAGPRClass(RC)) {
779c0981da4SDimitry Andric       UsesAGPRs = true;
780c0981da4SDimitry Andric       return true;
781ac9a064cSDimitry Andric     }
782ac9a064cSDimitry Andric     if (!RC && !MRI.use_empty(Reg) && MRI.getType(Reg).isValid()) {
783c0981da4SDimitry Andric       // Defer caching UsesAGPRs, function might not yet been regbank selected.
784c0981da4SDimitry Andric       return true;
785c0981da4SDimitry Andric     }
786c0981da4SDimitry Andric   }
787c0981da4SDimitry Andric 
788c0981da4SDimitry Andric   for (MCRegister Reg : AMDGPU::AGPR_32RegClass) {
789c0981da4SDimitry Andric     if (MRI.isPhysRegUsed(Reg)) {
790c0981da4SDimitry Andric       UsesAGPRs = true;
791c0981da4SDimitry Andric       return true;
792c0981da4SDimitry Andric     }
793c0981da4SDimitry Andric   }
794c0981da4SDimitry Andric 
795c0981da4SDimitry Andric   UsesAGPRs = false;
796c0981da4SDimitry Andric   return false;
797c0981da4SDimitry Andric }
798