Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 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 | |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 18 | #include "AMDGPU.h" |
| 19 | #include "AMDGPUTargetMachine.h" |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 24 | #include "llvm/Target/CostTable.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetLowering.h" |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame^] | 28 | #define DEBUG_TYPE "AMDGPUtti" |
| 29 | |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 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 | |
Craig Topper | 77dfe45 | 2014-03-02 08:08:51 +0000 | [diff] [blame] | 39 | class AMDGPUTTI final : public ImmutablePass, public TargetTransformInfo { |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 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(0), ST(0), TLI(0) { |
| 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->getTargetLowering()) { |
| 56 | initializeAMDGPUTTIPass(*PassRegistry::getPassRegistry()); |
| 57 | } |
| 58 | |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 59 | virtual void initializePass() override { pushTTIStack(this); } |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 60 | |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 61 | virtual void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 62 | TargetTransformInfo::getAnalysisUsage(AU); |
| 63 | } |
| 64 | |
| 65 | /// Pass identification. |
| 66 | static char ID; |
| 67 | |
| 68 | /// Provide necessary pointer adjustments for the two base classes. |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 69 | virtual void *getAdjustedAnalysisPointer(const void *ID) override { |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 70 | if (ID == &TargetTransformInfo::ID) |
| 71 | return (TargetTransformInfo *)this; |
| 72 | return this; |
| 73 | } |
| 74 | |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 75 | virtual bool hasBranchDivergence() const override; |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 76 | |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 77 | virtual void getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const; |
| 78 | |
Tom Stellard | 8b1e021 | 2013-07-27 00:01:07 +0000 | [diff] [blame] | 79 | /// @} |
| 80 | }; |
| 81 | |
| 82 | } // end anonymous namespace |
| 83 | |
| 84 | INITIALIZE_AG_PASS(AMDGPUTTI, TargetTransformInfo, "AMDGPUtti", |
| 85 | "AMDGPU Target Transform Info", true, true, false) |
| 86 | char AMDGPUTTI::ID = 0; |
| 87 | |
| 88 | ImmutablePass * |
| 89 | llvm::createAMDGPUTargetTransformInfoPass(const AMDGPUTargetMachine *TM) { |
| 90 | return new AMDGPUTTI(TM); |
| 91 | } |
| 92 | |
| 93 | bool AMDGPUTTI::hasBranchDivergence() const { return true; } |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 94 | |
| 95 | void AMDGPUTTI::getUnrollingPreferences(Loop *L, |
| 96 | UnrollingPreferences &UP) const { |
| 97 | for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end(); |
| 98 | BI != BE; ++BI) { |
| 99 | BasicBlock *BB = *BI; |
| 100 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 101 | I != E; ++I) { |
| 102 | const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I); |
| 103 | if (!GEP) |
| 104 | continue; |
| 105 | const Value *Ptr = GEP->getPointerOperand(); |
| 106 | const AllocaInst *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr)); |
| 107 | if (Alloca) { |
| 108 | // We want to do whatever we can to limit the number of alloca |
| 109 | // instructions that make it through to the code generator. allocas |
| 110 | // require us to use indirect addressing, which is slow and prone to |
| 111 | // compiler bugs. If this loop does an address calculation on an |
Tom Stellard | fd0d86c | 2014-02-25 21:36:21 +0000 | [diff] [blame] | 112 | // alloca ptr, then we want to use a higher than normal loop unroll |
Matt Arsenault | 5e1e431 | 2014-04-04 20:13:08 +0000 | [diff] [blame] | 113 | // threshold. This will give SROA a better chance to eliminate these |
| 114 | // allocas. |
| 115 | // |
| 116 | // Don't use the maximum allowed value here as it will make some |
| 117 | // programs way too big. |
Tom Stellard | fd0d86c | 2014-02-25 21:36:21 +0000 | [diff] [blame] | 118 | UP.Threshold = 500; |
Tom Stellard | 8cce9bd | 2014-01-23 18:49:28 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |