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