From 3ed01d5408045d80a1efc42fadacfcfb98f14997 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Tue, 20 Aug 2019 12:02:56 +0000 Subject: [PATCH] re PR tree-optimization/37242 (missed FRE opportunity because of signedness of addition) 2019-08-20 Richard Biener PR tree-optimization/37242 * tree-ssa-sccvn.c (visit_nary_op): Also CSE (T)(a + b) to (T)a + (T)b if we know that a + b does not overflow. * gcc.dg/tree-ssa/ssa-fre-80.c: New testcase. From-SVN: r274746 --- gcc/ChangeLog | 6 ++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-80.c | 16 ++++++++++++++++ gcc/tree-ssa-sccvn.c | 12 +++++++++--- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-80.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 7130cdab526..b750de4b421 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2019-08-20 Richard Biener + + PR tree-optimization/37242 + * tree-ssa-sccvn.c (visit_nary_op): Also CSE (T)(a + b) + to (T)a + (T)b if we know that a + b does not overflow. + 2019-08-20 Eric Botcazou PR rtl-optimization/91347 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f18ea5f7dd1..5316e07a434 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-08-20 Richard Biener + + PR tree-optimization/37242 + * gcc.dg/tree-ssa/ssa-fre-80.c: New testcase. + 2019-08-20 Ed Schonberg * gnat.dg/storage_size1.adb: New testcase. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-80.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-80.c new file mode 100644 index 00000000000..01765089062 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-80.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-fre1" } */ + +unsigned long a, b; +void foo (int m, int f) +{ + unsigned long tem = (unsigned long)m; + a = tem + 1; + if (f) + { + int tem2 = m + 1; + b = (unsigned long)tem2; /* Eliminated to a. */ + } +} + +/* { dg-final { scan-tree-dump-times "\\(long unsigned int\\)" 1 "fre1" } } */ diff --git a/gcc/tree-ssa-sccvn.c b/gcc/tree-ssa-sccvn.c index eb7e4be09e6..76cad432ea4 100644 --- a/gcc/tree-ssa-sccvn.c +++ b/gcc/tree-ssa-sccvn.c @@ -4312,8 +4312,12 @@ visit_nary_op (tree lhs, gassign *stmt) operation. */ if (INTEGRAL_TYPE_P (type) && TREE_CODE (rhs1) == SSA_NAME - /* We only handle sign-changes or zero-extension -> & mask. */ - && ((TYPE_UNSIGNED (TREE_TYPE (rhs1)) + /* We only handle sign-changes, zero-extension -> & mask or + sign-extension if we know the inner operation doesn't + overflow. */ + && (((TYPE_UNSIGNED (TREE_TYPE (rhs1)) + || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1)))) && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1))) || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1)))) { @@ -4347,7 +4351,9 @@ visit_nary_op (tree lhs, gassign *stmt) { unsigned lhs_prec = TYPE_PRECISION (type); unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1)); - if (lhs_prec == rhs_prec) + if (lhs_prec == rhs_prec + || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1)))) { gimple_match_op match_op (gimple_match_cond::UNCOND, NOP_EXPR, type, ops[0]); -- 2.30.2