[X86][SSE] Vector integer/float conversion memory folding
[oota-llvm.git] / lib / Target / R600 / 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 "AMDGPU.h"
19 #include "AMDGPUTargetMachine.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Target/CostTable.h"
25 #include "llvm/Target/TargetLowering.h"
26 using namespace llvm;
27
28 #define DEBUG_TYPE "AMDGPUtti"
29
30 // Declare the pass initialization routine locally as target-specific passes
31 // don't have a target-wide initialization entry point, and so we rely on the
32 // pass constructor initialization.
33 namespace llvm {
34 void initializeAMDGPUTTIPass(PassRegistry &);
35 }
36
37 namespace {
38
39 class AMDGPUTTI final : public ImmutablePass, public TargetTransformInfo {
40   const AMDGPUTargetMachine *TM;
41   const AMDGPUSubtarget *ST;
42   const AMDGPUTargetLowering *TLI;
43
44   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
45   /// are set if the result needs to be inserted and/or extracted from vectors.
46   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
47
48 public:
49   AMDGPUTTI() : ImmutablePass(ID), TM(nullptr), ST(nullptr), TLI(nullptr) {
50     llvm_unreachable("This pass cannot be directly constructed");
51   }
52
53   AMDGPUTTI(const AMDGPUTargetMachine *TM)
54       : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()),
55         TLI(TM->getSubtargetImpl()->getTargetLowering()) {
56     initializeAMDGPUTTIPass(*PassRegistry::getPassRegistry());
57   }
58
59   void initializePass() override { pushTTIStack(this); }
60
61   void getAnalysisUsage(AnalysisUsage &AU) const override {
62     TargetTransformInfo::getAnalysisUsage(AU);
63   }
64
65   /// Pass identification.
66   static char ID;
67
68   /// Provide necessary pointer adjustments for the two base classes.
69   void *getAdjustedAnalysisPointer(const void *ID) override {
70     if (ID == &TargetTransformInfo::ID)
71       return (TargetTransformInfo *)this;
72     return this;
73   }
74
75   bool hasBranchDivergence() const override;
76
77   void getUnrollingPreferences(const Function *F, Loop *L,
78                                UnrollingPreferences &UP) const override;
79
80   PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const override;
81
82   unsigned getNumberOfRegisters(bool Vector) const override;
83   unsigned getRegisterBitWidth(bool Vector) const override;
84   unsigned getMaxInterleaveFactor() const override;
85
86   /// @}
87 };
88
89 } // end anonymous namespace
90
91 INITIALIZE_AG_PASS(AMDGPUTTI, TargetTransformInfo, "AMDGPUtti",
92                    "AMDGPU Target Transform Info", true, true, false)
93 char AMDGPUTTI::ID = 0;
94
95 ImmutablePass *
96 llvm::createAMDGPUTargetTransformInfoPass(const AMDGPUTargetMachine *TM) {
97   return new AMDGPUTTI(TM);
98 }
99
100 bool AMDGPUTTI::hasBranchDivergence() const { return true; }
101
102 void AMDGPUTTI::getUnrollingPreferences(const Function *, Loop *L,
103                                         UnrollingPreferences &UP) const {
104   UP.Threshold = 300; // Twice the default.
105   UP.Count = UINT_MAX;
106   UP.Partial = true;
107
108   // TODO: Do we want runtime unrolling?
109
110   for (const BasicBlock *BB : L->getBlocks()) {
111     for (const Instruction &I : *BB) {
112       const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
113       if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
114         continue;
115
116       const Value *Ptr = GEP->getPointerOperand();
117       const AllocaInst *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr));
118       if (Alloca) {
119         // We want to do whatever we can to limit the number of alloca
120         // instructions that make it through to the code generator.  allocas
121         // require us to use indirect addressing, which is slow and prone to
122         // compiler bugs.  If this loop does an address calculation on an
123         // alloca ptr, then we want to use a higher than normal loop unroll
124         // threshold. This will give SROA a better chance to eliminate these
125         // allocas.
126         //
127         // Don't use the maximum allowed value here as it will make some
128         // programs way too big.
129         UP.Threshold = 800;
130       }
131     }
132   }
133 }
134
135 AMDGPUTTI::PopcntSupportKind
136 AMDGPUTTI::getPopcntSupport(unsigned TyWidth) const {
137   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
138   return ST->hasBCNT(TyWidth) ? PSK_FastHardware : PSK_Software;
139 }
140
141 unsigned AMDGPUTTI::getNumberOfRegisters(bool Vec) const {
142   if (Vec)
143     return 0;
144
145   // Number of VGPRs on SI.
146   if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
147     return 256;
148
149   return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
150 }
151
152 unsigned AMDGPUTTI::getRegisterBitWidth(bool) const {
153   return 32;
154 }
155
156 unsigned AMDGPUTTI::getMaxInterleaveFactor() const {
157   // Semi-arbitrary large amount.
158   return 64;
159 }