https://gcc.gnu.org/g:8adaf6a45cbf3fdda8a62122cc8ea980b13e27e6

commit r17-6500-g8adaf6a45cbf3fdda8a62122cc8ea980b13e27e6
Author: Richard Biener <[email protected]>
Date:   Fri Nov 20 10:52:02 2026 +0100

    tree-optimization/128884 - use available leader when simplifying in VN
    
    When value-numbering (T)(a op b) as ((T)a op (T)b) & mask for a
    widening conversion from a wrapping type, we accepted a wider
    operation that was merely insertable because its type wraps.
    Simplifying the BIT_AND_EXPR then may use range info of that
    value, which is only valid where its definition dominates.  With
    the value defined in a sibling branch the mask can be dropped
    wrongly, and PRE then hoists the unmasked wider operation.
    
    The same issue exists for the unmasked conversion case and for the
    inverse (T)A +- B handling, since rpo_vn_valueize does not resort
    to eliminate_avail for a value that is its own value number.
    
    The following factors the masked and the (T)A +- B cases into
    vn_nary_build_or_lookup_avail which simplifies using the available
    leader if there is one and otherwise, when the value is of wrapping
    type, inserts the unsimplified expression.  For the unmasked
    conversion case the wider operation already has the desired type,
    so value-number to it directly when it is available or of wrapping
    type.  This leaves vn_nary_result_avail_or_insertable_p
    with a single use where the type does not wrap, so replace it with a
    direct availability check.
    
            PR tree-optimization/128884
            * tree-ssa-sccvn.cc (vn_nary_result_avail_or_insertable_p): Remove.
            (vn_nary_build_or_lookup_avail): New.
            (visit_nary_op): Use it for the masked conversion and the
            PLUS_EXPR/MINUS_EXPR special cases instead of checking
            vn_nary_result_avail_or_insertable_p and simplifying with
            a possibly not available value.  Use the wider operation
            directly for the unmasked conversion case.  Check availability
            directly for the negated operand of the MULT_EXPR/RDIV_EXPR case.
    
            * gcc.dg/torture/pr128884.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr128884.c | 25 ++++++++++
 gcc/tree-ssa-sccvn.cc                   | 83 ++++++++++++++++++++-------------
 2 files changed, 76 insertions(+), 32 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr128884.c 
b/gcc/testsuite/gcc.dg/torture/pr128884.c
new file mode 100644
index 000000000000..5421f52d0454
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128884.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+
+unsigned char a;
+
+unsigned long __attribute__((noipa))
+f (unsigned long x)
+{
+  if (a >= 7)
+    {
+      unsigned char t = a * 2;
+      x = (x * (unsigned long) a) ^ t;
+    }
+  else
+    x += (unsigned long) a * 2;
+  return x;
+}
+
+int
+main ()
+{
+  a = 200;
+  if (f (0) != 144)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 57099021efc9..ae72f32a024c 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -5745,17 +5745,34 @@ valueized_wider_op (tree wide_type, tree op, bool 
allow_truncate)
   return NULL_TREE;
 }
 
-/* Return true if RESULT, the result of a value-number lookup, may be
-   used at the statement being visited.  A result of wrapping type can
-   be inserted for code hoisting without introducing undefined
-   overflow; anything else has to be available.  See PR86554.  */
+/* Return a value-number for RES_OP whose first operand is VAL, the
+   result of a value-number lookup, or NULL_TREE if VAL may not be used
+   at the statement being visited.  When VAL has a leader available in
+   the current block simplify using that, otherwise VAL is a leader
+   that does not dominate the statement being visited and using SSA
+   name info such as range information of it is not valid.  We can
+   still insert the unsimplified expression if VAL is of wrapping type
+   since that can be inserted for code hoisting without introducing
+   undefined overflow, see PR86554.  See PR128884.  */
 
-static bool
-vn_nary_result_avail_or_insertable_p (tree result)
+static tree
+vn_nary_build_or_lookup_avail (gimple_match_op *res_op, tree val)
 {
-  return (TYPE_OVERFLOW_WRAPS (TREE_TYPE (result))
-         || (rpo_avail && vn_context_bb
-             && rpo_avail->eliminate_avail (vn_context_bb, result)));
+  tree leader = val;
+  if (TREE_CODE (val) == SSA_NAME)
+    leader = (rpo_avail && vn_context_bb
+             ? rpo_avail->eliminate_avail (vn_context_bb, val) : NULL_TREE);
+  if (leader)
+    {
+      res_op->ops[0] = leader;
+      return vn_nary_build_or_lookup (res_op);
+    }
+  if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
+    {
+      res_op->ops[0] = val;
+      return vn_nary_build_or_lookup_1 (res_op, true, false);
+    }
+  return NULL_TREE;
 }
 
 /* If OP is an SSA name defined by a conversion from an integral type,
@@ -5831,8 +5848,7 @@ visit_nary_op (tree lhs, gassign *stmt)
                {
                  ops[0] = vn_nary_op_lookup_pieces
                      (2, gimple_assign_rhs_code (def), type, ops, NULL);
-                 /* We have wider operation available.  */
-                 if (ops[0] && vn_nary_result_avail_or_insertable_p (ops[0]))
+                 if (ops[0])
                    {
                      unsigned lhs_prec = TYPE_PRECISION (type);
                      unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
@@ -5840,16 +5856,15 @@ visit_nary_op (tree lhs, gassign *stmt)
                          || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
                              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
                        {
-                         gimple_match_op match_op (gimple_match_cond::UNCOND,
-                                                   NOP_EXPR, type, ops[0]);
-                         result = vn_nary_build_or_lookup (&match_op);
-                         if (result)
-                           {
-                             bool changed = set_ssa_val_to (lhs, result);
-                             if (TREE_CODE (result) == SSA_NAME)
-                               vn_nary_op_insert_stmt (stmt, result);
-                             return changed;
-                           }
+                         /* The wider operation computes the same value.
+                            Use it if it is available or can be inserted
+                            for code hoisting, see PR86554.  */
+                         if (TREE_CODE (ops[0]) != SSA_NAME
+                             || TYPE_OVERFLOW_WRAPS (type)
+                             || (rpo_avail && vn_context_bb
+                                 && rpo_avail->eliminate_avail (vn_context_bb,
+                                                                ops[0])))
+                           result = ops[0];
                        }
                      else
                        {
@@ -5859,14 +5874,15 @@ visit_nary_op (tree lhs, gassign *stmt)
                                                    BIT_AND_EXPR,
                                                    TREE_TYPE (lhs),
                                                    ops[0], mask);
-                         result = vn_nary_build_or_lookup (&match_op);
-                         if (result)
-                           {
-                             bool changed = set_ssa_val_to (lhs, result);
-                             if (TREE_CODE (result) == SSA_NAME)
-                               vn_nary_op_insert_stmt (stmt, result);
-                             return changed;
-                           }
+                         result = vn_nary_build_or_lookup_avail (&match_op,
+                                                                 ops[0]);
+                       }
+                     if (result)
+                       {
+                         bool changed = set_ssa_val_to (lhs, result);
+                         if (TREE_CODE (result) == SSA_NAME)
+                           vn_nary_op_insert_stmt (stmt, result);
+                         return changed;
                        }
                    }
                }
@@ -5924,11 +5940,11 @@ visit_nary_op (tree lhs, gassign *stmt)
        const tree narrow_val
          = vn_nary_op_lookup_pieces (2, code, ntype, ops, NULL);
        /* We have a narrower or sign-changed operation available.  */
-       if (narrow_val && vn_nary_result_avail_or_insertable_p (narrow_val))
+       if (narrow_val)
          {
            gimple_match_op match_op (gimple_match_cond::UNCOND,
                                      NOP_EXPR, type, narrow_val);
-           result = vn_nary_build_or_lookup (&match_op);
+           result = vn_nary_build_or_lookup_avail (&match_op, narrow_val);
            if (result)
              {
                const bool changed = set_ssa_val_to (lhs, result);
@@ -6052,7 +6068,10 @@ visit_nary_op (tree lhs, gassign *stmt)
                     see if such an operation is available.  */
                  ops[i] = vn_nary_op_lookup_pieces (1, NEGATE_EXPR, type,
                                                     &rhs[i], NULL);
-                 if (ops[i] && !vn_nary_result_avail_or_insertable_p (ops[i]))
+                 if (ops[i]
+                     && !(rpo_avail && vn_context_bb
+                          && rpo_avail->eliminate_avail (vn_context_bb,
+                                                         ops[i])))
                    ops[i] = NULL_TREE;
                }
              ops[j] = rhs[j];

Reply via email to