1 //=== llvm/Analysis/DominatorInternals.h - Dominator Calculation -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines shared implementation details of dominator and
11 // postdominator calculation. This file SHOULD NOT BE INCLUDED outside
12 // of the dominator and postdominator implementation files.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
17 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
19 #include "llvm/Analysis/Dominators.h"
23 template<class GraphT>
24 unsigned DFSPass(DominatorTreeBase& DT, typename GraphT::NodeType* V,
26 // This is more understandable as a recursive algorithm, but we can't use the
27 // recursive algorithm due to stack depth issues. Keep it here for
28 // documentation purposes.
30 InfoRec &VInfo = DT.Info[DT.Roots[i]];
34 Vertex.push_back(V); // Vertex[n] = V;
35 //Info[V].Ancestor = 0; // Ancestor[n] = 0
36 //Info[V].Child = 0; // Child[v] = 0
37 VInfo.Size = 1; // Size[v] = 1
39 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
40 InfoRec &SuccVInfo = DT.Info[*SI];
41 if (SuccVInfo.Semi == 0) {
43 N = DTDFSPass(DT, *SI, N);
47 std::vector<std::pair<typename GraphT::NodeType*,
48 typename GraphT::ChildIteratorType> > Worklist;
49 Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
50 while (!Worklist.empty()) {
51 typename GraphT::NodeType* BB = Worklist.back().first;
52 typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
54 // First time we visited this BB?
55 if (NextSucc == GraphT::child_begin(BB)) {
56 DominatorTree::InfoRec &BBInfo = DT.Info[BB];
60 DT.Vertex.push_back(BB); // Vertex[n] = V;
61 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
62 //BBInfo[V].Child = 0; // Child[v] = 0
63 BBInfo.Size = 1; // Size[v] = 1
66 // If we are done with this block, remove it from the worklist.
67 if (NextSucc == GraphT::child_end(BB)) {
72 // Increment the successor number for the next time we get to it.
73 ++Worklist.back().second;
75 // Visit the successor next, if it isn't already visited.
76 typename GraphT::NodeType* Succ = *NextSucc;
78 DominatorTree::InfoRec &SuccVInfo = DT.Info[Succ];
79 if (SuccVInfo.Semi == 0) {
80 SuccVInfo.Parent = BB;
81 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));