https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129266

Drea Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot 
gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #4 from Drea Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #3)
> Before that change it is
>   # c_17(ab) = PHI <1(9), c_10(D)(2)>
>   # d_18 = PHI <d_11(ab)(9), 0(2)>
>   d_11(ab) = d_18 + 1;
>   _1 = e[c_17(ab)];
>   goto _1;
> ;;    succ:       4
> ;;                7
> 
> ;;   basic block 4, loop depth 0
> ;;    pred:       3
>   # d_6(ab) = PHI <d_11(ab)(3)>
>   # c_3(ab) = PHI <c_17(ab)(3)>
> l1:
>   if (d_6(ab) == 1)
> 
> So, bb4 has a single predecessor but only reachable through abnormal edge.
> Guess trying to optimize in this case (like the d_6(ab) == 1 to d_18 == 0)
> case
> is dangerous, and I think it wouldn't be hard to
> --- a/gcc/match.pd    2026-12-03 16:40:53.935819042 +0100
> +++ b/gcc/match.pd    2026-12-03 17:46:45.576803221 +0100
> @@ -11214,7 +11214,10 @@ x == ~x is impossible, so le/ge behave l
>       (if (TREE_OVERFLOW (res)
>         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
>        { constant_boolean_node (cmp == NE_EXPR, type); }
> -      (if (single_use (@3) || wi::to_wide (res) == 0)
> +      (if (single_use (@3)
> +        || (wi::to_wide (res) == 0
> +            && (TREE_CODE (@3) != SSA_NAME
> +                || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (@3))))
>         (cmp @0 { TREE_OVERFLOW (res)
>                ? drop_tree_overflow (res) : res; }))))))))
>  (for cmp (lt le gt ge)
> but am not sure if that is the right thing to do.
> Note, maybe_push_res_to_seq has
>   /* Play safe and do not allow abnormals to be mentioned in
>      newly created statements.  */
>   for (unsigned int i = 0; i < num_ops; ++i)
>     if (TREE_CODE (ops[i]) == SSA_NAME
>         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i]))
>       return NULL_TREE;
> but that doesn't trigger here, we aren't adding any
> SSA_NAME_OCCURS_IN_ABNORMAL_PHI nor they appear in any replacement, we are
> instead removing them (which itself doesn't have to be a problem in cases
> like @0 - @0 being replaced with 0, but here where we replace it with some
> part of the (ab) def stmt it can be a problem.

I need to think about how to handle this.  I don't think the pattern should
have a SSA_NAME_OCCURS_IN_ABNORMAL_PHI check in it since there are many more
patterns which could happen a similar way. So have the check in the match.pd
seems too fragile.
Maybe what could be done is record all of the decls for the ssa names in
SSA_NAME_OCCURS_IN_ABNORMAL_PHI as we match backwards.  And then look to see if
we have an operand that will overlaps there.  This would need changes to
genmatch.  But from understanding this could get complex.

Maybe just record if there was a SSA_NAME_OCCURS_IN_ABNORMAL_PHI on the path
and reject if not a constant but that still gets more complex as we could need
to handle match patterns too.  

Maybe we could have wrap the valuizer to not go back on a
SSA_NAME_OCCURS_IN_ABNORMAL_PHI .  Yes this possible lose out some
optimizations but that might be ok for the cases.  Especially ones like `@0 -
@0` won't be losed out on.  Let me test/try that.

Reply via email to