1 //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- 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 // The kernel divergence analysis is an LLVM pass which can be used to find out 10 // if a branch instruction in a GPU program (kernel) is divergent or not. It can help 11 // branch optimizations such as jump threading and loop unswitching to make 12 // better decisions. 13 // 14 //===----------------------------------------------------------------------===// 15 #ifndef LLVM_ANALYSIS_LEGACYDIVERGENCEANALYSIS_H 16 #define LLVM_ANALYSIS_LEGACYDIVERGENCEANALYSIS_H 17 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/Analysis/LoopInfo.h" 20 #include "llvm/Analysis/PostDominators.h" 21 #include "llvm/IR/PassManager.h" 22 #include "llvm/Pass.h" 23 #include <memory> 24 25 namespace llvm { 26 class DivergenceInfo; 27 class Function; 28 class Module; 29 class raw_ostream; 30 class TargetTransformInfo; 31 class Use; 32 class Value; 33 34 class LegacyDivergenceAnalysisImpl { 35 public: 36 // Returns true if V is divergent at its definition. 37 bool isDivergent(const Value *V) const; 38 39 // Returns true if U is divergent. Uses of a uniform value can be divergent. 40 bool isDivergentUse(const Use *U) const; 41 42 // Returns true if V is uniform/non-divergent. isUniform(const Value * V)43 bool isUniform(const Value *V) const { return !isDivergent(V); } 44 45 // Returns true if U is uniform/non-divergent. Uses of a uniform value can be 46 // divergent. isUniformUse(const Use * U)47 bool isUniformUse(const Use *U) const { return !isDivergentUse(U); } 48 49 // Keep the analysis results uptodate by removing an erased value. removeValue(const Value * V)50 void removeValue(const Value *V) { DivergentValues.erase(V); } 51 52 // Print all divergent branches in the function. 53 void print(raw_ostream &OS, const Module *) const; 54 55 // Whether analysis should be performed by GPUDivergenceAnalysis. 56 bool shouldUseGPUDivergenceAnalysis(const Function &F, 57 const TargetTransformInfo &TTI, 58 const LoopInfo &LI); 59 60 void run(Function &F, TargetTransformInfo &TTI, DominatorTree &DT, 61 PostDominatorTree &PDT, const LoopInfo &LI); 62 63 protected: 64 // (optional) handle to new DivergenceAnalysis 65 std::unique_ptr<DivergenceInfo> gpuDA; 66 67 // Stores all divergent values. 68 DenseSet<const Value *> DivergentValues; 69 70 // Stores divergent uses of possibly uniform values. 71 DenseSet<const Use *> DivergentUses; 72 }; 73 74 class LegacyDivergenceAnalysis : public FunctionPass, 75 public LegacyDivergenceAnalysisImpl { 76 public: 77 static char ID; 78 79 LegacyDivergenceAnalysis(); 80 void getAnalysisUsage(AnalysisUsage &AU) const override; 81 bool runOnFunction(Function &F) override; 82 }; 83 84 class LegacyDivergenceAnalysisPass 85 : public PassInfoMixin<LegacyDivergenceAnalysisPass>, 86 public LegacyDivergenceAnalysisImpl { 87 public: 88 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 89 90 private: 91 // (optional) handle to new DivergenceAnalysis 92 std::unique_ptr<DivergenceInfo> gpuDA; 93 94 // Stores all divergent values. 95 DenseSet<const Value *> DivergentValues; 96 97 // Stores divergent uses of possibly uniform values. 98 DenseSet<const Use *> DivergentUses; 99 }; 100 101 } // end namespace llvm 102 103 #endif // LLVM_ANALYSIS_LEGACYDIVERGENCEANALYSIS_H 104