From a2b33cc36b5a722130092adeb0b7225adcf35133 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Sat, 15 Feb 2014 09:54:52 +0000 Subject: [PATCH] re PR tree-optimization/60183 (phiprop creates invalid code) 2014-02-15 Richard Biener PR tree-optimization/60183 * tree-ssa-phiprop.c (propagate_with_phi): Avoid speculating loads. (tree_ssa_phiprop): Calculate and free post-dominators. * gcc.dg/torture/pr60183.c: New testcase. From-SVN: r207797 --- gcc/ChangeLog | 7 +++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/torture/pr60183.c | 38 ++++++++++++++++++++++++++++++++++ gcc/tree-ssa-phiprop.c | 9 ++++++++ 4 files changed, 59 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/torture/pr60183.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 59d1671..6d2331b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2014-02-15 Richard Biener + + PR tree-optimization/60183 + * tree-ssa-phiprop.c (propagate_with_phi): Avoid speculating + loads. + (tree_ssa_phiprop): Calculate and free post-dominators. + 2014-02-14 Jeff Law PR rtl-optimization/60131 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0415b74..0891251 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-02-15 Richard Biener + + PR tree-optimization/60183 + * gcc.dg/torture/pr60183.c: New testcase. + 2014-02-14 Jeff Law PR rtl-optimization/60131 diff --git a/gcc/testsuite/gcc.dg/torture/pr60183.c b/gcc/testsuite/gcc.dg/torture/pr60183.c new file mode 100644 index 0000000..d37b4b8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr60183.c @@ -0,0 +1,38 @@ +/* { dg-do run } */ + +/* Large so an out-of-bound read will crash. */ +unsigned char c[0x30001] = { 1 }; +int j = 2; + +static void +foo (unsigned long *x, unsigned char *y) +{ + int i; + unsigned long w = x[0]; + for (i = 0; i < j; i++) + { + w += *y; + y += 0x10000; + w += *y; + y += 0x10000; + } + x[1] = w; +} + +__attribute__ ((noinline, noclone)) void +bar (unsigned long *x) +{ + foo (x, c); +} + +int +main () +{ + unsigned long a[2] = { 0, -1UL }; + asm volatile (""::"r" (c):"memory"); + c[0] = 0; + bar (a); + if (a[1] != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-ssa-phiprop.c b/gcc/tree-ssa-phiprop.c index bf2dcdb..4d66c12 100644 --- a/gcc/tree-ssa-phiprop.c +++ b/gcc/tree-ssa-phiprop.c @@ -309,6 +309,12 @@ propagate_with_phi (basic_block bb, gimple phi, struct phiprop_d *phivn, gimple def_stmt; tree vuse; + /* Only replace loads in blocks that post-dominate the PHI node. That + makes sure we don't end up speculating loads. */ + if (!dominated_by_p (CDI_POST_DOMINATORS, + bb, gimple_bb (use_stmt))) + continue; + /* Check whether this is a load of *ptr. */ if (!(is_gimple_assign (use_stmt) && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME @@ -380,6 +386,7 @@ tree_ssa_phiprop (void) size_t n; calculate_dominance_info (CDI_DOMINATORS); + calculate_dominance_info (CDI_POST_DOMINATORS); n = num_ssa_names; phivn = XCNEWVEC (struct phiprop_d, n); @@ -397,6 +404,8 @@ tree_ssa_phiprop (void) bbs.release (); free (phivn); + free_dominance_info (CDI_POST_DOMINATORS); + return 0; } -- 2.7.4