xref: /llvm-project/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h (revision 20566a2ed825c05d56708552d33d95ee12255f46)
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   // TODO: 10 may be a better default since it's the maximum.
279   unsigned Occupancy = 0;
280 
281   StringValue ScratchRSrcReg = "$private_rsrc_reg";
282   StringValue FrameOffsetReg = "$fp_reg";
283   StringValue StackPtrOffsetReg = "$sp_reg";
284 
285   Optional<SIArgumentInfo> ArgInfo;
286   SIMode Mode;
287 
288   SIMachineFunctionInfo() = default;
289   SIMachineFunctionInfo(const llvm::SIMachineFunctionInfo &,
290                         const TargetRegisterInfo &TRI);
291 
292   void mappingImpl(yaml::IO &YamlIO) override;
293   ~SIMachineFunctionInfo() = default;
294 };
295 
296 template <> struct MappingTraits<SIMachineFunctionInfo> {
297   static void mapping(IO &YamlIO, SIMachineFunctionInfo &MFI) {
298     YamlIO.mapOptional("explicitKernArgSize", MFI.ExplicitKernArgSize,
299                        UINT64_C(0));
300     YamlIO.mapOptional("maxKernArgAlign", MFI.MaxKernArgAlign, 0u);
301     YamlIO.mapOptional("ldsSize", MFI.LDSSize, 0u);
302     YamlIO.mapOptional("dynLDSAlign", MFI.DynLDSAlign, Align());
303     YamlIO.mapOptional("isEntryFunction", MFI.IsEntryFunction, false);
304     YamlIO.mapOptional("noSignedZerosFPMath", MFI.NoSignedZerosFPMath, false);
305     YamlIO.mapOptional("memoryBound", MFI.MemoryBound, false);
306     YamlIO.mapOptional("waveLimiter", MFI.WaveLimiter, false);
307     YamlIO.mapOptional("hasSpilledSGPRs", MFI.HasSpilledSGPRs, false);
308     YamlIO.mapOptional("hasSpilledVGPRs", MFI.HasSpilledVGPRs, false);
309     YamlIO.mapOptional("scratchRSrcReg", MFI.ScratchRSrcReg,
310                        StringValue("$private_rsrc_reg"));
311     YamlIO.mapOptional("frameOffsetReg", MFI.FrameOffsetReg,
312                        StringValue("$fp_reg"));
313     YamlIO.mapOptional("stackPtrOffsetReg", MFI.StackPtrOffsetReg,
314                        StringValue("$sp_reg"));
315     YamlIO.mapOptional("argumentInfo", MFI.ArgInfo);
316     YamlIO.mapOptional("mode", MFI.Mode, SIMode());
317     YamlIO.mapOptional("highBitsOf32BitAddress",
318                        MFI.HighBitsOf32BitAddress, 0u);
319     YamlIO.mapOptional("occupancy", MFI.Occupancy, 0);
320   }
321 };
322 
323 } // end namespace yaml
324 
325 /// This class keeps track of the SPI_SP_INPUT_ADDR config register, which
326 /// tells the hardware which interpolation parameters to load.
327 class SIMachineFunctionInfo final : public AMDGPUMachineFunction {
328   friend class GCNTargetMachine;
329 
330   Register TIDReg = AMDGPU::NoRegister;
331 
332   // Registers that may be reserved for spilling purposes. These may be the same
333   // as the input registers.
334   Register ScratchRSrcReg = AMDGPU::PRIVATE_RSRC_REG;
335 
336   // This is the the unswizzled offset from the current dispatch's scratch wave
337   // base to the beginning of the current function's frame.
338   Register FrameOffsetReg = AMDGPU::FP_REG;
339 
340   // This is an ABI register used in the non-entry calling convention to
341   // communicate the unswizzled offset from the current dispatch's scratch wave
342   // base to the beginning of the new function's frame.
343   Register StackPtrOffsetReg = AMDGPU::SP_REG;
344 
345   AMDGPUFunctionArgInfo ArgInfo;
346 
347   // Graphics info.
348   unsigned PSInputAddr = 0;
349   unsigned PSInputEnable = 0;
350 
351   /// Number of bytes of arguments this function has on the stack. If the callee
352   /// is expected to restore the argument stack this should be a multiple of 16,
353   /// all usable during a tail call.
354   ///
355   /// The alternative would forbid tail call optimisation in some cases: if we
356   /// want to transfer control from a function with 8-bytes of stack-argument
357   /// space to a function with 16-bytes then misalignment of this value would
358   /// make a stack adjustment necessary, which could not be undone by the
359   /// callee.
360   unsigned BytesInStackArgArea = 0;
361 
362   bool ReturnsVoid = true;
363 
364   // A pair of default/requested minimum/maximum flat work group sizes.
365   // Minimum - first, maximum - second.
366   std::pair<unsigned, unsigned> FlatWorkGroupSizes = {0, 0};
367 
368   // A pair of default/requested minimum/maximum number of waves per execution
369   // unit. Minimum - first, maximum - second.
370   std::pair<unsigned, unsigned> WavesPerEU = {0, 0};
371 
372   DenseMap<const Value *,
373            std::unique_ptr<const AMDGPUBufferPseudoSourceValue>> BufferPSVs;
374   DenseMap<const Value *,
375            std::unique_ptr<const AMDGPUImagePseudoSourceValue>> ImagePSVs;
376   std::unique_ptr<const AMDGPUGWSResourcePseudoSourceValue> GWSResourcePSV;
377 
378 private:
379   unsigned LDSWaveSpillSize = 0;
380   unsigned NumUserSGPRs = 0;
381   unsigned NumSystemSGPRs = 0;
382 
383   bool HasSpilledSGPRs = false;
384   bool HasSpilledVGPRs = false;
385   bool HasNonSpillStackObjects = false;
386   bool IsStackRealigned = false;
387 
388   unsigned NumSpilledSGPRs = 0;
389   unsigned NumSpilledVGPRs = 0;
390 
391   // Feature bits required for inputs passed in user SGPRs.
392   bool PrivateSegmentBuffer : 1;
393   bool DispatchPtr : 1;
394   bool QueuePtr : 1;
395   bool KernargSegmentPtr : 1;
396   bool DispatchID : 1;
397   bool FlatScratchInit : 1;
398 
399   // Feature bits required for inputs passed in system SGPRs.
400   bool WorkGroupIDX : 1; // Always initialized.
401   bool WorkGroupIDY : 1;
402   bool WorkGroupIDZ : 1;
403   bool WorkGroupInfo : 1;
404   bool PrivateSegmentWaveByteOffset : 1;
405 
406   bool WorkItemIDX : 1; // Always initialized.
407   bool WorkItemIDY : 1;
408   bool WorkItemIDZ : 1;
409 
410   // Private memory buffer
411   // Compute directly in sgpr[0:1]
412   // Other shaders indirect 64-bits at sgpr[0:1]
413   bool ImplicitBufferPtr : 1;
414 
415   // Pointer to where the ABI inserts special kernel arguments separate from the
416   // user arguments. This is an offset from the KernargSegmentPtr.
417   bool ImplicitArgPtr : 1;
418 
419   // The hard-wired high half of the address of the global information table
420   // for AMDPAL OS type. 0xffffffff represents no hard-wired high half, since
421   // current hardware only allows a 16 bit value.
422   unsigned GITPtrHigh;
423 
424   unsigned HighBitsOf32BitAddress;
425   unsigned GDSSize;
426 
427   // Current recorded maximum possible occupancy.
428   unsigned Occupancy;
429 
430   MCPhysReg getNextUserSGPR() const;
431 
432   MCPhysReg getNextSystemSGPR() const;
433 
434 public:
435   struct SpilledReg {
436     Register VGPR;
437     int Lane = -1;
438 
439     SpilledReg() = default;
440     SpilledReg(Register R, int L) : VGPR (R), Lane (L) {}
441 
442     bool hasLane() { return Lane != -1;}
443     bool hasReg() { return VGPR != 0;}
444   };
445 
446   struct SGPRSpillVGPRCSR {
447     // VGPR used for SGPR spills
448     Register VGPR;
449 
450     // If the VGPR is a CSR, the stack slot used to save/restore it in the
451     // prolog/epilog.
452     Optional<int> FI;
453 
454     SGPRSpillVGPRCSR(Register V, Optional<int> F) : VGPR(V), FI(F) {}
455   };
456 
457   struct VGPRSpillToAGPR {
458     SmallVector<MCPhysReg, 32> Lanes;
459     bool FullyAllocated = false;
460   };
461 
462   SparseBitVector<> WWMReservedRegs;
463 
464   void ReserveWWMRegister(Register Reg) { WWMReservedRegs.set(Reg); }
465 
466 private:
467   // Track VGPR + wave index for each subregister of the SGPR spilled to
468   // frameindex key.
469   DenseMap<int, std::vector<SpilledReg>> SGPRToVGPRSpills;
470   unsigned NumVGPRSpillLanes = 0;
471   SmallVector<SGPRSpillVGPRCSR, 2> SpillVGPRs;
472 
473   DenseMap<int, VGPRSpillToAGPR> VGPRToAGPRSpills;
474 
475   // AGPRs used for VGPR spills.
476   SmallVector<MCPhysReg, 32> SpillAGPR;
477 
478   // VGPRs used for AGPR spills.
479   SmallVector<MCPhysReg, 32> SpillVGPR;
480 
481 public: // FIXME
482   /// If this is set, an SGPR used for save/restore of the register used for the
483   /// frame pointer.
484   Register SGPRForFPSaveRestoreCopy;
485   Optional<int> FramePointerSaveIndex;
486 
487   /// If this is set, an SGPR used for save/restore of the register used for the
488   /// base pointer.
489   Register SGPRForBPSaveRestoreCopy;
490   Optional<int> BasePointerSaveIndex;
491 
492   Register VGPRReservedForSGPRSpill;
493   bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg);
494 
495 public:
496   SIMachineFunctionInfo(const MachineFunction &MF);
497 
498   bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI);
499 
500   ArrayRef<SpilledReg> getSGPRToVGPRSpills(int FrameIndex) const {
501     auto I = SGPRToVGPRSpills.find(FrameIndex);
502     return (I == SGPRToVGPRSpills.end()) ?
503       ArrayRef<SpilledReg>() : makeArrayRef(I->second);
504   }
505 
506   ArrayRef<SGPRSpillVGPRCSR> getSGPRSpillVGPRs() const {
507     return SpillVGPRs;
508   }
509 
510   void setSGPRSpillVGPRs(Register NewVGPR, Optional<int> newFI, int Index) {
511     SpillVGPRs[Index].VGPR = NewVGPR;
512     SpillVGPRs[Index].FI = newFI;
513     VGPRReservedForSGPRSpill = NewVGPR;
514   }
515 
516   bool removeVGPRForSGPRSpill(Register ReservedVGPR, MachineFunction &MF);
517 
518   ArrayRef<MCPhysReg> getAGPRSpillVGPRs() const {
519     return SpillAGPR;
520   }
521 
522   ArrayRef<MCPhysReg> getVGPRSpillAGPRs() const {
523     return SpillVGPR;
524   }
525 
526   MCPhysReg getVGPRToAGPRSpill(int FrameIndex, unsigned Lane) const {
527     auto I = VGPRToAGPRSpills.find(FrameIndex);
528     return (I == VGPRToAGPRSpills.end()) ? (MCPhysReg)AMDGPU::NoRegister
529                                          : I->second.Lanes[Lane];
530   }
531 
532   bool haveFreeLanesForSGPRSpill(const MachineFunction &MF,
533                                  unsigned NumLane) const;
534   bool allocateSGPRSpillToVGPR(MachineFunction &MF, int FI);
535   bool reserveVGPRforSGPRSpills(MachineFunction &MF);
536   bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR);
537   void removeDeadFrameIndices(MachineFrameInfo &MFI);
538 
539   bool hasCalculatedTID() const { return TIDReg != 0; };
540   Register getTIDReg() const { return TIDReg; };
541   void setTIDReg(Register Reg) { TIDReg = Reg; }
542 
543   unsigned getBytesInStackArgArea() const {
544     return BytesInStackArgArea;
545   }
546 
547   void setBytesInStackArgArea(unsigned Bytes) {
548     BytesInStackArgArea = Bytes;
549   }
550 
551   // Add user SGPRs.
552   Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI);
553   Register addDispatchPtr(const SIRegisterInfo &TRI);
554   Register addQueuePtr(const SIRegisterInfo &TRI);
555   Register addKernargSegmentPtr(const SIRegisterInfo &TRI);
556   Register addDispatchID(const SIRegisterInfo &TRI);
557   Register addFlatScratchInit(const SIRegisterInfo &TRI);
558   Register addImplicitBufferPtr(const SIRegisterInfo &TRI);
559 
560   // Add system SGPRs.
561   Register addWorkGroupIDX() {
562     ArgInfo.WorkGroupIDX = ArgDescriptor::createRegister(getNextSystemSGPR());
563     NumSystemSGPRs += 1;
564     return ArgInfo.WorkGroupIDX.getRegister();
565   }
566 
567   Register addWorkGroupIDY() {
568     ArgInfo.WorkGroupIDY = ArgDescriptor::createRegister(getNextSystemSGPR());
569     NumSystemSGPRs += 1;
570     return ArgInfo.WorkGroupIDY.getRegister();
571   }
572 
573   Register addWorkGroupIDZ() {
574     ArgInfo.WorkGroupIDZ = ArgDescriptor::createRegister(getNextSystemSGPR());
575     NumSystemSGPRs += 1;
576     return ArgInfo.WorkGroupIDZ.getRegister();
577   }
578 
579   Register addWorkGroupInfo() {
580     ArgInfo.WorkGroupInfo = ArgDescriptor::createRegister(getNextSystemSGPR());
581     NumSystemSGPRs += 1;
582     return ArgInfo.WorkGroupInfo.getRegister();
583   }
584 
585   // Add special VGPR inputs
586   void setWorkItemIDX(ArgDescriptor Arg) {
587     ArgInfo.WorkItemIDX = Arg;
588   }
589 
590   void setWorkItemIDY(ArgDescriptor Arg) {
591     ArgInfo.WorkItemIDY = Arg;
592   }
593 
594   void setWorkItemIDZ(ArgDescriptor Arg) {
595     ArgInfo.WorkItemIDZ = Arg;
596   }
597 
598   Register addPrivateSegmentWaveByteOffset() {
599     ArgInfo.PrivateSegmentWaveByteOffset
600       = ArgDescriptor::createRegister(getNextSystemSGPR());
601     NumSystemSGPRs += 1;
602     return ArgInfo.PrivateSegmentWaveByteOffset.getRegister();
603   }
604 
605   void setPrivateSegmentWaveByteOffset(Register Reg) {
606     ArgInfo.PrivateSegmentWaveByteOffset = ArgDescriptor::createRegister(Reg);
607   }
608 
609   bool hasPrivateSegmentBuffer() const {
610     return PrivateSegmentBuffer;
611   }
612 
613   bool hasDispatchPtr() const {
614     return DispatchPtr;
615   }
616 
617   bool hasQueuePtr() const {
618     return QueuePtr;
619   }
620 
621   bool hasKernargSegmentPtr() const {
622     return KernargSegmentPtr;
623   }
624 
625   bool hasDispatchID() const {
626     return DispatchID;
627   }
628 
629   bool hasFlatScratchInit() const {
630     return FlatScratchInit;
631   }
632 
633   bool hasWorkGroupIDX() const {
634     return WorkGroupIDX;
635   }
636 
637   bool hasWorkGroupIDY() const {
638     return WorkGroupIDY;
639   }
640 
641   bool hasWorkGroupIDZ() const {
642     return WorkGroupIDZ;
643   }
644 
645   bool hasWorkGroupInfo() const {
646     return WorkGroupInfo;
647   }
648 
649   bool hasPrivateSegmentWaveByteOffset() const {
650     return PrivateSegmentWaveByteOffset;
651   }
652 
653   bool hasWorkItemIDX() const {
654     return WorkItemIDX;
655   }
656 
657   bool hasWorkItemIDY() const {
658     return WorkItemIDY;
659   }
660 
661   bool hasWorkItemIDZ() const {
662     return WorkItemIDZ;
663   }
664 
665   bool hasImplicitArgPtr() const {
666     return ImplicitArgPtr;
667   }
668 
669   bool hasImplicitBufferPtr() const {
670     return ImplicitBufferPtr;
671   }
672 
673   AMDGPUFunctionArgInfo &getArgInfo() {
674     return ArgInfo;
675   }
676 
677   const AMDGPUFunctionArgInfo &getArgInfo() const {
678     return ArgInfo;
679   }
680 
681   std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT>
682   getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const {
683     return ArgInfo.getPreloadedValue(Value);
684   }
685 
686   MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const {
687     auto Arg = std::get<0>(ArgInfo.getPreloadedValue(Value));
688     return Arg ? Arg->getRegister() : MCRegister();
689   }
690 
691   unsigned getGITPtrHigh() const {
692     return GITPtrHigh;
693   }
694 
695   Register getGITPtrLoReg(const MachineFunction &MF) const;
696 
697   uint32_t get32BitAddressHighBits() const {
698     return HighBitsOf32BitAddress;
699   }
700 
701   unsigned getGDSSize() const {
702     return GDSSize;
703   }
704 
705   unsigned getNumUserSGPRs() const {
706     return NumUserSGPRs;
707   }
708 
709   unsigned getNumPreloadedSGPRs() const {
710     return NumUserSGPRs + NumSystemSGPRs;
711   }
712 
713   Register getPrivateSegmentWaveByteOffsetSystemSGPR() const {
714     return ArgInfo.PrivateSegmentWaveByteOffset.getRegister();
715   }
716 
717   /// Returns the physical register reserved for use as the resource
718   /// descriptor for scratch accesses.
719   Register getScratchRSrcReg() const {
720     return ScratchRSrcReg;
721   }
722 
723   void setScratchRSrcReg(Register Reg) {
724     assert(Reg != 0 && "Should never be unset");
725     ScratchRSrcReg = Reg;
726   }
727 
728   Register getFrameOffsetReg() const {
729     return FrameOffsetReg;
730   }
731 
732   void setFrameOffsetReg(Register Reg) {
733     assert(Reg != 0 && "Should never be unset");
734     FrameOffsetReg = Reg;
735   }
736 
737   void setStackPtrOffsetReg(Register Reg) {
738     assert(Reg != 0 && "Should never be unset");
739     StackPtrOffsetReg = Reg;
740   }
741 
742   // Note the unset value for this is AMDGPU::SP_REG rather than
743   // NoRegister. This is mostly a workaround for MIR tests where state that
744   // can't be directly computed from the function is not preserved in serialized
745   // MIR.
746   Register getStackPtrOffsetReg() const {
747     return StackPtrOffsetReg;
748   }
749 
750   Register getQueuePtrUserSGPR() const {
751     return ArgInfo.QueuePtr.getRegister();
752   }
753 
754   Register getImplicitBufferPtrUserSGPR() const {
755     return ArgInfo.ImplicitBufferPtr.getRegister();
756   }
757 
758   bool hasSpilledSGPRs() const {
759     return HasSpilledSGPRs;
760   }
761 
762   void setHasSpilledSGPRs(bool Spill = true) {
763     HasSpilledSGPRs = Spill;
764   }
765 
766   bool hasSpilledVGPRs() const {
767     return HasSpilledVGPRs;
768   }
769 
770   void setHasSpilledVGPRs(bool Spill = true) {
771     HasSpilledVGPRs = Spill;
772   }
773 
774   bool hasNonSpillStackObjects() const {
775     return HasNonSpillStackObjects;
776   }
777 
778   void setHasNonSpillStackObjects(bool StackObject = true) {
779     HasNonSpillStackObjects = StackObject;
780   }
781 
782   bool isStackRealigned() const {
783     return IsStackRealigned;
784   }
785 
786   void setIsStackRealigned(bool Realigned = true) {
787     IsStackRealigned = Realigned;
788   }
789 
790   unsigned getNumSpilledSGPRs() const {
791     return NumSpilledSGPRs;
792   }
793 
794   unsigned getNumSpilledVGPRs() const {
795     return NumSpilledVGPRs;
796   }
797 
798   void addToSpilledSGPRs(unsigned num) {
799     NumSpilledSGPRs += num;
800   }
801 
802   void addToSpilledVGPRs(unsigned num) {
803     NumSpilledVGPRs += num;
804   }
805 
806   unsigned getPSInputAddr() const {
807     return PSInputAddr;
808   }
809 
810   unsigned getPSInputEnable() const {
811     return PSInputEnable;
812   }
813 
814   bool isPSInputAllocated(unsigned Index) const {
815     return PSInputAddr & (1 << Index);
816   }
817 
818   void markPSInputAllocated(unsigned Index) {
819     PSInputAddr |= 1 << Index;
820   }
821 
822   void markPSInputEnabled(unsigned Index) {
823     PSInputEnable |= 1 << Index;
824   }
825 
826   bool returnsVoid() const {
827     return ReturnsVoid;
828   }
829 
830   void setIfReturnsVoid(bool Value) {
831     ReturnsVoid = Value;
832   }
833 
834   /// \returns A pair of default/requested minimum/maximum flat work group sizes
835   /// for this function.
836   std::pair<unsigned, unsigned> getFlatWorkGroupSizes() const {
837     return FlatWorkGroupSizes;
838   }
839 
840   /// \returns Default/requested minimum flat work group size for this function.
841   unsigned getMinFlatWorkGroupSize() const {
842     return FlatWorkGroupSizes.first;
843   }
844 
845   /// \returns Default/requested maximum flat work group size for this function.
846   unsigned getMaxFlatWorkGroupSize() const {
847     return FlatWorkGroupSizes.second;
848   }
849 
850   /// \returns A pair of default/requested minimum/maximum number of waves per
851   /// execution unit.
852   std::pair<unsigned, unsigned> getWavesPerEU() const {
853     return WavesPerEU;
854   }
855 
856   /// \returns Default/requested minimum number of waves per execution unit.
857   unsigned getMinWavesPerEU() const {
858     return WavesPerEU.first;
859   }
860 
861   /// \returns Default/requested maximum number of waves per execution unit.
862   unsigned getMaxWavesPerEU() const {
863     return WavesPerEU.second;
864   }
865 
866   /// \returns SGPR used for \p Dim's work group ID.
867   Register getWorkGroupIDSGPR(unsigned Dim) const {
868     switch (Dim) {
869     case 0:
870       assert(hasWorkGroupIDX());
871       return ArgInfo.WorkGroupIDX.getRegister();
872     case 1:
873       assert(hasWorkGroupIDY());
874       return ArgInfo.WorkGroupIDY.getRegister();
875     case 2:
876       assert(hasWorkGroupIDZ());
877       return ArgInfo.WorkGroupIDZ.getRegister();
878     }
879     llvm_unreachable("unexpected dimension");
880   }
881 
882   unsigned getLDSWaveSpillSize() const {
883     return LDSWaveSpillSize;
884   }
885 
886   const AMDGPUBufferPseudoSourceValue *getBufferPSV(const SIInstrInfo &TII,
887                                                     const Value *BufferRsrc) {
888     assert(BufferRsrc);
889     auto PSV = BufferPSVs.try_emplace(
890       BufferRsrc,
891       std::make_unique<AMDGPUBufferPseudoSourceValue>(TII));
892     return PSV.first->second.get();
893   }
894 
895   const AMDGPUImagePseudoSourceValue *getImagePSV(const SIInstrInfo &TII,
896                                                   const Value *ImgRsrc) {
897     assert(ImgRsrc);
898     auto PSV = ImagePSVs.try_emplace(
899       ImgRsrc,
900       std::make_unique<AMDGPUImagePseudoSourceValue>(TII));
901     return PSV.first->second.get();
902   }
903 
904   const AMDGPUGWSResourcePseudoSourceValue *getGWSPSV(const SIInstrInfo &TII) {
905     if (!GWSResourcePSV) {
906       GWSResourcePSV =
907           std::make_unique<AMDGPUGWSResourcePseudoSourceValue>(TII);
908     }
909 
910     return GWSResourcePSV.get();
911   }
912 
913   unsigned getOccupancy() const {
914     return Occupancy;
915   }
916 
917   unsigned getMinAllowedOccupancy() const {
918     if (!isMemoryBound() && !needsWaveLimiter())
919       return Occupancy;
920     return (Occupancy < 4) ? Occupancy : 4;
921   }
922 
923   void limitOccupancy(const MachineFunction &MF);
924 
925   void limitOccupancy(unsigned Limit) {
926     if (Occupancy > Limit)
927       Occupancy = Limit;
928   }
929 
930   void increaseOccupancy(const MachineFunction &MF, unsigned Limit) {
931     if (Occupancy < Limit)
932       Occupancy = Limit;
933     limitOccupancy(MF);
934   }
935 };
936 
937 } // end namespace llvm
938 
939 #endif // LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H
940