privately export isl_basic_map_add_div_constraints
[platform/upstream/isl.git] / isl_map.c
index 49291c6..d0009de 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
@@ -1,3 +1,12 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include <string.h>
 #include <strings.h>
 #include "isl_ctx.h"
@@ -12,6 +21,7 @@
 #include "isl_map_private.h"
 #include "isl_map_piplib.h"
 #include "isl_sample.h"
+#include "isl_tab.h"
 #include "isl_vec.h"
 
 /* Maps dst positions to src positions */
@@ -94,7 +104,6 @@ static void isl_dim_map_dump(struct isl_dim_map *dim_map)
 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
                                enum isl_dim_type type)
 {
-       struct isl_dim *dim = bmap->dim;
        switch (type) {
        case isl_dim_param:
        case isl_dim_in:
@@ -640,6 +649,56 @@ int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
        return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
 }
 
+__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
+       isl_int *eq)
+{
+       int k;
+
+       bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
+       if (!bmap)
+               return NULL;
+       k = isl_basic_map_alloc_equality(bmap);
+       if (k < 0)
+               goto error;
+       isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+__isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
+       isl_int *eq)
+{
+       return (isl_basic_set *)
+               isl_basic_map_add_eq((isl_basic_map *)bset, eq);
+}
+
+__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
+       isl_int *ineq)
+{
+       int k;
+
+       bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
+       if (!bmap)
+               return NULL;
+       k = isl_basic_map_alloc_inequality(bmap);
+       if (k < 0)
+               goto error;
+       isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+__isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
+       isl_int *ineq)
+{
+       return (isl_basic_set *)
+               isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
+}
+
 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
 {
        if (!bmap)
@@ -871,6 +930,8 @@ struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
        if (!ext)
                goto error;
 
+       if (dims_ok)
+               ext->sample = isl_vec_copy(base->sample);
        flags = base->flags;
        ext = add_constraints(ext, base, 0, 0);
        if (ext) {
@@ -1028,8 +1089,7 @@ struct isl_basic_set *isl_basic_set_swap_vars(
        isl_blk_free(bset->ctx, blk);
 
        ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
-       return bset;
-
+       return isl_basic_set_gauss(bset, NULL);
 error:
        isl_basic_set_free(bset);
        return NULL;
@@ -1072,6 +1132,8 @@ struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
        isl_int_set_si(bmap->eq[i][0], 1);
        isl_seq_clr(bmap->eq[i]+1, total);
        ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
+       isl_vec_free(bmap->sample);
+       bmap->sample = NULL;
        return isl_basic_map_finalize(bmap);
 error:
        isl_basic_map_free(bmap);
@@ -1190,6 +1252,10 @@ struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
                return bmap;
        bmap = isl_basic_map_eliminate_vars(bmap,
                        isl_basic_map_offset(bmap, type) - 1 + first, n);
+       if (!bmap)
+               return bmap;
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
+               return bmap;
        bmap = isl_basic_map_drop(bmap, type, first, n);
        return bmap;
 error:
@@ -1197,11 +1263,17 @@ error:
        return NULL;
 }
 
+__isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return (isl_basic_set *)
+               isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
+}
+
 struct isl_map *isl_map_remove(struct isl_map *map,
        enum isl_dim_type type, unsigned first, unsigned n)
 {
        int i;
-       unsigned nparam;
 
        if (n == 0)
                return map;
@@ -1224,6 +1296,12 @@ error:
        return NULL;
 }
 
+__isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
+}
+
 /* Project out n inputs starting at first using Fourier-Motzkin */
 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
        unsigned first, unsigned n)
@@ -1410,7 +1488,7 @@ struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
        isl_assert(dim->ctx, n >= 0, return NULL);
        set = isl_alloc(dim->ctx, struct isl_set,
                        sizeof(struct isl_set) +
-                       n * sizeof(struct isl_basic_set *));
+                       (n - 1) * sizeof(struct isl_basic_set *));
        if (!set)
                goto error;
 
@@ -1651,7 +1729,7 @@ error:
        return NULL;
 }
 
-static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
+int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
 {
        int i;
        unsigned total;
@@ -1686,7 +1764,7 @@ static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
 
 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
 {
-       return basic_map_contains((struct isl_basic_map *)bset, vec);
+       return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
 }
 
 struct isl_basic_map *isl_basic_map_intersect(
@@ -1711,12 +1789,12 @@ struct isl_basic_map *isl_basic_map_intersect(
                            isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
 
        if (bmap1->sample &&
-           basic_map_contains(bmap1, bmap1->sample) > 0 &&
-           basic_map_contains(bmap2, bmap1->sample) > 0)
+           isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
+           isl_basic_map_contains(bmap2, bmap1->sample) > 0)
                sample = isl_vec_copy(bmap1->sample);
        else if (bmap2->sample &&
-           basic_map_contains(bmap1, bmap2->sample) > 0 &&
-           basic_map_contains(bmap2, bmap2->sample) > 0)
+           isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
+           isl_basic_map_contains(bmap2, bmap2->sample) > 0)
                sample = isl_vec_copy(bmap2->sample);
 
        bmap1 = isl_basic_map_cow(bmap1);
@@ -1750,6 +1828,52 @@ struct isl_basic_set *isl_basic_set_intersect(
                        (struct isl_basic_map *)bset2);
 }
 
+/* Special case of isl_map_intersect, where both map1 and map2
+ * are convex, without any divs and such that either map1 or map2
+ * contains a single constraint.  This constraint is then simply
+ * added to the other map.
+ */
+static __isl_give isl_map *map_intersect_add_constraint(
+       __isl_take isl_map *map1, __isl_take isl_map *map2)
+{
+       struct isl_basic_map *bmap1;
+       struct isl_basic_map *bmap2;
+
+       isl_assert(map1->ctx, map1->n == 1, goto error);
+       isl_assert(map2->ctx, map1->n == 1, goto error);
+       isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
+       isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
+
+       if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
+               return isl_map_intersect(map2, map1);
+
+       isl_assert(map2->ctx,
+                   map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
+
+       map1 = isl_map_cow(map1);
+       if (!map1)
+               goto error;
+       map1->p[0] = isl_basic_map_cow(map1->p[0]);
+       if (map2->p[0]->n_eq == 1)
+               map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
+       else
+               map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
+                                                       map2->p[0]->ineq[0]);
+
+       map1->p[0] = isl_basic_map_simplify(map1->p[0]);
+       map1->p[0] = isl_basic_map_finalize(map1->p[0]);
+       if (!map1->p[0])
+               goto error;
+
+       isl_map_free(map2);
+
+       return map1;
+error:
+       isl_map_free(map1);
+       isl_map_free(map2);
+       return NULL;
+}
+
 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
 {
        unsigned flags = 0;
@@ -1759,6 +1883,12 @@ struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
        if (!map1 || !map2)
                goto error;
 
+       if (map1->n == 1 && map2->n == 1 &&
+           map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
+           isl_dim_equal(map1->dim, map2->dim) &&
+           (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
+            map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
+               return map_intersect_add_constraint(map1, map2);
        isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
                                         map2->dim, isl_dim_param), goto error);
        if (isl_dim_total(map1->dim) ==
@@ -1827,8 +1957,8 @@ struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
 
 /* Turn final n dimensions into existentially quantified variables.
  */
-struct isl_basic_set *isl_basic_set_project_out(
-               struct isl_basic_set *bset, unsigned n, unsigned flags)
+struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
+               enum isl_dim_type type, unsigned first, unsigned n)
 {
        int i;
        size_t row_size;
@@ -1838,11 +1968,15 @@ struct isl_basic_set *isl_basic_set_project_out(
        if (!bset)
                return NULL;
 
-       isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
+       isl_assert(bset->ctx, type == isl_dim_set, goto error);
+       isl_assert(bset->ctx, first + n == isl_basic_set_n_dim(bset), goto error);
 
        if (n == 0)
                return bset;
 
+       if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
+               return isl_basic_set_remove(bset, type, first, n);
+
        bset = isl_basic_set_cow(bset);
 
        row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
@@ -1870,12 +2004,46 @@ struct isl_basic_set *isl_basic_set_project_out(
        if (!bset->dim)
                goto error;
        bset = isl_basic_set_simplify(bset);
+       bset = isl_basic_set_drop_redundant_divs(bset);
        return isl_basic_set_finalize(bset);
 error:
        isl_basic_set_free(bset);
        return NULL;
 }
 
+/* Turn final n dimensions into existentially quantified variables.
+ */
+__isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
+               enum isl_dim_type type, unsigned first, unsigned n)
+{
+       int i;
+
+       if (!set)
+               return NULL;
+
+       isl_assert(set->ctx, type == isl_dim_set, goto error);
+       isl_assert(set->ctx, first + n == isl_set_n_dim(set), goto error);
+
+       if (n == 0)
+               return set;
+
+       set = isl_set_cow(set);
+       if (!set)
+               goto error;
+       set->dim = isl_dim_drop_outputs(set->dim, first, n);
+       for (i = 0; i < set->n; ++i) {
+               set->p[i] = isl_basic_set_project_out(set->p[i], type, first, n);
+               if (!set->p[i])
+                       goto error;
+       }
+
+       ISL_F_CLR(set, ISL_SET_NORMALIZED);
+       return set;
+error:
+       isl_set_free(set);
+       return NULL;
+}
+
 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
 {
        int i, j;
@@ -1931,6 +2099,7 @@ struct isl_basic_map *isl_basic_map_apply_range(
        bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
        bmap = add_divs(bmap, n);
        bmap = isl_basic_map_simplify(bmap);
+       bmap = isl_basic_map_drop_redundant_divs(bmap);
        return isl_basic_map_finalize(bmap);
 error:
        isl_basic_map_free(bmap1);
@@ -1944,7 +2113,7 @@ struct isl_basic_set *isl_basic_set_apply(
        if (!bset || !bmap)
                goto error;
 
-       isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
+       isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
                    goto error);
 
        return (struct isl_basic_set *)
@@ -1961,9 +2130,9 @@ struct isl_basic_map *isl_basic_map_apply_domain(
        if (!bmap1 || !bmap2)
                goto error;
 
-       isl_assert(ctx,
+       isl_assert(bmap1->ctx,
            isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
-       isl_assert(ctx,
+       isl_assert(bmap1->ctx,
            isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
            goto error);
 
@@ -2035,6 +2204,45 @@ error:
        return NULL;
 }
 
+/* Given two maps A -> f(A) and B -> g(B), construct a map
+ * A \cap B -> f(A) + f(B)
+ */
+struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
+{
+       struct isl_map *result;
+       int i, j;
+
+       if (!map1 || !map2)
+               goto error;
+
+       isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
+
+       result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
+                               map1->n * map2->n, 0);
+       if (!result)
+               goto error;
+       for (i = 0; i < map1->n; ++i)
+               for (j = 0; j < map2->n; ++j) {
+                       struct isl_basic_map *part;
+                       part = isl_basic_map_sum(
+                                   isl_basic_map_copy(map1->p[i]),
+                                   isl_basic_map_copy(map2->p[j]));
+                       if (isl_basic_map_is_empty(part))
+                               isl_basic_map_free(part);
+                       else
+                               result = isl_map_add(result, part);
+                       if (!result)
+                               goto error;
+               }
+       isl_map_free(map1);
+       isl_map_free(map2);
+       return result;
+error:
+       isl_map_free(map1);
+       isl_map_free(map2);
+       return NULL;
+}
+
 /* Given a basic map A -> f(A), construct A -> -f(A).
  */
 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
@@ -2060,6 +2268,28 @@ struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
        return isl_basic_map_finalize(bmap);
 }
 
+/* Given a map A -> f(A), construct A -> -f(A).
+ */
+struct isl_map *isl_map_neg(struct isl_map *map)
+{
+       int i;
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_neg(map->p[i]);
+               if (!map->p[i])
+                       goto error;
+       }
+
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
 /* Given a basic map A -> f(A) and an integer d, construct a basic map
  * A -> floor(f(A)/d).
  */
@@ -2114,6 +2344,31 @@ error:
        return NULL;
 }
 
+/* Given a map A -> f(A) and an integer d, construct a map
+ * A -> floor(f(A)/d).
+ */
+struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
+{
+       int i;
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       ISL_F_CLR(map, ISL_MAP_DISJOINT);
+       ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_floordiv(map->p[i], d);
+               if (!map->p[i])
+                       goto error;
+       }
+
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
 {
        int i;
@@ -2134,7 +2389,9 @@ error:
        return NULL;
 }
 
-static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
+/* Add a constraints to "bmap" expressing i_pos < o_pos
+ */
+static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
 {
        int i;
        unsigned nparam;
@@ -2155,7 +2412,9 @@ error:
        return NULL;
 }
 
-static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
+/* Add a constraints to "bmap" expressing i_pos > o_pos
+ */
+static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
 {
        int i;
        unsigned nparam;
@@ -2188,6 +2447,8 @@ struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
        return isl_basic_map_finalize(bmap);
 }
 
+/* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
+ */
 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
 {
        int i;
@@ -2202,6 +2463,8 @@ struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
        return isl_basic_map_finalize(bmap);
 }
 
+/* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
+ */
 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
 {
        int i;
@@ -2216,6 +2479,70 @@ struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
        return isl_basic_map_finalize(bmap);
 }
 
+static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
+{
+       struct isl_map *map;
+       unsigned dim;
+       int i;
+
+       if (!dims)
+               return NULL;
+       dim = dims->n_out;
+       map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
+
+       for (i = 0; i < dim; ++i)
+               map = isl_map_add(map,
+                                 isl_basic_map_less_at(isl_dim_copy(dims), i));
+       if (equal)
+               map = isl_map_add(map,
+                                 isl_basic_map_equal(isl_dim_copy(dims), dim));
+
+       isl_dim_free(dims);
+       return map;
+}
+
+__isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
+{
+       return map_lex_lte(isl_dim_map(set_dim), 0);
+}
+
+__isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
+{
+       return map_lex_lte(isl_dim_map(set_dim), 1);
+}
+
+static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
+{
+       struct isl_map *map;
+       unsigned dim;
+       int i;
+
+       if (!dims)
+               return NULL;
+       dim = dims->n_out;
+       map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
+
+       for (i = 0; i < dim; ++i)
+               map = isl_map_add(map,
+                                 isl_basic_map_more_at(isl_dim_copy(dims), i));
+       if (equal)
+               map = isl_map_add(map,
+                                 isl_basic_map_equal(isl_dim_copy(dims), dim));
+
+       isl_dim_free(dims);
+       return map;
+}
+
+__isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
+{
+       return map_lex_gte(isl_dim_map(set_dim), 0);
+}
+
+__isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
+{
+       return map_lex_gte(isl_dim_map(set_dim), 1);
+}
+
 struct isl_basic_map *isl_basic_map_from_basic_set(
                struct isl_basic_set *bset, struct isl_dim *dim)
 {
@@ -2266,7 +2593,7 @@ error:
  *
  *             f - m d >= n
  */
-static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
+int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
 {
        int i, j;
        unsigned total = isl_basic_map_total_dim(bmap);
@@ -2308,6 +2635,12 @@ error:
        return NULL;
 }
 
+__isl_give isl_basic_set *isl_basic_set_underlying_set(
+               __isl_take isl_basic_set *bset)
+{
+       return isl_basic_map_underlying_set((isl_basic_map *)bset);
+}
+
 struct isl_basic_map *isl_basic_map_overlying_set(
        struct isl_basic_set *bset, struct isl_basic_map *like)
 {
@@ -2362,7 +2695,7 @@ struct isl_basic_map *isl_basic_map_overlying_set(
                for (i = 0; i < like->n_div; ++i) {
                        if (isl_int_is_zero(bmap->div[i][0]))
                                continue;
-                       if (add_div_constraints(bmap, i) < 0)
+                       if (isl_basic_map_add_div_constraints(bmap, i) < 0)
                                goto error;
                }
        }
@@ -2459,12 +2792,14 @@ struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
 {
        struct isl_basic_set *domain;
+       unsigned n_in;
        unsigned n_out;
        if (!bmap)
                return NULL;
+       n_in = isl_basic_map_n_in(bmap);
        n_out = isl_basic_map_n_out(bmap);
        domain = isl_basic_set_from_basic_map(bmap);
-       return isl_basic_set_project_out(domain, n_out, 0);
+       return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
 }
 
 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
@@ -2568,7 +2903,7 @@ struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
        isl_assert(dim->ctx, n >= 0, return NULL);
        map = isl_alloc(dim->ctx, struct isl_map,
                        sizeof(struct isl_map) +
-                       n * sizeof(struct isl_basic_map *));
+                       (n - 1) * sizeof(struct isl_basic_map *));
        if (!map)
                goto error;
 
@@ -2600,11 +2935,10 @@ struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
        return map;
 }
 
-struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
-               unsigned nparam, unsigned in, unsigned out)
+struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
 {
        struct isl_basic_map *bmap;
-       bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
+       bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
        bmap = isl_basic_map_set_to_empty(bmap);
        return bmap;
 }
@@ -2661,6 +2995,14 @@ struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
        return bset;
 }
 
+__isl_give isl_basic_map *isl_basic_map_universe_like(
+               __isl_keep isl_basic_map *model)
+{
+       if (!model)
+               return NULL;
+       return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
+}
+
 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
 {
        if (!model)
@@ -2668,6 +3010,14 @@ struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
        return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
 }
 
+__isl_give isl_basic_set *isl_basic_set_universe_like_set(
+       __isl_keep isl_set *model)
+{
+       if (!model)
+               return NULL;
+       return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
+}
+
 struct isl_map *isl_map_empty(struct isl_dim *dim)
 {
        return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
@@ -2719,6 +3069,13 @@ struct isl_set *isl_set_universe(struct isl_dim *dim)
        return set;
 }
 
+__isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
+{
+       if (!model)
+               return NULL;
+       return isl_set_universe(isl_dim_copy(model->dim));
+}
+
 struct isl_map *isl_map_dup(struct isl_map *map)
 {
        int i;
@@ -2802,8 +3159,8 @@ struct isl_set *isl_set_extend(struct isl_set *base,
                                                        nparam, 0, dim);
 }
 
-static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
-               unsigned pos, int value)
+static struct isl_basic_map *isl_basic_map_fix_pos_si(
+       struct isl_basic_map *bmap, unsigned pos, int value)
 {
        int j;
 
@@ -2822,14 +3179,47 @@ error:
        return NULL;
 }
 
-struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
-               enum isl_dim_type type, unsigned pos, int value)
+static __isl_give isl_basic_map *isl_basic_map_fix_pos(
+       __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
 {
-       if (!bmap)
-               return NULL;
+       int j;
+
+       bmap = isl_basic_map_cow(bmap);
+       bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
+       j = isl_basic_map_alloc_equality(bmap);
+       if (j < 0)
+               goto error;
+       isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
+       isl_int_set_si(bmap->eq[j][pos], -1);
+       isl_int_set(bmap->eq[j][0], value);
+       bmap = isl_basic_map_simplify(bmap);
+       return isl_basic_map_finalize(bmap);
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
+               enum isl_dim_type type, unsigned pos, int value)
+{
+       if (!bmap)
+               return NULL;
        isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
-       return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
-                                       value);
+       return isl_basic_map_fix_pos_si(bmap,
+               isl_basic_map_offset(bmap, type) + pos, value);
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
+               enum isl_dim_type type, unsigned pos, isl_int value)
+{
+       if (!bmap)
+               return NULL;
+       isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
+       return isl_basic_map_fix_pos(bmap,
+               isl_basic_map_offset(bmap, type) + pos, value);
 error:
        isl_basic_map_free(bmap);
        return NULL;
@@ -2843,6 +3233,14 @@ struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
                                        type, pos, value);
 }
 
+__isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
+               enum isl_dim_type type, unsigned pos, isl_int value)
+{
+       return (struct isl_basic_set *)
+               isl_basic_map_fix((struct isl_basic_map *)bset,
+                                       type, pos, value);
+}
+
 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
                unsigned input, int value)
 {
@@ -2866,7 +3264,7 @@ struct isl_map *isl_map_fix_si(struct isl_map *map,
        if (!map)
                return NULL;
 
-       isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
+       isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
        for (i = 0; i < map->n; ++i) {
                map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
                if (!map->p[i])
@@ -2879,6 +3277,34 @@ error:
        return NULL;
 }
 
+__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
+               enum isl_dim_type type, unsigned pos, isl_int value)
+{
+       int i;
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
+               if (!map->p[i])
+                       goto error;
+       }
+       ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
+__isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
+               enum isl_dim_type type, unsigned pos, isl_int value)
+{
+       return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
+}
+
 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
                unsigned input, int value)
 {
@@ -2895,7 +3321,6 @@ struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
        unsigned dim, isl_int value)
 {
        int j;
-       unsigned nparam;
 
        bset = isl_basic_set_cow(bset);
        bset = isl_basic_set_extend_constraints(bset, 0, 1);
@@ -2936,7 +3361,6 @@ error:
 struct isl_map *isl_map_reverse(struct isl_map *map)
 {
        int i;
-       unsigned t;
 
        map = isl_map_cow(map);
        if (!map)
@@ -2961,7 +3385,18 @@ static struct isl_map *isl_basic_map_partial_lexopt(
                struct isl_basic_map *bmap, struct isl_basic_set *dom,
                struct isl_set **empty, int max)
 {
-       return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
+       if (!bmap)
+               goto error;
+       if (bmap->ctx->opt->pip == ISL_PIP_PIP)
+               return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
+       else
+               return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
+error:
+       isl_basic_map_free(bmap);
+       isl_basic_set_free(dom);
+       if (empty)
+               *empty = NULL;
+       return NULL;
 }
 
 struct isl_map *isl_basic_map_partial_lexmax(
@@ -2996,29 +3431,41 @@ struct isl_set *isl_basic_set_partial_lexmax(
                        dom, empty);
 }
 
-struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
+__isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
 {
-       struct isl_basic_map *bmap = NULL;
        struct isl_basic_set *dom = NULL;
-       struct isl_map *min;
-       struct isl_dim *param_dim;
+       struct isl_dim *dom_dim;
 
-       if (!bset)
-               goto error;
-       bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
        if (!bmap)
                goto error;
-       param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
-       dom = isl_basic_set_universe(param_dim);
-       if (!dom)
-               goto error;
-       min = isl_basic_map_partial_lexmin(bmap, dom, NULL);
-       return isl_map_range(min);
+       dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
+       dom = isl_basic_set_universe(dom_dim);
+       return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
 error:
        isl_basic_map_free(bmap);
        return NULL;
 }
 
+__isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
+{
+       return isl_basic_map_lexopt(bmap, 0);
+}
+
+__isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
+{
+       return isl_basic_map_lexopt(bmap, 1);
+}
+
+__isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
+{
+       return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
+}
+
+__isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
+{
+       return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
+}
+
 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
        struct isl_dim *dim)
 {
@@ -3275,36 +3722,66 @@ error:
        return NULL;
 }
 
-/* If bmap contains any unknown divs, then compute explicit
- * expressions for them.  However, this computation may be
- * quite expensive, so first try to remove divs that aren't
- * strictly needed.
- */
-struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
+static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
 {
        int i;
        unsigned off;
 
        if (!bmap)
-               return NULL;
+               return -1;
+
        off = isl_dim_total(bmap->dim);
        for (i = 0; i < bmap->n_div; ++i) {
                if (isl_int_is_zero(bmap->div[i][0]))
-                       break;
+                       return 0;
                isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
-                               goto error);
+                               return -1);
        }
-       if (i == bmap->n_div)
+       return 1;
+}
+
+static int map_divs_known(__isl_keep isl_map *map)
+{
+       int i;
+
+       if (!map)
+               return -1;
+
+       for (i = 0; i < map->n; ++i) {
+               int known = basic_map_divs_known(map->p[i]);
+               if (known <= 0)
+                       return known;
+       }
+
+       return 1;
+}
+
+/* If bmap contains any unknown divs, then compute explicit
+ * expressions for them.  However, this computation may be
+ * quite expensive, so first try to remove divs that aren't
+ * strictly needed.
+ */
+struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
+{
+       int i;
+       int known;
+       struct isl_map *map;
+
+       known = basic_map_divs_known(bmap);
+       if (known < 0)
+               goto error;
+       if (known)
                return isl_map_from_basic_map(bmap);
+
        bmap = isl_basic_map_drop_redundant_divs(bmap);
-       if (!bmap)
+
+       known = basic_map_divs_known(bmap);
+       if (known < 0)
                goto error;
-       for (i = 0; i < bmap->n_div; ++i)
-               if (isl_int_is_zero(bmap->div[i][0]))
-                       break;
-       if (i == bmap->n_div)
+       if (known)
                return isl_map_from_basic_map(bmap);
-       struct isl_map *map = compute_divs(bmap);
+
+       map = compute_divs(bmap);
        return map;
 error:
        isl_basic_map_free(bmap);
@@ -3314,12 +3791,22 @@ error:
 struct isl_map *isl_map_compute_divs(struct isl_map *map)
 {
        int i;
+       int known;
        struct isl_map *res;
 
        if (!map)
                return NULL;
        if (map->n == 0)
                return map;
+
+       known = map_divs_known(map);
+       if (known < 0) {
+               isl_map_free(map);
+               return NULL;
+       }
+       if (known)
+               return map;
+
        res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
        for (i = 1 ; i < map->n; ++i) {
                struct isl_map *r2;
@@ -3513,9 +4000,6 @@ struct isl_map *isl_map_apply_range(
        struct isl_dim *dim_result;
        struct isl_map *result;
        int i, j;
-       unsigned nparam;
-       unsigned n_in;
-       unsigned n_out;
 
        if (!map1 || !map2)
                goto error;
@@ -3575,7 +4059,7 @@ struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
                isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
                isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
        }
-       return isl_basic_set_project_out(bset, 2*dim, 0);
+       return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
 error:
        isl_basic_map_free(bmap);
        return NULL;
@@ -3668,7 +4152,16 @@ struct isl_map *isl_map_identity(struct isl_dim *set_dim)
        return map_identity(dim);
 }
 
-struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
+struct isl_map *isl_map_identity_like(struct isl_map *model)
+{
+       if (!model || !model->dim)
+               return NULL;
+       isl_assert(model->ctx,
+                       model->dim->n_in == model->dim->n_out, return NULL);
+       return map_identity(isl_dim_copy(model->dim));
+}
+
+struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
 {
        if (!model || !model->dim)
                return NULL;
@@ -3712,12 +4205,6 @@ int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
        return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
 }
 
-int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
-{
-       return isl_map_is_subset(
-                       (struct isl_map *)set1, (struct isl_map *)set2);
-}
-
 int isl_basic_map_is_subset(
                struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
 {
@@ -3782,34 +4269,14 @@ int isl_map_fast_is_empty(struct isl_map *map)
        return map->n == 0;
 }
 
-int isl_set_is_empty(struct isl_set *set)
+int isl_set_fast_is_empty(struct isl_set *set)
 {
-       return isl_map_is_empty((struct isl_map *)set);
+       return set->n == 0;
 }
 
-int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
+int isl_set_is_empty(struct isl_set *set)
 {
-       int i;
-       int is_subset = 0;
-       struct isl_map *diff;
-
-       if (!map1 || !map2)
-               return -1;
-
-       if (isl_map_is_empty(map1))
-               return 1;
-
-       if (isl_map_is_empty(map2))
-               return 0;
-
-       diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
-       if (!diff)
-               return -1;
-
-       is_subset = isl_map_is_empty(diff);
-       isl_map_free(diff);
-
-       return is_subset;
+       return isl_map_is_empty((struct isl_map *)set);
 }
 
 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
@@ -3841,6 +4308,26 @@ int isl_basic_map_is_strict_subset(
        return !is_subset;
 }
 
+int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
+{
+       int is_subset;
+
+       if (!map1 || !map2)
+               return -1;
+       is_subset = isl_map_is_subset(map1, map2);
+       if (is_subset != 1)
+               return is_subset;
+       is_subset = isl_map_is_subset(map2, map1);
+       if (is_subset == -1)
+               return is_subset;
+       return !is_subset;
+}
+
+int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
+{
+       return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
+}
+
 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
 {
        if (!bmap)
@@ -3855,6 +4342,14 @@ int isl_basic_set_is_universe(struct isl_basic_set *bset)
        return bset->n_eq == 0 && bset->n_ineq == 0;
 }
 
+int isl_map_fast_is_universe(__isl_keep isl_map *map)
+{
+       if (!map)
+               return -1;
+
+       return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
+}
+
 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
 {
        struct isl_basic_set *bset = NULL;
@@ -3878,7 +4373,7 @@ int isl_basic_map_is_empty(struct isl_basic_map *bmap)
 
        total = 1 + isl_basic_map_total_dim(bmap);
        if (bmap->sample && bmap->sample->size == total) {
-               int contains = basic_map_contains(bmap, bmap->sample);
+               int contains = isl_basic_map_contains(bmap, bmap->sample);
                if (contains < 0)
                        return -1;
                if (contains)
@@ -3889,7 +4384,7 @@ int isl_basic_map_is_empty(struct isl_basic_map *bmap)
        bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
        if (!bset)
                return -1;
-       sample = isl_basic_set_sample(bset);
+       sample = isl_basic_set_sample_vec(bset);
        if (!sample)
                return -1;
        empty = sample->size == 0;
@@ -3927,7 +4422,7 @@ struct isl_map *isl_basic_map_union(
        if (!bmap1 || !bmap2)
                return NULL;
 
-       isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
+       isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
 
        map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
        if (!map)
@@ -3950,13 +4445,15 @@ struct isl_set *isl_basic_set_union(
 }
 
 /* Order divs such that any div only depends on previous divs */
-static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
+struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
 {
        int i;
        unsigned off = isl_dim_total(bmap->dim);
 
        for (i = 0; i < bmap->n_div; ++i) {
                int pos;
+               if (isl_int_is_zero(bmap->div[i][0]))
+                       continue;
                pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
                                                            bmap->n_div-i);
                if (pos == -1)
@@ -3967,6 +4464,12 @@ static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
        return bmap;
 }
 
+struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
+{
+       return (struct isl_basic_set *)
+               isl_basic_map_order_divs((struct isl_basic_map *)bset);
+}
+
 /* Look for a div in dst that corresponds to the div "div" in src.
  * The divs before "div" in src and dst are assumed to be the same.
  * 
@@ -4004,7 +4507,7 @@ struct isl_basic_map *isl_basic_map_align_divs(
        for (i = 0; i < src->n_div; ++i)
                isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
 
-       src = order_divs(src);
+       src = isl_basic_map_order_divs(src);
        dst = isl_basic_map_cow(dst);
        dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
                        src->n_div, 0, 2 * src->n_div);
@@ -4018,7 +4521,7 @@ struct isl_basic_map *isl_basic_map_align_divs(
                                goto error;
                        isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
                        isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
-                       if (add_div_constraints(dst, j) < 0)
+                       if (isl_basic_map_add_div_constraints(dst, j) < 0)
                                goto error;
                }
                if (j != i)
@@ -4041,6 +4544,10 @@ struct isl_map *isl_map_align_divs(struct isl_map *map)
 {
        int i;
 
+       if (!map)
+               return NULL;
+       if (map->n == 0)
+               return map;
        map = isl_map_compute_divs(map);
        map = isl_map_cow(map);
        if (!map)
@@ -4060,165 +4567,6 @@ struct isl_set *isl_set_align_divs(struct isl_set *set)
        return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
 }
 
-static struct isl_map *add_cut_constraint(struct isl_map *dst,
-               struct isl_basic_map *src, isl_int *c,
-               unsigned len, int oppose)
-{
-       struct isl_basic_map *copy = NULL;
-       int is_empty;
-       int k;
-       unsigned total;
-
-       copy = isl_basic_map_copy(src);
-       copy = isl_basic_map_cow(copy);
-       if (!copy)
-               goto error;
-       copy = isl_basic_map_extend_constraints(copy, 0, 1);
-       k = isl_basic_map_alloc_inequality(copy);
-       if (k < 0)
-               goto error;
-       if (oppose)
-               isl_seq_neg(copy->ineq[k], c, len);
-       else
-               isl_seq_cpy(copy->ineq[k], c, len);
-       total = 1 + isl_basic_map_total_dim(copy);
-       isl_seq_clr(copy->ineq[k]+len, total - len);
-       isl_inequality_negate(copy, k);
-       copy = isl_basic_map_simplify(copy);
-       copy = isl_basic_map_finalize(copy);
-       is_empty = isl_basic_map_is_empty(copy);
-       if (is_empty < 0)
-               goto error;
-       if (!is_empty)
-               dst = isl_map_add(dst, copy);
-       else
-               isl_basic_map_free(copy);
-       return dst;
-error:
-       isl_basic_map_free(copy);
-       isl_map_free(dst);
-       return NULL;
-}
-
-static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
-{
-       int i, j, k;
-       unsigned flags = 0;
-       struct isl_map *rest = NULL;
-       unsigned max;
-       unsigned total = isl_basic_map_total_dim(bmap);
-
-       assert(bmap);
-
-       if (!map)
-               goto error;
-
-       if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
-               ISL_FL_SET(flags, ISL_MAP_DISJOINT);
-
-       max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
-       rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
-       if (!rest)
-               goto error;
-
-       for (i = 0; i < map->n; ++i) {
-               map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
-               if (!map->p[i])
-                       goto error;
-       }
-
-       for (j = 0; j < map->n; ++j)
-               map->p[j] = isl_basic_map_cow(map->p[j]);
-
-       for (i = 0; i < bmap->n_eq; ++i) {
-               for (j = 0; j < map->n; ++j) {
-                       rest = add_cut_constraint(rest,
-                               map->p[j], bmap->eq[i], 1+total, 0);
-                       if (!rest)
-                               goto error;
-
-                       rest = add_cut_constraint(rest,
-                               map->p[j], bmap->eq[i], 1+total, 1);
-                       if (!rest)
-                               goto error;
-
-                       map->p[j] = isl_basic_map_extend_constraints(map->p[j],
-                               1, 0);
-                       if (!map->p[j])
-                               goto error;
-                       k = isl_basic_map_alloc_equality(map->p[j]);
-                       if (k < 0)
-                               goto error;
-                       isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
-                       isl_seq_clr(map->p[j]->eq[k]+1+total,
-                                       map->p[j]->n_div - bmap->n_div);
-               }
-       }
-
-       for (i = 0; i < bmap->n_ineq; ++i) {
-               for (j = 0; j < map->n; ++j) {
-                       rest = add_cut_constraint(rest,
-                               map->p[j], bmap->ineq[i], 1+total, 0);
-                       if (!rest)
-                               goto error;
-
-                       map->p[j] = isl_basic_map_extend_constraints(map->p[j],
-                               0, 1);
-                       if (!map->p[j])
-                               goto error;
-                       k = isl_basic_map_alloc_inequality(map->p[j]);
-                       if (k < 0)
-                               goto error;
-                       isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
-                       isl_seq_clr(map->p[j]->ineq[k]+1+total,
-                                       map->p[j]->n_div - bmap->n_div);
-               }
-       }
-
-       isl_map_free(map);
-       return rest;
-error:
-       isl_map_free(map);
-       isl_map_free(rest);
-       return NULL;
-}
-
-struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
-{
-       int i;
-       if (!map1 || !map2)
-               goto error;
-
-       isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
-
-       if (isl_map_is_empty(map2)) {
-               isl_map_free(map2);
-               return map1;
-       }
-
-       map1 = isl_map_compute_divs(map1);
-       map2 = isl_map_compute_divs(map2);
-       if (!map1 || !map2)
-               goto error;
-
-       for (i = 0; map1 && i < map2->n; ++i)
-               map1 = subtract(map1, map2->p[i]);
-
-       isl_map_free(map2);
-       return map1;
-error:
-       isl_map_free(map1);
-       isl_map_free(map2);
-       return NULL;
-}
-
-struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
-{
-       return (struct isl_set *)
-               isl_map_subtract(
-                       (struct isl_map *)set1, (struct isl_map *)set2);
-}
-
 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
 {
        if (!set || !map)
@@ -4275,11 +4623,12 @@ struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
 
 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
 {
-       (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
+       return (struct isl_basic_set *)
+               isl_map_copy_basic_map((struct isl_map *)set);
 }
 
-struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
-                                               struct isl_basic_map *bmap)
+__isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
+                                               __isl_keep isl_basic_map *bmap)
 {
        int i;
 
@@ -4299,34 +4648,26 @@ struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
                map->n--;
                return map;
        }
-       isl_basic_map_free(bmap);
        return map;
 error:
        isl_map_free(map);
-       isl_basic_map_free(bmap);
        return NULL;
 }
 
 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
                                                struct isl_basic_set *bset)
 {
-       (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
+       return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
                                                (struct isl_basic_map *)bset);
 }
 
-/* Given two _disjoint_ basic sets bset1 and bset2, check whether
- * for any common value of the parameters and dimensions preceding dim
- * in both basic sets, the values of dimension pos in bset1 are
- * smaller or larger than those in bset2.
- *
- * Returns
- *      1 if bset1 follows bset2
- *     -1 if bset1 precedes bset2
- *      0 if bset1 and bset2 are incomparable
- *     -2 if some error occurred.
+/* Given two basic sets bset1 and bset2, compute the maximal difference
+ * between the values of dimension pos in bset1 and those in bset2
+ * for any common value of the parameters and dimensions preceding pos.
  */
-int isl_basic_set_compare_at(struct isl_basic_set *bset1,
-       struct isl_basic_set *bset2, int pos)
+static enum isl_lp_result basic_set_maximal_difference_at(
+       __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
+       int pos, isl_int *opt)
 {
        struct isl_dim *dims;
        struct isl_basic_map *bmap1 = NULL;
@@ -4336,12 +4677,10 @@ int isl_basic_set_compare_at(struct isl_basic_set *bset1,
        unsigned total;
        unsigned nparam;
        unsigned dim1, dim2;
-       isl_int num, den;
        enum isl_lp_result res;
-       int cmp;
 
        if (!bset1 || !bset2)
-               return -2;
+               return isl_lp_error;
 
        nparam = isl_basic_set_n_param(bset1);
        dim1 = isl_basic_set_n_dim(bset1);
@@ -4367,27 +4706,122 @@ int isl_basic_set_compare_at(struct isl_basic_set *bset1,
        isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
        if (!obj)
                goto error;
-       isl_int_init(num);
-       isl_int_init(den);
-       res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
+       res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
+                                       opt, NULL, NULL);
+       isl_basic_map_free(bmap1);
+       isl_vec_free(obj);
+       return res;
+error:
+       isl_basic_map_free(bmap1);
+       isl_basic_map_free(bmap2);
+       return isl_lp_error;
+}
+
+/* Given two _disjoint_ basic sets bset1 and bset2, check whether
+ * for any common value of the parameters and dimensions preceding pos
+ * in both basic sets, the values of dimension pos in bset1 are
+ * smaller or larger than those in bset2.
+ *
+ * Returns
+ *      1 if bset1 follows bset2
+ *     -1 if bset1 precedes bset2
+ *      0 if bset1 and bset2 are incomparable
+ *     -2 if some error occurred.
+ */
+int isl_basic_set_compare_at(struct isl_basic_set *bset1,
+       struct isl_basic_set *bset2, int pos)
+{
+       isl_int opt;
+       enum isl_lp_result res;
+       int cmp;
+
+       isl_int_init(opt);
+
+       res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
+
        if (res == isl_lp_empty)
                cmp = 0;
-       else if (res == isl_lp_ok && isl_int_is_pos(num))
+       else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
+                 res == isl_lp_unbounded)
                cmp = 1;
-       else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
+       else if (res == isl_lp_ok && isl_int_is_neg(opt))
+               cmp = -1;
+       else
+               cmp = -2;
+
+       isl_int_clear(opt);
+       return cmp;
+}
+
+/* Given two basic sets bset1 and bset2, check whether
+ * for any common value of the parameters and dimensions preceding pos
+ * there is a value of dimension pos in bset1 that is larger
+ * than a value of the same dimension in bset2.
+ *
+ * Return
+ *      1 if there exists such a pair
+ *      0 if there is no such pair, but there is a pair of equal values
+ *     -1 otherwise
+ *     -2 if some error occurred.
+ */
+int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
+       __isl_keep isl_basic_set *bset2, int pos)
+{
+       isl_int opt;
+       enum isl_lp_result res;
+       int cmp;
+
+       isl_int_init(opt);
+
+       res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
+
+       if (res == isl_lp_empty)
+               cmp = -1;
+       else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
                  res == isl_lp_unbounded)
+               cmp = 1;
+       else if (res == isl_lp_ok && isl_int_is_neg(opt))
                cmp = -1;
+       else if (res == isl_lp_ok)
+               cmp = 0;
        else
                cmp = -2;
-       isl_int_clear(num);
-       isl_int_clear(den);
-       isl_basic_map_free(bmap1);
-       isl_vec_free(obj);
+
+       isl_int_clear(opt);
        return cmp;
-error:
-       isl_basic_map_free(bmap1);
-       isl_basic_map_free(bmap2);
-       return -2;
+}
+
+/* Given two sets set1 and set2, check whether
+ * for any common value of the parameters and dimensions preceding pos
+ * there is a value of dimension pos in set1 that is larger
+ * than a value of the same dimension in set2.
+ *
+ * Return
+ *      1 if there exists such a pair
+ *      0 if there is no such pair, but there is a pair of equal values
+ *     -1 otherwise
+ *     -2 if some error occurred.
+ */
+int isl_set_follows_at(__isl_keep isl_set *set1,
+       __isl_keep isl_set *set2, int pos)
+{
+       int i, j;
+       int follows = -1;
+
+       if (!set1 || !set2)
+               return -2;
+
+       for (i = 0; i < set1->n; ++i)
+               for (j = 0; j < set2->n; ++j) {
+                       int f;
+                       f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
+                       if (f == 1 || f == -2)
+                               return f;
+                       if (f > follows)
+                               follows = f;
+               }
+
+       return follows;
 }
 
 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
@@ -4470,6 +4904,15 @@ int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
                isl_basic_map_offset(bmap, type) - 1 + pos, val);
 }
 
+int isl_map_fast_is_fixed(struct isl_map *map,
+       enum isl_dim_type type, unsigned pos, isl_int *val)
+{
+       if (pos >= isl_map_dim(map, type))
+               return -1;
+       return isl_map_fast_has_fixed_var(map,
+               map_offset(map, type) - 1 + pos, val);
+}
+
 /* Check if dimension dim has fixed value and if so and if val is not NULL,
  * then return this fixed value in *val.
  */
@@ -4865,7 +5308,7 @@ struct isl_basic_map *isl_basic_map_product(
        if (!bmap1 || !bmap2)
                goto error;
 
-       isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
+       isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
                                     bmap2->dim, isl_dim_param), goto error);
        dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
                                                   isl_dim_copy(bmap2->dim));
@@ -4958,7 +5401,7 @@ struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
 {
        int i;
-       uint32_t hash;
+       uint32_t hash = isl_hash_init();
        unsigned total;
 
        if (!bset)
@@ -5054,3 +5497,122 @@ int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
        }
        return 1;
 }
+
+int isl_map_foreach_basic_map(__isl_keep isl_map *map,
+       int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
+{
+       int i;
+
+       if (!map)
+               return -1;
+
+       for (i = 0; i < map->n; ++i)
+               if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
+                       return -1;
+
+       return 0;
+}
+
+int isl_set_foreach_basic_set(__isl_keep isl_set *set,
+       int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
+{
+       int i;
+
+       if (!set)
+               return -1;
+
+       for (i = 0; i < set->n; ++i)
+               if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
+                       return -1;
+
+       return 0;
+}
+
+__isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
+{
+       struct isl_dim *dim;
+       struct isl_basic_map *bmap;
+       unsigned n_set;
+       unsigned n_div;
+       unsigned n_param;
+       unsigned total;
+       int i, k, l;
+
+       set = isl_set_align_divs(set);
+
+       if (!set)
+               return NULL;
+
+       dim = isl_set_get_dim(set);
+       if (set->n == 0 || set->p[0]->n_div == 0) {
+               isl_set_free(set);
+               return isl_map_identity(dim);
+       }
+
+       n_div = set->p[0]->n_div;
+       dim = isl_dim_map(dim);
+       n_param = isl_dim_size(dim, isl_dim_param);
+       n_set = isl_dim_size(dim, isl_dim_in);
+       dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
+       bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
+       for (i = 0; i < n_set; ++i)
+               bmap = var_equal(bmap, i);
+
+       total = n_param + n_set + n_set + n_div;
+       for (i = 0; i < n_div; ++i) {
+               k = isl_basic_map_alloc_inequality(bmap);
+               if (k < 0)
+                       goto error;
+               isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
+               isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
+               isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
+                           set->p[0]->div[i]+1+1+n_param, n_set + n_div);
+               isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
+                           set->p[0]->div[i][0]);
+
+               l = isl_basic_map_alloc_inequality(bmap);
+               if (l < 0)
+                       goto error;
+               isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
+               isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
+                           set->p[0]->div[i][0]);
+               isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
+       }
+
+       isl_set_free(set);
+       return isl_map_from_basic_map(bmap);
+error:
+       isl_set_free(set);
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+int isl_basic_set_size(__isl_keep isl_basic_set *bset)
+{
+       unsigned dim;
+       int size = 0;
+
+       if (!bset)
+               return -1;
+
+       dim = isl_basic_set_total_dim(bset);
+       size += bset->n_eq * (1 + dim);
+       size += bset->n_ineq * (1 + dim);
+       size += bset->n_div * (2 + dim);
+
+       return size;
+}
+
+int isl_set_size(__isl_keep isl_set *set)
+{
+       int i;
+       int size = 0;
+
+       if (!set)
+               return -1;
+
+       for (i = 0; i < set->n; ++i)
+               size += isl_basic_set_size(set->p[i]);
+
+       return size;
+}