1 //===- AMDGPUTargetTransformInfo.h - AMDGPU specific TTI --------*- 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 /// This file a TargetTransformInfo::Concept conforming object specific to the
11 /// AMDGPU target machine. It uses the target's detailed information to
12 /// provide more precise answers to certain TTI queries, while letting the
13 /// target independent and default TTI implementations handle the rest.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUTARGETTRANSFORMINFO_H
18 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUTARGETTRANSFORMINFO_H
19 
20 #include "AMDGPU.h"
21 #include "AMDGPUSubtarget.h"
22 #include "AMDGPUTargetMachine.h"
23 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
24 #include "Utils/AMDGPUBaseInfo.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/CodeGen/BasicTTIImpl.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/MC/SubtargetFeature.h"
30 #include "llvm/Support/MathExtras.h"
31 #include <cassert>
32 
33 namespace llvm {
34 
35 class AMDGPUTargetLowering;
36 class Loop;
37 class ScalarEvolution;
38 class Type;
39 class Value;
40 
41 class AMDGPUTTIImpl final : public BasicTTIImplBase<AMDGPUTTIImpl> {
42   using BaseT = BasicTTIImplBase<AMDGPUTTIImpl>;
43   using TTI = TargetTransformInfo;
44 
45   friend BaseT;
46 
47   Triple TargetTriple;
48 
49   const GCNSubtarget *ST;
50   const TargetLoweringBase *TLI;
51 
52   const TargetSubtargetInfo *getST() const { return ST; }
53   const TargetLoweringBase *getTLI() const { return TLI; }
54 
55 public:
56   explicit AMDGPUTTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
57       : BaseT(TM, F.getParent()->getDataLayout()),
58         TargetTriple(TM->getTargetTriple()),
59         ST(static_cast<const GCNSubtarget *>(TM->getSubtargetImpl(F))),
60         TLI(ST->getTargetLowering()) {}
61 
62   void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
63                                TTI::UnrollingPreferences &UP);
64 };
65 
66 class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
67   using BaseT = BasicTTIImplBase<GCNTTIImpl>;
68   using TTI = TargetTransformInfo;
69 
70   friend BaseT;
71 
72   const GCNSubtarget *ST;
73   const SITargetLowering *TLI;
74   AMDGPUTTIImpl CommonTTI;
75   bool IsGraphicsShader;
76   bool HasFP32Denormals;
77 
78   const FeatureBitset InlineFeatureIgnoreList = {
79     // Codegen control options which don't matter.
80     AMDGPU::FeatureEnableLoadStoreOpt,
81     AMDGPU::FeatureEnableSIScheduler,
82     AMDGPU::FeatureEnableUnsafeDSOffsetFolding,
83     AMDGPU::FeatureFlatForGlobal,
84     AMDGPU::FeaturePromoteAlloca,
85     AMDGPU::FeatureUnalignedBufferAccess,
86     AMDGPU::FeatureUnalignedScratchAccess,
87 
88     AMDGPU::FeatureAutoWaitcntBeforeBarrier,
89 
90     // Property of the kernel/environment which can't actually differ.
91     AMDGPU::FeatureSGPRInitBug,
92     AMDGPU::FeatureXNACK,
93     AMDGPU::FeatureTrapHandler,
94     AMDGPU::FeatureCodeObjectV3,
95 
96     // The default assumption needs to be ecc is enabled, but no directly
97     // exposed operations depend on it, so it can be safely inlined.
98     AMDGPU::FeatureSRAMECC,
99 
100     // Perf-tuning features
101     AMDGPU::FeatureFastFMAF32,
102     AMDGPU::HalfRate64Ops
103   };
104 
105   const GCNSubtarget *getST() const { return ST; }
106   const AMDGPUTargetLowering *getTLI() const { return TLI; }
107 
108   static inline int getFullRateInstrCost() {
109     return TargetTransformInfo::TCC_Basic;
110   }
111 
112   static inline int getHalfRateInstrCost() {
113     return 2 * TargetTransformInfo::TCC_Basic;
114   }
115 
116   // TODO: The size is usually 8 bytes, but takes 4x as many cycles. Maybe
117   // should be 2 or 4.
118   static inline int getQuarterRateInstrCost() {
119     return 3 * TargetTransformInfo::TCC_Basic;
120   }
121 
122    // On some parts, normal fp64 operations are half rate, and others
123    // quarter. This also applies to some integer operations.
124   inline int get64BitInstrCost() const {
125     return ST->hasHalfRate64Ops() ?
126       getHalfRateInstrCost() : getQuarterRateInstrCost();
127   }
128 
129 public:
130   explicit GCNTTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
131     : BaseT(TM, F.getParent()->getDataLayout()),
132       ST(static_cast<const GCNSubtarget*>(TM->getSubtargetImpl(F))),
133       TLI(ST->getTargetLowering()),
134       CommonTTI(TM, F),
135       IsGraphicsShader(AMDGPU::isShader(F.getCallingConv())),
136       HasFP32Denormals(AMDGPU::SIModeRegisterDefaults(F).allFP32Denormals()) {}
137 
138   bool hasBranchDivergence() { return true; }
139   bool useGPUDivergenceAnalysis() const;
140 
141   void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
142                                TTI::UnrollingPreferences &UP);
143 
144   TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth) {
145     assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
146     return TTI::PSK_FastHardware;
147   }
148 
149   unsigned getHardwareNumberOfRegisters(bool Vector) const;
150   unsigned getNumberOfRegisters(bool Vector) const;
151   unsigned getRegisterBitWidth(bool Vector) const;
152   unsigned getMinVectorRegisterBitWidth() const;
153   unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
154                                unsigned ChainSizeInBytes,
155                                VectorType *VecTy) const;
156   unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
157                                 unsigned ChainSizeInBytes,
158                                 VectorType *VecTy) const;
159   unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const;
160 
161   bool isLegalToVectorizeMemChain(unsigned ChainSizeInBytes,
162                                   unsigned Alignment,
163                                   unsigned AddrSpace) const;
164   bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
165                                    unsigned Alignment,
166                                    unsigned AddrSpace) const;
167   bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
168                                     unsigned Alignment,
169                                     unsigned AddrSpace) const;
170   Type *getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
171                                   unsigned SrcAddrSpace, unsigned DestAddrSpace,
172                                   unsigned SrcAlign, unsigned DestAlign) const;
173 
174   void getMemcpyLoopResidualLoweringType(SmallVectorImpl<Type *> &OpsOut,
175                                          LLVMContext &Context,
176                                          unsigned RemainingBytes,
177                                          unsigned SrcAddrSpace,
178                                          unsigned DestAddrSpace,
179                                          unsigned SrcAlign,
180                                          unsigned DestAlign) const;
181   unsigned getMaxInterleaveFactor(unsigned VF);
182 
183   bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) const;
184 
185   int getArithmeticInstrCost(
186       unsigned Opcode, Type *Ty,
187       TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
188       TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
189       TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
190       TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None,
191       ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
192       const Instruction *CxtI = nullptr);
193 
194   unsigned getCFInstrCost(unsigned Opcode);
195 
196   bool isInlineAsmSourceOfDivergence(const CallInst *CI,
197                                      ArrayRef<unsigned> Indices = {}) const;
198 
199   int getVectorInstrCost(unsigned Opcode, Type *ValTy, unsigned Index);
200   bool isSourceOfDivergence(const Value *V) const;
201   bool isAlwaysUniform(const Value *V) const;
202 
203   unsigned getFlatAddressSpace() const {
204     // Don't bother running InferAddressSpaces pass on graphics shaders which
205     // don't use flat addressing.
206     if (IsGraphicsShader)
207       return -1;
208     return AMDGPUAS::FLAT_ADDRESS;
209   }
210 
211   bool collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
212                                   Intrinsic::ID IID) const;
213   bool rewriteIntrinsicWithAddressSpace(IntrinsicInst *II,
214                                         Value *OldV, Value *NewV) const;
215 
216   unsigned getVectorSplitCost() { return 0; }
217 
218   unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
219                           Type *SubTp);
220 
221   bool areInlineCompatible(const Function *Caller,
222                            const Function *Callee) const;
223 
224   unsigned getInliningThresholdMultiplier() { return 11; }
225 
226   int getInlinerVectorBonusPercent() { return 0; }
227 
228   int getArithmeticReductionCost(unsigned Opcode,
229                                  Type *Ty,
230                                  bool IsPairwise);
231   template <typename T>
232   int getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, ArrayRef<T *> Args,
233                             FastMathFlags FMF, unsigned VF,
234                             const Instruction *I = nullptr);
235   int getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
236                             ArrayRef<Type *> Tys, FastMathFlags FMF,
237                             unsigned ScalarizationCostPassed = UINT_MAX,
238                             const Instruction *I = nullptr);
239   int getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
240                             ArrayRef<Value *> Args, FastMathFlags FMF,
241                             unsigned VF = 1, const Instruction *I = nullptr);
242   int getMinMaxReductionCost(Type *Ty, Type *CondTy,
243                              bool IsPairwiseForm,
244                              bool IsUnsigned);
245   unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands);
246 };
247 
248 class R600TTIImpl final : public BasicTTIImplBase<R600TTIImpl> {
249   using BaseT = BasicTTIImplBase<R600TTIImpl>;
250   using TTI = TargetTransformInfo;
251 
252   friend BaseT;
253 
254   const R600Subtarget *ST;
255   const AMDGPUTargetLowering *TLI;
256   AMDGPUTTIImpl CommonTTI;
257 
258 public:
259   explicit R600TTIImpl(const AMDGPUTargetMachine *TM, const Function &F)
260     : BaseT(TM, F.getParent()->getDataLayout()),
261       ST(static_cast<const R600Subtarget*>(TM->getSubtargetImpl(F))),
262       TLI(ST->getTargetLowering()),
263       CommonTTI(TM, F)	{}
264 
265   const R600Subtarget *getST() const { return ST; }
266   const AMDGPUTargetLowering *getTLI() const { return TLI; }
267 
268   void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
269                                TTI::UnrollingPreferences &UP);
270   unsigned getHardwareNumberOfRegisters(bool Vec) const;
271   unsigned getNumberOfRegisters(bool Vec) const;
272   unsigned getRegisterBitWidth(bool Vector) const;
273   unsigned getMinVectorRegisterBitWidth() const;
274   unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const;
275   bool isLegalToVectorizeMemChain(unsigned ChainSizeInBytes, unsigned Alignment,
276                                   unsigned AddrSpace) const;
277   bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
278 		                   unsigned Alignment,
279                                    unsigned AddrSpace) const;
280   bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
281                                     unsigned Alignment,
282                                     unsigned AddrSpace) const;
283   unsigned getMaxInterleaveFactor(unsigned VF);
284   unsigned getCFInstrCost(unsigned Opcode);
285   int getVectorInstrCost(unsigned Opcode, Type *ValTy, unsigned Index);
286 };
287 
288 } // end namespace llvm
289 
290 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUTARGETTRANSFORMINFO_H
291