1 //===- llvm/Analysis/DivergenceAnalysis.h - Divergence 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 divergence analysis is an LLVM pass which can be used to find out
11 // if a branch instruction in a GPU program is divergent or not. It can help
12 // branch optimizations such as jump threading and loop unswitching to make
15 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/Pass.h"
23 class DivergenceAnalysis : public FunctionPass {
27 DivergenceAnalysis() : FunctionPass(ID) {
28 initializeDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
31 void getAnalysisUsage(AnalysisUsage &AU) const override;
33 bool runOnFunction(Function &F) override;
35 // Print all divergent branches in the function.
36 void print(raw_ostream &OS, const Module *) const override;
38 // Returns true if V is divergent.
39 bool isDivergent(const Value *V) const { return DivergentValues.count(V); }
41 // Returns true if V is uniform/non-divergent.
42 bool isUniform(const Value *V) const { return !isDivergent(V); }
45 // Stores all divergent values.
46 DenseSet<const Value *> DivergentValues;
48 } // End llvm namespace