From 8b432c8b8323a67de0ac1c4fce32b80a6eaef9be Mon Sep 17 00:00:00 2001
From: Alexander Monakov <amonakov@ispras.ru>
Date: Fri, 17 Dec 2010 15:54:59 +0300
Subject: [PATCH] re PR middle-end/46761 (-fgraphite-identity produces wrong
 code for array initialization arr[i] = i)

	PR middle-end/46761
	* graphite-clast-to-gimple.c (graphite_create_new_loop_guard): Prefer
	to use unadjusted UB.

testsuite:
	* gcc.dg/graphite/pr46761.c: New.

From-SVN: r167980
---
 gcc/ChangeLog                           |  6 ++++++
 gcc/graphite-clast-to-gimple.c          | 28 ++++++++++++++-----------
 gcc/testsuite/ChangeLog                 |  5 +++++
 gcc/testsuite/gcc.dg/graphite/pr46761.c | 22 +++++++++++++++++++
 4 files changed, 49 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/graphite/pr46761.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 571760f8f814..c5e80126a940 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2010-12-17  Alexander Monakov  <amonakov@ispras.ru>
+
+	PR middle-end/46761
+	* graphite-clast-to-gimple.c (graphite_create_new_loop_guard): Prefer
+	to use unadjusted UB.
+
 2010-12-17  Dodji Seketeli  <dodji@redhat.com>
 
 	* dwarf2out.c (gen_type_die_with_usage): Do not try to emit debug
diff --git a/gcc/graphite-clast-to-gimple.c b/gcc/graphite-clast-to-gimple.c
index eef5ad16abdb..0937fc8eb94a 100644
--- a/gcc/graphite-clast-to-gimple.c
+++ b/gcc/graphite-clast-to-gimple.c
@@ -975,20 +975,24 @@ graphite_create_new_loop_guard (sese region, edge entry_edge,
 				     newivs_index, params_index);
   tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
 				     newivs_index, params_index);
-  tree one = POINTER_TYPE_P (type) ? size_one_node
-    : fold_convert (type, integer_one_node);
-  /* Adding +1 and using LT_EXPR helps with loop latches that have a
-     loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
-     2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
-     However lb < ub + 1 is false, as expected.  */
-  tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
-			     : PLUS_EXPR, type, ub, one);
-
-  /* When ub + 1 wraps around, use lb <= ub.  */
-  if (integer_zerop (ub_one))
+  /* When ub is simply a constant or a parameter, use lb <= ub.  */
+  if (TREE_CODE (ub) == INTEGER_CST || TREE_CODE (ub) == SSA_NAME)
     cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
   else
-    cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
+    {
+      tree one = (POINTER_TYPE_P (type)
+		  ? size_one_node
+		  : fold_convert (type, integer_one_node));
+      /* Adding +1 and using LT_EXPR helps with loop latches that have a
+	 loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
+	 2^k-1 due to integer overflow, and the condition lb <= ub is true,
+	 even if we do not want this.  However lb < ub + 1 is false, as
+	 expected.  */
+      tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
+				 : PLUS_EXPR, type, ub, one);
+
+      cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
+    }
 
   exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index e32bf6e04d0c..67799d0d9a7a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2010-12-17  Alexander Monakov  <amonakov@ispras.ru>
+
+	PR middle-end/46761
+	* gcc.dg/graphite/pr46761.c: New.
+
 2010-12-17  Janus Weil  <janus@gcc.gnu.org>
 
 	PR fortran/46849
diff --git a/gcc/testsuite/gcc.dg/graphite/pr46761.c b/gcc/testsuite/gcc.dg/graphite/pr46761.c
new file mode 100644
index 000000000000..f45398a2b479
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/graphite/pr46761.c
@@ -0,0 +1,22 @@
+/* { dg-do run } */
+/* { dg-options "-O -fgraphite-identity" } */
+
+#define N 128
+
+int
+main ()
+{
+  int arr[N], i, s = 0;
+  for (i = 0; i < N; i++)
+    arr[i] = i;
+
+  for (i = 0; i < N; i++)
+    if (arr[i] != i)
+      __builtin_abort ();
+
+  for (i = 0; i < N; i++)
+    s += arr[i];
+  if (s != (N * (N - 1)) / 2)
+    __builtin_abort ();
+  return 0;
+}
-- 
GitLab