llvm.org GIT mirror llvm / ad56833
SCC: Un-inline long functions These are long functions that really shouldn't be inlined. Otherwise, no functionality change. <rdar://problem/14292693> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207249 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 6 years ago
1 changed file(s) with 85 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
8080 std::vector VisitStack;
8181
8282 // A single "visit" within the non-recursive DFS traversal.
83 void DFSVisitOne(NodeType *N) {
84 ++visitNum;
85 nodeVisitNumbers[N] = visitNum;
86 SCCNodeStack.push_back(N);
87 VisitStack.push_back(StackElement(N, GT::child_begin(N), visitNum));
88 #if 0 // Enable if needed when debugging.
89 dbgs() << "TarjanSCC: Node " << N <<
90 " : visitNum = " << visitNum << "\n";
91 #endif
92 }
83 void DFSVisitOne(NodeType *N);
9384
9485 // The stack-based DFS traversal; defined below.
95 void DFSVisitChildren() {
96 assert(!VisitStack.empty());
97 while (VisitStack.back().NextChild !=
98 GT::child_end(VisitStack.back().Node)) {
99 // TOS has at least one more child so continue DFS
100 NodeType *childN = *VisitStack.back().NextChild++;
101 typename DenseMap::iterator Visited =
102 nodeVisitNumbers.find(childN);
103 if (Visited == nodeVisitNumbers.end()) {
104 // this node has never been seen.
105 DFSVisitOne(childN);
106 continue;
107 }
108
109 unsigned childNum = Visited->second;
110 if (VisitStack.back().MinVisited > childNum)
111 VisitStack.back().MinVisited = childNum;
112 }
113 }
86 void DFSVisitChildren();
11487
11588 // Compute the next SCC using the DFS traversal.
116 void GetNextSCC() {
117 CurrentSCC.clear(); // Prepare to compute the next SCC
118 while (!VisitStack.empty()) {
119 DFSVisitChildren();
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));
125 VisitStack.pop_back();
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;
130
131 #if 0 // Enable if needed when debugging.
132 dbgs() << "TarjanSCC: Popped node " << visitingN <<
133 " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
134 nodeVisitNumbers[visitingN] << "\n";
135 #endif
136
137 if (minVisitNum != nodeVisitNumbers[visitingN])
138 continue;
139
140 // A full SCC is on the SCCNodeStack! It includes all nodes below
141 // visitingN on the stack. Copy those nodes to CurrentSCC,
142 // reset their minVisit values, and return (this suspends
143 // the DFS traversal till the next ++).
144 do {
145 CurrentSCC.push_back(SCCNodeStack.back());
146 SCCNodeStack.pop_back();
147 nodeVisitNumbers[CurrentSCC.back()] = ~0U;
148 } while (CurrentSCC.back() != visitingN);
149 return;
150 }
151 }
89 void GetNextSCC();
15290
15391 scc_iterator(NodeType *entryN) : visitNum(0) {
15492 DFSVisitOne(entryN);
199137 ///
200138 /// If the SCC has more than one node, this is trivially true. If not, it may
201139 /// still contain a loop if the node has an edge back to itself.
202 bool hasLoop() const {
140 bool hasLoop() const;
141
142 /// This informs the \c scc_iterator that the specified \c Old node
143 /// has been deleted, and \c New is to be used in its place.
144 void ReplaceNode(NodeType *Old, NodeType *New) {
145 assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
146 nodeVisitNumbers[New] = nodeVisitNumbers[Old];
147 nodeVisitNumbers.erase(Old);
148 }
149 };
150
151 template
152 void scc_iterator::DFSVisitOne(NodeType *N) {
153 ++visitNum;
154 nodeVisitNumbers[N] = visitNum;
155 SCCNodeStack.push_back(N);
156 VisitStack.push_back(StackElement(N, GT::child_begin(N), visitNum));
157 #if 0 // Enable if needed when debugging.
158 dbgs() << "TarjanSCC: Node " << N <<
159 " : visitNum = " << visitNum << "\n";
160 #endif
161 }
162
163 template
164 void scc_iterator::DFSVisitChildren() {
165 assert(!VisitStack.empty());
166 while (VisitStack.back().NextChild != GT::child_end(VisitStack.back().Node)) {
167 // TOS has at least one more child so continue DFS
168 NodeType *childN = *VisitStack.back().NextChild++;
169 typename DenseMap::iterator Visited =
170 nodeVisitNumbers.find(childN);
171 if (Visited == nodeVisitNumbers.end()) {
172 // this node has never been seen.
173 DFSVisitOne(childN);
174 continue;
175 }
176
177 unsigned childNum = Visited->second;
178 if (VisitStack.back().MinVisited > childNum)
179 VisitStack.back().MinVisited = childNum;
180 }
181 }
182
183 template void scc_iterator::GetNextSCC() {
184 CurrentSCC.clear(); // Prepare to compute the next SCC
185 while (!VisitStack.empty()) {
186 DFSVisitChildren();
187
188 // Pop the leaf on top of the VisitStack.
189 NodeType *visitingN = VisitStack.back().Node;
190 unsigned minVisitNum = VisitStack.back().MinVisited;
191 assert(VisitStack.back().NextChild == GT::child_end(visitingN));
192 VisitStack.pop_back();
193
194 // Propagate MinVisitNum to parent so we can detect the SCC starting node.
195 if (!VisitStack.empty() && VisitStack.back().MinVisited > minVisitNum)
196 VisitStack.back().MinVisited = minVisitNum;
197
198 #if 0 // Enable if needed when debugging.
199 dbgs() << "TarjanSCC: Popped node " << visitingN <<
200 " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
201 nodeVisitNumbers[visitingN] << "\n";
202 #endif
203
204 if (minVisitNum != nodeVisitNumbers[visitingN])
205 continue;
206
207 // A full SCC is on the SCCNodeStack! It includes all nodes below
208 // visitingN on the stack. Copy those nodes to CurrentSCC,
209 // reset their minVisit values, and return (this suspends
210 // the DFS traversal till the next ++).
211 do {
212 CurrentSCC.push_back(SCCNodeStack.back());
213 SCCNodeStack.pop_back();
214 nodeVisitNumbers[CurrentSCC.back()] = ~0U;
215 } while (CurrentSCC.back() != visitingN);
216 return;
217 }
218 }
219
220 template
221 bool scc_iterator::hasLoop() const {
203222 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
204223 if (CurrentSCC.size() > 1)
205224 return true;
211230 return false;
212231 }
213232
214 /// This informs the \c scc_iterator that the specified \c Old node
215 /// has been deleted, and \c New is to be used in its place.
216 void ReplaceNode(NodeType *Old, NodeType *New) {
217 assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
218 nodeVisitNumbers[New] = nodeVisitNumbers[Old];
219 nodeVisitNumbers.erase(Old);
220 }
221 };
222
223233 /// \brief Construct the begin iterator for a deduced graph type T.
224234 template scc_iterator scc_begin(const T &G) {
225235 return scc_iterator::begin(G);