AMDGPU/SI: Implement AMDGPUTargetTransformInfo::isSourceOfDivergence()
[oota-llvm.git] / lib / Target / AMDGPU / AMDGPUTargetTransformInfo.cpp
1 //===-- AMDGPUTargetTransformInfo.cpp - AMDGPU specific TTI pass ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // \file
11 // This file implements a TargetTransformInfo analysis pass specific to the
12 // AMDGPU target machine. It uses the target's detailed information to provide
13 // more precise answers to certain TTI queries, while letting the target
14 // independent and default TTI implementations handle the rest.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "AMDGPUTargetTransformInfo.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/CodeGen/BasicTTIImpl.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Target/CostTable.h"
26 #include "llvm/Target/TargetLowering.h"
27 using namespace llvm;
28
29 #define DEBUG_TYPE "AMDGPUtti"
30
31 void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L,
32                                             TTI::UnrollingPreferences &UP) {
33   UP.Threshold = 300; // Twice the default.
34   UP.MaxCount = UINT_MAX;
35   UP.Partial = true;
36
37   // TODO: Do we want runtime unrolling?
38
39   for (const BasicBlock *BB : L->getBlocks()) {
40     const DataLayout &DL = BB->getModule()->getDataLayout();
41     for (const Instruction &I : *BB) {
42       const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
43       if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
44         continue;
45
46       const Value *Ptr = GEP->getPointerOperand();
47       const AllocaInst *Alloca =
48           dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr, DL));
49       if (Alloca) {
50         // We want to do whatever we can to limit the number of alloca
51         // instructions that make it through to the code generator.  allocas
52         // require us to use indirect addressing, which is slow and prone to
53         // compiler bugs.  If this loop does an address calculation on an
54         // alloca ptr, then we want to use a higher than normal loop unroll
55         // threshold. This will give SROA a better chance to eliminate these
56         // allocas.
57         //
58         // Don't use the maximum allowed value here as it will make some
59         // programs way too big.
60         UP.Threshold = 800;
61       }
62     }
63   }
64 }
65
66 unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) {
67   if (Vec)
68     return 0;
69
70   // Number of VGPRs on SI.
71   if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
72     return 256;
73
74   return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
75 }
76
77 unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool) { return 32; }
78
79 unsigned AMDGPUTTIImpl::getMaxInterleaveFactor(unsigned VF) {
80   // Semi-arbitrary large amount.
81   return 64;
82 }
83
84 int AMDGPUTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
85                                       unsigned Index) {
86   switch (Opcode) {
87   case Instruction::ExtractElement:
88     // Dynamic indexing isn't free and is best avoided.
89     return Index == ~0u ? 2 : 0;
90   default:
91     return BaseT::getVectorInstrCost(Opcode, ValTy, Index);
92   }
93 }
94
95 static bool isIntrinsicSourceOfDivergence(const TargetIntrinsicInfo *TII,
96                                           const IntrinsicInst *I) {
97   switch (I->getIntrinsicID()) {
98   default:
99     return false;
100   case Intrinsic::not_intrinsic:
101     // This means we have an intrinsic that isn't defined in
102     // IntrinsicsAMDGPU.td
103     break;
104
105   case Intrinsic::amdgcn_interp_p1:
106   case Intrinsic::amdgcn_interp_p2:
107   case Intrinsic::amdgcn_mbcnt_hi:
108   case Intrinsic::amdgcn_mbcnt_lo:
109   case Intrinsic::r600_read_tidig_x:
110   case Intrinsic::r600_read_tidig_y:
111   case Intrinsic::r600_read_tidig_z:
112     return true;
113   }
114
115   StringRef Name = I->getCalledFunction()->getName();
116   switch (TII->lookupName((const char *)Name.bytes_begin(), Name.size())) {
117   default:
118     return false;
119   case AMDGPUIntrinsic::SI_tid:
120   case AMDGPUIntrinsic::SI_fs_interp:
121     return true;
122   }
123 }
124
125 static bool isArgPassedInSGPR(const Argument *A) {
126   const Function *F = A->getParent();
127   unsigned ShaderType = AMDGPU::getShaderType(*F);
128
129   // Arguments to compute shaders are never a source of divergence.
130   if (ShaderType == ShaderType::COMPUTE)
131     return true;
132
133   // For non-compute shaders, the inreg attribute is used to mark inputs,
134   // which pre-loaded into SGPRs.
135   if (F->getAttributes().hasAttribute(A->getArgNo(), Attribute::InReg))
136     return true;
137
138   // For non-compute shaders, 32-bit values are pre-loaded into vgprs, all
139   // other value types use SGPRS.
140   return !A->getType()->isIntegerTy(32) && !A->getType()->isFloatTy();
141 }
142
143 ///
144 /// \returns true if the result of the value could potentially be
145 /// different across workitems in a wavefront.
146 bool AMDGPUTTIImpl::isSourceOfDivergence(const Value *V) const {
147
148   if (const Argument *A = dyn_cast<Argument>(V))
149     return !isArgPassedInSGPR(A);
150
151   // Loads from the private address space are divergent, because threads
152   // can execute the load instruction with the same inputs and get different
153   // results.
154   //
155   // All other loads are not divergent, because if threads issue loads with the
156   // same arguments, they will always get the same result.
157   if (const LoadInst *Load = dyn_cast<LoadInst>(V))
158     return Load->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
159
160   if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(V)) {
161     const TargetMachine &TM = getTLI()->getTargetMachine();
162     return isIntrinsicSourceOfDivergence(TM.getIntrinsicInfo(), Intrinsic);
163   }
164
165   // Assume all function calls are a source of divergence.
166   if (isa<CallInst>(V) || isa<InvokeInst>(V))
167     return true;
168
169   return false;
170 }