llvm.org GIT mirror llvm / efe40a5
SCCIterator: Merge MinVisitNumStack and VisitStack This patch merges MinVisitNumStack with VisitStack using a StackElement struct. Patch by Mehdi Amini! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201353 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 6 years ago
1 changed file(s) with 39 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
4646 typedef typename super::reference reference;
4747 typedef typename super::pointer pointer;
4848
49 // Element of VisitStack during DFS.
50 struct StackElement {
51 NodeType *Node; ///< The current node pointer.
52 ChildItTy NextChild; ///< The next child, modified inplace during DFS.
53 unsigned MinVisited; ///< Minimum uplink value of all children of Node.
54
55 StackElement(NodeType *Node, const ChildItTy &Child, unsigned Min)
56 : Node(Node), NextChild(Child), MinVisited(Min) {}
57
58 bool operator==(const StackElement &Other) const {
59 return Node == Other.Node &&
60 NextChild == Other.NextChild &&
61 MinVisited == Other.MinVisited;
62 }
63 };
64
4965 // The visit counters used to detect when a complete SCC is on the stack.
5066 // visitNum is the global counter.
5167 // nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
5874 // The current SCC, retrieved using operator*().
5975 SccTy CurrentSCC;
6076
61 // Used to maintain the ordering. The top is the current block, the first
62 // element is basic block pointer, second is the 'next child' to visit.
63 std::vector > VisitStack;
64
65 // Stack holding the "min" values for each node in the DFS. This is used to
66 // track the minimum uplink values for all children of the corresponding node
67 // on the VisitStack.
68 std::vector MinVisitNumStack;
77
78 // DFS stack, Used to maintain the ordering. The top contains the current
79 // node, the next child to visit, and the minimum uplink value of all child
80 std::vector VisitStack;
6981
7082 // A single "visit" within the non-recursive DFS traversal.
7183 void DFSVisitOne(NodeType *N) {
7284 ++visitNum;
7385 nodeVisitNumbers[N] = visitNum;
7486 SCCNodeStack.push_back(N);
75 MinVisitNumStack.push_back(visitNum);
76 VisitStack.push_back(std::make_pair(N, GT::child_begin(N)));
87 VisitStack.push_back(StackElement(N, GT::child_begin(N), visitNum));
7788 #if 0 // Enable if needed when debugging.
7889 dbgs() << "TarjanSCC: Node " << N <<
7990 " : visitNum = " << visitNum << "\n";
8394 // The stack-based DFS traversal; defined below.
8495 void DFSVisitChildren() {
8596 assert(!VisitStack.empty());
86 while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
97 while (VisitStack.back().NextChild !=
98 GT::child_end(VisitStack.back().Node)) {
8799 // TOS has at least one more child so continue DFS
88 NodeType *childN = *VisitStack.back().second++;
89 if (!nodeVisitNumbers.count(childN)) {
100 NodeType *childN = *VisitStack.back().NextChild++;
101 typename DenseMap::iterator Visited =
102 nodeVisitNumbers.find(childN);
103 if (Visited == nodeVisitNumbers.end()) {
90104 // this node has never been seen.
91105 DFSVisitOne(childN);
92106 continue;
93107 }
94108
95 unsigned childNum = nodeVisitNumbers[childN];
96 if (MinVisitNumStack.back() > childNum)
97 MinVisitNumStack.back() = childNum;
109 unsigned childNum = Visited->second;
110 if (VisitStack.back().MinVisited > childNum)
111 VisitStack.back().MinVisited = childNum;
98112 }
99113 }
100114
101115 // Compute the next SCC using the DFS traversal.
102116 void GetNextSCC() {
103 assert(VisitStack.size() == MinVisitNumStack.size());
104117 CurrentSCC.clear(); // Prepare to compute the next SCC
105118 while (!VisitStack.empty()) {
106119 DFSVisitChildren();
107 assert(VisitStack.back().second ==
108 GT::child_end(VisitStack.back().first));
109 NodeType *visitingN = VisitStack.back().first;
110 unsigned minVisitNum = MinVisitNumStack.back();
120
121 // Pop the leaf on top of the VisitStack.
122 NodeType *visitingN = VisitStack.back().Node;
123 unsigned minVisitNum = VisitStack.back().MinVisited;
124 assert(VisitStack.back().NextChild == GT::child_end(visitingN));
111125 VisitStack.pop_back();
112 MinVisitNumStack.pop_back();
113 if (!MinVisitNumStack.empty() && MinVisitNumStack.back() > minVisitNum)
114 MinVisitNumStack.back() = minVisitNum;
126
127 // Propagate MinVisitNum to parent so we can detect the SCC starting node.
128 if (!VisitStack.empty() && VisitStack.back().MinVisited > minVisitNum)
129 VisitStack.back().MinVisited = minVisitNum;
115130
116131 #if 0 // Enable if needed when debugging.
117132 dbgs() << "TarjanSCC: Popped node " << visitingN <<