Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 1 | //===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | // |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 12 | // branch optimizations such as jump threading and loop unswitching to make |
| 13 | // better decisions. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 16 | #ifndef LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H |
| 17 | #define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | |
| 19 | #include "llvm/ADT/DenseSet.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | |
| 23 | namespace llvm { |
| 24 | class Value; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 25 | class LegacyDivergenceAnalysis : public FunctionPass { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | public: |
| 27 | static char ID; |
| 28 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 29 | LegacyDivergenceAnalysis() : FunctionPass(ID) { |
| 30 | initializeLegacyDivergenceAnalysisPass(*PassRegistry::getPassRegistry()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 34 | |
| 35 | bool runOnFunction(Function &F) override; |
| 36 | |
| 37 | // Print all divergent branches in the function. |
| 38 | void print(raw_ostream &OS, const Module *) const override; |
| 39 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | // Returns true if V is divergent at its definition. |
| 41 | // |
| 42 | // Even if this function returns false, V may still be divergent when used |
| 43 | // in a different basic block. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | bool isDivergent(const Value *V) const { return DivergentValues.count(V); } |
| 45 | |
| 46 | // Returns true if V is uniform/non-divergent. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 47 | // |
| 48 | // Even if this function returns true, V may still be divergent when used |
| 49 | // in a different basic block. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | bool isUniform(const Value *V) const { return !isDivergent(V); } |
| 51 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 52 | // Keep the analysis results uptodate by removing an erased value. |
| 53 | void removeValue(const Value *V) { DivergentValues.erase(V); } |
| 54 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | private: |
| 56 | // Stores all divergent values. |
| 57 | DenseSet<const Value *> DivergentValues; |
| 58 | }; |
| 59 | } // End llvm namespace |
| 60 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 61 | #endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H |