1 //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===//
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 // The kernel divergence analysis is an LLVM pass which can be used to find out
11 // if a branch instruction in a GPU program (kernel) is divergent or not. It can help
12 // branch optimizations such as jump threading and loop unswitching to make
13 // better decisions.
14 //
15 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
17 #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
18 
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Analysis/DivergenceAnalysis.h"
23 
24 namespace llvm {
25 class Value;
26 class GPUDivergenceAnalysis;
27 class LegacyDivergenceAnalysis : public FunctionPass {
28 public:
29   static char ID;
30 
LegacyDivergenceAnalysis()31   LegacyDivergenceAnalysis() : FunctionPass(ID) {
32     initializeLegacyDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
33   }
34 
35   void getAnalysisUsage(AnalysisUsage &AU) const override;
36 
37   bool runOnFunction(Function &F) override;
38 
39   // Print all divergent branches in the function.
40   void print(raw_ostream &OS, const Module *) const override;
41 
42   // Returns true if V is divergent at its definition.
43   //
44   // Even if this function returns false, V may still be divergent when used
45   // in a different basic block.
46   bool isDivergent(const Value *V) const;
47 
48   // Returns true if V is uniform/non-divergent.
49   //
50   // Even if this function returns true, V may still be divergent when used
51   // in a different basic block.
isUniform(const Value * V)52   bool isUniform(const Value *V) const { return !isDivergent(V); }
53 
54   // Keep the analysis results uptodate by removing an erased value.
removeValue(const Value * V)55   void removeValue(const Value *V) { DivergentValues.erase(V); }
56 
57 private:
58   // Whether analysis should be performed by GPUDivergenceAnalysis.
59   bool shouldUseGPUDivergenceAnalysis(const Function &F) const;
60 
61   // (optional) handle to new DivergenceAnalysis
62   std::unique_ptr<GPUDivergenceAnalysis> gpuDA;
63 
64   // Stores all divergent values.
65   DenseSet<const Value *> DivergentValues;
66 };
67 } // End llvm namespace
68 
69 #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
70