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