https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79981
--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #4)
> (In reply to Dominik Vogt from comment #3)
> > (In reply to Richard Biener from comment #2)
> > > of course needs to be conditional on oldlhs being bool and lhs being
> > > integral.
> >
> > Like so?
> >
> > --
> > diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
> > index 9fd45d1..e5e448e 100644
> > --- a/gcc/gimple-fold.c
> > +++ b/gcc/gimple-fold.c
> > @@ -3581,7 +3581,12 @@ fold_builtin_atomic_compare_exchange
> > (gimple_stmt_iterator *gsi)
> > }
> > else
> > gsi_insert_after (gsi, g, GSI_NEW_STMT);
> > - g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
> > + if (TREE_CODE (TREE_TYPE (oldlhs)) == BOOLEAN_TYPE
> > + && INTEGRAL_TYPE_P (itype))
> > + g = gimple_build_assign (oldlhs, NE_EXPR, gimple_assign_lhs (g),
> > + build_zero_cst (TREE_TYPE (gimple_assign_lhs
> > (g))));
> > + else
> > + g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
> > gsi_insert_after (gsi, g, GSI_NEW_STMT);
> > }
> > g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
>
> That looks like a pessimization instead of optimization, perhaps optimizers
> then do a better job at that, but maybe in other cases don't.
As said, we miss canonicalization here and yes, likely it's a pessimization
in some cases.
> Wouldn't it be better to look after building the NOP_EXPR at the immediate
> uses of oldlhs and if any of them is oldlhs != 0 or oldlhs == 0 comparison,
> forward propagate there immediately the rhs1 of the nop and adjust the other
> operand?
You can't do this from folding (looking at immediate uses).
There is the possibility to enhance combine_cond_expr_cond
to handle the if ((_Bool) int != 0) case. In this case it looks like
a missed canonicalization to me given we simplify this to (_Bool) int:
Index: gcc/gimple.c
===================================================================
--- gcc/gimple.c (revision 246023)
+++ gcc/gimple.c (working copy)
@@ -2084,8 +2084,16 @@ canonicalize_cond_expr_cond (tree t)
== BOOLEAN_TYPE))
t = TREE_OPERAND (t, 0);
+ /* For (bool)x use x != 0. */
+ if (CONVERT_EXPR_P (t)
+ && TREE_CODE (TREE_TYPE (t)) == BOOLEAN_TYPE)
+ {
+ tree top0 = TREE_OPERAND (t, 0);
+ t = build2 (NE_EXPR, TREE_TYPE (t),
+ top0, build_int_cst (TREE_TYPE (top0), 0));
+ }
/* For !x use x == 0. */
- if (TREE_CODE (t) == TRUTH_NOT_EXPR)
+ else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
{
tree top0 = TREE_OPERAND (t, 0);
t = build2 (EQ_EXPR, TREE_TYPE (t),
fixes the testcase.