The following fixes wrong-code when SLP ends up swapping operands of a reduction stmt, confusing code-generation later. Fixed by not doing such swapping.
Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2019-07-31 Richard Biener <rguent...@suse.de> PR tree-optimization/91293 * tree-vect-slp.c (vect_build_slp_tree_2): Do not swap operands of reduction stmts. * gcc.dg/vect/pr91293-1.c: New testcase. * gcc.dg/vect/pr91293-2.c: Likewise. * gcc.dg/vect/pr91293-3.c: Likewise. Index: gcc/tree-vect-slp.c =================================================================== --- gcc/tree-vect-slp.c (revision 273930) +++ gcc/tree-vect-slp.c (working copy) @@ -1296,6 +1296,9 @@ vect_build_slp_tree_2 (vec_info *vinfo, && nops == 2 && oprnds_info[1]->first_dt == vect_internal_def && is_gimple_assign (stmt_info->stmt) + /* Swapping operands for reductions breaks assumptions later on. */ + && STMT_VINFO_DEF_TYPE (stmt_info) != vect_reduction_def + && STMT_VINFO_DEF_TYPE (stmt_info) != vect_double_reduction_def /* Do so only if the number of not successful permutes was nor more than a cut-ff as re-trying the recursive match on possibly each level of the tree would expose exponential Index: gcc/testsuite/gcc.dg/vect/pr91293-1.c =================================================================== --- gcc/testsuite/gcc.dg/vect/pr91293-1.c (nonexistent) +++ gcc/testsuite/gcc.dg/vect/pr91293-1.c (working copy) @@ -0,0 +1,19 @@ +/* { dg-do run } */ +/* { dg-additional-options "-msse4.1" { target { sse4_runtime } } } */ + +long long a; +unsigned b, c; +int d = 62; +void e(long long *f, int p2) { *f = p2; } +int main() +{ + for (int g = 2; g <= d; g++) + { + b += g + 4; + c += 5 - g; + } + e(&a, b); + if (a != 2196) + __builtin_abort (); + return 0; +} Index: gcc/testsuite/gcc.dg/vect/pr91293-2.c =================================================================== --- gcc/testsuite/gcc.dg/vect/pr91293-2.c (nonexistent) +++ gcc/testsuite/gcc.dg/vect/pr91293-2.c (working copy) @@ -0,0 +1,19 @@ +/* { dg-do run } */ +/* { dg-additional-options "-msse4.1" { target { sse4_runtime } } } */ + +long long a; +unsigned b, c; +int d = 62; +void e(long long *f, int p2) { *f = p2; } +int main() +{ + for (int g = 2; g <= d; g++) + { + c += 5 - g; + b += g + 4; + } + e(&a, b); + if (a != 2196) + __builtin_abort (); + return 0; +} Index: gcc/testsuite/gcc.dg/vect/pr91293-3.c =================================================================== --- gcc/testsuite/gcc.dg/vect/pr91293-3.c (nonexistent) +++ gcc/testsuite/gcc.dg/vect/pr91293-3.c (working copy) @@ -0,0 +1,20 @@ +/* { dg-do run } */ +/* { dg-additional-options "-msse4.1" { target { sse4_runtime } } } */ + +long long a; +unsigned b, c; +int d = 62; +void e(long long *f, int p2) { *f = p2; } +int xx = 5, yy = 4; +int main() +{ + for (int g = 2; g <= d; g++) + { + c += xx - g; + b += yy + g; + } + e(&a, b); + if (a != 2196) + __builtin_abort (); + return 0; +}