A loop exit whose condition is an IOR of compares leaves when the compare
that finishes first becomes true.  The loop's latch count is therefore the
minimum of the compares' effective counts, with may_be_zero applied to
each side.

The analysis of an AND of compares is unchanged.  An IOR of compares is
handled by number_of_iterations_ior_exit, which analyzes each compare on its
own and combines the results recursively.  The leaf analysis is repeated in
that function for now.  The minimum is stored as the loop's niter, and the
assumptions of every compare are kept.

A mix of AND and IOR is not handled yet.

gcc/ChangeLog:

        PR tree-optimization/127510
        * tree-ssa-loop-niter.cc (number_of_iterations_ior_exit): New function.
        (number_of_iterations_combined_exit): Update the comment.
        (number_of_iterations_exit_assumptions): Try 
number_of_iterations_ior_exit
        after number_of_iterations_combined_exit.  Skip the single-compare
        analysis when the IOR result is used.

gcc/testsuite/ChangeLog:

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

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr127510-3.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-3.c
new file mode 100644
index 00000000000..7fc3e2c2805
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-3.c
@@ -0,0 +1,27 @@
+/* { 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 "MIN_EXPR <n_" "vect" } } */
+
+void
+f (int *c, unsigned n, unsigned m)
+{
+  unsigned i = 0;
+  unsigned j = 0;
+  int ci, cj;
+
+  if (n == 0 || m == 0)
+    return;
+
+  do
+    {
+      c[i] = j;
+      ++i;
+      ++j;
+      /* Keep the | in GIMPLE.  !((i == n) | (j == m)) would fold.  */
+      ci = (i == n);
+      cj = (j == m);
+    }
+  while ((ci | cj) == 0);
+}
diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index 992bb9f7f9f..cd4181c6331 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -3248,7 +3248,8 @@ loop_only_exit_p (const class loop *loop, basic_block 
*body, const_edge 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.
+   compares.  An | of compares is handled separately by
+   number_of_iterations_ior_exit.
 
    Both sides of each compare must be affine in LOOP with a constant step,
    which includes loop invariants with step zero, and
@@ -3352,6 +3353,124 @@ number_of_iterations_combined_exit (class loop *loop, 
gcond *stmt,
                                    false, safe);
 }
 
+/* Return true if the exit condition of LOOP at STMT combines several compares
+   with |, so that the latch count is the minimum of their effective counts.
+   COND is the condition for leaving the loop.
+
+   The exit is
+
+     if (((p1 == e1) | (p2 == e2) | ...) != 0) exit the loop.
+
+   whose stay condition is "COND == 0", where COND is a tree of BIT_IOR of
+   compares.
+
+   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.  The loop exits as soon as one compare
+   becomes true, that is, when the compare that finishes first is true, so the
+   latch count is the minimum of the effective counts.  On success that minimum
+   is stored in NITER, with may_be_zero folded into it, and the caller uses
+   NITER as the loop count.  Add to *ASSUMPTIONS the assumptions of every
+   compare.  Only single-use names are followed, so a shared operand is not
+   analyzed more than once.  */
+
+static bool
+number_of_iterations_ior_exit (class loop *loop, gcond *stmt, bool safe,
+                              tree cond, 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);
+  if (def_code != BIT_IOR_EXPR)
+    {
+      /* 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.  */
+      enum tree_code code = invert_tree_comparison (def_code, false);
+      /* number_of_iterations_cond counts only !=, <, and <=.  */
+      if (code == EQ_EXPR)
+       return false;
+      tree op0 = gimple_assign_rhs1 (def);
+      tree 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);
+    }
+
+  /* For (p1 == e1) | (p2 == e2), analyze each compare on its own.  The loop
+     exits when the compare that finishes first is true, so the latch count is
+     the minimum of the two counts, with may_be_zero applied, and add the
+     assumptions of every compare.  */
+  class tree_niter_desc right_niter;
+  if (!number_of_iterations_ior_exit (loop, stmt, safe,
+                                     gimple_assign_rhs1 (def), niter,
+                                     assumptions)
+      || !number_of_iterations_ior_exit (loop, stmt, safe,
+                                        gimple_assign_rhs2 (def),
+                                        &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;
+
+  /* Add the assumptions of every compare.  A nested | has already moved its
+     assumptions to *ASSUMPTIONS and left this NITER->assumptions true.  A
+     compare still has them in NITER.  */
+  *assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                             *assumptions, niter->assumptions);
+  *assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                             *assumptions, right_niter.assumptions);
+
+  /* Give up when the assumptions have folded to constant false.  */
+  if (integer_zerop (*assumptions))
+    return false;
+
+  /* The latch count is the minimum of the two 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);
+  niter->niter = fold_build2 (MIN_EXPR, type, left_count, right_count);
+  /* The MIN is the effective count, so may_be_zero is already applied.  */
+  niter->may_be_zero = boolean_false_node;
+  /* The assumptions are in *ASSUMPTIONS.  The caller ANDs that back.  */
+  niter->assumptions = boolean_true_node;
+  /* The loop exits within the smaller of the two bounds.  If the MIN folds to
+     a constant, that constant is the exact count and bounds max.  */
+  if (right_niter.max < niter->max)
+    niter->max = right_niter.max;
+  if (TREE_CODE (niter->niter) == INTEGER_CST)
+    niter->max = wi::to_widest (niter->niter);
+  /* The unroller may miscompile the loop if CMP stays the left compare.  */
+  niter->cmp = ERROR_MARK;
+  niter->bound = NULL_TREE;
+  return true;
+}
+
 /* 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
@@ -3408,9 +3527,12 @@ number_of_iterations_exit_assumptions (class loop *loop, 
const_edge exit,
 
   /* 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.  */
+     as a normal != exit.  An | of compares exits as soon as one compare
+     becomes true, that is, when the compare that finishes first is true;
+     that analysis returns the minimum count itself.  */
   tree combined_assumptions = boolean_true_node;
   bool combined_exit_p = false;
+  bool combined_niter_p = false;
   if (code == EQ_EXPR
       && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
       && integer_zerop (op1))
@@ -3429,8 +3551,24 @@ number_of_iterations_exit_assumptions (class loop *loop, 
const_edge exit,
          combined_assumptions = rep_assumptions;
          combined_exit_p = true;
        }
+      else if (number_of_iterations_ior_exit (loop, stmt, safe, op0,
+                                             &rep_niter, &rep_assumptions))
+       {
+         *niter = rep_niter;
+         combined_assumptions = rep_assumptions;
+         combined_exit_p = true;
+         combined_niter_p = true;
+       }
     }
 
+  /* The | walk already stored the minimum in *NITER.  Skip the single-compare
+     analysis, which would overwrite it.  The braces keep the jump from
+     crossing its initialized locals.  TODO: Replace this jump with
+     "if (!combined_niter_p)" if a goto is not advised.  */
+  if (combined_niter_p)
+    goto combined_niter;
+
+  {
   switch (code)
     {
     case GT_EXPR:
@@ -3499,7 +3637,9 @@ number_of_iterations_exit_assumptions (class loop *loop, 
const_edge exit,
          && niter->max > wi::to_widest (iv_niters))
        niter->max = wi::to_widest (iv_niters);
     }
+  }
 
+combined_niter:
   /* There is no assumptions if the loop is known to be finite.  */
   if (!integer_zerop (niter->assumptions)
       && loop_constraint_set_p (loop, LOOP_C_FINITE))
-- 
2.53.0

Reply via email to