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 |  24 ++++
 gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c |  29 ++++
 gcc/tree-ssa-loop-niter.cc                 | 149 ++++++++++++++++++++-
 3 files changed, 200 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..e3c2d1feb87
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-1.c
@@ -0,0 +1,24 @@
+/* { 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 "Loop to be versioned with 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..48ac13b432a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127510-2.c
@@ -0,0 +1,29 @@
+/* { 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 "Loop to be versioned with 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 77f066b8ab1..992bb9f7f9f 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, 
const_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, 
const_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, 
const_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