From 4216dbac10887476feef287854e9e4037f2d1a59 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 21 Mar 2019 21:24:24 -0400 Subject: opt: Optimize redundant comma expressions --- opt.c | 8 +++++--- tests.sh | 16 ++++++++++++++-- tests/test_comma_redundant_false.out | 1 + tests/test_comma_redundant_true.out | 1 + 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 tests/test_comma_redundant_false.out create mode 100644 tests/test_comma_redundant_true.out diff --git a/opt.c b/opt.c index fd2dc3c..1ece9be 100644 --- a/opt.c +++ b/opt.c @@ -595,9 +595,11 @@ static struct expr *optimize_comma_expr(const struct opt_state *state, struct ex if (expr_never_returns(lhs)) { debug_opt(state, "-O1: reachability: %e <==> %e\n", expr, lhs); return extract_child_expr(expr, &expr->lhs); - } - - if (optlevel >= 2 && lhs->pure) { + } else if ((lhs->always_true && rhs == &expr_true) + || (lhs->always_false && rhs == &expr_false)) { + debug_opt(state, "-O1: redundancy elimination: %e <==> %e\n", expr, lhs); + return extract_child_expr(expr, &expr->lhs); + } else if (optlevel >= 2 && lhs->pure) { debug_opt(state, "-O2: purity: %e <==> %e\n", expr, rhs); return extract_child_expr(expr, &expr->rhs); } diff --git a/tests.sh b/tests.sh index 4006534..bd3f399 100755 --- a/tests.sh +++ b/tests.sh @@ -610,6 +610,8 @@ gnu_tests=( test_not_reachability test_comma_reachability test_and_false_or_true + test_comma_redundant_true + test_comma_redundant_false ) bfs_tests=( @@ -2005,11 +2007,21 @@ function test_de_morgan_or() { } function test_and_false_or_true() { - # Test (-a lhs(always_true) false) <==> (! lhs), - # (-a lhs(always_false) true) <==> (! lhs) + # Test (-a lhs(always_true) -false) <==> (! lhs), + # (-a lhs(always_false) -true) <==> (! lhs) bfs_diff basic -prune -false -o -true } +function test_comma_redundant_true() { + # Test (, lhs(always_true) -true) <==> lhs, + bfs_diff basic -prune , -true +} + +function test_comma_redundant_false() { + # Test (, lhs(always_false) -false) <==> lhs, + bfs_diff basic -print -not -prune , -false +} + function test_data_flow_depth() { bfs_diff basic -depth +1 -depth -4 } diff --git a/tests/test_comma_redundant_false.out b/tests/test_comma_redundant_false.out new file mode 100644 index 0000000..15a13db --- /dev/null +++ b/tests/test_comma_redundant_false.out @@ -0,0 +1 @@ +basic diff --git a/tests/test_comma_redundant_true.out b/tests/test_comma_redundant_true.out new file mode 100644 index 0000000..15a13db --- /dev/null +++ b/tests/test_comma_redundant_true.out @@ -0,0 +1 @@ +basic -- cgit v1.2.3