38#include <gtest/gtest.h>
59 for (
int i = 0; i < n; ++i)
60 nodes(
static_cast<size_t>(i)) = g.insert_node(i);
69 for (
auto it = idoms.get_it(); it.has_curr(); it.next_ne())
71 auto [node, idom] = it.get_curr();
72 m.
insert(node->get_info(), idom ? idom->get_info() : -1);
87TEST(Dominators, InvalidateDropsTheCache)
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));
97 ASSERT_EQ(dom.
get_idom(g, n(0), n(3)), n(1));
100 g.insert_arc(n(2), n(3));
103 EXPECT_EQ(dom.
get_idom(g, n(0), n(3)), n(0));
106 EXPECT_EQ(other.
get_idom(g, n(2), n(3)), n(2));
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)));
113TEST(Dominators, PostDominatorsSeeAChangeOfTheGraph)
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));
123 ASSERT_EQ(pd.
get_ipdom(g, n(3), n(0)), n(1));
124 g.insert_arc(n(2), n(3));
125 EXPECT_EQ(pd.
get_ipdom(g, n(3), n(0)), n(3));
131 auto nodes = make_nodes(g, 1);
134 auto m = idom_map(idoms);
136 EXPECT_EQ(
m.
size(), 1U);
145 auto nodes = make_nodes(g, 5);
169 auto nodes = make_nodes(g, 4);
194 auto nodes = make_nodes(g, 5);
221 auto nodes = make_nodes(g, 5);
237TEST(Dominators, ComplexTextbookCFG)
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]);
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]);
265 g.insert_arc(n[11], n[12]);
289 EXPECT_EQ(
m[11], 10);
300TEST(Dominators, UnreachableNodes)
304 auto nodes = make_nodes(g, 4);
310 auto m = idom_map(idoms);
313 EXPECT_EQ(
m.
size(), 3U);
314 EXPECT_EQ(
m.
search(3),
nullptr);
321 auto nodes = make_nodes(g, 2);
338 EXPECT_TRUE(g.get_num_nodes() == 0);
341TEST(Dominators, ErrorHandlingNullRoot)
344 auto nodes = make_nodes(g, 2);
355TEST(Dominators, DominatorTreeStructure)
365 auto nodes = make_nodes(g, 5);
376 EXPECT_EQ(tree.get_num_nodes(), 5U);
377 EXPECT_EQ(tree.get_num_arcs(), 4U);
380 for (
auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
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());
389TEST(Dominators, DominatorTreeLinearChain)
393 auto nodes = make_nodes(g, 4);
401 EXPECT_EQ(tree.get_num_nodes(), 4U);
402 EXPECT_EQ(tree.get_num_arcs(), 3U);
410TEST(Dominators, DominanceReflexive)
413 auto nodes = make_nodes(g, 3);
420 for (
int i = 0; i < 3; ++i)
424TEST(Dominators, RootDominatesAll)
427 auto nodes = make_nodes(g, 5);
435 for (
int i = 0; i < 5; ++i)
447 auto nodes = make_nodes(g, 4);
466TEST(Dominators, DominanceFrontiersIfThenElse)
474 auto nodes = make_nodes(g, 4);
483 EXPECT_TRUE(
df.find(
nodes[0]).is_empty());
488 auto & df1 =
df.find(
nodes[1]);
489 EXPECT_EQ(df1.size(), 1U);
490 EXPECT_EQ(df1.get_first()->get_info(), 3);
493 auto & df2 =
df.find(
nodes[2]);
494 EXPECT_EQ(df2.size(), 1U);
495 EXPECT_EQ(df2.get_first()->get_info(), 3);
498 EXPECT_TRUE(
df.find(
nodes[3]).is_empty());
501TEST(Dominators, DominanceFrontiersLoop)
507 auto nodes = make_nodes(g, 3);
516 auto & df1 =
df.find(
nodes[1]);
517 EXPECT_EQ(df1.size(), 1U);
518 EXPECT_EQ(df1.get_first()->get_info(), 1);
521 auto & df2 =
df.find(
nodes[2]);
522 EXPECT_EQ(df2.size(), 1U);
523 EXPECT_EQ(df2.get_first()->get_info(), 1);
531TEST(Dominators, GetIdomSpecificNode)
534 auto nodes = make_nodes(g, 4);
541 DG::Node * n0 =
nodes[0];
542 DG::Node * n1 =
nodes[1];
543 DG::Node * n2 =
nodes[2];
544 DG::Node * n3 =
nodes[3];
546 EXPECT_EQ(alg.
get_idom(g, n0, n0),
nullptr);
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);
552TEST(Dominators, GetIdomNullNodeThrows)
555 auto nodes = make_nodes(g, 2);
559 EXPECT_THROW(alg.
get_idom(g,
nodes[0],
nullptr), std::domain_error);
567TEST(Dominators, LargeRandomDAG)
572 auto nodes = make_nodes(g,
N);
574 std::mt19937
rng(42);
577 for (
int i = 0; i <
N - 1; ++i)
580 int j = i + 1 +
static_cast<int>(
rng() % std::min(5,
N - i - 1));
585 for (
int k = 0;
k < 2; ++
k)
587 int t = i + 1 +
static_cast<int>(
rng() % (
N - i - 1));
594 auto m = idom_map(idoms);
600 for (
auto it =
m.
get_it(); it.has_curr(); it.next_ne())
602 const auto & [node_id, idom_id] = it.get_curr();
605 EXPECT_GE(idom_id, 0) <<
"Node " << node_id <<
" has no idom";
611 EXPECT_EQ(tree.get_num_nodes(),
m.
size());
612 EXPECT_EQ(tree.get_num_arcs(),
m.
size() - 1);
615TEST(Dominators, PerformanceLargeRandomDAG)
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";
621 constexpr int N = 5000;
623 constexpr long long kBudgetMs = 9000;
625 constexpr long long kBudgetMs = 20000;
629 auto nodes = make_nodes(g,
N);
630 std::mt19937
rng(0xD01A70u);
632 for (
int i = 0; i <
N - 1; ++i)
634 const int max_step = std::min(12,
N - i - 1);
635 const int j = i + 1 +
static_cast<int>(
rng() % max_step);
638 for (
int k = 0;
k < 4; ++
k)
640 const int t = i + 1 +
static_cast<int>(
rng() % (
N - i - 1));
645 const auto t0 = std::chrono::steady_clock::now();
647 auto m = idom_map(idoms);
650 const auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
651 std::chrono::steady_clock::now() - t0).count();
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;
670 auto nodes = make_nodes(g, 3);
678 alg(g,
nodes[0], tree2);
680 EXPECT_EQ(tree1.get_num_nodes(), tree2.get_num_nodes());
681 EXPECT_EQ(tree1.get_num_arcs(), tree2.get_num_arcs());
692 auto nodes = make_nodes(g, 4);
717TEST(Dominators, StringTypedGraph)
722 auto entry = g.insert_node(
"entry");
723 auto body = g.insert_node(
"body");
724 auto exit_node = g.insert_node(
"exit");
726 g.insert_arc(entry, body);
727 g.insert_arc(body, exit_node);
732 for (
auto it = idoms.get_it(); it.has_curr(); it.next_ne())
734 auto [node, idom] = it.get_curr();
735 m[node->get_info()] = idom ? idom->get_info() :
"none";
738 EXPECT_EQ(
m[
"entry"],
"none");
739 EXPECT_EQ(
m[
"body"],
"entry");
740 EXPECT_EQ(
m[
"exit"],
"body");
752 DG::Node*>> & ipdoms)
755 for (
auto it = ipdoms.get_it(); it.has_curr(); it.next_ne())
757 auto [node, ipdom] = it.get_curr();
758 m[node->get_info()] = ipdom ? ipdom->get_info() : -1;
764TEST(PostDominators, SingleNode)
767 auto nodes = make_nodes(g, 1);
770 auto m = ipdom_map(ipdoms);
772 EXPECT_EQ(
m.
size(), 1U);
776TEST(PostDominators, LinearChain)
781 auto nodes = make_nodes(g, 4);
794TEST(PostDominators, DiamondGraph)
803 auto nodes = make_nodes(g, 4);
817TEST(PostDominators, IfThenElseCFG)
829 auto nodes = make_nodes(g, 5);
860 auto nodes = make_nodes(g, 5);
875TEST(PostDominators, PostDominanceReflexive)
880 auto nodes = make_nodes(g, 3);
886 for (
int i = 0; i < 3; ++i)
890TEST(PostDominators, ExitPostDominatesAll)
895 auto nodes = make_nodes(g, 4);
902 for (
int i = 0; i < 4; ++i)
906TEST(PostDominators, PostDominanceFrontiers)
924 auto nodes = make_nodes(g, 4);
933 EXPECT_TRUE(pdf.find(
nodes[3]).is_empty());
936 auto & pdf1 = pdf.find(
nodes[1]);
937 EXPECT_EQ(pdf1.size(), 1U);
938 EXPECT_EQ(pdf1.get_first()->get_info(), 0);
941 auto & pdf2 = pdf.find(
nodes[2]);
942 EXPECT_EQ(pdf2.size(), 1U);
943 EXPECT_EQ(pdf2.get_first()->get_info(), 0);
946 EXPECT_TRUE(pdf.find(
nodes[0]).is_empty());
949TEST(PostDominators, PostDominatorTreeStructure)
954 auto nodes = make_nodes(g, 4);
963 EXPECT_EQ(tree.get_num_nodes(), 4U);
964 EXPECT_EQ(tree.get_num_arcs(), 3U);
967 for (
auto it = tree.get_node_it(); it.has_curr(); it.next_ne())
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());
976TEST(PostDominators, ErrorHandlingNullExit)
979 auto nodes = make_nodes(g, 2);
Dominator and post-dominator tree computation via the Lengauer-Tarjan algorithm.
Generic directed graph (digraph) wrapper template.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Doubly-linked list (defined in tpl_dynList.H).
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.
void build_tree(GT &g, Node *root, GT &tree)
Build the dominator tree as a separate graph.
bool has_computation() const noexcept
Check if a computation has been performed.
bool dominates(GT &g, Node *root, Node *d, Node *v)
Test whether node d dominates node v.
void invalidate() noexcept
Drop the cached computation, so that the next query computes the dominators again.
Node * get_idom(GT &g, Node *root, Node *node)
Get the immediate dominator of a specific node.
DynList< std::pair< Node *, Node * > > compute_idom(GT &g, Node *root)
Compute immediate dominators.
Node * get_root() const noexcept
Get the root of the last computation.
GT * get_graph() const noexcept
Get the graph of the last computation.
Computes post-dominator tree and post-dominance frontiers of a digraph using the Lengauer-Tarjan algo...
bool post_dominates(const GT &g, Node *exit_node, Node *d, Node *v)
Test whether node d post-dominates node v.
Node * get_ipdom(const GT &g, Node *exit_node, Node *node)
Get the immediate post-dominator of a specific node.
Number Theoretic Transform over Z / MOD Z.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
__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)
DynArray< Graph::Node * > nodes
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.
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.
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.
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.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Lazy and scalable dynamic array implementation.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.