xref: /llvm-project/llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.h (revision bc7e099aa82d44b5682ec3dbd1322ccc5000a50d)
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 "GCNSubtarget.h"
20 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
21 #include "SIInstrInfo.h"
22 #include "SIModeRegisterDefaults.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/CodeGen/MIRYamlMapping.h"
26 #include "llvm/CodeGen/PseudoSourceValue.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <optional>
29 
30 namespace llvm {
31 
32 class MachineFrameInfo;
33 class MachineFunction;
34 class SIMachineFunctionInfo;
35 class SIRegisterInfo;
36 class TargetRegisterClass;
37 
38 class AMDGPUPseudoSourceValue : public PseudoSourceValue {
39 public:
40   enum AMDGPUPSVKind : unsigned {
41     PSVImage = PseudoSourceValue::TargetCustom,
42     GWSResource
43   };
44 
45 protected:
46   AMDGPUPseudoSourceValue(unsigned Kind, const AMDGPUTargetMachine &TM)
47       : PseudoSourceValue(Kind, TM) {}
48 
49 public:
50   bool isConstant(const MachineFrameInfo *) const override {
51     // This should probably be true for most images, but we will start by being
52     // conservative.
53     return false;
54   }
55 
56   bool isAliased(const MachineFrameInfo *) const override {
57     return true;
58   }
59 
60   bool mayAlias(const MachineFrameInfo *) const override {
61     return true;
62   }
63 };
64 
65 class AMDGPUGWSResourcePseudoSourceValue final : public AMDGPUPseudoSourceValue {
66 public:
67   explicit AMDGPUGWSResourcePseudoSourceValue(const AMDGPUTargetMachine &TM)
68       : AMDGPUPseudoSourceValue(GWSResource, TM) {}
69 
70   static bool classof(const PseudoSourceValue *V) {
71     return V->kind() == GWSResource;
72   }
73 
74   // These are inaccessible memory from IR.
75   bool isAliased(const MachineFrameInfo *) const override {
76     return false;
77   }
78 
79   // These are inaccessible memory from IR.
80   bool mayAlias(const MachineFrameInfo *) const override {
81     return false;
82   }
83 
84   void printCustom(raw_ostream &OS) const override {
85     OS << "GWSResource";
86   }
87 };
88 
89 namespace yaml {
90 
91 struct SIArgument {
92   bool IsRegister;
93   union {
94     StringValue RegisterName;
95     unsigned StackOffset;
96   };
97   std::optional<unsigned> Mask;
98 
99   // Default constructor, which creates a stack argument.
100   SIArgument() : IsRegister(false), StackOffset(0) {}
101   SIArgument(const SIArgument &Other) {
102     IsRegister = Other.IsRegister;
103     if (IsRegister)
104       new (&RegisterName) StringValue(Other.RegisterName);
105     else
106       StackOffset = Other.StackOffset;
107     Mask = Other.Mask;
108   }
109   SIArgument &operator=(const SIArgument &Other) {
110     // Default-construct or destruct the old RegisterName in case of switching
111     // union members
112     if (IsRegister != Other.IsRegister) {
113       if (Other.IsRegister)
114         new (&RegisterName) StringValue();
115       else
116         RegisterName.~StringValue();
117     }
118     IsRegister = Other.IsRegister;
119     if (IsRegister)
120       RegisterName = Other.RegisterName;
121     else
122       StackOffset = Other.StackOffset;
123     Mask = Other.Mask;
124     return *this;
125   }
126   ~SIArgument() {
127     if (IsRegister)
128       RegisterName.~StringValue();
129   }
130 
131   // Helper to create a register or stack argument.
132   static inline SIArgument createArgument(bool IsReg) {
133     if (IsReg)
134       return SIArgument(IsReg);
135     return SIArgument();
136   }
137 
138 private:
139   // Construct a register argument.
140   SIArgument(bool) : IsRegister(true), RegisterName() {}
141 };
142 
143 template <> struct MappingTraits<SIArgument> {
144   static void mapping(IO &YamlIO, SIArgument &A) {
145     if (YamlIO.outputting()) {
146       if (A.IsRegister)
147         YamlIO.mapRequired("reg", A.RegisterName);
148       else
149         YamlIO.mapRequired("offset", A.StackOffset);
150     } else {
151       auto Keys = YamlIO.keys();
152       if (is_contained(Keys, "reg")) {
153         A = SIArgument::createArgument(true);
154         YamlIO.mapRequired("reg", A.RegisterName);
155       } else if (is_contained(Keys, "offset"))
156         YamlIO.mapRequired("offset", A.StackOffset);
157       else
158         YamlIO.setError("missing required key 'reg' or 'offset'");
159     }
160     YamlIO.mapOptional("mask", A.Mask);
161   }
162   static const bool flow = true;
163 };
164 
165 struct SIArgumentInfo {
166   std::optional<SIArgument> PrivateSegmentBuffer;
167   std::optional<SIArgument> DispatchPtr;
168   std::optional<SIArgument> QueuePtr;
169   std::optional<SIArgument> KernargSegmentPtr;
170   std::optional<SIArgument> DispatchID;
171   std::optional<SIArgument> FlatScratchInit;
172   std::optional<SIArgument> PrivateSegmentSize;
173 
174   std::optional<SIArgument> WorkGroupIDX;
175   std::optional<SIArgument> WorkGroupIDY;
176   std::optional<SIArgument> WorkGroupIDZ;
177   std::optional<SIArgument> WorkGroupInfo;
178   std::optional<SIArgument> LDSKernelId;
179   std::optional<SIArgument> PrivateSegmentWaveByteOffset;
180 
181   std::optional<SIArgument> ImplicitArgPtr;
182   std::optional<SIArgument> ImplicitBufferPtr;
183 
184   std::optional<SIArgument> WorkItemIDX;
185   std::optional<SIArgument> WorkItemIDY;
186   std::optional<SIArgument> WorkItemIDZ;
187 };
188 
189 template <> struct MappingTraits<SIArgumentInfo> {
190   static void mapping(IO &YamlIO, SIArgumentInfo &AI) {
191     YamlIO.mapOptional("privateSegmentBuffer", AI.PrivateSegmentBuffer);
192     YamlIO.mapOptional("dispatchPtr", AI.DispatchPtr);
193     YamlIO.mapOptional("queuePtr", AI.QueuePtr);
194     YamlIO.mapOptional("kernargSegmentPtr", AI.KernargSegmentPtr);
195     YamlIO.mapOptional("dispatchID", AI.DispatchID);
196     YamlIO.mapOptional("flatScratchInit", AI.FlatScratchInit);
197     YamlIO.mapOptional("privateSegmentSize", AI.PrivateSegmentSize);
198 
199     YamlIO.mapOptional("workGroupIDX", AI.WorkGroupIDX);
200     YamlIO.mapOptional("workGroupIDY", AI.WorkGroupIDY);
201     YamlIO.mapOptional("workGroupIDZ", AI.WorkGroupIDZ);
202     YamlIO.mapOptional("workGroupInfo", AI.WorkGroupInfo);
203     YamlIO.mapOptional("LDSKernelId", AI.LDSKernelId);
204     YamlIO.mapOptional("privateSegmentWaveByteOffset",
205                        AI.PrivateSegmentWaveByteOffset);
206 
207     YamlIO.mapOptional("implicitArgPtr", AI.ImplicitArgPtr);
208     YamlIO.mapOptional("implicitBufferPtr", AI.ImplicitBufferPtr);
209 
210     YamlIO.mapOptional("workItemIDX", AI.WorkItemIDX);
211     YamlIO.mapOptional("workItemIDY", AI.WorkItemIDY);
212     YamlIO.mapOptional("workItemIDZ", AI.WorkItemIDZ);
213   }
214 };
215 
216 // Default to default mode for default calling convention.
217 struct SIMode {
218   bool IEEE = true;
219   bool DX10Clamp = true;
220   bool FP32InputDenormals = true;
221   bool FP32OutputDenormals = true;
222   bool FP64FP16InputDenormals = true;
223   bool FP64FP16OutputDenormals = true;
224 
225   SIMode() = default;
226 
227   SIMode(const SIModeRegisterDefaults &Mode) {
228     IEEE = Mode.IEEE;
229     DX10Clamp = Mode.DX10Clamp;
230     FP32InputDenormals = Mode.FP32Denormals.Input != DenormalMode::PreserveSign;
231     FP32OutputDenormals =
232         Mode.FP32Denormals.Output != DenormalMode::PreserveSign;
233     FP64FP16InputDenormals =
234         Mode.FP64FP16Denormals.Input != DenormalMode::PreserveSign;
235     FP64FP16OutputDenormals =
236         Mode.FP64FP16Denormals.Output != DenormalMode::PreserveSign;
237   }
238 
239   bool operator ==(const SIMode Other) const {
240     return IEEE == Other.IEEE &&
241            DX10Clamp == Other.DX10Clamp &&
242            FP32InputDenormals == Other.FP32InputDenormals &&
243            FP32OutputDenormals == Other.FP32OutputDenormals &&
244            FP64FP16InputDenormals == Other.FP64FP16InputDenormals &&
245            FP64FP16OutputDenormals == Other.FP64FP16OutputDenormals;
246   }
247 };
248 
249 template <> struct MappingTraits<SIMode> {
250   static void mapping(IO &YamlIO, SIMode &Mode) {
251     YamlIO.mapOptional("ieee", Mode.IEEE, true);
252     YamlIO.mapOptional("dx10-clamp", Mode.DX10Clamp, true);
253     YamlIO.mapOptional("fp32-input-denormals", Mode.FP32InputDenormals, true);
254     YamlIO.mapOptional("fp32-output-denormals", Mode.FP32OutputDenormals, true);
255     YamlIO.mapOptional("fp64-fp16-input-denormals", Mode.FP64FP16InputDenormals, true);
256     YamlIO.mapOptional("fp64-fp16-output-denormals", Mode.FP64FP16OutputDenormals, true);
257   }
258 };
259 
260 struct SIMachineFunctionInfo final : public yaml::MachineFunctionInfo {
261   uint64_t ExplicitKernArgSize = 0;
262   Align MaxKernArgAlign;
263   uint32_t LDSSize = 0;
264   uint32_t GDSSize = 0;
265   Align DynLDSAlign;
266   bool IsEntryFunction = false;
267   bool IsChainFunction = false;
268   bool NoSignedZerosFPMath = false;
269   bool MemoryBound = false;
270   bool WaveLimiter = false;
271   bool HasSpilledSGPRs = false;
272   bool HasSpilledVGPRs = false;
273   uint32_t HighBitsOf32BitAddress = 0;
274 
275   // TODO: 10 may be a better default since it's the maximum.
276   unsigned Occupancy = 0;
277 
278   SmallVector<StringValue, 2> SpillPhysVGPRS;
279   SmallVector<StringValue> WWMReservedRegs;
280 
281   StringValue ScratchRSrcReg = "$private_rsrc_reg";
282   StringValue FrameOffsetReg = "$fp_reg";
283   StringValue StackPtrOffsetReg = "$sp_reg";
284 
285   unsigned BytesInStackArgArea = 0;
286   bool ReturnsVoid = true;
287 
288   std::optional<SIArgumentInfo> ArgInfo;
289 
290   unsigned PSInputAddr = 0;
291   unsigned PSInputEnable = 0;
292 
293   SIMode Mode;
294   std::optional<FrameIndex> ScavengeFI;
295   StringValue VGPRForAGPRCopy;
296   StringValue SGPRForEXECCopy;
297   StringValue LongBranchReservedReg;
298 
299   bool HasInitWholeWave = false;
300 
301   SIMachineFunctionInfo() = default;
302   SIMachineFunctionInfo(const llvm::SIMachineFunctionInfo &,
303                         const TargetRegisterInfo &TRI,
304                         const llvm::MachineFunction &MF);
305 
306   void mappingImpl(yaml::IO &YamlIO) override;
307   ~SIMachineFunctionInfo() = default;
308 };
309 
310 template <> struct MappingTraits<SIMachineFunctionInfo> {
311   static void mapping(IO &YamlIO, SIMachineFunctionInfo &MFI) {
312     YamlIO.mapOptional("explicitKernArgSize", MFI.ExplicitKernArgSize,
313                        UINT64_C(0));
314     YamlIO.mapOptional("maxKernArgAlign", MFI.MaxKernArgAlign);
315     YamlIO.mapOptional("ldsSize", MFI.LDSSize, 0u);
316     YamlIO.mapOptional("gdsSize", MFI.GDSSize, 0u);
317     YamlIO.mapOptional("dynLDSAlign", MFI.DynLDSAlign, Align());
318     YamlIO.mapOptional("isEntryFunction", MFI.IsEntryFunction, false);
319     YamlIO.mapOptional("isChainFunction", MFI.IsChainFunction, 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("psInputAddr", MFI.PSInputAddr, 0u);
335     YamlIO.mapOptional("psInputEnable", MFI.PSInputEnable, 0u);
336     YamlIO.mapOptional("mode", MFI.Mode, SIMode());
337     YamlIO.mapOptional("highBitsOf32BitAddress",
338                        MFI.HighBitsOf32BitAddress, 0u);
339     YamlIO.mapOptional("occupancy", MFI.Occupancy, 0);
340     YamlIO.mapOptional("spillPhysVGPRs", MFI.SpillPhysVGPRS);
341     YamlIO.mapOptional("wwmReservedRegs", MFI.WWMReservedRegs);
342     YamlIO.mapOptional("scavengeFI", MFI.ScavengeFI);
343     YamlIO.mapOptional("vgprForAGPRCopy", MFI.VGPRForAGPRCopy,
344                        StringValue()); // Don't print out when it's empty.
345     YamlIO.mapOptional("sgprForEXECCopy", MFI.SGPRForEXECCopy,
346                        StringValue()); // Don't print out when it's empty.
347     YamlIO.mapOptional("longBranchReservedReg", MFI.LongBranchReservedReg,
348                        StringValue());
349     YamlIO.mapOptional("hasInitWholeWave", MFI.HasInitWholeWave, false);
350   }
351 };
352 
353 } // end namespace yaml
354 
355 // A CSR SGPR value can be preserved inside a callee using one of the following
356 // methods.
357 //   1. Copy to an unused scratch SGPR.
358 //   2. Spill to a VGPR lane.
359 //   3. Spill to memory via. a scratch VGPR.
360 // class PrologEpilogSGPRSaveRestoreInfo represents the save/restore method used
361 // for an SGPR at function prolog/epilog.
362 enum class SGPRSaveKind : uint8_t {
363   COPY_TO_SCRATCH_SGPR,
364   SPILL_TO_VGPR_LANE,
365   SPILL_TO_MEM
366 };
367 
368 class PrologEpilogSGPRSaveRestoreInfo {
369   SGPRSaveKind Kind;
370   union {
371     int Index;
372     Register Reg;
373   };
374 
375 public:
376   PrologEpilogSGPRSaveRestoreInfo(SGPRSaveKind K, int I) : Kind(K), Index(I) {}
377   PrologEpilogSGPRSaveRestoreInfo(SGPRSaveKind K, Register R)
378       : Kind(K), Reg(R) {}
379   Register getReg() const { return Reg; }
380   int getIndex() const { return Index; }
381   SGPRSaveKind getKind() const { return Kind; }
382 };
383 
384 /// This class keeps track of the SPI_SP_INPUT_ADDR config register, which
385 /// tells the hardware which interpolation parameters to load.
386 class SIMachineFunctionInfo final : public AMDGPUMachineFunction,
387                                     private MachineRegisterInfo::Delegate {
388   friend class GCNTargetMachine;
389 
390   // State of MODE register, assumed FP mode.
391   SIModeRegisterDefaults Mode;
392 
393   // Registers that may be reserved for spilling purposes. These may be the same
394   // as the input registers.
395   Register ScratchRSrcReg = AMDGPU::PRIVATE_RSRC_REG;
396 
397   // This is the unswizzled offset from the current dispatch's scratch wave
398   // base to the beginning of the current function's frame.
399   Register FrameOffsetReg = AMDGPU::FP_REG;
400 
401   // This is an ABI register used in the non-entry calling convention to
402   // communicate the unswizzled offset from the current dispatch's scratch wave
403   // base to the beginning of the new function's frame.
404   Register StackPtrOffsetReg = AMDGPU::SP_REG;
405 
406   // Registers that may be reserved when RA doesn't allocate enough
407   // registers to plan for the case where an indirect branch ends up
408   // being needed during branch relaxation.
409   Register LongBranchReservedReg;
410 
411   AMDGPUFunctionArgInfo ArgInfo;
412 
413   // Graphics info.
414   unsigned PSInputAddr = 0;
415   unsigned PSInputEnable = 0;
416 
417   /// Number of bytes of arguments this function has on the stack. If the callee
418   /// is expected to restore the argument stack this should be a multiple of 16,
419   /// all usable during a tail call.
420   ///
421   /// The alternative would forbid tail call optimisation in some cases: if we
422   /// want to transfer control from a function with 8-bytes of stack-argument
423   /// space to a function with 16-bytes then misalignment of this value would
424   /// make a stack adjustment necessary, which could not be undone by the
425   /// callee.
426   unsigned BytesInStackArgArea = 0;
427 
428   bool ReturnsVoid = true;
429 
430   // A pair of default/requested minimum/maximum flat work group sizes.
431   // Minimum - first, maximum - second.
432   std::pair<unsigned, unsigned> FlatWorkGroupSizes = {0, 0};
433 
434   // A pair of default/requested minimum/maximum number of waves per execution
435   // unit. Minimum - first, maximum - second.
436   std::pair<unsigned, unsigned> WavesPerEU = {0, 0};
437 
438   const AMDGPUGWSResourcePseudoSourceValue GWSResourcePSV;
439 
440   // Default/requested number of work groups for the function.
441   SmallVector<unsigned> MaxNumWorkGroups = {0, 0, 0};
442 
443 private:
444   unsigned NumUserSGPRs = 0;
445   unsigned NumSystemSGPRs = 0;
446 
447   bool HasSpilledSGPRs = false;
448   bool HasSpilledVGPRs = false;
449   bool HasNonSpillStackObjects = false;
450   bool IsStackRealigned = false;
451 
452   unsigned NumSpilledSGPRs = 0;
453   unsigned NumSpilledVGPRs = 0;
454 
455   // Tracks information about user SGPRs that will be setup by hardware which
456   // will apply to all wavefronts of the grid.
457   GCNUserSGPRUsageInfo UserSGPRInfo;
458 
459   // Feature bits required for inputs passed in system SGPRs.
460   bool WorkGroupIDX : 1; // Always initialized.
461   bool WorkGroupIDY : 1;
462   bool WorkGroupIDZ : 1;
463   bool WorkGroupInfo : 1;
464   bool LDSKernelId : 1;
465   bool PrivateSegmentWaveByteOffset : 1;
466 
467   bool WorkItemIDX : 1; // Always initialized.
468   bool WorkItemIDY : 1;
469   bool WorkItemIDZ : 1;
470 
471   // Pointer to where the ABI inserts special kernel arguments separate from the
472   // user arguments. This is an offset from the KernargSegmentPtr.
473   bool ImplicitArgPtr : 1;
474 
475   bool MayNeedAGPRs : 1;
476 
477   // The hard-wired high half of the address of the global information table
478   // for AMDPAL OS type. 0xffffffff represents no hard-wired high half, since
479   // current hardware only allows a 16 bit value.
480   unsigned GITPtrHigh;
481 
482   unsigned HighBitsOf32BitAddress;
483 
484   // Flags associated with the virtual registers.
485   IndexedMap<uint8_t, VirtReg2IndexFunctor> VRegFlags;
486 
487   // Current recorded maximum possible occupancy.
488   unsigned Occupancy;
489 
490   mutable std::optional<bool> UsesAGPRs;
491 
492   MCPhysReg getNextUserSGPR() const;
493 
494   MCPhysReg getNextSystemSGPR() const;
495 
496   // MachineRegisterInfo callback functions to notify events.
497   void MRI_NoteNewVirtualRegister(Register Reg) override;
498   void MRI_NoteCloneVirtualRegister(Register NewReg, Register SrcReg) override;
499 
500 public:
501   struct VGPRSpillToAGPR {
502     SmallVector<MCPhysReg, 32> Lanes;
503     bool FullyAllocated = false;
504     bool IsDead = false;
505   };
506 
507 private:
508   // To track virtual VGPR + lane index for each subregister of the SGPR spilled
509   // to frameindex key during SILowerSGPRSpills pass.
510   DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>>
511       SGPRSpillsToVirtualVGPRLanes;
512   // To track physical VGPR + lane index for CSR SGPR spills and special SGPRs
513   // like Frame Pointer identified during PrologEpilogInserter.
514   DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>>
515       SGPRSpillsToPhysicalVGPRLanes;
516   unsigned NumVirtualVGPRSpillLanes = 0;
517   unsigned NumPhysicalVGPRSpillLanes = 0;
518   SmallVector<Register, 2> SpillVGPRs;
519   SmallVector<Register, 2> SpillPhysVGPRs;
520   using WWMSpillsMap = MapVector<Register, int>;
521   // To track the registers used in instructions that can potentially modify the
522   // inactive lanes. The WWM instructions and the writelane instructions for
523   // spilling SGPRs to VGPRs fall under such category of operations. The VGPRs
524   // modified by them should be spilled/restored at function prolog/epilog to
525   // avoid any undesired outcome. Each entry in this map holds a pair of values,
526   // the VGPR and its stack slot index.
527   WWMSpillsMap WWMSpills;
528 
529   // Before allocation, the VGPR registers are partitioned into two distinct
530   // sets, the first one for WWM-values and the second set for non-WWM values.
531   // The latter set should be reserved during WWM-regalloc.
532   BitVector NonWWMRegMask;
533 
534   using ReservedRegSet = SmallSetVector<Register, 8>;
535   // To track the VGPRs reserved for WWM instructions. They get stack slots
536   // later during PrologEpilogInserter and get added into the superset WWMSpills
537   // for actual spilling. A separate set makes the register reserved part and
538   // the serialization easier.
539   ReservedRegSet WWMReservedRegs;
540 
541   using PrologEpilogSGPRSpill =
542       std::pair<Register, PrologEpilogSGPRSaveRestoreInfo>;
543   // To track the SGPR spill method used for a CSR SGPR register during
544   // frame lowering. Even though the SGPR spills are handled during
545   // SILowerSGPRSpills pass, some special handling needed later during the
546   // PrologEpilogInserter.
547   SmallVector<PrologEpilogSGPRSpill, 3> PrologEpilogSGPRSpills;
548 
549   // To save/restore EXEC MASK around WWM spills and copies.
550   Register SGPRForEXECCopy;
551 
552   DenseMap<int, VGPRSpillToAGPR> VGPRToAGPRSpills;
553 
554   // AGPRs used for VGPR spills.
555   SmallVector<MCPhysReg, 32> SpillAGPR;
556 
557   // VGPRs used for AGPR spills.
558   SmallVector<MCPhysReg, 32> SpillVGPR;
559 
560   // Emergency stack slot. Sometimes, we create this before finalizing the stack
561   // frame, so save it here and add it to the RegScavenger later.
562   std::optional<int> ScavengeFI;
563 
564 private:
565   Register VGPRForAGPRCopy;
566 
567   bool allocateVirtualVGPRForSGPRSpills(MachineFunction &MF, int FI,
568                                         unsigned LaneIndex);
569   bool allocatePhysicalVGPRForSGPRSpills(MachineFunction &MF, int FI,
570                                          unsigned LaneIndex,
571                                          bool IsPrologEpilog);
572 
573 public:
574   Register getVGPRForAGPRCopy() const {
575     return VGPRForAGPRCopy;
576   }
577 
578   void setVGPRForAGPRCopy(Register NewVGPRForAGPRCopy) {
579     VGPRForAGPRCopy = NewVGPRForAGPRCopy;
580   }
581 
582   bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg) const;
583 
584 public:
585   SIMachineFunctionInfo(const SIMachineFunctionInfo &MFI) = default;
586   SIMachineFunctionInfo(const Function &F, const GCNSubtarget *STI);
587 
588   MachineFunctionInfo *
589   clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
590         const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
591       const override;
592 
593   bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI,
594                                 const MachineFunction &MF,
595                                 PerFunctionMIParsingState &PFS,
596                                 SMDiagnostic &Error, SMRange &SourceRange);
597 
598   void reserveWWMRegister(Register Reg) { WWMReservedRegs.insert(Reg); }
599 
600   void updateNonWWMRegMask(BitVector &RegMask) { NonWWMRegMask = RegMask; }
601   BitVector getNonWWMRegMask() const { return NonWWMRegMask; }
602   void clearNonWWMRegAllocMask() { NonWWMRegMask.clear(); }
603 
604   SIModeRegisterDefaults getMode() const { return Mode; }
605 
606   ArrayRef<SIRegisterInfo::SpilledReg>
607   getSGPRSpillToVirtualVGPRLanes(int FrameIndex) const {
608     auto I = SGPRSpillsToVirtualVGPRLanes.find(FrameIndex);
609     return (I == SGPRSpillsToVirtualVGPRLanes.end())
610                ? ArrayRef<SIRegisterInfo::SpilledReg>()
611                : ArrayRef(I->second);
612   }
613 
614   ArrayRef<Register> getSGPRSpillVGPRs() const { return SpillVGPRs; }
615   ArrayRef<Register> getSGPRSpillPhysVGPRs() const { return SpillPhysVGPRs; }
616 
617   const WWMSpillsMap &getWWMSpills() const { return WWMSpills; }
618   const ReservedRegSet &getWWMReservedRegs() const { return WWMReservedRegs; }
619 
620   ArrayRef<PrologEpilogSGPRSpill> getPrologEpilogSGPRSpills() const {
621     assert(is_sorted(PrologEpilogSGPRSpills, llvm::less_first()));
622     return PrologEpilogSGPRSpills;
623   }
624 
625   GCNUserSGPRUsageInfo &getUserSGPRInfo() { return UserSGPRInfo; }
626 
627   const GCNUserSGPRUsageInfo &getUserSGPRInfo() const { return UserSGPRInfo; }
628 
629   void addToPrologEpilogSGPRSpills(Register Reg,
630                                    PrologEpilogSGPRSaveRestoreInfo SI) {
631     assert(!hasPrologEpilogSGPRSpillEntry(Reg));
632 
633     // Insert a new entry in the right place to keep the vector in sorted order.
634     // This should be cheap since the vector is expected to be very short.
635     PrologEpilogSGPRSpills.insert(
636         upper_bound(
637             PrologEpilogSGPRSpills, Reg,
638             [](const auto &LHS, const auto &RHS) { return LHS < RHS.first; }),
639         std::make_pair(Reg, SI));
640   }
641 
642   // Check if an entry created for \p Reg in PrologEpilogSGPRSpills. Return true
643   // on success and false otherwise.
644   bool hasPrologEpilogSGPRSpillEntry(Register Reg) const {
645     const auto *I = find_if(PrologEpilogSGPRSpills, [&Reg](const auto &Spill) {
646       return Spill.first == Reg;
647     });
648     return I != PrologEpilogSGPRSpills.end();
649   }
650 
651   // Get the scratch SGPR if allocated to save/restore \p Reg.
652   Register getScratchSGPRCopyDstReg(Register Reg) const {
653     const auto *I = find_if(PrologEpilogSGPRSpills, [&Reg](const auto &Spill) {
654       return Spill.first == Reg;
655     });
656     if (I != PrologEpilogSGPRSpills.end() &&
657         I->second.getKind() == SGPRSaveKind::COPY_TO_SCRATCH_SGPR)
658       return I->second.getReg();
659 
660     return AMDGPU::NoRegister;
661   }
662 
663   // Get all scratch SGPRs allocated to copy/restore the SGPR spills.
664   void getAllScratchSGPRCopyDstRegs(SmallVectorImpl<Register> &Regs) const {
665     for (const auto &SI : PrologEpilogSGPRSpills) {
666       if (SI.second.getKind() == SGPRSaveKind::COPY_TO_SCRATCH_SGPR)
667         Regs.push_back(SI.second.getReg());
668     }
669   }
670 
671   // Check if \p FI is allocated for any SGPR spill to a VGPR lane during PEI.
672   bool checkIndexInPrologEpilogSGPRSpills(int FI) const {
673     return find_if(PrologEpilogSGPRSpills,
674                    [FI](const std::pair<Register,
675                                         PrologEpilogSGPRSaveRestoreInfo> &SI) {
676                      return SI.second.getKind() ==
677                                 SGPRSaveKind::SPILL_TO_VGPR_LANE &&
678                             SI.second.getIndex() == FI;
679                    }) != PrologEpilogSGPRSpills.end();
680   }
681 
682   const PrologEpilogSGPRSaveRestoreInfo &
683   getPrologEpilogSGPRSaveRestoreInfo(Register Reg) const {
684     const auto *I = find_if(PrologEpilogSGPRSpills, [&Reg](const auto &Spill) {
685       return Spill.first == Reg;
686     });
687     assert(I != PrologEpilogSGPRSpills.end());
688 
689     return I->second;
690   }
691 
692   ArrayRef<SIRegisterInfo::SpilledReg>
693   getSGPRSpillToPhysicalVGPRLanes(int FrameIndex) const {
694     auto I = SGPRSpillsToPhysicalVGPRLanes.find(FrameIndex);
695     return (I == SGPRSpillsToPhysicalVGPRLanes.end())
696                ? ArrayRef<SIRegisterInfo::SpilledReg>()
697                : ArrayRef(I->second);
698   }
699 
700   void setFlag(Register Reg, uint8_t Flag) {
701     assert(Reg.isVirtual());
702     if (VRegFlags.inBounds(Reg))
703       VRegFlags[Reg] |= Flag;
704   }
705 
706   bool checkFlag(Register Reg, uint8_t Flag) const {
707     if (Reg.isPhysical())
708       return false;
709 
710     return VRegFlags.inBounds(Reg) && VRegFlags[Reg] & Flag;
711   }
712 
713   bool hasVRegFlags() { return VRegFlags.size(); }
714 
715   void allocateWWMSpill(MachineFunction &MF, Register VGPR, uint64_t Size = 4,
716                         Align Alignment = Align(4));
717 
718   void splitWWMSpillRegisters(
719       MachineFunction &MF,
720       SmallVectorImpl<std::pair<Register, int>> &CalleeSavedRegs,
721       SmallVectorImpl<std::pair<Register, int>> &ScratchRegs) const;
722 
723   ArrayRef<MCPhysReg> getAGPRSpillVGPRs() const {
724     return SpillAGPR;
725   }
726 
727   Register getSGPRForEXECCopy() const { return SGPRForEXECCopy; }
728 
729   void setSGPRForEXECCopy(Register Reg) { SGPRForEXECCopy = Reg; }
730 
731   ArrayRef<MCPhysReg> getVGPRSpillAGPRs() const {
732     return SpillVGPR;
733   }
734 
735   MCPhysReg getVGPRToAGPRSpill(int FrameIndex, unsigned Lane) const {
736     auto I = VGPRToAGPRSpills.find(FrameIndex);
737     return (I == VGPRToAGPRSpills.end()) ? (MCPhysReg)AMDGPU::NoRegister
738                                          : I->second.Lanes[Lane];
739   }
740 
741   void setVGPRToAGPRSpillDead(int FrameIndex) {
742     auto I = VGPRToAGPRSpills.find(FrameIndex);
743     if (I != VGPRToAGPRSpills.end())
744       I->second.IsDead = true;
745   }
746 
747   // To bring the allocated WWM registers in \p WWMVGPRs to the lowest available
748   // range.
749   void shiftWwmVGPRsToLowestRange(MachineFunction &MF,
750                                   SmallVectorImpl<Register> &WWMVGPRs,
751                                   BitVector &SavedVGPRs);
752 
753   bool allocateSGPRSpillToVGPRLane(MachineFunction &MF, int FI,
754                                    bool SpillToPhysVGPRLane = false,
755                                    bool IsPrologEpilog = false);
756   bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR);
757 
758   /// If \p ResetSGPRSpillStackIDs is true, reset the stack ID from sgpr-spill
759   /// to the default stack.
760   bool removeDeadFrameIndices(MachineFrameInfo &MFI,
761                               bool ResetSGPRSpillStackIDs);
762 
763   int getScavengeFI(MachineFrameInfo &MFI, const SIRegisterInfo &TRI);
764   std::optional<int> getOptionalScavengeFI() const { return ScavengeFI; }
765 
766   unsigned getBytesInStackArgArea() const {
767     return BytesInStackArgArea;
768   }
769 
770   void setBytesInStackArgArea(unsigned Bytes) {
771     BytesInStackArgArea = Bytes;
772   }
773 
774   // Add user SGPRs.
775   Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI);
776   Register addDispatchPtr(const SIRegisterInfo &TRI);
777   Register addQueuePtr(const SIRegisterInfo &TRI);
778   Register addKernargSegmentPtr(const SIRegisterInfo &TRI);
779   Register addDispatchID(const SIRegisterInfo &TRI);
780   Register addFlatScratchInit(const SIRegisterInfo &TRI);
781   Register addPrivateSegmentSize(const SIRegisterInfo &TRI);
782   Register addImplicitBufferPtr(const SIRegisterInfo &TRI);
783   Register addLDSKernelId();
784   SmallVectorImpl<MCRegister> *
785   addPreloadedKernArg(const SIRegisterInfo &TRI, const TargetRegisterClass *RC,
786                       unsigned AllocSizeDWord, int KernArgIdx,
787                       int PaddingSGPRs);
788 
789   /// Increment user SGPRs used for padding the argument list only.
790   Register addReservedUserSGPR() {
791     Register Next = getNextUserSGPR();
792     ++NumUserSGPRs;
793     return Next;
794   }
795 
796   // Add system SGPRs.
797   Register addWorkGroupIDX() {
798     ArgInfo.WorkGroupIDX = ArgDescriptor::createRegister(getNextSystemSGPR());
799     NumSystemSGPRs += 1;
800     return ArgInfo.WorkGroupIDX.getRegister();
801   }
802 
803   Register addWorkGroupIDY() {
804     ArgInfo.WorkGroupIDY = ArgDescriptor::createRegister(getNextSystemSGPR());
805     NumSystemSGPRs += 1;
806     return ArgInfo.WorkGroupIDY.getRegister();
807   }
808 
809   Register addWorkGroupIDZ() {
810     ArgInfo.WorkGroupIDZ = ArgDescriptor::createRegister(getNextSystemSGPR());
811     NumSystemSGPRs += 1;
812     return ArgInfo.WorkGroupIDZ.getRegister();
813   }
814 
815   Register addWorkGroupInfo() {
816     ArgInfo.WorkGroupInfo = ArgDescriptor::createRegister(getNextSystemSGPR());
817     NumSystemSGPRs += 1;
818     return ArgInfo.WorkGroupInfo.getRegister();
819   }
820 
821   bool hasLDSKernelId() const { return LDSKernelId; }
822 
823   // Add special VGPR inputs
824   void setWorkItemIDX(ArgDescriptor Arg) {
825     ArgInfo.WorkItemIDX = Arg;
826   }
827 
828   void setWorkItemIDY(ArgDescriptor Arg) {
829     ArgInfo.WorkItemIDY = Arg;
830   }
831 
832   void setWorkItemIDZ(ArgDescriptor Arg) {
833     ArgInfo.WorkItemIDZ = Arg;
834   }
835 
836   Register addPrivateSegmentWaveByteOffset() {
837     ArgInfo.PrivateSegmentWaveByteOffset
838       = ArgDescriptor::createRegister(getNextSystemSGPR());
839     NumSystemSGPRs += 1;
840     return ArgInfo.PrivateSegmentWaveByteOffset.getRegister();
841   }
842 
843   void setPrivateSegmentWaveByteOffset(Register Reg) {
844     ArgInfo.PrivateSegmentWaveByteOffset = ArgDescriptor::createRegister(Reg);
845   }
846 
847   bool hasWorkGroupIDX() const {
848     return WorkGroupIDX;
849   }
850 
851   bool hasWorkGroupIDY() const {
852     return WorkGroupIDY;
853   }
854 
855   bool hasWorkGroupIDZ() const {
856     return WorkGroupIDZ;
857   }
858 
859   bool hasWorkGroupInfo() const {
860     return WorkGroupInfo;
861   }
862 
863   bool hasPrivateSegmentWaveByteOffset() const {
864     return PrivateSegmentWaveByteOffset;
865   }
866 
867   bool hasWorkItemIDX() const {
868     return WorkItemIDX;
869   }
870 
871   bool hasWorkItemIDY() const {
872     return WorkItemIDY;
873   }
874 
875   bool hasWorkItemIDZ() const {
876     return WorkItemIDZ;
877   }
878 
879   bool hasImplicitArgPtr() const {
880     return ImplicitArgPtr;
881   }
882 
883   AMDGPUFunctionArgInfo &getArgInfo() {
884     return ArgInfo;
885   }
886 
887   const AMDGPUFunctionArgInfo &getArgInfo() const {
888     return ArgInfo;
889   }
890 
891   std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT>
892   getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const {
893     return ArgInfo.getPreloadedValue(Value);
894   }
895 
896   MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const {
897     const auto *Arg = std::get<0>(ArgInfo.getPreloadedValue(Value));
898     return Arg ? Arg->getRegister() : MCRegister();
899   }
900 
901   unsigned getGITPtrHigh() const {
902     return GITPtrHigh;
903   }
904 
905   Register getGITPtrLoReg(const MachineFunction &MF) const;
906 
907   uint32_t get32BitAddressHighBits() const {
908     return HighBitsOf32BitAddress;
909   }
910 
911   unsigned getNumUserSGPRs() const {
912     return NumUserSGPRs;
913   }
914 
915   unsigned getNumPreloadedSGPRs() const {
916     return NumUserSGPRs + NumSystemSGPRs;
917   }
918 
919   unsigned getNumKernargPreloadedSGPRs() const {
920     return UserSGPRInfo.getNumKernargPreloadSGPRs();
921   }
922 
923   Register getPrivateSegmentWaveByteOffsetSystemSGPR() const {
924     return ArgInfo.PrivateSegmentWaveByteOffset.getRegister();
925   }
926 
927   /// Returns the physical register reserved for use as the resource
928   /// descriptor for scratch accesses.
929   Register getScratchRSrcReg() const {
930     return ScratchRSrcReg;
931   }
932 
933   void setScratchRSrcReg(Register Reg) {
934     assert(Reg != 0 && "Should never be unset");
935     ScratchRSrcReg = Reg;
936   }
937 
938   Register getFrameOffsetReg() const {
939     return FrameOffsetReg;
940   }
941 
942   void setFrameOffsetReg(Register Reg) {
943     assert(Reg != 0 && "Should never be unset");
944     FrameOffsetReg = Reg;
945   }
946 
947   void setStackPtrOffsetReg(Register Reg) {
948     assert(Reg != 0 && "Should never be unset");
949     StackPtrOffsetReg = Reg;
950   }
951 
952   void setLongBranchReservedReg(Register Reg) { LongBranchReservedReg = Reg; }
953 
954   // Note the unset value for this is AMDGPU::SP_REG rather than
955   // NoRegister. This is mostly a workaround for MIR tests where state that
956   // can't be directly computed from the function is not preserved in serialized
957   // MIR.
958   Register getStackPtrOffsetReg() const {
959     return StackPtrOffsetReg;
960   }
961 
962   Register getLongBranchReservedReg() const { return LongBranchReservedReg; }
963 
964   Register getQueuePtrUserSGPR() const {
965     return ArgInfo.QueuePtr.getRegister();
966   }
967 
968   Register getImplicitBufferPtrUserSGPR() const {
969     return ArgInfo.ImplicitBufferPtr.getRegister();
970   }
971 
972   bool hasSpilledSGPRs() const {
973     return HasSpilledSGPRs;
974   }
975 
976   void setHasSpilledSGPRs(bool Spill = true) {
977     HasSpilledSGPRs = Spill;
978   }
979 
980   bool hasSpilledVGPRs() const {
981     return HasSpilledVGPRs;
982   }
983 
984   void setHasSpilledVGPRs(bool Spill = true) {
985     HasSpilledVGPRs = Spill;
986   }
987 
988   bool hasNonSpillStackObjects() const {
989     return HasNonSpillStackObjects;
990   }
991 
992   void setHasNonSpillStackObjects(bool StackObject = true) {
993     HasNonSpillStackObjects = StackObject;
994   }
995 
996   bool isStackRealigned() const {
997     return IsStackRealigned;
998   }
999 
1000   void setIsStackRealigned(bool Realigned = true) {
1001     IsStackRealigned = Realigned;
1002   }
1003 
1004   unsigned getNumSpilledSGPRs() const {
1005     return NumSpilledSGPRs;
1006   }
1007 
1008   unsigned getNumSpilledVGPRs() const {
1009     return NumSpilledVGPRs;
1010   }
1011 
1012   void addToSpilledSGPRs(unsigned num) {
1013     NumSpilledSGPRs += num;
1014   }
1015 
1016   void addToSpilledVGPRs(unsigned num) {
1017     NumSpilledVGPRs += num;
1018   }
1019 
1020   unsigned getPSInputAddr() const {
1021     return PSInputAddr;
1022   }
1023 
1024   unsigned getPSInputEnable() const {
1025     return PSInputEnable;
1026   }
1027 
1028   bool isPSInputAllocated(unsigned Index) const {
1029     return PSInputAddr & (1 << Index);
1030   }
1031 
1032   void markPSInputAllocated(unsigned Index) {
1033     PSInputAddr |= 1 << Index;
1034   }
1035 
1036   void markPSInputEnabled(unsigned Index) {
1037     PSInputEnable |= 1 << Index;
1038   }
1039 
1040   bool returnsVoid() const {
1041     return ReturnsVoid;
1042   }
1043 
1044   void setIfReturnsVoid(bool Value) {
1045     ReturnsVoid = Value;
1046   }
1047 
1048   /// \returns A pair of default/requested minimum/maximum flat work group sizes
1049   /// for this function.
1050   std::pair<unsigned, unsigned> getFlatWorkGroupSizes() const {
1051     return FlatWorkGroupSizes;
1052   }
1053 
1054   /// \returns Default/requested minimum flat work group size for this function.
1055   unsigned getMinFlatWorkGroupSize() const {
1056     return FlatWorkGroupSizes.first;
1057   }
1058 
1059   /// \returns Default/requested maximum flat work group size for this function.
1060   unsigned getMaxFlatWorkGroupSize() const {
1061     return FlatWorkGroupSizes.second;
1062   }
1063 
1064   /// \returns A pair of default/requested minimum/maximum number of waves per
1065   /// execution unit.
1066   std::pair<unsigned, unsigned> getWavesPerEU() const {
1067     return WavesPerEU;
1068   }
1069 
1070   /// \returns Default/requested minimum number of waves per execution unit.
1071   unsigned getMinWavesPerEU() const {
1072     return WavesPerEU.first;
1073   }
1074 
1075   /// \returns Default/requested maximum number of waves per execution unit.
1076   unsigned getMaxWavesPerEU() const {
1077     return WavesPerEU.second;
1078   }
1079 
1080   const AMDGPUGWSResourcePseudoSourceValue *
1081   getGWSPSV(const AMDGPUTargetMachine &TM) {
1082     return &GWSResourcePSV;
1083   }
1084 
1085   unsigned getOccupancy() const {
1086     return Occupancy;
1087   }
1088 
1089   unsigned getMinAllowedOccupancy() const {
1090     if (!isMemoryBound() && !needsWaveLimiter())
1091       return Occupancy;
1092     return (Occupancy < 4) ? Occupancy : 4;
1093   }
1094 
1095   void limitOccupancy(const MachineFunction &MF);
1096 
1097   void limitOccupancy(unsigned Limit) {
1098     if (Occupancy > Limit)
1099       Occupancy = Limit;
1100   }
1101 
1102   void increaseOccupancy(const MachineFunction &MF, unsigned Limit) {
1103     if (Occupancy < Limit)
1104       Occupancy = Limit;
1105     limitOccupancy(MF);
1106   }
1107 
1108   bool mayNeedAGPRs() const {
1109     return MayNeedAGPRs;
1110   }
1111 
1112   // \returns true if a function has a use of AGPRs via inline asm or
1113   // has a call which may use it.
1114   bool mayUseAGPRs(const Function &F) const;
1115 
1116   // \returns true if a function needs or may need AGPRs.
1117   bool usesAGPRs(const MachineFunction &MF) const;
1118 
1119   /// \returns Default/requested number of work groups for this function.
1120   SmallVector<unsigned> getMaxNumWorkGroups() const { return MaxNumWorkGroups; }
1121 
1122   unsigned getMaxNumWorkGroupsX() const { return MaxNumWorkGroups[0]; }
1123   unsigned getMaxNumWorkGroupsY() const { return MaxNumWorkGroups[1]; }
1124   unsigned getMaxNumWorkGroupsZ() const { return MaxNumWorkGroups[2]; }
1125 };
1126 
1127 } // end namespace llvm
1128 
1129 #endif // LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H
1130