DivergenceAnalysis.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //===- llvm/Analysis/DivergenceAnalysis.h - Divergence 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 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
  13. // better decisions.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/ADT/DenseSet.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/Pass.h"
  19. namespace llvm {
  20. class Value;
  21. class DivergenceAnalysis : public FunctionPass {
  22. public:
  23. static char ID;
  24. DivergenceAnalysis() : FunctionPass(ID) {
  25. initializeDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
  26. }
  27. void getAnalysisUsage(AnalysisUsage &AU) const override;
  28. bool runOnFunction(Function &F) override;
  29. // Print all divergent branches in the function.
  30. void print(raw_ostream &OS, const Module *) const override;
  31. // Returns true if V is divergent.
  32. bool isDivergent(const Value *V) const { return DivergentValues.count(V); }
  33. // Returns true if V is uniform/non-divergent.
  34. bool isUniform(const Value *V) const { return !isDivergent(V); }
  35. private:
  36. // Stores all divergent values.
  37. DenseSet<const Value *> DivergentValues;
  38. };
  39. } // End llvm namespace