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