On Sat, May 10, 2025 at 3:13 AM Andrew Pinski <quic_apin...@quicinc.com> wrote:
>
> This move this canonicalization from forwprop 
> (forward_propagate_into_gimple_cond)
> to gimple-fold.
> This is a step in removing forward_propagate_into_gimple_cond from forwprop.

I don't think fold_stmt should mess with the CFG, so NACK.  The code has to
stay in forwprop, possibly it's also sth for CFG cleanup
cleanup_control_expr_graph
where we convert single-case switch () to if ().

> Bootstrapped and tested on x86_64-linux-gnu.
>
> gcc/ChangeLog:
>
>         * gimple-fold.cc (replace_stmt_with_simplification): Canonicalize
>         `_Bool == 0` and `_Bool != 1` into `_Bool != 0` with swapping
>         the edges.
>
> Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
> ---
>  gcc/gimple-fold.cc | 19 ++++++++++++++++++-
>  1 file changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
> index c3a9f6356d4..e6d1384c416 100644
> --- a/gcc/gimple-fold.cc
> +++ b/gcc/gimple-fold.cc
> @@ -6079,7 +6079,24 @@ replace_stmt_with_simplification (gimple_stmt_iterator 
> *gsi,
>      {
>        gcc_assert (res_op->code.is_tree_code ());
>        auto code = tree_code (res_op->code);
> -      if (TREE_CODE_CLASS (code) == tcc_comparison
> +      /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping 
> edges.  */
> +      if ((TREE_CODE (TREE_TYPE (ops[0])) == BOOLEAN_TYPE
> +          || (INTEGRAL_TYPE_P (TREE_TYPE (ops[0]))
> +              && TYPE_PRECISION (TREE_TYPE (ops[0])) == 1))
> +          && ((code == EQ_EXPR
> +               && integer_zerop (ops[1]))
> +              || (code == NE_EXPR
> +                  && integer_onep (ops[1])))
> +           && gimple_bb (stmt))
> +       {
> +         basic_block bb = gimple_bb (stmt);
> +         gimple_cond_set_code (cond_stmt, NE_EXPR);
> +         gimple_cond_set_lhs (cond_stmt, ops[0]);
> +         gimple_cond_set_rhs (cond_stmt, build_zero_cst (TREE_TYPE 
> (ops[0])));
> +         EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
> +         EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
> +       }
> +      else if (TREE_CODE_CLASS (code) == tcc_comparison
>           /* GIMPLE_CONDs condition may not throw.  */
>           && (!flag_exceptions
>               || !cfun->can_throw_non_call_exceptions
> --
> 2.43.0
>

Reply via email to