On Wed, Apr 23, 2025 at 2:03 AM Richard Biener
<richard.guent...@gmail.com> wrote:
>
> On Wed, Apr 23, 2025 at 5:59 AM Andrew Pinski <quic_apin...@quicinc.com> 
> wrote:
> >
> > To speed up things slightly so not needing to call all the way through
> > to match and simplify, we should return early for true/false on GIMPLE_COND.
>
> I think we'd still canonicalize the various forms matched by
> gimple_cond_true/false_p
> to a standard one - we should go through resimplify2 which should constant 
> fold
> the compare and in the end we do gimple_cond_make_true/false.
>
> I'm also not sure it's worth short-cutting this, it shouldn't be common to 
> fold
> an already canonical if (0) or if (1), no?

yes I agree, I posted a new version of the patch which does similar to
the `bool_name != 0` and what was suggested above:
https://gcc.gnu.org/pipermail/gcc-patches/2025-May/683018.html

Thanks,
Andrew



>
> Richard.
>
> > gcc/ChangeLog:
> >
> >         * gimple-fold.cc (fold_stmt_1): For GIMPLE_COND return early
> >         for true/false.
> >
> > Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
> > ---
> >  gcc/gimple-fold.cc | 13 ++++++++++---
> >  1 file changed, 10 insertions(+), 3 deletions(-)
> >
> > diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
> > index 94d5a1ebbd7..2381a82d2b1 100644
> > --- a/gcc/gimple-fold.cc
> > +++ b/gcc/gimple-fold.cc
> > @@ -6646,12 +6646,19 @@ fold_stmt_1 (gimple_stmt_iterator *gsi, bool 
> > inplace, tree (*valueize) (tree),
> >        break;
> >      case GIMPLE_COND:
> >        {
> > +       gcond *gc = as_a <gcond *> (stmt);
> > +       /* If the cond is already true/false, just return false.  */
> > +       if (gimple_cond_true_p (gc)
> > +           || gimple_cond_false_p (gc))
> > +         {
> > +           fold_undefer_overflow_warnings (false, stmt, 0);
> > +           return false;
> > +         }
> >         /* Canonicalize operand order.  */
> > -       tree lhs = gimple_cond_lhs (stmt);
> > -       tree rhs = gimple_cond_rhs (stmt);
> > +       tree lhs = gimple_cond_lhs (gc);
> > +       tree rhs = gimple_cond_rhs (gc);
> >         if (tree_swap_operands_p (lhs, rhs))
> >           {
> > -           gcond *gc = as_a <gcond *> (stmt);
> >             gimple_cond_set_lhs (gc, rhs);
> >             gimple_cond_set_rhs (gc, lhs);
> >             gimple_cond_set_code (gc,
> > --
> > 2.43.0
> >

Reply via email to