blob: 15400f5e07ff99c98786e59179c6391f783ad595 [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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019#include "llvm/Pass.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020020#include <memory>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021
22namespace llvm {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020023class Function;
Andrew Walbran16937d02019-10-22 13:54:20 +010024class GPUDivergenceAnalysis;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020025class Module;
26class raw_ostream;
27class TargetTransformInfo;
28class Use;
29class Value;
30
Andrew Scull0372a572018-11-16 15:47:06 +000031class LegacyDivergenceAnalysis : public FunctionPass {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010032public:
33 static char ID;
34
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020035 LegacyDivergenceAnalysis();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010036
37 void getAnalysisUsage(AnalysisUsage &AU) const override;
38
39 bool runOnFunction(Function &F) override;
40
41 // Print all divergent branches in the function.
42 void print(raw_ostream &OS, const Module *) const override;
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044 // Returns true if V is divergent at its definition.
Andrew Walbran16937d02019-10-22 13:54:20 +010045 bool isDivergent(const Value *V) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020047 // Returns true if U is divergent. Uses of a uniform value can be divergent.
48 bool isDivergentUse(const Use *U) const;
49
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 // Returns true if V is uniform/non-divergent.
51 bool isUniform(const Value *V) const { return !isDivergent(V); }
52
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020053 // Returns true if U is uniform/non-divergent. Uses of a uniform value can be
54 // divergent.
55 bool isUniformUse(const Use *U) const { return !isDivergentUse(U); }
56
Andrew Scullcdfcccc2018-10-05 20:58:37 +010057 // Keep the analysis results uptodate by removing an erased value.
58 void removeValue(const Value *V) { DivergentValues.erase(V); }
59
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060private:
Andrew Walbran16937d02019-10-22 13:54:20 +010061 // Whether analysis should be performed by GPUDivergenceAnalysis.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020062 bool shouldUseGPUDivergenceAnalysis(const Function &F,
63 const TargetTransformInfo &TTI) const;
Andrew Walbran16937d02019-10-22 13:54:20 +010064
65 // (optional) handle to new DivergenceAnalysis
66 std::unique_ptr<GPUDivergenceAnalysis> gpuDA;
67
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 // Stores all divergent values.
69 DenseSet<const Value *> DivergentValues;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020070
71 // Stores divergent uses of possibly uniform values.
72 DenseSet<const Use *> DivergentUses;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073};
74} // End llvm namespace
75
Andrew Scull0372a572018-11-16 15:47:06 +000076#endif //LLVM_ANALYSIS_LEGACY_DIVERGENCE_ANALYSIS_H