1 //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
16 #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Analysis/DivergenceAnalysis.h"
25 class GPUDivergenceAnalysis
;
26 class LegacyDivergenceAnalysis
: public FunctionPass
{
30 LegacyDivergenceAnalysis() : FunctionPass(ID
) {
31 initializeLegacyDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
34 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
36 bool runOnFunction(Function
&F
) override
;
38 // Print all divergent branches in the function.
39 void print(raw_ostream
&OS
, const Module
*) const override
;
41 // Returns true if V is divergent at its definition.
42 bool isDivergent(const Value
*V
) const;
44 // Returns true if U is divergent. Uses of a uniform value can be divergent.
45 bool isDivergentUse(const Use
*U
) const;
47 // Returns true if V is uniform/non-divergent.
48 bool isUniform(const Value
*V
) const { return !isDivergent(V
); }
50 // Returns true if U is uniform/non-divergent. Uses of a uniform value can be
52 bool isUniformUse(const Use
*U
) const { return !isDivergentUse(U
); }
54 // Keep the analysis results uptodate by removing an erased value.
55 void removeValue(const Value
*V
) { DivergentValues
.erase(V
); }
58 // Whether analysis should be performed by GPUDivergenceAnalysis.
59 bool shouldUseGPUDivergenceAnalysis(const Function
&F
) const;
61 // (optional) handle to new DivergenceAnalysis
62 std::unique_ptr
<GPUDivergenceAnalysis
> gpuDA
;
64 // Stores all divergent values.
65 DenseSet
<const Value
*> DivergentValues
;
67 // Stores divergent uses of possibly uniform values.
68 DenseSet
<const Use
*> DivergentUses
;
70 } // End llvm namespace
72 #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H