2011-07-28 22:17:11 +08:00
|
|
|
//===----- llvm/unittest/ADT/SCCIteratorTest.cpp - SCCIterator tests ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2016-08-23 05:59:26 +08:00
|
|
|
#include "TestGraph.h"
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "gtest/gtest.h"
|
2016-04-06 04:45:04 +08:00
|
|
|
#include <limits.h>
|
2011-07-28 22:17:11 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
TEST(SCCIteratorTest, AllSmallGraphs) {
|
|
|
|
// Test SCC computation against every graph with NUM_NODES nodes or less.
|
|
|
|
// Since SCC considers every node to have an implicit self-edge, we only
|
|
|
|
// create graphs for which every node has a self-edge.
|
|
|
|
#define NUM_NODES 4
|
|
|
|
#define NUM_GRAPHS (NUM_NODES * (NUM_NODES - 1))
|
2011-07-29 15:50:02 +08:00
|
|
|
typedef Graph<NUM_NODES> GT;
|
2011-07-28 22:17:11 +08:00
|
|
|
|
2011-07-29 15:50:02 +08:00
|
|
|
/// Enumerate all graphs using NUM_GRAPHS bits.
|
2015-03-16 17:53:42 +08:00
|
|
|
static_assert(NUM_GRAPHS < sizeof(unsigned) * CHAR_BIT, "Too many graphs!");
|
2011-07-29 15:50:02 +08:00
|
|
|
for (unsigned GraphDescriptor = 0; GraphDescriptor < (1U << NUM_GRAPHS);
|
|
|
|
++GraphDescriptor) {
|
2011-07-28 22:17:11 +08:00
|
|
|
GT G;
|
|
|
|
|
|
|
|
// Add edges as specified by the descriptor.
|
2011-07-28 22:37:53 +08:00
|
|
|
unsigned DescriptorCopy = GraphDescriptor;
|
2011-07-28 22:17:11 +08:00
|
|
|
for (unsigned i = 0; i != NUM_NODES; ++i)
|
|
|
|
for (unsigned j = 0; j != NUM_NODES; ++j) {
|
|
|
|
// Always add a self-edge.
|
|
|
|
if (i == j) {
|
|
|
|
G.AddEdge(i, j);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (DescriptorCopy & 1)
|
|
|
|
G.AddEdge(i, j);
|
|
|
|
DescriptorCopy >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the SCC logic on this graph.
|
|
|
|
|
|
|
|
/// NodesInSomeSCC - Those nodes which are in some SCC.
|
|
|
|
GT::NodeSubset NodesInSomeSCC;
|
|
|
|
|
|
|
|
for (scc_iterator<GT> I = scc_begin(G), E = scc_end(G); I != E; ++I) {
|
2014-04-26 02:24:50 +08:00
|
|
|
const std::vector<GT::NodeType *> &SCC = *I;
|
2011-07-28 22:17:11 +08:00
|
|
|
|
|
|
|
// Get the nodes in this SCC as a NodeSubset rather than a vector.
|
|
|
|
GT::NodeSubset NodesInThisSCC;
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
|
|
|
|
NodesInThisSCC.AddNode(SCC[i]->first);
|
|
|
|
|
|
|
|
// There should be at least one node in every SCC.
|
|
|
|
EXPECT_FALSE(NodesInThisSCC.isEmpty());
|
|
|
|
|
|
|
|
// Check that every node in the SCC is reachable from every other node in
|
|
|
|
// the SCC.
|
|
|
|
for (unsigned i = 0; i != NUM_NODES; ++i)
|
2017-06-16 05:00:40 +08:00
|
|
|
if (NodesInThisSCC.count(i)) {
|
2011-07-28 22:17:11 +08:00
|
|
|
EXPECT_TRUE(NodesInThisSCC.isSubsetOf(G.NodesReachableFrom(i)));
|
2017-06-16 05:00:40 +08:00
|
|
|
}
|
2011-07-28 22:17:11 +08:00
|
|
|
|
|
|
|
// OK, now that we now that every node in the SCC is reachable from every
|
|
|
|
// other, this means that the set of nodes reachable from any node in the
|
|
|
|
// SCC is the same as the set of nodes reachable from every node in the
|
|
|
|
// SCC. Check that for every node N not in the SCC but reachable from the
|
|
|
|
// SCC, no element of the SCC is reachable from N.
|
|
|
|
for (unsigned i = 0; i != NUM_NODES; ++i)
|
|
|
|
if (NodesInThisSCC.count(i)) {
|
|
|
|
GT::NodeSubset NodesReachableFromSCC = G.NodesReachableFrom(i);
|
|
|
|
GT::NodeSubset ReachableButNotInSCC =
|
|
|
|
NodesReachableFromSCC.Meet(NodesInThisSCC.Complement());
|
|
|
|
|
|
|
|
for (unsigned j = 0; j != NUM_NODES; ++j)
|
2017-06-16 05:00:40 +08:00
|
|
|
if (ReachableButNotInSCC.count(j)) {
|
2011-07-28 22:17:11 +08:00
|
|
|
EXPECT_TRUE(G.NodesReachableFrom(j).Meet(NodesInThisSCC).isEmpty());
|
2017-06-16 05:00:40 +08:00
|
|
|
}
|
2011-07-28 22:17:11 +08:00
|
|
|
|
|
|
|
// The result must be the same for all other nodes in this SCC, so
|
|
|
|
// there is no point in checking them.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is indeed a SCC: a maximal set of nodes for which each node is
|
|
|
|
// reachable from every other.
|
|
|
|
|
|
|
|
// Check that we didn't already see this SCC.
|
|
|
|
EXPECT_TRUE(NodesInSomeSCC.Meet(NodesInThisSCC).isEmpty());
|
|
|
|
|
|
|
|
NodesInSomeSCC = NodesInSomeSCC.Join(NodesInThisSCC);
|
2011-07-28 22:33:01 +08:00
|
|
|
|
|
|
|
// Check a property that is specific to the LLVM SCC iterator and
|
|
|
|
// guaranteed by it: if a node in SCC S1 has an edge to a node in
|
|
|
|
// SCC S2, then S1 is visited *after* S2. This means that the set
|
|
|
|
// of nodes reachable from this SCC must be contained either in the
|
|
|
|
// union of this SCC and all previously visited SCC's.
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NUM_NODES; ++i)
|
|
|
|
if (NodesInThisSCC.count(i)) {
|
|
|
|
GT::NodeSubset NodesReachableFromSCC = G.NodesReachableFrom(i);
|
|
|
|
EXPECT_TRUE(NodesReachableFromSCC.isSubsetOf(NodesInSomeSCC));
|
|
|
|
// The result must be the same for all other nodes in this SCC, so
|
|
|
|
// there is no point in checking them.
|
|
|
|
break;
|
|
|
|
}
|
2011-07-28 22:17:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, check that the nodes in some SCC are exactly those that are
|
|
|
|
// reachable from the initial node.
|
|
|
|
EXPECT_EQ(NodesInSomeSCC, G.NodesReachableFrom(0));
|
2011-07-29 15:50:02 +08:00
|
|
|
}
|
2011-07-28 22:17:11 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
}
|