LegacyDivergenceAnalysis.h 2.5 KB

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