Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dominators_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39#include <chrono>
40#include <cstdlib>
41#include <string>
42#include <random>
43
44#include <Dominators.H>
45#include <tpl_dynArray.H>
46#include <tpl_dynMapTree.H>
47#include <tpl_graph.H>
48
49using namespace Aleph;
50
51namespace
52{
54
55 DynArray<DG::Node *> make_nodes(DG & g, int n)
56 {
58 nodes.reserve(static_cast<size_t>(n));
59 for (int i = 0; i < n; ++i)
60 nodes(static_cast<size_t>(i)) = g.insert_node(i);
61 return nodes;
62 }
63
64 // Build idom map: node_info -> idom_info (-1 if root/no idom)
65 DynMapTree<int, int> idom_map(const DynList<std::pair<DG::Node *,
66 DG::Node *>> & idoms)
67 {
69 for (auto it = idoms.get_it(); it.has_curr(); it.next_ne())
70 {
71 auto [node, idom] = it.get_curr();
72 m.insert(node->get_info(), idom ? idom->get_info() : -1);
73 }
74 return m;
75 }
76
77} // namespace
78
79
80// ============================================================================
81// Basic Structure Tests
82// ============================================================================
83
84// The cache knows the graph and the root by their addresses, and finds the
85// nodes by their counters: after a change of the graph, or another
86// computation on it, invalidate() drops it
87TEST(Dominators, InvalidateDropsTheCache)
88{
89 DG g;
90 auto n = make_nodes(g, 4);
91 g.insert_arc(n(0), n(1));
92 g.insert_arc(n(1), n(3));
93 g.insert_arc(n(0), n(2));
94 g.insert_arc(n(2), n(1));
95
97 ASSERT_EQ(dom.get_idom(g, n(0), n(3)), n(1));
98 ASSERT_TRUE(dom.has_computation());
99
100 g.insert_arc(n(2), n(3)); // 0 -> 2 -> 3 avoids 1
101 dom.invalidate();
102 EXPECT_FALSE(dom.has_computation());
103 EXPECT_EQ(dom.get_idom(g, n(0), n(3)), n(0));
104
105 Lengauer_Tarjan_Dominators<DG> other; // it writes the counters
106 EXPECT_EQ(other.get_idom(g, n(2), n(3)), n(2));
107 dom.invalidate();
108 EXPECT_EQ(dom.get_idom(g, n(0), n(3)), n(0));
109 EXPECT_TRUE(dom.dominates(g, n(0), n(0), n(2)));
110}
111
112// Each method of the post-dominators computes them again
113TEST(Dominators, PostDominatorsSeeAChangeOfTheGraph)
114{
115 DG g;
116 auto n = make_nodes(g, 4);
117 g.insert_arc(n(0), n(1));
118 g.insert_arc(n(1), n(3));
119 g.insert_arc(n(0), n(2));
120 g.insert_arc(n(2), n(1));
121
123 ASSERT_EQ(pd.get_ipdom(g, n(3), n(0)), n(1));
124 g.insert_arc(n(2), n(3)); // 0 -> 2 -> 3 avoids 1
125 EXPECT_EQ(pd.get_ipdom(g, n(3), n(0)), n(3));
126}
127
128TEST(Dominators, SingleNode)
129{
130 DG g;
131 auto nodes = make_nodes(g, 1);
132
133 auto idoms = compute_dominators(g, nodes[0]);
134 auto m = idom_map(idoms);
135
136 EXPECT_EQ(m.size(), 1U);
137 EXPECT_EQ(m[0], -1); // root has no idom
138}
139
140TEST(Dominators, LinearChain)
141{
142 // 0 -> 1 -> 2 -> 3 -> 4
143 // idom: 0=root, 1<-0, 2<-1, 3<-2, 4<-3
144 DG g;
145 auto nodes = make_nodes(g, 5);
146 g.insert_arc(nodes[0], nodes[1]);
147 g.insert_arc(nodes[1], nodes[2]);
148 g.insert_arc(nodes[2], nodes[3]);
149 g.insert_arc(nodes[3], nodes[4]);
150
151 auto m = idom_map(compute_dominators(g, nodes[0]));
152
153 EXPECT_EQ(m[0], -1);
154 EXPECT_EQ(m[1], 0);
155 EXPECT_EQ(m[2], 1);
156 EXPECT_EQ(m[3], 2);
157 EXPECT_EQ(m[4], 3);
158}
159
160TEST(Dominators, DiamondGraph)
161{
162 // 0
163 // / |
164 // 1 2
165 // \ /
166 // 3
167 // idom(1) = 0, idom(2) = 0, idom(3) = 0
168 DG g;
169 auto nodes = make_nodes(g, 4);
170 g.insert_arc(nodes[0], nodes[1]);
171 g.insert_arc(nodes[0], nodes[2]);
172 g.insert_arc(nodes[1], nodes[3]);
173 g.insert_arc(nodes[2], nodes[3]);
174
175 auto m = idom_map(compute_dominators(g, nodes[0]));
176
177 EXPECT_EQ(m[0], -1);
178 EXPECT_EQ(m[1], 0);
179 EXPECT_EQ(m[2], 0);
180 EXPECT_EQ(m[3], 0);
181}
182
183TEST(Dominators, IfThenElseCFG)
184{
185 // Classic if-then-else CFG:
186 // 0 (entry)
187 // / |
188 // 1 2 (then / else)
189 // \ /
190 // 3 (join)
191 // |
192 // 4 (exit)
193 DG g;
194 auto nodes = make_nodes(g, 5);
195 g.insert_arc(nodes[0], nodes[1]);
196 g.insert_arc(nodes[0], nodes[2]);
197 g.insert_arc(nodes[1], nodes[3]);
198 g.insert_arc(nodes[2], nodes[3]);
199 g.insert_arc(nodes[3], nodes[4]);
200
201 auto m = idom_map(compute_dominators(g, nodes[0]));
202
203 EXPECT_EQ(m[0], -1);
204 EXPECT_EQ(m[1], 0);
205 EXPECT_EQ(m[2], 0);
206 EXPECT_EQ(m[3], 0);
207 EXPECT_EQ(m[4], 3);
208}
209
210TEST(Dominators, LoopCFG)
211{
212 // CFG with a loop:
213 // 0 (entry)
214 // |
215 // 1 (loop header) <---+
216 // / | |
217 // 2 3 (loop body) ----+
218 // |
219 // 4 (exit)
220 DG g;
221 auto nodes = make_nodes(g, 5);
222 g.insert_arc(nodes[0], nodes[1]);
223 g.insert_arc(nodes[1], nodes[2]);
224 g.insert_arc(nodes[1], nodes[3]);
225 g.insert_arc(nodes[3], nodes[1]); // back-edge
226 g.insert_arc(nodes[2], nodes[4]);
227
228 auto m = idom_map(compute_dominators(g, nodes[0]));
229
230 EXPECT_EQ(m[0], -1);
231 EXPECT_EQ(m[1], 0);
232 EXPECT_EQ(m[2], 1);
233 EXPECT_EQ(m[3], 1);
234 EXPECT_EQ(m[4], 2);
235}
236
237TEST(Dominators, ComplexTextbookCFG)
238{
239 // Classic textbook example (Appel "Modern Compiler Implementation")
240 // 13 nodes (0..12), root = 0
241 //
242 // Edges:
243 // 0->1, 0->5, 0->9
244 // 1->2, 2->3, 3->4, 3->1 (back), 4->12
245 // 5->6, 6->7, 6->8, 7->4, 8->12
246 // 9->10, 10->11, 11->9 (back), 11->12
247 DG g;
248 auto n = make_nodes(g, 13);
249 g.insert_arc(n[0], n[1]);
250 g.insert_arc(n[0], n[5]);
251 g.insert_arc(n[0], n[9]);
252 g.insert_arc(n[1], n[2]);
253 g.insert_arc(n[2], n[3]);
254 g.insert_arc(n[3], n[4]);
255 g.insert_arc(n[3], n[1]); // back-edge
256 g.insert_arc(n[4], n[12]);
257 g.insert_arc(n[5], n[6]);
258 g.insert_arc(n[6], n[7]);
259 g.insert_arc(n[6], n[8]);
260 g.insert_arc(n[7], n[4]);
261 g.insert_arc(n[8], n[12]);
262 g.insert_arc(n[9], n[10]);
263 g.insert_arc(n[10], n[11]);
264 g.insert_arc(n[11], n[9]); // back-edge
265 g.insert_arc(n[11], n[12]);
266
267 auto m = idom_map(compute_dominators(g, n[0]));
268
269 // Root
270 EXPECT_EQ(m[0], -1);
271
272 // Branch 1: 0->1->2->3
273 EXPECT_EQ(m[1], 0);
274 EXPECT_EQ(m[2], 1);
275 EXPECT_EQ(m[3], 2);
276
277 // Node 4: reachable from both 3 and 7; idom = 0
278 EXPECT_EQ(m[4], 0);
279
280 // Branch 2: 0->5->6->{7,8}
281 EXPECT_EQ(m[5], 0);
282 EXPECT_EQ(m[6], 5);
283 EXPECT_EQ(m[7], 6);
284 EXPECT_EQ(m[8], 6);
285
286 // Branch 3: 0->9->10->11
287 EXPECT_EQ(m[9], 0);
288 EXPECT_EQ(m[10], 9);
289 EXPECT_EQ(m[11], 10);
290
291 // Node 12: reachable from 4, 8, 11; idom = 0
292 EXPECT_EQ(m[12], 0);
293}
294
295
296// ============================================================================
297// Unreachable Nodes and Edge Cases
298// ============================================================================
299
300TEST(Dominators, UnreachableNodes)
301{
302 // 0 -> 1 -> 2, but 3 is disconnected
303 DG g;
304 auto nodes = make_nodes(g, 4);
305 g.insert_arc(nodes[0], nodes[1]);
306 g.insert_arc(nodes[1], nodes[2]);
307 // Node 3 is unreachable
308
309 auto idoms = compute_dominators(g, nodes[0]);
310 auto m = idom_map(idoms);
311
312 // Only 3 reachable nodes
313 EXPECT_EQ(m.size(), 3U);
314 EXPECT_EQ(m.search(3), nullptr); // node 3 not in result
315}
316
317TEST(Dominators, SelfLoop)
318{
319 // 0 -> 1 -> 1 (self-loop)
320 DG g;
321 auto nodes = make_nodes(g, 2);
322 g.insert_arc(nodes[0], nodes[1]);
323 g.insert_arc(nodes[1], nodes[1]); // self-loop
324
325 auto m = idom_map(compute_dominators(g, nodes[0]));
326
327 EXPECT_EQ(m[0], -1);
328 EXPECT_EQ(m[1], 0);
329}
330
331TEST(Dominators, EmptyGraph)
332{
333 DG g;
334 // Create algorithm instance for empty graph
336 // Empty graph has no nodes, so we can't call with a root.
337 // The algorithm should handle this gracefully.
338 EXPECT_TRUE(g.get_num_nodes() == 0);
339}
340
341TEST(Dominators, ErrorHandlingNullRoot)
342{
343 DG g;
344 auto nodes = make_nodes(g, 2);
345 g.insert_arc(nodes[0], nodes[1]);
346
347 EXPECT_THROW(compute_dominators(g, nullptr), std::domain_error);
348}
349
350
351// ============================================================================
352// Dominator Tree Structure
353// ============================================================================
354
355TEST(Dominators, DominatorTreeStructure)
356{
357 // 0
358 // / |
359 // 1 2
360 // \ /
361 // 3
362 // |
363 // 4
364 DG g;
365 auto nodes = make_nodes(g, 5);
366 g.insert_arc(nodes[0], nodes[1]);
367 g.insert_arc(nodes[0], nodes[2]);
368 g.insert_arc(nodes[1], nodes[3]);
369 g.insert_arc(nodes[2], nodes[3]);
370 g.insert_arc(nodes[3], nodes[4]);
371
372 DG tree;
373 build_dominator_tree(g, nodes[0], tree);
374
375 // Tree should have 5 nodes and 4 arcs (n-1 for a tree)
376 EXPECT_EQ(tree.get_num_nodes(), 5U);
377 EXPECT_EQ(tree.get_num_arcs(), 4U);
378
379 // Verify node mapping
380 for (auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
381 {
382 auto tn = it.get_curr();
383 auto orig = mapped_node<DG>(tn);
384 EXPECT_NE(orig, nullptr);
385 EXPECT_EQ(tn->get_info(), orig->get_info());
386 }
387}
388
389TEST(Dominators, DominatorTreeLinearChain)
390{
391 // 0->1->2->3: dom tree is the same chain
392 DG g;
393 auto nodes = make_nodes(g, 4);
394 g.insert_arc(nodes[0], nodes[1]);
395 g.insert_arc(nodes[1], nodes[2]);
396 g.insert_arc(nodes[2], nodes[3]);
397
398 DG tree;
399 build_dominator_tree(g, nodes[0], tree);
400
401 EXPECT_EQ(tree.get_num_nodes(), 4U);
402 EXPECT_EQ(tree.get_num_arcs(), 3U);
403}
404
405
406// ============================================================================
407// Dominance Checks
408// ============================================================================
409
410TEST(Dominators, DominanceReflexive)
411{
412 DG g;
413 auto nodes = make_nodes(g, 3);
414 g.insert_arc(nodes[0], nodes[1]);
415 g.insert_arc(nodes[1], nodes[2]);
416
418
419 // Every reachable node dominates itself
420 for (int i = 0; i < 3; ++i)
421 EXPECT_TRUE(alg.dominates(g, nodes[0], nodes[i], nodes[i]));
422}
423
424TEST(Dominators, RootDominatesAll)
425{
426 DG g;
427 auto nodes = make_nodes(g, 5);
428 g.insert_arc(nodes[0], nodes[1]);
429 g.insert_arc(nodes[0], nodes[2]);
430 g.insert_arc(nodes[1], nodes[3]);
431 g.insert_arc(nodes[2], nodes[4]);
432
434
435 for (int i = 0; i < 5; ++i)
436 EXPECT_TRUE(alg.dominates(g, nodes[0], nodes[0], nodes[i]));
437}
438
439TEST(Dominators, NonDominance)
440{
441 // 0
442 // / |
443 // 1 2
444 // \ /
445 // 3
446 DG g;
447 auto nodes = make_nodes(g, 4);
448 g.insert_arc(nodes[0], nodes[1]);
449 g.insert_arc(nodes[0], nodes[2]);
450 g.insert_arc(nodes[1], nodes[3]);
451 g.insert_arc(nodes[2], nodes[3]);
452
454
455 // Node 1 does NOT dominate 3 (path 0->2->3 bypasses 1)
456 EXPECT_FALSE(alg.dominates(g, nodes[0], nodes[1], nodes[3]));
457 // Node 2 does NOT dominate 3 (path 0->1->3 bypasses 2)
458 EXPECT_FALSE(alg.dominates(g, nodes[0], nodes[2], nodes[3]));
459}
460
461
462// ============================================================================
463// Dominance Frontiers
464// ============================================================================
465
466TEST(Dominators, DominanceFrontiersIfThenElse)
467{
468 // 0 (entry)
469 // / |
470 // 1 2
471 // \ /
472 // 3 (join)
473 DG g;
474 auto nodes = make_nodes(g, 4);
475 g.insert_arc(nodes[0], nodes[1]);
476 g.insert_arc(nodes[0], nodes[2]);
477 g.insert_arc(nodes[1], nodes[3]);
478 g.insert_arc(nodes[2], nodes[3]);
479
481
482 // DF(0) = {} (root dominates everything reachable)
483 EXPECT_TRUE(df.find(nodes[0]).is_empty());
484
485 // DF(1) = {3} (1 dominates itself. 3 has predecessors 1 and 2.
486 // 1 dominates pred 1 of 3, but 1 does NOT strictly dominate 3 (idom(3)=0).
487 // So 3 is in DF(1).
488 auto & df1 = df.find(nodes[1]);
489 EXPECT_EQ(df1.size(), 1U);
490 EXPECT_EQ(df1.get_first()->get_info(), 3);
491
492 // DF(2) = {3}
493 auto & df2 = df.find(nodes[2]);
494 EXPECT_EQ(df2.size(), 1U);
495 EXPECT_EQ(df2.get_first()->get_info(), 3);
496
497 // DF(3) = {} (3 has only one predecessor in the idom sense)
498 EXPECT_TRUE(df.find(nodes[3]).is_empty());
499}
500
501TEST(Dominators, DominanceFrontiersLoop)
502{
503 // 0 -> 1 -> 2
504 // ^ |
505 // +----+ (back-edge 2->1)
506 DG g;
507 auto nodes = make_nodes(g, 3);
508 g.insert_arc(nodes[0], nodes[1]);
509 g.insert_arc(nodes[1], nodes[2]);
510 g.insert_arc(nodes[2], nodes[1]); // back-edge
511
513
514 // Node 1 is a loop header with predecessors 0 and 2.
515 // DF(1) = {1} (1 dominates 2 which is pred of 1, but 1 doesn't strictly dom 1)
516 auto & df1 = df.find(nodes[1]);
517 EXPECT_EQ(df1.size(), 1U);
518 EXPECT_EQ(df1.get_first()->get_info(), 1);
519
520 // DF(2) = {1}
521 auto & df2 = df.find(nodes[2]);
522 EXPECT_EQ(df2.size(), 1U);
523 EXPECT_EQ(df2.get_first()->get_info(), 1);
524}
525
526
527// ============================================================================
528// get_idom tests
529// ============================================================================
530
531TEST(Dominators, GetIdomSpecificNode)
532{
533 DG g;
534 auto nodes = make_nodes(g, 4);
535 g.insert_arc(nodes[0], nodes[1]);
536 g.insert_arc(nodes[1], nodes[2]);
537 g.insert_arc(nodes[2], nodes[3]);
538
540
541 DG::Node * n0 = nodes[0];
542 DG::Node * n1 = nodes[1];
543 DG::Node * n2 = nodes[2];
544 DG::Node * n3 = nodes[3];
545
546 EXPECT_EQ(alg.get_idom(g, n0, n0), nullptr); // root
547 EXPECT_EQ(alg.get_idom(g, n0, n1), n0);
548 EXPECT_EQ(alg.get_idom(g, n0, n2), n1);
549 EXPECT_EQ(alg.get_idom(g, n0, n3), n2);
550}
551
552TEST(Dominators, GetIdomNullNodeThrows)
553{
554 DG g;
555 auto nodes = make_nodes(g, 2);
556 g.insert_arc(nodes[0], nodes[1]);
557
559 EXPECT_THROW(alg.get_idom(g, nodes[0], nullptr), std::domain_error);
560}
561
562
563// ============================================================================
564// Large / Random Graph
565// ============================================================================
566
567TEST(Dominators, LargeRandomDAG)
568{
569 // Build a random DAG with ~500 nodes and verify structural properties
570 const int N = 500;
571 DG g;
572 auto nodes = make_nodes(g, N);
573
574 std::mt19937 rng(42);
575
576 // Add random forward edges (i -> j where j > i to ensure DAG)
577 for (int i = 0; i < N - 1; ++i)
578 {
579 // Each node has at least one outgoing edge
580 int j = i + 1 + static_cast<int>(rng() % std::min(5, N - i - 1));
581 if (j < N)
582 g.insert_arc(nodes[i], nodes[j]);
583
584 // Add a few more random edges
585 for (int k = 0; k < 2; ++k)
586 {
587 int t = i + 1 + static_cast<int>(rng() % (N - i - 1));
588 if (t < N)
589 g.insert_arc(nodes[i], nodes[t]);
590 }
591 }
592
593 auto idoms = compute_dominators(g, nodes[0]);
594 auto m = idom_map(idoms);
595
596 // Root should have no idom
597 EXPECT_EQ(m[0], -1);
598
599 // Every non-root reachable node should have an idom
600 for (auto it = m.get_it(); it.has_curr(); it.next_ne())
601 {
602 const auto & [node_id, idom_id] = it.get_curr();
603 if (node_id == 0)
604 continue;
605 EXPECT_GE(idom_id, 0) << "Node " << node_id << " has no idom";
606 }
607
608 // Verify dominator tree is a proper tree
609 DG tree;
610 build_dominator_tree(g, nodes[0], tree);
611 EXPECT_EQ(tree.get_num_nodes(), m.size());
612 EXPECT_EQ(tree.get_num_arcs(), m.size() - 1);
613}
614
615TEST(Dominators, PerformanceLargeRandomDAG)
616{
617 const char * run_perf = std::getenv("ALEPH_RUN_DOMINATORS_PERF");
618 if (run_perf == nullptr or std::string(run_perf) != "1")
619 GTEST_SKIP() << "Set ALEPH_RUN_DOMINATORS_PERF=1 to run dominators perf test";
620
621 constexpr int N = 5000;
622# ifdef NDEBUG
623 constexpr long long kBudgetMs = 9000;
624# else
625 constexpr long long kBudgetMs = 20000;
626# endif
627
628 DG g;
629 auto nodes = make_nodes(g, N);
630 std::mt19937 rng(0xD01A70u);
631
632 for (int i = 0; i < N - 1; ++i)
633 {
634 const int max_step = std::min(12, N - i - 1);
635 const int j = i + 1 + static_cast<int>(rng() % max_step);
636 g.insert_arc(nodes[i], nodes[j]);
637
638 for (int k = 0; k < 4; ++k)
639 {
640 const int t = i + 1 + static_cast<int>(rng() % (N - i - 1));
641 g.insert_arc(nodes[i], nodes[t]);
642 }
643 }
644
645 const auto t0 = std::chrono::steady_clock::now();
646 auto idoms = compute_dominators(g, nodes[0]);
647 auto m = idom_map(idoms);
648 DG tree;
649 build_dominator_tree(g, nodes[0], tree);
650 const auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
651 std::chrono::steady_clock::now() - t0).count();
652
653 EXPECT_EQ(m[0], -1);
654 EXPECT_EQ(tree.get_num_nodes(), m.size());
655 EXPECT_EQ(tree.get_num_arcs(), m.size() - 1);
656 EXPECT_LT(elapsed_ms, kBudgetMs)
657 << "Dominators perf regression: N=" << N
658 << ", elapsed_ms=" << elapsed_ms
659 << ", budget_ms=" << kBudgetMs;
660}
661
662
663// ============================================================================
664// Operator() alias
665// ============================================================================
666
667TEST(Dominators, OperatorAlias)
668{
669 DG g;
670 auto nodes = make_nodes(g, 3);
671 g.insert_arc(nodes[0], nodes[1]);
672 g.insert_arc(nodes[1], nodes[2]);
673
674 DG tree1, tree2;
676
677 alg.build_tree(g, nodes[0], tree1);
678 alg(g, nodes[0], tree2);
679
680 EXPECT_EQ(tree1.get_num_nodes(), tree2.get_num_nodes());
681 EXPECT_EQ(tree1.get_num_arcs(), tree2.get_num_arcs());
682}
683
684
685// ============================================================================
686// Cache reuse test
687// ============================================================================
688
689TEST(Dominators, CacheReuse)
690{
691 DG g;
692 auto nodes = make_nodes(g, 4);
693 g.insert_arc(nodes[0], nodes[1]);
694 g.insert_arc(nodes[0], nodes[2]);
695 g.insert_arc(nodes[1], nodes[3]);
696 g.insert_arc(nodes[2], nodes[3]);
697
699
700 // First call computes
701 auto m1 = idom_map(alg.compute_idom(g, nodes[0]));
702 // Second call should reuse cache
703 auto m2 = idom_map(alg.compute_idom(g, nodes[0]));
704
705 EXPECT_EQ(m1, m2);
706 EXPECT_TRUE(alg.has_computation());
707 EXPECT_EQ(alg.get_graph(), &g);
708 DG::Node * root = nodes[0];
709 EXPECT_EQ(alg.get_root(), root);
710}
711
712
713// ============================================================================
714// String-typed graph
715// ============================================================================
716
717TEST(Dominators, StringTypedGraph)
718{
720
721 SDG g;
722 auto entry = g.insert_node("entry");
723 auto body = g.insert_node("body");
724 auto exit_node = g.insert_node("exit");
725
726 g.insert_arc(entry, body);
727 g.insert_arc(body, exit_node);
728
729 auto idoms = compute_dominators(g, entry);
730
732 for (auto it = idoms.get_it(); it.has_curr(); it.next_ne())
733 {
734 auto [node, idom] = it.get_curr();
735 m[node->get_info()] = idom ? idom->get_info() : "none";
736 }
737
738 EXPECT_EQ(m["entry"], "none");
739 EXPECT_EQ(m["body"], "entry");
740 EXPECT_EQ(m["exit"], "body");
741}
742
743
744// ============================================================================
745// Post-Dominator Tests
746// ============================================================================
747
748namespace
749{
750 // Build ipdom map: node_info -> ipdom_info (-1 if exit/no ipdom)
751 DynMapTree<int, int> ipdom_map(const DynList<std::pair<DG::Node*,
752 DG::Node*>> & ipdoms)
753 {
755 for (auto it = ipdoms.get_it(); it.has_curr(); it.next_ne())
756 {
757 auto [node, ipdom] = it.get_curr();
758 m[node->get_info()] = ipdom ? ipdom->get_info() : -1;
759 }
760 return m;
761 }
762} // namespace
763
764TEST(PostDominators, SingleNode)
765{
766 DG g;
767 auto nodes = make_nodes(g, 1);
768
769 auto ipdoms = compute_post_dominators(g, nodes[0]);
770 auto m = ipdom_map(ipdoms);
771
772 EXPECT_EQ(m.size(), 1U);
773 EXPECT_EQ(m[0], -1); // exit has no ipdom
774}
775
776TEST(PostDominators, LinearChain)
777{
778 // 0 -> 1 -> 2 -> 3; exit = 3
779 // ipdom(2) = 3, ipdom(1) = 2, ipdom(0) = 1
780 DG g;
781 auto nodes = make_nodes(g, 4);
782 g.insert_arc(nodes[0], nodes[1]);
783 g.insert_arc(nodes[1], nodes[2]);
784 g.insert_arc(nodes[2], nodes[3]);
785
786 auto m = ipdom_map(compute_post_dominators(g, nodes[3]));
787
788 EXPECT_EQ(m[3], -1); // exit
789 EXPECT_EQ(m[2], 3);
790 EXPECT_EQ(m[1], 2);
791 EXPECT_EQ(m[0], 1);
792}
793
794TEST(PostDominators, DiamondGraph)
795{
796 // 0
797 // / |
798 // 1 2
799 // \ /
800 // 3
801 // exit = 3; ipdom(1) = 3, ipdom(2) = 3, ipdom(0) = 3
802 DG g;
803 auto nodes = make_nodes(g, 4);
804 g.insert_arc(nodes[0], nodes[1]);
805 g.insert_arc(nodes[0], nodes[2]);
806 g.insert_arc(nodes[1], nodes[3]);
807 g.insert_arc(nodes[2], nodes[3]);
808
809 auto m = ipdom_map(compute_post_dominators(g, nodes[3]));
810
811 EXPECT_EQ(m[3], -1);
812 EXPECT_EQ(m[1], 3);
813 EXPECT_EQ(m[2], 3);
814 EXPECT_EQ(m[0], 3);
815}
816
817TEST(PostDominators, IfThenElseCFG)
818{
819 // Classic if-then-else CFG:
820 // 0 (entry)
821 // / |
822 // 1 2 (then / else)
823 // \ /
824 // 3 (join)
825 // |
826 // 4 (exit)
827 // ipdom(3) = 4, ipdom(1) = 3, ipdom(2) = 3, ipdom(0) = 3
828 DG g;
829 auto nodes = make_nodes(g, 5);
830 g.insert_arc(nodes[0], nodes[1]);
831 g.insert_arc(nodes[0], nodes[2]);
832 g.insert_arc(nodes[1], nodes[3]);
833 g.insert_arc(nodes[2], nodes[3]);
834 g.insert_arc(nodes[3], nodes[4]);
835
836 auto m = ipdom_map(compute_post_dominators(g, nodes[4]));
837
838 EXPECT_EQ(m[4], -1);
839 EXPECT_EQ(m[3], 4);
840 EXPECT_EQ(m[1], 3);
841 EXPECT_EQ(m[2], 3);
842 EXPECT_EQ(m[0], 3);
843}
844
845TEST(PostDominators, LoopCFG)
846{
847 // CFG with a loop:
848 // 0 (entry)
849 // |
850 // 1 (loop header) <---+
851 // / | |
852 // 2 3 (loop body) ----+
853 // |
854 // 4 (exit)
855 // exit = 4; ipdom(2) = 4, ipdom(1) = 2, ipdom(0) = 1
856 // Node 3 cannot reach 4 (it only loops back to 1), so it may or may
857 // not appear in the post-dominator result depending on reachability
858 // in the reversed graph.
859 DG g;
860 auto nodes = make_nodes(g, 5);
861 g.insert_arc(nodes[0], nodes[1]);
862 g.insert_arc(nodes[1], nodes[2]);
863 g.insert_arc(nodes[1], nodes[3]);
864 g.insert_arc(nodes[3], nodes[1]); // back-edge
865 g.insert_arc(nodes[2], nodes[4]);
866
867 auto m = ipdom_map(compute_post_dominators(g, nodes[4]));
868
869 EXPECT_EQ(m[4], -1);
870 EXPECT_EQ(m[2], 4);
871 EXPECT_EQ(m[1], 2);
872 EXPECT_EQ(m[0], 1);
873}
874
875TEST(PostDominators, PostDominanceReflexive)
876{
877 // Every node post-dominates itself
878 // 0 -> 1 -> 2
879 DG g;
880 auto nodes = make_nodes(g, 3);
881 g.insert_arc(nodes[0], nodes[1]);
882 g.insert_arc(nodes[1], nodes[2]);
883
885
886 for (int i = 0; i < 3; ++i)
887 EXPECT_TRUE(alg.post_dominates(g, nodes[2], nodes[i], nodes[i]));
888}
889
890TEST(PostDominators, ExitPostDominatesAll)
891{
892 // The exit node post-dominates every node that can reach it
893 // 0 -> 1 -> 2 -> 3
894 DG g;
895 auto nodes = make_nodes(g, 4);
896 g.insert_arc(nodes[0], nodes[1]);
897 g.insert_arc(nodes[1], nodes[2]);
898 g.insert_arc(nodes[2], nodes[3]);
899
901
902 for (int i = 0; i < 4; ++i)
903 EXPECT_TRUE(alg.post_dominates(g, nodes[3], nodes[3], nodes[i]));
904}
905
906TEST(PostDominators, PostDominanceFrontiers)
907{
908 // Diamond graph for PDF:
909 // 0
910 // / |
911 // 1 2
912 // \ /
913 // 3
914 // exit = 3
915 // In reversed graph: 3->{1,2}, 1->0, 2->0. Root=3.
916 // Dom on reversed: idom(1)=3, idom(2)=3, idom(0)=3
917 // DF on reversed at node 0: {0 has preds 1,2 in rev. Runner from 1: 1!=idom(0)=3 → DF(1)+=0,
918 // runner=idom(1)=3=idom(0) stop. Runner from 2: 2!=3 → DF(2)+=0, runner=3 stop.}
919 // PDF(1) = DF_rev(1) mapped back = {0}
920 // PDF(2) = DF_rev(2) mapped back = {0}
921 // PDF(0) = {} (only one "predecessor" in rev)
922 // PDF(3) = {}
923 DG g;
924 auto nodes = make_nodes(g, 4);
925 g.insert_arc(nodes[0], nodes[1]);
926 g.insert_arc(nodes[0], nodes[2]);
927 g.insert_arc(nodes[1], nodes[3]);
928 g.insert_arc(nodes[2], nodes[3]);
929
930 auto pdf = compute_post_dominance_frontiers(g, nodes[3]);
931
932 // PDF(3) = {} (exit)
933 EXPECT_TRUE(pdf.find(nodes[3]).is_empty());
934
935 // PDF(1) = {0}
936 auto & pdf1 = pdf.find(nodes[1]);
937 EXPECT_EQ(pdf1.size(), 1U);
938 EXPECT_EQ(pdf1.get_first()->get_info(), 0);
939
940 // PDF(2) = {0}
941 auto & pdf2 = pdf.find(nodes[2]);
942 EXPECT_EQ(pdf2.size(), 1U);
943 EXPECT_EQ(pdf2.get_first()->get_info(), 0);
944
945 // PDF(0) = {}
946 EXPECT_TRUE(pdf.find(nodes[0]).is_empty());
947}
948
949TEST(PostDominators, PostDominatorTreeStructure)
950{
951 // 0 -> 1 -> 2 -> 3
952 // exit = 3; post-dom tree: 3->2->1->0 (chain)
953 DG g;
954 auto nodes = make_nodes(g, 4);
955 g.insert_arc(nodes[0], nodes[1]);
956 g.insert_arc(nodes[1], nodes[2]);
957 g.insert_arc(nodes[2], nodes[3]);
958
959 DG tree;
960 build_post_dominator_tree(g, nodes[3], tree);
961
962 // Tree should have 4 nodes and 3 arcs (n-1)
963 EXPECT_EQ(tree.get_num_nodes(), 4U);
964 EXPECT_EQ(tree.get_num_arcs(), 3U);
965
966 // Verify node mapping
967 for (auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
968 {
969 auto tn = it.get_curr();
970 auto orig = mapped_node<DG>(tn);
971 EXPECT_NE(orig, nullptr);
972 EXPECT_EQ(tn->get_info(), orig->get_info());
973 }
974}
975
976TEST(PostDominators, ErrorHandlingNullExit)
977{
978 DG g;
979 auto nodes = make_nodes(g, 2);
980 g.insert_arc(nodes[0], nodes[1]);
981
982 EXPECT_THROW(compute_post_dominators(g, nullptr), std::domain_error);
983}
Dominator and post-dominator tree computation via the Lengauer-Tarjan algorithm.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Generic key-value map implemented on top of a binary search tree.
Computes dominator tree and dominance frontiers of a digraph using the Lengauer-Tarjan algorithm.
Definition Dominators.H:156
void build_tree(GT &g, Node *root, GT &tree)
Build the dominator tree as a separate graph.
Definition Dominators.H:452
bool has_computation() const noexcept
Check if a computation has been performed.
Definition Dominators.H:610
bool dominates(GT &g, Node *root, Node *d, Node *v)
Test whether node d dominates node v.
Definition Dominators.H:510
void invalidate() noexcept
Drop the cached computation, so that the next query computes the dominators again.
Definition Dominators.H:614
Node * get_idom(GT &g, Node *root, Node *node)
Get the immediate dominator of a specific node.
Definition Dominators.H:486
DynList< std::pair< Node *, Node * > > compute_idom(GT &g, Node *root)
Compute immediate dominators.
Definition Dominators.H:427
Node * get_root() const noexcept
Get the root of the last computation.
Definition Dominators.H:625
GT * get_graph() const noexcept
Get the graph of the last computation.
Definition Dominators.H:622
Computes post-dominator tree and post-dominance frontiers of a digraph using the Lengauer-Tarjan algo...
Definition Dominators.H:749
bool post_dominates(const GT &g, Node *exit_node, Node *d, Node *v)
Test whether node d post-dominates node v.
Definition Dominators.H:966
Node * get_ipdom(const GT &g, Node *exit_node, Node *node)
Get the immediate post-dominator of a specific node.
Definition Dominators.H:939
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_odhash.H:657
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
#define TEST(name)
static mt19937 rng
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
void build_post_dominator_tree(const GT &g, typename GT::Node *exit_node, GT &tree, SA sa=SA())
Build the post-dominator tree of a digraph.
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
Definition Dominators.H:678
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_dominators(GT &g, typename GT::Node *root, SA sa=SA())
Compute immediate dominators of a digraph from a root node.
Definition Dominators.H:657
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_post_dominators(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute immediate post-dominators of a digraph from an exit node.
DynMapTree< typename GT::Node *, DynSetTree< typename GT::Node * > > compute_post_dominance_frontiers(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute post-dominance frontiers of a digraph.
DynMapTree< typename GT::Node *, DynSetTree< typename GT::Node * > > compute_dominance_frontiers(GT &g, typename GT::Node *root, SA sa=SA())
Compute dominance frontiers of a digraph.
Definition Dominators.H:700
Main namespace for Aleph-w library functions.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Lazy and scalable dynamic array implementation.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.