re PR tree-optimization/81196 (Number of iterations found for p!=q but not for p<q)
authorBin Cheng <bin.cheng@arm.com>
Thu, 29 Jun 2017 10:41:28 +0000 (10:41 +0000)
committerBin Cheng <amker@gcc.gnu.org>
Thu, 29 Jun 2017 10:41:28 +0000 (10:41 +0000)
PR tree-optimization/81196
* tree-ssa-loop-niter.c (number_of_iterations_cond): Handle loop
exit condition comparing two IVs.

gcc/testsuite
* gcc.dg/vect/pr81196.c: New.

From-SVN: r249778

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/vect/pr81196.c [new file with mode: 0644]
gcc/tree-ssa-loop-niter.c

index af354ccf0a9b527b08af7b600a5fe2855e236062..bed5b90f86820d8da8fe6be98599c7004d7b8f79 100644 (file)
@@ -1,3 +1,9 @@
+2017-06-29  Bin Cheng  <bin.cheng@arm.com>
+
+       PR tree-optimization/81196
+       * tree-ssa-loop-niter.c (number_of_iterations_cond): Handle loop
+       exit condition comparing two IVs.
+
 2017-06-29  Richard Earnshaw  <rearnsha@arm.com>
 
        * config/arm/parsecpu.awk (gen_comm_data): Add initializer for
index cf4d7e618d574a392d25d7b02244720cefb63a47..c38422a7797cac3329f1e7410f62a7c7b041b24b 100644 (file)
@@ -1,3 +1,8 @@
+2017-06-29  Bin Cheng  <bin.cheng@arm.com>
+
+       PR tree-optimization/81196
+       * gcc.dg/vect/pr81196.c: New.
+
 2017-06-29  Michael Collison  <michael.collison@arm.com>
 
        Fix date on previous ChangeLog entry.
diff --git a/gcc/testsuite/gcc.dg/vect/pr81196.c b/gcc/testsuite/gcc.dg/vect/pr81196.c
new file mode 100644 (file)
index 0000000..46d7a9e
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-require-effective-target vect_perm_short } */
+
+void f(short*p){
+  p=(short*)__builtin_assume_aligned(p,64);
+  short*q=p+256;
+  for(;p!=q;++p,--q){
+    short t=*p;*p=*q;*q=t;
+  }
+}
+void b(short*p){
+  p=(short*)__builtin_assume_aligned(p,64);
+  short*q=p+256;
+  for(;p<q;++p,--q){
+    short t=*p;*p=*q;*q=t;
+  }
+}
+/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 2 "vect" } } */
index 848e812cb96b4efd24b0d831176e41a347730994..5a7cab529bfbab1d27dc72af6ab5896a1c51961e 100644 (file)
@@ -1668,18 +1668,34 @@ number_of_iterations_cond (struct loop *loop,
        exit_must_be_taken = true;
     }
 
-  /* We can handle the case when neither of the sides of the comparison is
-     invariant, provided that the test is NE_EXPR.  This rarely occurs in
-     practice, but it is simple enough to manage.  */
+  /* We can handle cases which neither of the sides of the comparison is
+     invariant:
+
+       {iv0.base, iv0.step} cmp_code {iv1.base, iv1.step}
+     as if:
+       {iv0.base, iv0.step - iv1.step} cmp_code {iv1.base, 0}
+
+     provided that either below condition is satisfied:
+
+       a) the test is NE_EXPR;
+       b) iv0.step - iv1.step is positive integer.
+
+     This rarely occurs in practice, but it is simple enough to manage.  */
   if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step))
     {
       tree step_type = POINTER_TYPE_P (type) ? sizetype : type;
-      if (code != NE_EXPR)
+      tree step = fold_binary_to_constant (MINUS_EXPR, step_type,
+                                          iv0->step, iv1->step);
+
+      /* No need to check sign of the new step since below code takes care
+        of this well.  */
+      if (code != NE_EXPR && TREE_CODE (step) != INTEGER_CST)
        return false;
 
-      iv0->step = fold_binary_to_constant (MINUS_EXPR, step_type,
-                                          iv0->step, iv1->step);
-      iv0->no_overflow = false;
+      iv0->step = step;
+      if (!POINTER_TYPE_P (type))
+       iv0->no_overflow = false;
+
       iv1->step = build_int_cst (step_type, 0);
       iv1->no_overflow = true;
     }