1 //=====-- AMDGPUSubtarget.h - Define Subtarget for AMDGPU ------*- 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 /// AMDGPU specific subclass of TargetSubtarget.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
15 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
16 
17 #include "AMDGPU.h"
18 #include "AMDGPUCallLowering.h"
19 #include "R600FrameLowering.h"
20 #include "R600ISelLowering.h"
21 #include "R600InstrInfo.h"
22 #include "SIFrameLowering.h"
23 #include "SIISelLowering.h"
24 #include "SIInstrInfo.h"
25 #include "Utils/AMDGPUBaseInfo.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
28 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
29 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
32 #include "llvm/MC/MCInstrItineraries.h"
33 #include "llvm/Support/MathExtras.h"
34 #include <cassert>
35 #include <cstdint>
36 #include <memory>
37 #include <utility>
38 
39 #define GET_SUBTARGETINFO_HEADER
40 #include "AMDGPUGenSubtargetInfo.inc"
41 #define GET_SUBTARGETINFO_HEADER
42 #include "R600GenSubtargetInfo.inc"
43 
44 namespace llvm {
45 
46 class StringRef;
47 
48 class AMDGPUSubtarget {
49 public:
50   enum Generation {
51     R600 = 0,
52     R700 = 1,
53     EVERGREEN = 2,
54     NORTHERN_ISLANDS = 3,
55     SOUTHERN_ISLANDS = 4,
56     SEA_ISLANDS = 5,
57     VOLCANIC_ISLANDS = 6,
58     GFX9 = 7,
59     GFX10 = 8
60   };
61 
62 private:
63   Triple TargetTriple;
64 
65 protected:
66   bool Has16BitInsts;
67   bool HasMadMixInsts;
68   bool FP32Denormals;
69   bool FPExceptions;
70   bool HasSDWA;
71   bool HasVOP3PInsts;
72   bool HasMulI24;
73   bool HasMulU24;
74   bool HasInv2PiInlineImm;
75   bool HasFminFmaxLegacy;
76   bool EnablePromoteAlloca;
77   bool HasTrigReducedRange;
78   unsigned MaxWavesPerEU;
79   int LocalMemorySize;
80   unsigned WavefrontSize;
81 
82 public:
83   AMDGPUSubtarget(const Triple &TT);
84 
85   static const AMDGPUSubtarget &get(const MachineFunction &MF);
86   static const AMDGPUSubtarget &get(const TargetMachine &TM,
87                                     const Function &F);
88 
89   /// \returns Default range flat work group size for a calling convention.
90   std::pair<unsigned, unsigned> getDefaultFlatWorkGroupSize(CallingConv::ID CC) const;
91 
92   /// \returns Subtarget's default pair of minimum/maximum flat work group sizes
93   /// for function \p F, or minimum/maximum flat work group sizes explicitly
94   /// requested using "amdgpu-flat-work-group-size" attribute attached to
95   /// function \p F.
96   ///
97   /// \returns Subtarget's default values if explicitly requested values cannot
98   /// be converted to integer, or violate subtarget's specifications.
99   std::pair<unsigned, unsigned> getFlatWorkGroupSizes(const Function &F) const;
100 
101   /// \returns Subtarget's default pair of minimum/maximum number of waves per
102   /// execution unit for function \p F, or minimum/maximum number of waves per
103   /// execution unit explicitly requested using "amdgpu-waves-per-eu" attribute
104   /// attached to function \p F.
105   ///
106   /// \returns Subtarget's default values if explicitly requested values cannot
107   /// be converted to integer, violate subtarget's specifications, or are not
108   /// compatible with minimum/maximum number of waves limited by flat work group
109   /// size, register usage, and/or lds usage.
110   std::pair<unsigned, unsigned> getWavesPerEU(const Function &F) const;
111 
112   /// Return the amount of LDS that can be used that will not restrict the
113   /// occupancy lower than WaveCount.
114   unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount,
115                                            const Function &) const;
116 
117   /// Inverse of getMaxLocalMemWithWaveCount. Return the maximum wavecount if
118   /// the given LDS memory size is the only constraint.
119   unsigned getOccupancyWithLocalMemSize(uint32_t Bytes, const Function &) const;
120 
121   unsigned getOccupancyWithLocalMemSize(const MachineFunction &MF) const;
122 
123   bool isAmdHsaOS() const {
124     return TargetTriple.getOS() == Triple::AMDHSA;
125   }
126 
127   bool isAmdPalOS() const {
128     return TargetTriple.getOS() == Triple::AMDPAL;
129   }
130 
131   bool isMesa3DOS() const {
132     return TargetTriple.getOS() == Triple::Mesa3D;
133   }
134 
135   bool isMesaKernel(const Function &F) const {
136     return isMesa3DOS() && !AMDGPU::isShader(F.getCallingConv());
137   }
138 
139   bool isAmdHsaOrMesa(const Function &F) const {
140     return isAmdHsaOS() || isMesaKernel(F);
141   }
142 
143   bool has16BitInsts() const {
144     return Has16BitInsts;
145   }
146 
147   bool hasMadMixInsts() const {
148     return HasMadMixInsts;
149   }
150 
151   bool hasFP32Denormals(const Function &F) const {
152     // FIXME: This should not be a property of the subtarget. This should be a
153     // property with a default set by the calling convention which can be
154     // overridden by attributes. For now, use the subtarget feature as a
155     // placeholder attribute. The function arguments only purpose is to
156     // discourage use without a function context until this is removed.
157     return FP32Denormals;
158   }
159 
160   bool hasFPExceptions() const {
161     return FPExceptions;
162   }
163 
164   bool hasSDWA() const {
165     return HasSDWA;
166   }
167 
168   bool hasVOP3PInsts() const {
169     return HasVOP3PInsts;
170   }
171 
172   bool hasMulI24() const {
173     return HasMulI24;
174   }
175 
176   bool hasMulU24() const {
177     return HasMulU24;
178   }
179 
180   bool hasInv2PiInlineImm() const {
181     return HasInv2PiInlineImm;
182   }
183 
184   bool hasFminFmaxLegacy() const {
185     return HasFminFmaxLegacy;
186   }
187 
188   bool hasTrigReducedRange() const {
189     return HasTrigReducedRange;
190   }
191 
192   bool isPromoteAllocaEnabled() const {
193     return EnablePromoteAlloca;
194   }
195 
196   unsigned getWavefrontSize() const {
197     return WavefrontSize;
198   }
199 
200   int getLocalMemorySize() const {
201     return LocalMemorySize;
202   }
203 
204   Align getAlignmentForImplicitArgPtr() const {
205     return isAmdHsaOS() ? Align(8) : Align(4);
206   }
207 
208   /// Returns the offset in bytes from the start of the input buffer
209   ///        of the first explicit kernel argument.
210   unsigned getExplicitKernelArgOffset(const Function &F) const {
211     return isAmdHsaOrMesa(F) ? 0 : 36;
212   }
213 
214   /// \returns Maximum number of work groups per compute unit supported by the
215   /// subtarget and limited by given \p FlatWorkGroupSize.
216   virtual unsigned getMaxWorkGroupsPerCU(unsigned FlatWorkGroupSize) const = 0;
217 
218   /// \returns Minimum flat work group size supported by the subtarget.
219   virtual unsigned getMinFlatWorkGroupSize() const = 0;
220 
221   /// \returns Maximum flat work group size supported by the subtarget.
222   virtual unsigned getMaxFlatWorkGroupSize() const = 0;
223 
224   /// \returns Maximum number of waves per execution unit supported by the
225   /// subtarget and limited by given \p FlatWorkGroupSize.
226   virtual unsigned getMaxWavesPerEU(unsigned FlatWorkGroupSize) const  = 0;
227 
228   /// \returns Minimum number of waves per execution unit supported by the
229   /// subtarget.
230   virtual unsigned getMinWavesPerEU() const = 0;
231 
232   /// \returns Maximum number of waves per execution unit supported by the
233   /// subtarget without any kind of limitation.
234   unsigned getMaxWavesPerEU() const { return MaxWavesPerEU; }
235 
236   /// Creates value range metadata on an workitemid.* inrinsic call or load.
237   bool makeLIDRangeMetadata(Instruction *I) const;
238 
239   /// \returns Number of bytes of arguments that are passed to a shader or
240   /// kernel in addition to the explicit ones declared for the function.
241   unsigned getImplicitArgNumBytes(const Function &F) const {
242     if (isMesaKernel(F))
243       return 16;
244     return AMDGPU::getIntegerAttribute(F, "amdgpu-implicitarg-num-bytes", 0);
245   }
246   uint64_t getExplicitKernArgSize(const Function &F, Align &MaxAlign) const;
247   unsigned getKernArgSegmentSize(const Function &F, Align &MaxAlign) const;
248 
249   virtual ~AMDGPUSubtarget() {}
250 };
251 
252 class GCNSubtarget : public AMDGPUGenSubtargetInfo,
253                      public AMDGPUSubtarget {
254 
255   using AMDGPUSubtarget::getMaxWavesPerEU;
256 
257 public:
258   enum TrapHandlerAbi {
259     TrapHandlerAbiNone = 0,
260     TrapHandlerAbiHsa = 1
261   };
262 
263   enum TrapID {
264     TrapIDHardwareReserved = 0,
265     TrapIDHSADebugTrap = 1,
266     TrapIDLLVMTrap = 2,
267     TrapIDLLVMDebugTrap = 3,
268     TrapIDDebugBreakpoint = 7,
269     TrapIDDebugReserved8 = 8,
270     TrapIDDebugReservedFE = 0xfe,
271     TrapIDDebugReservedFF = 0xff
272   };
273 
274   enum TrapRegValues {
275     LLVMTrapHandlerRegValue = 1
276   };
277 
278 private:
279   /// GlobalISel related APIs.
280   std::unique_ptr<AMDGPUCallLowering> CallLoweringInfo;
281   std::unique_ptr<InstructionSelector> InstSelector;
282   std::unique_ptr<LegalizerInfo> Legalizer;
283   std::unique_ptr<RegisterBankInfo> RegBankInfo;
284 
285 protected:
286   // Basic subtarget description.
287   Triple TargetTriple;
288   unsigned Gen;
289   InstrItineraryData InstrItins;
290   int LDSBankCount;
291   unsigned MaxPrivateElementSize;
292 
293   // Possibly statically set by tablegen, but may want to be overridden.
294   bool FastFMAF32;
295   bool HalfRate64Ops;
296 
297   // Dynamially set bits that enable features.
298   bool FP64FP16Denormals;
299   bool FlatForGlobal;
300   bool AutoWaitcntBeforeBarrier;
301   bool CodeObjectV3;
302   bool UnalignedScratchAccess;
303   bool UnalignedBufferAccess;
304   bool HasApertureRegs;
305   bool EnableXNACK;
306   bool DoesNotSupportXNACK;
307   bool EnableCuMode;
308   bool TrapHandler;
309 
310   // Used as options.
311   bool EnableLoadStoreOpt;
312   bool EnableUnsafeDSOffsetFolding;
313   bool EnableSIScheduler;
314   bool EnableDS128;
315   bool EnablePRTStrictNull;
316   bool DumpCode;
317 
318   // Subtarget statically properties set by tablegen
319   bool FP64;
320   bool FMA;
321   bool MIMG_R128;
322   bool IsGCN;
323   bool GCN3Encoding;
324   bool CIInsts;
325   bool GFX8Insts;
326   bool GFX9Insts;
327   bool GFX10Insts;
328   bool GFX7GFX8GFX9Insts;
329   bool SGPRInitBug;
330   bool HasSMemRealTime;
331   bool HasIntClamp;
332   bool HasFmaMixInsts;
333   bool HasMovrel;
334   bool HasVGPRIndexMode;
335   bool HasScalarStores;
336   bool HasScalarAtomics;
337   bool HasSDWAOmod;
338   bool HasSDWAScalar;
339   bool HasSDWASdst;
340   bool HasSDWAMac;
341   bool HasSDWAOutModsVOPC;
342   bool HasDPP;
343   bool HasDPP8;
344   bool HasR128A16;
345   bool HasGFX10A16;
346   bool HasNSAEncoding;
347   bool HasDLInsts;
348   bool HasDot1Insts;
349   bool HasDot2Insts;
350   bool HasDot3Insts;
351   bool HasDot4Insts;
352   bool HasDot5Insts;
353   bool HasDot6Insts;
354   bool HasMAIInsts;
355   bool HasPkFmacF16Inst;
356   bool HasAtomicFaddInsts;
357   bool EnableSRAMECC;
358   bool DoesNotSupportSRAMECC;
359   bool HasNoSdstCMPX;
360   bool HasVscnt;
361   bool HasRegisterBanking;
362   bool HasVOP3Literal;
363   bool HasNoDataDepHazard;
364   bool FlatAddressSpace;
365   bool FlatInstOffsets;
366   bool FlatGlobalInsts;
367   bool FlatScratchInsts;
368   bool ScalarFlatScratchInsts;
369   bool AddNoCarryInsts;
370   bool HasUnpackedD16VMem;
371   bool R600ALUInst;
372   bool CaymanISA;
373   bool CFALUBug;
374   bool LDSMisalignedBug;
375   bool HasMFMAInlineLiteralBug;
376   bool HasVertexCache;
377   short TexVTXClauseSize;
378   bool ScalarizeGlobal;
379 
380   bool HasVcmpxPermlaneHazard;
381   bool HasVMEMtoScalarWriteHazard;
382   bool HasSMEMtoVectorWriteHazard;
383   bool HasInstFwdPrefetchBug;
384   bool HasVcmpxExecWARHazard;
385   bool HasLdsBranchVmemWARHazard;
386   bool HasNSAtoVMEMBug;
387   bool HasOffset3fBug;
388   bool HasFlatSegmentOffsetBug;
389 
390   // Dummy feature to use for assembler in tablegen.
391   bool FeatureDisable;
392 
393   SelectionDAGTargetInfo TSInfo;
394 private:
395   SIInstrInfo InstrInfo;
396   SITargetLowering TLInfo;
397   SIFrameLowering FrameLowering;
398 
399   // See COMPUTE_TMPRING_SIZE.WAVESIZE, 13-bit field in units of 256-dword.
400   static const unsigned MaxWaveScratchSize = (256 * 4) * ((1 << 13) - 1);
401 
402 public:
403   GCNSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
404                const GCNTargetMachine &TM);
405   ~GCNSubtarget() override;
406 
407   GCNSubtarget &initializeSubtargetDependencies(const Triple &TT,
408                                                    StringRef GPU, StringRef FS);
409 
410   const SIInstrInfo *getInstrInfo() const override {
411     return &InstrInfo;
412   }
413 
414   const SIFrameLowering *getFrameLowering() const override {
415     return &FrameLowering;
416   }
417 
418   const SITargetLowering *getTargetLowering() const override {
419     return &TLInfo;
420   }
421 
422   const SIRegisterInfo *getRegisterInfo() const override {
423     return &InstrInfo.getRegisterInfo();
424   }
425 
426   const CallLowering *getCallLowering() const override {
427     return CallLoweringInfo.get();
428   }
429 
430   InstructionSelector *getInstructionSelector() const override {
431     return InstSelector.get();
432   }
433 
434   const LegalizerInfo *getLegalizerInfo() const override {
435     return Legalizer.get();
436   }
437 
438   const RegisterBankInfo *getRegBankInfo() const override {
439     return RegBankInfo.get();
440   }
441 
442   // Nothing implemented, just prevent crashes on use.
443   const SelectionDAGTargetInfo *getSelectionDAGInfo() const override {
444     return &TSInfo;
445   }
446 
447   const InstrItineraryData *getInstrItineraryData() const override {
448     return &InstrItins;
449   }
450 
451   void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
452 
453   Generation getGeneration() const {
454     return (Generation)Gen;
455   }
456 
457   unsigned getWavefrontSizeLog2() const {
458     return Log2_32(WavefrontSize);
459   }
460 
461   /// Return the number of high bits known to be zero fror a frame index.
462   unsigned getKnownHighZeroBitsForFrameIndex() const {
463     return countLeadingZeros(MaxWaveScratchSize) + getWavefrontSizeLog2();
464   }
465 
466   int getLDSBankCount() const {
467     return LDSBankCount;
468   }
469 
470   unsigned getMaxPrivateElementSize() const {
471     return MaxPrivateElementSize;
472   }
473 
474   unsigned getConstantBusLimit(unsigned Opcode) const;
475 
476   bool hasIntClamp() const {
477     return HasIntClamp;
478   }
479 
480   bool hasFP64() const {
481     return FP64;
482   }
483 
484   bool hasMIMG_R128() const {
485     return MIMG_R128;
486   }
487 
488   bool hasHWFP64() const {
489     return FP64;
490   }
491 
492   bool hasFastFMAF32() const {
493     return FastFMAF32;
494   }
495 
496   bool hasHalfRate64Ops() const {
497     return HalfRate64Ops;
498   }
499 
500   bool hasAddr64() const {
501     return (getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS);
502   }
503 
504   // Return true if the target only has the reverse operand versions of VALU
505   // shift instructions (e.g. v_lshrrev_b32, and no v_lshr_b32).
506   bool hasOnlyRevVALUShifts() const {
507     return getGeneration() >= VOLCANIC_ISLANDS;
508   }
509 
510   bool hasFractBug() const {
511     return getGeneration() == SOUTHERN_ISLANDS;
512   }
513 
514   bool hasBFE() const {
515     return true;
516   }
517 
518   bool hasBFI() const {
519     return true;
520   }
521 
522   bool hasBFM() const {
523     return hasBFE();
524   }
525 
526   bool hasBCNT(unsigned Size) const {
527     return true;
528   }
529 
530   bool hasFFBL() const {
531     return true;
532   }
533 
534   bool hasFFBH() const {
535     return true;
536   }
537 
538   bool hasMed3_16() const {
539     return getGeneration() >= AMDGPUSubtarget::GFX9;
540   }
541 
542   bool hasMin3Max3_16() const {
543     return getGeneration() >= AMDGPUSubtarget::GFX9;
544   }
545 
546   bool hasFmaMixInsts() const {
547     return HasFmaMixInsts;
548   }
549 
550   bool hasCARRY() const {
551     return true;
552   }
553 
554   bool hasFMA() const {
555     return FMA;
556   }
557 
558   bool hasSwap() const {
559     return GFX9Insts;
560   }
561 
562   bool hasScalarPackInsts() const {
563     return GFX9Insts;
564   }
565 
566   bool hasScalarMulHiInsts() const {
567     return GFX9Insts;
568   }
569 
570   TrapHandlerAbi getTrapHandlerAbi() const {
571     return isAmdHsaOS() ? TrapHandlerAbiHsa : TrapHandlerAbiNone;
572   }
573 
574   /// True if the offset field of DS instructions works as expected. On SI, the
575   /// offset uses a 16-bit adder and does not always wrap properly.
576   bool hasUsableDSOffset() const {
577     return getGeneration() >= SEA_ISLANDS;
578   }
579 
580   bool unsafeDSOffsetFoldingEnabled() const {
581     return EnableUnsafeDSOffsetFolding;
582   }
583 
584   /// Condition output from div_scale is usable.
585   bool hasUsableDivScaleConditionOutput() const {
586     return getGeneration() != SOUTHERN_ISLANDS;
587   }
588 
589   /// Extra wait hazard is needed in some cases before
590   /// s_cbranch_vccnz/s_cbranch_vccz.
591   bool hasReadVCCZBug() const {
592     return getGeneration() <= SEA_ISLANDS;
593   }
594 
595   /// Writes to VCC_LO/VCC_HI update the VCCZ flag.
596   bool partialVCCWritesUpdateVCCZ() const {
597     return getGeneration() >= GFX10;
598   }
599 
600   /// A read of an SGPR by SMRD instruction requires 4 wait states when the SGPR
601   /// was written by a VALU instruction.
602   bool hasSMRDReadVALUDefHazard() const {
603     return getGeneration() == SOUTHERN_ISLANDS;
604   }
605 
606   /// A read of an SGPR by a VMEM instruction requires 5 wait states when the
607   /// SGPR was written by a VALU Instruction.
608   bool hasVMEMReadSGPRVALUDefHazard() const {
609     return getGeneration() >= VOLCANIC_ISLANDS;
610   }
611 
612   bool hasRFEHazards() const {
613     return getGeneration() >= VOLCANIC_ISLANDS;
614   }
615 
616   /// Number of hazard wait states for s_setreg_b32/s_setreg_imm32_b32.
617   unsigned getSetRegWaitStates() const {
618     return getGeneration() <= SEA_ISLANDS ? 1 : 2;
619   }
620 
621   bool dumpCode() const {
622     return DumpCode;
623   }
624 
625   /// Return the amount of LDS that can be used that will not restrict the
626   /// occupancy lower than WaveCount.
627   unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount,
628                                            const Function &) const;
629 
630   /// Alias for hasFP64FP16Denormals
631   bool hasFP16Denormals(const Function &F) const {
632     return FP64FP16Denormals;
633   }
634 
635   /// Alias for hasFP64FP16Denormals
636   bool hasFP64Denormals(const Function &F) const {
637     return FP64FP16Denormals;
638   }
639 
640   bool hasFP64FP16Denormals(const Function &F) const {
641     return FP64FP16Denormals;
642   }
643 
644   bool supportsMinMaxDenormModes() const {
645     return getGeneration() >= AMDGPUSubtarget::GFX9;
646   }
647 
648   /// \returns If target supports S_DENORM_MODE.
649   bool hasDenormModeInst() const {
650     return getGeneration() >= AMDGPUSubtarget::GFX10;
651   }
652 
653   bool useFlatForGlobal() const {
654     return FlatForGlobal;
655   }
656 
657   /// \returns If target supports ds_read/write_b128 and user enables generation
658   /// of ds_read/write_b128.
659   bool useDS128() const {
660     return CIInsts && EnableDS128;
661   }
662 
663   /// Have v_trunc_f64, v_ceil_f64, v_rndne_f64
664   bool haveRoundOpsF64() const {
665     return CIInsts;
666   }
667 
668   /// \returns If MUBUF instructions always perform range checking, even for
669   /// buffer resources used for private memory access.
670   bool privateMemoryResourceIsRangeChecked() const {
671     return getGeneration() < AMDGPUSubtarget::GFX9;
672   }
673 
674   /// \returns If target requires PRT Struct NULL support (zero result registers
675   /// for sparse texture support).
676   bool usePRTStrictNull() const {
677     return EnablePRTStrictNull;
678   }
679 
680   bool hasAutoWaitcntBeforeBarrier() const {
681     return AutoWaitcntBeforeBarrier;
682   }
683 
684   bool hasCodeObjectV3() const {
685     // FIXME: Need to add code object v3 support for mesa and pal.
686     return isAmdHsaOS() ? CodeObjectV3 : false;
687   }
688 
689   bool hasUnalignedBufferAccess() const {
690     return UnalignedBufferAccess;
691   }
692 
693   bool hasUnalignedScratchAccess() const {
694     return UnalignedScratchAccess;
695   }
696 
697   bool hasApertureRegs() const {
698     return HasApertureRegs;
699   }
700 
701   bool isTrapHandlerEnabled() const {
702     return TrapHandler;
703   }
704 
705   bool isXNACKEnabled() const {
706     return EnableXNACK;
707   }
708 
709   bool isCuModeEnabled() const {
710     return EnableCuMode;
711   }
712 
713   bool hasFlatAddressSpace() const {
714     return FlatAddressSpace;
715   }
716 
717   bool hasFlatScrRegister() const {
718     return hasFlatAddressSpace();
719   }
720 
721   bool hasFlatInstOffsets() const {
722     return FlatInstOffsets;
723   }
724 
725   bool hasFlatGlobalInsts() const {
726     return FlatGlobalInsts;
727   }
728 
729   bool hasFlatScratchInsts() const {
730     return FlatScratchInsts;
731   }
732 
733   bool hasScalarFlatScratchInsts() const {
734     return ScalarFlatScratchInsts;
735   }
736 
737   bool hasMultiDwordFlatScratchAddressing() const {
738     return getGeneration() >= GFX9;
739   }
740 
741   bool hasFlatSegmentOffsetBug() const {
742     return HasFlatSegmentOffsetBug;
743   }
744 
745   bool hasFlatLgkmVMemCountInOrder() const {
746     return getGeneration() > GFX9;
747   }
748 
749   bool hasD16LoadStore() const {
750     return getGeneration() >= GFX9;
751   }
752 
753   bool d16PreservesUnusedBits() const {
754     return hasD16LoadStore() && !isSRAMECCEnabled();
755   }
756 
757   bool hasD16Images() const {
758     return getGeneration() >= VOLCANIC_ISLANDS;
759   }
760 
761   /// Return if most LDS instructions have an m0 use that require m0 to be
762   /// iniitalized.
763   bool ldsRequiresM0Init() const {
764     return getGeneration() < GFX9;
765   }
766 
767   // True if the hardware rewinds and replays GWS operations if a wave is
768   // preempted.
769   //
770   // If this is false, a GWS operation requires testing if a nack set the
771   // MEM_VIOL bit, and repeating if so.
772   bool hasGWSAutoReplay() const {
773     return getGeneration() >= GFX9;
774   }
775 
776   /// \returns if target has ds_gws_sema_release_all instruction.
777   bool hasGWSSemaReleaseAll() const {
778     return CIInsts;
779   }
780 
781   bool hasAddNoCarry() const {
782     return AddNoCarryInsts;
783   }
784 
785   bool hasUnpackedD16VMem() const {
786     return HasUnpackedD16VMem;
787   }
788 
789   // Covers VS/PS/CS graphics shaders
790   bool isMesaGfxShader(const Function &F) const {
791     return isMesa3DOS() && AMDGPU::isShader(F.getCallingConv());
792   }
793 
794   bool hasMad64_32() const {
795     return getGeneration() >= SEA_ISLANDS;
796   }
797 
798   bool hasSDWAOmod() const {
799     return HasSDWAOmod;
800   }
801 
802   bool hasSDWAScalar() const {
803     return HasSDWAScalar;
804   }
805 
806   bool hasSDWASdst() const {
807     return HasSDWASdst;
808   }
809 
810   bool hasSDWAMac() const {
811     return HasSDWAMac;
812   }
813 
814   bool hasSDWAOutModsVOPC() const {
815     return HasSDWAOutModsVOPC;
816   }
817 
818   bool hasDLInsts() const {
819     return HasDLInsts;
820   }
821 
822   bool hasDot1Insts() const {
823     return HasDot1Insts;
824   }
825 
826   bool hasDot2Insts() const {
827     return HasDot2Insts;
828   }
829 
830   bool hasDot3Insts() const {
831     return HasDot3Insts;
832   }
833 
834   bool hasDot4Insts() const {
835     return HasDot4Insts;
836   }
837 
838   bool hasDot5Insts() const {
839     return HasDot5Insts;
840   }
841 
842   bool hasDot6Insts() const {
843     return HasDot6Insts;
844   }
845 
846   bool hasMAIInsts() const {
847     return HasMAIInsts;
848   }
849 
850   bool hasPkFmacF16Inst() const {
851     return HasPkFmacF16Inst;
852   }
853 
854   bool hasAtomicFaddInsts() const {
855     return HasAtomicFaddInsts;
856   }
857 
858   bool isSRAMECCEnabled() const {
859     return EnableSRAMECC;
860   }
861 
862   bool hasNoSdstCMPX() const {
863     return HasNoSdstCMPX;
864   }
865 
866   bool hasVscnt() const {
867     return HasVscnt;
868   }
869 
870   bool hasRegisterBanking() const {
871     return HasRegisterBanking;
872   }
873 
874   bool hasVOP3Literal() const {
875     return HasVOP3Literal;
876   }
877 
878   bool hasNoDataDepHazard() const {
879     return HasNoDataDepHazard;
880   }
881 
882   bool vmemWriteNeedsExpWaitcnt() const {
883     return getGeneration() < SEA_ISLANDS;
884   }
885 
886   // Scratch is allocated in 256 dword per wave blocks for the entire
887   // wavefront. When viewed from the perspecive of an arbitrary workitem, this
888   // is 4-byte aligned.
889   //
890   // Only 4-byte alignment is really needed to access anything. Transformations
891   // on the pointer value itself may rely on the alignment / known low bits of
892   // the pointer. Set this to something above the minimum to avoid needing
893   // dynamic realignment in common cases.
894   Align getStackAlignment() const { return Align(16); }
895 
896   bool enableMachineScheduler() const override {
897     return true;
898   }
899 
900   bool enableSubRegLiveness() const override {
901     return true;
902   }
903 
904   void setScalarizeGlobalBehavior(bool b) { ScalarizeGlobal = b; }
905   bool getScalarizeGlobalBehavior() const { return ScalarizeGlobal; }
906 
907   /// \returns Number of execution units per compute unit supported by the
908   /// subtarget.
909   unsigned getEUsPerCU() const {
910     return AMDGPU::IsaInfo::getEUsPerCU(this);
911   }
912 
913   /// \returns Maximum number of waves per compute unit supported by the
914   /// subtarget without any kind of limitation.
915   unsigned getMaxWavesPerCU() const {
916     return AMDGPU::IsaInfo::getMaxWavesPerCU(this);
917   }
918 
919   /// \returns Maximum number of waves per compute unit supported by the
920   /// subtarget and limited by given \p FlatWorkGroupSize.
921   unsigned getMaxWavesPerCU(unsigned FlatWorkGroupSize) const {
922     return AMDGPU::IsaInfo::getMaxWavesPerCU(this, FlatWorkGroupSize);
923   }
924 
925   /// \returns Number of waves per work group supported by the subtarget and
926   /// limited by given \p FlatWorkGroupSize.
927   unsigned getWavesPerWorkGroup(unsigned FlatWorkGroupSize) const {
928     return AMDGPU::IsaInfo::getWavesPerWorkGroup(this, FlatWorkGroupSize);
929   }
930 
931   // static wrappers
932   static bool hasHalfRate64Ops(const TargetSubtargetInfo &STI);
933 
934   // XXX - Why is this here if it isn't in the default pass set?
935   bool enableEarlyIfConversion() const override {
936     return true;
937   }
938 
939   void overrideSchedPolicy(MachineSchedPolicy &Policy,
940                            unsigned NumRegionInstrs) const override;
941 
942   unsigned getMaxNumUserSGPRs() const {
943     return 16;
944   }
945 
946   bool hasSMemRealTime() const {
947     return HasSMemRealTime;
948   }
949 
950   bool hasMovrel() const {
951     return HasMovrel;
952   }
953 
954   bool hasVGPRIndexMode() const {
955     return HasVGPRIndexMode;
956   }
957 
958   bool useVGPRIndexMode() const;
959 
960   bool hasScalarCompareEq64() const {
961     return getGeneration() >= VOLCANIC_ISLANDS;
962   }
963 
964   bool hasScalarStores() const {
965     return HasScalarStores;
966   }
967 
968   bool hasScalarAtomics() const {
969     return HasScalarAtomics;
970   }
971 
972   bool hasLDSFPAtomics() const {
973     return GFX8Insts;
974   }
975 
976   bool hasDPP() const {
977     return HasDPP;
978   }
979 
980   bool hasDPPBroadcasts() const {
981     return HasDPP && getGeneration() < GFX10;
982   }
983 
984   bool hasDPPWavefrontShifts() const {
985     return HasDPP && getGeneration() < GFX10;
986   }
987 
988   bool hasDPP8() const {
989     return HasDPP8;
990   }
991 
992   bool hasR128A16() const {
993     return HasR128A16;
994   }
995 
996   bool hasGFX10A16() const {
997     return HasGFX10A16;
998   }
999 
1000   bool hasOffset3fBug() const {
1001     return HasOffset3fBug;
1002   }
1003 
1004   bool hasNSAEncoding() const {
1005     return HasNSAEncoding;
1006   }
1007 
1008   bool hasMadF16() const;
1009 
1010   bool enableSIScheduler() const {
1011     return EnableSIScheduler;
1012   }
1013 
1014   bool loadStoreOptEnabled() const {
1015     return EnableLoadStoreOpt;
1016   }
1017 
1018   bool hasSGPRInitBug() const {
1019     return SGPRInitBug;
1020   }
1021 
1022   bool hasMFMAInlineLiteralBug() const {
1023     return HasMFMAInlineLiteralBug;
1024   }
1025 
1026   bool has12DWordStoreHazard() const {
1027     return getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS;
1028   }
1029 
1030   // \returns true if the subtarget supports DWORDX3 load/store instructions.
1031   bool hasDwordx3LoadStores() const {
1032     return CIInsts;
1033   }
1034 
1035   bool hasSMovFedHazard() const {
1036     return getGeneration() == AMDGPUSubtarget::GFX9;
1037   }
1038 
1039   bool hasReadM0MovRelInterpHazard() const {
1040     return getGeneration() == AMDGPUSubtarget::GFX9;
1041   }
1042 
1043   bool hasReadM0SendMsgHazard() const {
1044     return getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
1045            getGeneration() <= AMDGPUSubtarget::GFX9;
1046   }
1047 
1048   bool hasVcmpxPermlaneHazard() const {
1049     return HasVcmpxPermlaneHazard;
1050   }
1051 
1052   bool hasVMEMtoScalarWriteHazard() const {
1053     return HasVMEMtoScalarWriteHazard;
1054   }
1055 
1056   bool hasSMEMtoVectorWriteHazard() const {
1057     return HasSMEMtoVectorWriteHazard;
1058   }
1059 
1060   bool hasLDSMisalignedBug() const {
1061     return LDSMisalignedBug && !EnableCuMode;
1062   }
1063 
1064   bool hasInstFwdPrefetchBug() const {
1065     return HasInstFwdPrefetchBug;
1066   }
1067 
1068   bool hasVcmpxExecWARHazard() const {
1069     return HasVcmpxExecWARHazard;
1070   }
1071 
1072   bool hasLdsBranchVmemWARHazard() const {
1073     return HasLdsBranchVmemWARHazard;
1074   }
1075 
1076   bool hasNSAtoVMEMBug() const {
1077     return HasNSAtoVMEMBug;
1078   }
1079 
1080   /// Return the maximum number of waves per SIMD for kernels using \p SGPRs
1081   /// SGPRs
1082   unsigned getOccupancyWithNumSGPRs(unsigned SGPRs) const;
1083 
1084   /// Return the maximum number of waves per SIMD for kernels using \p VGPRs
1085   /// VGPRs
1086   unsigned getOccupancyWithNumVGPRs(unsigned VGPRs) const;
1087 
1088   /// Return occupancy for the given function. Used LDS and a number of
1089   /// registers if provided.
1090   /// Note, occupancy can be affected by the scratch allocation as well, but
1091   /// we do not have enough information to compute it.
1092   unsigned computeOccupancy(const MachineFunction &MF, unsigned LDSSize = 0,
1093                             unsigned NumSGPRs = 0, unsigned NumVGPRs = 0) const;
1094 
1095   /// \returns true if the flat_scratch register should be initialized with the
1096   /// pointer to the wave's scratch memory rather than a size and offset.
1097   bool flatScratchIsPointer() const {
1098     return getGeneration() >= AMDGPUSubtarget::GFX9;
1099   }
1100 
1101   /// \returns true if the machine has merged shaders in which s0-s7 are
1102   /// reserved by the hardware and user SGPRs start at s8
1103   bool hasMergedShaders() const {
1104     return getGeneration() >= GFX9;
1105   }
1106 
1107   /// \returns SGPR allocation granularity supported by the subtarget.
1108   unsigned getSGPRAllocGranule() const {
1109     return AMDGPU::IsaInfo::getSGPRAllocGranule(this);
1110   }
1111 
1112   /// \returns SGPR encoding granularity supported by the subtarget.
1113   unsigned getSGPREncodingGranule() const {
1114     return AMDGPU::IsaInfo::getSGPREncodingGranule(this);
1115   }
1116 
1117   /// \returns Total number of SGPRs supported by the subtarget.
1118   unsigned getTotalNumSGPRs() const {
1119     return AMDGPU::IsaInfo::getTotalNumSGPRs(this);
1120   }
1121 
1122   /// \returns Addressable number of SGPRs supported by the subtarget.
1123   unsigned getAddressableNumSGPRs() const {
1124     return AMDGPU::IsaInfo::getAddressableNumSGPRs(this);
1125   }
1126 
1127   /// \returns Minimum number of SGPRs that meets the given number of waves per
1128   /// execution unit requirement supported by the subtarget.
1129   unsigned getMinNumSGPRs(unsigned WavesPerEU) const {
1130     return AMDGPU::IsaInfo::getMinNumSGPRs(this, WavesPerEU);
1131   }
1132 
1133   /// \returns Maximum number of SGPRs that meets the given number of waves per
1134   /// execution unit requirement supported by the subtarget.
1135   unsigned getMaxNumSGPRs(unsigned WavesPerEU, bool Addressable) const {
1136     return AMDGPU::IsaInfo::getMaxNumSGPRs(this, WavesPerEU, Addressable);
1137   }
1138 
1139   /// \returns Reserved number of SGPRs for given function \p MF.
1140   unsigned getReservedNumSGPRs(const MachineFunction &MF) const;
1141 
1142   /// \returns Maximum number of SGPRs that meets number of waves per execution
1143   /// unit requirement for function \p MF, or number of SGPRs explicitly
1144   /// requested using "amdgpu-num-sgpr" attribute attached to function \p MF.
1145   ///
1146   /// \returns Value that meets number of waves per execution unit requirement
1147   /// if explicitly requested value cannot be converted to integer, violates
1148   /// subtarget's specifications, or does not meet number of waves per execution
1149   /// unit requirement.
1150   unsigned getMaxNumSGPRs(const MachineFunction &MF) const;
1151 
1152   /// \returns VGPR allocation granularity supported by the subtarget.
1153   unsigned getVGPRAllocGranule() const {
1154     return AMDGPU::IsaInfo::getVGPRAllocGranule(this);
1155   }
1156 
1157   /// \returns VGPR encoding granularity supported by the subtarget.
1158   unsigned getVGPREncodingGranule() const {
1159     return AMDGPU::IsaInfo::getVGPREncodingGranule(this);
1160   }
1161 
1162   /// \returns Total number of VGPRs supported by the subtarget.
1163   unsigned getTotalNumVGPRs() const {
1164     return AMDGPU::IsaInfo::getTotalNumVGPRs(this);
1165   }
1166 
1167   /// \returns Addressable number of VGPRs supported by the subtarget.
1168   unsigned getAddressableNumVGPRs() const {
1169     return AMDGPU::IsaInfo::getAddressableNumVGPRs(this);
1170   }
1171 
1172   /// \returns Minimum number of VGPRs that meets given number of waves per
1173   /// execution unit requirement supported by the subtarget.
1174   unsigned getMinNumVGPRs(unsigned WavesPerEU) const {
1175     return AMDGPU::IsaInfo::getMinNumVGPRs(this, WavesPerEU);
1176   }
1177 
1178   /// \returns Maximum number of VGPRs that meets given number of waves per
1179   /// execution unit requirement supported by the subtarget.
1180   unsigned getMaxNumVGPRs(unsigned WavesPerEU) const {
1181     return AMDGPU::IsaInfo::getMaxNumVGPRs(this, WavesPerEU);
1182   }
1183 
1184   /// \returns Maximum number of VGPRs that meets number of waves per execution
1185   /// unit requirement for function \p MF, or number of VGPRs explicitly
1186   /// requested using "amdgpu-num-vgpr" attribute attached to function \p MF.
1187   ///
1188   /// \returns Value that meets number of waves per execution unit requirement
1189   /// if explicitly requested value cannot be converted to integer, violates
1190   /// subtarget's specifications, or does not meet number of waves per execution
1191   /// unit requirement.
1192   unsigned getMaxNumVGPRs(const MachineFunction &MF) const;
1193 
1194   void getPostRAMutations(
1195       std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations)
1196       const override;
1197 
1198   bool isWave32() const {
1199     return WavefrontSize == 32;
1200   }
1201 
1202   const TargetRegisterClass *getBoolRC() const {
1203     return getRegisterInfo()->getBoolRC();
1204   }
1205 
1206   /// \returns Maximum number of work groups per compute unit supported by the
1207   /// subtarget and limited by given \p FlatWorkGroupSize.
1208   unsigned getMaxWorkGroupsPerCU(unsigned FlatWorkGroupSize) const override {
1209     return AMDGPU::IsaInfo::getMaxWorkGroupsPerCU(this, FlatWorkGroupSize);
1210   }
1211 
1212   /// \returns Minimum flat work group size supported by the subtarget.
1213   unsigned getMinFlatWorkGroupSize() const override {
1214     return AMDGPU::IsaInfo::getMinFlatWorkGroupSize(this);
1215   }
1216 
1217   /// \returns Maximum flat work group size supported by the subtarget.
1218   unsigned getMaxFlatWorkGroupSize() const override {
1219     return AMDGPU::IsaInfo::getMaxFlatWorkGroupSize(this);
1220   }
1221 
1222   /// \returns Maximum number of waves per execution unit supported by the
1223   /// subtarget and limited by given \p FlatWorkGroupSize.
1224   unsigned getMaxWavesPerEU(unsigned FlatWorkGroupSize) const override {
1225     return AMDGPU::IsaInfo::getMaxWavesPerEU(this, FlatWorkGroupSize);
1226   }
1227 
1228   /// \returns Minimum number of waves per execution unit supported by the
1229   /// subtarget.
1230   unsigned getMinWavesPerEU() const override {
1231     return AMDGPU::IsaInfo::getMinWavesPerEU(this);
1232   }
1233 
1234   void adjustSchedDependency(SUnit *Src, SUnit *Dst, SDep &Dep) const override;
1235 };
1236 
1237 class R600Subtarget final : public R600GenSubtargetInfo,
1238                             public AMDGPUSubtarget {
1239 private:
1240   R600InstrInfo InstrInfo;
1241   R600FrameLowering FrameLowering;
1242   bool FMA;
1243   bool CaymanISA;
1244   bool CFALUBug;
1245   bool HasVertexCache;
1246   bool R600ALUInst;
1247   bool FP64;
1248   short TexVTXClauseSize;
1249   Generation Gen;
1250   R600TargetLowering TLInfo;
1251   InstrItineraryData InstrItins;
1252   SelectionDAGTargetInfo TSInfo;
1253 
1254 public:
1255   R600Subtarget(const Triple &TT, StringRef CPU, StringRef FS,
1256                 const TargetMachine &TM);
1257 
1258   const R600InstrInfo *getInstrInfo() const override { return &InstrInfo; }
1259 
1260   const R600FrameLowering *getFrameLowering() const override {
1261     return &FrameLowering;
1262   }
1263 
1264   const R600TargetLowering *getTargetLowering() const override {
1265     return &TLInfo;
1266   }
1267 
1268   const R600RegisterInfo *getRegisterInfo() const override {
1269     return &InstrInfo.getRegisterInfo();
1270   }
1271 
1272   const InstrItineraryData *getInstrItineraryData() const override {
1273     return &InstrItins;
1274   }
1275 
1276   // Nothing implemented, just prevent crashes on use.
1277   const SelectionDAGTargetInfo *getSelectionDAGInfo() const override {
1278     return &TSInfo;
1279   }
1280 
1281   void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
1282 
1283   Generation getGeneration() const {
1284     return Gen;
1285   }
1286 
1287   Align getStackAlignment() const { return Align(4); }
1288 
1289   R600Subtarget &initializeSubtargetDependencies(const Triple &TT,
1290                                                  StringRef GPU, StringRef FS);
1291 
1292   bool hasBFE() const {
1293     return (getGeneration() >= EVERGREEN);
1294   }
1295 
1296   bool hasBFI() const {
1297     return (getGeneration() >= EVERGREEN);
1298   }
1299 
1300   bool hasBCNT(unsigned Size) const {
1301     if (Size == 32)
1302       return (getGeneration() >= EVERGREEN);
1303 
1304     return false;
1305   }
1306 
1307   bool hasBORROW() const {
1308     return (getGeneration() >= EVERGREEN);
1309   }
1310 
1311   bool hasCARRY() const {
1312     return (getGeneration() >= EVERGREEN);
1313   }
1314 
1315   bool hasCaymanISA() const {
1316     return CaymanISA;
1317   }
1318 
1319   bool hasFFBL() const {
1320     return (getGeneration() >= EVERGREEN);
1321   }
1322 
1323   bool hasFFBH() const {
1324     return (getGeneration() >= EVERGREEN);
1325   }
1326 
1327   bool hasFMA() const { return FMA; }
1328 
1329   bool hasCFAluBug() const { return CFALUBug; }
1330 
1331   bool hasVertexCache() const { return HasVertexCache; }
1332 
1333   short getTexVTXClauseSize() const { return TexVTXClauseSize; }
1334 
1335   bool enableMachineScheduler() const override {
1336     return true;
1337   }
1338 
1339   bool enableSubRegLiveness() const override {
1340     return true;
1341   }
1342 
1343   /// \returns Maximum number of work groups per compute unit supported by the
1344   /// subtarget and limited by given \p FlatWorkGroupSize.
1345   unsigned getMaxWorkGroupsPerCU(unsigned FlatWorkGroupSize) const override {
1346     return AMDGPU::IsaInfo::getMaxWorkGroupsPerCU(this, FlatWorkGroupSize);
1347   }
1348 
1349   /// \returns Minimum flat work group size supported by the subtarget.
1350   unsigned getMinFlatWorkGroupSize() const override {
1351     return AMDGPU::IsaInfo::getMinFlatWorkGroupSize(this);
1352   }
1353 
1354   /// \returns Maximum flat work group size supported by the subtarget.
1355   unsigned getMaxFlatWorkGroupSize() const override {
1356     return AMDGPU::IsaInfo::getMaxFlatWorkGroupSize(this);
1357   }
1358 
1359   /// \returns Maximum number of waves per execution unit supported by the
1360   /// subtarget and limited by given \p FlatWorkGroupSize.
1361   unsigned getMaxWavesPerEU(unsigned FlatWorkGroupSize) const override {
1362     return AMDGPU::IsaInfo::getMaxWavesPerEU(this, FlatWorkGroupSize);
1363   }
1364 
1365   /// \returns Minimum number of waves per execution unit supported by the
1366   /// subtarget.
1367   unsigned getMinWavesPerEU() const override {
1368     return AMDGPU::IsaInfo::getMinWavesPerEU(this);
1369   }
1370 };
1371 
1372 } // end namespace llvm
1373 
1374 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
1375