From d30078b8e7dc6f94dbd8aa1b56f59bdf479303a8 Mon Sep 17 00:00:00 2001 From: Jeff Law Date: Mon, 10 Oct 2016 14:40:59 -0600 Subject: [PATCH] re PR tree-optimization/71947 (x ^ y not folded to 0 if x == y by DOM) PR tree-optimization/71947 * tree-ssa-dom.c (cprop_into_stmt): Avoid replacing A with B, then B with A within a single statement. PR tree-optimization/71947 * gcc.dg/tree-ssa/pr71947-1.c: New test. * gcc.dg/tree-ssa/pr71947-2.c: New test. * gcc.dg/tree-ssa/pr71947-3.c: New test. * gcc.dg/tree-ssa/pr71947-4.c: New test. * gcc.dg/tree-ssa/pr71947-5.c: New test. * gcc.dg/tree-ssa/pr71947-6.c: New test. From-SVN: r240947 --- gcc/ChangeLog | 6 ++++++ gcc/testsuite/ChangeLog | 10 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c | 19 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c | 16 ++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c | 12 ++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c | 22 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c | 21 +++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c | 16 ++++++++++++++++ gcc/tree-ssa-dom.c | 19 ++++++++++++++++++- 9 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1738bc7e7fe..16e25bfe2d3 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2016-10-10 Jeff Law + + PR tree-optimization/71947 + * tree-ssa-dom.c (cprop_into_stmt): Avoid replacing A with B, then + B with A within a single statement. + 2016-10-10 Bill Schmidt PR tree-optimization/77824 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 04966cfbe5c..e31bcc62453 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,13 @@ +2016-10-10 Jeff Law + + PR tree-optimization/71947 + * gcc.dg/tree-ssa/pr71947-1.c: New test. + * gcc.dg/tree-ssa/pr71947-2.c: New test. + * gcc.dg/tree-ssa/pr71947-3.c: New test. + * gcc.dg/tree-ssa/pr71947-4.c: New test. + * gcc.dg/tree-ssa/pr71947-5.c: New test. + * gcc.dg/tree-ssa/pr71947-6.c: New test. + 2016-10-10 Thomas Koenig PR fortran/77915 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c new file mode 100644 index 00000000000..b03349546fd --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-1.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */ + + +int f(int x, int y) +{ + int ret; + + if (x == y) + ret = x ^ y; + else + ret = 1; + + return ret; +} + +/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;" "dom2" } } */ + + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c new file mode 100644 index 00000000000..de8f88b88d7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-2.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */ + + +int f(int x, int y) +{ + int ret; + if (x == y) + ret = x - y; + else + ret = 1; + + return ret; +} + +/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;" "dom2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c new file mode 100644 index 00000000000..e79847f83c8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-3.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */ + +int f(int x, int y) +{ + int ret = 10; + if (x == y) + ret = x - y; + return ret; +} + +/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;" "dom2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c new file mode 100644 index 00000000000..a881f0d52cd --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-4.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */ + + + +static inline long load(long *p) +{ + long ret; + asm ("movq %1,%0\n\t" : "=r" (ret) : "m" (*p)); + if (ret != *p) + __builtin_unreachable(); + return ret; +} + +long foo(long *mem) +{ + long ret; + ret = load(mem); + return ret + *mem; +} + +/* { dg-final { scan-tree-dump "Folded to: _\[0-9\]+ = _\[0-9\]+ \\* 2" "dom2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c new file mode 100644 index 00000000000..fa679f0f9da --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-5.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */ + + +static inline long load(long *p) +{ + long ret; + asm ("movq %1,%0\n\t" : "=r" (ret) : "m" (*p)); + if (ret != *p) + __builtin_unreachable(); + return ret; +} + +long foo(long *mem) +{ + long ret; + ret = load(mem); + return ret - *mem; +} + +/* { dg-final { scan-tree-dump "Folded to: _\[0-9\]+ = 0;" "dom2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c new file mode 100644 index 00000000000..9cb89cbfe03 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71947-6.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-tree-vrp -fdump-tree-dom-details" } */ + + +int f(int x, int y, int a, int b) +{ + int ret = 10; + if (a == x + && b == y + && a == b) + ret = x - y; + + return ret; +} + +/* { dg-final { scan-tree-dump "Folded to: ret_\[0-9\]+ = 0;" "dom2" { xfail *-*-* } } } */ diff --git a/gcc/tree-ssa-dom.c b/gcc/tree-ssa-dom.c index b007388f3ea..5376ff9fc90 100644 --- a/gcc/tree-ssa-dom.c +++ b/gcc/tree-ssa-dom.c @@ -1731,9 +1731,26 @@ cprop_into_stmt (gimple *stmt) { use_operand_p op_p; ssa_op_iter iter; + tree last_copy_propagated_op = NULL; FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE) - cprop_operand (stmt, op_p); + { + tree old_op = USE_FROM_PTR (op_p); + + /* If we have A = B and B = A in the copy propagation tables + (due to an equality comparison), avoid substituting B for A + then A for B in the trivially discovered cases. This allows + optimization of statements were A and B appear as input + operands. */ + if (old_op != last_copy_propagated_op) + { + cprop_operand (stmt, op_p); + + tree new_op = USE_FROM_PTR (op_p); + if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME) + last_copy_propagated_op = new_op; + } + } } /* Optimize the statement in block BB pointed to by iterator SI -- 2.30.2