• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LEGACY_DIVERGENCE_ANALYSIS_H
16 #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
17 
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/Analysis/DivergenceAnalysis.h"
20 #include "llvm/Pass.h"
21 
22 namespace llvm {
23 class Value;
24 class Function;
25 class GPUDivergenceAnalysis;
26 class LegacyDivergenceAnalysis : public FunctionPass {
27 public:
28   static char ID;
29 
30   LegacyDivergenceAnalysis();
31 
32   void getAnalysisUsage(AnalysisUsage &AU) const override;
33 
34   bool runOnFunction(Function &F) override;
35 
36   // Print all divergent branches in the function.
37   void print(raw_ostream &OS, const Module *) const override;
38 
39   // Returns true if V is divergent at its definition.
40   bool isDivergent(const Value *V) const;
41 
42   // Returns true if U is divergent. Uses of a uniform value can be divergent.
43   bool isDivergentUse(const Use *U) const;
44 
45   // Returns true if V is uniform/non-divergent.
isUniform(const Value * V)46   bool isUniform(const Value *V) const { return !isDivergent(V); }
47 
48   // Returns true if U is uniform/non-divergent. Uses of a uniform value can be
49   // divergent.
isUniformUse(const Use * U)50   bool isUniformUse(const Use *U) const { return !isDivergentUse(U); }
51 
52   // Keep the analysis results uptodate by removing an erased value.
removeValue(const Value * V)53   void removeValue(const Value *V) { DivergentValues.erase(V); }
54 
55 private:
56   // Whether analysis should be performed by GPUDivergenceAnalysis.
57   bool shouldUseGPUDivergenceAnalysis(const Function &F) const;
58 
59   // (optional) handle to new DivergenceAnalysis
60   std::unique_ptr<GPUDivergenceAnalysis> gpuDA;
61 
62   // Stores all divergent values.
63   DenseSet<const Value *> DivergentValues;
64 
65   // Stores divergent uses of possibly uniform values.
66   DenseSet<const Use *> DivergentUses;
67 };
68 } // End llvm namespace
69 
70 #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
71