xref: /minix3/external/bsd/llvm/dist/llvm/unittests/ADT/SCCIteratorTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===----- llvm/unittest/ADT/SCCIteratorTest.cpp - SCCIterator tests ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "llvm/ADT/SCCIterator.h"
11f4a2713aSLionel Sambuc #include "llvm/ADT/GraphTraits.h"
12f4a2713aSLionel Sambuc #include "gtest/gtest.h"
13f4a2713aSLionel Sambuc #include <limits.h>
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc using namespace llvm;
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc namespace llvm {
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc /// Graph<N> - A graph with N nodes.  Note that N can be at most 8.
20f4a2713aSLionel Sambuc template <unsigned N>
21f4a2713aSLionel Sambuc class Graph {
22f4a2713aSLionel Sambuc private:
23f4a2713aSLionel Sambuc   // Disable copying.
24f4a2713aSLionel Sambuc   Graph(const Graph&);
25f4a2713aSLionel Sambuc   Graph& operator=(const Graph&);
26f4a2713aSLionel Sambuc 
ValidateIndex(unsigned Idx)27f4a2713aSLionel Sambuc   static void ValidateIndex(unsigned Idx) {
28f4a2713aSLionel Sambuc     assert(Idx < N && "Invalid node index!");
29f4a2713aSLionel Sambuc   }
30f4a2713aSLionel Sambuc public:
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc   /// NodeSubset - A subset of the graph's nodes.
33f4a2713aSLionel Sambuc   class NodeSubset {
34f4a2713aSLionel Sambuc     typedef unsigned char BitVector; // Where the limitation N <= 8 comes from.
35f4a2713aSLionel Sambuc     BitVector Elements;
NodeSubset(BitVector e)36f4a2713aSLionel Sambuc     NodeSubset(BitVector e) : Elements(e) {}
37f4a2713aSLionel Sambuc   public:
38f4a2713aSLionel Sambuc     /// NodeSubset - Default constructor, creates an empty subset.
NodeSubset()39f4a2713aSLionel Sambuc     NodeSubset() : Elements(0) {
40f4a2713aSLionel Sambuc       assert(N <= sizeof(BitVector)*CHAR_BIT && "Graph too big!");
41f4a2713aSLionel Sambuc     }
42f4a2713aSLionel Sambuc     /// NodeSubset - Copy constructor.
NodeSubset(const NodeSubset & other)43f4a2713aSLionel Sambuc     NodeSubset(const NodeSubset &other) : Elements(other.Elements) {}
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc     /// Comparison operators.
operator ==(const NodeSubset & other) const46f4a2713aSLionel Sambuc     bool operator==(const NodeSubset &other) const {
47f4a2713aSLionel Sambuc       return other.Elements == this->Elements;
48f4a2713aSLionel Sambuc     }
operator !=(const NodeSubset & other) const49f4a2713aSLionel Sambuc     bool operator!=(const NodeSubset &other) const {
50f4a2713aSLionel Sambuc       return !(*this == other);
51f4a2713aSLionel Sambuc     }
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc     /// AddNode - Add the node with the given index to the subset.
AddNode(unsigned Idx)54f4a2713aSLionel Sambuc     void AddNode(unsigned Idx) {
55f4a2713aSLionel Sambuc       ValidateIndex(Idx);
56f4a2713aSLionel Sambuc       Elements |= 1U << Idx;
57f4a2713aSLionel Sambuc     }
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc     /// DeleteNode - Remove the node with the given index from the subset.
DeleteNode(unsigned Idx)60f4a2713aSLionel Sambuc     void DeleteNode(unsigned Idx) {
61f4a2713aSLionel Sambuc       ValidateIndex(Idx);
62f4a2713aSLionel Sambuc       Elements &= ~(1U << Idx);
63f4a2713aSLionel Sambuc     }
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc     /// count - Return true if the node with the given index is in the subset.
count(unsigned Idx)66f4a2713aSLionel Sambuc     bool count(unsigned Idx) {
67f4a2713aSLionel Sambuc       ValidateIndex(Idx);
68f4a2713aSLionel Sambuc       return (Elements & (1U << Idx)) != 0;
69f4a2713aSLionel Sambuc     }
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc     /// isEmpty - Return true if this is the empty set.
isEmpty() const72f4a2713aSLionel Sambuc     bool isEmpty() const {
73f4a2713aSLionel Sambuc       return Elements == 0;
74f4a2713aSLionel Sambuc     }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc     /// isSubsetOf - Return true if this set is a subset of the given one.
isSubsetOf(const NodeSubset & other) const77f4a2713aSLionel Sambuc     bool isSubsetOf(const NodeSubset &other) const {
78f4a2713aSLionel Sambuc       return (this->Elements | other.Elements) == other.Elements;
79f4a2713aSLionel Sambuc     }
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc     /// Complement - Return the complement of this subset.
Complement() const82f4a2713aSLionel Sambuc     NodeSubset Complement() const {
83f4a2713aSLionel Sambuc       return ~(unsigned)this->Elements & ((1U << N) - 1);
84f4a2713aSLionel Sambuc     }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc     /// Join - Return the union of this subset and the given one.
Join(const NodeSubset & other) const87f4a2713aSLionel Sambuc     NodeSubset Join(const NodeSubset &other) const {
88f4a2713aSLionel Sambuc       return this->Elements | other.Elements;
89f4a2713aSLionel Sambuc     }
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc     /// Meet - Return the intersection of this subset and the given one.
Meet(const NodeSubset & other) const92f4a2713aSLionel Sambuc     NodeSubset Meet(const NodeSubset &other) const {
93f4a2713aSLionel Sambuc       return this->Elements & other.Elements;
94f4a2713aSLionel Sambuc     }
95f4a2713aSLionel Sambuc   };
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   /// NodeType - Node index and set of children of the node.
98f4a2713aSLionel Sambuc   typedef std::pair<unsigned, NodeSubset> NodeType;
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc private:
101f4a2713aSLionel Sambuc   /// Nodes - The list of nodes for this graph.
102f4a2713aSLionel Sambuc   NodeType Nodes[N];
103f4a2713aSLionel Sambuc public:
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   /// Graph - Default constructor.  Creates an empty graph.
Graph()106f4a2713aSLionel Sambuc   Graph() {
107f4a2713aSLionel Sambuc     // Let each node know which node it is.  This allows us to find the start of
108f4a2713aSLionel Sambuc     // the Nodes array given a pointer to any element of it.
109f4a2713aSLionel Sambuc     for (unsigned i = 0; i != N; ++i)
110f4a2713aSLionel Sambuc       Nodes[i].first = i;
111f4a2713aSLionel Sambuc   }
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc   /// AddEdge - Add an edge from the node with index FromIdx to the node with
114f4a2713aSLionel Sambuc   /// index ToIdx.
AddEdge(unsigned FromIdx,unsigned ToIdx)115f4a2713aSLionel Sambuc   void AddEdge(unsigned FromIdx, unsigned ToIdx) {
116f4a2713aSLionel Sambuc     ValidateIndex(FromIdx);
117f4a2713aSLionel Sambuc     Nodes[FromIdx].second.AddNode(ToIdx);
118f4a2713aSLionel Sambuc   }
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc   /// DeleteEdge - Remove the edge (if any) from the node with index FromIdx to
121f4a2713aSLionel Sambuc   /// the node with index ToIdx.
DeleteEdge(unsigned FromIdx,unsigned ToIdx)122f4a2713aSLionel Sambuc   void DeleteEdge(unsigned FromIdx, unsigned ToIdx) {
123f4a2713aSLionel Sambuc     ValidateIndex(FromIdx);
124f4a2713aSLionel Sambuc     Nodes[FromIdx].second.DeleteNode(ToIdx);
125f4a2713aSLionel Sambuc   }
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc   /// AccessNode - Get a pointer to the node with the given index.
AccessNode(unsigned Idx) const128f4a2713aSLionel Sambuc   NodeType *AccessNode(unsigned Idx) const {
129f4a2713aSLionel Sambuc     ValidateIndex(Idx);
130f4a2713aSLionel Sambuc     // The constant cast is needed when working with GraphTraits, which insists
131f4a2713aSLionel Sambuc     // on taking a constant Graph.
132f4a2713aSLionel Sambuc     return const_cast<NodeType *>(&Nodes[Idx]);
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   /// NodesReachableFrom - Return the set of all nodes reachable from the given
136f4a2713aSLionel Sambuc   /// node.
NodesReachableFrom(unsigned Idx) const137f4a2713aSLionel Sambuc   NodeSubset NodesReachableFrom(unsigned Idx) const {
138f4a2713aSLionel Sambuc     // This algorithm doesn't scale, but that doesn't matter given the small
139f4a2713aSLionel Sambuc     // size of our graphs.
140f4a2713aSLionel Sambuc     NodeSubset Reachable;
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc     // The initial node is reachable.
143f4a2713aSLionel Sambuc     Reachable.AddNode(Idx);
144f4a2713aSLionel Sambuc     do {
145f4a2713aSLionel Sambuc       NodeSubset Previous(Reachable);
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc       // Add in all nodes which are children of a reachable node.
148f4a2713aSLionel Sambuc       for (unsigned i = 0; i != N; ++i)
149f4a2713aSLionel Sambuc         if (Previous.count(i))
150f4a2713aSLionel Sambuc           Reachable = Reachable.Join(Nodes[i].second);
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc       // If nothing changed then we have found all reachable nodes.
153f4a2713aSLionel Sambuc       if (Reachable == Previous)
154f4a2713aSLionel Sambuc         return Reachable;
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc       // Rinse and repeat.
157f4a2713aSLionel Sambuc     } while (1);
158f4a2713aSLionel Sambuc   }
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc   /// ChildIterator - Visit all children of a node.
161f4a2713aSLionel Sambuc   class ChildIterator {
162f4a2713aSLionel Sambuc     friend class Graph;
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc     /// FirstNode - Pointer to first node in the graph's Nodes array.
165f4a2713aSLionel Sambuc     NodeType *FirstNode;
166f4a2713aSLionel Sambuc     /// Children - Set of nodes which are children of this one and that haven't
167f4a2713aSLionel Sambuc     /// yet been visited.
168f4a2713aSLionel Sambuc     NodeSubset Children;
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc     ChildIterator(); // Disable default constructor.
171f4a2713aSLionel Sambuc   protected:
ChildIterator(NodeType * F,NodeSubset C)172f4a2713aSLionel Sambuc     ChildIterator(NodeType *F, NodeSubset C) : FirstNode(F), Children(C) {}
173f4a2713aSLionel Sambuc 
174f4a2713aSLionel Sambuc   public:
175f4a2713aSLionel Sambuc     /// ChildIterator - Copy constructor.
ChildIterator(const ChildIterator & other)176f4a2713aSLionel Sambuc     ChildIterator(const ChildIterator& other) : FirstNode(other.FirstNode),
177f4a2713aSLionel Sambuc       Children(other.Children) {}
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc     /// Comparison operators.
operator ==(const ChildIterator & other) const180f4a2713aSLionel Sambuc     bool operator==(const ChildIterator &other) const {
181f4a2713aSLionel Sambuc       return other.FirstNode == this->FirstNode &&
182f4a2713aSLionel Sambuc         other.Children == this->Children;
183f4a2713aSLionel Sambuc     }
operator !=(const ChildIterator & other) const184f4a2713aSLionel Sambuc     bool operator!=(const ChildIterator &other) const {
185f4a2713aSLionel Sambuc       return !(*this == other);
186f4a2713aSLionel Sambuc     }
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc     /// Prefix increment operator.
operator ++()189f4a2713aSLionel Sambuc     ChildIterator& operator++() {
190f4a2713aSLionel Sambuc       // Find the next unvisited child node.
191f4a2713aSLionel Sambuc       for (unsigned i = 0; i != N; ++i)
192f4a2713aSLionel Sambuc         if (Children.count(i)) {
193f4a2713aSLionel Sambuc           // Remove that child - it has been visited.  This is the increment!
194f4a2713aSLionel Sambuc           Children.DeleteNode(i);
195f4a2713aSLionel Sambuc           return *this;
196f4a2713aSLionel Sambuc         }
197f4a2713aSLionel Sambuc       assert(false && "Incrementing end iterator!");
198f4a2713aSLionel Sambuc       return *this; // Avoid compiler warnings.
199f4a2713aSLionel Sambuc     }
200f4a2713aSLionel Sambuc 
201f4a2713aSLionel Sambuc     /// Postfix increment operator.
operator ++(int)202f4a2713aSLionel Sambuc     ChildIterator operator++(int) {
203f4a2713aSLionel Sambuc       ChildIterator Result(*this);
204f4a2713aSLionel Sambuc       ++(*this);
205f4a2713aSLionel Sambuc       return Result;
206f4a2713aSLionel Sambuc     }
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc     /// Dereference operator.
operator *()209f4a2713aSLionel Sambuc     NodeType *operator*() {
210f4a2713aSLionel Sambuc       // Find the next unvisited child node.
211f4a2713aSLionel Sambuc       for (unsigned i = 0; i != N; ++i)
212f4a2713aSLionel Sambuc         if (Children.count(i))
213f4a2713aSLionel Sambuc           // Return a pointer to it.
214f4a2713aSLionel Sambuc           return FirstNode + i;
215f4a2713aSLionel Sambuc       assert(false && "Dereferencing end iterator!");
216*0a6a1f1dSLionel Sambuc       return nullptr; // Avoid compiler warning.
217f4a2713aSLionel Sambuc     }
218f4a2713aSLionel Sambuc   };
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc   /// child_begin - Return an iterator pointing to the first child of the given
221f4a2713aSLionel Sambuc   /// node.
child_begin(NodeType * Parent)222f4a2713aSLionel Sambuc   static ChildIterator child_begin(NodeType *Parent) {
223f4a2713aSLionel Sambuc     return ChildIterator(Parent - Parent->first, Parent->second);
224f4a2713aSLionel Sambuc   }
225f4a2713aSLionel Sambuc 
226f4a2713aSLionel Sambuc   /// child_end - Return the end iterator for children of the given node.
child_end(NodeType * Parent)227f4a2713aSLionel Sambuc   static ChildIterator child_end(NodeType *Parent) {
228f4a2713aSLionel Sambuc     return ChildIterator(Parent - Parent->first, NodeSubset());
229f4a2713aSLionel Sambuc   }
230f4a2713aSLionel Sambuc };
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc template <unsigned N>
233f4a2713aSLionel Sambuc struct GraphTraits<Graph<N> > {
234f4a2713aSLionel Sambuc   typedef typename Graph<N>::NodeType NodeType;
235f4a2713aSLionel Sambuc   typedef typename Graph<N>::ChildIterator ChildIteratorType;
236f4a2713aSLionel Sambuc 
getEntryNodellvm::GraphTraits237f4a2713aSLionel Sambuc  static inline NodeType *getEntryNode(const Graph<N> &G) { return G.AccessNode(0); }
child_beginllvm::GraphTraits238f4a2713aSLionel Sambuc  static inline ChildIteratorType child_begin(NodeType *Node) {
239f4a2713aSLionel Sambuc    return Graph<N>::child_begin(Node);
240f4a2713aSLionel Sambuc  }
child_endllvm::GraphTraits241f4a2713aSLionel Sambuc  static inline ChildIteratorType child_end(NodeType *Node) {
242f4a2713aSLionel Sambuc    return Graph<N>::child_end(Node);
243f4a2713aSLionel Sambuc  }
244f4a2713aSLionel Sambuc };
245f4a2713aSLionel Sambuc 
TEST(SCCIteratorTest,AllSmallGraphs)246f4a2713aSLionel Sambuc TEST(SCCIteratorTest, AllSmallGraphs) {
247f4a2713aSLionel Sambuc   // Test SCC computation against every graph with NUM_NODES nodes or less.
248f4a2713aSLionel Sambuc   // Since SCC considers every node to have an implicit self-edge, we only
249f4a2713aSLionel Sambuc   // create graphs for which every node has a self-edge.
250f4a2713aSLionel Sambuc #define NUM_NODES 4
251f4a2713aSLionel Sambuc #define NUM_GRAPHS (NUM_NODES * (NUM_NODES - 1))
252f4a2713aSLionel Sambuc   typedef Graph<NUM_NODES> GT;
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc   /// Enumerate all graphs using NUM_GRAPHS bits.
255f4a2713aSLionel Sambuc   assert(NUM_GRAPHS < sizeof(unsigned) * CHAR_BIT && "Too many graphs!");
256f4a2713aSLionel Sambuc   for (unsigned GraphDescriptor = 0; GraphDescriptor < (1U << NUM_GRAPHS);
257f4a2713aSLionel Sambuc        ++GraphDescriptor) {
258f4a2713aSLionel Sambuc     GT G;
259f4a2713aSLionel Sambuc 
260f4a2713aSLionel Sambuc     // Add edges as specified by the descriptor.
261f4a2713aSLionel Sambuc     unsigned DescriptorCopy = GraphDescriptor;
262f4a2713aSLionel Sambuc     for (unsigned i = 0; i != NUM_NODES; ++i)
263f4a2713aSLionel Sambuc       for (unsigned j = 0; j != NUM_NODES; ++j) {
264f4a2713aSLionel Sambuc         // Always add a self-edge.
265f4a2713aSLionel Sambuc         if (i == j) {
266f4a2713aSLionel Sambuc           G.AddEdge(i, j);
267f4a2713aSLionel Sambuc           continue;
268f4a2713aSLionel Sambuc         }
269f4a2713aSLionel Sambuc         if (DescriptorCopy & 1)
270f4a2713aSLionel Sambuc           G.AddEdge(i, j);
271f4a2713aSLionel Sambuc         DescriptorCopy >>= 1;
272f4a2713aSLionel Sambuc       }
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc     // Test the SCC logic on this graph.
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc     /// NodesInSomeSCC - Those nodes which are in some SCC.
277f4a2713aSLionel Sambuc     GT::NodeSubset NodesInSomeSCC;
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc     for (scc_iterator<GT> I = scc_begin(G), E = scc_end(G); I != E; ++I) {
280*0a6a1f1dSLionel Sambuc       const std::vector<GT::NodeType *> &SCC = *I;
281f4a2713aSLionel Sambuc 
282f4a2713aSLionel Sambuc       // Get the nodes in this SCC as a NodeSubset rather than a vector.
283f4a2713aSLionel Sambuc       GT::NodeSubset NodesInThisSCC;
284f4a2713aSLionel Sambuc       for (unsigned i = 0, e = SCC.size(); i != e; ++i)
285f4a2713aSLionel Sambuc         NodesInThisSCC.AddNode(SCC[i]->first);
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc       // There should be at least one node in every SCC.
288f4a2713aSLionel Sambuc       EXPECT_FALSE(NodesInThisSCC.isEmpty());
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc       // Check that every node in the SCC is reachable from every other node in
291f4a2713aSLionel Sambuc       // the SCC.
292f4a2713aSLionel Sambuc       for (unsigned i = 0; i != NUM_NODES; ++i)
293f4a2713aSLionel Sambuc         if (NodesInThisSCC.count(i))
294f4a2713aSLionel Sambuc           EXPECT_TRUE(NodesInThisSCC.isSubsetOf(G.NodesReachableFrom(i)));
295f4a2713aSLionel Sambuc 
296f4a2713aSLionel Sambuc       // OK, now that we now that every node in the SCC is reachable from every
297f4a2713aSLionel Sambuc       // other, this means that the set of nodes reachable from any node in the
298f4a2713aSLionel Sambuc       // SCC is the same as the set of nodes reachable from every node in the
299f4a2713aSLionel Sambuc       // SCC.  Check that for every node N not in the SCC but reachable from the
300f4a2713aSLionel Sambuc       // SCC, no element of the SCC is reachable from N.
301f4a2713aSLionel Sambuc       for (unsigned i = 0; i != NUM_NODES; ++i)
302f4a2713aSLionel Sambuc         if (NodesInThisSCC.count(i)) {
303f4a2713aSLionel Sambuc           GT::NodeSubset NodesReachableFromSCC = G.NodesReachableFrom(i);
304f4a2713aSLionel Sambuc           GT::NodeSubset ReachableButNotInSCC =
305f4a2713aSLionel Sambuc             NodesReachableFromSCC.Meet(NodesInThisSCC.Complement());
306f4a2713aSLionel Sambuc 
307f4a2713aSLionel Sambuc           for (unsigned j = 0; j != NUM_NODES; ++j)
308f4a2713aSLionel Sambuc             if (ReachableButNotInSCC.count(j))
309f4a2713aSLionel Sambuc               EXPECT_TRUE(G.NodesReachableFrom(j).Meet(NodesInThisSCC).isEmpty());
310f4a2713aSLionel Sambuc 
311f4a2713aSLionel Sambuc           // The result must be the same for all other nodes in this SCC, so
312f4a2713aSLionel Sambuc           // there is no point in checking them.
313f4a2713aSLionel Sambuc           break;
314f4a2713aSLionel Sambuc         }
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc       // This is indeed a SCC: a maximal set of nodes for which each node is
317f4a2713aSLionel Sambuc       // reachable from every other.
318f4a2713aSLionel Sambuc 
319f4a2713aSLionel Sambuc       // Check that we didn't already see this SCC.
320f4a2713aSLionel Sambuc       EXPECT_TRUE(NodesInSomeSCC.Meet(NodesInThisSCC).isEmpty());
321f4a2713aSLionel Sambuc 
322f4a2713aSLionel Sambuc       NodesInSomeSCC = NodesInSomeSCC.Join(NodesInThisSCC);
323f4a2713aSLionel Sambuc 
324f4a2713aSLionel Sambuc       // Check a property that is specific to the LLVM SCC iterator and
325f4a2713aSLionel Sambuc       // guaranteed by it: if a node in SCC S1 has an edge to a node in
326f4a2713aSLionel Sambuc       // SCC S2, then S1 is visited *after* S2.  This means that the set
327f4a2713aSLionel Sambuc       // of nodes reachable from this SCC must be contained either in the
328f4a2713aSLionel Sambuc       // union of this SCC and all previously visited SCC's.
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc       for (unsigned i = 0; i != NUM_NODES; ++i)
331f4a2713aSLionel Sambuc         if (NodesInThisSCC.count(i)) {
332f4a2713aSLionel Sambuc           GT::NodeSubset NodesReachableFromSCC = G.NodesReachableFrom(i);
333f4a2713aSLionel Sambuc           EXPECT_TRUE(NodesReachableFromSCC.isSubsetOf(NodesInSomeSCC));
334f4a2713aSLionel Sambuc           // The result must be the same for all other nodes in this SCC, so
335f4a2713aSLionel Sambuc           // there is no point in checking them.
336f4a2713aSLionel Sambuc           break;
337f4a2713aSLionel Sambuc         }
338f4a2713aSLionel Sambuc     }
339f4a2713aSLionel Sambuc 
340f4a2713aSLionel Sambuc     // Finally, check that the nodes in some SCC are exactly those that are
341f4a2713aSLionel Sambuc     // reachable from the initial node.
342f4a2713aSLionel Sambuc     EXPECT_EQ(NodesInSomeSCC, G.NodesReachableFrom(0));
343f4a2713aSLionel Sambuc   }
344f4a2713aSLionel Sambuc }
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc }
347