re PR tree-optimization/78005 (172.mgrid and 450.soplex miscompare)
authorBin Cheng <bin.cheng@arm.com>
Wed, 19 Oct 2016 11:02:23 +0000 (11:02 +0000)
committerBin Cheng <amker@gcc.gnu.org>
Wed, 19 Oct 2016 11:02:23 +0000 (11:02 +0000)
PR tree-optimization/78005
* tree-vect-loop-manip.c (vect_gen_prolog_loop_niters): Compute
upper (included) bound for niters of prolog loop.
(vect_gen_scalar_loop_niters): Change parameter VF to VFM1.
Compute niters of scalar loop above which vectorized loop is
preferred, as well as the upper (included) bound for the niters.
(vect_do_peeling): Record niter bound for loops accordingly.

gcc/testsuite
PR tree-optimization/78005
* gcc.dg/vect/pr78005.c: New.
* gcc.target/i386/l_fma_float_1.c: Revise test.
* gcc.target/i386/l_fma_float_2.c: Ditto.
* gcc.target/i386/l_fma_float_3.c: Ditto.
* gcc.target/i386/l_fma_float_4.c: Ditto.
* gcc.target/i386/l_fma_float_5.c: Ditto.
* gcc.target/i386/l_fma_float_6.c: Ditto.
* gcc.target/i386/l_fma_double_1.c: Ditto.
* gcc.target/i386/l_fma_double_2.c: Ditto.
* gcc.target/i386/l_fma_double_3.c: Ditto.
* gcc.target/i386/l_fma_double_4.c: Ditto.
* gcc.target/i386/l_fma_double_5.c: Ditto.
* gcc.target/i386/l_fma_double_6.c: Ditto.

From-SVN: r241339

16 files changed:
gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/vect/pr78005.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/l_fma_double_1.c
gcc/testsuite/gcc.target/i386/l_fma_double_2.c
gcc/testsuite/gcc.target/i386/l_fma_double_3.c
gcc/testsuite/gcc.target/i386/l_fma_double_4.c
gcc/testsuite/gcc.target/i386/l_fma_double_5.c
gcc/testsuite/gcc.target/i386/l_fma_double_6.c
gcc/testsuite/gcc.target/i386/l_fma_float_1.c
gcc/testsuite/gcc.target/i386/l_fma_float_2.c
gcc/testsuite/gcc.target/i386/l_fma_float_3.c
gcc/testsuite/gcc.target/i386/l_fma_float_4.c
gcc/testsuite/gcc.target/i386/l_fma_float_5.c
gcc/testsuite/gcc.target/i386/l_fma_float_6.c
gcc/tree-vect-loop-manip.c

index b50e2e4004e7e01a904df1b960cbba5cc8513458..a212b55af203cde1448d4f60c384b51571dccedd 100644 (file)
@@ -1,3 +1,13 @@
+2016-10-19  Bin Cheng  <bin.cheng@arm.com>
+
+       PR tree-optimization/78005
+       * tree-vect-loop-manip.c (vect_gen_prolog_loop_niters): Compute
+       upper (included) bound for niters of prolog loop.
+       (vect_gen_scalar_loop_niters): Change parameter VF to VFM1.
+       Compute niters of scalar loop above which vectorized loop is
+       preferred, as well as the upper (included) bound for the niters.
+       (vect_do_peeling): Record niter bound for loops accordingly.
+
 2016-10-19  Thomas Schwinge  <thomas@codesourcery.com>
 
        PR lto/77458
        (vect_can_advance_ivs_p): Call iv_phi_p.
        (vect_update_ivs_after_vectorizer): Call iv_phi_p.  Directly insert
        new gimple stmts in basic block.
-       (vect_do_peeling_for_loop_bound):
-       (vect_do_peeling_for_alignment):
        (vect_gen_niters_for_prolog_loop): Rename to...
        (vect_gen_prolog_loop_niters): ...Rename from.  Change parameters and
        adjust implementation.
index ec875aaa5cae5ff394bde31bb3a706a32857ff88..a2707003931387bb7ea4803ad3479ddc62a1b674 100644 (file)
@@ -1,3 +1,20 @@
+2016-10-19  Bin Cheng  <bin.cheng@arm.com>
+
+       PR tree-optimization/78005
+       * gcc.dg/vect/pr78005.c: New.
+       * gcc.target/i386/l_fma_float_1.c: Revise test.
+       * gcc.target/i386/l_fma_float_2.c: Ditto.
+       * gcc.target/i386/l_fma_float_3.c: Ditto.
+       * gcc.target/i386/l_fma_float_4.c: Ditto.
+       * gcc.target/i386/l_fma_float_5.c: Ditto.
+       * gcc.target/i386/l_fma_float_6.c: Ditto.
+       * gcc.target/i386/l_fma_double_1.c: Ditto.
+       * gcc.target/i386/l_fma_double_2.c: Ditto.
+       * gcc.target/i386/l_fma_double_3.c: Ditto.
+       * gcc.target/i386/l_fma_double_4.c: Ditto.
+       * gcc.target/i386/l_fma_double_5.c: Ditto.
+       * gcc.target/i386/l_fma_double_6.c: Ditto.
+
 2016-10-19  Thomas Schwinge  <thomas@codesourcery.com>
 
        PR tree-optimization/78024
diff --git a/gcc/testsuite/gcc.dg/vect/pr78005.c b/gcc/testsuite/gcc.dg/vect/pr78005.c
new file mode 100644 (file)
index 0000000..7cefe73
--- /dev/null
@@ -0,0 +1,48 @@
+/* { dg-require-effective-target vect_int } */
+#include "tree-vect.h"
+
+#define N 20
+int u[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+int z[N] = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,  10, 11, 12, 13, 14, 15, 16, 17, 18};
+int res4[N] = {0, 1, 8, 3, 22, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+int res5[N] = {0, 1, 8, 3, 22, 5, 36, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+int res6[N] = {0, 1, 8, 3, 22, 5, 36, 7, 50, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+int res7[N] = {0, 1, 8, 3, 22, 5, 36, 7, 50, 9, 64, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+int res8[N] = {0, 1, 8, 3, 22, 5, 36, 7, 50, 9, 64, 11, 78, 13, 14, 15, 16, 17, 18, 19};
+int res9[N] = {0, 1, 8, 3, 22, 5, 36, 7, 50, 9, 64, 11, 78, 13, 92, 15, 16, 17, 18, 19};
+int res10[N] = {0, 1, 8, 3, 22, 5, 36, 7, 50, 9, 64, 11, 78, 13, 92, 15, 106, 17, 18, 19};
+
+__attribute__ ((noinline)) void
+foo (int n, int d)
+{
+  int i;
+  for (i = 2; i < n; i++)
+    u[2*i-2] = u[2*i-2] + d * (z[i-1] + z[i] + z[i-1] + z[i] + z[i-1] + z[i]);
+}
+
+#define check_u(x)             \
+  foo (x, 2);                  \
+  for (i = 0; i < N; i++)      \
+    {                          \
+      if (u[i] != res##x[i])   \
+       abort ();               \
+      u[i] = i;                        \
+    }
+
+int main(void)
+{
+  int i;
+
+  check_vect ();
+
+  /* Need to check for all possible vector factors.  */
+  check_u(4);
+  check_u(5);
+  check_u(6);
+  check_u(7);
+  check_u(8);
+  check_u(9);
+  check_u(10);
+
+  return 0;
+}
index ea90a35b50570c5c77bcc96f484e7544ee6e82f4..c8ea28a6ab371ae3a78732ae900a1272aeb12968 100644 (file)
@@ -13,7 +13,7 @@ typedef double adouble __attribute__((aligned(sizeof (double))));
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+pd" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 88 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 80 } } */
index d604d57858d16bc6d041058db9c248210fe14beb..bd873bc854c542fae21299dae3e56650d7395bad 100644 (file)
@@ -13,7 +13,7 @@ typedef double adouble __attribute__((aligned(sizeof (double))));
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+pd" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 88 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 80 } } */
index d1ac6a55efe42fbd7b1f979eead662bbd53ab99e..7677ecb21058e537756801d4ddbbb92de71be34c 100644 (file)
@@ -13,7 +13,7 @@ typedef double adouble __attribute__((aligned(sizeof (double))));
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+pd" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 88 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 80 } } */
index 58cd2722cd9b19189f0b8fa06a4d3cede4723d72..e5de796f5c51b8bb856c89c5182b769264d8da34 100644 (file)
@@ -13,7 +13,7 @@ typedef double adouble __attribute__((aligned(sizeof (double))));
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+pd" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 88 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 80 } } */
index 6005a18d0d7f86431eae94c0202cfe312e729019..dfeb96be01a325ce3b14c3994b97dec4eb71e49f 100644 (file)
@@ -13,7 +13,7 @@ typedef double adouble __attribute__((aligned(sizeof (double))));
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+pd" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 88 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 80 } } */
index 3289baae0a7f60e7cca44d120db25eb2f05fc0e8..13cbd7a35e6fe2850df09afac1c4504eb49a5e39 100644 (file)
@@ -13,7 +13,7 @@ typedef double adouble __attribute__((aligned(sizeof (double))));
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+pd" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+pd" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 88 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 88 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+sd" 80 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+sd" 80 } } */
index 8ecf81cef0a0c044cbcd998fc5214b5ae001be24..f20c009b19c9678f8311a930603f68611f9db962 100644 (file)
@@ -12,7 +12,7 @@
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+ps" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 184 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 176 } } */
index a0cb9c7e35052805fe578293eb37a7a28036851d..ce4efe92f79fa25dfcad7d86c3d63b964639c5cb 100644 (file)
@@ -12,7 +12,7 @@
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+ps" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 184 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 176 } } */
index 9045ce4abe5238390f8a686461a936d4dc52f5ee..91b953ac631f555f0c4eadffa02730f758c910d2 100644 (file)
@@ -12,7 +12,7 @@
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+ps" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 184 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 176 } } */
index 3a552119cb00d33e28b3239b908039d869093e1c..447be12f524a4704fe33d122f9177dc3ccf72eb3 100644 (file)
@@ -12,7 +12,7 @@
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+ps" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 184 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 176 } } */
index 6e5cbead2dd10ea5274efecd6e4285c7cc5aeb48..99a5b969cd4c0024ff47c2d671bfde1f2b5f6be9 100644 (file)
@@ -12,7 +12,7 @@
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+ps" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 184 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 176 } } */
index bf4edcf85d0e2d1cc47dd51898cf2cb406cd7b29..02f4b100111290d7aa57d8b0a8df4ec8640caf82 100644 (file)
@@ -12,7 +12,7 @@
 /* { dg-final { scan-assembler-times "vfmsub\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmadd\[123\]+ps" 8 } } */
 /* { dg-final { scan-assembler-times "vfnmsub\[123\]+ps" 8 } } */
-/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 184 } } */
-/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 184 } } */
+/* { dg-final { scan-assembler-times "vfmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfmsub\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmadd\[123\]+ss" 176 } } */
+/* { dg-final { scan-assembler-times "vfnmsub\[123\]+ss" 176 } } */
index 291ecd940dba5885bfc1c703a6a114128db54d78..6bfd332a1019f7a72365b61a5aba28f4a0b3c47e 100644 (file)
@@ -904,7 +904,7 @@ vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo,
    is the inner type of the vectype)
 
    The computations will be emitted at the end of BB.  We also compute and
-   store upper bound of the result in BOUND.
+   store upper bound (included) of the result in BOUND.
 
    When the step of the data-ref in the loop is not 1 (as in interleaved data
    and SLP), the number of iterations of the prolog must be divided by the step
@@ -941,7 +941,7 @@ vect_gen_prolog_loop_niters (loop_vec_info loop_vinfo,
                          "known peeling = %d.\n", npeel);
 
       iters = build_int_cst (niters_type, npeel);
-      *bound = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) + 1;
+      *bound = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
     }
   else
     {
@@ -976,7 +976,7 @@ vect_gen_prolog_loop_niters (loop_vec_info loop_vinfo,
        iters = fold_build2 (MINUS_EXPR, type, nelements_tree, elem_misalign);
       iters = fold_build2 (BIT_AND_EXPR, type, iters, nelements_minus_1);
       iters = fold_convert (niters_type, iters);
-      *bound = nelements;
+      *bound = nelements - 1;
     }
 
   if (dump_enabled_p ())
@@ -1090,43 +1090,47 @@ vect_build_loop_niters (loop_vec_info loop_vinfo)
     }
 }
 
-/* Calculate the number of iterations under which scalar loop will be
-   preferred than vectorized loop.  NITERS_PROLOG is the number of
-   iterations of prolog loop.  If it's integer const, the integer
-   number is also passed by INT_NITERS_PROLOG.  VF is vector factor;
-   TH is the threshold for vectorized loop if CHECK_PROFITABILITY is
-   true.  This function also store upper bound of the result in BOUND.  */
+/* Calculate the number of iterations above which vectorized loop will be
+   preferred than scalar loop.  NITERS_PROLOG is the number of iterations
+   of prolog loop.  If it's integer const, the integer number is also passed
+   in INT_NITERS_PROLOG.  BOUND_PROLOG is the upper bound (included) of
+   number of iterations of prolog loop.  VFM1 is vector factor minus one.
+   If CHECK_PROFITABILITY is true, TH is the threshold below which scalar
+   (rather than vectorized) loop will be executed.  This function stores
+   upper bound (included) of the result in BOUND_SCALAR.  */
 
 static tree
 vect_gen_scalar_loop_niters (tree niters_prolog, int int_niters_prolog,
-                            int bound_prolog, int vf, int th, int *bound,
-                            bool check_profitability)
+                            int bound_prolog, int vfm1, int th,
+                            int *bound_scalar, bool check_profitability)
 {
   tree type = TREE_TYPE (niters_prolog);
   tree niters = fold_build2 (PLUS_EXPR, type, niters_prolog,
-                            build_int_cst (type, vf));
+                            build_int_cst (type, vfm1));
 
-  *bound = vf + bound_prolog;
+  *bound_scalar = vfm1 + bound_prolog;
   if (check_profitability)
     {
-      th++;
+      /* TH indicates the minimum niters of vectorized loop, while we
+        compute the maximum niters of scalar loop.  */
+      th--;
       /* Peeling for constant times.  */
       if (int_niters_prolog >= 0)
        {
-         *bound = (int_niters_prolog + vf < th
-                                          ? th
-                                          : vf + int_niters_prolog);
-         return build_int_cst (type, *bound);
+         *bound_scalar = (int_niters_prolog + vfm1 < th
+                           ? th
+                           : vfm1 + int_niters_prolog);
+         return build_int_cst (type, *bound_scalar);
        }
-      /* Peeling for unknown times, in this case, prolog loop must
-        execute less than bound_prolog times.  */
-      if (th >=  vf + bound_prolog - 1)
+      /* Peeling for unknown times.  Note BOUND_PROLOG is the upper
+        bound (inlcuded) of niters of prolog loop.  */
+      if (th >=  vfm1 + bound_prolog)
        {
-         *bound = th;
+         *bound_scalar = th;
          return build_int_cst (type, th);
        }
-      /* Need to do runtime comparison, but bound remains the same.  */
-      else if (th > vf)
+      /* Need to do runtime comparison, but BOUND_SCALAR remains the same.  */
+      else if (th > vfm1)
        return fold_build2 (MAX_EXPR, type, build_int_cst (type, th), niters);
     }
   return niters;
@@ -1620,7 +1624,7 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
   tree type = TREE_TYPE (niters), guard_cond;
   basic_block guard_bb, guard_to;
   int prob_prolog, prob_vector, prob_epilog;
-  int bound_prolog = 0, bound_epilog = 0, bound = 0;
+  int bound_prolog = 0, bound_scalar = 0, bound = 0;
   int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
   int prolog_peeling = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
   bool epilog_peeling = (LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo)
@@ -1721,9 +1725,9 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
                       LOOP_VINFO_NITERSM1 (loop_vinfo), niters_prolog);
       niters = vect_build_loop_niters (loop_vinfo);
 
-      /* Prolog iterates at most bound_prolog - 1 times, latch iterates
-        at most bound_prolog - 2 times.  */
-      record_niter_bound (prolog, bound_prolog - 2, false, true);
+      /* Prolog iterates at most bound_prolog times, latch iterates at
+        most bound_prolog - 1 times.  */
+      record_niter_bound (prolog, bound_prolog - 1, false, true);
       delete_update_ssa ();
       adjust_vec_debug_stmts ();
       scev_reset ();
@@ -1754,16 +1758,15 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
         won't be vectorized.  */
       if (skip_vector)
        {
-         /* Guard_cond needs is based on NITERSM1 because NITERS might
-            overflow, so here it is niters_scalar - 1 generated.  In
-            other words, both niters_scalar and bound_epilog are for
-            scalar loop's latch.  */
+         /* Additional epilogue iteration is peeled if gap exists.  */
+         bool peel_for_gaps = LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo);
          tree t = vect_gen_scalar_loop_niters (niters_prolog, prolog_peeling,
-                                               bound_prolog, vf - 1, th - 1,
-                                               &bound_epilog,
+                                               bound_prolog,
+                                               peel_for_gaps ? vf : vf - 1,
+                                               th, &bound_scalar,
                                                check_profitability);
-         guard_cond = fold_build2 (LT_EXPR, boolean_type_node,
-                                   nitersm1, t);
+         /* Build guard against NITERSM1 since NITERS may overflow.  */
+         guard_cond = fold_build2 (LT_EXPR, boolean_type_node, nitersm1, t);
          guard_bb = anchor;
          guard_to = split_edge (loop_preheader_edge (epilog));
          guard_e = slpeel_add_loop_guard (guard_bb, guard_cond,
@@ -1772,7 +1775,7 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
          e = EDGE_PRED (guard_to, 0);
          e = (e != guard_e ? e : EDGE_PRED (guard_to, 1));
          slpeel_update_phi_nodes_for_guard1 (first_loop, epilog, guard_e, e);
-         scale_loop_profile (epilog, prob_vector, bound_epilog);
+         scale_loop_profile (epilog, prob_vector, bound_scalar);
        }
 
       tree niters_vector_mult_vf;
@@ -1807,10 +1810,10 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
       else
        slpeel_update_phi_nodes_for_lcssa (epilog);
 
-      bound = (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) ? vf * 2 : vf) - 2;
+      bound = LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) ? vf - 1 : vf - 2;
       /* We share epilog loop with scalar version loop.  */
-      bound_epilog = MAX (bound, bound_epilog - 1);
-      record_niter_bound (epilog, bound_epilog, false, true);
+      bound = MAX (bound, bound_scalar - 1);
+      record_niter_bound (epilog, bound, false, true);
 
       delete_update_ssa ();
       adjust_vec_debug_stmts ();