IA MCU psABI support: changes to libraries
[gcc.git] / gcc / graphite-isl-ast-to-gimple.c
index 6f58c7c600fc9cd2a0c45312a55e7a955429f658..b6ef13d5b373a51c0280f4d5fbaee49d440b4740 100644 (file)
@@ -1,5 +1,5 @@
 /* Translation of ISL AST to Gimple.
-   Copyright (C) 2014 Free Software Foundation, Inc.
+   Copyright (C) 2014-2015 Free Software Foundation, Inc.
    Contributed by Roman Gareev <gareevroman@gmail.com>.
 
 This file is part of GCC.
@@ -20,28 +20,39 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "config.h"
 
-#ifdef HAVE_cloog
+#ifdef HAVE_isl
 #include <isl/set.h>
 #include <isl/map.h>
 #include <isl/union_map.h>
 #include <isl/ast_build.h>
-#if defined(__cplusplus)
+
+/* Since ISL-0.13, the extern is in val_gmp.h.  */
+#if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
 extern "C" {
 #endif
 #include <isl/val_gmp.h>
-#if defined(__cplusplus)
+#if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
 }
 #endif
 #endif
 
 #include "system.h"
 #include "coretypes.h"
+#include "alias.h"
+#include "symtab.h"
+#include "options.h"
 #include "tree.h"
+#include "fold-const.h"
+#include "predict.h"
+#include "tm.h"
+#include "hard-reg-set.h"
+#include "function.h"
+#include "dominance.h"
+#include "cfg.h"
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
 #include "gimple-iterator.h"
 #include "tree-ssa-loop.h"
@@ -55,7 +66,7 @@ extern "C" {
 #include "tree-into-ssa.h"
 #include <map>
 
-#ifdef HAVE_cloog
+#ifdef HAVE_isl
 #include "graphite-poly.h"
 #include "graphite-isl-ast-to-gimple.h"
 
@@ -73,6 +84,14 @@ static int max_mode_int_precision =
 static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
                                                128 : max_mode_int_precision;
 
+struct ast_build_info
+{
+  ast_build_info()
+    : is_parallelizable(false)
+  { };
+  bool is_parallelizable;
+};
+
 /* Converts a GMP constant VAL to a tree and returns it.  */
 
 static tree
@@ -122,10 +141,16 @@ gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *,
                                    ivs_params &ip);
 
 /* Return the tree variable that corresponds to the given isl ast identifier
- expression (an isl_ast_expr of type isl_ast_expr_id).  */
+   expression (an isl_ast_expr of type isl_ast_expr_id).
+
+   FIXME: We should replace blind conversation of id's type with derivation
+   of the optimal type when we get the corresponding isl support. Blindly
+   converting type sizes may be problematic when we switch to smaller
+   types.  */
 
 static tree
-gcc_expression_from_isl_ast_expr_id (__isl_keep isl_ast_expr *expr_id,
+gcc_expression_from_isl_ast_expr_id (tree type,
+                                    __isl_keep isl_ast_expr *expr_id,
                                     ivs_params &ip)
 {
   gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
@@ -136,7 +161,7 @@ gcc_expression_from_isl_ast_expr_id (__isl_keep isl_ast_expr *expr_id,
   gcc_assert (res != ip.end () &&
               "Could not map isl_id to tree expression");
   isl_ast_expr_free (expr_id);
-  return res->second;
+  return fold_convert (type, res->second);
 }
 
 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
@@ -186,6 +211,12 @@ binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
     case isl_ast_op_div:
       return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
+    case isl_ast_op_pdiv_q:
+      return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_pdiv_r:
+      return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
     case isl_ast_op_fdiv_q:
       return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
@@ -299,8 +330,6 @@ gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
     case isl_ast_op_call:
     case isl_ast_op_and_then:
     case isl_ast_op_or_else:
-    case isl_ast_op_pdiv_q:
-    case isl_ast_op_pdiv_r:
     case isl_ast_op_select:
       gcc_unreachable ();
 
@@ -312,6 +341,8 @@ gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
     case isl_ast_op_sub:
     case isl_ast_op_mul:
     case isl_ast_op_div:
+    case isl_ast_op_pdiv_q:
+    case isl_ast_op_pdiv_r:
     case isl_ast_op_fdiv_q:
     case isl_ast_op_and:
     case isl_ast_op_or:
@@ -345,7 +376,7 @@ gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
   switch (isl_ast_expr_get_type (expr))
     {
     case isl_ast_expr_id:
-      return gcc_expression_from_isl_ast_expr_id (expr, ip);
+      return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
 
     case isl_ast_expr_int:
       return gcc_expression_from_isl_expr_int (type, expr);
@@ -383,6 +414,10 @@ graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
 
   isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
   isl_id *id = isl_ast_expr_get_id (for_iterator);
+  std::map<isl_id *, tree>::iterator res;
+  res = ip.find (id);
+  if (ip.count (id))
+    isl_id_free (res->first);
   ip[id] = iv;
   isl_ast_expr_free (for_iterator);
   return loop;
@@ -419,7 +454,15 @@ translate_isl_ast_for_loop (loop_p context_loop,
   redirect_edge_succ_nodup (next_e, after);
   set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
 
-  /* TODO: Add checking for the loop parallelism.  */
+  if (flag_loop_parallelize_all)
+  {
+    isl_id *id = isl_ast_node_get_annotation (node_for);
+    gcc_assert (id);
+    ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
+    loop->can_be_parallel = for_info->is_parallelizable;
+    free (for_info);
+    isl_id_free (id);
+  }
 
   return last_e;
 }
@@ -600,9 +643,9 @@ translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
   gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
              "The entry block should not even appear within a scop");
 
-  loop_p loop = gbb_loop (gbb);
-  iv_map.create (loop->num + 1);
-  iv_map.safe_grow_cleared (loop->num + 1);
+  int nb_loops = number_of_loops (cfun);
+  iv_map.create (nb_loops);
+  iv_map.safe_grow_cleared (nb_loops);
 
   build_iv_mapping (iv_map, gbb, user_expr, ip, SCOP_REGION (pbb->scop));
   isl_ast_expr_free (user_expr);
@@ -635,6 +678,43 @@ translate_isl_ast_node_block (loop_p context_loop,
   isl_ast_node_list_free (node_list);
   return next_e;
 }
+/* Creates a new if region corresponding to ISL's cond.  */
+
+static edge
+graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
+                          ivs_params &ip)
+{
+  tree type =
+    build_nonstandard_integer_type (graphite_expression_type_precision, 0);
+  tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
+  edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
+  return exit_edge;
+}
+
+/* Translates an isl_ast_node_if to Gimple.  */
+
+static edge
+translate_isl_ast_node_if (loop_p context_loop,
+                          __isl_keep isl_ast_node *node,
+                          edge next_e, ivs_params &ip)
+{
+  gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
+  isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
+  edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
+
+  edge true_e = get_true_edge_from_guard_bb (next_e->dest);
+  isl_ast_node *then_node = isl_ast_node_if_get_then (node);
+  translate_isl_ast (context_loop, then_node, true_e, ip);
+  isl_ast_node_free (then_node);
+
+  edge false_e = get_false_edge_from_guard_bb (next_e->dest);
+  isl_ast_node *else_node = isl_ast_node_if_get_else (node);
+  if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
+    translate_isl_ast (context_loop, else_node, false_e, ip);
+  isl_ast_node_free (else_node);
+  return last_e;
+}
 
 /* Translates an ISL AST node NODE to GCC representation in the
    context of a SESE.  */
@@ -653,7 +733,8 @@ translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
                                         next_e, ip);
 
     case isl_ast_node_if:
-      return next_e;
+      return translate_isl_ast_node_if (context_loop, node,
+                                       next_e, ip);
 
     case isl_ast_node_user:
       return translate_isl_ast_node_user (node, next_e, ip);
@@ -750,6 +831,92 @@ extend_schedule (__isl_take isl_map *schedule, int nb_schedule_dims)
   return schedule;
 }
 
+/* Set the separation_class option for unroll and jam. */
+
+static __isl_give isl_union_map *
+generate_luj_sepclass_opt (scop_p scop, __isl_take isl_union_set *domain, 
+                       int dim, int cl)
+{
+  isl_map  *map;
+  isl_space *space, *space_sep;
+  isl_ctx *ctx;
+  isl_union_map *mapu;
+  int nsched = get_max_schedule_dimensions (scop);
+  ctx = scop->ctx;
+  space_sep = isl_space_alloc (ctx, 0, 1, 1);
+  space_sep = isl_space_wrap (space_sep);
+  space_sep = isl_space_set_tuple_name (space_sep, isl_dim_set,
+                                       "separation_class");
+  space = isl_set_get_space (scop->context);
+  space_sep = isl_space_align_params (space_sep, isl_space_copy(space));
+  space = isl_space_map_from_domain_and_range (space, space_sep);
+  space = isl_space_add_dims (space,isl_dim_in, nsched);
+  map = isl_map_universe (space);
+  isl_map_fix_si (map,isl_dim_out,0,dim);
+  isl_map_fix_si (map,isl_dim_out,1,cl);
+
+  mapu = isl_union_map_intersect_domain (isl_union_map_from_map (map), 
+                                        domain);
+  return (mapu);
+}
+
+/* Compute the separation class for loop unroll and jam.  */
+
+static __isl_give isl_union_set *
+generate_luj_sepclass (scop_p scop)
+{
+  int i;
+  poly_bb_p pbb;
+  isl_union_set *domain_isl;
+
+  domain_isl = isl_union_set_empty (isl_set_get_space (scop->context));
+
+  FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
+    {
+      isl_set *bb_domain;
+      isl_set *bb_domain_s;
+
+      if (pbb->map_sepclass == NULL)
+       continue;
+
+      if (isl_set_is_empty (pbb->domain))
+       continue;
+
+      bb_domain = isl_set_copy (pbb->domain);
+      bb_domain_s = isl_set_apply (bb_domain, pbb->map_sepclass);
+      pbb->map_sepclass = NULL;
+
+      domain_isl =
+       isl_union_set_union (domain_isl, isl_union_set_from_set (bb_domain_s));
+    }
+
+  return domain_isl;
+}
+
+/* Set the AST built options for loop unroll and jam. */
+static __isl_give isl_union_map *
+generate_luj_options (scop_p scop)
+{
+  isl_union_set *domain_isl;
+  isl_union_map *options_isl_ss;
+  isl_union_map *options_isl =
+    isl_union_map_empty (isl_set_get_space (scop->context));
+  int dim = get_max_schedule_dimensions (scop) - 1;
+  int dim1 = dim - PARAM_VALUE (PARAM_LOOP_UNROLL_JAM_DEPTH);
+
+  if (!flag_loop_unroll_jam)
+    return options_isl;
+
+  domain_isl = generate_luj_sepclass (scop);
+
+  options_isl_ss = generate_luj_sepclass_opt (scop, domain_isl, dim1, 0);
+  options_isl = isl_union_map_union (options_isl, options_isl_ss);
+
+  return options_isl;
+}
+
 /* Generates a schedule, which specifies an order used to
    visit elements in a domain.  */
 
@@ -780,6 +947,47 @@ generate_isl_schedule (scop_p scop)
   return schedule_isl;
 }
 
+/* This method is executed before the construction of a for node.  */
+static __isl_give isl_id *
+ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
+{
+  isl_union_map *dependences = (isl_union_map *) user;
+  ast_build_info *for_info = XNEW (struct ast_build_info);
+  isl_union_map *schedule = isl_ast_build_get_schedule (build);
+  isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
+  int dimension = isl_space_dim (schedule_space, isl_dim_out);
+  for_info->is_parallelizable =
+    !carries_deps (schedule, dependences, dimension);
+  isl_union_map_free (schedule);
+  isl_space_free (schedule_space);
+  isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
+  return id;
+}
+
+/* Set the separate option for all dimensions.
+   This helps to reduce control overhead.
+   Set the options for unroll and jam.  */
+
+static __isl_give isl_ast_build *
+set_options (__isl_take isl_ast_build *control,
+            __isl_keep isl_union_map *schedule,
+            __isl_take isl_union_map *opt_luj)
+{
+  isl_ctx *ctx = isl_union_map_get_ctx (schedule);
+  isl_space *range_space = isl_space_set_alloc (ctx, 0, 1);
+  range_space =
+    isl_space_set_tuple_name (range_space, isl_dim_set, "separate");
+  isl_union_set *range =
+    isl_union_set_from_set (isl_set_universe (range_space));  
+  isl_union_set *domain = isl_union_map_range (isl_union_map_copy (schedule));
+  domain = isl_union_set_universe (domain);
+  isl_union_map *options = isl_union_map_from_domain_and_range (domain, range);
+
+  options = isl_union_map_union (options, opt_luj);
+
+  return isl_ast_build_set_options (control, options);
+}
+
 static __isl_give isl_ast_node *
 scop_to_isl_ast (scop_p scop, ivs_params &ip)
 {
@@ -790,10 +998,26 @@ scop_to_isl_ast (scop_p scop, ivs_params &ip)
   isl_options_set_ast_build_atomic_upper_bound (scop->ctx, true);
 
   add_parameters_to_ivs_params (scop, ip);
+
+  isl_union_map *options_luj = generate_luj_options (scop);
+
   isl_union_map *schedule_isl = generate_isl_schedule (scop);
   isl_ast_build *context_isl = generate_isl_context (scop);
+
+  context_isl = set_options (context_isl, schedule_isl, options_luj);
+
+  isl_union_map *dependences = NULL;
+  if (flag_loop_parallelize_all)
+  {
+    dependences = scop_get_dependences (scop);
+    context_isl =
+      isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
+                                        dependences);
+  }
   isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
                                                           schedule_isl);
+  if(dependences)
+    isl_union_map_free (dependences);
   isl_ast_build_free (context_isl);
   return ast_isl;
 }
@@ -839,6 +1063,10 @@ graphite_regenerate_ast_isl (scop_p scop)
 
   translate_isl_ast (context_loop, root_node, if_region->true_region->entry,
                     ip);
+
+  mark_virtual_operands_for_renaming (cfun);
+  update_ssa (TODO_update_ssa);
+
   graphite_verify ();
   scev_reset ();
   recompute_all_dominators ();
@@ -854,7 +1082,20 @@ graphite_regenerate_ast_isl (scop_p scop)
   ivs_params_clear (ip);
   isl_ast_node_free (root_node);
   timevar_pop (TV_GRAPHITE_CODE_GEN);
-  /* TODO: Add dump  */
+
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    {
+      loop_p loop;
+      int num_no_dependency = 0;
+
+      FOR_EACH_LOOP (loop, 0)
+       if (loop->can_be_parallel)
+         num_no_dependency++;
+
+      fprintf (dump_file, "\n%d loops carried no dependency.\n",
+              num_no_dependency);
+    }
+
   return !graphite_regenerate_error;
 }
 #endif