From f813d16ec532adc9760b0d6d4574b2028034b73a Mon Sep 17 00:00:00 2001 From: Alexandre Oliva Date: Fri, 9 Oct 2015 12:18:24 +0000 Subject: [PATCH] [PR67828] don't unswitch on default defs of non-parms for gcc/ChangeLog PR rtl-optimizatoin/67828 * tree-ssa-loop-unswitch.c: Include tree-ssa.h. (tree_may_unswitch_on): Don't unswitch on expressions involving undefined values. for gcc/testsuite/ChangeLog PR rtl-optimization/67828 * gcc.dg/torture/pr67828.c: New. From-SVN: r228650 --- gcc/ChangeLog | 7 +++++ gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/gcc.dg/torture/pr67828.c | 43 ++++++++++++++++++++++++++ gcc/tree-ssa-loop-unswitch.c | 5 +++ 4 files changed, 60 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/torture/pr67828.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 11c3b47b9d9..e31c1800002 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2015-10-09 Alexandre Oliva + + PR rtl-optimization/67828 + * tree-ssa-loop-unswitch.c: Include tree-ssa.h. + (tree_may_unswitch_on): Don't unswitch on expressions + involving undefined values. + 2015-10-09 Richard Biener * genmatch.c (print_operand): Fix formatting. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 84720481b17..e5673bd1846 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2015-10-09 Alexandre Oliva + + PR rtl-optimization/67828 + * gcc.dg/torture/pr67828.c: New. + 2015-10-09 Ramana Radhakrishnan PR target/67366 diff --git a/gcc/testsuite/gcc.dg/torture/pr67828.c b/gcc/testsuite/gcc.dg/torture/pr67828.c new file mode 100644 index 00000000000..c7b696501a5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr67828.c @@ -0,0 +1,43 @@ +/* Check that we don't misoptimize the final value of d. We used to + apply loop unswitching on if(j), introducing undefined behavior + that the original code wouldn't exercise, and this undefined + behavior would get later passes to misoptimize the loop. */ + +/* { dg-do run } */ + +#include +#include + +int x; + +int __attribute__ ((noinline, noclone)) +xprintf (int d) { + if (d) + { + if (x) + printf ("%d", d); + abort (); + } +} + +int a, b; +short c; + +int +main () +{ + int j, d = 1; + for (; c >= 0; c++) + { + a = d; + d = 0; + if (b) + { + xprintf (0); + if (j) + xprintf (0); + } + } + xprintf (d); + exit (0); +} diff --git a/gcc/tree-ssa-loop-unswitch.c b/gcc/tree-ssa-loop-unswitch.c index 4328d6ae6de..d6faa378ba8 100644 --- a/gcc/tree-ssa-loop-unswitch.c +++ b/gcc/tree-ssa-loop-unswitch.c @@ -32,6 +32,7 @@ along with GCC; see the file COPYING3. If not see #include "internal-fn.h" #include "gimplify.h" #include "tree-cfg.h" +#include "tree-ssa.h" #include "tree-ssa-loop-niter.h" #include "tree-ssa-loop.h" #include "tree-into-ssa.h" @@ -139,6 +140,10 @@ tree_may_unswitch_on (basic_block bb, struct loop *loop) /* Condition must be invariant. */ FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE) { + /* Unswitching on undefined values would introduce undefined + behavior that the original program might never exercise. */ + if (ssa_undefined_value_p (use, true)) + return NULL_TREE; def = SSA_NAME_DEF_STMT (use); def_bb = gimple_bb (def); if (def_bb -- 2.30.2