]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gimple-fold: Don't replace `bool_var != 0` with `bool_var` inside GIMPLE_COND
authorAndrew Pinski <quic_apinski@quicinc.com>
Tue, 22 Apr 2025 06:22:02 +0000 (23:22 -0700)
committerAndrew Pinski <quic_apinski@quicinc.com>
Thu, 8 May 2025 18:53:07 +0000 (11:53 -0700)
Since match and simplify will simplify `bool_var != 0` to just `bool_var` and
this is inside a GIMPLE_COND, fold_stmt will return true but nothing has changed.
So let's just reject the replacement if we are replacing with the same simplification
inside replace_stmt_with_simplification. This can speed up things slightly because
now fold_stmt won't return true on all GIMPLE_COND with `bool_var != 0` in it.

gcc/ChangeLog:

* gimple-fold.cc (replace_stmt_with_simplification): Return false
if replacing `bool_var != 0` with `bool_var` in GIMPLE_COND.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/gimple-fold.cc

index fd52b58905c0b89ced4ca0ef0556b1ba7a14c6ee..f801e8b6d419a03bc7a45cc4e6abb261de5a488e 100644 (file)
@@ -6246,8 +6246,16 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
                                          false, NULL_TREE)))
        gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
       else if (code == SSA_NAME)
-       gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
-                                  build_zero_cst (TREE_TYPE (ops[0])));
+       {
+         /* If setting the gimple cond to the same thing,
+            return false as nothing changed.  */
+         if (gimple_cond_code (cond_stmt) == NE_EXPR
+             && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
+             && integer_zerop (gimple_cond_rhs (cond_stmt)))
+           return false;
+         gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
+                                    build_zero_cst (TREE_TYPE (ops[0])));
+       }
       else if (code == INTEGER_CST)
        {
          if (integer_zerop (ops[0]))