blob: 0a338b816640e3e08ae015ef979687ecfa76b822 [file] [log] [blame]
Andrew Scull0372a572018-11-16 15:47:06 +00001//===- llvm/Analysis/LegacyDivergenceAnalysis.h - KernelDivergence Analysis -*- C++ -*-===//
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
Andrew Scull0372a572018-11-16 15:47:06 +00009// 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 Scull5e1ddfa2018-08-14 10:06:54 +010011// branch optimizations such as jump threading and loop unswitching to make
12// better decisions.
13//
14//===----------------------------------------------------------------------===//
Andrew Scull0372a572018-11-16 15:47:06 +000015#ifndef LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
16#define LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/IR/Function.h"
20#include "llvm/Pass.h"
Andrew Walbran16937d02019-10-22 13:54:20 +010021#include "llvm/Analysis/DivergenceAnalysis.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022
23namespace llvm {
24class Value;
Andrew Walbran16937d02019-10-22 13:54:20 +010025class GPUDivergenceAnalysis;
Andrew Scull0372a572018-11-16 15:47:06 +000026class LegacyDivergenceAnalysis : public FunctionPass {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027public:
28 static char ID;
29
Andrew Scull0372a572018-11-16 15:47:06 +000030 LegacyDivergenceAnalysis() : FunctionPass(ID) {
31 initializeLegacyDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010032 }
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 Scullcdfcccc2018-10-05 20:58:37 +010041 // 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 Walbran16937d02019-10-22 13:54:20 +010045 bool isDivergent(const Value *V) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046
47 // Returns true if V is uniform/non-divergent.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010048 //
49 // Even if this function returns true, V may still be divergent when used
50 // in a different basic block.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 bool isUniform(const Value *V) const { return !isDivergent(V); }
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053 // Keep the analysis results uptodate by removing an erased value.
54 void removeValue(const Value *V) { DivergentValues.erase(V); }
55
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056private:
Andrew Walbran16937d02019-10-22 13:54:20 +010057 // 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 Scull5e1ddfa2018-08-14 10:06:54 +010063 // Stores all divergent values.
64 DenseSet<const Value *> DivergentValues;
65};
66} // End llvm namespace
67
Andrew Scull0372a572018-11-16 15:47:06 +000068#endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H