On Wed, Apr 23, 2025 at 6:00 AM Andrew Pinski <quic_apin...@quicinc.com> wrote: > > 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.
OK. Note the same should happen for code == INTEGER_CST where we should return false if the cond is already gimple_cond_false/true_p (but we should still canonicalize, thus make_true/false there or more literally just avoid returning true for the exact same form generated). Richard. > 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_apin...@quicinc.com> > --- > gcc/gimple-fold.cc | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc > index 2381a82d2b1..de1f1a44aa3 100644 > --- a/gcc/gimple-fold.cc > +++ b/gcc/gimple-fold.cc > @@ -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])) > -- > 2.43.0 >