OSDN Git Service

Use ssizetype when long_long_integer_type_node is NULL.
[pf3gnuchains/gcc-fork.git] / gcc / graphite-clast-to-gimple.c
index 4f45708..f99a158 100644 (file)
@@ -52,6 +52,10 @@ along with GCC; see the file COPYING3.  If not see
 #include "graphite-clast-to-gimple.h"
 #include "graphite-dependences.h"
 
+/* This flag is set when an error occurred during the translation of
+   CLAST to Gimple.  */
+static bool gloog_error;
+
 /* Verifies properties that GRAPHITE should maintain during translation.  */
 
 static inline void
@@ -117,7 +121,12 @@ save_clast_name_index (htab_t index_table, const char *name, int index)
   slot = htab_find_slot (index_table, &tmp, INSERT);
 
   if (slot)
-    *slot = new_clast_name_index (name, index);
+    {
+      if (*slot)
+       free (*slot);
+
+      *slot = new_clast_name_index (name, index);
+    }
 }
 
 /* Print to stderr the element ELT.  */
@@ -273,14 +282,24 @@ clast_to_gcc_expression (tree type, struct clast_expr *e,
              {
                tree name = clast_name_to_gcc (t->var, region, newivs,
                                               newivs_index, params_index);
-               return fold_convert (type, name);
+
+               if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
+                 name = fold_convert (sizetype, name);
+
+               name = fold_convert (type, name);
+               return name;
              }
 
            else if (value_mone_p (t->val))
              {
                tree name = clast_name_to_gcc (t->var, region, newivs,
                                               newivs_index, params_index);
+
+               if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
+                 name = fold_convert (sizetype, name);
+
                name = fold_convert (type, name);
+
                return fold_build1 (NEGATE_EXPR, type, name);
              }
            else
@@ -288,8 +307,17 @@ clast_to_gcc_expression (tree type, struct clast_expr *e,
                tree name = clast_name_to_gcc (t->var, region, newivs,
                                               newivs_index, params_index);
                tree cst = gmp_cst_to_tree (type, t->val);
+
+               if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
+                 name = fold_convert (sizetype, name);
+
                name = fold_convert (type, name);
-               return fold_build2 (MULT_EXPR, type, cst, name);
+
+               if (!POINTER_TYPE_P (type))
+                 return fold_build2 (MULT_EXPR, type, cst, name);
+
+               gloog_error = true;
+               return cst;
              }
          }
        else
@@ -519,9 +547,19 @@ 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.  */
+/* Java does not initialize long_long_integer_type_node.  */
+#define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
+
+/* 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, we fail.
+
+   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)
@@ -533,11 +571,44 @@ gcc_type_for_cloog_iv (const char *cloog_iv, gimple_bb_p gbb)
   slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, NO_INSERT);
 
   if (slot && *slot)
-    return ((ivtype_map_elt) *slot)->type;
+    {
+      tree type = ((ivtype_map_elt) *slot)->type;
+      int type_precision = TYPE_PRECISION (type);
+
+      /* Find the smallest signed type possible.  */
+      if (!TYPE_UNSIGNED (type))
+       {
+         if (type_precision <= TYPE_PRECISION (integer_type_node))
+           return integer_type_node;
+
+         if (type_precision <= TYPE_PRECISION (long_integer_type_node))
+           return long_integer_type_node;
+
+         if (type_precision <= TYPE_PRECISION (my_long_long))
+           return my_long_long;
+
+         gcc_unreachable ();
+       }
+
+      if (type_precision < TYPE_PRECISION (integer_type_node))
+       return integer_type_node;
 
-  return integer_type_node;
+      if (type_precision < TYPE_PRECISION (long_integer_type_node))
+       return long_integer_type_node;
+
+      if (type_precision < TYPE_PRECISION (my_long_long))
+       return my_long_long;
+
+      /* There is no signed type available, that is large enough to hold the
+        original value.  */
+      gcc_unreachable ();
+    }
+
+  return my_long_long;
 }
 
+#undef my_long_long
+
 /* Returns the induction variable for the loop that gets translated to
    STMT.  */
 
@@ -629,7 +700,7 @@ copy_renames (void **slot, void *s)
   tmp.old_name = old_name;
   x = htab_find_slot (res, &tmp, INSERT);
 
-  if (!*x)
+  if (x && !*x)
     *x = new_rename_map_elt (old_name, expr);
 
   return 1;
@@ -660,7 +731,7 @@ mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
   tmp.bb = bb;
   x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
 
-  if (!*x)
+  if (x && !*x)
     *x = new_bb_pbb_def (bb, pbb);
 }
 
@@ -719,13 +790,14 @@ dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
 }
 
 static edge
-translate_clast (sese, struct clast_stmt *, edge, htab_t, VEC (tree, heap) **,
-                htab_t, htab_t, htab_t);
+translate_clast (sese, loop_p, struct clast_stmt *, edge, htab_t,
+                VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
 
 /* Translates a clast user statement STMT to gimple.
 
    - REGION is the sese region we used to generate the scop.
    - NEXT_E is the edge where new generated code should be attached.
+   - CONTEXT_LOOP is the loop in which the generated code will be placed
    - RENAME_MAP contains a set of tuples of new names associated to
      the original variables names.
    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
@@ -737,8 +809,10 @@ translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
                      htab_t newivs_index, htab_t bb_pbb_mapping,
                      htab_t params_index)
 {
+  gimple_bb_p gbb;
+  basic_block new_bb;
   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
-  gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
+  gbb = PBB_BLACK_BOX (pbb);
 
   if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
     return next_e;
@@ -747,29 +821,51 @@ translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
                    params_index);
   next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
                                           next_e, rename_map);
-  mark_bb_with_pbb (pbb, next_e->src, bb_pbb_mapping);
+  new_bb = next_e->src;
+  mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
   update_ssa (TODO_update_ssa);
 
   return next_e;
 }
 
-/* Mark a loop parallel, if the graphite dependency check cannot find any
- * dependencies.  This triggers parallel code generation in the autopar pass.
- * */
-static void
-try_mark_loop_parallel (sese region, loop_p loop, htab_t bb_pbb_mapping)
+/* Creates a new if region protecting the loop to be executed, if the execution
+   count is zero (lb > ub).  */
+static edge
+graphite_create_new_loop_guard (sese region, edge entry_edge,
+                               struct clast_for *stmt,
+                               VEC (tree, heap) *newivs,
+                               htab_t newivs_index, htab_t params_index)
 {
-  loop_p outermost_loop =  SESE_ENTRY (region)->src->loop_father;
-  int level = loop_depth (loop) - loop_depth (outermost_loop);
+  tree cond_expr;
+  edge exit_edge;
+  tree type = gcc_type_for_iv_of_clast_loop (stmt);
+  tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
+                                    newivs_index, params_index);
+  tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
+                                    newivs_index, params_index);
 
-  if (flag_loop_parallelize_all
-      && !dependency_in_loop_p (loop, bb_pbb_mapping,
-       get_scattering_level (level)))
-    loop->can_be_parallel = true;
+  /* XXX: 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.
+     There might be a problem with cases where ub is 2^32.  */
+  tree one;
+  Value gmp_one;
+  value_init (gmp_one);
+  value_set_si (gmp_one, 1);
+  one = gmp_cst_to_tree (type, gmp_one);
+  value_clear (gmp_one);
+
+  ub = fold_build2 (PLUS_EXPR, type, ub, one);
+  cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub);
+
+  exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
+
+  return exit_edge;
 }
 
 
-/* Translates a clast for statement STMT to gimple.
+/* Create the loop for a clast for statement.
 
    - REGION is the sese region we used to generate the scop.
    - NEXT_E is the edge where new generated code should be attached.
@@ -779,25 +875,79 @@ try_mark_loop_parallel (sese region, loop_p loop, htab_t bb_pbb_mapping)
    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
      the sese region.  */
 static edge
-translate_clast_for (sese region, struct clast_for *stmt, edge next_e,
-                    htab_t rename_map, VEC (tree, heap) **newivs,
-                    htab_t newivs_index, htab_t bb_pbb_mapping,
-                    htab_t params_index)
+translate_clast_for_loop (sese region, loop_p context_loop,
+                         struct clast_for *stmt, edge next_e,
+                         htab_t rename_map, VEC (tree, heap) **newivs,
+                         htab_t newivs_index, htab_t bb_pbb_mapping,
+                         int level, htab_t params_index)
 {
-  loop_p context_loop = next_e->dest->loop_father;
-  loop_p loop = graphite_create_new_loop (region, next_e, stmt, context_loop,
-                                         newivs, newivs_index, params_index);
+  struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
+                                               context_loop, newivs,
+                                               newivs_index, params_index);
   edge last_e = single_exit (loop);
-  edge body = single_succ_edge (loop->header);
-
-  next_e = translate_clast (region, stmt->body, body, rename_map, newivs,
-                           newivs_index, bb_pbb_mapping, params_index);
+  edge to_body = single_succ_edge (loop->header);
+  basic_block after = to_body->dest;
 
   /* Create a basic block for loop close phi nodes.  */
   last_e = single_succ_edge (split_edge (last_e));
+
+  /* Translate the body of the loop.  */
+  next_e = translate_clast (region, loop, stmt->body, to_body, rename_map,
+                           newivs, newivs_index, bb_pbb_mapping, level + 1,
+                           params_index);
+  redirect_edge_succ_nodup (next_e, after);
+  set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
+
+   /* Remove from rename_map all the tuples containing variables
+      defined in loop's body.  */
   insert_loop_close_phis (rename_map, loop);
 
-  try_mark_loop_parallel (region, loop, bb_pbb_mapping);
+  if (flag_loop_parallelize_all
+      && !dependency_in_loop_p (loop, bb_pbb_mapping,
+                               get_scattering_level (level)))
+    loop->can_be_parallel = true;
+
+  return last_e;
+}
+
+/* Translates a clast for statement STMT to gimple.  First a guard is created
+   protecting the loop, if it is executed zero times.  In this guard we create
+   the real loop structure.
+
+   - REGION is the sese region we used to generate the scop.
+   - NEXT_E is the edge where new generated code should be attached.
+   - RENAME_MAP contains a set of tuples of new names associated to
+     the original variables names.
+   - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
+   - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
+     the sese region.  */
+static edge
+translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
+                    edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
+                    htab_t newivs_index, htab_t bb_pbb_mapping, int level,
+                    htab_t params_index)
+{
+  edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
+                                          newivs_index, params_index);
+
+  edge true_e = get_true_edge_from_guard_bb (next_e->dest);
+  edge false_e = get_false_edge_from_guard_bb (next_e->dest);
+  edge exit_true_e = single_succ_edge (true_e->dest);
+  edge exit_false_e = single_succ_edge (false_e->dest);
+
+  htab_t before_guard = htab_create (10, rename_map_elt_info,
+                                    eq_rename_map_elts, free);
+  htab_traverse (rename_map, copy_renames, before_guard);
+
+  next_e = translate_clast_for_loop (region, context_loop, stmt, true_e,
+                                    rename_map, newivs,
+                                    newivs_index, bb_pbb_mapping, level,
+                                    params_index);
+
+  insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
+                    before_guard, rename_map);
+
+  htab_delete (before_guard);
 
   return last_e;
 }
@@ -806,15 +956,17 @@ translate_clast_for (sese region, struct clast_for *stmt, edge next_e,
 
    - REGION is the sese region we used to generate the scop.
    - NEXT_E is the edge where new generated code should be attached.
+   - CONTEXT_LOOP is the loop in which the generated code will be placed
    - RENAME_MAP contains a set of tuples of new names associated to
      the original variables names.
    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
      the sese region.  */
 static edge
-translate_clast_guard (sese region, struct clast_guard *stmt, edge next_e,
+translate_clast_guard (sese region, loop_p context_loop,
+                      struct clast_guard *stmt, edge next_e,
                       htab_t rename_map, VEC (tree, heap) **newivs,
-                      htab_t newivs_index, htab_t bb_pbb_mapping,
+                      htab_t newivs_index, htab_t bb_pbb_mapping, int level,
                       htab_t params_index)
 {
   edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
@@ -829,9 +981,9 @@ translate_clast_guard (sese region, struct clast_guard *stmt, edge next_e,
                                     eq_rename_map_elts, free);
   htab_traverse (rename_map, copy_renames, before_guard);
 
-  next_e = translate_clast (region, stmt->then, true_e,
+  next_e = translate_clast (region, context_loop, stmt->then, true_e,
                            rename_map, newivs, newivs_index, bb_pbb_mapping,
-                           params_index);
+                           level, params_index);
 
   insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
                     before_guard, rename_map);
@@ -845,13 +997,14 @@ translate_clast_guard (sese region, struct clast_guard *stmt, edge next_e,
    context of a SESE.
 
    - NEXT_E is the edge where new generated code should be attached.
+   - CONTEXT_LOOP is the loop in which the generated code will be placed
    - RENAME_MAP contains a set of tuples of new names associated to
      the original variables names.
    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
 static edge
-translate_clast (sese region, struct clast_stmt *stmt,
+translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
                 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
-                htab_t newivs_index, htab_t bb_pbb_mapping, 
+                htab_t newivs_index, htab_t bb_pbb_mapping, int level,
                 htab_t params_index)
 {
   if (!stmt)
@@ -866,28 +1019,31 @@ translate_clast (sese region, struct clast_stmt *stmt,
                                   bb_pbb_mapping, params_index);
 
   else if (CLAST_STMT_IS_A (stmt, stmt_for))
-    next_e = translate_clast_for (region,
-                                 (struct clast_for *) stmt, next_e, rename_map,
-                                 newivs, newivs_index, bb_pbb_mapping,
-                                 params_index);
+    next_e = translate_clast_for (region, context_loop,
+                                 (struct clast_for *) stmt, next_e,
+                                 rename_map, newivs, newivs_index,
+                                 bb_pbb_mapping, level, params_index);
 
   else if (CLAST_STMT_IS_A (stmt, stmt_guard))
-    next_e = translate_clast_guard (region, (struct clast_guard *) stmt, next_e,
+    next_e = translate_clast_guard (region, context_loop,
+                                   (struct clast_guard *) stmt, next_e,
                                    rename_map, newivs, newivs_index,
-                                   bb_pbb_mapping, params_index);
+                                   bb_pbb_mapping, level, params_index);
 
   else if (CLAST_STMT_IS_A (stmt, stmt_block))
-    next_e = translate_clast (region, ((struct clast_block *) stmt)->body,
+    next_e = translate_clast (region, context_loop,
+                             ((struct clast_block *) stmt)->body,
                              next_e, rename_map, newivs, newivs_index,
-                             bb_pbb_mapping, params_index);
+                             bb_pbb_mapping, level, params_index);
   else
     gcc_unreachable();
 
   recompute_all_dominators ();
   graphite_verify ();
 
-  return translate_clast (region, stmt->next, next_e, rename_map, newivs,
-                         newivs_index, bb_pbb_mapping, params_index);
+  return translate_clast (region, context_loop, stmt->next, next_e,
+                         rename_map, newivs, newivs_index,
+                         bb_pbb_mapping, level, params_index);
 }
 
 /* Returns the first cloog name used in EXPR.  */
@@ -896,34 +1052,30 @@ static const char *
 find_cloog_iv_in_expr (struct clast_expr *expr)
 {
   struct clast_term *term = (struct clast_term *) expr;
-
-  if (expr->type == expr_term
-      && !term->var)
-    return NULL;
+  struct clast_reduction *red;
+  int i;
 
   if (expr->type == expr_term)
     return term->var;
 
-  if (expr->type == expr_red)
-    {
-      int i;
-      struct clast_reduction *red = (struct clast_reduction *) expr;
+  if (expr->type != expr_red)
+    return NULL;
 
-      for (i = 0; i < red->n; i++)
-       {
-         const char *res = find_cloog_iv_in_expr ((red)->elts[i]);
+  red = (struct clast_reduction *) expr;
+  for (i = 0; i < red->n; i++)
+    {
+      const char *res = find_cloog_iv_in_expr (red->elts[i]);
 
-         if (res)
-           return res;
-       }
+      if (res)
+       return res;
     }
 
   return NULL;
 }
 
-/* Build for a clast_user_stmt USER_STMT a map between the CLAST
-   induction variables and the corresponding GCC old induction
-   variables.  This information is stored on each GRAPHITE_BB.  */
+/* Build for USER_STMT a map between the CLAST induction variables and
+   the corresponding GCC old induction variables.  This information is
+   stored on each GRAPHITE_BB.  */
 
 static void
 compute_cloog_iv_types_1 (poly_bb_p pbb, struct clast_user_stmt *user_stmt)
@@ -946,10 +1098,10 @@ compute_cloog_iv_types_1 (poly_bb_p pbb, struct clast_user_stmt *user_stmt)
 
       slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, INSERT);
 
-      if (!*slot)
+      if (slot && !*slot)
        {
          tree oldiv = pbb_to_depth_to_oldiv (pbb, index);
-         tree type = oldiv ? TREE_TYPE (oldiv) : integer_type_node;
+         tree type = TREE_TYPE (oldiv);
          *slot = new_ivtype_map_elt (tmp.cloog_iv, type);
        }
     }
@@ -1307,8 +1459,8 @@ debug_generated_program (scop_p scop)
   print_generated_program (stderr, scop);
 }
 
-/* Add CLooG names to parameter index.  The index is used to translate back from
* CLooG names to GCC trees.  */
+/* Add CLooG names to parameter index.  The index is used to translate
  back from CLooG names to GCC trees.  */
 
 static void
 create_params_index (htab_t index_table, CloogProgram *prog) {
@@ -1327,16 +1479,18 @@ create_params_index (htab_t index_table, CloogProgram *prog) {
 */
 
 bool
-gloog (scop_p scop, htab_t bb_pbb_mapping)
+gloog (scop_p scop, VEC (scop_p, heap) *scops, htab_t bb_pbb_mapping)
 {
-  edge new_scop_exit_edge = NULL;
   VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
+  loop_p context_loop;
   sese region = SCOP_REGION (scop);
   ifsese if_region = NULL;
   htab_t rename_map, newivs_index, params_index;
   cloog_prog_clast pc;
+  int i;
 
   timevar_push (TV_GRAPHITE_CODE_GEN);
+  gloog_error = false;
 
   pc = scop_to_clast (scop);
 
@@ -1358,6 +1512,7 @@ gloog (scop_p scop, htab_t bb_pbb_mapping)
   recompute_all_dominators ();
   graphite_verify ();
 
+  context_loop = SESE_ENTRY (region)->src->loop_father;
   compute_cloog_iv_types (pc.stmt);
   rename_map = htab_create (10, rename_map_elt_info, eq_rename_map_elts, free);
   newivs_index = htab_create (10, clast_name_index_elt_info,
@@ -1367,18 +1522,27 @@ gloog (scop_p scop, htab_t bb_pbb_mapping)
 
   create_params_index (params_index, pc.prog);
 
-  new_scop_exit_edge = translate_clast (region, pc.stmt,
-                                       if_region->true_region->entry,
-                                       rename_map, &newivs, newivs_index,
-                                       bb_pbb_mapping, params_index);
+  translate_clast (region, context_loop, pc.stmt,
+                  if_region->true_region->entry,
+                  rename_map, &newivs, newivs_index,
+                  bb_pbb_mapping, 1, params_index);
   graphite_verify ();
   sese_adjust_liveout_phis (region, rename_map,
                            if_region->region->exit->src,
                            if_region->false_region->exit,
                            if_region->true_region->exit);
+  scev_reset_htab ();
+  rename_nb_iterations (rename_map);
+
+  for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
+    rename_sese_parameters (rename_map, SCOP_REGION (scop));
+
   recompute_all_dominators ();
   graphite_verify ();
 
+  if (gloog_error)
+    set_ifsese_condition (if_region, integer_zero_node);
+
   free (if_region->true_region);
   free (if_region->region);
   free (if_region);
@@ -1405,7 +1569,7 @@ gloog (scop_p scop, htab_t bb_pbb_mapping)
               num_no_dependency);
     }
 
-  return true;
+  return !gloog_error;
 }
 
 #endif