https://gcc.gnu.org/g:b14dc9ddea18a3d1fd7bd05bfaf8c572bf08f428

commit r17-6472-gb14dc9ddea18a3d1fd7bd05bfaf8c572bf08f428
Author: Richard Biener <[email protected]>
Date:   Thu Nov 19 13:43:53 2026 +0100

    tree-optimization/128658 do not set min_input_precision for non-truncatable 
operations
    
    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.
    
    THere is a similar issue with shifts: vect_determine_precisions_from_users
    computes min_input_precision for LSHIFT_EXPR and RSHIFT_EXPR based on the
    shift amount, and that precision only applies to the shifted value, not
    to the shift count.  vect_determine_min_output_precision_1 however
    propagated use_stmt_info->min_input_precision to all uses, including
    the shift count.  For a left shift by an amount in [MIN, MAX] the
    recorded input precision can be smaller than the number of bits
    needed to represent the shift amount itself, so the def chain feeding
    the shift count could be narrowed and compute a wrong shift amount.
    
            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.
            (vect_determine_min_output_precision_1): Return false if the use is
            the shift amount of an LSHIFT_EXPR or RSHIFT_EXPR.
    
            * gcc.dg/vect/bb-slp-over-widen-3.c: New test.
            * gcc.dg/vect/bb-slp-over-widen-4.c: Likewise.

Diff:
---
 gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-3.c | 27 ++++++++++++++++++++
 gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-4.c | 34 +++++++++++++++++++++++++
 gcc/tree-vect-patterns.cc                       | 15 ++++++++++-
 3 files changed, 75 insertions(+), 1 deletion(-)

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 000000000000..7c3f5ab51eb6
--- /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/testsuite/gcc.dg/vect/bb-slp-over-widen-4.c 
b/gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-4.c
new file mode 100644
index 000000000000..1af92e6c66a5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/bb-slp-over-widen-4.c
@@ -0,0 +1,34 @@
+/* { dg-require-effective-target lp64 } */
+/* { dg-additional-options "-O3 -fno-tree-forwprop -fno-tree-vrp" } */
+
+#include "tree-vect.h"
+
+struct S {
+  unsigned long a : 4;
+};
+
+__attribute__((noinline))
+int n(long o, struct S *sp)
+{
+  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 s = e >> 28;
+  long f = 1L << s;
+  sp->a = f;
+  return sp->a;
+}
+
+int main()
+{
+  check_vect ();
+  volatile long o = 809516896;
+  struct S sp;
+  if (n(o, &sp) != 2)
+    __builtin_abort();
+  return 0;
+}
diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc
index cbe834e2aafb..7496ed5b2ae2 100644
--- a/gcc/tree-vect-patterns.cc
+++ b/gcc/tree-vect-patterns.cc
@@ -7129,6 +7129,14 @@ vect_determine_min_output_precision_1 (vec_info *vinfo,
          && use->use != gimple_assign_rhs2_ptr (assign)
          && use->use != gimple_assign_rhs3_ptr (assign))
        return false;
+      /* The input precision recorded for shifts applies only to the
+        shifted value, not the shift amount.  */
+      gassign *use_assign = dyn_cast <gassign *> (use_stmt);
+      if (use_assign
+         && (gimple_assign_rhs_code (use_assign) == LSHIFT_EXPR
+             || gimple_assign_rhs_code (use_assign) == RSHIFT_EXPR)
+         && use->use != gimple_assign_rhs1_ptr (use_assign))
+       return false;
       precision = MAX (precision, use_stmt_info->min_input_precision);
     }
 
@@ -7296,7 +7304,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

Reply via email to