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