In vect_determine_precisions_from_range, value_precision is computed
from the ranges of the LHS and RHS operands.  For non-truncatable operations
such as TRUNC_DIV_EXPR, the determined input precision only holds if the
operation is actually narrowed.  When doing BB vectorization a stmt
may stay scalar and so a truncated input can cause wrong-code.  I have
opted to disable the propagation for both loop and BB vectorization
though technically loop vectorization should never hit this issue.

Bootstrapped and tested on x86_64-unknown-linux-gnu, will push later.

Richard.

        PR tree-optimization/128658
        * tree-vect-patterns.cc (vect_determine_precisions_from_range): Only
        call vect_set_min_input_precision if vect_truncatable_operation_p.

        * gcc.dg/vect/bb-slp-over-widen-3.c: New test.
---
 .../gcc.dg/vect/bb-slp-over-widen-3.c         | 27 +++++++++++++++++++
 gcc/tree-vect-patterns.cc                     |  7 ++++-
 2 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-3.c

diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-3.c 
b/gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-3.c
new file mode 100644
index 00000000000..7c3f5ab51eb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-3.c
@@ -0,0 +1,27 @@
+/* { dg-require-effective-target lp64 } */
+/* { dg-additional-options "-O3 -fno-tree-forwprop -fno-tree-vrp" } */
+
+#include "tree-vect.h"
+
+__attribute__((noipa))
+int n(long o)
+{
+  long x = 536870911 - 1 / o;
+  long g = (x ^ 3124462952626119202L) & 5121363712956983201L;
+  long j = ~g;
+  long d = x ^ -3124462952626119203L;
+  long i = d & ~5121363712956983201L;
+  long c = ~i;
+  long e = (c & j) ^ 7804438717590242691L;
+  long f = 2147483647 / e;
+  return f & 5;
+}
+
+int main()
+{
+  check_vect ();
+  volatile long o = 809516896;
+  if (n(o) != 4)
+    __builtin_abort();
+  return 0;
+}
diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc
index cbe834e2aaf..07246e3efc7 100644
--- a/gcc/tree-vect-patterns.cc
+++ b/gcc/tree-vect-patterns.cc
@@ -7296,7 +7296,12 @@ vect_determine_precisions_from_range (stmt_vec_info 
stmt_info, gassign *stmt)
                     value_precision, (gimple *) stmt);
 
   vect_set_operation_type (stmt_info, type, value_precision, sign);
-  vect_set_min_input_precision (stmt_info, type, value_precision);
+  /* Non-truncatable operations like division depend on all bits of their
+     inputs if the operation itself is not narrowed - which we cannot
+     guarantee if doing BB vectorization.  So do not claim that only
+     VALUE_PRECISION bits of the inputs are needed.  */
+  if (vect_truncatable_operation_p (code))
+    vect_set_min_input_precision (stmt_info, type, value_precision);
 }
 
 /* Use information about the users of STMT's result to decide whether
-- 
2.51.0

Reply via email to