]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/graphite-isl-ast-to-gimple.c
isl.m4: Remove support for ISL 0.14.
[thirdparty/gcc.git] / gcc / graphite-isl-ast-to-gimple.c
index 16cb5faacda77ba061a3046059966a49913f90d2..dbdae7a7c155dc133d66843e0a24abef8c5eeb8a 100644 (file)
@@ -1,5 +1,5 @@
-/* Translation of ISL AST to Gimple.
-   Copyright (C) 2014-2015 Free Software Foundation, Inc.
+/* Translation of isl AST to Gimple.
+   Copyright (C) 2014-2017 Free Software Foundation, Inc.
    Contributed by Roman Gareev <gareevroman@gmail.com>.
 
 This file is part of GCC.
@@ -24,6 +24,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #ifdef HAVE_isl
 
+#define INCLUDE_MAP
 #include "system.h"
 #include "coretypes.h"
 #include "backend.h"
@@ -53,27 +54,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "cfganal.h"
 #include "value-prof.h"
-
-#include <isl/constraint.h>
-#include <isl/set.h>
-#include <isl/union_set.h>
-#include <isl/map.h>
-#include <isl/union_map.h>
-#include <isl/ast_build.h>
-
-/* 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(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
-}
-#endif
-
 #include "graphite.h"
 
-#include <map>
-
 /* We always try to use signed 128 bit types, but fall back to smaller types
    in case a platform does not provide types of these sizes. In the future we
    should use isl to derive the optimal type for each subexpression.  */
@@ -116,14 +98,14 @@ graphite_verify (void)
   checking_verify_loop_closed_ssa (true);
 }
 
-/* IVS_PARAMS maps ISL's scattering and parameter identifiers
+/* IVS_PARAMS maps isl's scattering and parameter identifiers
    to corresponding trees.  */
 
 typedef std::map<isl_id *, tree> ivs_params;
 
-/* Free all memory allocated for ISL's identifiers.  */
+/* Free all memory allocated for isl's identifiers.  */
 
-void ivs_params_clear (ivs_params &ip)
+static void ivs_params_clear (ivs_params &ip)
 {
   std::map<isl_id *, tree>::iterator it;
   for (it = ip.begin ();
@@ -133,344 +115,159 @@ void ivs_params_clear (ivs_params &ip)
     }
 }
 
+/* Set the "separate" option for the schedule node.  */
+
+static isl_schedule_node *
+set_separate_option (__isl_take isl_schedule_node *node, void *user)
+{
+  if (user)
+    return node;
+
+  if (isl_schedule_node_get_type (node) != isl_schedule_node_band)
+    return node;
+
+  /* Set the "separate" option unless it is set earlier to another option.  */
+  if (isl_schedule_node_band_member_get_ast_loop_type (node, 0)
+      == isl_ast_loop_default)
+    return isl_schedule_node_band_member_set_ast_loop_type
+      (node, 0, isl_ast_loop_separate);
+
+  return node;
+}
+
+/* Print SCHEDULE under an AST form on file F.  */
+
+void
+print_schedule_ast (FILE *f, __isl_keep isl_schedule *schedule, scop_p scop)
+{
+  isl_set *set = isl_set_params (isl_set_copy (scop->param_context));
+  isl_ast_build *context = isl_ast_build_from_context (set);
+  isl_ast_node *ast
+    = isl_ast_build_node_from_schedule (context, isl_schedule_copy (schedule));
+  isl_ast_build_free (context);
+  print_isl_ast (f, ast);
+  isl_ast_node_free (ast);
+}
+
+DEBUG_FUNCTION void
+debug_schedule_ast (__isl_keep isl_schedule *s, scop_p scop)
+{
+  print_schedule_ast (stderr, s, scop);
+}
+
+enum phi_node_kind
+{
+  unknown_phi,
+  loop_phi,
+  close_phi,
+  cond_phi
+};
+
 class translate_isl_ast_to_gimple
 {
  public:
   translate_isl_ast_to_gimple (sese_info_p r)
-    : region (r), codegen_error (false)
-    { }
-
-  /* Translates an ISL AST node NODE to GCC representation in the
-     context of a SESE.  */
+    : region (r), codegen_error (false) { }
   edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
                          edge next_e, ivs_params &ip);
-
-  /* Translates an isl_ast_node_for to Gimple.  */
   edge translate_isl_ast_node_for (loop_p context_loop,
                                   __isl_keep isl_ast_node *node,
                                   edge next_e, ivs_params &ip);
-
-  /* Create the loop for a isl_ast_node_for.
-
-     - NEXT_E is the edge where new generated code should be attached.  */
   edge translate_isl_ast_for_loop (loop_p context_loop,
                                   __isl_keep isl_ast_node *node_for,
                                   edge next_e,
                                   tree type, tree lb, tree ub,
                                   ivs_params &ip);
-
-  /* Translates an isl_ast_node_if to Gimple.  */
   edge translate_isl_ast_node_if (loop_p context_loop,
                                  __isl_keep isl_ast_node *node,
                                  edge next_e, ivs_params &ip);
-
-  /* Translates an isl_ast_node_user to Gimple.
-
-     FIXME: We should remove iv_map.create (loop->num + 1), if it is
-     possible.  */
   edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
                                    edge next_e, ivs_params &ip);
-
-  /* Translates an isl_ast_node_block to Gimple.  */
   edge translate_isl_ast_node_block (loop_p context_loop,
                                     __isl_keep isl_ast_node *node,
                                     edge next_e, ivs_params &ip);
-
-  /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
-     type TYPE.  */
   tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
                         ivs_params &ip);
-
-  /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
-     type TYPE.  */
   tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
                          ivs_params &ip);
-
-  /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
-     type TYPE.  */
   tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
                           ivs_params &ip);
-
-  /* Converts an isl_ast_expr_op expression E with unknown number of arguments
-     to a GCC expression tree of type TYPE.  */
   tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
                        ivs_params &ip);
-
-  /* Converts an ISL AST expression E back to a GCC expression tree of
-     type TYPE.  */
   tree 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).
-
-     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.  */
   tree gcc_expression_from_isl_ast_expr_id (tree type,
                                            __isl_keep isl_ast_expr *expr_id,
                                            ivs_params &ip);
-
-  /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
-     type TYPE.  */
   tree gcc_expression_from_isl_expr_int (tree type,
                                         __isl_take isl_ast_expr *expr);
-
-  /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
-     type TYPE.  */
   tree gcc_expression_from_isl_expr_op (tree type,
                                        __isl_take isl_ast_expr *expr,
                                        ivs_params &ip);
-
-  /* Creates a new LOOP corresponding to isl_ast_node_for.  Inserts an
-     induction variable for the new LOOP.  New LOOP is attached to CFG
-     starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
-     becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
-     ISL's scattering name to the induction variable created for the
-     loop of STMT.  The new induction variable is inserted in the NEWIVS
-     vector and is of type TYPE.  */
   struct loop *graphite_create_new_loop (edge entry_edge,
                                         __isl_keep isl_ast_node *node_for,
                                         loop_p outer, tree type,
                                         tree lb, tree ub, ivs_params &ip);
-
-  /* All loops generated by create_empty_loop_on_edge have the form of
-     a post-test loop:
-
-     do
-
-     {
-     body of the loop;
-     } while (lower bound < upper bound);
-
-     We create a new if region protecting the loop to be executed, if
-     the execution count is zero (lower bound > upper bound).  */
   edge graphite_create_new_loop_guard (edge entry_edge,
                                       __isl_keep isl_ast_node *node_for,
                                       tree *type,
                                       tree *lb, tree *ub, ivs_params &ip);
-
-  /* Creates a new if region corresponding to ISL's cond.  */
   edge graphite_create_new_guard (edge entry_edge,
                                  __isl_take isl_ast_expr *if_cond,
                                  ivs_params &ip);
-
-  /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
-     variables of the loops around GBB in SESE.
-
-     FIXME: Instead of using a vec<tree> that maps each loop id to a possible
-     chrec, we could consider using a map<int, tree> that maps loop ids to the
-     corresponding tree expressions.  */
   void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
                         __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
                         sese_l &region);
-
-  /* Patch the missing arguments of the phi nodes.  */
-
   void translate_pending_phi_nodes (void);
-
-  /* Add ISL's parameter identifiers and corresponding trees to ivs_params.  */
-
   void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
-
-  /* Get the maximal number of schedule dimensions in the scop SCOP.  */
-
-  int get_max_schedule_dimensions (scop_p scop);
-
-  /* Generates a build, which specifies the constraints on the parameters.  */
-
   __isl_give isl_ast_build *generate_isl_context (scop_p scop);
 
-  /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
-
-     For schedules with different dimensionality, the isl AST generator can not
-     define an order and will just randomly choose an order.  The solution to
-     this problem is to extend all schedules to the maximal number of schedule
-     dimensions (using '0's for the remaining values).  */
-
-  __isl_give isl_map *extend_schedule (__isl_take isl_map *schedule,
-                                      int nb_schedule_dims);
-
-  /* Generates a schedule, which specifies an order used to
-     visit elements in a domain.  */
-
-  __isl_give isl_union_map *generate_isl_schedule (scop_p scop);
-
-  /* Set the separate option for all dimensions.
-     This helps to reduce control overhead.  */
-
-  __isl_give isl_ast_build * set_options (__isl_take isl_ast_build *control,
-                                         __isl_keep isl_union_map *schedule);
-
-  /* Generate isl AST from schedule of SCOP.  Also, collects IVS_PARAMS in
-     IP.  */
-
-  __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop, ivs_params &ip);
-
-
-  /* Return true if RENAME (defined in BB) is a valid use in NEW_BB.  The
-     definition should flow into use, and the use should respect the loop-closed
-     SSA form.  */
+  __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop);
 
   bool is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
-                       bool loop_phi, tree old_name, basic_block old_bb) const;
-
-  /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
-     NEW_BB from RENAME_MAP.  LOOP_PHI is true when we want to rename OLD_NAME
-     within a loop PHI instruction.  */
-
+                       phi_node_kind, tree old_name, basic_block old_bb) const;
   tree get_rename (basic_block new_bb, tree old_name,
-                  basic_block old_bb, bool loop_phi) const;
-
-  /* For ops which are scev_analyzeable, we can regenerate a new name from
-  its scalar evolution around LOOP.  */
-
+                  basic_block old_bb, phi_node_kind) const;
   tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
                             basic_block new_bb, basic_block old_bb,
                             vec<tree> iv_map);
-
-  /* Returns a basic block that could correspond to where a constant was defined
-     in the original code.  In the original code OLD_BB had the definition, we
-     need to find which basic block out of the copies of old_bb, in the new
-     region, should a definition correspond to if it has to reach BB.  */
-
   basic_block get_def_bb_for_const (basic_block bb, basic_block old_bb) const;
-
-  /* Get the new name of OP (from OLD_BB) to be used in NEW_BB.  LOOP_PHI is
-     true when we want to rename an OP within a loop PHI instruction.  */
-
   tree get_new_name (basic_block new_bb, tree op,
-                    basic_block old_bb, bool loop_phi) const;
-
-  /* Collect all the operands of NEW_EXPR by recursively visiting each
-     operand.  */
-
+                    basic_block old_bb, phi_node_kind) const;
   void collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa);
-
-  /* Copy the PHI arguments from OLD_PHI to the NEW_PHI.  The arguments to
-     NEW_PHI must be found unless they can be POSTPONEd for later.  */
-
   bool copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
                           gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
                           bool postpone);
-
-  /* Copy loop phi nodes from BB to NEW_BB.  */
-
   bool copy_loop_phi_nodes (basic_block bb, basic_block new_bb);
-
-  /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
-     the close phi node PHI.  */
-
   bool add_close_phis_to_merge_points (gphi *old_phi, gphi *new_phi,
                                       tree default_value);
-
   tree add_close_phis_to_outer_loops (tree last_merge_name, edge merge_e,
                                      gimple *old_close_phi);
-
-  /* Copy all the loop-close phi args from BB to NEW_BB.  */
-
   bool copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
                                 bool postpone);
-
-  /* Copy loop close phi nodes from BB to NEW_BB.  */
-
   bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb);
-
-  /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
-     region.  If postpone is true and it isn't possible to copy any arg of PHI,
-     the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
-     Returns false if the copying was unsuccessful.  */
-
   bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
                           bool postpone);
-
-  /* Copy cond phi nodes from BB to NEW_BB.  A cond-phi node is a basic block
-  containing phi nodes coming from two predecessors, and none of them are back
-  edges.  */
-
   bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb,
                            vec<tree> iv_map);
-
-  /* Duplicates the statements of basic block BB into basic block NEW_BB
-     and compute the new induction variables according to the IV_MAP.
-     CODEGEN_ERROR is set when the code generation cannot continue.  */
-
   bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
                                       vec<tree> iv_map);
-
-  /* Copies BB and includes in the copied BB all the statements that can
-     be reached following the use-def chains from the memory accesses,
-     and returns the next edge following this new block.  codegen_error is
-     set when the code generation cannot continue.  */
-
   edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
                                       vec<tree> iv_map);
-
-  /* Given a basic block containing close-phi it returns the new basic block
-     where to insert a copy of the close-phi nodes.  All the uses in close phis
-     should come from a single loop otherwise it returns NULL.  */
   edge edge_for_new_close_phis (basic_block bb);
-
-  /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
-     DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates
-     the other pred of OLD_BB as well.  If no such basic block exists then it is
-     NULL.  NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it
-     cannot be NULL.
-
-     Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice
-     versa.  In this case DOMINATING_PRED = NULL.
-
-     Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
-
-     Returns true on successful copy of the args, false otherwise.  */
-
   bool add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
                                 edge old_bb_dominating_edge,
                                 edge old_bb_non_dominating_edge,
                                 gphi *phi, gphi *new_phi,
                                 basic_block new_bb);
-
-  /* Renames the scalar uses of the statement COPY, using the substitution map
-     RENAME_MAP, inserting the gimplification code at GSI_TGT, for the
-     translation REGION, with the original copied statement in LOOP, and using
-     the induction variable renaming map IV_MAP.  Returns true when something
-     has been renamed.  codegen_error is set when the code generation cannot
-     continue.  */
-
   bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
                    basic_block old_bb, loop_p loop, vec<tree> iv_map);
-
-  /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
-     When OLD_NAME and EXPR are the same we assert.  */
-
   void set_rename (tree old_name, tree expr);
-
-  /* Create new names for all the definitions created by COPY and add
-     replacement mappings for each new name.  */
-
   void set_rename_for_each_def (gimple *stmt);
-
-  /* Insert each statement from SEQ at its earliest insertion p.  */
-
   void gsi_insert_earliest (gimple_seq seq);
-
-  /* Rename all the operands of NEW_EXPR by recursively visiting each
-     operand.  */
-
   tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb);
-
-  bool codegen_error_p () const
-  { return codegen_error; }
-
-  /* Prints NODE to FILE.  */
-
-  void print_isl_ast_node (FILE *file, __isl_keep isl_ast_node *node,
-                          __isl_keep isl_ctx *ctx) const;
-
-  /* Return true when OP is a constant tree.  */
-
+  bool codegen_error_p () const { return codegen_error; }
   bool is_constant (tree op) const
   {
     return TREE_CODE (op) == INTEGER_CST
@@ -483,7 +280,7 @@ private:
   /* The region to be translated.  */
   sese_info_p region;
 
-  /* This flag is set when an error occurred during the translation of ISL AST
+  /* This flag is set when an error occurred during the translation of isl AST
      to Gimple.  */
   bool codegen_error;
 
@@ -494,15 +291,14 @@ private:
 /* Return the tree variable that corresponds to the given isl ast identifier
    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
+   FIXME: We should replace blind conversion 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.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 gcc_expression_from_isl_ast_expr_id (tree type,
-                                    __isl_keep isl_ast_expr *expr_id,
+                                    __isl_take isl_ast_expr *expr_id,
                                     ivs_params &ip)
 {
   gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
@@ -514,14 +310,17 @@ gcc_expression_from_isl_ast_expr_id (tree type,
              "Could not map isl_id to tree expression");
   isl_ast_expr_free (expr_id);
   tree t = res->second;
-  return fold_convert (type, t);
+  tree *val = region->parameter_rename_map->get(t);
+
+  if (!val)
+   val = &t;
+  return fold_convert (type, *val);
 }
 
 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
    type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
 {
   gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
@@ -542,16 +341,20 @@ gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
    type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
 {
   isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
   tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
   arg_expr = isl_ast_expr_get_op_arg (expr, 1);
   tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
+
   enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
   isl_ast_expr_free (expr);
+
+  if (codegen_error_p ())
+    return NULL_TREE;
+
   switch (expr_type)
     {
     case isl_ast_op_add:
@@ -564,15 +367,44 @@ binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
       return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
     case isl_ast_op_div:
+      /* As isl operates on arbitrary precision numbers, we may end up with
+        division by 2^64 that is folded to 0.  */
+      if (integer_zerop (tree_rhs_expr))
+       {
+         codegen_error = true;
+         return NULL_TREE;
+       }
       return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
     case isl_ast_op_pdiv_q:
+      /* As isl operates on arbitrary precision numbers, we may end up with
+        division by 2^64 that is folded to 0.  */
+      if (integer_zerop (tree_rhs_expr))
+       {
+         codegen_error = true;
+         return NULL_TREE;
+       }
       return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
+    case isl_ast_op_zdiv_r:
     case isl_ast_op_pdiv_r:
+      /* As isl operates on arbitrary precision numbers, we may end up with
+        division by 2^64 that is folded to 0.  */
+      if (integer_zerop (tree_rhs_expr))
+       {
+         codegen_error = true;
+         return NULL_TREE;
+       }
       return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
     case isl_ast_op_fdiv_q:
+      /* As isl operates on arbitrary precision numbers, we may end up with
+        division by 2^64 that is folded to 0.  */
+      if (integer_zerop (tree_rhs_expr))
+       {
+         codegen_error = true;
+         return NULL_TREE;
+       }
       return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
 
     case isl_ast_op_and:
@@ -605,44 +437,43 @@ binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
    type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
 {
-  gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
+  enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
+  gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
   isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
-  tree tree_first_expr
-    = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
   arg_expr = isl_ast_expr_get_op_arg (expr, 1);
-  tree tree_second_expr
-    = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
   arg_expr = isl_ast_expr_get_op_arg (expr, 2);
-  tree tree_third_expr
-    = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
   isl_ast_expr_free (expr);
-  return fold_build3 (COND_EXPR, type, tree_first_expr,
-                     tree_second_expr, tree_third_expr);
+
+  if (codegen_error_p ())
+    return NULL_TREE;
+
+  return fold_build3 (COND_EXPR, type, a, b, c);
 }
 
 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
    type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
 {
   gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
   isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
   tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
   isl_ast_expr_free (expr);
-  return fold_build1 (NEGATE_EXPR, type, tree_expr);
+  return codegen_error_p () ? NULL_TREE
+    : fold_build1 (NEGATE_EXPR, type, tree_expr);
 }
 
 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
    to a GCC expression tree of type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
 {
   enum tree_code op_code;
@@ -661,11 +492,25 @@ nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
     }
   isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
   tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
+
+  if (codegen_error_p ())
+    {
+      isl_ast_expr_free (expr);
+      return NULL_TREE;
+    }
+
   int i;
   for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
     {
       arg_expr = isl_ast_expr_get_op_arg (expr, i);
       tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
+
+      if (codegen_error_p ())
+       {
+         isl_ast_expr_free (expr);
+         return NULL_TREE;
+       }
+
       res = fold_build2 (op_code, type, res, t);
     }
   isl_ast_expr_free (expr);
@@ -675,11 +520,16 @@ nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
    type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
                                 ivs_params &ip)
 {
+  if (codegen_error_p ())
+    {
+      isl_ast_expr_free (expr);
+      return NULL_TREE;
+    }
+
   gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
   switch (isl_ast_expr_get_op_type (expr))
     {
@@ -688,7 +538,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_select:
       gcc_unreachable ();
 
     case isl_ast_op_max:
@@ -702,6 +551,7 @@ gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
     case isl_ast_op_pdiv_q:
     case isl_ast_op_pdiv_r:
     case isl_ast_op_fdiv_q:
+    case isl_ast_op_zdiv_r:
     case isl_ast_op_and:
     case isl_ast_op_or:
     case isl_ast_op_eq:
@@ -715,6 +565,7 @@ gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
       return unary_op_to_tree (type, expr, ip);
 
     case isl_ast_op_cond:
+    case isl_ast_op_select:
       return ternary_op_to_tree (type, expr, ip);
 
     default:
@@ -724,14 +575,19 @@ gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
   return NULL_TREE;
 }
 
-/* Converts an ISL AST expression E back to a GCC expression tree of
+/* Converts an isl AST expression E back to a GCC expression tree of
    type TYPE.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
                                    ivs_params &ip)
 {
+  if (codegen_error_p ())
+    {
+      isl_ast_expr_free (expr);
+      return NULL_TREE;
+    }
+
   switch (isl_ast_expr_get_type (expr))
     {
     case isl_ast_expr_id:
@@ -754,18 +610,22 @@ gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
    induction variable for the new LOOP.  New LOOP is attached to CFG
    starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
    becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
-   ISL's scattering name to the induction variable created for the
+   isl's scattering name to the induction variable created for the
    loop of STMT.  The new induction variable is inserted in the NEWIVS
    vector and is of type TYPE.  */
 
-struct loop *
-translate_isl_ast_to_gimple::
+struct loop *translate_isl_ast_to_gimple::
 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
                          loop_p outer, tree type, tree lb, tree ub,
                          ivs_params &ip)
 {
   isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
   tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
+
+  /* To fail code generation, we generate wrong code until we discard it.  */
+  if (codegen_error_p ())
+    stride = integer_zero_node;
+
   tree ivvar = create_tmp_var (type, "graphite_IV");
   tree iv, iv_after_increment;
   loop_p loop = create_empty_loop_on_edge
@@ -787,8 +647,7 @@ graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
 
    - NEXT_E is the edge where new generated code should be attached.  */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 translate_isl_ast_for_loop (loop_p context_loop,
                            __isl_keep isl_ast_node *node_for, edge next_e,
                            tree type, tree lb, tree ub,
@@ -895,8 +754,7 @@ get_upper_bound (__isl_keep isl_ast_node *node_for)
    We create a new if region protecting the loop to be executed, if
    the execution count is zero (lower bound > upper bound).  */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 graphite_create_new_loop_guard (edge entry_edge,
                                __isl_keep isl_ast_node *node_for, tree *type,
                                tree *lb, tree *ub, ivs_params &ip)
@@ -909,8 +767,17 @@ graphite_create_new_loop_guard (edge entry_edge,
     build_nonstandard_integer_type (graphite_expression_type_precision, 0);
   isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
   *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
+
+  /* To fail code generation, we generate wrong code until we discard it.  */
+  if (codegen_error_p ())
+    *lb = integer_zero_node;
+
   isl_ast_expr *upper_bound = get_upper_bound (node_for);
   *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
+
+  /* To fail code generation, we generate wrong code until we discard it.  */
+  if (codegen_error_p ())
+    *ub = integer_zero_node;
   
   /* When ub is simply a constant or a parameter, use lb <= ub.  */
   if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
@@ -934,15 +801,15 @@ graphite_create_new_loop_guard (edge entry_edge,
   if (integer_onep (cond_expr))
     exit_edge = entry_edge;
   else
-    exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
+    exit_edge = create_empty_if_region_on_edge (entry_edge,
+                                               unshare_expr (cond_expr));
 
   return exit_edge;
 }
 
 /* Translates an isl_ast_node_for to Gimple. */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
                            edge next_e, ivs_params &ip)
 {
@@ -977,8 +844,7 @@ translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
    chrec, we could consider using a map<int, tree> that maps loop ids to the
    corresponding tree expressions.  */
 
-void
-translate_isl_ast_to_gimple::
+void translate_isl_ast_to_gimple::
 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
                  __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
                  sese_l &region)
@@ -993,6 +859,11 @@ build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
       tree type =
        build_nonstandard_integer_type (graphite_expression_type_precision, 0);
       tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
+
+      /* To fail code generation, we generate wrong code until we discard it.  */
+      if (codegen_error_p ())
+       t = integer_zero_node;
+
       loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
       iv_map[old_loop->num] = t;
     }
@@ -1002,8 +873,7 @@ build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
 
    FIXME: We should remove iv_map.create (loop->num + 1), if it is possible.  */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
                             edge next_e, ivs_params &ip)
 {
@@ -1033,16 +903,17 @@ translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
   build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
   isl_ast_expr_free (user_expr);
 
+  basic_block old_bb = GBB_BB (gbb);
   if (dump_file)
     {
-      fprintf (dump_file, "[codegen] copying from basic block\n");
+      fprintf (dump_file,
+              "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
+              old_bb->index, next_e->src->index, next_e->dest->index);
       print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
-      fprintf (dump_file, "[codegen] to new basic block\n");
-      print_loops_bb (dump_file, next_e->src, 0, 3);
+
     }
 
-  next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), next_e,
-                                          iv_map);
+  next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
 
   iv_map.release ();
 
@@ -1060,8 +931,7 @@ translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
 
 /* Translates an isl_ast_node_block to Gimple. */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 translate_isl_ast_node_block (loop_p context_loop,
                              __isl_keep isl_ast_node *node,
                              edge next_e, ivs_params &ip)
@@ -1079,24 +949,27 @@ translate_isl_ast_node_block (loop_p context_loop,
   return next_e;
 }
  
-/* Creates a new if region corresponding to ISL's cond.  */
+/* Creates a new if region corresponding to isl's cond.  */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 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);
+
+  /* To fail code generation, we generate wrong code until we discard it.  */
+  if (codegen_error_p ())
+    cond_expr = integer_zero_node;
+
   edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
   return exit_edge;
 }
 
 /* Translates an isl_ast_node_if to Gimple.  */
 
-edge
-translate_isl_ast_to_gimple::
+edge translate_isl_ast_to_gimple::
 translate_isl_ast_node_if (loop_p context_loop,
                           __isl_keep isl_ast_node *node,
                           edge next_e, ivs_params &ip)
@@ -1120,13 +993,12 @@ translate_isl_ast_node_if (loop_p context_loop,
   return last_e;
 }
 
-/* Translates an ISL AST node NODE to GCC representation in the
+/* Translates an isl AST node NODE to GCC representation in the
    context of a SESE.  */
 
-edge
-translate_isl_ast_to_gimple::translate_isl_ast (loop_p context_loop,
-                                               __isl_keep isl_ast_node *node,
-                                               edge next_e, ivs_params &ip)
+edge translate_isl_ast_to_gimple::
+translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
+                  edge next_e, ivs_params &ip)
 {
   if (codegen_error_p ())
     return NULL;
@@ -1151,6 +1023,14 @@ translate_isl_ast_to_gimple::translate_isl_ast (loop_p context_loop,
       return translate_isl_ast_node_block (context_loop, node,
                                           next_e, ip);
 
+    case isl_ast_node_mark:
+      {
+       isl_ast_node *n = isl_ast_node_mark_get_node (node);
+       edge e = translate_isl_ast (context_loop, n, next_e, ip);
+       isl_ast_node_free (n);
+       return e;
+      }
+
     default:
       gcc_unreachable ();
     }
@@ -1173,9 +1053,7 @@ bb_contains_loop_close_phi_nodes (basic_block bb)
 static bool
 bb_contains_loop_phi_nodes (basic_block bb)
 {
-  gcc_assert (EDGE_COUNT (bb->preds) <= 2);
-
-  if (bb->preds->length () == 1)
+  if (EDGE_COUNT (bb->preds) != 2)
     return false;
 
   unsigned depth = loop_depth (bb->loop_father);
@@ -1253,10 +1131,9 @@ phi_uses_name (basic_block bb, tree name)
    definition should flow into use, and the use should respect the loop-closed
    SSA form.  */
 
-bool
-translate_isl_ast_to_gimple::
+bool translate_isl_ast_to_gimple::
 is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
-                bool loop_phi, tree old_name, basic_block old_bb) const
+                phi_node_kind phi_kind, tree old_name, basic_block old_bb) const
 {
   /* The def of the rename must either dominate the uses or come from a
      back-edge.  Also the def must respect the loop closed ssa form.  */
@@ -1264,7 +1141,7 @@ is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
     {
       if (dump_file)
        {
-         fprintf (dump_file, "[codegen] rename not in loop closed ssa:");
+         fprintf (dump_file, "[codegen] rename not in loop closed ssa: ");
          print_generic_expr (dump_file, rename, 0);
          fprintf (dump_file, "\n");
        }
@@ -1274,7 +1151,7 @@ is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
   if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
     return true;
 
-  if (bb_contains_loop_phi_nodes (use_bb) && loop_phi)
+  if (bb_contains_loop_phi_nodes (use_bb) && phi_kind == loop_phi)
     {
       /* The loop-header dominates the loop-body.  */
       if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
@@ -1293,14 +1170,11 @@ is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
 }
 
 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
-   NEW_BB from RENAME_MAP.  LOOP_PHI is true when we want to rename OLD_NAME
-   within a loop PHI instruction.  */
+   NEW_BB from RENAME_MAP.  PHI_KIND determines the kind of phi node.  */
 
-tree
-translate_isl_ast_to_gimple::get_rename (basic_block new_bb,
-                                        tree old_name,
-                                        basic_block old_bb,
-                                        bool loop_phi) const
+tree translate_isl_ast_to_gimple::
+get_rename (basic_block new_bb, tree old_name, basic_block old_bb,
+           phi_node_kind phi_kind) const
 {
   gcc_assert (TREE_CODE (old_name) == SSA_NAME);
   vec <tree> *renames = region->rename_map->get (old_name);
@@ -1314,7 +1188,9 @@ translate_isl_ast_to_gimple::get_rename (basic_block new_bb,
       if (TREE_CODE (rename) == SSA_NAME)
        {
          basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename));
-         if (is_valid_rename (rename, bb, new_bb, loop_phi, old_name, old_bb))
+         if (is_valid_rename (rename, bb, new_bb, phi_kind, old_name, old_bb)
+             && (phi_kind == close_phi
+                 || flow_bb_inside_loop_p (bb->loop_father, new_bb)))
            return rename;
          return NULL_TREE;
        }
@@ -1342,6 +1218,9 @@ translate_isl_ast_to_gimple::get_rename (basic_block new_bb,
       if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb))
        continue;
 
+      if (!flow_bb_inside_loop_p (t2_bb->loop_father, new_bb))
+       continue;
+
       /* Compute the nearest dominator.  */
       if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
        {
@@ -1356,8 +1235,8 @@ translate_isl_ast_to_gimple::get_rename (basic_block new_bb,
 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
    When OLD_NAME and EXPR are the same we assert.  */
 
-void
-translate_isl_ast_to_gimple::set_rename (tree old_name, tree expr)
+void translate_isl_ast_to_gimple::
+set_rename (tree old_name, tree expr)
 {
   if (dump_file)
     {
@@ -1382,6 +1261,13 @@ translate_isl_ast_to_gimple::set_rename (tree old_name, tree expr)
       r.safe_push (expr);
       region->rename_map->put (old_name, r);
     }
+
+  tree t;
+  int i;
+  /* For a parameter of a scop we don't want to rename it.  */
+  FOR_EACH_VEC_ELT (region->params, i, t)
+    if (old_name == t)
+      region->parameter_rename_map->put(old_name, expr);
 }
 
 /* Return an iterator to the instructions comes last in the execution order.
@@ -1397,6 +1283,18 @@ later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
   /* Find the iterator which is the latest.  */
   if (bb1 == bb2)
     {
+      gimple *stmt1 = gsi_stmt (gsi1);
+      gimple *stmt2 = gsi_stmt (gsi2);
+
+      if (stmt1 != NULL && stmt2 != NULL)
+       {
+         bool is_phi1 = gimple_code (stmt1) == GIMPLE_PHI;
+         bool is_phi2 = gimple_code (stmt2) == GIMPLE_PHI;
+
+         if (is_phi1 != is_phi2)
+           return is_phi1 ? gsi2 : gsi1;
+       }
+
       /* For empty basic blocks gsis point to the end of the sequence.  Since
         there is no operator== defined for gimple_stmt_iterator and for gsis
         not pointing to a valid statement gsi_next would assert.  */
@@ -1420,8 +1318,8 @@ later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
 
 /* Insert each statement from SEQ at its earliest insertion p.  */
 
-void
-translate_isl_ast_to_gimple::gsi_insert_earliest (gimple_seq seq)
+void translate_isl_ast_to_gimple::
+gsi_insert_earliest (gimple_seq seq)
 {
   update_modified_stmts (seq);
   sese_l &codegen_region = region->if_region->true_region->region;
@@ -1491,10 +1389,11 @@ translate_isl_ast_to_gimple::gsi_insert_earliest (gimple_seq seq)
 /* Collect all the operands of NEW_EXPR by recursively visiting each
    operand.  */
 
-void
-translate_isl_ast_to_gimple::collect_all_ssa_names (tree new_expr,
-                                                   vec<tree> *vec_ssa)
+void translate_isl_ast_to_gimple::
+collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa)
 {
+  if (new_expr == NULL_TREE)
+    return;
 
   /* Rename all uses in new_expr.  */
   if (TREE_CODE (new_expr) == SSA_NAME)
@@ -1511,8 +1410,8 @@ translate_isl_ast_to_gimple::collect_all_ssa_names (tree new_expr,
     }
 }
 
-/* This is abridged version of the function:
-   tree.c:substitute_in_expr (tree exp, tree f, tree r). */
+/* This is abridged version of the function copied from:
+   tree.c:substitute_in_expr (tree exp, tree f, tree r).  */
 
 static tree
 substitute_ssa_name (tree exp, tree f, tree r)
@@ -1575,7 +1474,7 @@ substitute_ssa_name (tree exp, tree f, tree r)
        if (exp == f)
          return r;
 
-       /* Fall through...  */
+       /* Fall through.  */
 
       case tcc_exceptional:
       case tcc_unary:
@@ -1654,16 +1553,15 @@ substitute_ssa_name (tree exp, tree f, tree r)
 
 /* Rename all the operands of NEW_EXPR by recursively visiting each operand.  */
 
-tree
-translate_isl_ast_to_gimple::rename_all_uses (tree new_expr, basic_block new_bb,
-                                             basic_block old_bb)
+tree translate_isl_ast_to_gimple::
+rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb)
 {
   auto_vec<tree, 2> ssa_names;
   collect_all_ssa_names (new_expr, &ssa_names);
   tree t;
   int i;
   FOR_EACH_VEC_ELT (ssa_names, i, t)
-    if (tree r = get_rename (new_bb, t, old_bb, false))
+    if (tree r = get_rename (new_bb, t, old_bb, unknown_phi))
       new_expr = substitute_ssa_name (new_expr, t, r);
 
   return new_expr;
@@ -1672,8 +1570,7 @@ translate_isl_ast_to_gimple::rename_all_uses (tree new_expr, basic_block new_bb,
 /* For ops which are scev_analyzeable, we can regenerate a new name from its
    scalar evolution around LOOP.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
                      basic_block new_bb, basic_block old_bb,
                      vec<tree> iv_map)
@@ -1704,8 +1601,6 @@ get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
       return build_zero_cst (TREE_TYPE (old_name));
     }
 
-  /* We should check all the operands and all of them should dominate the use at
-     new_expr.  */
   if (TREE_CODE (new_expr) == SSA_NAME)
     {
       basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
@@ -1717,15 +1612,23 @@ get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
     }
 
   new_expr = rename_all_uses (new_expr, new_bb, old_bb);
-  /* We should check all the operands and all of them should dominate the use at
+
+  /* We check all the operands and all of them should dominate the use at
      new_expr.  */
-  if (TREE_CODE (new_expr) == SSA_NAME)
+  auto_vec <tree, 2> new_ssa_names;
+  collect_all_ssa_names (new_expr, &new_ssa_names);
+  int i;
+  tree new_ssa_name;
+  FOR_EACH_VEC_ELT (new_ssa_names, i, new_ssa_name)
     {
-      basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
-      if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
+      if (TREE_CODE (new_ssa_name) == SSA_NAME)
        {
-         codegen_error = true;
-         return build_zero_cst (TREE_TYPE (old_name));
+         basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_ssa_name));
+         if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
+           {
+             codegen_error = true;
+             return build_zero_cst (TREE_TYPE (old_name));
+           }
        }
     }
 
@@ -1738,14 +1641,11 @@ get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
    substitution map RENAME_MAP, inserting the gimplification code at
    GSI_TGT, for the translation REGION, with the original copied
    statement in LOOP, and using the induction variable renaming map
-   IV_MAP.  Returns true when something has been renamed.  codegen_error
-   is set when the code generation cannot continue.  */
+   IV_MAP.  Returns true when something has been renamed.  */
 
-bool
-translate_isl_ast_to_gimple::rename_uses (gimple *copy,
-                                         gimple_stmt_iterator *gsi_tgt,
-                                         basic_block old_bb,
-                                         loop_p loop, vec<tree> iv_map)
+bool translate_isl_ast_to_gimple::
+rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt, basic_block old_bb,
+            loop_p loop, vec<tree> iv_map)
 {
   bool changed = false;
 
@@ -1786,7 +1686,7 @@ translate_isl_ast_to_gimple::rename_uses (gimple *copy,
 
       changed = true;
       tree new_expr = get_rename (gsi_tgt->bb, old_name,
-                                 old_bb, false);
+                                 old_bb, unknown_phi);
 
       if (new_expr)
        {
@@ -1853,9 +1753,8 @@ translate_isl_ast_to_gimple::rename_uses (gimple *copy,
    need to find which basic block out of the copies of old_bb, in the new
    region, should a definition correspond to if it has to reach BB.  */
 
-basic_block
-translate_isl_ast_to_gimple::get_def_bb_for_const (basic_block bb,
-                                                  basic_block old_bb) const
+basic_block translate_isl_ast_to_gimple::
+get_def_bb_for_const (basic_block bb, basic_block old_bb) const
 {
   vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
 
@@ -1881,23 +1780,21 @@ translate_isl_ast_to_gimple::get_def_bb_for_const (basic_block bb,
        b1 = b2;
     }
 
-  gcc_assert (b1);
   return b1;
 }
 
-/* Get the new name of OP (from OLD_BB) to be used in NEW_BB.  LOOP_PHI is true
-   when we want to rename an OP within a loop PHI instruction.  */
+/* Get the new name of OP (from OLD_BB) to be used in NEW_BB.  PHI_KIND
+   determines the kind of phi node.  */
 
-tree
-translate_isl_ast_to_gimple::
+tree translate_isl_ast_to_gimple::
 get_new_name (basic_block new_bb, tree op,
-             basic_block old_bb, bool loop_phi) const
+             basic_block old_bb, phi_node_kind phi_kind) const
 {
   /* For constants the names are the same.  */
-  if (is_constant (op))
+  if (TREE_CODE (op) != SSA_NAME)
     return op;
 
-  return get_rename (new_bb, op, old_bb, loop_phi);
+  return get_rename (new_bb, op, old_bb, phi_kind);
 }
 
 /* Return a debug location for OP.  */
@@ -1933,8 +1830,7 @@ get_edges (basic_block bb)
 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI.  The arguments to NEW_PHI
    must be found unless they can be POSTPONEd for later.  */
 
-bool
-translate_isl_ast_to_gimple::
+bool translate_isl_ast_to_gimple::
 copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
                    gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
                    bool postpone)
@@ -1952,7 +1848,7 @@ copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
 
       tree old_name = gimple_phi_arg_def (old_phi, i);
       tree new_name = get_new_name (new_bb, old_name,
-                                   gimple_bb (old_phi), true);
+                                   gimple_bb (old_phi), loop_phi);
       if (new_name)
        {
          add_phi_arg (new_phi, new_name, e, get_loc (old_name));
@@ -1981,9 +1877,8 @@ copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
 
 /* Copy loop phi nodes from BB to NEW_BB.  */
 
-bool
-translate_isl_ast_to_gimple::copy_loop_phi_nodes (basic_block bb,
-                                                 basic_block new_bb)
+bool translate_isl_ast_to_gimple::
+copy_loop_phi_nodes (basic_block bb, basic_block new_bb)
 {
   if (dump_file)
     fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n",
@@ -2013,8 +1908,14 @@ translate_isl_ast_to_gimple::copy_loop_phi_nodes (basic_block bb,
                                         gimple_phi_result_ptr (new_phi));
       set_rename (res, new_res);
       codegen_error = !copy_loop_phi_args (phi, ibp_old_bb, new_phi,
-                                         ibp_new_bb, true);
+                                          ibp_new_bb, true);
       update_stmt (new_phi);
+
+      if (dump_file)
+       {
+         fprintf (dump_file, "[codegen] creating loop-phi node: ");
+         print_gimple_stmt (dump_file, new_phi, 0, 0);
+       }
     }
 
   return true;
@@ -2185,10 +2086,8 @@ add_close_phis_to_merge_points (gphi *old_close_phi, gphi *new_close_phi,
 
 /* Copy all the loop-close phi args from BB to NEW_BB.  */
 
-bool
-translate_isl_ast_to_gimple::copy_loop_close_phi_args (basic_block old_bb,
-                                                      basic_block new_bb,
-                                                      bool postpone)
+bool translate_isl_ast_to_gimple::
+copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb, bool postpone)
 {
   for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
        gsi_next (&psi))
@@ -2208,7 +2107,7 @@ translate_isl_ast_to_gimple::copy_loop_close_phi_args (basic_block old_bb,
       set_rename (res, new_res);
 
       tree old_name = gimple_phi_arg_def (old_close_phi, 0);
-      tree new_name = get_new_name (new_bb, old_name, old_bb, false);
+      tree new_name = get_new_name (new_bb, old_name, old_bb, close_phi);
 
       /* Predecessor basic blocks of a loop close phi should have been code
         generated before.  FIXME: This is fixable by merging PHIs from inner
@@ -2260,9 +2159,8 @@ translate_isl_ast_to_gimple::copy_loop_close_phi_args (basic_block old_bb,
 
 /* Copy loop close phi nodes from BB to NEW_BB.  */
 
-bool
-translate_isl_ast_to_gimple::copy_loop_close_phi_nodes (basic_block old_bb,
-                                                       basic_block new_bb)
+bool translate_isl_ast_to_gimple::
+copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb)
 {
   if (dump_file)
     fprintf (dump_file, "[codegen] copying loop close phi nodes in bb_%d.\n",
@@ -2287,8 +2185,7 @@ translate_isl_ast_to_gimple::copy_loop_close_phi_nodes (basic_block old_bb,
 
    Returns true on successful copy of the args, false otherwise.  */
 
-bool
-translate_isl_ast_to_gimple::
+bool translate_isl_ast_to_gimple::
 add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
                          edge old_bb_dominating_edge,
                          edge old_bb_non_dominating_edge,
@@ -2450,10 +2347,8 @@ add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
    the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
    Returns false if the copying was unsuccessful.  */
 
-bool
-translate_isl_ast_to_gimple::copy_cond_phi_args (gphi *phi, gphi *new_phi,
-                                                vec<tree> iv_map,
-                                                bool postpone)
+bool translate_isl_ast_to_gimple::
+copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map, bool postpone)
 {
   if (dump_file)
     fprintf (dump_file, "[codegen] copying cond phi args.\n");
@@ -2482,7 +2377,7 @@ translate_isl_ast_to_gimple::copy_cond_phi_args (gphi *phi, gphi *new_phi,
   for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
     {
       tree old_name = gimple_phi_arg_def (phi, i);
-      tree new_name = get_new_name (new_bb, old_name, old_bb, false);
+      tree new_name = get_new_name (new_bb, old_name, old_bb, cond_phi);
       old_phi_args[i] = old_name;
       if (new_name)
        {
@@ -2531,7 +2426,7 @@ translate_isl_ast_to_gimple::copy_cond_phi_args (gphi *phi, gphi *new_phi,
                  fprintf (dump_file, "\n");
                }
              gsi_insert_earliest (stmts);
-             new_phi_args [i] = new_name;
+             new_phi_args[i] = new_expr;
              continue;
            }
 
@@ -2567,21 +2462,20 @@ translate_isl_ast_to_gimple::copy_cond_phi_args (gphi *phi, gphi *new_phi,
    containing phi nodes coming from two predecessors, and none of them are back
    edges.  */
 
-bool
-translate_isl_ast_to_gimple::copy_cond_phi_nodes (basic_block bb,
-                                                 basic_block new_bb,
-                                                 vec<tree> iv_map)
+bool translate_isl_ast_to_gimple::
+copy_cond_phi_nodes (basic_block bb, basic_block new_bb, vec<tree> iv_map)
 {
 
   gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
 
+  /* TODO: Handle cond phi nodes with more than 2 predecessors.  */
+  if (EDGE_COUNT (bb->preds) != 2)
+    return false;
+
   if (dump_file)
     fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n",
             new_bb->index);
 
-  /* Cond phi nodes should have exactly two arguments.  */
-  gcc_assert (2 == EDGE_COUNT (bb->preds));
-
   for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
        gsi_next (&psi))
     {
@@ -2589,9 +2483,6 @@ translate_isl_ast_to_gimple::copy_cond_phi_nodes (basic_block bb,
       tree res = gimple_phi_result (phi);
       if (virtual_operand_p (res))
        continue;
-      if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
-       /* Cond phi nodes should not be scev_analyzable_p.  */
-       gcc_unreachable ();
 
       gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
       tree new_res = create_new_def_for (res, new_phi,
@@ -2628,14 +2519,22 @@ should_copy_to_new_region (gimple *stmt, sese_info_p region)
       && scev_analyzable_p (lhs, region->region))
     return false;
 
+  /* Do not copy parameters that have been generated in the header of the
+     scop.  */
+  if (is_gimple_assign (stmt)
+      && (lhs = gimple_assign_lhs (stmt))
+      && TREE_CODE (lhs) == SSA_NAME
+      && region->parameter_rename_map->get(lhs))
+    return false;
+
   return true;
 }
 
 /* Create new names for all the definitions created by COPY and add replacement
    mappings for each new name.  */
 
-void
-translate_isl_ast_to_gimple::set_rename_for_each_def (gimple *stmt)
+void translate_isl_ast_to_gimple::
+set_rename_for_each_def (gimple *stmt)
 {
   def_operand_p def_p;
   ssa_op_iter op_iter;
@@ -2648,13 +2547,11 @@ translate_isl_ast_to_gimple::set_rename_for_each_def (gimple *stmt)
 }
 
 /* Duplicates the statements of basic block BB into basic block NEW_BB
-   and compute the new induction variables according to the IV_MAP.
-   CODEGEN_ERROR is set when the code generation cannot continue.  */
+   and compute the new induction variables according to the IV_MAP.  */
 
-bool
-translate_isl_ast_to_gimple::graphite_copy_stmts_from_block (basic_block bb,
-                                                            basic_block new_bb,
-                                                            vec<tree> iv_map)
+bool translate_isl_ast_to_gimple::
+graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
+                               vec<tree> iv_map)
 {
   /* Iterator poining to the place where new statement (s) will be inserted.  */
   gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
@@ -2693,6 +2590,25 @@ translate_isl_ast_to_gimple::graphite_copy_stmts_from_block (basic_block bb,
       if (codegen_error_p ())
        return false;
 
+      /* For each SSA_NAME in the parameter_rename_map rename their usage.  */
+      ssa_op_iter iter;
+      use_operand_p use_p;
+      if (!is_gimple_debug (copy))
+       FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
+         {
+           tree old_name = USE_FROM_PTR (use_p);
+
+           if (TREE_CODE (old_name) != SSA_NAME
+               || SSA_NAME_IS_DEFAULT_DEF (old_name))
+             continue;
+
+           tree *new_expr = region->parameter_rename_map->get (old_name);
+           if (!new_expr)
+             continue;
+
+           replace_exp (use_p, *new_expr);
+         }
+
       update_stmt (copy);
     }
 
@@ -2704,8 +2620,8 @@ translate_isl_ast_to_gimple::graphite_copy_stmts_from_block (basic_block bb,
    to insert a copy of the close-phi nodes.  All the uses in close phis should
    come from a single loop otherwise it returns NULL.  */
 
-edge
-translate_isl_ast_to_gimple::edge_for_new_close_phis (basic_block bb)
+edge translate_isl_ast_to_gimple::
+edge_for_new_close_phis (basic_block bb)
 {
   /* Make sure that NEW_BB is the new_loop->exit->dest.  We find the definition
      of close phi in the original code and then find the mapping of basic block
@@ -2742,13 +2658,10 @@ translate_isl_ast_to_gimple::edge_for_new_close_phis (basic_block bb)
 
 /* Copies BB and includes in the copied BB all the statements that can
    be reached following the use-def chains from the memory accesses,
-   and returns the next edge following this new block.  codegen_error is
-   set when the code generation cannot continue.  */
+   and returns the next edge following this new block.  */
 
-edge
-translate_isl_ast_to_gimple::copy_bb_and_scalar_dependences (basic_block bb,
-                                                            edge next_e,
-                                                            vec<tree> iv_map)
+edge translate_isl_ast_to_gimple::
+copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
 {
   int num_phis = number_of_phi_nodes (bb);
 
@@ -2813,6 +2726,26 @@ translate_isl_ast_to_gimple::copy_bb_and_scalar_dependences (basic_block bb,
              return NULL;
            }
 
+         /* In case isl did some loop peeling, like this:
+
+              S_8(0);
+              for (int c1 = 1; c1 <= 5; c1 += 1) {
+                S_8(c1);
+              }
+              S_8(6);
+
+            there should be no loop-phi nodes in S_8(0).
+
+            FIXME: We need to reason about dynamic instances of S_8, i.e., the
+            values of all scalar variables: for the moment we instantiate only
+            SCEV analyzable expressions on the iteration domain, and we need to
+            extend that to reductions that cannot be analyzed by SCEV.  */
+         if (!bb_in_sese_p (phi_bb, region->if_region->true_region->region))
+           {
+             codegen_error = true;
+             return NULL;
+           }
+
          if (dump_file)
            fprintf (dump_file, "[codegen] bb_%d contains loop phi nodes.\n",
                     bb->index);
@@ -2837,6 +2770,7 @@ translate_isl_ast_to_gimple::copy_bb_and_scalar_dependences (basic_block bb,
 
          /* If a corresponding merge-point was not found, then abort codegen.  */
          if (phi_bb->loop_father != loop_father
+             || !bb_in_sese_p (phi_bb, region->if_region->true_region->region)
              || !copy_cond_phi_nodes (bb, phi_bb, iv_map))
            {
              codegen_error = true;
@@ -2871,8 +2805,8 @@ translate_isl_ast_to_gimple::copy_bb_and_scalar_dependences (basic_block bb,
 
 /* Patch the missing arguments of the phi nodes.  */
 
-void
-translate_isl_ast_to_gimple::translate_pending_phi_nodes ()
+void translate_isl_ast_to_gimple::
+translate_pending_phi_nodes ()
 {
   int i;
   phi_rename *rename;
@@ -2907,30 +2841,15 @@ translate_isl_ast_to_gimple::translate_pending_phi_nodes ()
          fprintf (dump_file, "[codegen] to new-phi: ");
          print_gimple_stmt (dump_file, new_phi, 0, 0);
        }
-      if (codegen_error)
+      if (codegen_error_p ())
        return;
     }
 }
 
-/* Prints NODE to FILE.  */
+/* Add isl's parameter identifiers and corresponding trees to ivs_params.  */
 
-void
-translate_isl_ast_to_gimple::print_isl_ast_node (FILE *file,
-                                                __isl_keep isl_ast_node *node,
-                                                __isl_keep isl_ctx *ctx) const
-{
-  isl_printer *prn = isl_printer_to_file (ctx, file);
-  prn = isl_printer_set_output_format (prn, ISL_FORMAT_C);
-  prn = isl_printer_print_ast_node (prn, node);
-  prn = isl_printer_print_str (prn, "\n");
-  isl_printer_free (prn);
-}
-
-/* Add ISL's parameter identifiers and corresponding trees to ivs_params.  */
-
-void
-translate_isl_ast_to_gimple::add_parameters_to_ivs_params (scop_p scop,
-                                                          ivs_params &ip)
+void translate_isl_ast_to_gimple::
+add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
 {
   sese_info_p region = scop->scop_info;
   unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
@@ -2947,88 +2866,13 @@ translate_isl_ast_to_gimple::add_parameters_to_ivs_params (scop_p scop,
 
 /* Generates a build, which specifies the constraints on the parameters.  */
 
-__isl_give isl_ast_build *
-translate_isl_ast_to_gimple::generate_isl_context (scop_p scop)
+__isl_give isl_ast_build *translate_isl_ast_to_gimple::
+generate_isl_context (scop_p scop)
 {
   isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
   return isl_ast_build_from_context (context_isl);
 }
 
-/* Get the maximal number of schedule dimensions in the scop SCOP.  */
-
-int
-translate_isl_ast_to_gimple::get_max_schedule_dimensions (scop_p scop)
-{
-  int i;
-  poly_bb_p pbb;
-  int schedule_dims = 0;
-
-  FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
-    {
-      int pbb_schedule_dims = isl_map_dim (pbb->transformed, isl_dim_out);
-      if (pbb_schedule_dims > schedule_dims)
-       schedule_dims = pbb_schedule_dims;
-    }
-
-  return schedule_dims;
-}
-
-/* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
-
-   For schedules with different dimensionality, the isl AST generator can not
-   define an order and will just randomly choose an order.  The solution to this
-   problem is to extend all schedules to the maximal number of schedule
-   dimensions (using '0's for the remaining values).  */
-
-__isl_give isl_map *
-translate_isl_ast_to_gimple::extend_schedule (__isl_take isl_map *schedule,
-                                             int nb_schedule_dims)
-{
-  int tmp_dims = isl_map_dim (schedule, isl_dim_out);
-  schedule =
-    isl_map_add_dims (schedule, isl_dim_out, nb_schedule_dims - tmp_dims);
-  isl_val *zero =
-    isl_val_int_from_si (isl_map_get_ctx (schedule), 0);
-  int i;
-  for (i = tmp_dims; i < nb_schedule_dims; i++)
-    {
-      schedule
-       = isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero));
-    }
-  isl_val_free (zero);
-  return schedule;
-}
-
-/* Generates a schedule, which specifies an order used to
-   visit elements in a domain.  */
-
-__isl_give isl_union_map *
-translate_isl_ast_to_gimple::generate_isl_schedule (scop_p scop)
-{
-  int nb_schedule_dims = get_max_schedule_dimensions (scop);
-  int i;
-  poly_bb_p pbb;
-  isl_union_map *schedule_isl =
-    isl_union_map_empty (isl_set_get_space (scop->param_context));
-
-  FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
-    {
-      /* Dead code elimination: when the domain of a PBB is empty,
-        don't generate code for the PBB.  */
-      if (isl_set_is_empty (pbb->domain))
-       continue;
-
-      isl_map *bb_schedule = isl_map_copy (pbb->transformed);
-      bb_schedule = isl_map_intersect_domain (bb_schedule,
-                                             isl_set_copy (pbb->domain));
-      bb_schedule = extend_schedule (bb_schedule, nb_schedule_dims);
-      schedule_isl
-       = isl_union_map_union (schedule_isl,
-                              isl_union_map_from_map (bb_schedule));
-    }
-  return schedule_isl;
-}
-
 /* This method is executed before the construction of a for node.  */
 __isl_give isl_id *
 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
@@ -3046,61 +2890,98 @@ ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
   return id;
 }
 
-/* Set the separate option for all dimensions.
-   This helps to reduce control overhead.  */
+/* Generate isl AST from schedule of SCOP.  */
 
-__isl_give isl_ast_build *
-translate_isl_ast_to_gimple::set_options (__isl_take isl_ast_build *control,
-                                         __isl_keep isl_union_map *schedule)
+__isl_give isl_ast_node *translate_isl_ast_to_gimple::
+scop_to_isl_ast (scop_p scop)
 {
-  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);
-  return isl_ast_build_set_options (control, options);
-}
-
-/* Generate isl AST from schedule of SCOP.  Also, collects IVS_PARAMS in IP.  */
+  gcc_assert (scop->transformed_schedule);
 
-__isl_give isl_ast_node *
-translate_isl_ast_to_gimple::scop_to_isl_ast (scop_p scop, ivs_params &ip)
-{
-  /* Generate loop upper bounds that consist of the current loop iterator, an
-     operator (< or <=) and an expression not involving the iterator.  If this
-     option is not set, then the current loop iterator may appear several times
-     in the upper bound.  See the isl manual for more details.  */
-  isl_options_set_ast_build_atomic_upper_bound (scop->isl_context, true);
-
-  add_parameters_to_ivs_params (scop, ip);
-  isl_union_map *schedule_isl = generate_isl_schedule (scop);
+  /* Set the separate option to reduce control flow overhead.  */
+  isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
+    (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
   isl_ast_build *context_isl = generate_isl_context (scop);
-  context_isl = set_options (context_isl, schedule_isl);
-  isl_union_map *dependences = NULL;
+
   if (flag_loop_parallelize_all)
     {
-      dependences = scop_get_dependences (scop);
+      scop_get_dependences (scop);
       context_isl =
        isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
-                                          dependences);
+                                          scop->dependence);
     }
-  isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
-                                                          schedule_isl);
-  if (dependences)
-    isl_union_map_free (dependences);
+
+  isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
+    (context_isl, schedule);
   isl_ast_build_free (context_isl);
   return ast_isl;
 }
 
-/* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
-   the given SCOP.  Return true if code generation succeeded.
+/* Copy def from sese REGION to the newly created TO_REGION. TR is defined by
+   DEF_STMT. GSI points to entry basic block of the TO_REGION.  */
+
+static void
+copy_def (tree tr, gimple *def_stmt, sese_info_p region, sese_info_p to_region,
+         gimple_stmt_iterator *gsi)
+{
+  if (!defined_in_sese_p (tr, region->region))
+    return;
+
+  ssa_op_iter iter;
+  use_operand_p use_p;
+  FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
+    {
+      tree use_tr = USE_FROM_PTR (use_p);
+
+      /* Do not copy parameters that have been generated in the header of the
+        scop.  */
+      if (region->parameter_rename_map->get(use_tr))
+       continue;
+
+      gimple *def_of_use = SSA_NAME_DEF_STMT (use_tr);
+      if (!def_of_use)
+       continue;
+
+      copy_def (use_tr, def_of_use, region, to_region, gsi);
+    }
+
+  gimple *copy = gimple_copy (def_stmt);
+  gsi_insert_after (gsi, copy, GSI_NEW_STMT);
+
+  /* Create new names for all the definitions created by COPY and
+     add replacement mappings for each new name.  */
+  def_operand_p def_p;
+  ssa_op_iter op_iter;
+  FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
+    {
+      tree old_name = DEF_FROM_PTR (def_p);
+      tree new_name = create_new_def_for (old_name, copy, def_p);
+      region->parameter_rename_map->put(old_name, new_name);
+    }
+
+  update_stmt (copy);
+}
+
+static void
+copy_internal_parameters (sese_info_p region, sese_info_p to_region)
+{
+  /* For all the parameters which definitino is in the if_region->false_region,
+     insert code on true_region (if_region->true_region->entry). */
+
+  int i;
+  tree tr;
+  gimple_stmt_iterator gsi = gsi_start_bb(to_region->region.entry->dest);
 
-   FIXME: This is not yet a full implementation of the code generator
-   with ISL ASTs.  Generation of GIMPLE code has to be completed.  */
+  FOR_EACH_VEC_ELT (region->params, i, tr)
+    {
+      // If def is not in region.
+      gimple *def_stmt = SSA_NAME_DEF_STMT (tr);
+      if (def_stmt)
+       copy_def (tr, def_stmt, region, to_region, &gsi);
+    }
+}
+
+/* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
+   Return true if code generation succeeded.  */
 
 bool
 graphite_regenerate_ast_isl (scop_p scop)
@@ -3113,12 +2994,20 @@ graphite_regenerate_ast_isl (scop_p scop)
   ivs_params ip;
 
   timevar_push (TV_GRAPHITE_CODE_GEN);
-  root_node = t.scop_to_isl_ast (scop, ip);
+  t.add_parameters_to_ivs_params (scop, ip);
+  root_node = t.scop_to_isl_ast (scop);
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
-      fprintf (dump_file, "ISL AST generated by ISL: \n");
-      t.print_isl_ast_node (dump_file, root_node, scop->isl_context);
+      fprintf (dump_file, "[scheduler] original schedule:\n");
+      print_isl_schedule (dump_file, scop->original_schedule);
+      fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
+      print_isl_schedule (dump_file, scop->transformed_schedule);
+
+      fprintf (dump_file, "[scheduler] original ast:\n");
+      print_schedule_ast (dump_file, scop->original_schedule, scop);
+      fprintf (dump_file, "[scheduler] AST generated by isl:\n");
+      print_isl_ast (dump_file, root_node);
     }
 
   recompute_all_dominators ();
@@ -3130,6 +3019,9 @@ graphite_regenerate_ast_isl (scop_p scop)
 
   loop_p context_loop = region->region.entry->src->loop_father;
 
+  /* Copy all the parameters which are defined in the region.  */
+  copy_internal_parameters(if_region->false_region, if_region->true_region);
+
   edge e = single_succ_edge (if_region->true_region->region.entry->dest);
   basic_block bb = split_edge (e);
 
@@ -3140,8 +3032,8 @@ graphite_regenerate_ast_isl (scop_p scop)
   if (t.codegen_error_p ())
     {
       if (dump_file)
-       fprintf (dump_file, "[codegen] unsuccessful,"
-                " reverting back to the original code.\n");
+       fprintf (dump_file, "codegen error: "
+                "reverting back to the original code.\n");
       set_ifsese_condition (if_region, integer_zero_node);
     }
   else
@@ -3161,6 +3053,9 @@ graphite_regenerate_ast_isl (scop_p scop)
          scev_reset ();
          recompute_all_dominators ();
          graphite_verify ();
+
+         if (dump_file)
+           fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
        }
       else
        {