1 //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
17 #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Analysis/DivergenceAnalysis.h"
26 class GPUDivergenceAnalysis;
27 class LegacyDivergenceAnalysis : public FunctionPass {
31 LegacyDivergenceAnalysis() : FunctionPass(ID) {
32 initializeLegacyDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
35 void getAnalysisUsage(AnalysisUsage &AU) const override;
37 bool runOnFunction(Function &F) override;
39 // Print all divergent branches in the function.
40 void print(raw_ostream &OS, const Module *) const override;
42 // Returns true if V is divergent at its definition.
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;
48 // Returns true if V is uniform/non-divergent.
50 // Even if this function returns true, V may still be divergent when used
51 // in a different basic block.
52 bool isUniform(const Value *V) const { return !isDivergent(V); }
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 } // End llvm namespace
69 #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H