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