xref: /llvm-project/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h (revision 6a87e9b08bf093ba3ccba8650b89f4d337c497f4)
1 //==- SIMachineFunctionInfo.h - SIMachineFunctionInfo interface --*- C++ -*-==//
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 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H
14 #define LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H
15 
16 #include "AMDGPUArgumentUsageInfo.h"
17 #include "AMDGPUMachineFunction.h"
18 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
19 #include "SIInstrInfo.h"
20 #include "llvm/CodeGen/MIRYamlMapping.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 namespace llvm {
25 
26 class MachineFrameInfo;
27 class MachineFunction;
28 class TargetRegisterClass;
29 class SIMachineFunctionInfo;
30 class SIRegisterInfo;
31 
32 class AMDGPUPseudoSourceValue : public PseudoSourceValue {
33 public:
34   enum AMDGPUPSVKind : unsigned {
35     PSVBuffer = PseudoSourceValue::TargetCustom,
36     PSVImage,
37     GWSResource
38   };
39 
40 protected:
41   AMDGPUPseudoSourceValue(unsigned Kind, const TargetInstrInfo &TII)
42       : PseudoSourceValue(Kind, TII) {}
43 
44 public:
45   bool isConstant(const MachineFrameInfo *) const override {
46     // This should probably be true for most images, but we will start by being
47     // conservative.
48     return false;
49   }
50 
51   bool isAliased(const MachineFrameInfo *) const override {
52     return true;
53   }
54 
55   bool mayAlias(const MachineFrameInfo *) const override {
56     return true;
57   }
58 };
59 
60 class AMDGPUBufferPseudoSourceValue final : public AMDGPUPseudoSourceValue {
61 public:
62   explicit AMDGPUBufferPseudoSourceValue(const TargetInstrInfo &TII)
63       : AMDGPUPseudoSourceValue(PSVBuffer, TII) {}
64 
65   static bool classof(const PseudoSourceValue *V) {
66     return V->kind() == PSVBuffer;
67   }
68 };
69 
70 class AMDGPUImagePseudoSourceValue final : public AMDGPUPseudoSourceValue {
71 public:
72   // TODO: Is the img rsrc useful?
73   explicit AMDGPUImagePseudoSourceValue(const TargetInstrInfo &TII)
74       : AMDGPUPseudoSourceValue(PSVImage, TII) {}
75 
76   static bool classof(const PseudoSourceValue *V) {
77     return V->kind() == PSVImage;
78   }
79 };
80 
81 class AMDGPUGWSResourcePseudoSourceValue final : public AMDGPUPseudoSourceValue {
82 public:
83   explicit AMDGPUGWSResourcePseudoSourceValue(const TargetInstrInfo &TII)
84       : AMDGPUPseudoSourceValue(GWSResource, TII) {}
85 
86   static bool classof(const PseudoSourceValue *V) {
87     return V->kind() == GWSResource;
88   }
89 
90   // These are inaccessible memory from IR.
91   bool isAliased(const MachineFrameInfo *) const override {
92     return false;
93   }
94 
95   // These are inaccessible memory from IR.
96   bool mayAlias(const MachineFrameInfo *) const override {
97     return false;
98   }
99 
100   void printCustom(raw_ostream &OS) const override {
101     OS << "GWSResource";
102   }
103 };
104 
105 namespace yaml {
106 
107 struct SIArgument {
108   bool IsRegister;
109   union {
110     StringValue RegisterName;
111     unsigned StackOffset;
112   };
113   Optional<unsigned> Mask;
114 
115   // Default constructor, which creates a stack argument.
116   SIArgument() : IsRegister(false), StackOffset(0) {}
117   SIArgument(const SIArgument &Other) {
118     IsRegister = Other.IsRegister;
119     if (IsRegister) {
120       ::new ((void *)std::addressof(RegisterName))
121           StringValue(Other.RegisterName);
122     } else
123       StackOffset = Other.StackOffset;
124     Mask = Other.Mask;
125   }
126   SIArgument &operator=(const SIArgument &Other) {
127     IsRegister = Other.IsRegister;
128     if (IsRegister) {
129       ::new ((void *)std::addressof(RegisterName))
130           StringValue(Other.RegisterName);
131     } else
132       StackOffset = Other.StackOffset;
133     Mask = Other.Mask;
134     return *this;
135   }
136   ~SIArgument() {
137     if (IsRegister)
138       RegisterName.~StringValue();
139   }
140 
141   // Helper to create a register or stack argument.
142   static inline SIArgument createArgument(bool IsReg) {
143     if (IsReg)
144       return SIArgument(IsReg);
145     return SIArgument();
146   }
147 
148 private:
149   // Construct a register argument.
150   SIArgument(bool) : IsRegister(true), RegisterName() {}
151 };
152 
153 template <> struct MappingTraits<SIArgument> {
154   static void mapping(IO &YamlIO, SIArgument &A) {
155     if (YamlIO.outputting()) {
156       if (A.IsRegister)
157         YamlIO.mapRequired("reg", A.RegisterName);
158       else
159         YamlIO.mapRequired("offset", A.StackOffset);
160     } else {
161       auto Keys = YamlIO.keys();
162       if (is_contained(Keys, "reg")) {
163         A = SIArgument::createArgument(true);
164         YamlIO.mapRequired("reg", A.RegisterName);
165       } else if (is_contained(Keys, "offset"))
166         YamlIO.mapRequired("offset", A.StackOffset);
167       else
168         YamlIO.setError("missing required key 'reg' or 'offset'");
169     }
170     YamlIO.mapOptional("mask", A.Mask);
171   }
172   static const bool flow = true;
173 };
174 
175 struct SIArgumentInfo {
176   Optional<SIArgument> PrivateSegmentBuffer;
177   Optional<SIArgument> DispatchPtr;
178   Optional<SIArgument> QueuePtr;
179   Optional<SIArgument> KernargSegmentPtr;
180   Optional<SIArgument> DispatchID;
181   Optional<SIArgument> FlatScratchInit;
182   Optional<SIArgument> PrivateSegmentSize;
183 
184   Optional<SIArgument> WorkGroupIDX;
185   Optional<SIArgument> WorkGroupIDY;
186   Optional<SIArgument> WorkGroupIDZ;
187   Optional<SIArgument> WorkGroupInfo;
188   Optional<SIArgument> PrivateSegmentWaveByteOffset;
189 
190   Optional<SIArgument> ImplicitArgPtr;
191   Optional<SIArgument> ImplicitBufferPtr;
192 
193   Optional<SIArgument> WorkItemIDX;
194   Optional<SIArgument> WorkItemIDY;
195   Optional<SIArgument> WorkItemIDZ;
196 };
197 
198 template <> struct MappingTraits<SIArgumentInfo> {
199   static void mapping(IO &YamlIO, SIArgumentInfo &AI) {
200     YamlIO.mapOptional("privateSegmentBuffer", AI.PrivateSegmentBuffer);
201     YamlIO.mapOptional("dispatchPtr", AI.DispatchPtr);
202     YamlIO.mapOptional("queuePtr", AI.QueuePtr);
203     YamlIO.mapOptional("kernargSegmentPtr", AI.KernargSegmentPtr);
204     YamlIO.mapOptional("dispatchID", AI.DispatchID);
205     YamlIO.mapOptional("flatScratchInit", AI.FlatScratchInit);
206     YamlIO.mapOptional("privateSegmentSize", AI.PrivateSegmentSize);
207 
208     YamlIO.mapOptional("workGroupIDX", AI.WorkGroupIDX);
209     YamlIO.mapOptional("workGroupIDY", AI.WorkGroupIDY);
210     YamlIO.mapOptional("workGroupIDZ", AI.WorkGroupIDZ);
211     YamlIO.mapOptional("workGroupInfo", AI.WorkGroupInfo);
212     YamlIO.mapOptional("privateSegmentWaveByteOffset",
213                        AI.PrivateSegmentWaveByteOffset);
214 
215     YamlIO.mapOptional("implicitArgPtr", AI.ImplicitArgPtr);
216     YamlIO.mapOptional("implicitBufferPtr", AI.ImplicitBufferPtr);
217 
218     YamlIO.mapOptional("workItemIDX", AI.WorkItemIDX);
219     YamlIO.mapOptional("workItemIDY", AI.WorkItemIDY);
220     YamlIO.mapOptional("workItemIDZ", AI.WorkItemIDZ);
221   }
222 };
223 
224 // Default to default mode for default calling convention.
225 struct SIMode {
226   bool IEEE = true;
227   bool DX10Clamp = true;
228   bool FP32InputDenormals = true;
229   bool FP32OutputDenormals = true;
230   bool FP64FP16InputDenormals = true;
231   bool FP64FP16OutputDenormals = true;
232 
233   SIMode() = default;
234 
235   SIMode(const AMDGPU::SIModeRegisterDefaults &Mode) {
236     IEEE = Mode.IEEE;
237     DX10Clamp = Mode.DX10Clamp;
238     FP32InputDenormals = Mode.FP32InputDenormals;
239     FP32OutputDenormals = Mode.FP32OutputDenormals;
240     FP64FP16InputDenormals = Mode.FP64FP16InputDenormals;
241     FP64FP16OutputDenormals = Mode.FP64FP16OutputDenormals;
242   }
243 
244   bool operator ==(const SIMode Other) const {
245     return IEEE == Other.IEEE &&
246            DX10Clamp == Other.DX10Clamp &&
247            FP32InputDenormals == Other.FP32InputDenormals &&
248            FP32OutputDenormals == Other.FP32OutputDenormals &&
249            FP64FP16InputDenormals == Other.FP64FP16InputDenormals &&
250            FP64FP16OutputDenormals == Other.FP64FP16OutputDenormals;
251   }
252 };
253 
254 template <> struct MappingTraits<SIMode> {
255   static void mapping(IO &YamlIO, SIMode &Mode) {
256     YamlIO.mapOptional("ieee", Mode.IEEE, true);
257     YamlIO.mapOptional("dx10-clamp", Mode.DX10Clamp, true);
258     YamlIO.mapOptional("fp32-input-denormals", Mode.FP32InputDenormals, true);
259     YamlIO.mapOptional("fp32-output-denormals", Mode.FP32OutputDenormals, true);
260     YamlIO.mapOptional("fp64-fp16-input-denormals", Mode.FP64FP16InputDenormals, true);
261     YamlIO.mapOptional("fp64-fp16-output-denormals", Mode.FP64FP16OutputDenormals, true);
262   }
263 };
264 
265 struct SIMachineFunctionInfo final : public yaml::MachineFunctionInfo {
266   uint64_t ExplicitKernArgSize = 0;
267   unsigned MaxKernArgAlign = 0;
268   unsigned LDSSize = 0;
269   Align DynLDSAlign;
270   bool IsEntryFunction = false;
271   bool NoSignedZerosFPMath = false;
272   bool MemoryBound = false;
273   bool WaveLimiter = false;
274   bool HasSpilledSGPRs = false;
275   bool HasSpilledVGPRs = false;
276   uint32_t HighBitsOf32BitAddress = 0;
277 
278   StringValue ScratchRSrcReg = "$private_rsrc_reg";
279   StringValue FrameOffsetReg = "$fp_reg";
280   StringValue StackPtrOffsetReg = "$sp_reg";
281 
282   Optional<SIArgumentInfo> ArgInfo;
283   SIMode Mode;
284 
285   SIMachineFunctionInfo() = default;
286   SIMachineFunctionInfo(const llvm::SIMachineFunctionInfo &,
287                         const TargetRegisterInfo &TRI);
288 
289   void mappingImpl(yaml::IO &YamlIO) override;
290   ~SIMachineFunctionInfo() = default;
291 };
292 
293 template <> struct MappingTraits<SIMachineFunctionInfo> {
294   static void mapping(IO &YamlIO, SIMachineFunctionInfo &MFI) {
295     YamlIO.mapOptional("explicitKernArgSize", MFI.ExplicitKernArgSize,
296                        UINT64_C(0));
297     YamlIO.mapOptional("maxKernArgAlign", MFI.MaxKernArgAlign, 0u);
298     YamlIO.mapOptional("ldsSize", MFI.LDSSize, 0u);
299     YamlIO.mapOptional("dynLDSAlign", MFI.DynLDSAlign, Align());
300     YamlIO.mapOptional("isEntryFunction", MFI.IsEntryFunction, false);
301     YamlIO.mapOptional("noSignedZerosFPMath", MFI.NoSignedZerosFPMath, false);
302     YamlIO.mapOptional("memoryBound", MFI.MemoryBound, false);
303     YamlIO.mapOptional("waveLimiter", MFI.WaveLimiter, false);
304     YamlIO.mapOptional("hasSpilledSGPRs", MFI.HasSpilledSGPRs, false);
305     YamlIO.mapOptional("hasSpilledVGPRs", MFI.HasSpilledVGPRs, false);
306     YamlIO.mapOptional("scratchRSrcReg", MFI.ScratchRSrcReg,
307                        StringValue("$private_rsrc_reg"));
308     YamlIO.mapOptional("frameOffsetReg", MFI.FrameOffsetReg,
309                        StringValue("$fp_reg"));
310     YamlIO.mapOptional("stackPtrOffsetReg", MFI.StackPtrOffsetReg,
311                        StringValue("$sp_reg"));
312     YamlIO.mapOptional("argumentInfo", MFI.ArgInfo);
313     YamlIO.mapOptional("mode", MFI.Mode, SIMode());
314     YamlIO.mapOptional("highBitsOf32BitAddress",
315                        MFI.HighBitsOf32BitAddress, 0u);
316   }
317 };
318 
319 } // end namespace yaml
320 
321 /// This class keeps track of the SPI_SP_INPUT_ADDR config register, which
322 /// tells the hardware which interpolation parameters to load.
323 class SIMachineFunctionInfo final : public AMDGPUMachineFunction {
324   friend class GCNTargetMachine;
325 
326   Register TIDReg = AMDGPU::NoRegister;
327 
328   // Registers that may be reserved for spilling purposes. These may be the same
329   // as the input registers.
330   Register ScratchRSrcReg = AMDGPU::PRIVATE_RSRC_REG;
331 
332   // This is the the unswizzled offset from the current dispatch's scratch wave
333   // base to the beginning of the current function's frame.
334   Register FrameOffsetReg = AMDGPU::FP_REG;
335 
336   // This is an ABI register used in the non-entry calling convention to
337   // communicate the unswizzled offset from the current dispatch's scratch wave
338   // base to the beginning of the new function's frame.
339   Register StackPtrOffsetReg = AMDGPU::SP_REG;
340 
341   AMDGPUFunctionArgInfo ArgInfo;
342 
343   // Graphics info.
344   unsigned PSInputAddr = 0;
345   unsigned PSInputEnable = 0;
346 
347   /// Number of bytes of arguments this function has on the stack. If the callee
348   /// is expected to restore the argument stack this should be a multiple of 16,
349   /// all usable during a tail call.
350   ///
351   /// The alternative would forbid tail call optimisation in some cases: if we
352   /// want to transfer control from a function with 8-bytes of stack-argument
353   /// space to a function with 16-bytes then misalignment of this value would
354   /// make a stack adjustment necessary, which could not be undone by the
355   /// callee.
356   unsigned BytesInStackArgArea = 0;
357 
358   bool ReturnsVoid = true;
359 
360   // A pair of default/requested minimum/maximum flat work group sizes.
361   // Minimum - first, maximum - second.
362   std::pair<unsigned, unsigned> FlatWorkGroupSizes = {0, 0};
363 
364   // A pair of default/requested minimum/maximum number of waves per execution
365   // unit. Minimum - first, maximum - second.
366   std::pair<unsigned, unsigned> WavesPerEU = {0, 0};
367 
368   DenseMap<const Value *,
369            std::unique_ptr<const AMDGPUBufferPseudoSourceValue>> BufferPSVs;
370   DenseMap<const Value *,
371            std::unique_ptr<const AMDGPUImagePseudoSourceValue>> ImagePSVs;
372   std::unique_ptr<const AMDGPUGWSResourcePseudoSourceValue> GWSResourcePSV;
373 
374 private:
375   unsigned LDSWaveSpillSize = 0;
376   unsigned NumUserSGPRs = 0;
377   unsigned NumSystemSGPRs = 0;
378 
379   bool HasSpilledSGPRs = false;
380   bool HasSpilledVGPRs = false;
381   bool HasNonSpillStackObjects = false;
382   bool IsStackRealigned = false;
383 
384   unsigned NumSpilledSGPRs = 0;
385   unsigned NumSpilledVGPRs = 0;
386 
387   // Feature bits required for inputs passed in user SGPRs.
388   bool PrivateSegmentBuffer : 1;
389   bool DispatchPtr : 1;
390   bool QueuePtr : 1;
391   bool KernargSegmentPtr : 1;
392   bool DispatchID : 1;
393   bool FlatScratchInit : 1;
394 
395   // Feature bits required for inputs passed in system SGPRs.
396   bool WorkGroupIDX : 1; // Always initialized.
397   bool WorkGroupIDY : 1;
398   bool WorkGroupIDZ : 1;
399   bool WorkGroupInfo : 1;
400   bool PrivateSegmentWaveByteOffset : 1;
401 
402   bool WorkItemIDX : 1; // Always initialized.
403   bool WorkItemIDY : 1;
404   bool WorkItemIDZ : 1;
405 
406   // Private memory buffer
407   // Compute directly in sgpr[0:1]
408   // Other shaders indirect 64-bits at sgpr[0:1]
409   bool ImplicitBufferPtr : 1;
410 
411   // Pointer to where the ABI inserts special kernel arguments separate from the
412   // user arguments. This is an offset from the KernargSegmentPtr.
413   bool ImplicitArgPtr : 1;
414 
415   // The hard-wired high half of the address of the global information table
416   // for AMDPAL OS type. 0xffffffff represents no hard-wired high half, since
417   // current hardware only allows a 16 bit value.
418   unsigned GITPtrHigh;
419 
420   unsigned HighBitsOf32BitAddress;
421   unsigned GDSSize;
422 
423   // Current recorded maximum possible occupancy.
424   unsigned Occupancy;
425 
426   MCPhysReg getNextUserSGPR() const;
427 
428   MCPhysReg getNextSystemSGPR() const;
429 
430 public:
431   struct SpilledReg {
432     Register VGPR;
433     int Lane = -1;
434 
435     SpilledReg() = default;
436     SpilledReg(Register R, int L) : VGPR (R), Lane (L) {}
437 
438     bool hasLane() { return Lane != -1;}
439     bool hasReg() { return VGPR != 0;}
440   };
441 
442   struct SGPRSpillVGPRCSR {
443     // VGPR used for SGPR spills
444     Register VGPR;
445 
446     // If the VGPR is a CSR, the stack slot used to save/restore it in the
447     // prolog/epilog.
448     Optional<int> FI;
449 
450     SGPRSpillVGPRCSR(Register V, Optional<int> F) : VGPR(V), FI(F) {}
451   };
452 
453   struct VGPRSpillToAGPR {
454     SmallVector<MCPhysReg, 32> Lanes;
455     bool FullyAllocated = false;
456   };
457 
458   SparseBitVector<> WWMReservedRegs;
459 
460   void ReserveWWMRegister(Register Reg) { WWMReservedRegs.set(Reg); }
461 
462 private:
463   // Track VGPR + wave index for each subregister of the SGPR spilled to
464   // frameindex key.
465   DenseMap<int, std::vector<SpilledReg>> SGPRToVGPRSpills;
466   unsigned NumVGPRSpillLanes = 0;
467   SmallVector<SGPRSpillVGPRCSR, 2> SpillVGPRs;
468 
469   DenseMap<int, VGPRSpillToAGPR> VGPRToAGPRSpills;
470 
471   // AGPRs used for VGPR spills.
472   SmallVector<MCPhysReg, 32> SpillAGPR;
473 
474   // VGPRs used for AGPR spills.
475   SmallVector<MCPhysReg, 32> SpillVGPR;
476 
477 public: // FIXME
478   /// If this is set, an SGPR used for save/restore of the register used for the
479   /// frame pointer.
480   Register SGPRForFPSaveRestoreCopy;
481   Optional<int> FramePointerSaveIndex;
482 
483   /// If this is set, an SGPR used for save/restore of the register used for the
484   /// base pointer.
485   Register SGPRForBPSaveRestoreCopy;
486   Optional<int> BasePointerSaveIndex;
487 
488   Register VGPRReservedForSGPRSpill;
489   bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg);
490 
491 public:
492   SIMachineFunctionInfo(const MachineFunction &MF);
493 
494   bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI);
495 
496   ArrayRef<SpilledReg> getSGPRToVGPRSpills(int FrameIndex) const {
497     auto I = SGPRToVGPRSpills.find(FrameIndex);
498     return (I == SGPRToVGPRSpills.end()) ?
499       ArrayRef<SpilledReg>() : makeArrayRef(I->second);
500   }
501 
502   ArrayRef<SGPRSpillVGPRCSR> getSGPRSpillVGPRs() const {
503     return SpillVGPRs;
504   }
505 
506   void setSGPRSpillVGPRs(Register NewVGPR, Optional<int> newFI, int Index) {
507     SpillVGPRs[Index].VGPR = NewVGPR;
508     SpillVGPRs[Index].FI = newFI;
509     VGPRReservedForSGPRSpill = NewVGPR;
510   }
511 
512   bool removeVGPRForSGPRSpill(Register ReservedVGPR, MachineFunction &MF);
513 
514   ArrayRef<MCPhysReg> getAGPRSpillVGPRs() const {
515     return SpillAGPR;
516   }
517 
518   ArrayRef<MCPhysReg> getVGPRSpillAGPRs() const {
519     return SpillVGPR;
520   }
521 
522   MCPhysReg getVGPRToAGPRSpill(int FrameIndex, unsigned Lane) const {
523     auto I = VGPRToAGPRSpills.find(FrameIndex);
524     return (I == VGPRToAGPRSpills.end()) ? (MCPhysReg)AMDGPU::NoRegister
525                                          : I->second.Lanes[Lane];
526   }
527 
528   bool haveFreeLanesForSGPRSpill(const MachineFunction &MF,
529                                  unsigned NumLane) const;
530   bool allocateSGPRSpillToVGPR(MachineFunction &MF, int FI);
531   bool reserveVGPRforSGPRSpills(MachineFunction &MF);
532   bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR);
533   void removeDeadFrameIndices(MachineFrameInfo &MFI);
534 
535   bool hasCalculatedTID() const { return TIDReg != 0; };
536   Register getTIDReg() const { return TIDReg; };
537   void setTIDReg(Register Reg) { TIDReg = Reg; }
538 
539   unsigned getBytesInStackArgArea() const {
540     return BytesInStackArgArea;
541   }
542 
543   void setBytesInStackArgArea(unsigned Bytes) {
544     BytesInStackArgArea = Bytes;
545   }
546 
547   // Add user SGPRs.
548   Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI);
549   Register addDispatchPtr(const SIRegisterInfo &TRI);
550   Register addQueuePtr(const SIRegisterInfo &TRI);
551   Register addKernargSegmentPtr(const SIRegisterInfo &TRI);
552   Register addDispatchID(const SIRegisterInfo &TRI);
553   Register addFlatScratchInit(const SIRegisterInfo &TRI);
554   Register addImplicitBufferPtr(const SIRegisterInfo &TRI);
555 
556   // Add system SGPRs.
557   Register addWorkGroupIDX() {
558     ArgInfo.WorkGroupIDX = ArgDescriptor::createRegister(getNextSystemSGPR());
559     NumSystemSGPRs += 1;
560     return ArgInfo.WorkGroupIDX.getRegister();
561   }
562 
563   Register addWorkGroupIDY() {
564     ArgInfo.WorkGroupIDY = ArgDescriptor::createRegister(getNextSystemSGPR());
565     NumSystemSGPRs += 1;
566     return ArgInfo.WorkGroupIDY.getRegister();
567   }
568 
569   Register addWorkGroupIDZ() {
570     ArgInfo.WorkGroupIDZ = ArgDescriptor::createRegister(getNextSystemSGPR());
571     NumSystemSGPRs += 1;
572     return ArgInfo.WorkGroupIDZ.getRegister();
573   }
574 
575   Register addWorkGroupInfo() {
576     ArgInfo.WorkGroupInfo = ArgDescriptor::createRegister(getNextSystemSGPR());
577     NumSystemSGPRs += 1;
578     return ArgInfo.WorkGroupInfo.getRegister();
579   }
580 
581   // Add special VGPR inputs
582   void setWorkItemIDX(ArgDescriptor Arg) {
583     ArgInfo.WorkItemIDX = Arg;
584   }
585 
586   void setWorkItemIDY(ArgDescriptor Arg) {
587     ArgInfo.WorkItemIDY = Arg;
588   }
589 
590   void setWorkItemIDZ(ArgDescriptor Arg) {
591     ArgInfo.WorkItemIDZ = Arg;
592   }
593 
594   Register addPrivateSegmentWaveByteOffset() {
595     ArgInfo.PrivateSegmentWaveByteOffset
596       = ArgDescriptor::createRegister(getNextSystemSGPR());
597     NumSystemSGPRs += 1;
598     return ArgInfo.PrivateSegmentWaveByteOffset.getRegister();
599   }
600 
601   void setPrivateSegmentWaveByteOffset(Register Reg) {
602     ArgInfo.PrivateSegmentWaveByteOffset = ArgDescriptor::createRegister(Reg);
603   }
604 
605   bool hasPrivateSegmentBuffer() const {
606     return PrivateSegmentBuffer;
607   }
608 
609   bool hasDispatchPtr() const {
610     return DispatchPtr;
611   }
612 
613   bool hasQueuePtr() const {
614     return QueuePtr;
615   }
616 
617   bool hasKernargSegmentPtr() const {
618     return KernargSegmentPtr;
619   }
620 
621   bool hasDispatchID() const {
622     return DispatchID;
623   }
624 
625   bool hasFlatScratchInit() const {
626     return FlatScratchInit;
627   }
628 
629   bool hasWorkGroupIDX() const {
630     return WorkGroupIDX;
631   }
632 
633   bool hasWorkGroupIDY() const {
634     return WorkGroupIDY;
635   }
636 
637   bool hasWorkGroupIDZ() const {
638     return WorkGroupIDZ;
639   }
640 
641   bool hasWorkGroupInfo() const {
642     return WorkGroupInfo;
643   }
644 
645   bool hasPrivateSegmentWaveByteOffset() const {
646     return PrivateSegmentWaveByteOffset;
647   }
648 
649   bool hasWorkItemIDX() const {
650     return WorkItemIDX;
651   }
652 
653   bool hasWorkItemIDY() const {
654     return WorkItemIDY;
655   }
656 
657   bool hasWorkItemIDZ() const {
658     return WorkItemIDZ;
659   }
660 
661   bool hasImplicitArgPtr() const {
662     return ImplicitArgPtr;
663   }
664 
665   bool hasImplicitBufferPtr() const {
666     return ImplicitBufferPtr;
667   }
668 
669   AMDGPUFunctionArgInfo &getArgInfo() {
670     return ArgInfo;
671   }
672 
673   const AMDGPUFunctionArgInfo &getArgInfo() const {
674     return ArgInfo;
675   }
676 
677   std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT>
678   getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const {
679     return ArgInfo.getPreloadedValue(Value);
680   }
681 
682   MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const {
683     auto Arg = std::get<0>(ArgInfo.getPreloadedValue(Value));
684     return Arg ? Arg->getRegister() : MCRegister();
685   }
686 
687   unsigned getGITPtrHigh() const {
688     return GITPtrHigh;
689   }
690 
691   Register getGITPtrLoReg(const MachineFunction &MF) const;
692 
693   uint32_t get32BitAddressHighBits() const {
694     return HighBitsOf32BitAddress;
695   }
696 
697   unsigned getGDSSize() const {
698     return GDSSize;
699   }
700 
701   unsigned getNumUserSGPRs() const {
702     return NumUserSGPRs;
703   }
704 
705   unsigned getNumPreloadedSGPRs() const {
706     return NumUserSGPRs + NumSystemSGPRs;
707   }
708 
709   Register getPrivateSegmentWaveByteOffsetSystemSGPR() const {
710     return ArgInfo.PrivateSegmentWaveByteOffset.getRegister();
711   }
712 
713   /// Returns the physical register reserved for use as the resource
714   /// descriptor for scratch accesses.
715   Register getScratchRSrcReg() const {
716     return ScratchRSrcReg;
717   }
718 
719   void setScratchRSrcReg(Register Reg) {
720     assert(Reg != 0 && "Should never be unset");
721     ScratchRSrcReg = Reg;
722   }
723 
724   Register getFrameOffsetReg() const {
725     return FrameOffsetReg;
726   }
727 
728   void setFrameOffsetReg(Register Reg) {
729     assert(Reg != 0 && "Should never be unset");
730     FrameOffsetReg = Reg;
731   }
732 
733   void setStackPtrOffsetReg(Register Reg) {
734     assert(Reg != 0 && "Should never be unset");
735     StackPtrOffsetReg = Reg;
736   }
737 
738   // Note the unset value for this is AMDGPU::SP_REG rather than
739   // NoRegister. This is mostly a workaround for MIR tests where state that
740   // can't be directly computed from the function is not preserved in serialized
741   // MIR.
742   Register getStackPtrOffsetReg() const {
743     return StackPtrOffsetReg;
744   }
745 
746   Register getQueuePtrUserSGPR() const {
747     return ArgInfo.QueuePtr.getRegister();
748   }
749 
750   Register getImplicitBufferPtrUserSGPR() const {
751     return ArgInfo.ImplicitBufferPtr.getRegister();
752   }
753 
754   bool hasSpilledSGPRs() const {
755     return HasSpilledSGPRs;
756   }
757 
758   void setHasSpilledSGPRs(bool Spill = true) {
759     HasSpilledSGPRs = Spill;
760   }
761 
762   bool hasSpilledVGPRs() const {
763     return HasSpilledVGPRs;
764   }
765 
766   void setHasSpilledVGPRs(bool Spill = true) {
767     HasSpilledVGPRs = Spill;
768   }
769 
770   bool hasNonSpillStackObjects() const {
771     return HasNonSpillStackObjects;
772   }
773 
774   void setHasNonSpillStackObjects(bool StackObject = true) {
775     HasNonSpillStackObjects = StackObject;
776   }
777 
778   bool isStackRealigned() const {
779     return IsStackRealigned;
780   }
781 
782   void setIsStackRealigned(bool Realigned = true) {
783     IsStackRealigned = Realigned;
784   }
785 
786   unsigned getNumSpilledSGPRs() const {
787     return NumSpilledSGPRs;
788   }
789 
790   unsigned getNumSpilledVGPRs() const {
791     return NumSpilledVGPRs;
792   }
793 
794   void addToSpilledSGPRs(unsigned num) {
795     NumSpilledSGPRs += num;
796   }
797 
798   void addToSpilledVGPRs(unsigned num) {
799     NumSpilledVGPRs += num;
800   }
801 
802   unsigned getPSInputAddr() const {
803     return PSInputAddr;
804   }
805 
806   unsigned getPSInputEnable() const {
807     return PSInputEnable;
808   }
809 
810   bool isPSInputAllocated(unsigned Index) const {
811     return PSInputAddr & (1 << Index);
812   }
813 
814   void markPSInputAllocated(unsigned Index) {
815     PSInputAddr |= 1 << Index;
816   }
817 
818   void markPSInputEnabled(unsigned Index) {
819     PSInputEnable |= 1 << Index;
820   }
821 
822   bool returnsVoid() const {
823     return ReturnsVoid;
824   }
825 
826   void setIfReturnsVoid(bool Value) {
827     ReturnsVoid = Value;
828   }
829 
830   /// \returns A pair of default/requested minimum/maximum flat work group sizes
831   /// for this function.
832   std::pair<unsigned, unsigned> getFlatWorkGroupSizes() const {
833     return FlatWorkGroupSizes;
834   }
835 
836   /// \returns Default/requested minimum flat work group size for this function.
837   unsigned getMinFlatWorkGroupSize() const {
838     return FlatWorkGroupSizes.first;
839   }
840 
841   /// \returns Default/requested maximum flat work group size for this function.
842   unsigned getMaxFlatWorkGroupSize() const {
843     return FlatWorkGroupSizes.second;
844   }
845 
846   /// \returns A pair of default/requested minimum/maximum number of waves per
847   /// execution unit.
848   std::pair<unsigned, unsigned> getWavesPerEU() const {
849     return WavesPerEU;
850   }
851 
852   /// \returns Default/requested minimum number of waves per execution unit.
853   unsigned getMinWavesPerEU() const {
854     return WavesPerEU.first;
855   }
856 
857   /// \returns Default/requested maximum number of waves per execution unit.
858   unsigned getMaxWavesPerEU() const {
859     return WavesPerEU.second;
860   }
861 
862   /// \returns SGPR used for \p Dim's work group ID.
863   Register getWorkGroupIDSGPR(unsigned Dim) const {
864     switch (Dim) {
865     case 0:
866       assert(hasWorkGroupIDX());
867       return ArgInfo.WorkGroupIDX.getRegister();
868     case 1:
869       assert(hasWorkGroupIDY());
870       return ArgInfo.WorkGroupIDY.getRegister();
871     case 2:
872       assert(hasWorkGroupIDZ());
873       return ArgInfo.WorkGroupIDZ.getRegister();
874     }
875     llvm_unreachable("unexpected dimension");
876   }
877 
878   unsigned getLDSWaveSpillSize() const {
879     return LDSWaveSpillSize;
880   }
881 
882   const AMDGPUBufferPseudoSourceValue *getBufferPSV(const SIInstrInfo &TII,
883                                                     const Value *BufferRsrc) {
884     assert(BufferRsrc);
885     auto PSV = BufferPSVs.try_emplace(
886       BufferRsrc,
887       std::make_unique<AMDGPUBufferPseudoSourceValue>(TII));
888     return PSV.first->second.get();
889   }
890 
891   const AMDGPUImagePseudoSourceValue *getImagePSV(const SIInstrInfo &TII,
892                                                   const Value *ImgRsrc) {
893     assert(ImgRsrc);
894     auto PSV = ImagePSVs.try_emplace(
895       ImgRsrc,
896       std::make_unique<AMDGPUImagePseudoSourceValue>(TII));
897     return PSV.first->second.get();
898   }
899 
900   const AMDGPUGWSResourcePseudoSourceValue *getGWSPSV(const SIInstrInfo &TII) {
901     if (!GWSResourcePSV) {
902       GWSResourcePSV =
903           std::make_unique<AMDGPUGWSResourcePseudoSourceValue>(TII);
904     }
905 
906     return GWSResourcePSV.get();
907   }
908 
909   unsigned getOccupancy() const {
910     return Occupancy;
911   }
912 
913   unsigned getMinAllowedOccupancy() const {
914     if (!isMemoryBound() && !needsWaveLimiter())
915       return Occupancy;
916     return (Occupancy < 4) ? Occupancy : 4;
917   }
918 
919   void limitOccupancy(const MachineFunction &MF);
920 
921   void limitOccupancy(unsigned Limit) {
922     if (Occupancy > Limit)
923       Occupancy = Limit;
924   }
925 
926   void increaseOccupancy(const MachineFunction &MF, unsigned Limit) {
927     if (Occupancy < Limit)
928       Occupancy = Limit;
929     limitOccupancy(MF);
930   }
931 };
932 
933 } // end namespace llvm
934 
935 #endif // LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H
936