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