SCCIteratorTest.cpp 11 KB

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