]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/graphite-sese-to-poly.c
Update copyright years.
[thirdparty/gcc.git] / gcc / graphite-sese-to-poly.c
index 2af50b5b394f107615f61eb59d477755e0a40b8e..c42415e0554fb132c01662b580ea57618ac90c5c 100644 (file)
@@ -1,5 +1,5 @@
 /* Conversion of SESE regions to Polyhedra.
-   Copyright (C) 2009-2017 Free Software Foundation, Inc.
+   Copyright (C) 2009-2020 Free Software Foundation, Inc.
    Contributed by Sebastian Pop <sebastian.pop@amd.com>.
 
 This file is part of GCC.
@@ -31,7 +31,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree.h"
 #include "gimple.h"
 #include "ssa.h"
-#include "params.h"
 #include "fold-const.h"
 #include "gimple-iterator.h"
 #include "gimplify.h"
@@ -55,175 +54,19 @@ along with GCC; see the file COPYING3.  If not see
 #include <isl/constraint.h>
 #include <isl/aff.h>
 #include <isl/val.h>
-#include <isl/val_gmp.h>
 
 #include "graphite.h"
 
-/* Assigns to RES the value of the INTEGER_CST T.  */
-
-static inline void
-tree_int_to_gmp (tree t, mpz_t res)
-{
-  wi::to_mpz (t, res, TYPE_SIGN (TREE_TYPE (t)));
-}
-
 /* Return an isl identifier for the polyhedral basic block PBB.  */
 
 static isl_id *
 isl_id_for_pbb (scop_p s, poly_bb_p pbb)
 {
-  char name[10];
+  char name[14];
   snprintf (name, sizeof (name), "S_%d", pbb_index (pbb));
   return isl_id_alloc (s->isl_context, name, pbb);
 }
 
-#ifndef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
-/* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
-   We generate SCATTERING_DIMENSIONS scattering dimensions.
-
-   The scattering polyhedron consists of these dimensions: scattering,
-   loop_iterators, parameters.
-
-   Example:
-
-   | scattering_dimensions = 5
-   | nb_iterators = 1
-   | scop_nb_params = 2
-   |
-   | Schedule:
-   |   i
-   | 4 5
-   |
-   | Scattering polyhedron:
-   |
-   | scattering: {s1, s2, s3, s4, s5}
-   | loop_iterators: {i}
-   | parameters: {p1, p2}
-   |
-   | s1  s2  s3  s4  s5  i   p1  p2  1
-   | 1   0   0   0   0   0   0   0  -4  = 0
-   | 0   1   0   0   0  -1   0   0   0  = 0
-   | 0   0   1   0   0   0   0   0  -5  = 0  */
-
-static void
-build_pbb_scattering_polyhedrons (isl_aff *static_sched,
-                                 poly_bb_p pbb)
-{
-  isl_val *val;
-
-  int scattering_dimensions = isl_set_dim (pbb->domain, isl_dim_set) * 2 + 1;
-
-  isl_space *dc = isl_set_get_space (pbb->domain);
-  isl_space *dm = isl_space_add_dims (isl_space_from_domain (dc),
-                                     isl_dim_out, scattering_dimensions);
-  pbb->schedule = isl_map_universe (dm);
-
-  for (int i = 0; i < scattering_dimensions; i++)
-    {
-      /* Textual order inside this loop.  */
-      if ((i % 2) == 0)
-       {
-         isl_constraint *c = isl_equality_alloc
-             (isl_local_space_from_space (isl_map_get_space (pbb->schedule)));
-
-         val = isl_aff_get_coefficient_val (static_sched, isl_dim_in, i / 2);
-         gcc_assert (val && isl_val_is_int (val));
-
-         val = isl_val_neg (val);
-         c = isl_constraint_set_constant_val (c, val);
-         c = isl_constraint_set_coefficient_si (c, isl_dim_out, i, 1);
-         pbb->schedule = isl_map_add_constraint (pbb->schedule, c);
-       }
-
-      /* Iterations of this loop.  */
-      else /* if ((i % 2) == 1) */
-       {
-         int loop = (i - 1) / 2;
-         pbb->schedule = isl_map_equate (pbb->schedule, isl_dim_in, loop,
-                                         isl_dim_out, i);
-       }
-    }
-
-  /* Simplify the original schedule.  */
-  pbb->schedule = isl_map_coalesce (pbb->schedule);
-
-  /* At the beginning, set the transformed schedule to the original.  */
-  pbb->transformed = isl_map_copy (pbb->schedule);
-}
-
-/* Build for BB the static schedule.
-
-   The static schedule is a Dewey numbering of the abstract syntax
-   tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
-
-   The following example informally defines the static schedule:
-
-   A
-   for (i: ...)
-     {
-       for (j: ...)
-         {
-           B
-           C
-         }
-
-       for (k: ...)
-         {
-           D
-           E
-         }
-     }
-   F
-
-   Static schedules for A to F:
-
-     DEPTH
-     0 1 2
-   A 0
-   B 1 0 0
-   C 1 0 1
-   D 1 1 0
-   E 1 1 1
-   F 2
-*/
-
-static void
-build_scop_scattering (scop_p scop)
-{
-  gimple_poly_bb_p previous_gbb = NULL;
-  isl_space *dc = isl_set_get_space (scop->param_context);
-  isl_aff *static_sched;
-
-  dc = isl_space_add_dims (dc, isl_dim_set, number_of_loops (cfun));
-  static_sched = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
-
-  /* We have to start schedules at 0 on the first component and
-     because we cannot compare_prefix_loops against a previous loop,
-     prefix will be equal to zero, and that index will be
-     incremented before copying.  */
-  static_sched = isl_aff_add_coefficient_si (static_sched, isl_dim_in, 0, -1);
-
-  int i;
-  poly_bb_p pbb;
-  FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
-    {
-      gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
-      int prefix = 0;
-
-      if (previous_gbb)
-       prefix = nb_common_loops (scop->scop_info->region, previous_gbb, gbb);
-
-      previous_gbb = gbb;
-
-      static_sched = isl_aff_add_coefficient_si (static_sched, isl_dim_in,
-                                                prefix, 1);
-      build_pbb_scattering_polyhedrons (static_sched, pbb);
-    }
-
-  isl_aff_free (static_sched);
-}
-#endif
-
 static isl_pw_aff *extract_affine (scop_p, tree, __isl_take isl_space *space);
 
 /* Extract an affine expression from the chain of recurrence E.  */
@@ -271,7 +114,7 @@ extract_affine_mul (scop_p s, tree e, __isl_take isl_space *space)
 static isl_id *
 isl_id_for_ssa_name (scop_p s, tree e)
 {
-  char name1[10];
+  char name1[14];
   snprintf (name1, sizeof (name1), "P_%d", SSA_NAME_VERSION (e));
   return isl_id_alloc (s->isl_context, name1, e);
 }
@@ -290,27 +133,40 @@ isl_id_for_dr (scop_p s)
 /* Extract an affine expression from the ssa_name E.  */
 
 static isl_pw_aff *
-extract_affine_name (scop_p s, tree e, __isl_take isl_space *space)
+extract_affine_name (int dimension, __isl_take isl_space *space)
 {
-  isl_id *id = isl_id_for_ssa_name (s, e);
-  int dimension = isl_space_find_dim_by_id (space, isl_dim_param, id);
-  isl_id_free (id);
   isl_set *dom = isl_set_universe (isl_space_copy (space));
   isl_aff *aff = isl_aff_zero_on_domain (isl_local_space_from_space (space));
   aff = isl_aff_add_coefficient_si (aff, isl_dim_param, dimension, 1);
   return isl_pw_aff_alloc (dom, aff);
 }
 
+/* Convert WI to a isl_val with CTX.  */
+
+static __isl_give isl_val *
+isl_val_int_from_wi (isl_ctx *ctx, const widest_int &wi)
+{
+  if (wi::neg_p (wi, SIGNED))
+    {
+      widest_int mwi = -wi;
+      return isl_val_neg (isl_val_int_from_chunks (ctx, mwi.get_len (),
+                                                  sizeof (HOST_WIDE_INT),
+                                                  mwi.get_val ()));
+    }
+  return isl_val_int_from_chunks (ctx, wi.get_len (), sizeof (HOST_WIDE_INT),
+                                 wi.get_val ());
+}
+
 /* Extract an affine expression from the gmp constant G.  */
 
 static isl_pw_aff *
-extract_affine_gmp (mpz_t g, __isl_take isl_space *space)
+extract_affine_wi (const widest_int &g, __isl_take isl_space *space)
 {
   isl_local_space *ls = isl_local_space_from_space (isl_space_copy (space));
   isl_aff *aff = isl_aff_zero_on_domain (ls);
   isl_set *dom = isl_set_universe (space);
   isl_ctx *ct = isl_aff_get_ctx (aff);
-  isl_val *v = isl_val_int_from_gmp (ct, g);
+  isl_val *v = isl_val_int_from_wi (ct, g);
   aff = isl_aff_add_constant_val (aff, v);
 
   return isl_pw_aff_alloc (dom, aff);
@@ -321,13 +177,7 @@ extract_affine_gmp (mpz_t g, __isl_take isl_space *space)
 static isl_pw_aff *
 extract_affine_int (tree e, __isl_take isl_space *space)
 {
-  mpz_t g;
-
-  mpz_init (g);
-  tree_int_to_gmp (e, g);
-  isl_pw_aff *res = extract_affine_gmp (g, space);
-  mpz_clear (g);
-
+  isl_pw_aff *res = extract_affine_wi (wi::to_widest (e), space);
   return res;
 }
 
@@ -349,17 +199,13 @@ wrap (isl_pw_aff *pwaff, unsigned width)
    Otherwise returns -1.  */
 
 static inline int
-parameter_index_in_region_1 (tree name, sese_info_p region)
+parameter_index_in_region (tree name, sese_info_p region)
 {
   int i;
   tree p;
-
-  gcc_assert (TREE_CODE (name) == SSA_NAME);
-
   FOR_EACH_VEC_ELT (region->params, i, p)
     if (p == name)
       return i;
-
   return -1;
 }
 
@@ -375,6 +221,7 @@ extract_affine (scop_p s, tree e, __isl_take isl_space *space)
     return NULL;
   }
 
+  tree type = TREE_TYPE (e);
   switch (TREE_CODE (e))
     {
     case POLYNOMIAL_CHREC:
@@ -385,8 +232,22 @@ extract_affine (scop_p s, tree e, __isl_take isl_space *space)
       res = extract_affine_mul (s, e, space);
       break;
 
-    case PLUS_EXPR:
     case POINTER_PLUS_EXPR:
+      {
+       lhs = extract_affine (s, TREE_OPERAND (e, 0), isl_space_copy (space));
+       /* The RHS of a pointer-plus expression is to be interpreted
+          as signed value.  Try to look through a sign-changing conversion
+          first.  */
+       tree tem = TREE_OPERAND (e, 1);
+       STRIP_NOPS (tem);
+       rhs = extract_affine (s, tem, space);
+       if (TYPE_UNSIGNED (TREE_TYPE (tem)))
+         rhs = wrap (rhs, TYPE_PRECISION (type) - 1);
+       res = isl_pw_aff_add (lhs, rhs);
+       break;
+      }
+
+    case PLUS_EXPR:
       lhs = extract_affine (s, TREE_OPERAND (e, 0), isl_space_copy (space));
       rhs = extract_affine (s, TREE_OPERAND (e, 1), space);
       res = isl_pw_aff_add (lhs, rhs);
@@ -398,18 +259,27 @@ extract_affine (scop_p s, tree e, __isl_take isl_space *space)
       res = isl_pw_aff_sub (lhs, rhs);
       break;
 
-    case NEGATE_EXPR:
     case BIT_NOT_EXPR:
+      lhs = extract_affine (s, integer_minus_one_node, isl_space_copy (space));
+      rhs = extract_affine (s, TREE_OPERAND (e, 0), space);
+      res = isl_pw_aff_sub (lhs, rhs);
+      /* We need to always wrap the result of a bitwise operation.  */
+      return wrap (res, TYPE_PRECISION (type) - (TYPE_UNSIGNED (type) ? 0 : 1));
+
+    case NEGATE_EXPR:
       lhs = extract_affine (s, TREE_OPERAND (e, 0), isl_space_copy (space));
       rhs = extract_affine (s, integer_minus_one_node, space);
       res = isl_pw_aff_mul (lhs, rhs);
       break;
 
     case SSA_NAME:
-      gcc_assert (-1 != parameter_index_in_region_1 (e, s->scop_info)
-                 || !invariant_in_sese_p_rec (e, s->scop_info->region, NULL));
-      res = extract_affine_name (s, e, space);
-      break;
+      {
+       gcc_assert (! defined_in_sese_p (e, s->scop_info->region));
+       int dim = parameter_index_in_region (e, s->scop_info);
+       gcc_assert (dim != -1);
+       /* No need to wrap a parameter.  */
+       return extract_affine_name (dim, space);
+      }
 
     case INTEGER_CST:
       res = extract_affine_int (e, space);
@@ -417,6 +287,23 @@ extract_affine (scop_p s, tree e, __isl_take isl_space *space)
       return res;
 
     CASE_CONVERT:
+      {
+       tree itype = TREE_TYPE (TREE_OPERAND (e, 0));
+       res = extract_affine (s, TREE_OPERAND (e, 0), space);
+       /* Signed values, even if overflow is undefined, get modulo-reduced.
+          But only if not all values of the old type fit in the new.  */
+       if (! TYPE_UNSIGNED (type)
+           && ((TYPE_UNSIGNED (itype)
+                && TYPE_PRECISION (type) <= TYPE_PRECISION (itype))
+               || TYPE_PRECISION (type) < TYPE_PRECISION (itype)))
+         res = wrap (res, TYPE_PRECISION (type) - 1);
+       else if (TYPE_UNSIGNED (type)
+                && (!TYPE_UNSIGNED (itype)
+                    || TYPE_PRECISION (type) < TYPE_PRECISION (itype)))
+         res = wrap (res, TYPE_PRECISION (type));
+       return res;
+      }
+
     case NON_LVALUE_EXPR:
       res = extract_affine (s, TREE_OPERAND (e, 0), space);
       break;
@@ -426,8 +313,8 @@ extract_affine (scop_p s, tree e, __isl_take isl_space *space)
       break;
     }
 
-  tree type = TREE_TYPE (e);
-  if (TYPE_UNSIGNED (type))
+  /* For all wrapping arithmetic wrap the result.  */
+  if (TYPE_OVERFLOW_WRAPS (type))
     res = wrap (res, TYPE_PRECISION (type));
 
   return res;
@@ -436,11 +323,11 @@ extract_affine (scop_p s, tree e, __isl_take isl_space *space)
 /* Returns a linear expression for tree T evaluated in PBB.  */
 
 static isl_pw_aff *
-create_pw_aff_from_tree (poly_bb_p pbb, tree t)
+create_pw_aff_from_tree (poly_bb_p pbb, loop_p loop, tree t)
 {
   scop_p scop = PBB_SCOP (pbb);
 
-  t = scalar_evolution_in_region (scop->scop_info->region, pbb_loop (pbb), t);
+  t = cached_scalar_evolution_in_region (scop->scop_info->region, loop, t);
 
   gcc_assert (!chrec_contains_undetermined (t));
   gcc_assert (!automatically_generated_chrec_p (t));
@@ -455,8 +342,9 @@ create_pw_aff_from_tree (poly_bb_p pbb, tree t)
 static void
 add_condition_to_pbb (poly_bb_p pbb, gcond *stmt, enum tree_code code)
 {
-  isl_pw_aff *lhs = create_pw_aff_from_tree (pbb, gimple_cond_lhs (stmt));
-  isl_pw_aff *rhs = create_pw_aff_from_tree (pbb, gimple_cond_rhs (stmt));
+  loop_p loop = gimple_bb (stmt)->loop_father;
+  isl_pw_aff *lhs = create_pw_aff_from_tree (pbb, loop, gimple_cond_lhs (stmt));
+  isl_pw_aff *rhs = create_pw_aff_from_tree (pbb, loop, gimple_cond_rhs (stmt));
 
   isl_set *cond;
   switch (code)
@@ -536,62 +424,40 @@ add_conditions_to_domain (poly_bb_p pbb)
    of P.  */
 
 static void
-add_param_constraints (scop_p scop, graphite_dim_t p)
+add_param_constraints (scop_p scop, graphite_dim_t p, tree parameter)
 {
-  tree parameter = scop->scop_info->params[p];
   tree type = TREE_TYPE (parameter);
-  tree lb = NULL_TREE;
-  tree ub = NULL_TREE;
+  wide_int min, max;
 
-  if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
-    lb = lower_bound_in_type (type, type);
-  else
-    lb = TYPE_MIN_VALUE (type);
+  gcc_assert (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type));
 
-  if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
-    ub = upper_bound_in_type (type, type);
+  if (INTEGRAL_TYPE_P (type)
+      && get_range_info (parameter, &min, &max) == VR_RANGE)
+    ;
   else
-    ub = TYPE_MAX_VALUE (type);
-
-  if (lb)
     {
-      isl_space *space = isl_set_get_space (scop->param_context);
-      isl_constraint *c;
-      mpz_t g;
-      isl_val *v;
-
-      c = isl_inequality_alloc (isl_local_space_from_space (space));
-      mpz_init (g);
-      tree_int_to_gmp (lb, g);
-      v = isl_val_int_from_gmp (scop->isl_context, g);
-      v = isl_val_neg (v);
-      mpz_clear (g);
-      c = isl_constraint_set_constant_val (c, v);
-      c = isl_constraint_set_coefficient_si (c, isl_dim_param, p, 1);
-
-      scop->param_context = isl_set_coalesce
-       (isl_set_add_constraint (scop->param_context, c));
+      min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
+      max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
     }
 
-  if (ub)
-    {
-      isl_space *space = isl_set_get_space (scop->param_context);
-      isl_constraint *c;
-      mpz_t g;
-      isl_val *v;
-
-      c = isl_inequality_alloc (isl_local_space_from_space (space));
-
-      mpz_init (g);
-      tree_int_to_gmp (ub, g);
-      v = isl_val_int_from_gmp (scop->isl_context, g);
-      mpz_clear (g);
-      c = isl_constraint_set_constant_val (c, v);
-      c = isl_constraint_set_coefficient_si (c, isl_dim_param, p, -1);
-
-      scop->param_context = isl_set_coalesce
-       (isl_set_add_constraint (scop->param_context, c));
-    }
+  isl_space *space = isl_set_get_space (scop->param_context);
+  isl_constraint *c = isl_inequality_alloc (isl_local_space_from_space (space));
+  isl_val *v = isl_val_int_from_wi (scop->isl_context,
+                                   widest_int::from (min, TYPE_SIGN (type)));
+  v = isl_val_neg (v);
+  c = isl_constraint_set_constant_val (c, v);
+  c = isl_constraint_set_coefficient_si (c, isl_dim_param, p, 1);
+  scop->param_context = isl_set_coalesce
+      (isl_set_add_constraint (scop->param_context, c));
+
+  space = isl_set_get_space (scop->param_context);
+  c = isl_inequality_alloc (isl_local_space_from_space (space));
+  v = isl_val_int_from_wi (scop->isl_context,
+                          widest_int::from (max, TYPE_SIGN (type)));
+  c = isl_constraint_set_constant_val (c, v);
+  c = isl_constraint_set_coefficient_si (c, isl_dim_param, p, -1);
+  scop->param_context = isl_set_coalesce
+      (isl_set_add_constraint (scop->param_context, c));
 }
 
 /* Add a constrain to the ACCESSES polyhedron for the alias set of
@@ -611,25 +477,6 @@ pdr_add_alias_set (isl_map *acc, dr_info &dri)
   return isl_map_add_constraint (acc, c);
 }
 
-/* Add a constrain to the ACCESSES polyhedron for the alias set of
-   data reference DR.  ACCESSP_NB_DIMS is the dimension of the
-   ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
-   domain.  */
-
-static isl_map *
-add_scalar_version_numbers (isl_map *acc, tree var)
-{
-  isl_constraint *c = isl_equality_alloc
-      (isl_local_space_from_space (isl_map_get_space (acc)));
-  int max_arrays = PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP);
-  /* Each scalar variables has a unique alias set number starting from
-     max_arrays.  */
-  c = isl_constraint_set_constant_si (c, -max_arrays - SSA_NAME_VERSION (var));
-  c = isl_constraint_set_coefficient_si (c, isl_dim_out, 0, 1);
-
-  return isl_map_add_constraint (acc, c);
-}
-
 /* Assign the affine expression INDEX to the output dimension POS of
    MAP and return the result.  */
 
@@ -804,13 +651,21 @@ static void
 build_poly_sr_1 (poly_bb_p pbb, gimple *stmt, tree var, enum poly_dr_type kind,
                 isl_map *acc, isl_set *subscript_sizes)
 {
-  int max_arrays = PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP);
+  scop_p scop = PBB_SCOP (pbb);
   /* Each scalar variables has a unique alias set number starting from
-     max_arrays.  */
+     the maximum alias set assigned to a dr.  */
+  int alias_set = scop->max_alias_set + SSA_NAME_VERSION (var);
   subscript_sizes = isl_set_fix_si (subscript_sizes, isl_dim_set, 0,
-                                   max_arrays + SSA_NAME_VERSION (var));
+                                   alias_set);
 
-  new_poly_dr (pbb, stmt, kind, add_scalar_version_numbers (acc, var),
+  /* Add a constrain to the ACCESSES polyhedron for the alias set of
+     data reference DR.  */
+  isl_constraint *c
+    = isl_equality_alloc (isl_local_space_from_space (isl_map_get_space (acc)));
+  c = isl_constraint_set_constant_si (c, -alias_set);
+  c = isl_constraint_set_coefficient_si (c, isl_dim_out, 0, 1);
+
+  new_poly_dr (pbb, stmt, kind, isl_map_add_constraint (acc, c),
               subscript_sizes);
 }
 
@@ -919,17 +774,14 @@ add_loop_constraints (scop_p scop, __isl_take isl_set *domain, loop_p loop,
       isl_local_space *ls = isl_local_space_from_space (space);
       isl_constraint *c = isl_inequality_alloc (ls);
       c = isl_constraint_set_coefficient_si (c, isl_dim_set, loop_index, -1);
-      mpz_t g;
-      mpz_init (g);
-      tree_int_to_gmp (nb_iters, g);
-      isl_val *v = isl_val_int_from_gmp (scop->isl_context, g);
-      mpz_clear (g);
+      isl_val *v
+       = isl_val_int_from_wi (scop->isl_context, wi::to_widest (nb_iters));
       c = isl_constraint_set_constant_val (c, v);
       return isl_set_add_constraint (domain, c);
     }
   /* loop_i <= expr_nb_iters */
   gcc_assert (!chrec_contains_undetermined (nb_iters));
-  nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
+  nb_iters = cached_scalar_evolution_in_region (region, loop, nb_iters);
   gcc_assert (!chrec_contains_undetermined (nb_iters));
 
   isl_pw_aff *aff_nb_iters = extract_affine (scop, nb_iters,
@@ -963,12 +815,9 @@ add_loop_constraints (scop_p scop, __isl_take isl_set *domain, loop_p loop,
 
   /* NIT is an upper bound to NB_ITERS: "NIT >= NB_ITERS", although we
      do not know whether the loop executes at least once.  */
-  mpz_t g;
-  mpz_init (g);
-  wi::to_mpz (nit, g, SIGNED);
-  mpz_sub_ui (g, g, 1);
+  --nit;
 
-  isl_pw_aff *approx = extract_affine_gmp (g, isl_space_copy (space));
+  isl_pw_aff *approx = extract_affine_wi (nit, isl_space_copy (space));
   isl_set *x = isl_pw_aff_ge_set (approx, aff_nb_iters);
   x = isl_set_project_out (x, isl_dim_set, 0,
                           isl_set_dim (x, isl_dim_set));
@@ -977,8 +826,7 @@ add_loop_constraints (scop_p scop, __isl_take isl_set *domain, loop_p loop,
   ls = isl_local_space_from_space (space);
   c = isl_inequality_alloc (ls);
   c = isl_constraint_set_coefficient_si (c, isl_dim_set, loop_index, -1);
-  isl_val *v = isl_val_int_from_gmp (scop->isl_context, g);
-  mpz_clear (g);
+  isl_val *v = isl_val_int_from_wi (scop->isl_context, nit);
   c = isl_constraint_set_constant_val (c, v);
 
   if (dump_file)
@@ -1008,9 +856,7 @@ build_iteration_domains (scop_p scop, __isl_keep isl_set *context,
       loop_p loop = pbb_loop (pbb);
       if (current == loop)
        {
-#ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
          pbb->iterators = isl_set_copy (domain);
-#endif
          pbb->domain = isl_set_copy (domain);
          pbb->domain = isl_set_set_tuple_id (pbb->domain,
                                              isl_id_for_pbb (scop, pbb));
@@ -1063,13 +909,10 @@ build_scop_context (scop_p scop)
 
   scop->param_context = isl_set_universe (space);
 
-  graphite_dim_t p;
-  for (p = 0; p < nbp; p++)
-    add_param_constraints (scop, p);
+  FOR_EACH_VEC_ELT (region->params, i, e)
+    add_param_constraints (scop, i, e);
 }
 
-#ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
-
 /* Return true when loop A is nested in loop B.  */
 
 static bool
@@ -1199,6 +1042,14 @@ add_loop_schedule (__isl_take isl_schedule *schedule, loop_p loop,
   if (empty < 0 || empty)
     return empty < 0 ? isl_schedule_free (schedule) : schedule;
 
+  isl_union_set *domain = isl_schedule_get_domain (schedule);
+  /* We cannot apply an empty domain to pbbs in this loop so return early.  */
+  if (isl_union_set_is_empty (domain))
+    {
+      isl_union_set_free (domain);
+      return schedule;
+    }
+
   isl_space *space = isl_set_get_space (iterators);
   int loop_index = isl_space_dim (space, isl_dim_set) - 1;
 
@@ -1219,7 +1070,6 @@ add_loop_schedule (__isl_take isl_schedule *schedule, loop_p loop,
   prefix = isl_multi_aff_set_tuple_id (prefix, isl_dim_out, label);
 
   int n = isl_multi_aff_dim (prefix, isl_dim_in);
-  isl_union_set *domain = isl_schedule_get_domain (schedule);
   isl_multi_union_pw_aff *mupa = outer_projection_mupa (domain, n);
   mupa = isl_multi_union_pw_aff_apply_multi_aff (mupa, prefix);
   return isl_schedule_insert_partial_schedule (schedule, mupa);
@@ -1322,7 +1172,7 @@ build_schedule_loop_nest (scop_p scop, int *index, loop_p context_loop)
 
 /* Build the schedule of the SCOP.  */
 
-static bool
+static void
 build_original_schedule (scop_p scop)
 {
   int i = 0;
@@ -1344,18 +1194,16 @@ build_original_schedule (scop_p scop)
       fprintf (dump_file, "[sese-to-poly] original schedule:\n");
       print_isl_schedule (dump_file, scop->original_schedule);
     }
-  if (!scop->original_schedule)
-    return false;
-  return true;
 }
 
-#endif
-
 /* Builds the polyhedral representation for a SESE region.  */
 
 bool
 build_poly_scop (scop_p scop)
 {
+  int old_err = isl_options_get_on_error (scop->isl_context);
+  isl_options_set_on_error (scop->isl_context, ISL_ON_ERROR_CONTINUE);
+
   build_scop_context (scop);
 
   unsigned i = 0;
@@ -1364,11 +1212,16 @@ build_poly_scop (scop_p scop)
     i = build_iteration_domains (scop, scop->param_context, i, NULL);
 
   build_scop_drs (scop);
-#ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
   build_original_schedule (scop);
-#else
-  build_scop_scattering (scop);
-#endif
-  return true;
+
+  enum isl_error err = isl_ctx_last_error (scop->isl_context);
+  isl_ctx_reset_error (scop->isl_context);
+  isl_options_set_on_error (scop->isl_context, old_err);
+  if (err != isl_error_none
+      && dump_enabled_p ())
+    dump_printf (MSG_MISSED_OPTIMIZATION,
+                "ISL error while building poly scop\n");
+
+  return err == isl_error_none;
 }
 #endif  /* HAVE_isl */