We have to be able to choose one signedness for the epilogue part of the reduction - for MIN/MAX this means if we have two both have to be of the same signedness.
Bootstrapped and tested on x86_64-unknown-linux-gnu, applied. Richard. 2019-11-05 Richard Biener <rguent...@suse.de> PR tree-optimization/92324 * tree-vect-loop.c (check_reduction_path): For MIN/MAX require all signed or unsigned operations. * gcc.dg/vect/pr92324-3.c: New testcase. Index: gcc/tree-vect-loop.c =================================================================== --- gcc/tree-vect-loop.c (revision 277782) +++ gcc/tree-vect-loop.c (working copy) @@ -2744,6 +2744,7 @@ pop: /* Check whether the reduction path detected is valid. */ bool fail = path.length () == 0; bool neg = false; + int sign = -1; *code = ERROR_MARK; for (unsigned i = 1; i < path.length (); ++i) { @@ -2787,12 +2788,22 @@ pop: TREE_TYPE (gimple_assign_rhs1 (use_stmt)))) ; else if (*code == ERROR_MARK) - *code = use_code; + { + *code = use_code; + sign = TYPE_SIGN (TREE_TYPE (gimple_assign_lhs (use_stmt))); + } else if (use_code != *code) { fail = true; break; } + else if ((use_code == MIN_EXPR + || use_code == MAX_EXPR) + && sign != TYPE_SIGN (TREE_TYPE (gimple_assign_lhs (use_stmt)))) + { + fail = true; + break; + } } return ! fail && ! neg && *code != ERROR_MARK; } Index: gcc/testsuite/gcc.dg/vect/pr92324-3.c =================================================================== --- gcc/testsuite/gcc.dg/vect/pr92324-3.c (nonexistent) +++ gcc/testsuite/gcc.dg/vect/pr92324-3.c (working copy) @@ -0,0 +1,27 @@ +#include "tree-vect.h" + +int a[1024]; +unsigned b[1024]; + +int __attribute__((noipa)) +foo (int n) +{ + int res = 0; + for (int i = 0; i < n; ++i) + { + res = res > a[i] ? res : a[i]; + res = res > b[i] ? res : b[i]; + } + return res; +} + +int main () +{ + check_vect (); + b[3] = (unsigned)__INT_MAX__ + 1; + if (foo (4) != -__INT_MAX__ - 1) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-tree-dump-not "vectorized \[1-9\] loops" "vect" } } */