Patchwork [Cilkplus] Array Notation Max/Min Index fix

login
register
mail settings
Submitter Iyer, Balaji V
Date Jan. 26, 2012, 11:46 p.m.
Message ID <BF230D13CA30DD48930C31D409933000022978@FMSMSX101.amr.corp.intel.com>
Download mbox | patch
Permalink /patch/138084/
State New
Headers show

Comments

Iyer, Balaji V - Jan. 26, 2012, 11:46 p.m.
Hello Everyone,
    This patch is for the Cilkplus branch affecting both the C and C++ Compilers. This patch will fix a bug for max and min-index builtin function. The original implemention was comparing the max/min index with the value in the array. This patch will add a new variable and store the value in the array to this variable.

Thanking You,

Yours Sincerely,

Balaji V. Iyer.

Patch

diff --git a/gcc/ChangeLog.cilk b/gcc/ChangeLog.cilk
index 98e0528..4297c91 100644
--- a/gcc/ChangeLog.cilk
+++ b/gcc/ChangeLog.cilk
@@ -1,5 +1,10 @@ 
 2012-01-26  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
+	* c-array-notation.c (fix_builtin_array_notation_fn): Initialized the
+	maximum/minimum value for max/min index builtin function.
+
+2012-01-26  Balaji V. Iyer  <balaji.v.iyer@intel.com>
+
 	* array-notation-common.c (array_notation_init_builtins): Added the
 	internal array notation function "__sec_reduce_implicit_index."
 	(is_sec_implicit_index_fn): New function.
diff --git a/gcc/c-array-notation.c b/gcc/c-array-notation.c
index 3ff52dc..d4a94f2 100644
--- a/gcc/c-array-notation.c
+++ b/gcc/c-array-notation.c
@@ -1379,7 +1379,7 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
 {
   tree new_var_type = NULL_TREE, func_parm, new_expr, new_yes_expr, new_no_expr;
   tree array_ind_value = NULL_TREE, new_no_ind, new_yes_ind, new_no_list;
-  tree new_yes_list, new_cond_expr, new_var_init;
+  tree new_yes_list, new_cond_expr, new_var_init, new_exp_init;
   an_reduce_type an_type = REDUCE_UNKNOWN;
   tree *array_list = NULL;
   int list_size = 0;
@@ -1719,6 +1719,9 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
       new_var_init = build_modify_expr
 	(UNKNOWN_LOCATION, *new_var, TREE_TYPE (*new_var), NOP_EXPR,
 	 UNKNOWN_LOCATION, build_zero_cst (new_var_type), new_var_type);
+      new_exp_init = build_modify_expr
+	(UNKNOWN_LOCATION, array_ind_value, TREE_TYPE (array_ind_value),
+	 NOP_EXPR, UNKNOWN_LOCATION, func_parm, TREE_TYPE (func_parm));
       new_no_ind = build_modify_expr
 	(UNKNOWN_LOCATION, *new_var, TREE_TYPE (*new_var), NOP_EXPR,
 	 UNKNOWN_LOCATION, *new_var, TREE_TYPE (*new_var));
@@ -1766,6 +1769,9 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
       new_var_init = build_modify_expr
 	(UNKNOWN_LOCATION, *new_var, TREE_TYPE (*new_var), NOP_EXPR,
 	 UNKNOWN_LOCATION, build_zero_cst (new_var_type), new_var_type);
+      new_exp_init = build_modify_expr
+	(UNKNOWN_LOCATION, array_ind_value, TREE_TYPE (array_ind_value),
+	 NOP_EXPR, UNKNOWN_LOCATION, func_parm, TREE_TYPE (func_parm));
       new_no_ind = build_modify_expr
 	(UNKNOWN_LOCATION, *new_var, TREE_TYPE (*new_var), NOP_EXPR,
 	 UNKNOWN_LOCATION, *new_var, TREE_TYPE (*new_var));
@@ -1817,12 +1823,12 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
   for (ii = 0; ii < rank; ii++)
     append_to_statement_list (ind_init [ii], &loop);
 
+  if (an_type == REDUCE_MAX_INDEX || an_type == REDUCE_MIN_INDEX)
+    append_to_statement_list (new_exp_init, &loop);
   append_to_statement_list (new_var_init, &loop);
   
   for (ii = 0; ii < rank; ii++)
     {
-      /* append_to_statement_list (ind_init [ii], &loop); */
-
       append_to_statement_list
 	(build1 (LABEL_EXPR, void_type_node, if_stmt_label[ii]), &loop);
       append_to_statement_list
diff --git a/gcc/cp/ChangeLog.cilk b/gcc/cp/ChangeLog.cilk
index cf9d993..dff1fb5 100644
--- a/gcc/cp/ChangeLog.cilk
+++ b/gcc/cp/ChangeLog.cilk
@@ -1,5 +1,18 @@ 
 2012-01-26  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
+	* cp-array-notation.c (fix_builtin_array_notation_fn): Added new var
+	called "array_ind_value," that stores the max/min value and the *new_var
+	stores the index.  Also, replaced all build_int_cst with build_one_cst
+	and build_zero_cst.  Also, in REDUCE_ANY_ZEROS and REDUCE_ALL_ZEROS
+	cases, replaced comparing with func_parm with a new_cond_expr.  Also
+	initialized the maximum/minimum value for the max/min index builtin
+	function.
+	(contains_array_notation_expr): Deferenced array_list in if statement.
+	* typeck.c (convert_for_assignment): Checked if the rhs contains array
+	notation expr, if so we don't do any conversion.
+
+2012-01-26  Balaji V. Iyer  <balaji.v.iyer@intel.com>
+
 	* cp-array-notation.c (extract_array_notation_exprs): Added a check
 	to see if the call expr is sec_implicit_index function.
 	(replace_array_notations): Likewise.
diff --git a/gcc/cp/cp-array-notation.c b/gcc/cp/cp-array-notation.c
index 2a27c62..11bc99a 100644
--- a/gcc/cp/cp-array-notation.c
+++ b/gcc/cp/cp-array-notation.c
@@ -1294,7 +1294,8 @@  static tree
 fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
 {
   tree new_var_type = NULL_TREE, func_parm, new_expr, new_yes_expr, new_no_expr;
-  tree new_var_init;
+  tree array_ind_value = NULL_TREE, new_no_ind, new_yes_ind, new_no_list;
+  tree new_yes_list, new_cond_expr, new_var_init, new_exp_init;
   an_reduce_type an_type = REDUCE_UNKNOWN;
   tree *array_list = NULL;
   int list_size = 0;
@@ -1538,55 +1539,55 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
 
   *new_var = build_decl (UNKNOWN_LOCATION, VAR_DECL, NULL_TREE, new_var_type);
   gcc_assert (*new_var);
+  if (an_type == REDUCE_MAX_INDEX || an_type == REDUCE_MIN_INDEX)
+    array_ind_value = build_decl
+      (UNKNOWN_LOCATION, VAR_DECL, NULL_TREE, TREE_TYPE (func_parm));
 
   switch (an_type)
     {
     case REDUCE_ADD:
       new_var_init = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (new_var_type, 0), 1);
+	(*new_var, NOP_EXPR, build_zero_cst (new_var_type), 1);
       new_expr = build_x_modify_expr (*new_var, PLUS_EXPR, func_parm, 1);
       break;
     case REDUCE_MUL:
       new_var_init = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (new_var_type, 1), 1);
+	(*new_var, NOP_EXPR, build_one_cst (new_var_type), 1);
       new_expr = build_x_modify_expr (*new_var, MULT_EXPR, func_parm, 1);
       break;
     case REDUCE_ALL_ZEROS:
       new_var_init = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (new_var_type, 1), 1);
+	(*new_var, NOP_EXPR, build_one_cst (new_var_type), 1);
       /* Initially you assume everything is zero, now if we find a case where
        * it is NOT true, then we set the result to false. Otherwise
        * we just keep the previous value
        */
       new_yes_expr = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (TREE_TYPE (*new_var), 0), 1);
+	(*new_var, NOP_EXPR, build_zero_cst (TREE_TYPE (*new_var)), 1);
       new_no_expr = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
-      new_expr = build_x_conditional_expr (func_parm, new_yes_expr,
+      new_cond_expr = build2 (NE_EXPR, TREE_TYPE (func_parm), func_parm,
+			      build_zero_cst (TREE_TYPE (func_parm)));
+      new_expr = build_x_conditional_expr (new_cond_expr, new_yes_expr,
 					   new_no_expr, 1);
       break;
     case REDUCE_ANY_ZEROS:
       new_var_init = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (new_var_type, 0), 1);
+	(*new_var, NOP_EXPR, build_zero_cst (new_var_type), 1);
       /* Initially we assume there are NO zeros in the list. When we find
        * a non-zero, we keep the previous value. If we find a zero, we
        * set the value to true
        */
       new_no_expr = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (TREE_TYPE (*new_var), 1), 1);
+	(*new_var, NOP_EXPR, build_one_cst (TREE_TYPE (*new_var)), 1);
       new_yes_expr = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
-      new_expr = build_x_conditional_expr (func_parm, new_yes_expr,
+      new_cond_expr = build2 (NE_EXPR, TREE_TYPE (func_parm), func_parm,
+			      build_zero_cst (TREE_TYPE (func_parm)));
+      new_expr = build_x_conditional_expr (new_cond_expr, new_yes_expr,
 					   new_no_expr, 1);      
       break;
     case REDUCE_MAX:
-      if (INTEGRAL_TYPE_P (new_var_type))
-	new_var_init = build_x_modify_expr
-	  (*new_var, NOP_EXPR, TYPE_MIN_VALUE (new_var_type), 1);
-      else /* Otherwise, we pick the first item in the array */
-	new_var_init = build_x_modify_expr
-	  (*new_var, NOP_EXPR,
-	   build_array_ref (UNKNOWN_LOCATION,
-			    TREE_OPERAND (array_operand[0], 0),
-			    build_int_cst (integer_type_node, 0)), 1);
+      /* set initial value as the first element in the list */
+      new_var_init = build_x_modify_expr (*new_var, NOP_EXPR, func_parm, 1);
       new_no_expr  = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
       new_yes_expr = build_x_modify_expr (*new_var, NOP_EXPR, func_parm, 1);
       new_expr = build_x_conditional_expr
@@ -1594,15 +1595,7 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
 	 new_yes_expr, new_no_expr, 1);      
       break;
     case REDUCE_MIN:
-      if (INTEGRAL_TYPE_P (new_var_type))
-	new_var_init = build_x_modify_expr
-	  (*new_var, NOP_EXPR, TYPE_MAX_VALUE (new_var_type), 1);
-      else /* Otherwise, we the first item in the array */
-	new_var_init = build_x_modify_expr
-	  (*new_var, NOP_EXPR,
-	   build_array_ref (UNKNOWN_LOCATION,
-			    TREE_OPERAND (array_operand[0], 0),
-			    build_int_cst (integer_type_node, 0)), 1);
+      new_var_init = build_x_modify_expr (*new_var, NOP_EXPR, func_parm, 1);
       new_no_expr  = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
       new_yes_expr = build_x_modify_expr (*new_var, NOP_EXPR, func_parm, 1);
       new_expr = build_x_conditional_expr
@@ -1610,46 +1603,87 @@  fix_builtin_array_notation_fn (tree an_builtin_fn, tree *new_var)
 	 new_yes_expr, new_no_expr, 1);
       break;
     case REDUCE_MAX_INDEX:
-      new_var_init = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (new_var_type, 0), 1);
-      new_no_expr  = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
+      new_var_init = build_x_modify_expr (*new_var, NOP_EXPR, array_var[0], 1);
+      new_exp_init = build_x_modify_expr (array_ind_value, NOP_EXPR, func_parm,
+					  1);
+      new_no_ind   = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
+      new_no_expr  = build_x_modify_expr (array_ind_value, NOP_EXPR,
+					  array_ind_value, 1);
       if (list_size > 1) /* this means there is more than 1 */
-	new_yes_expr = build_x_modify_expr (*new_var, NOP_EXPR, array_var[0],
-					    1);
-      else				    
-	new_yes_expr = build_x_modify_expr
-	  (*new_var, NOP_EXPR, TREE_OPERAND (array_operand[0], 1), 1);
+	{
+	  new_yes_ind  = build_x_modify_expr (*new_var, NOP_EXPR, array_var[0],
+					      1);
+	  new_yes_expr = build_x_modify_expr (array_ind_value, NOP_EXPR,
+					      func_parm, 1);
+	}
+      else
+	{
+	  new_yes_ind  = build_x_modify_expr
+	    (*new_var, NOP_EXPR, TREE_OPERAND (array_operand[0], 1), 1);
+	  new_yes_expr = build_x_modify_expr (array_ind_value, NOP_EXPR,
+					      func_parm, 1);
+	}
+      new_yes_list = alloc_stmt_list ();
+      append_to_statement_list (new_yes_ind, &new_yes_list);
+      append_to_statement_list (new_yes_expr, &new_yes_list);
+
+      new_no_list = alloc_stmt_list ();
+      append_to_statement_list (new_no_ind, &new_no_list);
+      append_to_statement_list (new_no_expr, &new_no_list);
       
       new_expr = build_x_conditional_expr
-	(build2 (LT_EXPR, TREE_TYPE (*new_var), *new_var, func_parm),
-	 new_yes_expr, new_no_expr, 1);
+	(build2 (LT_EXPR, TREE_TYPE (array_ind_value), array_ind_value,
+		 func_parm),
+	 new_yes_list, new_no_list, 1);
       break;
     case REDUCE_MIN_INDEX:
-      new_var_init = build_x_modify_expr
-	(*new_var, NOP_EXPR, build_int_cst (new_var_type, 0), 1);
-      new_no_expr  = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
-      if (list_size > 1)
-	new_yes_expr = build_x_modify_expr (*new_var, NOP_EXPR, array_var[0],
-					    1);
+      new_var_init = build_x_modify_expr (*new_var, NOP_EXPR, array_var[0], 1);
+      new_exp_init = build_x_modify_expr (array_ind_value, NOP_EXPR, func_parm,
+					  1);
+      new_no_ind   = build_x_modify_expr (*new_var, NOP_EXPR, *new_var, 1);
+      new_no_expr  = build_x_modify_expr (array_ind_value, NOP_EXPR,
+					  array_ind_value, 1);
+      if (list_size > 1) /* this means there is more than 1 */
+	{
+	  new_yes_ind  = build_x_modify_expr (*new_var, NOP_EXPR, array_var[0],
+					      1);
+	  new_yes_expr = build_x_modify_expr (array_ind_value, NOP_EXPR,
+					      func_parm, 1);
+	}
       else
-	new_yes_expr = build_x_modify_expr
-	  (*new_var, NOP_EXPR, TREE_OPERAND (array_operand[0], 1), 1);
+	{
+	  new_yes_ind  = build_x_modify_expr
+	    (*new_var, NOP_EXPR, TREE_OPERAND (array_operand[0], 1), 1);
+	  new_yes_expr = build_x_modify_expr (array_ind_value, NOP_EXPR,
+					      func_parm, 1);
+	}
+      new_yes_list = alloc_stmt_list ();
+      append_to_statement_list (new_yes_ind, &new_yes_list);
+      append_to_statement_list (new_yes_expr, &new_yes_list);
+
+      new_no_list = alloc_stmt_list ();
+      append_to_statement_list (new_no_ind, &new_no_list);
+      append_to_statement_list (new_no_expr, &new_no_list);
       
       new_expr = build_x_conditional_expr
-	(build2 (GT_EXPR, TREE_TYPE (*new_var), *new_var, func_parm),
-	 new_yes_expr, new_no_expr, 1);
+	(build2 (GT_EXPR, TREE_TYPE (array_ind_value), array_ind_value,
+		 func_parm),
+	 new_yes_list, new_no_list, 1);
       break;
     default:
       gcc_unreachable ();
       break;
     }
 
+  for (ii = 0; ii < rank; ii++)
+    append_to_statement_list (ind_init[ii], &loop);
+    
   append_to_statement_list (new_var_init, &loop);
+  if (an_type == REDUCE_MAX_INDEX || an_type == REDUCE_MIN_INDEX)
+    append_to_statement_list (new_exp_init, &loop);
   
   for (ii = 0; ii < rank; ii++)
     {
-      append_to_statement_list (ind_init [ii], &loop);
-
       append_to_statement_list
 	(build1 (LABEL_EXPR, void_type_node, if_stmt_label[ii]), &loop);
       append_to_statement_list
@@ -2078,7 +2112,7 @@  contains_array_notation_expr (tree expr)
   int list_size = 0;
   
   extract_array_notation_exprs (expr, false, &array_list, &list_size);
-  if (*array_list == NULL || list_size == 0)
+  if (array_list == NULL || list_size == 0)
     return false;
   else
     return true;
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 741d5b4..a161742 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -62,6 +62,7 @@  static tree lookup_destructor (tree, tree, tree);
 static void warn_args_num (location_t, tree, bool);
 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
                               tsubst_flags_t);
+extern bool contains_array_notation_expr (tree);
 
 /* Do `exp = require_complete_type (exp);' to make sure exp
    does not have an incomplete type.  (That includes void types.)
@@ -7393,6 +7394,9 @@  convert_for_assignment (tree type, tree rhs,
 {
   tree rhstype;
   enum tree_code coder;
+  
+  if (flag_enable_cilk && contains_array_notation_expr (rhs))
+    return rhs;
 
   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
diff --git a/gcc/testsuite/ChangeLog.cilk b/gcc/testsuite/ChangeLog.cilk
index 7f67680..7753823 100644
--- a/gcc/testsuite/ChangeLog.cilk
+++ b/gcc/testsuite/ChangeLog.cilk
@@ -1,3 +1,9 @@ 
+2012-01-26  Balaji V. Iyer  <balaji.v.iyer@intel.com>
+
+	* gcc.dg/cilk-plus/array_notation_tests/builtin_func_double.c(main):
+	Fixed a variable mistake.
+	* g++.dg/cilk-plus/array_notation_tests/builtin_func_double.cc: New.
+
 2012-01-25  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
 	* gcc.dg/cilk-plus/array_notation_tests/sec_implicit_ex.c: New.
diff --git a/gcc/testsuite/g++.dg/cilk-plus/array_notation_tests/builtin_func_double.cc b/gcc/testsuite/g++.dg/cilk-plus/array_notation_tests/builtin_func_double.cc
new file mode 100644
index 0000000..682c008
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cilk-plus/array_notation_tests/builtin_func_double.cc
@@ -0,0 +1,72 @@ 
+#if HAVE_IO
+#include <iostream>
+using namespace std;
+#endif
+#include <stdlib.h>
+
+/* char __sec_reduce_add (int *); */
+int main(int argc, char **argv)
+{
+  int ii,array[10], y = 0, y_int = 0, array2[10];
+  double x, yy, array3[10], array4[10];
+  for (ii = 0; ii < 10; ii++)
+    {
+      array[ii] = 1+ii;
+      array2[ii]= 2; 
+    }
+
+  for (ii = 0; ii < 10; ii++)
+    {
+      if (ii%2 && ii)
+	array3[ii] = (double)(1.0000/(double)ii);
+      else
+	array3[ii] = (double) ii + 0.10;
+      array4[ii] = (double) (1.00000/ (double)(ii+1));
+    }
+
+  /* array[:] = 5; */
+  x = __sec_reduce_max (array3[:] * array4[:]); 
+  y = __sec_reduce_max_ind ( array3[:] * array4[:]); 
+#if HAVE_IO
+  for (ii = 0; ii < 10; ii++) 
+    cout << array3[ii] * array4[ii] << " ";
+  cout << endl;
+  cout << "Max = " << x << " Max Index = " << y << endl;
+#endif
+
+  x = __sec_reduce_min (array3[:] * array4[:]); 
+  y = __sec_reduce_min_ind ( array3[:] * array4[:]); 
+#if HAVE_IO
+  for (ii = 0; ii < 10; ii++) 
+    cout << array3[ii] * array4[ii] << " ";
+  cout << endl;
+  cout << "Min = " << x << " Min Index = " << y << endl;
+#endif
+
+  x = __sec_reduce_add (array3[:] * array4[:]); 
+  yy = __sec_reduce_mul ( array3[:] * array4[:]); 
+#if HAVE_IO
+  for (ii = 0; ii < 10; ii++) 
+    cout << array3[ii] * array4[ii] << " ";
+  cout << endl;
+  cout << "Add = " << x << " Mul = " << yy << endl;
+#endif
+
+  for (ii = 0; ii < 10; ii++)
+    {
+      if (ii%2 && ii)
+	array3[ii] = (double)(1.0000/(double)ii);
+      else
+	array3[ii] = (double) ii + 0.00;
+      array4[ii] = (double) (1.00000/ (double)(ii+1));
+    }
+  y_int = __sec_reduce_any_zeros (array3[:] * array4[:]); 
+  y = __sec_reduce_all_zeros ( array3[:] * array4[:]); 
+#if HAVE_IO
+  for (ii = 0; ii < 10; ii++) 
+    cout << array3[ii] * array4[ii] << " ";
+  cout << endl;
+  cout << "Any Zeros = " << y_int << " All Zeros = " << y << endl;
+#endif
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/cilk-plus/array_notation_tests/builtin_func_double.c b/gcc/testsuite/gcc.dg/cilk-plus/array_notation_tests/builtin_func_double.c
index e52bbcc..ff38a71 100644
--- a/gcc/testsuite/gcc.dg/cilk-plus/array_notation_tests/builtin_func_double.c
+++ b/gcc/testsuite/gcc.dg/cilk-plus/array_notation_tests/builtin_func_double.c
@@ -50,7 +50,7 @@  int main(int argc, char **argv)
   for (ii = 0; ii < 10; ii++) 
     printf("%5.3f ", array3[ii] * array4[ii]);
   printf("\n");
-  printf("Add = %5.3f\t Mul = %f\n", x, y);
+  printf("Add = %5.3f\t Mul = %f\n", x, yy);
 #endif
 
   for (ii = 0; ii < 10; ii++)