This fixes a missed PRE which causes a missed tailcall. Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.
Richard. 2017-04-21 Richard Biener <rguent...@suse.de> PR tree-optimization/80237 * tree-ssa-pre.c (find_leader_in_sets): Add third set argument, defaulted to NULL. (phi_translate_1): Also allow a leader in AVAIL_OUT of pred for a simplified result. * gcc.dg/tree-ssa/tailcall-9.c: New testcase. * gcc.dg/tree-ssa/ldist-pr45948.c: Remove undefined behavior, adjust expected optimizations. Index: gcc/testsuite/gcc.dg/tree-ssa/tailcall-9.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/tailcall-9.c (nonexistent) +++ gcc/testsuite/gcc.dg/tree-ssa/tailcall-9.c (working copy) @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-tailc-details" } */ + +__attribute__((noinline)) +static float f(float x) +{ + return x*x; +} + +static double g(float x) +{ + return x>0 ? f(x) : x+1.0; +} + +float foo(float x) +{ + return g(x); +} + +/* { dg-final { scan-tree-dump "Found tail call" "tailc" } } */ Index: gcc/testsuite/gcc.dg/tree-ssa/ldist-pr45948.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/ldist-pr45948.c (revision 246533) +++ gcc/testsuite/gcc.dg/tree-ssa/ldist-pr45948.c (working copy) @@ -1,14 +1,16 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -ftree-loop-distribution -ftree-loop-distribute-patterns -fdump-tree-ldist-details" } */ +/* { dg-options "-O2 -ftree-loop-distribution -ftree-loop-distribute-patterns -fdump-tree-ldist-details -fdump-tree-optimized" } */ extern void bar(int); void -foo (int i, int n) +foo (unsigned i, unsigned n) { int a[30]; int b[30]; - for (; i < n; i++) + if (n == 0) + return; + for (i=0; i < n; i++) a[i] = b[i] = 0; while (1) @@ -16,7 +18,9 @@ foo (int i, int n) bar (a[i - 1]); } -/* We should apply loop distribution and generate 2 memset (0). */ +/* We should apply loop distribution and generate 1 memset (0). PRE optimizes + away a[] completely. */ -/* { dg-final { scan-tree-dump "distributed: split to 0 loops and 2 library calls" "ldist" } } */ -/* { dg-final { scan-tree-dump-times "generated memset zero" 2 "ldist" } } */ +/* { dg-final { scan-tree-dump "distributed: split to 0 loops and 1 library calls" "ldist" } } */ +/* { dg-final { scan-tree-dump-times "generated memset zero" 1 "ldist" } } */ +/* { dg-final { scan-tree-dump-times "int a" 0 "optimized" } } */ Index: gcc/tree-ssa-pre.c =================================================================== --- gcc/tree-ssa-pre.c (revision 247024) +++ gcc/tree-ssa-pre.c (working copy) @@ -1313,17 +1313,20 @@ translate_vuse_through_block (vec<vn_ref } /* Like bitmap_find_leader, but checks for the value existing in SET1 *or* - SET2. This is used to avoid making a set consisting of the union - of PA_IN and ANTIC_IN during insert. */ + SET2 *or* SET3. This is used to avoid making a set consisting of the union + of PA_IN and ANTIC_IN during insert and phi-translation. */ static inline pre_expr -find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2) +find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2, + bitmap_set_t set3 = NULL) { pre_expr result; result = bitmap_find_leader (set1, val); if (!result && set2) result = bitmap_find_leader (set2, val); + if (!result && set3) + result = bitmap_find_leader (set3, val); return result; } @@ -1478,7 +1481,8 @@ phi_translate_1 (pre_expr expr, bitmap_s else { unsigned value_id = get_expr_value_id (constant); - constant = find_leader_in_sets (value_id, set1, set2); + constant = find_leader_in_sets (value_id, set1, set2, + AVAIL_OUT (pred)); if (constant) return constant; }