File indexing completed on 2026-05-10 08:43:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef LLVM_ADT_SCCITERATOR_H
0023 #define LLVM_ADT_SCCITERATOR_H
0024
0025 #include "llvm/ADT/DenseMap.h"
0026 #include "llvm/ADT/DenseSet.h"
0027 #include "llvm/ADT/GraphTraits.h"
0028 #include "llvm/ADT/iterator.h"
0029 #include <cassert>
0030 #include <cstddef>
0031 #include <iterator>
0032 #include <queue>
0033 #include <set>
0034 #include <unordered_map>
0035 #include <unordered_set>
0036 #include <vector>
0037
0038 namespace llvm {
0039
0040
0041
0042
0043
0044
0045
0046 template <class GraphT, class GT = GraphTraits<GraphT>>
0047 class scc_iterator : public iterator_facade_base<
0048 scc_iterator<GraphT, GT>, std::forward_iterator_tag,
0049 const std::vector<typename GT::NodeRef>, ptrdiff_t> {
0050 using NodeRef = typename GT::NodeRef;
0051 using ChildItTy = typename GT::ChildIteratorType;
0052 using SccTy = std::vector<NodeRef>;
0053 using reference = typename scc_iterator::reference;
0054
0055
0056 struct StackElement {
0057 NodeRef Node;
0058 ChildItTy NextChild;
0059 unsigned MinVisited;
0060
0061 StackElement(NodeRef Node, const ChildItTy &Child, unsigned Min)
0062 : Node(Node), NextChild(Child), MinVisited(Min) {}
0063
0064 bool operator==(const StackElement &Other) const {
0065 return Node == Other.Node &&
0066 NextChild == Other.NextChild &&
0067 MinVisited == Other.MinVisited;
0068 }
0069 };
0070
0071
0072
0073
0074
0075 unsigned visitNum;
0076 DenseMap<NodeRef, unsigned> nodeVisitNumbers;
0077
0078
0079 std::vector<NodeRef> SCCNodeStack;
0080
0081
0082 SccTy CurrentSCC;
0083
0084
0085
0086 std::vector<StackElement> VisitStack;
0087
0088
0089 void DFSVisitOne(NodeRef N);
0090
0091
0092 void DFSVisitChildren();
0093
0094
0095 void GetNextSCC();
0096
0097 scc_iterator(NodeRef entryN) : visitNum(0) {
0098 DFSVisitOne(entryN);
0099 GetNextSCC();
0100 }
0101
0102
0103 scc_iterator() = default;
0104
0105 public:
0106 static scc_iterator begin(const GraphT &G) {
0107 return scc_iterator(GT::getEntryNode(G));
0108 }
0109 static scc_iterator end(const GraphT &) { return scc_iterator(); }
0110
0111
0112
0113 bool isAtEnd() const {
0114 assert(!CurrentSCC.empty() || VisitStack.empty());
0115 return CurrentSCC.empty();
0116 }
0117
0118 bool operator==(const scc_iterator &x) const {
0119 return VisitStack == x.VisitStack && CurrentSCC == x.CurrentSCC;
0120 }
0121
0122 scc_iterator &operator++() {
0123 GetNextSCC();
0124 return *this;
0125 }
0126
0127 reference operator*() const {
0128 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
0129 return CurrentSCC;
0130 }
0131
0132
0133
0134
0135
0136 bool hasCycle() const;
0137
0138
0139
0140 void ReplaceNode(NodeRef Old, NodeRef New) {
0141 assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
0142
0143
0144 auto tempVal = nodeVisitNumbers[Old];
0145 nodeVisitNumbers[New] = tempVal;
0146 nodeVisitNumbers.erase(Old);
0147 }
0148 };
0149
0150 template <class GraphT, class GT>
0151 void scc_iterator<GraphT, GT>::DFSVisitOne(NodeRef N) {
0152 ++visitNum;
0153 nodeVisitNumbers[N] = visitNum;
0154 SCCNodeStack.push_back(N);
0155 VisitStack.push_back(StackElement(N, GT::child_begin(N), visitNum));
0156 #if 0
0157 dbgs() << "TarjanSCC: Node " << N <<
0158 " : visitNum = " << visitNum << "\n";
0159 #endif
0160 }
0161
0162 template <class GraphT, class GT>
0163 void scc_iterator<GraphT, GT>::DFSVisitChildren() {
0164 assert(!VisitStack.empty());
0165 while (VisitStack.back().NextChild != GT::child_end(VisitStack.back().Node)) {
0166
0167 NodeRef childN = *VisitStack.back().NextChild++;
0168 typename DenseMap<NodeRef, unsigned>::iterator Visited =
0169 nodeVisitNumbers.find(childN);
0170 if (Visited == nodeVisitNumbers.end()) {
0171
0172 DFSVisitOne(childN);
0173 continue;
0174 }
0175
0176 unsigned childNum = Visited->second;
0177 if (VisitStack.back().MinVisited > childNum)
0178 VisitStack.back().MinVisited = childNum;
0179 }
0180 }
0181
0182 template <class GraphT, class GT> void scc_iterator<GraphT, GT>::GetNextSCC() {
0183 CurrentSCC.clear();
0184 while (!VisitStack.empty()) {
0185 DFSVisitChildren();
0186
0187
0188 NodeRef visitingN = VisitStack.back().Node;
0189 unsigned minVisitNum = VisitStack.back().MinVisited;
0190 assert(VisitStack.back().NextChild == GT::child_end(visitingN));
0191 VisitStack.pop_back();
0192
0193
0194 if (!VisitStack.empty() && VisitStack.back().MinVisited > minVisitNum)
0195 VisitStack.back().MinVisited = minVisitNum;
0196
0197 #if 0
0198 dbgs() << "TarjanSCC: Popped node " << visitingN <<
0199 " : minVisitNum = " << minVisitNum << "; Node visit num = " <<
0200 nodeVisitNumbers[visitingN] << "\n";
0201 #endif
0202
0203 if (minVisitNum != nodeVisitNumbers[visitingN])
0204 continue;
0205
0206
0207
0208
0209
0210 do {
0211 CurrentSCC.push_back(SCCNodeStack.back());
0212 SCCNodeStack.pop_back();
0213 nodeVisitNumbers[CurrentSCC.back()] = ~0U;
0214 } while (CurrentSCC.back() != visitingN);
0215 return;
0216 }
0217 }
0218
0219 template <class GraphT, class GT>
0220 bool scc_iterator<GraphT, GT>::hasCycle() const {
0221 assert(!CurrentSCC.empty() && "Dereferencing END SCC iterator!");
0222 if (CurrentSCC.size() > 1)
0223 return true;
0224 NodeRef N = CurrentSCC.front();
0225 for (ChildItTy CI = GT::child_begin(N), CE = GT::child_end(N); CI != CE;
0226 ++CI)
0227 if (*CI == N)
0228 return true;
0229 return false;
0230 }
0231
0232
0233 template <class T> scc_iterator<T> scc_begin(const T &G) {
0234 return scc_iterator<T>::begin(G);
0235 }
0236
0237
0238 template <class T> scc_iterator<T> scc_end(const T &G) {
0239 return scc_iterator<T>::end(G);
0240 }
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 template <class GraphT, class GT = GraphTraits<GraphT>>
0253 class scc_member_iterator {
0254 using NodeType = typename GT::NodeType;
0255 using EdgeType = typename GT::EdgeType;
0256 using NodesType = std::vector<NodeType *>;
0257
0258
0259 struct NodeInfo {
0260 NodeInfo *Group = this;
0261 uint32_t Rank = 0;
0262 bool Visited = false;
0263 DenseSet<const EdgeType *> IncomingMSTEdges;
0264 };
0265
0266
0267
0268 NodeInfo *find(NodeInfo *Node) {
0269 if (Node->Group != Node)
0270 Node->Group = find(Node->Group);
0271 return Node->Group;
0272 }
0273
0274
0275
0276 bool unionGroups(const EdgeType *Edge) {
0277 NodeInfo *G1 = find(&NodeInfoMap[Edge->Source]);
0278 NodeInfo *G2 = find(&NodeInfoMap[Edge->Target]);
0279
0280
0281 if (G1 == G2)
0282 return false;
0283
0284
0285 if (G1->Rank < G2->Rank)
0286 G1->Group = G2;
0287 else {
0288 G2->Group = G1;
0289
0290 if (G1->Rank == G2->Rank)
0291 G1->Rank++;
0292 }
0293 return true;
0294 }
0295
0296 std::unordered_map<NodeType *, NodeInfo> NodeInfoMap;
0297 NodesType Nodes;
0298
0299 public:
0300 scc_member_iterator(const NodesType &InputNodes);
0301
0302 NodesType &operator*() { return Nodes; }
0303 };
0304
0305 template <class GraphT, class GT>
0306 scc_member_iterator<GraphT, GT>::scc_member_iterator(
0307 const NodesType &InputNodes) {
0308 if (InputNodes.size() <= 1) {
0309 Nodes = InputNodes;
0310 return;
0311 }
0312
0313
0314 NodeInfoMap.clear();
0315 for (auto *Node : InputNodes) {
0316
0317
0318
0319 (void)NodeInfoMap[Node].Group;
0320 }
0321
0322
0323 struct EdgeComparer {
0324 bool operator()(const EdgeType *L, const EdgeType *R) const {
0325 return L->Weight > R->Weight;
0326 }
0327 };
0328
0329 std::multiset<const EdgeType *, EdgeComparer> SortedEdges;
0330 for (auto *Node : InputNodes) {
0331 for (auto &Edge : Node->Edges) {
0332 if (NodeInfoMap.count(Edge.Target))
0333 SortedEdges.insert(&Edge);
0334 }
0335 }
0336
0337
0338
0339 std::unordered_set<const EdgeType *> MSTEdges;
0340 for (auto *Edge : SortedEdges) {
0341 if (unionGroups(Edge))
0342 MSTEdges.insert(Edge);
0343 }
0344
0345
0346
0347
0348
0349
0350 std::queue<NodeType *> Queue;
0351 for (const auto *Edge : MSTEdges)
0352 NodeInfoMap[Edge->Target].IncomingMSTEdges.insert(Edge);
0353
0354
0355
0356 for (auto *Edge : SortedEdges) {
0357 if (!NodeInfoMap[Edge->Source].Visited &&
0358 NodeInfoMap[Edge->Source].IncomingMSTEdges.empty()) {
0359 Queue.push(Edge->Source);
0360 NodeInfoMap[Edge->Source].Visited = true;
0361 }
0362 }
0363
0364 while (!Queue.empty()) {
0365 auto *Node = Queue.front();
0366 Queue.pop();
0367 Nodes.push_back(Node);
0368 for (auto &Edge : Node->Edges) {
0369 NodeInfoMap[Edge.Target].IncomingMSTEdges.erase(&Edge);
0370 if (MSTEdges.count(&Edge) &&
0371 NodeInfoMap[Edge.Target].IncomingMSTEdges.empty()) {
0372 Queue.push(Edge.Target);
0373 }
0374 }
0375 }
0376
0377 assert(InputNodes.size() == Nodes.size() && "missing nodes in MST");
0378 std::reverse(Nodes.begin(), Nodes.end());
0379 }
0380 }
0381
0382 #endif