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