>> However, this form doesn't have loop guards which are generated by
>> graphite_create_new_loop_guard in gcc/graphite-isl-ast-to-gimple.c and
>> by graphite_create_new_loop_guard in graphite-clast-to-gimple.c.
>
>
> Maybe the guards are directly constant folded? Can you try with:

I've tried this. It seems that the result is the same. If we consider
the following code, we'll see that the guards are generated:

int
main (int n, int *a)
{
  int i;

  for (i = n; i < 100; i++)
    a[i] = i;

  return 0;
}

gcc/graphite-isl-ast-to-gimple.c

loop_0 (header = 0, latch = 1, niter = )
{
  bb_2 (preds = {bb_0 }, succs = {bb_4 bb_3 })
  {
    <bb 2>:
    if (n_3(D) <= 99)
      goto <bb 4>;
    else
      goto <bb 3>;

  }
  bb_3 (preds = {bb_2 bb_8 }, succs = {bb_1 })
  {
    <bb 3>:
    # .MEM_12 = PHI <.MEM_4(D)(2), .MEM_11(8)>
    # VUSE <.MEM_12>
    return 0;

  }
  bb_4 (preds = {bb_2 }, succs = {bb_5 bb_8 })
  {
    <bb 4>:
    _2 = n_3(D) <= 99;
    if (_2 != 0)
      goto <bb 5>;
    else
      goto <bb 8>;

  }
  bb_5 (preds = {bb_4 }, succs = {bb_6 })
  {
    <bb 5>:
    _1 = (__int128) n_3(D);
    _16 = 99 - _1;

  }
  bb_8 (preds = {bb_4 bb_6 }, succs = {bb_3 })
  {
    <bb 8>:
    # .MEM_11 = PHI <.MEM_4(D)(4), .MEM_4(D)(6)>
    goto <bb 3>;

  }
  loop_2 (header = 6, latch = 7, niter = (uint128_t) MAX_EXPR <_16,
0>, upper_bound = 0x7ffffffffffffffffffffffffffffffe)
  {
    bb_6 (preds = {bb_5 bb_7 }, succs = {bb_7 bb_8 })
    {
      <bb 6>:
      # graphite_IV.3_17 = PHI <0(5), graphite_IV.3_18(7)>
      graphite_IV.3_18 = graphite_IV.3_17 + 1;
      if (graphite_IV.3_17 < _16)
        goto <bb 7>;
      else
        goto <bb 8>;

    }
    bb_7 (preds = {bb_6 }, succs = {bb_6 })
    {
      <bb 7>:
      goto <bb 6>;

    }
  }
}

graphite-clast-to-gimple.c

loop_0 (header = 0, latch = 1, niter = )
{
  bb_2 (preds = {bb_0 }, succs = {bb_4 bb_3 })
  {
    <bb 2>:
    if (n_3(D) <= 99)
      goto <bb 4>;
    else
      goto <bb 3>;

  }
  bb_3 (preds = {bb_2 bb_8 }, succs = {bb_1 })
  {
    <bb 3>:
    # .MEM_12 = PHI <.MEM_4(D)(2), .MEM_25(8)>
    # VUSE <.MEM_12>
    return 0;

  }
  bb_4 (preds = {bb_2 }, succs = {bb_5 bb_8 })
  {
    <bb 4>:
    _2 = n_3(D) <= 99;
    if (_2 != 0)
      goto <bb 5>;
    else
      goto <bb 8>;

  }
  bb_5 (preds = {bb_4 }, succs = {bb_6 })
  {
    <bb 5>:
    _1 = 99 - n_3(D);

  }
  bb_8 (preds = {bb_6 bb_4 }, succs = {bb_3 })
  {
    <bb 8>:
    # .MEM_25 = PHI <.MEM_18(6), .MEM_4(D)(4)>
    goto <bb 3>;

  }
  loop_2 (header = 6, latch = 7, niter = (unsigned int) MAX_EXPR <_1,
0>, upper_bound = 2147483646)
  {
    bb_6 (preds = {bb_5 bb_7 }, succs = {bb_7 bb_8 })
    {
      <bb 6>:
      # graphite_IV.3_16 = PHI <0(5), graphite_IV.3_17(7)>
      # .MEM_26 = PHI <.MEM_4(D)(5), .MEM_18(7)>
      _19 = (sizetype) n_3(D);
      _20 = (sizetype) graphite_IV.3_16;
      _21 = _19 + _20;
      _22 = _21 * 4;
      _23 = a_7(D) + _22;
      _24 = n_3(D) + graphite_IV.3_16;
      # .MEM_18 = VDEF <.MEM_26>
      *_23 = _24;
      graphite_IV.3_17 = graphite_IV.3_16 + 1;
      if (graphite_IV.3_16 < _1)
        goto <bb 7>;
      else
        goto <bb 8>;

    }
    bb_7 (preds = {bb_6 }, succs = {bb_6 })
    {
      <bb 7>:
      goto <bb 6>;

    }
  }
}

>> Below is the code of this generation (It still uses isl_int for
>> generation of isl_expr_int, because the error related to isl/val_gmp.h
>> still arises. I've tried to use isl 0.12.2 and 0.13, but gotten the
>> same error).
>
>
> Did using 'extern "C"' around the include statement not help?

I can't build gcc without using 'extern "C"'. After the successful
building the error is arising when I am trying to compile, for
example, the following code:

int
main (int n, int *a)
{
  int i;

  for (i = n; i < 100; i++)
    a[i] = i;

  return 0;
}

>> +/* Stores the INDEX in a vector and the loop nesting LEVEL for a given
>> +   isl_id NAME.  BOUND_ONE and BOUND_TWO represent the exact lower and
>> +   upper bounds that can be inferred from the polyhedral representation.
>> */
>
>
> Why do you mention BOUND_ONE & BOUND_TWO? I do not see any use of them?

This is a misprint. I've fixed it.

> Any reason this is not a simpel std::map<isl_id *, tree>, but you instead
> have this manually implemented hash table?

I think that using of std::map may create installation dependency,
which requires libstdc++. I've asked the community about this.

>Does gcc have a policy that forbids std::map?

I think, that it is normal to use std::map from the GNU C++ Library.

> Is there a reason we have separat newivs_index and params_index maps?
> This was necessary for CLooG as far as I remember, but for isl a simple
> isl_id -> tree map should be sufficient, no?

> Is there aneed for the level at all? I think in the current
> implementation and for isl in general it may not be needed as we have
> isl_ids to find the corresponding loop ivs.

Yes, this is redundant and removed in the new version. I wanted to
write the code, which reuses the code from graphite-clast-to-gimple.c
as much as possible, to reduce a number of errors. After this, I
planned to eliminate redundant parts.

> Please explain why we need a special function to get the upper bound.
> Specifically, explain that isl in some configurations can generate loop
> exit conditions such as:
>
> for (i = ?; i + 2 >= 3 && 3 <= i + m; i++)
>   ;
>
> This get upper bound assumes a certain shape of loop exit condition(
>
> for (i = ?; i < expr; i++)
>
> Also, you need to set the option --no-isl-ast-build-atomic-upper-bound
> in isl_ast_build to be able to rely on this behavior.
> (You did this below)
>
> Do you have a specific motivation, why you don't want to support
> arbitrary expressions? I assume this is necessary for the if - do-while
> optimization. If this is the case please state this.

I haven't found another function for generation loops in Gimple form.
The create_empty_loop_on_edge needs, which is being used now, requires
upper bound.

 /* create_empty_loop_on_edge
    |
    |    - pred_bb -             -------- pred_bb ------
    |   |           |                 | iv0 = initial_value |
    |    -----|-----                  --------------|-----------
    |         |                            ___    | entry_edge
    |         | entry_edge         /      |    |
    |         |             ====> /   -----V---V- loop_header -------------
    |         V                     |    |  iv_before = phi (iv0, iv_after) |
    |    - succ_bb -           |
----|-----------------------------------------
    |   |           |                |         |
    |    -----------                |      ---V--- loop_body
--------------------
    |                               |     | iv_after = iv_before + stride     |
    |                               |     | if (iv_before < upper_bound)     |
    |                               |
---|--------------------------\--------------
    |                               |         |
   \ exit_e
    |                               |         V                           \
    |                               |       - loop_latch -
---V- succ_bb -
    |                               |      |                   |
|                        |
    |                               |       /-----------------
-------------------------
    |                                \ _ /


Furthermore, at the moment of loop generation we don't have the
induction variable, which is need for generation of a loop condition
in case of the option –no-isl-ast-build-atomic-upper-bound is unset.
The induction variable is returned by create_empty_loop_on_edge. Could
you please advise me another function to generate them?

> Please explain why we do not just generate a loop that has the loop
> bound at the top, but instead create a structure of the form
>
> if (lb > ub)
>   do {
>
>   } while (lb ..)
>
> (Such a figure, but completed might help).
>
> (I think the original motivation was that later we may be able to prove
> that a loop is executed at least once which allows us to remove the if
> which again enables better loop invariant code motion)

I didn't have special intentions for this. As was mentioned above, I
haven't found another way for generation of loops.

> Why are the previous two functions necessary?

Yes, they are unimportant. I've replaced them with
add_parameters_to_ivs_params, which add tree representations and names
of parameters to ivs_param

> Why did you switch back to an isl_map?
> This seems incorrect for scops with more than two statements.

Yes, this is a mistake. I've fixed it.

> P.S.: I just wanted to let you know that your work is amazing. Almost
> fully unsupervised you are always providing high-quality patches! I am
> very impressed.

Thank you!

--
                                   Cheers, Roman Gareev
Index: gcc/graphite-isl-ast-to-gimple.c
===================================================================
--- gcc/graphite-isl-ast-to-gimple.c    (revision 212194)
+++ gcc/graphite-isl-ast-to-gimple.c    (working copy)
@@ -42,16 +42,610 @@
 #include "cfgloop.h"
 #include "tree-data-ref.h"
 #include "sese.h"
+#include "tree-ssa-loop-manip.h"
+#include "tree-scalar-evolution.h"
 
 #ifdef HAVE_cloog
 #include "graphite-poly.h"
 #include "graphite-isl-ast-to-gimple.h"
+#include "graphite-htab.h"
 
 /* This flag is set when an error occurred during the translation of
    ISL AST to Gimple.  */
 
 static bool graphite_regenerate_error;
 
+/* We always use signed 128, until the isl is able to give information about
+types  */
+
+static tree *graphite_temporary_tree_type = &int128_integer_type_node;
+
+/* Converts a GMP constant VAL to a tree and returns it.  */
+
+static tree
+gmp_cst_to_tree (tree type, mpz_t val)
+{
+  tree t = type ? type : integer_type_node;
+  mpz_t tmp;
+
+  mpz_init (tmp);
+  mpz_set (tmp, val);
+  wide_int wi = wi::from_mpz (t, tmp, true);
+  mpz_clear (tmp);
+
+  return wide_int_to_tree (t, wi);
+}
+
+/* Verifies properties that GRAPHITE should maintain during translation.  */
+
+static inline void
+graphite_verify (void)
+{
+#ifdef ENABLE_CHECKING
+  verify_loop_structure ();
+  verify_loop_closed_ssa (true);
+#endif
+}
+
+/* Stores the INDEX in a vector for a given isl_id NAME.  */
+
+typedef struct ast_isl_name_index {
+  int index;
+  const char *name;
+  /* If free_name is set, the content of name was allocated by us and needs
+     to be freed.  */
+  char *free_name;
+} *ast_isl_name_index_p;
+
+/* Helper for hashing ast_isl_name_index.  */
+
+struct ast_isl_index_hasher
+{
+  typedef ast_isl_name_index value_type;
+  typedef ast_isl_name_index compare_type;
+  static inline hashval_t hash (const value_type *);
+  static inline bool equal (const value_type *, const compare_type *);
+  static inline void remove (value_type *);
+};
+
+/* Computes a hash function for database element E.  */
+
+inline hashval_t
+ast_isl_index_hasher::hash (const value_type *e)
+{
+  hashval_t hash = 0;
+
+  int length = strlen (e->name);
+  int i;
+
+  for (i = 0; i < length; ++i)
+    hash = hash | (e->name[i] << (i % 4));
+
+  return hash;
+}
+
+/* Compares database elements ELT1 and ELT2.  */
+
+inline bool
+ast_isl_index_hasher::equal (const value_type *elt1, const compare_type *elt2)
+{
+  return strcmp (elt1->name, elt2->name) == 0;
+}
+
+/* Free the memory taken by a ast_isl_name_index struct.  */
+
+inline void
+ast_isl_index_hasher::remove (value_type *c)
+{
+  if (c->free_name)
+    free (c->free_name);
+  free (c);
+}
+
+typedef hash_table<ast_isl_index_hasher> ast_isl_index_htab_type;
+
+/* Returns a pointer to a new element of type ast_isl_name_index_p built
+   from NAME, INDEX.  */
+
+static inline ast_isl_name_index_p
+new_ast_isl_name_index (const char *name, int index)
+{
+  ast_isl_name_index_p res = XNEW (struct ast_isl_name_index);
+  char *new_name = XNEWVEC (char, strlen (name) + 1);
+  strcpy (new_name, name);
+
+  res->name = new_name;
+  res->free_name = new_name;
+  res->index = index;
+  return res;
+}
+
+/* For a given name of the isl_id, which is stored in the isl_ast_expr EXPR_ID,
+   returns -1 if it does not correspond to any parameter, or otherwise, returns
+   the index in the PARAMS or SCATTERING_DIMENSIONS vector.  */
+
+static inline int
+ast_expr_id_to_index (__isl_keep isl_ast_expr *expr_id,
+                     ast_isl_index_htab_type *index_table)
+{
+  struct ast_isl_name_index tmp;
+  ast_isl_name_index **slot;
+
+  isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
+  tmp.name = isl_id_get_name (tmp_isl_id);
+  tmp.free_name = NULL;
+
+  slot = index_table->find_slot (&tmp, NO_INSERT);
+
+  isl_id_free (tmp_isl_id);
+  if (slot && *slot)
+    return (*slot)->index;
+
+  return -1;
+}
+
+/* Records in INDEX_TABLE the INDEX and for NAME.  */
+
+static inline void
+save_isl_id_name_index (ast_isl_index_htab_type *index_table,
+                       const char *name, int index)
+{
+  struct ast_isl_name_index tmp;
+  ast_isl_name_index **slot;
+
+  tmp.name = name;
+  tmp.free_name = NULL;
+  slot = index_table->find_slot (&tmp, INSERT);
+
+  if (slot)
+    {
+      free (*slot);
+      *slot = new_ast_isl_name_index (name, index);
+    }
+}
+
+/* INDEX binds ISL's scattering and parameter name to the index of the tree
+   induction variable and paramatere in IVS_PARAMS_VEC.
+
+   PARAMS_INDEX binds ISL's parameter name to the index of the tree
+   parameter in PARAMS.  */
+
+typedef struct ivs_params {
+  vec<tree> ivs_params_vec;
+  ast_isl_index_htab_type *ivs_params_index;
+  sese region;
+} *ivs_params_p;
+
+static tree
+gcc_expression_from_isl_expression (tree type, __isl_keep isl_ast_expr *,
+                                   ivs_params_p ip);
+
+/* Returns the tree variable from the name of isl_id, which is stored
+   in the isl_ast_expr EXPR_ID that was given in ISL representation.  */
+
+static tree
+gcc_expression_from_isl_ast_expr_id (__isl_keep isl_ast_expr *expr_id,
+                                    ivs_params_p ip)
+{
+  gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
+  int index;
+  gcc_assert (ip->ivs_params_vec.exists () && ip->ivs_params_index);
+  index = ast_expr_id_to_index (expr_id, ip->ivs_params_index);
+  gcc_assert (index >= 0);
+  return (ip->ivs_params_vec)[index];
+}
+
+/* Converts a isl_ast_expr_int expression E to a GCC expression tree of
+   type TYPE.  */
+
+static tree
+gcc_expression_from_isl_expr_int (tree type, __isl_keep isl_ast_expr *expr)
+{
+  gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
+  isl_int val;
+  isl_int_init (val);
+  if (isl_ast_expr_get_int (expr, &val) == -1)
+    {
+      isl_int_clear (val);
+      return NULL_TREE;
+    }
+  else
+    return gmp_cst_to_tree (type, val);
+}
+
+/* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
+   type TYPE.  */
+
+static tree
+binary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr, ivs_params_p 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);
+  isl_ast_expr_free (arg_expr);
+  arg_expr = isl_ast_expr_get_op_arg (expr, 1);
+  tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  isl_ast_expr_free (arg_expr);
+  switch (isl_ast_expr_get_op_type (expr))
+    {
+    case isl_ast_op_add:
+      return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_sub:
+      return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_mul:
+      return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_div:
+      return fold_build2 (EXACT_DIV_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);
+
+    case isl_ast_op_and:
+      return fold_build2 (TRUTH_ANDIF_EXPR, type,
+                         tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_or:
+      return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_eq:
+      return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_le:
+      return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_lt:
+      return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_ge:
+      return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    case isl_ast_op_gt:
+      return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+    default:
+      gcc_unreachable ();
+    }
+}
+
+/* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
+   type TYPE.  */
+
+static tree
+ternary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr, ivs_params_p 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_first_expr
+    = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  isl_ast_expr_free (arg_expr);
+  arg_expr = isl_ast_expr_get_op_arg (expr, 1);
+  tree tree_second_expr
+    = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  isl_ast_expr_free (arg_expr);
+  arg_expr = isl_ast_expr_get_op_arg (expr, 2);
+  tree tree_third_expr
+    = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  isl_ast_expr_free (arg_expr);
+  return fold_build3 (COND_EXPR, type, tree_first_expr,
+                     tree_second_expr, tree_third_expr);
+}
+
+/* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
+   type TYPE.  */
+
+static tree
+unary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr, ivs_params_p 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 (arg_expr);
+  return fold_build1 (NEGATE_EXPR, type, tree_expr);
+}
+
+/* Converts a isl_ast_expr_op expression E with unknown number of arguments
+   to a GCC expression tree of type TYPE.  */
+
+static tree
+nary_op_to_tree (tree type, __isl_keep isl_ast_expr *expr, ivs_params_p ip)
+{
+  enum tree_code op_code;
+  switch (isl_ast_expr_get_op_type (expr))
+    {
+    case isl_ast_op_max:
+      op_code = MAX_EXPR;
+      break;
+
+    case isl_ast_op_min:
+      op_code = MIN_EXPR;
+      break;
+
+    default:
+      gcc_unreachable ();    
+    }
+  isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
+  tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
+  isl_ast_expr_free (arg_expr);
+  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);
+      res = fold_build2 (op_code, type, res, t);
+      isl_ast_expr_free (arg_expr);
+    }
+  return res;
+}
+
+
+/* Converts an isl_ast_expr_op expression E to a GCC expression tree of
+   type TYPE.  */
+
+static tree
+gcc_expression_from_isl_expr_op (tree type, __isl_keep isl_ast_expr *expr,
+                                ivs_params_p ip)
+{
+  gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
+  switch (isl_ast_expr_get_op_type (expr))
+    {
+    /* These isl ast expressions are not supported yet.  */
+    case isl_ast_op_error:
+    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 ();
+
+    case isl_ast_op_max:
+    case isl_ast_op_min:
+      return nary_op_to_tree (type, expr, ip);
+
+    case isl_ast_op_add:
+    case isl_ast_op_sub:
+    case isl_ast_op_mul:
+    case isl_ast_op_div:
+    case isl_ast_op_fdiv_q:
+    case isl_ast_op_and:
+    case isl_ast_op_or:
+    case isl_ast_op_eq:
+    case isl_ast_op_le:
+    case isl_ast_op_lt:
+    case isl_ast_op_ge:
+    case isl_ast_op_gt:
+      return binary_op_to_tree (type, expr, ip);
+
+    case isl_ast_op_minus:
+      return unary_op_to_tree (type, expr, ip);
+
+    case isl_ast_op_cond:
+      return ternary_op_to_tree (type, expr, ip);
+
+    default:
+      gcc_unreachable ();
+    }
+
+  return NULL_TREE;
+}
+
+/* Converts a ISL AST expression E back to a GCC expression tree of
+   type TYPE.  */
+
+static tree
+gcc_expression_from_isl_expression (tree type, __isl_keep isl_ast_expr *expr,
+                                   ivs_params_p ip)
+{
+  switch (isl_ast_expr_get_type (expr))
+    {
+    case isl_ast_expr_id:
+      return gcc_expression_from_isl_ast_expr_id (expr, ip);
+
+    case isl_ast_expr_int:
+      return gcc_expression_from_isl_expr_int (type, expr);
+
+    case isl_ast_expr_op:
+      return gcc_expression_from_isl_expr_op (type, expr, ip);
+
+    default:
+      gcc_unreachable ();
+    }
+
+  return NULL_TREE;
+}
+
+/* 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.  */
+
+static 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_p 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);
+  isl_ast_expr_free (for_inc);
+  tree ivvar = create_tmp_var (type, "graphite_IV");
+  tree iv, iv_after_increment;
+  loop_p loop = create_empty_loop_on_edge
+    (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
+     outer ? outer : entry_edge->src->loop_father);
+
+  isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
+  isl_id *id = isl_ast_expr_get_id (for_iterator);
+  save_isl_id_name_index (ip->ivs_params_index, isl_id_get_name (id),
+                         (ip->ivs_params_vec).length ());
+  isl_id_free (id);
+  isl_ast_expr_free (for_iterator);
+  (ip->ivs_params_vec).safe_push (iv);
+  return loop;
+}
+
+static edge
+translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
+                  edge next_e, ivs_params_p ip);
+
+/* Create the loop for a isl_ast_node_for.
+
+   - NEXT_E is the edge where new generated code should be attached.  */
+
+static 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_p ip)
+{
+  gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
+  struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
+                                               type, lb, ub, ip);
+  edge last_e = single_exit (loop);
+  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.  */
+  isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
+  next_e = translate_isl_ast (loop, for_body, to_body, ip);
+  isl_ast_node_free (for_body);
+  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.  */
+
+  return last_e;
+}
+
+/* Get upper bound from the cond of the for */
+
+static __isl_give isl_ast_expr *
+get_upper_bound (__isl_keep isl_ast_node *node_for)
+{
+  gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
+  isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
+  gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
+  isl_ast_expr *res;
+  switch (isl_ast_expr_get_op_type (for_cond))
+    {
+    case isl_ast_op_le:
+      res = isl_ast_expr_get_op_arg (for_cond, 1);
+      break;
+
+    case isl_ast_op_lt:
+      {
+        // (iteraotr < ub) => (iterator <= ub - 1)
+        isl_val *one = isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 
1);
+        isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
+        res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
+        break;
+      }
+
+    default:
+      gcc_unreachable ();
+    }
+  isl_ast_expr_free (for_cond);
+  return res;
+}
+
+/* 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 (edge entry_edge,
+                               __isl_keep isl_ast_node *node_for, tree *type,
+                               tree *lb, tree *ub, ivs_params_p ip)
+{
+  gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
+  tree cond_expr;
+  edge exit_edge;
+
+  *type = *graphite_temporary_tree_type;
+  isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
+  *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
+  isl_ast_expr_free (for_init);
+  isl_ast_expr *upper_bound = get_upper_bound (node_for);
+  *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
+  isl_ast_expr_free (upper_bound);
+  
+  /* When ub is simply a constant or a parameter, use lb <= ub.  */
+  if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
+    cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
+  else
+    {
+      tree one = (POINTER_TYPE_P (*type)
+                 ? convert_to_ptrofftype (integer_one_node)
+                 : fold_convert (*type, integer_one_node));
+      /* 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^k-1 due to integer overflow, and the condition lb <= ub
+        is true, even if we do not want this.  However lb < ub + 1 is false,
+        as expected.  */
+      tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
+                                : PLUS_EXPR, *type, *ub, one);
+
+      cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
+    }
+
+  exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
+
+  return exit_edge;
+}
+
+/* Translates an isl_ast_node_for to Gimple. */
+
+static edge
+translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
+                           edge next_e, ivs_params_p ip)
+{
+  gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
+  tree type, lb, ub;
+  edge last_e = graphite_create_new_loop_guard (next_e, node, &type,
+                                               &lb, &ub, ip);
+  edge true_e = get_true_edge_from_guard_bb (next_e->dest);
+
+  translate_isl_ast_for_loop (context_loop, node, true_e,
+                             type, lb, ub, ip);
+  return last_e;
+}
+
+/* Translates an ISL AST node NODE to GCC representation in the
+   context of a SESE.  */
+
+static edge
+translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
+                  edge next_e, ivs_params_p ip)
+{
+  switch (isl_ast_node_get_type (node))
+    {
+    case isl_ast_node_error:
+      gcc_unreachable ();
+
+    case isl_ast_node_for:
+      return translate_isl_ast_node_for (context_loop, node,
+                                        next_e, ip);
+
+    case isl_ast_node_if:
+      return next_e;
+
+    case isl_ast_node_user:
+      return next_e;
+
+    case isl_ast_node_block:
+      return next_e;
+
+    default:
+      gcc_unreachable ();
+    }
+}
+
 /* Prints NODE to FILE.  */
 
 void
@@ -65,9 +659,33 @@
   isl_printer_free (prn);
 }
 
+/* Add tree representations and names of parameters to ivs_params  */
+
+static void
+add_parameters_to_ivs_params (scop_p scop, ivs_params_p ip)
+{
+  sese region = SCOP_REGION (scop);
+  int i;
+  int nb_parameters = SESE_PARAMS (region).length ();
+  gcc_assert (nb_parameters == (short) isl_set_dim (scop->context,
+                                                   isl_dim_param));
+
+  for (i = 0; i < nb_parameters; i++)
+    {
+      tree param = SESE_PARAMS (region)[i];
+      const char *name = get_name (param);
+
+      if (!name)
+       name = "T";
+      save_isl_id_name_index (ip->ivs_params_index, name, i);
+      (ip->ivs_params_vec).safe_push (param);
+    }
+}
+
+
 /* Generates a build, which specifies the constraints on the parameters.  */
 
-static isl_ast_build *
+static __isl_give isl_ast_build *
 generate_isl_context (scop_p scop)
 {
   isl_set *context_isl = isl_set_params (isl_set_copy (scop->context));
@@ -77,7 +695,7 @@
 /* Generates a schedule, which specifies an order used to
    visit elements in a domain.  */
 
-static isl_union_map *
+static __isl_give isl_union_map *
 generate_isl_schedule (scop_p scop)
 {
   int i;
@@ -102,9 +720,16 @@
   return schedule_isl;
 }
 
-static isl_ast_node *
-scop_to_isl_ast (scop_p scop)
+static __isl_give isl_ast_node *
+scop_to_isl_ast (scop_p scop, ivs_params_p 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->ctx, true);
+
+  add_parameters_to_ivs_params (scop, ip);
   isl_union_map *schedule_isl = generate_isl_schedule (scop);
   isl_ast_build *context_isl = generate_isl_context (scop);
   isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
@@ -117,21 +742,68 @@
    the given SCOP.  Return true if code generation succeeded.
 
    FIXME: This is not yet a full implementation of the code generator
-          with ISL ASTs. Generation of GIMPLE code is have to be added.  */
+          with ISL ASTs. Generation of GIMPLE code has to be completed.  */
 
 bool
 graphite_regenerate_ast_isl (scop_p scop)
 {
+  loop_p context_loop;
+  sese region = SCOP_REGION (scop);
+  ifsese if_region = NULL;
+  ast_isl_index_htab_type *ivs_params_index;
+  isl_ast_node *root_node;
+  struct ivs_params ip;
+
   timevar_push (TV_GRAPHITE_CODE_GEN);
   graphite_regenerate_error = false;
-  isl_ast_node *root_node = scop_to_isl_ast (scop);
+
+  auto_vec<tree, 10> ivs_params_vec;
+  ivs_params_index = new ast_isl_index_htab_type (10);
+  ip.ivs_params_vec = ivs_params_vec;
+  ip.ivs_params_index = ivs_params_index;
+  ip.region = region;
+
+  root_node = scop_to_isl_ast (scop, &ip);
+
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "\nISL AST generated by ISL: \n");
       print_isl_ast_node (dump_file, root_node, scop->ctx);
+      fprintf (dump_file, "\n");
     }
+
+  recompute_all_dominators ();
+  graphite_verify ();
+
+  if_region = move_sese_in_condition (region);
+  sese_insert_phis_for_liveouts (region,
+                                if_region->region->exit->src,
+                                if_region->false_region->exit,
+                                if_region->true_region->exit);
+  recompute_all_dominators ();
+  graphite_verify ();
+
+  context_loop = SESE_ENTRY (region)->src->loop_father;
+
+  translate_isl_ast (context_loop, root_node, if_region->true_region->entry,
+                    &ip);
+  graphite_verify ();
+  scev_reset ();
+  recompute_all_dominators ();
+  graphite_verify ();
+
+  if (graphite_regenerate_error)
+    set_ifsese_condition (if_region, integer_zero_node);
+
+  free (if_region->true_region);
+  free (if_region->region);
+  free (if_region);
+
+  delete ivs_params_index;
+  ivs_params_index = NULL;
   isl_ast_node_free (root_node);
   timevar_pop (TV_GRAPHITE_CODE_GEN);
+  /* TODO: Add dump  */
   return !graphite_regenerate_error;
 }
 #endif

Reply via email to