blob: 48350959d046ba434b3b77f91893b4fc0ccd01a4 [file] [log] [blame]
Duncan Sandsdea551c2011-07-28 14:17:11 +00001//===----- llvm/unittest/ADT/SCCIteratorTest.cpp - SCCIterator tests ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Duncan Sandsdea551c2011-07-28 14:17:11 +00006//
7//===----------------------------------------------------------------------===//
8
Duncan Sandsdea551c2011-07-28 14:17:11 +00009#include "llvm/ADT/SCCIterator.h"
Tim Shen608ca252016-08-22 21:59:26 +000010#include "TestGraph.h"
Chandler Carruth9a67b072017-06-06 11:06:56 +000011#include "gtest/gtest.h"
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000012#include <limits.h>
Duncan Sandsdea551c2011-07-28 14:17:11 +000013
14using namespace llvm;
15
16namespace llvm {
17
Duncan Sandsdea551c2011-07-28 14:17:11 +000018TEST(SCCIteratorTest, AllSmallGraphs) {
19 // Test SCC computation against every graph with NUM_NODES nodes or less.
20 // Since SCC considers every node to have an implicit self-edge, we only
21 // create graphs for which every node has a self-edge.
22#define NUM_NODES 4
23#define NUM_GRAPHS (NUM_NODES * (NUM_NODES - 1))
Duncan Sandsbe64bbf2011-07-29 07:50:02 +000024 typedef Graph<NUM_NODES> GT;
Duncan Sandsdea551c2011-07-28 14:17:11 +000025
Duncan Sandsbe64bbf2011-07-29 07:50:02 +000026 /// Enumerate all graphs using NUM_GRAPHS bits.
Gabor Horvathfee04342015-03-16 09:53:42 +000027 static_assert(NUM_GRAPHS < sizeof(unsigned) * CHAR_BIT, "Too many graphs!");
Duncan Sandsbe64bbf2011-07-29 07:50:02 +000028 for (unsigned GraphDescriptor = 0; GraphDescriptor < (1U << NUM_GRAPHS);
29 ++GraphDescriptor) {
Duncan Sandsdea551c2011-07-28 14:17:11 +000030 GT G;
31
32 // Add edges as specified by the descriptor.
Duncan Sands251daee2011-07-28 14:37:53 +000033 unsigned DescriptorCopy = GraphDescriptor;
Duncan Sandsdea551c2011-07-28 14:17:11 +000034 for (unsigned i = 0; i != NUM_NODES; ++i)
35 for (unsigned j = 0; j != NUM_NODES; ++j) {
36 // Always add a self-edge.
37 if (i == j) {
38 G.AddEdge(i, j);
39 continue;
40 }
41 if (DescriptorCopy & 1)
42 G.AddEdge(i, j);
43 DescriptorCopy >>= 1;
44 }
45
46 // Test the SCC logic on this graph.
47
48 /// NodesInSomeSCC - Those nodes which are in some SCC.
49 GT::NodeSubset NodesInSomeSCC;
50
51 for (scc_iterator<GT> I = scc_begin(G), E = scc_end(G); I != E; ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +000052 const std::vector<GT::NodeType *> &SCC = *I;
Duncan Sandsdea551c2011-07-28 14:17:11 +000053
54 // Get the nodes in this SCC as a NodeSubset rather than a vector.
55 GT::NodeSubset NodesInThisSCC;
56 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
57 NodesInThisSCC.AddNode(SCC[i]->first);
58
59 // There should be at least one node in every SCC.
60 EXPECT_FALSE(NodesInThisSCC.isEmpty());
61
62 // Check that every node in the SCC is reachable from every other node in
63 // the SCC.
64 for (unsigned i = 0; i != NUM_NODES; ++i)
Galina Kistanovafcae62d2017-06-15 21:00:40 +000065 if (NodesInThisSCC.count(i)) {
Duncan Sandsdea551c2011-07-28 14:17:11 +000066 EXPECT_TRUE(NodesInThisSCC.isSubsetOf(G.NodesReachableFrom(i)));
Galina Kistanovafcae62d2017-06-15 21:00:40 +000067 }
Duncan Sandsdea551c2011-07-28 14:17:11 +000068
69 // OK, now that we now that every node in the SCC is reachable from every
70 // other, this means that the set of nodes reachable from any node in the
71 // SCC is the same as the set of nodes reachable from every node in the
72 // SCC. Check that for every node N not in the SCC but reachable from the
73 // SCC, no element of the SCC is reachable from N.
74 for (unsigned i = 0; i != NUM_NODES; ++i)
75 if (NodesInThisSCC.count(i)) {
76 GT::NodeSubset NodesReachableFromSCC = G.NodesReachableFrom(i);
77 GT::NodeSubset ReachableButNotInSCC =
78 NodesReachableFromSCC.Meet(NodesInThisSCC.Complement());
79
80 for (unsigned j = 0; j != NUM_NODES; ++j)
Galina Kistanovafcae62d2017-06-15 21:00:40 +000081 if (ReachableButNotInSCC.count(j)) {
Duncan Sandsdea551c2011-07-28 14:17:11 +000082 EXPECT_TRUE(G.NodesReachableFrom(j).Meet(NodesInThisSCC).isEmpty());
Galina Kistanovafcae62d2017-06-15 21:00:40 +000083 }
Duncan Sandsdea551c2011-07-28 14:17:11 +000084
85 // The result must be the same for all other nodes in this SCC, so
86 // there is no point in checking them.
87 break;
88 }
89
90 // This is indeed a SCC: a maximal set of nodes for which each node is
91 // reachable from every other.
92
93 // Check that we didn't already see this SCC.
94 EXPECT_TRUE(NodesInSomeSCC.Meet(NodesInThisSCC).isEmpty());
95
96 NodesInSomeSCC = NodesInSomeSCC.Join(NodesInThisSCC);
Duncan Sands6d0f0ff2011-07-28 14:33:01 +000097
98 // Check a property that is specific to the LLVM SCC iterator and
99 // guaranteed by it: if a node in SCC S1 has an edge to a node in
100 // SCC S2, then S1 is visited *after* S2. This means that the set
101 // of nodes reachable from this SCC must be contained either in the
102 // union of this SCC and all previously visited SCC's.
103
104 for (unsigned i = 0; i != NUM_NODES; ++i)
105 if (NodesInThisSCC.count(i)) {
106 GT::NodeSubset NodesReachableFromSCC = G.NodesReachableFrom(i);
107 EXPECT_TRUE(NodesReachableFromSCC.isSubsetOf(NodesInSomeSCC));
108 // The result must be the same for all other nodes in this SCC, so
109 // there is no point in checking them.
110 break;
111 }
Duncan Sandsdea551c2011-07-28 14:17:11 +0000112 }
113
114 // Finally, check that the nodes in some SCC are exactly those that are
115 // reachable from the initial node.
116 EXPECT_EQ(NodesInSomeSCC, G.NodesReachableFrom(0));
Duncan Sandsbe64bbf2011-07-29 07:50:02 +0000117 }
Duncan Sandsdea551c2011-07-28 14:17:11 +0000118}
119
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000120}