On Tue, Aug 25, 2026 at 4:35 PM Eikansh Gupta
<[email protected]> wrote:
>
> This is a regression caused by
> https://gcc.gnu.org/cgit/gcc/commit/?id=62993cc9d2200ef7a05f317cdef3262996c0ac10,
> which added variable trip count XOR final value replacement.
>
> The XOR final-value replacement used tree_niter_desc::niter directly to
> compute parity.  That expression is valid only when may_be_zero is false.
> For a do loop with zero latch executions, using it can produce an incorrect
> parity.  Use number_of_latch_executions instead, which incorporates the
> may_be_zero condition, and bail out when it is not available.

Please instead simply test integer_zerop (niter_desc.may_be_zero) before
calling the function like we do for compute_final_value_from_loop_phi_latch.

OK with that change.

Richard.

>         PR tree-optimization/126994
> gcc/ChangeLog:
>
>         * tree-scalar-evolution.cc 
> (analyze_and_compute_bitop_with_inv_effect):
>         Use number_of_latch_executions for XOR parity.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/pr126994.c: New test.
>
> Signed-off-by: Eikansh Gupta <[email protected]>
> ---
>  gcc/testsuite/gcc.dg/tree-ssa/pr126994.c | 31 ++++++++++++++++++++++++
>  gcc/tree-scalar-evolution.cc             |  3 +++
>  2 files changed, 34 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126994.c
>
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126994.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/pr126994.c
> new file mode 100644
> index 00000000000..d4cc421793c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126994.c
> @@ -0,0 +1,31 @@
> +/* PR tree-optimization/126994 */
> +/* { dg-do run } */
> +/* { dg-options "-O1" } */
> +
> +long
> +__attribute__ ((noipa))
> +foo (int a)
> +{
> +  long c = 0;
> +  int d = 0;
> +  for (; a < 2; a++)
> +    do
> +      {
> +       d++;
> +       c ^= 3;
> +      }
> +    while (d < 2);
> +  return c;
> +}
> +
> +int
> +main (void)
> +{
> +  if (foo (0) != 3)
> +    __builtin_abort ();
> +  if (foo (1) != 0)
> +    __builtin_abort ();
> +  if (foo (2) != 0)
> +    __builtin_abort ();
> +  return 0;
> +}
> diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
> index 0466a1d36b6..1a321eddc3d 100644
> --- a/gcc/tree-scalar-evolution.cc
> +++ b/gcc/tree-scalar-evolution.cc
> @@ -3850,6 +3850,9 @@ analyze_and_compute_bitop_with_inv_effect (class loop* 
> loop, tree phidef,
>
>    if (code1 == BIT_XOR_EXPR)
>      {
> +      niter = number_of_latch_executions (loop);
> +      if (niter == chrec_dont_know)
> +       return NULL_TREE;
>        tree niter_type = TREE_TYPE (niter);
>        tree one = build_one_cst (niter_type);
>        tree contributes = fold_build2 (BIT_XOR_EXPR, niter_type,
> --
> 2.34.1
>

Reply via email to