xref: /aosp_15_r20/external/llvm/lib/Analysis/DivergenceAnalysis.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- DivergenceAnalysis.cpp --------- Divergence Analysis Implementation -==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements divergence analysis which determines whether a branch
11*9880d681SAndroid Build Coastguard Worker // in a GPU program is divergent.It can help branch optimizations such as jump
12*9880d681SAndroid Build Coastguard Worker // threading and loop unswitching to make better decisions.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker // GPU programs typically use the SIMD execution model, where multiple threads
15*9880d681SAndroid Build Coastguard Worker // in the same execution group have to execute in lock-step. Therefore, if the
16*9880d681SAndroid Build Coastguard Worker // code contains divergent branches (i.e., threads in a group do not agree on
17*9880d681SAndroid Build Coastguard Worker // which path of the branch to take), the group of threads has to execute all
18*9880d681SAndroid Build Coastguard Worker // the paths from that branch with different subsets of threads enabled until
19*9880d681SAndroid Build Coastguard Worker // they converge at the immediately post-dominating BB of the paths.
20*9880d681SAndroid Build Coastguard Worker //
21*9880d681SAndroid Build Coastguard Worker // Due to this execution model, some optimizations such as jump
22*9880d681SAndroid Build Coastguard Worker // threading and loop unswitching can be unfortunately harmful when performed on
23*9880d681SAndroid Build Coastguard Worker // divergent branches. Therefore, an analysis that computes which branches in a
24*9880d681SAndroid Build Coastguard Worker // GPU program are divergent can help the compiler to selectively run these
25*9880d681SAndroid Build Coastguard Worker // optimizations.
26*9880d681SAndroid Build Coastguard Worker //
27*9880d681SAndroid Build Coastguard Worker // This file defines divergence analysis which computes a conservative but
28*9880d681SAndroid Build Coastguard Worker // non-trivial approximation of all divergent branches in a GPU program. It
29*9880d681SAndroid Build Coastguard Worker // partially implements the approach described in
30*9880d681SAndroid Build Coastguard Worker //
31*9880d681SAndroid Build Coastguard Worker //   Divergence Analysis
32*9880d681SAndroid Build Coastguard Worker //   Sampaio, Souza, Collange, Pereira
33*9880d681SAndroid Build Coastguard Worker //   TOPLAS '13
34*9880d681SAndroid Build Coastguard Worker //
35*9880d681SAndroid Build Coastguard Worker // The divergence analysis identifies the sources of divergence (e.g., special
36*9880d681SAndroid Build Coastguard Worker // variables that hold the thread ID), and recursively marks variables that are
37*9880d681SAndroid Build Coastguard Worker // data or sync dependent on a source of divergence as divergent.
38*9880d681SAndroid Build Coastguard Worker //
39*9880d681SAndroid Build Coastguard Worker // While data dependency is a well-known concept, the notion of sync dependency
40*9880d681SAndroid Build Coastguard Worker // is worth more explanation. Sync dependence characterizes the control flow
41*9880d681SAndroid Build Coastguard Worker // aspect of the propagation of branch divergence. For example,
42*9880d681SAndroid Build Coastguard Worker //
43*9880d681SAndroid Build Coastguard Worker //   %cond = icmp slt i32 %tid, 10
44*9880d681SAndroid Build Coastguard Worker //   br i1 %cond, label %then, label %else
45*9880d681SAndroid Build Coastguard Worker // then:
46*9880d681SAndroid Build Coastguard Worker //   br label %merge
47*9880d681SAndroid Build Coastguard Worker // else:
48*9880d681SAndroid Build Coastguard Worker //   br label %merge
49*9880d681SAndroid Build Coastguard Worker // merge:
50*9880d681SAndroid Build Coastguard Worker //   %a = phi i32 [ 0, %then ], [ 1, %else ]
51*9880d681SAndroid Build Coastguard Worker //
52*9880d681SAndroid Build Coastguard Worker // Suppose %tid holds the thread ID. Although %a is not data dependent on %tid
53*9880d681SAndroid Build Coastguard Worker // because %tid is not on its use-def chains, %a is sync dependent on %tid
54*9880d681SAndroid Build Coastguard Worker // because the branch "br i1 %cond" depends on %tid and affects which value %a
55*9880d681SAndroid Build Coastguard Worker // is assigned to.
56*9880d681SAndroid Build Coastguard Worker //
57*9880d681SAndroid Build Coastguard Worker // The current implementation has the following limitations:
58*9880d681SAndroid Build Coastguard Worker // 1. intra-procedural. It conservatively considers the arguments of a
59*9880d681SAndroid Build Coastguard Worker //    non-kernel-entry function and the return value of a function call as
60*9880d681SAndroid Build Coastguard Worker //    divergent.
61*9880d681SAndroid Build Coastguard Worker // 2. memory as black box. It conservatively considers values loaded from
62*9880d681SAndroid Build Coastguard Worker //    generic or local address as divergent. This can be improved by leveraging
63*9880d681SAndroid Build Coastguard Worker //    pointer analysis.
64*9880d681SAndroid Build Coastguard Worker //
65*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
66*9880d681SAndroid Build Coastguard Worker 
67*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/DivergenceAnalysis.h"
68*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/Passes.h"
69*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/PostDominators.h"
70*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
71*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
72*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
73*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
74*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
75*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Value.h"
76*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
77*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
78*9880d681SAndroid Build Coastguard Worker #include <vector>
79*9880d681SAndroid Build Coastguard Worker using namespace llvm;
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker namespace {
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker class DivergencePropagator {
84*9880d681SAndroid Build Coastguard Worker public:
DivergencePropagator(Function & F,TargetTransformInfo & TTI,DominatorTree & DT,PostDominatorTree & PDT,DenseSet<const Value * > & DV)85*9880d681SAndroid Build Coastguard Worker   DivergencePropagator(Function &F, TargetTransformInfo &TTI, DominatorTree &DT,
86*9880d681SAndroid Build Coastguard Worker                        PostDominatorTree &PDT, DenseSet<const Value *> &DV)
87*9880d681SAndroid Build Coastguard Worker       : F(F), TTI(TTI), DT(DT), PDT(PDT), DV(DV) {}
88*9880d681SAndroid Build Coastguard Worker   void populateWithSourcesOfDivergence();
89*9880d681SAndroid Build Coastguard Worker   void propagate();
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker private:
92*9880d681SAndroid Build Coastguard Worker   // A helper function that explores data dependents of V.
93*9880d681SAndroid Build Coastguard Worker   void exploreDataDependency(Value *V);
94*9880d681SAndroid Build Coastguard Worker   // A helper function that explores sync dependents of TI.
95*9880d681SAndroid Build Coastguard Worker   void exploreSyncDependency(TerminatorInst *TI);
96*9880d681SAndroid Build Coastguard Worker   // Computes the influence region from Start to End. This region includes all
97*9880d681SAndroid Build Coastguard Worker   // basic blocks on any simple path from Start to End.
98*9880d681SAndroid Build Coastguard Worker   void computeInfluenceRegion(BasicBlock *Start, BasicBlock *End,
99*9880d681SAndroid Build Coastguard Worker                               DenseSet<BasicBlock *> &InfluenceRegion);
100*9880d681SAndroid Build Coastguard Worker   // Finds all users of I that are outside the influence region, and add these
101*9880d681SAndroid Build Coastguard Worker   // users to Worklist.
102*9880d681SAndroid Build Coastguard Worker   void findUsersOutsideInfluenceRegion(
103*9880d681SAndroid Build Coastguard Worker       Instruction &I, const DenseSet<BasicBlock *> &InfluenceRegion);
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker   Function &F;
106*9880d681SAndroid Build Coastguard Worker   TargetTransformInfo &TTI;
107*9880d681SAndroid Build Coastguard Worker   DominatorTree &DT;
108*9880d681SAndroid Build Coastguard Worker   PostDominatorTree &PDT;
109*9880d681SAndroid Build Coastguard Worker   std::vector<Value *> Worklist; // Stack for DFS.
110*9880d681SAndroid Build Coastguard Worker   DenseSet<const Value *> &DV;   // Stores all divergent values.
111*9880d681SAndroid Build Coastguard Worker };
112*9880d681SAndroid Build Coastguard Worker 
populateWithSourcesOfDivergence()113*9880d681SAndroid Build Coastguard Worker void DivergencePropagator::populateWithSourcesOfDivergence() {
114*9880d681SAndroid Build Coastguard Worker   Worklist.clear();
115*9880d681SAndroid Build Coastguard Worker   DV.clear();
116*9880d681SAndroid Build Coastguard Worker   for (auto &I : instructions(F)) {
117*9880d681SAndroid Build Coastguard Worker     if (TTI.isSourceOfDivergence(&I)) {
118*9880d681SAndroid Build Coastguard Worker       Worklist.push_back(&I);
119*9880d681SAndroid Build Coastguard Worker       DV.insert(&I);
120*9880d681SAndroid Build Coastguard Worker     }
121*9880d681SAndroid Build Coastguard Worker   }
122*9880d681SAndroid Build Coastguard Worker   for (auto &Arg : F.args()) {
123*9880d681SAndroid Build Coastguard Worker     if (TTI.isSourceOfDivergence(&Arg)) {
124*9880d681SAndroid Build Coastguard Worker       Worklist.push_back(&Arg);
125*9880d681SAndroid Build Coastguard Worker       DV.insert(&Arg);
126*9880d681SAndroid Build Coastguard Worker     }
127*9880d681SAndroid Build Coastguard Worker   }
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker 
exploreSyncDependency(TerminatorInst * TI)130*9880d681SAndroid Build Coastguard Worker void DivergencePropagator::exploreSyncDependency(TerminatorInst *TI) {
131*9880d681SAndroid Build Coastguard Worker   // Propagation rule 1: if branch TI is divergent, all PHINodes in TI's
132*9880d681SAndroid Build Coastguard Worker   // immediate post dominator are divergent. This rule handles if-then-else
133*9880d681SAndroid Build Coastguard Worker   // patterns. For example,
134*9880d681SAndroid Build Coastguard Worker   //
135*9880d681SAndroid Build Coastguard Worker   // if (tid < 5)
136*9880d681SAndroid Build Coastguard Worker   //   a1 = 1;
137*9880d681SAndroid Build Coastguard Worker   // else
138*9880d681SAndroid Build Coastguard Worker   //   a2 = 2;
139*9880d681SAndroid Build Coastguard Worker   // a = phi(a1, a2); // sync dependent on (tid < 5)
140*9880d681SAndroid Build Coastguard Worker   BasicBlock *ThisBB = TI->getParent();
141*9880d681SAndroid Build Coastguard Worker 
142*9880d681SAndroid Build Coastguard Worker   // Unreachable blocks may not be in the dominator tree.
143*9880d681SAndroid Build Coastguard Worker   if (!DT.isReachableFromEntry(ThisBB))
144*9880d681SAndroid Build Coastguard Worker     return;
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker   // If the function has no exit blocks or doesn't reach any exit blocks, the
147*9880d681SAndroid Build Coastguard Worker   // post dominator may be null.
148*9880d681SAndroid Build Coastguard Worker   DomTreeNode *ThisNode = PDT.getNode(ThisBB);
149*9880d681SAndroid Build Coastguard Worker   if (!ThisNode)
150*9880d681SAndroid Build Coastguard Worker     return;
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker   BasicBlock *IPostDom = ThisNode->getIDom()->getBlock();
153*9880d681SAndroid Build Coastguard Worker   if (IPostDom == nullptr)
154*9880d681SAndroid Build Coastguard Worker     return;
155*9880d681SAndroid Build Coastguard Worker 
156*9880d681SAndroid Build Coastguard Worker   for (auto I = IPostDom->begin(); isa<PHINode>(I); ++I) {
157*9880d681SAndroid Build Coastguard Worker     // A PHINode is uniform if it returns the same value no matter which path is
158*9880d681SAndroid Build Coastguard Worker     // taken.
159*9880d681SAndroid Build Coastguard Worker     if (!cast<PHINode>(I)->hasConstantOrUndefValue() && DV.insert(&*I).second)
160*9880d681SAndroid Build Coastguard Worker       Worklist.push_back(&*I);
161*9880d681SAndroid Build Coastguard Worker   }
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker   // Propagation rule 2: if a value defined in a loop is used outside, the user
164*9880d681SAndroid Build Coastguard Worker   // is sync dependent on the condition of the loop exits that dominate the
165*9880d681SAndroid Build Coastguard Worker   // user. For example,
166*9880d681SAndroid Build Coastguard Worker   //
167*9880d681SAndroid Build Coastguard Worker   // int i = 0;
168*9880d681SAndroid Build Coastguard Worker   // do {
169*9880d681SAndroid Build Coastguard Worker   //   i++;
170*9880d681SAndroid Build Coastguard Worker   //   if (foo(i)) ... // uniform
171*9880d681SAndroid Build Coastguard Worker   // } while (i < tid);
172*9880d681SAndroid Build Coastguard Worker   // if (bar(i)) ...   // divergent
173*9880d681SAndroid Build Coastguard Worker   //
174*9880d681SAndroid Build Coastguard Worker   // A program may contain unstructured loops. Therefore, we cannot leverage
175*9880d681SAndroid Build Coastguard Worker   // LoopInfo, which only recognizes natural loops.
176*9880d681SAndroid Build Coastguard Worker   //
177*9880d681SAndroid Build Coastguard Worker   // The algorithm used here handles both natural and unstructured loops.  Given
178*9880d681SAndroid Build Coastguard Worker   // a branch TI, we first compute its influence region, the union of all simple
179*9880d681SAndroid Build Coastguard Worker   // paths from TI to its immediate post dominator (IPostDom). Then, we search
180*9880d681SAndroid Build Coastguard Worker   // for all the values defined in the influence region but used outside. All
181*9880d681SAndroid Build Coastguard Worker   // these users are sync dependent on TI.
182*9880d681SAndroid Build Coastguard Worker   DenseSet<BasicBlock *> InfluenceRegion;
183*9880d681SAndroid Build Coastguard Worker   computeInfluenceRegion(ThisBB, IPostDom, InfluenceRegion);
184*9880d681SAndroid Build Coastguard Worker   // An insight that can speed up the search process is that all the in-region
185*9880d681SAndroid Build Coastguard Worker   // values that are used outside must dominate TI. Therefore, instead of
186*9880d681SAndroid Build Coastguard Worker   // searching every basic blocks in the influence region, we search all the
187*9880d681SAndroid Build Coastguard Worker   // dominators of TI until it is outside the influence region.
188*9880d681SAndroid Build Coastguard Worker   BasicBlock *InfluencedBB = ThisBB;
189*9880d681SAndroid Build Coastguard Worker   while (InfluenceRegion.count(InfluencedBB)) {
190*9880d681SAndroid Build Coastguard Worker     for (auto &I : *InfluencedBB)
191*9880d681SAndroid Build Coastguard Worker       findUsersOutsideInfluenceRegion(I, InfluenceRegion);
192*9880d681SAndroid Build Coastguard Worker     DomTreeNode *IDomNode = DT.getNode(InfluencedBB)->getIDom();
193*9880d681SAndroid Build Coastguard Worker     if (IDomNode == nullptr)
194*9880d681SAndroid Build Coastguard Worker       break;
195*9880d681SAndroid Build Coastguard Worker     InfluencedBB = IDomNode->getBlock();
196*9880d681SAndroid Build Coastguard Worker   }
197*9880d681SAndroid Build Coastguard Worker }
198*9880d681SAndroid Build Coastguard Worker 
findUsersOutsideInfluenceRegion(Instruction & I,const DenseSet<BasicBlock * > & InfluenceRegion)199*9880d681SAndroid Build Coastguard Worker void DivergencePropagator::findUsersOutsideInfluenceRegion(
200*9880d681SAndroid Build Coastguard Worker     Instruction &I, const DenseSet<BasicBlock *> &InfluenceRegion) {
201*9880d681SAndroid Build Coastguard Worker   for (User *U : I.users()) {
202*9880d681SAndroid Build Coastguard Worker     Instruction *UserInst = cast<Instruction>(U);
203*9880d681SAndroid Build Coastguard Worker     if (!InfluenceRegion.count(UserInst->getParent())) {
204*9880d681SAndroid Build Coastguard Worker       if (DV.insert(UserInst).second)
205*9880d681SAndroid Build Coastguard Worker         Worklist.push_back(UserInst);
206*9880d681SAndroid Build Coastguard Worker     }
207*9880d681SAndroid Build Coastguard Worker   }
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker // A helper function for computeInfluenceRegion that adds successors of "ThisBB"
211*9880d681SAndroid Build Coastguard Worker // to the influence region.
212*9880d681SAndroid Build Coastguard Worker static void
addSuccessorsToInfluenceRegion(BasicBlock * ThisBB,BasicBlock * End,DenseSet<BasicBlock * > & InfluenceRegion,std::vector<BasicBlock * > & InfluenceStack)213*9880d681SAndroid Build Coastguard Worker addSuccessorsToInfluenceRegion(BasicBlock *ThisBB, BasicBlock *End,
214*9880d681SAndroid Build Coastguard Worker                                DenseSet<BasicBlock *> &InfluenceRegion,
215*9880d681SAndroid Build Coastguard Worker                                std::vector<BasicBlock *> &InfluenceStack) {
216*9880d681SAndroid Build Coastguard Worker   for (BasicBlock *Succ : successors(ThisBB)) {
217*9880d681SAndroid Build Coastguard Worker     if (Succ != End && InfluenceRegion.insert(Succ).second)
218*9880d681SAndroid Build Coastguard Worker       InfluenceStack.push_back(Succ);
219*9880d681SAndroid Build Coastguard Worker   }
220*9880d681SAndroid Build Coastguard Worker }
221*9880d681SAndroid Build Coastguard Worker 
computeInfluenceRegion(BasicBlock * Start,BasicBlock * End,DenseSet<BasicBlock * > & InfluenceRegion)222*9880d681SAndroid Build Coastguard Worker void DivergencePropagator::computeInfluenceRegion(
223*9880d681SAndroid Build Coastguard Worker     BasicBlock *Start, BasicBlock *End,
224*9880d681SAndroid Build Coastguard Worker     DenseSet<BasicBlock *> &InfluenceRegion) {
225*9880d681SAndroid Build Coastguard Worker   assert(PDT.properlyDominates(End, Start) &&
226*9880d681SAndroid Build Coastguard Worker          "End does not properly dominate Start");
227*9880d681SAndroid Build Coastguard Worker 
228*9880d681SAndroid Build Coastguard Worker   // The influence region starts from the end of "Start" to the beginning of
229*9880d681SAndroid Build Coastguard Worker   // "End". Therefore, "Start" should not be in the region unless "Start" is in
230*9880d681SAndroid Build Coastguard Worker   // a loop that doesn't contain "End".
231*9880d681SAndroid Build Coastguard Worker   std::vector<BasicBlock *> InfluenceStack;
232*9880d681SAndroid Build Coastguard Worker   addSuccessorsToInfluenceRegion(Start, End, InfluenceRegion, InfluenceStack);
233*9880d681SAndroid Build Coastguard Worker   while (!InfluenceStack.empty()) {
234*9880d681SAndroid Build Coastguard Worker     BasicBlock *BB = InfluenceStack.back();
235*9880d681SAndroid Build Coastguard Worker     InfluenceStack.pop_back();
236*9880d681SAndroid Build Coastguard Worker     addSuccessorsToInfluenceRegion(BB, End, InfluenceRegion, InfluenceStack);
237*9880d681SAndroid Build Coastguard Worker   }
238*9880d681SAndroid Build Coastguard Worker }
239*9880d681SAndroid Build Coastguard Worker 
exploreDataDependency(Value * V)240*9880d681SAndroid Build Coastguard Worker void DivergencePropagator::exploreDataDependency(Value *V) {
241*9880d681SAndroid Build Coastguard Worker   // Follow def-use chains of V.
242*9880d681SAndroid Build Coastguard Worker   for (User *U : V->users()) {
243*9880d681SAndroid Build Coastguard Worker     Instruction *UserInst = cast<Instruction>(U);
244*9880d681SAndroid Build Coastguard Worker     if (DV.insert(UserInst).second)
245*9880d681SAndroid Build Coastguard Worker       Worklist.push_back(UserInst);
246*9880d681SAndroid Build Coastguard Worker   }
247*9880d681SAndroid Build Coastguard Worker }
248*9880d681SAndroid Build Coastguard Worker 
propagate()249*9880d681SAndroid Build Coastguard Worker void DivergencePropagator::propagate() {
250*9880d681SAndroid Build Coastguard Worker   // Traverse the dependency graph using DFS.
251*9880d681SAndroid Build Coastguard Worker   while (!Worklist.empty()) {
252*9880d681SAndroid Build Coastguard Worker     Value *V = Worklist.back();
253*9880d681SAndroid Build Coastguard Worker     Worklist.pop_back();
254*9880d681SAndroid Build Coastguard Worker     if (TerminatorInst *TI = dyn_cast<TerminatorInst>(V)) {
255*9880d681SAndroid Build Coastguard Worker       // Terminators with less than two successors won't introduce sync
256*9880d681SAndroid Build Coastguard Worker       // dependency. Ignore them.
257*9880d681SAndroid Build Coastguard Worker       if (TI->getNumSuccessors() > 1)
258*9880d681SAndroid Build Coastguard Worker         exploreSyncDependency(TI);
259*9880d681SAndroid Build Coastguard Worker     }
260*9880d681SAndroid Build Coastguard Worker     exploreDataDependency(V);
261*9880d681SAndroid Build Coastguard Worker   }
262*9880d681SAndroid Build Coastguard Worker }
263*9880d681SAndroid Build Coastguard Worker 
264*9880d681SAndroid Build Coastguard Worker } /// end namespace anonymous
265*9880d681SAndroid Build Coastguard Worker 
266*9880d681SAndroid Build Coastguard Worker // Register this pass.
267*9880d681SAndroid Build Coastguard Worker char DivergenceAnalysis::ID = 0;
268*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(DivergenceAnalysis, "divergence", "Divergence Analysis",
269*9880d681SAndroid Build Coastguard Worker                       false, true)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)270*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
271*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
272*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(DivergenceAnalysis, "divergence", "Divergence Analysis",
273*9880d681SAndroid Build Coastguard Worker                     false, true)
274*9880d681SAndroid Build Coastguard Worker 
275*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createDivergenceAnalysisPass() {
276*9880d681SAndroid Build Coastguard Worker   return new DivergenceAnalysis();
277*9880d681SAndroid Build Coastguard Worker }
278*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const279*9880d681SAndroid Build Coastguard Worker void DivergenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
280*9880d681SAndroid Build Coastguard Worker   AU.addRequired<DominatorTreeWrapperPass>();
281*9880d681SAndroid Build Coastguard Worker   AU.addRequired<PostDominatorTreeWrapperPass>();
282*9880d681SAndroid Build Coastguard Worker   AU.setPreservesAll();
283*9880d681SAndroid Build Coastguard Worker }
284*9880d681SAndroid Build Coastguard Worker 
runOnFunction(Function & F)285*9880d681SAndroid Build Coastguard Worker bool DivergenceAnalysis::runOnFunction(Function &F) {
286*9880d681SAndroid Build Coastguard Worker   auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
287*9880d681SAndroid Build Coastguard Worker   if (TTIWP == nullptr)
288*9880d681SAndroid Build Coastguard Worker     return false;
289*9880d681SAndroid Build Coastguard Worker 
290*9880d681SAndroid Build Coastguard Worker   TargetTransformInfo &TTI = TTIWP->getTTI(F);
291*9880d681SAndroid Build Coastguard Worker   // Fast path: if the target does not have branch divergence, we do not mark
292*9880d681SAndroid Build Coastguard Worker   // any branch as divergent.
293*9880d681SAndroid Build Coastguard Worker   if (!TTI.hasBranchDivergence())
294*9880d681SAndroid Build Coastguard Worker     return false;
295*9880d681SAndroid Build Coastguard Worker 
296*9880d681SAndroid Build Coastguard Worker   DivergentValues.clear();
297*9880d681SAndroid Build Coastguard Worker   auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
298*9880d681SAndroid Build Coastguard Worker   DivergencePropagator DP(F, TTI,
299*9880d681SAndroid Build Coastguard Worker                           getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
300*9880d681SAndroid Build Coastguard Worker                           PDT, DivergentValues);
301*9880d681SAndroid Build Coastguard Worker   DP.populateWithSourcesOfDivergence();
302*9880d681SAndroid Build Coastguard Worker   DP.propagate();
303*9880d681SAndroid Build Coastguard Worker   return false;
304*9880d681SAndroid Build Coastguard Worker }
305*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & OS,const Module *) const306*9880d681SAndroid Build Coastguard Worker void DivergenceAnalysis::print(raw_ostream &OS, const Module *) const {
307*9880d681SAndroid Build Coastguard Worker   if (DivergentValues.empty())
308*9880d681SAndroid Build Coastguard Worker     return;
309*9880d681SAndroid Build Coastguard Worker   const Value *FirstDivergentValue = *DivergentValues.begin();
310*9880d681SAndroid Build Coastguard Worker   const Function *F;
311*9880d681SAndroid Build Coastguard Worker   if (const Argument *Arg = dyn_cast<Argument>(FirstDivergentValue)) {
312*9880d681SAndroid Build Coastguard Worker     F = Arg->getParent();
313*9880d681SAndroid Build Coastguard Worker   } else if (const Instruction *I =
314*9880d681SAndroid Build Coastguard Worker                  dyn_cast<Instruction>(FirstDivergentValue)) {
315*9880d681SAndroid Build Coastguard Worker     F = I->getParent()->getParent();
316*9880d681SAndroid Build Coastguard Worker   } else {
317*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Only arguments and instructions can be divergent");
318*9880d681SAndroid Build Coastguard Worker   }
319*9880d681SAndroid Build Coastguard Worker 
320*9880d681SAndroid Build Coastguard Worker   // Dumps all divergent values in F, arguments and then instructions.
321*9880d681SAndroid Build Coastguard Worker   for (auto &Arg : F->args()) {
322*9880d681SAndroid Build Coastguard Worker     if (DivergentValues.count(&Arg))
323*9880d681SAndroid Build Coastguard Worker       OS << "DIVERGENT:  " << Arg << "\n";
324*9880d681SAndroid Build Coastguard Worker   }
325*9880d681SAndroid Build Coastguard Worker   // Iterate instructions using instructions() to ensure a deterministic order.
326*9880d681SAndroid Build Coastguard Worker   for (auto &I : instructions(F)) {
327*9880d681SAndroid Build Coastguard Worker     if (DivergentValues.count(&I))
328*9880d681SAndroid Build Coastguard Worker       OS << "DIVERGENT:" << I << "\n";
329*9880d681SAndroid Build Coastguard Worker   }
330*9880d681SAndroid Build Coastguard Worker }
331