On Tue, Sep 29, 2026 at 11:45 PM Dominic P <[email protected]> wrote:
>
> When X may be negative, r17-3791 still rewrites M = X % PHI <2^a, 2^b>
> into M = X & PHI <2^a-1, 2^b-1> because M is only compared with zero.
> That keeps the zero-ness of M but not its value, yet the range recorded
> for M before the rewrite stays, so VRP folded M to 0 in the testcase.
> Reset the flow-sensitive info of M in that case.
>
> Bootstrapped and regression tested on x86_64-pc-linux-gnu; dg.exp,
> tree-ssa.exp, execute.exp and the new test were also run on
> aarch64-linux-gnu (qemu); no regressions.
>
> Assisted-by: Claude Opus 5 (Anthropic)
>
> gcc/ChangeLog:
>
>         PR tree-optimization/101179
>         * tree-ssa-forwprop.cc (simplify_phi_result_movdiv): Reset the
>         flow-sensitive info of the result when its value changes.
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optimization/101179
>         * gcc.dg/torture/pr101179.c: New test.
>
> Signed-off-by: Dominic P <[email protected]>
> ---
> OK for trunk?
>
>  gcc/testsuite/gcc.dg/torture/pr101179.c | 28 +++++++++++++++++++++++++
>  gcc/tree-ssa-forwprop.cc                |  2 ++
>  2 files changed, 30 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/torture/pr101179.c
>
> diff --git a/gcc/testsuite/gcc.dg/torture/pr101179.c 
> b/gcc/testsuite/gcc.dg/torture/pr101179.c
> new file mode 100644
> index 00000000000..0493c01b626
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/torture/pr101179.c
> @@ -0,0 +1,28 @@
> +/* { dg-do run } */
> +
> +static int __attribute__((noinline))
> +f (int a, int b, int x, int p, int q)
> +{
> +  if (x > -1 || x < -100)
> +    return 5;
> +  int y;
> +  if (a)
> +    y = p;
> +  else
> +    y = q;
> +  int m = x % y;
> +  if (b)
> +    return 7;
> +  if (m == 0)
> +    return 1;
> +  return 2;
> +}
> +
> +int
> +main (void)
> +{
> +  volatile int one = 1, zero = 0, v = -100;
> +  if (f (one, zero, v, 64, 16) != 2)
> +    __builtin_abort ();
> +  return 0;
> +}
> diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
> index ce2e2a8b169..2c35c99cdf7 100644
> --- a/gcc/tree-ssa-forwprop.cc
> +++ b/gcc/tree-ssa-forwprop.cc
> @@ -4889,6 +4889,8 @@ simplify_phi_result_movdiv (gimple *stmt, tree_code 
> code)
>
>    gimple_assign_set_rhs2 (stmt, new_phires);
>    gimple_assign_set_rhs_code (stmt, new_code);
> +  if (!tree_expr_nonnegative_p (rhs1))
> +    reset_flow_sensitive_info (gimple_assign_lhs (stmt));

Can you instead set a bool for this earlier to reset it?

That is under
    {
      case TRUNC_MOD_EXPR:
      case FLOOR_MOD_EXPR:
        if (!tree_expr_nonnegative_p (rhs1)
            && !use_in_zero_equality (gimple_assign_lhs (stmt), true))
          return false;

do this instead:
bool needs_reset = false;
...
    {
      case TRUNC_MOD_EXPR:
      case FLOOR_MOD_EXPR:
        if (!tree_expr_nonnegative_p (rhs1))
          {
            needs_reset = true;
            if (!use_in_zero_equality (gimple_assign_lhs (stmt), true))
              return false;
          }
...

if (needs_reset)
  reset_flow_sensitive_info (gimple_assign_lhs (stmt));


This should be slightly faster and easier to understand when a reset is needed.
Oh and a comment around `needs_reset = true;` should be similar to
your commit message of why resetting flow sensitive info is needed.

>    update_stmt (stmt);
>
>    gsi = gsi_for_phi (phi);
> --
> 2.55.0
>

Reply via email to