On Tue Oct 13, 2026 at 7:41 PM IST, Richard Biener wrote:
> On Tue, 13 Oct 2026, Raghesh Aloor wrote:
>
>> Teach niter analysis to count a loop exit formed by a chain of equality
>> compares combined with &.  Each compare contains an affine induction
>> variable and a loop invariant, and all compares have the same trip count.
>> This enables vectorization.
>> 
>> +      enum tree_code code = gimple_assign_rhs_code (def);
>> +      /* If it is a BIT_AND, keep walking through its operands.  */
>> +      if (code == BIT_AND_EXPR)
>> +    {
>> +      worklist.safe_push (gimple_assign_rhs1 (def));
>> +      worklist.safe_push (gimple_assign_rhs2 (def));
>
> I think you want to restrict yourself to single-uses to avoid
> growing exponentially for non-simplified (a & b) & (a & b) ...
> with the sub-ands CSEd.
>

This is addressed now.

>> +      continue;
>> +    }
>> +      /* If it is an EQ_EXPR, save this compare.  */
>> +      if (code == EQ_EXPR)
>> +  if (all_compares_same_trip_count (loop, code, op0, op1, &rep_iv,
>> +                                &rep_bound))
>> +    {
>> +      /* All compares finish together, so the condition for staying in
>> +     the loop is NE_EXPR.  */
>> +      code = NE_EXPR;
>> +      op0 = rep_iv;
>> +      op1 = rep_bound;
>> +    }
>> +
>
> So I would have expected a slightly refactored 
> number_of_iterations_exit_assumptions that would simply do sth like
>
>    if (code == EQ_EXPR/NE_EXPR && boolean type op0)
>      {
>        if def of op0 is BIT_AND
>          recurse for op0 of BIT_AND ne/eq zero
>          same for op1
>          merge both niter solutions by picking either niter solution and 
> adding
>          niter w/ maybe_zero (op0) == niter w/ maybe_zero (op1) && all 
> assumptions
>        if def is tcc_comparison
>          process the comparison
>      }
>
> we can similarly combine BIT_IOR conditions using MIN of the niters
> (with their maybe_zero conditioin) and assumptions that both assumptions 
> hold.
>

The revised patch below performs the analysis as per your suggestion.
Please note the following

1. A new function number_of_iterations_combined_exit is introduced,
  which would recursively compute the latch counts and merge the
  assumptions and return a representative compare to the caller along
  with the combined assumptions. In that process we had already
  analyzed the representative compare. The rest of the code after
  this new function call would analyze the same compare again.
  That seems to be redundant. I tried to address that, but that
  refactoring makes the code a bit ugly. Or we may return early
  without further processing too. So, please suggest.

2. The BIT_IOR case is not added yet, but can be added in the next
   version.

3. Unlike, the previous patch, we are not computing the exact
   trip-count of the compare, but attaching the assumptions as you
   suggested. For our test-cases, later passes are able to remove
   the runtime check and avoid versioning. I hope this is what you
   wanted.


>From 95fdc894d7f4dc7b0083b138aac676e491aeb7a7 Mon Sep 17 00:00:00 2001
From: Raghesh Aloor <[email protected]>
Date: Wed, 30 Sep 2026 11:08:56 +0530
Subject: [PATCH v2] tree-optimization/127510 - count a loop exit that combines
 several compares

Teach niter analysis to count a loop exit formed by a chain of compares
combined with &.  Recursively analyze each compare and keep one as the
representative.  Add the assumptions of the other compares and require
their effective latch counts, with may_be_zero applied, to be equal.
This enables vectorization.

Keep the equality in its symbolic form, for example
(ae - a - 4) / 4 == (be - b - 4) / 4.  When it does not fold, the
vectorizer versions the loop using that equality.  Later optimization
passes can remove the runtime check if possible.

For now, only an & of compares is handled.  An | of compares can follow
as a later extension.

gcc/ChangeLog:

        PR tree-optimization/127510
        * tree-ssa-loop-niter.cc (number_of_iterations_combined_exit):
        New function.
        (number_of_iterations_exit_assumptions): Analyze an &-combined
        exit recursively.  Use one compare as the representative and add
        the assumptions of the other compares and the equality of their
        effective latch counts.

gcc/testsuite/ChangeLog:

        PR tree-optimization/127510
        * gcc.dg/tree-ssa/pr127510-1.c: New test.
        * gcc.dg/tree-ssa/pr127510-2.c: New test.
---
 gcc/testsuite/gcc.dg/tree-ssa/pr127510-1.c |  25 ++++
 gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c |  30 +++++
 gcc/tree-ssa-loop-niter.cc                 | 149 ++++++++++++++++++++-
 3 files changed, 202 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr127510-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr127510-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-1.c
new file mode 100644
index 00000000000..57c5845b70c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-1.c
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -fdump-tree-vect-details" } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump-not "no loop iteration estimate on the 
uncounted loop" "vect" } } */
+/* { dg-final { scan-tree-dump "Loop to be versioned with niter assumption" 
"vect" } } */
+
+void
+f (const int *a, const int *b, int *c, int n)
+{
+  if (n == 0)
+    return;
+
+  const int *ae = a + n;
+  const int *be = b + n;
+  int *ce = c + n;
+  do
+    {
+      *c = *a + *b;
+      ++a;
+      ++b;
+      ++c;
+    }
+  while (!((a == ae) & (b == be) & (c == ce)));
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c
new file mode 100644
index 00000000000..e63f23c5287
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -fdump-tree-vect-details" } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump-not "no loop iteration estimate on the 
uncounted loop" "vect" } } */
+/* { dg-final { scan-tree-dump "Loop to be versioned with niter assumption" 
"vect" } } */
+
+void
+f (int *x, int *y, int sn, int sm, int n)
+{
+  int i = sn;
+  int j = sm;
+  if (n == 0)
+    return;
+
+  int ie = sn + n;
+  int je = sm + n;
+  int ci, cj;
+  do
+    {
+      x[i] = y[j];
+      ++i;
+      ++j;
+      /* !((i == ie) & (j == je)) folds to an | of != before niter.
+        That form is not handled currently.  A future update can.  */
+      ci = (i == ie);
+      cj = (j == je);
+    }
+  while ((ci & cj) == 0);
+}
diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index 871bd1bdf2a..30d96aed35b 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -3239,6 +3239,119 @@ loop_only_exit_p (const class loop *loop, basic_block 
*body, const_edge exit)
   return true;
 }
 
+/* Return true if the exit condition of LOOP at STMT combines several
+   compares, so that analyzing one of them is enough when all of them have
+   the same trip count.  COND is the condition for leaving the loop.
+
+   Only an & of compares is handled for now, that is the exit
+
+     if (((p1 == e1) & (p2 == e2) & ...) != 0) exit the loop.
+
+   whose stay condition is "COND == 0", where COND is a tree of BIT_AND of
+   compares.  Other combinations can be added here when they are needed.
+
+   Both sides of each compare must be affine in LOOP with a constant step,
+   which includes loop invariants with step zero, and
+   number_of_iterations_cond must be able to count the compare.  On success
+   *CODE, *OP0 and *OP1 are one such compare, so the caller can analyze the
+   loop as "stay while *OP0 *CODE *OP1", and NITER is its number of
+   iterations.  Add to *ASSUMPTIONS that every other compare has the same
+   trip count, together with the assumptions of those compares.  Only
+   single-use names are followed, so a shared operand is not analyzed more
+   than once.  */
+
+static bool
+number_of_iterations_combined_exit (class loop *loop, gcond *stmt,
+                                   bool safe, tree cond, enum tree_code *code,
+                                   tree *op0, tree *op1,
+                                   class tree_niter_desc *niter,
+                                   tree *assumptions)
+{
+  if (TREE_CODE (cond) != SSA_NAME || !has_single_use (cond))
+    return false;
+  gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
+  if (!def || !flow_bb_inside_loop_p (loop, gimple_bb (def)))
+    return false;
+  enum tree_code def_code = gimple_assign_rhs_code (def);
+
+  /* For (p1 == e1) & (p2 == e2), analyze each compare on its own and keep
+     p1 == e1.  The loop exits when both are true, so assume that both have
+     the same latch count, with may_be_zero applied, and add the assumptions
+     of p2 == e2.  */
+  if (def_code == BIT_AND_EXPR)
+    {
+      class tree_niter_desc right_niter;
+      enum tree_code right_code;
+      tree right_op0, right_op1;
+      if (!number_of_iterations_combined_exit (loop, stmt, safe,
+                                              gimple_assign_rhs1 (def), code,
+                                              op0, op1, niter, assumptions)
+         || !number_of_iterations_combined_exit (loop, stmt, safe,
+                                                 gimple_assign_rhs2 (def),
+                                                 &right_code, &right_op0,
+                                                 &right_op1, &right_niter,
+                                                 assumptions))
+       return false;
+
+      /* The two latch counts can only be compared if they have the same
+        type.  */
+      tree type = TREE_TYPE (niter->niter);
+      if (!types_compatible_p (type, TREE_TYPE (right_niter.niter)))
+       return false;
+
+      /* Compare the latch counts with may_be_zero applied, that is
+        may_be_zero ? 0 : niter.  */
+      tree left_count = fold_build3 (COND_EXPR, type, niter->may_be_zero,
+                                    build_zero_cst (type), niter->niter);
+      tree right_count = fold_build3 (COND_EXPR, type,
+                                     right_niter.may_be_zero,
+                                     build_zero_cst (type), right_niter.niter);
+      /* The two compares become true on the same iteration only if their
+        counts are equal.  */
+      tree same_count = fold_build2 (EQ_EXPR, boolean_type_node,
+                                    left_count, right_count);
+
+      /* Add the assumptions of p2 == e2 and the equality.  p1 == e1 is
+        kept as the representative, so the caller analyzes it again.  */
+      *assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                                 *assumptions, right_niter.assumptions);
+      *assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                                 *assumptions, same_count);
+
+      /* Give up when the assumptions have folded to constant false.  */
+      if (integer_zerop (*assumptions))
+       return false;
+      return true;
+    }
+
+  /* The leaf is a compare, such as p1 == e1.  */
+  if (TREE_CODE_CLASS (def_code) != tcc_comparison)
+    return false;
+
+  /* The loop stays while the compare is false, so p1 == e1 becomes
+     p1 != e1.  */
+  *code = invert_tree_comparison (def_code, false);
+  /* number_of_iterations_cond counts only !=, <, and <=.  */
+  if (*code == EQ_EXPR)
+    return false;
+  *op0 = gimple_assign_rhs1 (def);
+  *op1 = gimple_assign_rhs2 (def);
+
+  tree type = TREE_TYPE (*op0);
+  if (!INTEGRAL_NB_TYPE_P (type) && !POINTER_TYPE_P (type))
+    return false;
+  affine_iv iv0, iv1;
+  /* Both sides must be affine in the loop with a constant step.  */
+  if (!simple_iv (loop, loop_containing_stmt (stmt), *op0, &iv0, false)
+      || !simple_iv (loop, loop_containing_stmt (stmt), *op1, &iv1, false))
+    return false;
+  iv0.base = expand_simple_operations (iv0.base);
+  iv1.base = expand_simple_operations (iv1.base);
+  /* Count the latch executions of this compare, such as p1 != e1.  */
+  return number_of_iterations_cond (loop, type, &iv0, *code, &iv1, niter,
+                                   false, safe);
+}
+
 /* Stores description of number of iterations of LOOP derived from
    EXIT (an exit edge of the LOOP) in NITER.  Returns true if some useful
    information could be derived (and fields of NITER have meaning described
@@ -3290,6 +3403,34 @@ number_of_iterations_exit_assumptions (class loop *loop, 
edge exit,
   if (exit->flags & EDGE_TRUE_VALUE)
     code = invert_tree_comparison (code, false);
 
+  op0 = gimple_cond_lhs (stmt);
+  op1 = gimple_cond_rhs (stmt);
+
+  /* if (((p1 == e1) & (p2 == e2) & ...) != 0) exit the loop.  When every
+     compare has the same trip count, it is enough to analyze one of them
+     as a normal != exit.  */
+  tree combined_assumptions = boolean_true_node;
+  bool combined_exit_p = false;
+  if (code == EQ_EXPR
+      && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
+      && integer_zerop (op1))
+    {
+      enum tree_code rep_code;
+      tree rep_op0, rep_op1;
+      class tree_niter_desc rep_niter;
+      tree rep_assumptions = boolean_true_node;
+      if (number_of_iterations_combined_exit (loop, stmt, safe, op0,
+                                             &rep_code, &rep_op0, &rep_op1,
+                                             &rep_niter, &rep_assumptions))
+       {
+         code = rep_code;
+         op0 = rep_op0;
+         op1 = rep_op1;
+         combined_assumptions = rep_assumptions;
+         combined_exit_p = true;
+       }
+    }
+
   switch (code)
     {
     case GT_EXPR:
@@ -3306,8 +3447,6 @@ number_of_iterations_exit_assumptions (class loop *loop, 
edge exit,
       return false;
     }
 
-  op0 = gimple_cond_lhs (stmt);
-  op1 = gimple_cond_rhs (stmt);
   type = TREE_TYPE (op0);
 
   if (!INTEGRAL_NB_TYPE_P (type)
@@ -3366,6 +3505,12 @@ number_of_iterations_exit_assumptions (class loop *loop, 
edge exit,
       && loop_constraint_set_p (loop, LOOP_C_FINITE))
     niter->assumptions = boolean_true_node;
 
+  /* Add the other compares' assumptions.  */
+  if (combined_exit_p)
+    niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                                     niter->assumptions,
+                                     combined_assumptions);
+
   if (optimize >= 3)
     {
       niter->assumptions = simplify_using_outer_evolutions (loop,
-- 
2.53.0

Reply via email to