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

commit r16-5674-ga651bb353554ecb79898d48dd077b7cd288467df
Author: Andrew Pinski <[email protected]>
Date:   Wed Nov 26 13:55:41 2025 -0800

    reassociation: Fix canonical ordering in some cases
    
    This was noticed in PR122843 were sometimes reassociation
    would create the uncanonical order of operands. This fixes
    the problem by swapping the order as the rewrite happens.
    
    Wstringop-overflow.c needed to be xfailed since it started
    not to warn because well the warning is too dependent on
    the order of operands to MIN_EXPR. This testcase
    failed if we had supplied -fno-tree-reassoc before too;
    but nothing in the IR changes except the order of 2 operands
    of MIN_EXPR. I filed PR 122881 for this xfail.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
    gcc/ChangeLog:
    
            * tree-ssa-reassoc.cc (rewrite_expr_tree): Swap
            oe1 and oe2 if commutative code and not in
            canonical order.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/Wstringop-overflow.c: Xfail, PR 122881.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/testsuite/c-c++-common/Wstringop-overflow.c | 4 ++--
 gcc/tree-ssa-reassoc.cc                         | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/c-c++-common/Wstringop-overflow.c 
b/gcc/testsuite/c-c++-common/Wstringop-overflow.c
index 5757a23141ed..9829fe14116f 100644
--- a/gcc/testsuite/c-c++-common/Wstringop-overflow.c
+++ b/gcc/testsuite/c-c++-common/Wstringop-overflow.c
@@ -90,8 +90,8 @@ void test_strncat (char **d, const char* s, int i)
   }
 
   {
-    size_t n = i < strlen (s) ? i : strlen (s);   /* { dg-message "length 
computed here" } */
-    T (d, s, n);                  /* { dg-message ".strncat\[^\n\r\]* 
specified bound depends on the length of the source argument" } */
+    size_t n = i < strlen (s) ? i : strlen (s);   /* { dg-message "length 
computed here" "PR122881" { xfail *-*-* } } */
+    T (d, s, n);                  /* { dg-message ".strncat\[^\n\r\]* 
specified bound depends on the length of the source argument" "PR122881" { 
xfail *-*-* } } */
   }
 }
 
diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc
index c140f76766eb..6e220e02ecd6 100644
--- a/gcc/tree-ssa-reassoc.cc
+++ b/gcc/tree-ssa-reassoc.cc
@@ -5268,6 +5268,9 @@ rewrite_expr_tree (gimple *stmt, enum tree_code rhs_code, 
unsigned int opindex,
 
       oe1 = ops[opindex];
       oe2 = ops[opindex + 1];
+      if (commutative_tree_code (rhs_code)
+         && tree_swap_operands_p (oe1->op, oe2->op))
+       std::swap (oe1, oe2);
 
       if (rhs1 != oe1->op || rhs2 != oe2->op)
        {

Reply via email to