Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 1 | //===----- llvm/unittest/ADT/SCCIteratorTest.cpp - SCCIterator tests ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/SCCIterator.h" |
Tim Shen | 608ca25 | 2016-08-22 21:59:26 +0000 | [diff] [blame] | 10 | #include "TestGraph.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 12 | #include <limits.h> |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | namespace llvm { |
| 17 | |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 18 | TEST(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 Sands | be64bbf | 2011-07-29 07:50:02 +0000 | [diff] [blame] | 24 | typedef Graph<NUM_NODES> GT; |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 25 | |
Duncan Sands | be64bbf | 2011-07-29 07:50:02 +0000 | [diff] [blame] | 26 | /// Enumerate all graphs using NUM_GRAPHS bits. |
Gabor Horvath | fee0434 | 2015-03-16 09:53:42 +0000 | [diff] [blame] | 27 | static_assert(NUM_GRAPHS < sizeof(unsigned) * CHAR_BIT, "Too many graphs!"); |
Duncan Sands | be64bbf | 2011-07-29 07:50:02 +0000 | [diff] [blame] | 28 | for (unsigned GraphDescriptor = 0; GraphDescriptor < (1U << NUM_GRAPHS); |
| 29 | ++GraphDescriptor) { |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 30 | GT G; |
| 31 | |
| 32 | // Add edges as specified by the descriptor. |
Duncan Sands | 251daee | 2011-07-28 14:37:53 +0000 | [diff] [blame] | 33 | unsigned DescriptorCopy = GraphDescriptor; |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 34 | 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 Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 52 | const std::vector<GT::NodeType *> &SCC = *I; |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 53 | |
| 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 Kistanova | fcae62d | 2017-06-15 21:00:40 +0000 | [diff] [blame] | 65 | if (NodesInThisSCC.count(i)) { |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 66 | EXPECT_TRUE(NodesInThisSCC.isSubsetOf(G.NodesReachableFrom(i))); |
Galina Kistanova | fcae62d | 2017-06-15 21:00:40 +0000 | [diff] [blame] | 67 | } |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 68 | |
| 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 Kistanova | fcae62d | 2017-06-15 21:00:40 +0000 | [diff] [blame] | 81 | if (ReachableButNotInSCC.count(j)) { |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 82 | EXPECT_TRUE(G.NodesReachableFrom(j).Meet(NodesInThisSCC).isEmpty()); |
Galina Kistanova | fcae62d | 2017-06-15 21:00:40 +0000 | [diff] [blame] | 83 | } |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 84 | |
| 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 Sands | 6d0f0ff | 2011-07-28 14:33:01 +0000 | [diff] [blame] | 97 | |
| 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 Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 112 | } |
| 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 Sands | be64bbf | 2011-07-29 07:50:02 +0000 | [diff] [blame] | 117 | } |
Duncan Sands | dea551c | 2011-07-28 14:17:11 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 120 | } |