Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:07

0001 //===- ADT/SCCIterator.h - Strongly Connected Comp. Iter. -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// This builds on the llvm/ADT/GraphTraits.h file to find the strongly
0011 /// connected components (SCCs) of a graph in O(N+E) time using Tarjan's DFS
0012 /// algorithm.
0013 ///
0014 /// The SCC iterator has the important property that if a node in SCC S1 has an
0015 /// edge to a node in SCC S2, then it visits S1 *after* S2.
0016 ///
0017 /// To visit S1 *before* S2, use the scc_iterator on the Inverse graph. (NOTE:
0018 /// This requires some simple wrappers and is not supported yet.)
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 /// Enumerate the SCCs of a directed graph in reverse topological order
0041 /// of the SCC DAG.
0042 ///
0043 /// This is implemented using Tarjan's DFS algorithm using an internal stack to
0044 /// build up a vector of nodes in a particular SCC. Note that it is a forward
0045 /// iterator and thus you cannot backtrack or re-visit nodes.
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   /// Element of VisitStack during DFS.
0056   struct StackElement {
0057     NodeRef Node;         ///< The current node pointer.
0058     ChildItTy NextChild;  ///< The next child, modified inplace during DFS.
0059     unsigned MinVisited;  ///< Minimum uplink value of all children of Node.
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   /// The visit counters used to detect when a complete SCC is on the stack.
0072   /// visitNum is the global counter.
0073   ///
0074   /// nodeVisitNumbers are per-node visit numbers, also used as DFS flags.
0075   unsigned visitNum;
0076   DenseMap<NodeRef, unsigned> nodeVisitNumbers;
0077 
0078   /// Stack holding nodes of the SCC.
0079   std::vector<NodeRef> SCCNodeStack;
0080 
0081   /// The current SCC, retrieved using operator*().
0082   SccTy CurrentSCC;
0083 
0084   /// DFS stack, Used to maintain the ordering.  The top contains the current
0085   /// node, the next child to visit, and the minimum uplink value of all child
0086   std::vector<StackElement> VisitStack;
0087 
0088   /// A single "visit" within the non-recursive DFS traversal.
0089   void DFSVisitOne(NodeRef N);
0090 
0091   /// The stack-based DFS traversal; defined below.
0092   void DFSVisitChildren();
0093 
0094   /// Compute the next SCC using the DFS traversal.
0095   void GetNextSCC();
0096 
0097   scc_iterator(NodeRef entryN) : visitNum(0) {
0098     DFSVisitOne(entryN);
0099     GetNextSCC();
0100   }
0101 
0102   /// End is when the DFS stack is empty.
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   /// Direct loop termination test which is more efficient than
0112   /// comparison with \c end().
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   /// Test if the current SCC has a cycle.
0133   ///
0134   /// If the SCC has more than one node, this is trivially true.  If not, it may
0135   /// still contain a cycle if the node has an edge back to itself.
0136   bool hasCycle() const;
0137 
0138   /// This informs the \c scc_iterator that the specified \c Old node
0139   /// has been deleted, and \c New is to be used in its place.
0140   void ReplaceNode(NodeRef Old, NodeRef New) {
0141     assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
0142     // Do the assignment in two steps, in case 'New' is not yet in the map, and
0143     // inserting it causes the map to grow.
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 // Enable if needed when debugging.
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     // TOS has at least one more child so continue DFS
0167     NodeRef childN = *VisitStack.back().NextChild++;
0168     typename DenseMap<NodeRef, unsigned>::iterator Visited =
0169         nodeVisitNumbers.find(childN);
0170     if (Visited == nodeVisitNumbers.end()) {
0171       // this node has never been seen.
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(); // Prepare to compute the next SCC
0184   while (!VisitStack.empty()) {
0185     DFSVisitChildren();
0186 
0187     // Pop the leaf on top of the VisitStack.
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     // Propagate MinVisitNum to parent so we can detect the SCC starting node.
0194     if (!VisitStack.empty() && VisitStack.back().MinVisited > minVisitNum)
0195       VisitStack.back().MinVisited = minVisitNum;
0196 
0197 #if 0 // Enable if needed when debugging.
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     // A full SCC is on the SCCNodeStack!  It includes all nodes below
0207     // visitingN on the stack.  Copy those nodes to CurrentSCC,
0208     // reset their minVisit values, and return (this suspends
0209     // the DFS traversal till the next ++).
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 /// Construct the begin iterator for a deduced graph type T.
0233 template <class T> scc_iterator<T> scc_begin(const T &G) {
0234   return scc_iterator<T>::begin(G);
0235 }
0236 
0237 /// Construct the end iterator for a deduced graph type T.
0238 template <class T> scc_iterator<T> scc_end(const T &G) {
0239   return scc_iterator<T>::end(G);
0240 }
0241 
0242 /// Sort the nodes of a directed SCC in the decreasing order of the edge
0243 /// weights. The instantiating GraphT type should have weighted edge type
0244 /// declared in its graph traits in order to use this iterator.
0245 ///
0246 /// This is implemented using Kruskal's minimal spanning tree algorithm followed
0247 /// by Kahn's algorithm to compute a topological order on the MST. First a
0248 /// maximum spanning tree (forest) is built based on all edges within the SCC
0249 /// collection. Then a topological walk is initiated on tree nodes that do not
0250 /// have a predecessor and then applied to all nodes of the SCC. Such order
0251 /// ensures that high-weighted edges are visited first during the traversal.
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   // Auxilary node information used during the MST calculation.
0259   struct NodeInfo {
0260     NodeInfo *Group = this;
0261     uint32_t Rank = 0;
0262     bool Visited = false;
0263     DenseSet<const EdgeType *> IncomingMSTEdges;
0264   };
0265 
0266   // Find the root group of the node and compress the path from node to the
0267   // root.
0268   NodeInfo *find(NodeInfo *Node) {
0269     if (Node->Group != Node)
0270       Node->Group = find(Node->Group);
0271     return Node->Group;
0272   }
0273 
0274   // Union the source and target node into the same group and return true.
0275   // Returns false if they are already in the same group.
0276   bool unionGroups(const EdgeType *Edge) {
0277     NodeInfo *G1 = find(&NodeInfoMap[Edge->Source]);
0278     NodeInfo *G2 = find(&NodeInfoMap[Edge->Target]);
0279 
0280     // If the edge forms a cycle, do not add it to MST
0281     if (G1 == G2)
0282       return false;
0283 
0284     // Make the smaller rank tree a direct child of high rank tree.
0285     if (G1->Rank < G2->Rank)
0286       G1->Group = G2;
0287     else {
0288       G2->Group = G1;
0289       // If the ranks are the same, increment root of one tree by one.
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   // Initialize auxilary node information.
0314   NodeInfoMap.clear();
0315   for (auto *Node : InputNodes) {
0316     // This is specifically used to construct a `NodeInfo` object in place. An
0317     // insert operation will involve a copy construction which invalidate the
0318     // initial value of the `Group` field which should be `this`.
0319     (void)NodeInfoMap[Node].Group;
0320   }
0321 
0322   // Sort edges by weights.
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   // Traverse all the edges and compute the Maximum Weight Spanning Tree
0338   // using Kruskal's algorithm.
0339   std::unordered_set<const EdgeType *> MSTEdges;
0340   for (auto *Edge : SortedEdges) {
0341     if (unionGroups(Edge))
0342       MSTEdges.insert(Edge);
0343   }
0344 
0345   // Run Kahn's algorithm on MST to compute a topological traversal order.
0346   // The algorithm starts from nodes that have no incoming edge. These nodes are
0347   // "roots" of the MST forest. This ensures that nodes are visited before their
0348   // descendants are, thus ensures hot edges are processed before cold edges,
0349   // based on how MST is computed.
0350   std::queue<NodeType *> Queue;
0351   for (const auto *Edge : MSTEdges)
0352     NodeInfoMap[Edge->Target].IncomingMSTEdges.insert(Edge);
0353 
0354   // Walk through SortedEdges to initialize the queue, instead of using NodeInfoMap
0355   // to ensure an ordered deterministic push.
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 } // end namespace llvm
0381 
0382 #endif // LLVM_ADT_SCCITERATOR_H