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