From c1118d8929bed1b516acc4bc433218ccf0c4db6d Mon Sep 17 00:00:00 2001
From: Ramakrishna Upadrasta <Ramakrishna.Upadrasta@inria.fr>
Date: Fri, 22 Jan 2010 17:41:37 +0100
Subject: [PATCH] Fix DealII type problems.

2010-01-22  Ramakrishna Upadrasta <Ramakrishna.Upadrasta@inria.fr>
	    Tobias Grosser  <grosser@fim.uni-passau.de>

	PR middle-end/42644
	PR middle-end/42130
	* graphite-clast-to-gimple.c (gcc_type_for_cloog_iv): Always use
	sufficiently large signed type (signed long long).  If IV does not
	fit in signed long long, then bail safely out of graphite.
	* testsuite/gcc.dg/graphite/run-id-3.c: New
---
 gcc/ChangeLog.graphite                   |   10 ++++++
 gcc/graphite-clast-to-gimple.c           |   30 ++++++++++++++++---
 gcc/testsuite/gcc.dg/graphite/run-id-3.c |   45 ++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/graphite/run-id-3.c

diff --git a/gcc/ChangeLog.graphite b/gcc/ChangeLog.graphite
index 300fb16..6b12e61 100644
--- a/gcc/ChangeLog.graphite
+++ b/gcc/ChangeLog.graphite
@@ -1,3 +1,13 @@
+2010-01-22  Ramakrishna Upadrasta <Ramakrishna.Upadrasta@inria.fr>,
+	    Tobias Grosser  <grosser@fim.uni-passau.de>
+
+	PR middle-end/42644
+	PR middle-end/42130
+	* graphite-clast-to-gimple.c (gcc_type_for_cloog_iv): Always use
+	sufficiently large signed type (signed long long).  If IV does not 
+	fit in signed long long, then bail safely out of graphite. 
+	* testsuite/gcc.dg/graphite/run-id-3.c: New
+
 2010-01-22  Ramakrishna Upadrasta <Ramakrishna.Upadrasta@inria.fr>
 
 	* graphite-scop-detection.c (dot_all_scops): Make calls to dot run in
diff --git a/gcc/graphite-clast-to-gimple.c b/gcc/graphite-clast-to-gimple.c
index 73ffbbc..348a38d 100644
--- a/gcc/graphite-clast-to-gimple.c
+++ b/gcc/graphite-clast-to-gimple.c
@@ -532,23 +532,43 @@ clast_get_body_of_loop (struct clast_stmt *stmt)
   gcc_unreachable ();
 }
 
-/* Given a CLOOG_IV, returns the type that it should have in GCC land.
-   If the information is not available, i.e. in the case one of the
-   transforms created the loop, just return integer_type_node.  */
+/* Given a CLOOG_IV, return the type that CLOOG_IV should have in GCC
+   land.  The selected type is big enough to include the original loop
+   iteration variable, but signed to work with the subtractions CLooG may
+   have introduced.  If such a type is not available, gloog_error is set.
+    
+   TODO: Do not always return long_long, but the smallest possible type,
+   that still holds the original type.
+   TODO: Get the types using CLooG instead.  This enables further
+   optimizations, but needs CLooG support.  */
 
 static tree
 gcc_type_for_cloog_iv (const char *cloog_iv, gimple_bb_p gbb)
 {
   struct ivtype_map_elt_s tmp;
   PTR *slot;
+  tree type;
 
   tmp.cloog_iv = cloog_iv;
   slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, NO_INSERT);
 
   if (slot && *slot)
-    return ((ivtype_map_elt) *slot)->type;
+  {
+    type = ((ivtype_map_elt) *slot)->type;
+     
+    if (!useless_type_conversion_p (long_long_integer_type_node, type))
+    {
+      /* There is no signed type available, that is large
+         enough to hold the original value.  */
+
+      gloog_error = true;
+      return long_long_integer_type_node;
+    }
+
+    return type;
+  }
 
-  return integer_type_node;
+  return long_long_integer_type_node;
 }
 
 /* Returns the induction variable for the loop that gets translated to
diff --git a/gcc/testsuite/gcc.dg/graphite/run-id-3.c b/gcc/testsuite/gcc.dg/graphite/run-id-3.c
new file mode 100644
index 0000000..f52df8d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/graphite/run-id-3.c
@@ -0,0 +1,45 @@
+double Ke[2], ds[2];
+
+void foo(double Ke[2], int i, double ds[],  int column)
+{ 
+    double tt, ts;
+    unsigned long long j; 
+    
+    for (j = 0; j < 2; j++) { 
+	++column;
+          ts = ds[i];
+          if (i == j)
+            tt = 123;
+	  else 
+            tt = 0;
+          Ke[column] = Ke[column] + ts + tt;
+        }
+}
+
+
+int
+main ()
+{ 
+  extern int printf (const char *, ...);
+  int i, j;
+
+  ds[0] = 1.0;
+  ds[1] = 1.0;
+
+  foo(Ke, 0, ds, -1);
+
+#ifdef DBG
+  for (i = 0; i < 1; i++)
+  { 
+    for (j = 0; j < 2; j++) 
+      printf ("%d ",  (int) Ke[j]);
+      printf("# ");
+    }
+
+    printf("\n");
+#endif
+
+  return 0;
+}
+
+
-- 
1.6.2.5

