isl_basic_map_gist: prefer contraints without existentially quantified variables
[platform/upstream/isl.git] / isl_map.c
index ccc9b7d..06aa85c 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
 
 #include <string.h>
 #include <strings.h>
-#include "isl_ctx.h"
-#include "isl_blk.h"
+#include <isl_map_private.h>
+#include <isl/ctx.h>
+#include <isl/blk.h>
 #include "isl_dim_private.h"
 #include "isl_equalities.h"
-#include "isl_list.h"
-#include "isl_lp.h"
-#include "isl_seq.h"
-#include "isl_set.h"
-#include "isl_map.h"
-#include "isl_map_private.h"
+#include <isl/list.h>
+#include <isl/lp.h>
+#include <isl/seq.h>
+#include <isl/set.h>
+#include <isl/map.h>
 #include "isl_map_piplib.h"
 #include <isl_reordering.h>
 #include "isl_sample.h"
 #include "isl_tab.h"
-#include "isl_vec.h"
+#include <isl/vec.h>
 #include <isl_mat_private.h>
 
 /* Maps dst positions to src positions */
@@ -323,6 +323,7 @@ __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
        bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
        if (!bmap->dim)
                goto error;
+       bmap = isl_basic_map_finalize(bmap);
        return bmap;
 error:
        isl_basic_map_free(bmap);
@@ -372,11 +373,34 @@ __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
        return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
 }
 
+const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
+{
+       return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
+}
+
 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
 {
        return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
 }
 
+const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
+       enum isl_dim_type type, unsigned pos)
+{
+       return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
+}
+
+const char *isl_map_get_dim_name(__isl_keep isl_map *map,
+       enum isl_dim_type type, unsigned pos)
+{
+       return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
+}
+
+const char *isl_set_get_dim_name(__isl_keep isl_set *set,
+       enum isl_dim_type type, unsigned pos)
+{
+       return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
+}
+
 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
        __isl_take isl_basic_map *bmap,
        enum isl_dim_type type, unsigned pos, const char *s)
@@ -437,6 +461,11 @@ int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
        return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
 }
 
+int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
+{
+       return isl_basic_map_is_rational(bset);
+}
+
 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
                struct isl_basic_map *bmap, unsigned extra,
                unsigned n_eq, unsigned n_ineq)
@@ -505,9 +534,12 @@ struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
        struct isl_basic_map *bmap;
        if (!dim)
                return NULL;
-       isl_assert(dim->ctx, dim->n_in == 0, return NULL);
+       isl_assert(dim->ctx, dim->n_in == 0, goto error);
        bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
        return (struct isl_basic_set *)bmap;
+error:
+       isl_dim_free(dim);
+       return NULL;
 }
 
 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
@@ -1389,15 +1421,6 @@ __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
        return isl_set_eliminate(set, isl_dim_set, first, n);
 }
 
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_set *isl_set_remove_dims(struct isl_set *set,
-       unsigned first, unsigned n)
-{
-       set = isl_set_eliminate_dims(set, first, n);
-       set = isl_set_drop_dims(set, first, n);
-       return set;
-}
-
 __isl_give isl_basic_map *isl_basic_map_remove_divs(
        __isl_take isl_basic_map *bmap)
 {
@@ -1406,7 +1429,7 @@ __isl_give isl_basic_map *isl_basic_map_remove_divs(
        if (!bmap)
                return NULL;
        bmap->n_div = 0;
-       return bmap;
+       return isl_basic_map_finalize(bmap);
 }
 
 __isl_give isl_basic_set *isl_basic_set_remove_divs(
@@ -1440,7 +1463,7 @@ error:
        return NULL;
 }
 
-struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
+struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
        enum isl_dim_type type, unsigned first, unsigned n)
 {
        if (!bmap)
@@ -1462,14 +1485,171 @@ error:
        return NULL;
 }
 
-__isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
+/* Return true if the definition of the given div (recursively) involves
+ * any of the given variables.
+ */
+static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
+       unsigned first, unsigned n)
+{
+       int i;
+       unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
+
+       if (isl_int_is_zero(bmap->div[div][0]))
+               return 0;
+       if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
+               return 1;
+
+       for (i = bmap->n_div - 1; i >= 0; --i) {
+               if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
+                       continue;
+               if (div_involves_vars(bmap, i, first, n))
+                       return 1;
+       }
+
+       return 0;
+}
+
+/* Remove all divs (recursively) involving any of the given dimensions
+ * in their definitions.
+ */
+__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
+       __isl_take isl_basic_map *bmap,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       int i;
+
+       if (!bmap)
+               return NULL;
+       isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
+                       goto error);
+       first += isl_basic_map_offset(bmap, type);
+
+       for (i = bmap->n_div - 1; i >= 0; --i) {
+               if (!div_involves_vars(bmap, i, first, n))
+                       continue;
+               bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
+       }
+
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       int i;
+
+       if (!map)
+               return NULL;
+       if (map->n == 0)
+               return map;
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
+                                                               type, first, n);
+               if (!map->p[i])
+                       goto error;
+       }
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
+__isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
+                                                             type, first, n);
+}
+
+/* Return true if the definition of the given div is unknown or depends
+ * on unknown divs.
+ */
+static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
+{
+       int i;
+       unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
+
+       if (isl_int_is_zero(bmap->div[div][0]))
+               return 1;
+
+       for (i = bmap->n_div - 1; i >= 0; --i) {
+               if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
+                       continue;
+               if (div_is_unknown(bmap, i))
+                       return 1;
+       }
+
+       return 0;
+}
+
+/* Remove all divs that are unknown or defined in terms of unknown divs.
+ */
+__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
+       __isl_take isl_basic_map *bmap)
+{
+       int i;
+
+       if (!bmap)
+               return NULL;
+
+       for (i = bmap->n_div - 1; i >= 0; --i) {
+               if (!div_is_unknown(bmap, i))
+                       continue;
+               bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
+       }
+
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
+{
+       int i;
+
+       if (!map)
+               return NULL;
+       if (map->n == 0)
+               return map;
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
+               if (!map->p[i])
+                       goto error;
+       }
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
+__isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
+{
+       return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
+}
+
+__isl_give isl_basic_set *isl_basic_set_remove_dims(
+       __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);
+           isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
 }
 
-struct isl_map *isl_map_remove(struct isl_map *map,
+struct isl_map *isl_map_remove_dims(struct isl_map *map,
        enum isl_dim_type type, unsigned first, unsigned n)
 {
        int i;
@@ -1495,27 +1675,17 @@ error:
        return NULL;
 }
 
-__isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
+__isl_give isl_set *isl_set_remove_dims(__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);
+       return (isl_set *)isl_map_remove_dims((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)
 {
-       return isl_map_remove(map, isl_dim_in, first, n);
-}
-
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
-       unsigned first, unsigned n)
-{
-       unsigned nparam = isl_basic_set_n_param(bset);
-       bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
-       bset = isl_basic_set_drop_dims(bset, first, n);
-       return bset;
+       return isl_map_remove_dims(map, isl_dim_in, first, n);
 }
 
 static void dump_term(struct isl_basic_map *bmap,
@@ -1634,7 +1804,8 @@ static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
        }
 }
 
-void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
+void isl_basic_set_print_internal(struct isl_basic_set *bset,
+       FILE *out, int indent)
 {
        if (!bset) {
                fprintf(out, "null basic set\n");
@@ -1648,7 +1819,8 @@ void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
        dump((struct isl_basic_map *)bset, out, indent);
 }
 
-void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
+void isl_basic_map_print_internal(struct isl_basic_map *bmap,
+       FILE *out, int indent)
 {
        if (!bmap) {
                fprintf(out, "null basic map\n");
@@ -1683,8 +1855,8 @@ struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
 
        if (!dim)
                return NULL;
-       isl_assert(dim->ctx, dim->n_in == 0, return NULL);
-       isl_assert(dim->ctx, n >= 0, return NULL);
+       isl_assert(dim->ctx, dim->n_in == 0, goto error);
+       isl_assert(dim->ctx, n >= 0, goto error);
        set = isl_alloc(dim->ctx, struct isl_set,
                        sizeof(struct isl_set) +
                        (n - 1) * sizeof(struct isl_basic_set *));
@@ -1772,17 +1944,7 @@ struct isl_set *isl_set_dup(struct isl_set *set)
 
 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
 {
-       struct isl_set *set;
-
-       if (!bset)
-               return NULL;
-
-       set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
-       if (!set) {
-               isl_basic_set_free(bset);
-               return NULL;
-       }
-       return isl_set_add_basic_set(set, bset);
+       return isl_map_from_basic_map(bset);
 }
 
 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
@@ -1793,10 +1955,6 @@ struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
                return NULL;
 
        map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
-       if (!map) {
-               isl_basic_map_free(bmap);
-               return NULL;
-       }
        return isl_map_add_basic_map(map, bmap);
 }
 
@@ -1824,7 +1982,7 @@ void isl_set_free(struct isl_set *set)
        free(set);
 }
 
-void isl_set_dump(struct isl_set *set, FILE *out, int indent)
+void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
 {
        int i;
 
@@ -1840,11 +1998,11 @@ void isl_set_dump(struct isl_set *set, FILE *out, int indent)
        for (i = 0; i < set->n; ++i) {
                fprintf(out, "%*s", indent, "");
                fprintf(out, "basic set %d:\n", i);
-               isl_basic_set_dump(set->p[i], out, indent+4);
+               isl_basic_set_print_internal(set->p[i], out, indent+4);
        }
 }
 
-void isl_map_dump(struct isl_map *map, FILE *out, int indent)
+void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
 {
        int i;
 
@@ -1861,7 +2019,7 @@ void isl_map_dump(struct isl_map *map, FILE *out, int indent)
        for (i = 0; i < map->n; ++i) {
                fprintf(out, "%*s", indent, "");
                fprintf(out, "basic map %d:\n", i);
-               isl_basic_map_dump(map->p[i], out, indent+4);
+               isl_basic_map_print_internal(map->p[i], out, indent+4);
        }
 }
 
@@ -1869,7 +2027,6 @@ struct isl_basic_map *isl_basic_map_intersect_domain(
                struct isl_basic_map *bmap, struct isl_basic_set *bset)
 {
        struct isl_basic_map *bmap_domain;
-       struct isl_dim *dim;
 
        if (!bmap || !bset)
                goto error;
@@ -1886,8 +2043,7 @@ struct isl_basic_map *isl_basic_map_intersect_domain(
                goto error;
        bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
                        bset->n_div, bset->n_eq, bset->n_ineq);
-       dim = isl_dim_reverse(isl_dim_copy(bset->dim));
-       bmap_domain = isl_basic_map_from_basic_set(bset, dim);
+       bmap_domain = isl_basic_map_from_domain(bset);
        bmap = add_constraints(bmap, bmap_domain, 0, 0);
 
        bmap = isl_basic_map_simplify(bmap);
@@ -2211,8 +2367,9 @@ __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
 
        res = isl_basic_map_alloc_dim(res_dim,
                        bmap->n_div, bmap->n_eq, bmap->n_ineq);
+       if (isl_basic_map_is_rational(bmap))
+               res = isl_basic_map_set_rational(res);
        res = add_constraints_dim_map(res, bmap, dim_map);
-       res = isl_basic_map_simplify(res);
        return isl_basic_map_finalize(res);
 }
 
@@ -2508,7 +2665,7 @@ __isl_give isl_basic_map *isl_basic_map_project_out(
                return NULL;
 
        if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
-               return isl_basic_map_remove(bmap, type, first, n);
+               return isl_basic_map_remove_dims(bmap, type, first, n);
 
        isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
                        goto error);
@@ -2835,6 +2992,11 @@ struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
        return isl_basic_map_finalize(bmap);
 }
 
+__isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
+{
+       return isl_basic_map_neg(bset);
+}
+
 /* Given a map A -> f(A), construct A -> -f(A).
  */
 struct isl_map *isl_map_neg(struct isl_map *map)
@@ -3406,7 +3568,9 @@ struct isl_basic_set *isl_basic_map_underlying_set(
        if (!bmap)
                goto error;
        if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
-           bmap->n_div == 0 && !isl_dim_get_tuple_name(bmap->dim, isl_dim_out))
+           bmap->n_div == 0 &&
+           !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
+           !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
                return (struct isl_basic_set *)bmap;
        bmap = isl_basic_map_cow(bmap);
        if (!bmap)
@@ -3576,7 +3740,7 @@ struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
        return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
 }
 
-static __isl_give isl_basic_map *isl_basic_map_reset_dim(
+__isl_give isl_basic_map *isl_basic_map_reset_dim(
        __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
 {
        bmap = isl_basic_map_cow(bmap);
@@ -3869,6 +4033,13 @@ __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
        return isl_map_reverse(isl_map_from_range(set));
 }
 
+__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
+       __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
+{
+       return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
+                                        isl_basic_map_from_range(range));
+}
+
 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
        __isl_take isl_set *range)
 {
@@ -3992,6 +4163,7 @@ struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
 {
        struct isl_basic_map *bmap;
        bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
+       bmap = isl_basic_map_finalize(bmap);
        return bmap;
 }
 
@@ -3999,9 +4171,45 @@ struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
 {
        struct isl_basic_set *bset;
        bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
+       bset = isl_basic_set_finalize(bset);
        return bset;
 }
 
+__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
+{
+       int i;
+       unsigned total = isl_dim_total(dim);
+       isl_basic_map *bmap;
+
+       bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
+       for (i = 0; i < total; ++i) {
+               int k = isl_basic_map_alloc_inequality(bmap);
+               if (k < 0)
+                       goto error;
+               isl_seq_clr(bmap->ineq[k], 1 + total);
+               isl_int_set_si(bmap->ineq[k][1 + i], 1);
+       }
+       return bmap;
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+__isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
+{
+       return isl_basic_map_nat_universe(dim);
+}
+
+__isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
+{
+       return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
+}
+
+__isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
+{
+       return isl_map_nat_universe(dim);
+}
+
 __isl_give isl_basic_map *isl_basic_map_universe_like(
                __isl_keep isl_basic_map *model)
 {
@@ -4501,7 +4709,7 @@ struct isl_set *isl_basic_set_partial_lexmax(
                        dom, empty);
 }
 
-/* Given a basic map "bmap", compute the lexicograhically minimal
+/* Given a basic map "bmap", compute the lexicographically minimal
  * (or maximal) image element for each domain element in dom.
  * Set *empty to those elements in dom that do not have an image element.
  *
@@ -4553,7 +4761,7 @@ error:
        return NULL;
 }
 
-/* Given a map "map", compute the lexicograhically minimal
+/* Given a map "map", compute the lexicographically minimal
  * (or maximal) image element for each domain element in dom.
  * Set *empty to those elements in dom that do not have an image element.
  *
@@ -4565,16 +4773,46 @@ error:
  *
  * Let res^k and todo^k be the results after k steps and let i = k + 1.
  * Assume we are computing the lexicographical maximum.
- * We first intersect basic map i with a relation that maps elements
- * to elements that are lexicographically larger than the image elements
- * in res^k and the compute the maximum image element of this intersection.
- * The result ("better") corresponds to those image elements in basic map i
- * that are better than what we had before.  The remainder ("keep") are the
- * domain elements for which the image element in res_k was better.
- * We also compute the lexicographical maximum of basic map i in todo^k.
- * res^i is the result of the operation + better + those elements in
- *             res^k that we should keep
- * todo^i is the remainder of the maximum operation on todo^k.
+ * We first compute the lexicographically maximal element in basic map i.
+ * This results in a partial solution res_i and a subset todo_i.
+ * Then we combine these results with those obtain for the first k basic maps
+ * to obtain a result that is valid for the first k+1 basic maps.
+ * In particular, the set where there is no solution is the set where
+ * there is no solution for the first k basic maps and also no solution
+ * for the ith basic map, i.e.,
+ *
+ *     todo^i = todo^k * todo_i
+ *
+ * On dom(res^k) * dom(res_i), we need to pick the larger of the two
+ * solutions, arbitrarily breaking ties in favor of res^k.
+ * That is, when res^k(a) >= res_i(a), we pick res^k and
+ * when res^k(a) < res_i(a), we pick res_i.  (Here, ">=" and "<" denote
+ * the lexicographic order.)
+ * In practice, we compute
+ *
+ *     res^k * (res_i . "<=")
+ *
+ * and
+ *
+ *     res_i * (res^k . "<")
+ *
+ * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
+ * where only one of res^k and res_i provides a solution and we simply pick
+ * that one, i.e.,
+ *
+ *     res^k * todo_i
+ * and
+ *     res_i * todo^k
+ *
+ * Note that we only compute these intersections when dom(res^k) intersects
+ * dom(res_i).  Otherwise, the only effect of these intersections is to
+ * potentially break up res^k and res_i into smaller pieces.
+ * We want to avoid such splintering as much as possible.
+ * In fact, an earlier implementation of this function would look for
+ * better results in the domain of res^k and for extra results in todo^k,
+ * but this would always result in a splintering according to todo^k,
+ * even when the domain of basic map i is disjoint from the domains of
+ * the previous basic maps.
  */
 static __isl_give isl_map *isl_map_partial_lexopt(
                __isl_take isl_map *map, __isl_take isl_set *dom,
@@ -4599,32 +4837,38 @@ static __isl_give isl_map *isl_map_partial_lexopt(
                                        isl_set_copy(dom), &todo, max);
 
        for (i = 1; i < map->n; ++i) {
-               struct isl_map *lt;
-               struct isl_map *better;
-               struct isl_set *keep;
-               struct isl_map *res_i;
-               struct isl_set *todo_i;
-               struct isl_dim *dim = isl_map_get_dim(res);
-
-               dim = isl_dim_range(dim);
-               if (max)
-                       lt = isl_map_lex_lt(dim);
-               else
-                       lt = isl_map_lex_gt(dim);
-               lt = isl_map_apply_range(isl_map_copy(res), lt);
-               lt = isl_map_intersect(lt,
-                       isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
-               better = isl_map_partial_lexopt(lt,
-                       isl_map_domain(isl_map_copy(res)),
-                       &keep, max);
+               isl_map *lt, *le;
+               isl_map *res_i;
+               isl_set *todo_i;
+               isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
 
                res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
-                                               todo, &todo_i, max);
+                                       isl_set_copy(dom), &todo_i, max);
+
+               if (max) {
+                       lt = isl_map_lex_lt(isl_dim_copy(dim));
+                       le = isl_map_lex_le(dim);
+               } else {
+                       lt = isl_map_lex_gt(isl_dim_copy(dim));
+                       le = isl_map_lex_ge(dim);
+               }
+               lt = isl_map_apply_range(isl_map_copy(res), lt);
+               lt = isl_map_intersect(lt, isl_map_copy(res_i));
+               le = isl_map_apply_range(isl_map_copy(res_i), le);
+               le = isl_map_intersect(le, isl_map_copy(res));
+
+               if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
+                       res = isl_map_intersect_domain(res,
+                                                       isl_set_copy(todo_i));
+                       res_i = isl_map_intersect_domain(res_i,
+                                                       isl_set_copy(todo));
+               }
 
-               res = isl_map_intersect_domain(res, keep);
                res = isl_map_union_disjoint(res, res_i);
-               res = isl_map_union_disjoint(res, better);
-               todo = todo_i;
+               res = isl_map_union_disjoint(res, lt);
+               res = isl_map_union_disjoint(res, le);
+
+               todo = isl_set_intersect(todo, todo_i);
        }
 
        isl_set_free(dom);
@@ -5444,6 +5688,14 @@ struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
        return map_identity(isl_dim_copy(model->dim));
 }
 
+__isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
+{
+       isl_dim *dim = isl_set_get_dim(set);
+       isl_map *id;
+       id = isl_map_identity(dim);
+       return isl_map_intersect_range(id, set);
+}
+
 /* Construct a basic set with all set dimensions having only non-negative
  * values.
  */
@@ -5474,12 +5726,54 @@ error:
        return NULL;
 }
 
+/* Construct the half-space x_pos >= 0.
+ */
+static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
+       int pos)
+{
+       int k;
+       isl_basic_set *nonneg;
+
+       nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
+       k = isl_basic_set_alloc_inequality(nonneg);
+       if (k < 0)
+               goto error;
+       isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
+       isl_int_set_si(nonneg->ineq[k][pos], 1);
+
+       return isl_basic_set_finalize(nonneg);
+error:
+       isl_basic_set_free(nonneg);
+       return NULL;
+}
+
+/* Construct the half-space x_pos <= -1.
+ */
+static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
+{
+       int k;
+       isl_basic_set *neg;
+
+       neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
+       k = isl_basic_set_alloc_inequality(neg);
+       if (k < 0)
+               goto error;
+       isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
+       isl_int_set_si(neg->ineq[k][0], -1);
+       isl_int_set_si(neg->ineq[k][pos], -1);
+
+       return isl_basic_set_finalize(neg);
+error:
+       isl_basic_set_free(neg);
+       return NULL;
+}
+
 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
        enum isl_dim_type type, unsigned first, unsigned n)
 {
        int i;
-       isl_basic_set *nonneg = NULL;
-       isl_basic_set *neg = NULL;
+       isl_basic_set *nonneg;
+       isl_basic_set *neg;
 
        if (!set)
                return NULL;
@@ -5489,35 +5783,84 @@ __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
        isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
 
        for (i = 0; i < n; ++i) {
-               int k;
-
-               neg = NULL;
-               nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
-               k = isl_basic_set_alloc_inequality(nonneg);
-               if (k < 0)
-                       goto error;
-               isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
-               isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
-
-               neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
-               k = isl_basic_set_alloc_inequality(neg);
-               if (k < 0)
-                       goto error;
-               isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
-               isl_int_set_si(neg->ineq[k][0], -1);
-               isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
+               nonneg = nonneg_halfspace(isl_set_get_dim(set),
+                                         pos(set->dim, type) + first + i);
+               neg = neg_halfspace(isl_set_get_dim(set),
+                                         pos(set->dim, type) + first + i);
 
                set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
        }
 
        return set;
 error:
-       isl_basic_set_free(nonneg);
-       isl_basic_set_free(neg);
        isl_set_free(set);
        return NULL;
 }
 
+static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
+       int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
+       void *user)
+{
+       isl_set *half;
+
+       if (!set)
+               return -1;
+       if (isl_set_fast_is_empty(set)) {
+               isl_set_free(set);
+               return 0;
+       }
+       if (first == len)
+               return fn(set, signs, user);
+
+       signs[first] = 1;
+       half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
+                                                       1 + first));
+       half = isl_set_intersect(half, isl_set_copy(set));
+       if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
+               goto error;
+
+       signs[first] = -1;
+       half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
+                                                       1 + first));
+       half = isl_set_intersect(half, set);
+       return foreach_orthant(half, signs, first + 1, len, fn, user);
+error:
+       isl_set_free(set);
+       return -1;
+}
+
+/* Call "fn" on the intersections of "set" with each of the orthants
+ * (except for obviously empty intersections).  The orthant is identified
+ * by the signs array, with each entry having value 1 or -1 according
+ * to the sign of the corresponding variable.
+ */
+int isl_set_foreach_orthant(__isl_keep isl_set *set,
+       int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
+       void *user)
+{
+       unsigned nparam;
+       unsigned nvar;
+       int *signs;
+       int r;
+
+       if (!set)
+               return -1;
+       if (isl_set_fast_is_empty(set))
+               return 0;
+
+       nparam = isl_set_dim(set, isl_dim_param);
+       nvar = isl_set_dim(set, isl_dim_set);
+
+       signs = isl_alloc_array(set->ctx, int, nparam + nvar);
+
+       r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
+                           fn, user);
+
+       free(signs);
+
+       return r;
+}
+
 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);
@@ -6392,11 +6735,22 @@ struct constraint {
        isl_int         *c;
 };
 
+/* uset_gist depends on constraints without existentially quantified
+ * variables sorting first.
+ */
 static int qsort_constraint_cmp(const void *p1, const void *p2)
 {
        const struct constraint *c1 = (const struct constraint *)p1;
        const struct constraint *c2 = (const struct constraint *)p2;
+       int l1, l2;
        unsigned size = isl_min(c1->size, c2->size);
+
+       l1 = isl_seq_last_non_zero(c1->c, size);
+       l2 = isl_seq_last_non_zero(c2->c, size);
+
+       if (l1 != l2)
+               return l1 - l2;
+
        return isl_seq_cmp(c1->c, c2->c, size);
 }
 
@@ -6460,6 +6814,9 @@ int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
 
        if (bmap1 == bmap2)
                return 0;
+       if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
+           ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
+               return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
        if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
                return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
        if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
@@ -6636,7 +6993,8 @@ error:
 
 /* Return the Cartesian product of the basic sets in list (in the given order).
  */
-struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
+__isl_give isl_basic_set *isl_basic_set_list_product(
+       __isl_take struct isl_basic_set_list *list)
 {
        int i;
        unsigned dim;
@@ -6730,9 +7088,73 @@ error:
        return NULL;
 }
 
-/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
- */
-struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
+__isl_give isl_basic_map *isl_basic_map_flat_product(
+       __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
+{
+       isl_basic_map *prod;
+
+       prod = isl_basic_map_product(bmap1, bmap2);
+       prod = isl_basic_map_flatten(prod);
+       return prod;
+}
+
+__isl_give isl_basic_set *isl_basic_set_flat_product(
+       __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
+{
+       return isl_basic_map_flat_product(bset1, bset2);
+}
+
+__isl_give isl_basic_map *isl_basic_map_range_product(
+       __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
+{
+       isl_dim *dim_result = NULL;
+       isl_basic_map *bmap;
+       unsigned in, out1, out2, nparam, total, pos;
+       struct isl_dim_map *dim_map1, *dim_map2;
+
+       if (!bmap1 || !bmap2)
+               goto error;
+
+       dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
+                                          isl_dim_copy(bmap2->dim));
+
+       in = isl_basic_map_dim(bmap1, isl_dim_in);
+       out1 = isl_basic_map_n_out(bmap1);
+       out2 = isl_basic_map_n_out(bmap2);
+       nparam = isl_basic_map_n_param(bmap1);
+
+       total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
+       dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
+       dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
+       isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
+       isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
+       isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
+       isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
+       isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
+       isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
+       isl_dim_map_div(dim_map1, bmap1, pos += out2);
+       isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
+
+       bmap = isl_basic_map_alloc_dim(dim_result,
+                       bmap1->n_div + bmap2->n_div,
+                       bmap1->n_eq + bmap2->n_eq,
+                       bmap1->n_ineq + bmap2->n_ineq);
+       bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
+       bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
+       bmap = isl_basic_map_simplify(bmap);
+       return isl_basic_map_finalize(bmap);
+error:
+       isl_basic_map_free(bmap1);
+       isl_basic_map_free(bmap2);
+       return NULL;
+}
+
+static __isl_give isl_map *map_product(__isl_take isl_map *map1,
+       __isl_take isl_map *map2,
+       __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
+                                          __isl_take isl_dim *right),
+       __isl_give isl_basic_map *(*basic_map_product)(
+               __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
 {
        unsigned flags = 0;
        struct isl_map *result;
@@ -6748,17 +7170,16 @@ struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
            ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
                ISL_FL_SET(flags, ISL_MAP_DISJOINT);
 
-       result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
-                                                  isl_dim_copy(map2->dim)),
+       result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
+                                              isl_dim_copy(map2->dim)),
                                map1->n * map2->n, flags);
        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_product(
-                                   isl_basic_map_copy(map1->p[i]),
-                                   isl_basic_map_copy(map2->p[j]));
+                       part = basic_map_product(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
@@ -6775,6 +7196,13 @@ error:
        return NULL;
 }
 
+/* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
+ */
+struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
+{
+       return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
+}
+
 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
  */
 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
@@ -6801,68 +7229,87 @@ __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
        return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
 }
 
-uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
+/* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
+ */
+__isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
+       __isl_take isl_map *map2)
+{
+       return map_product(map1, map2, &isl_dim_range_product,
+                               &isl_basic_map_range_product);
+}
+
+uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
 {
        int i;
        uint32_t hash = isl_hash_init();
        unsigned total;
 
-       if (!bset)
+       if (!bmap)
                return 0;
-       bset = isl_basic_set_copy(bset);
-       bset = isl_basic_set_normalize(bset);
-       if (!bset)
+       bmap = isl_basic_map_copy(bmap);
+       bmap = isl_basic_map_normalize(bmap);
+       if (!bmap)
                return 0;
-       total = isl_basic_set_total_dim(bset);
-       isl_hash_byte(hash, bset->n_eq & 0xFF);
-       for (i = 0; i < bset->n_eq; ++i) {
+       total = isl_basic_map_total_dim(bmap);
+       isl_hash_byte(hash, bmap->n_eq & 0xFF);
+       for (i = 0; i < bmap->n_eq; ++i) {
                uint32_t c_hash;
-               c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
+               c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
                isl_hash_hash(hash, c_hash);
        }
-       isl_hash_byte(hash, bset->n_ineq & 0xFF);
-       for (i = 0; i < bset->n_ineq; ++i) {
+       isl_hash_byte(hash, bmap->n_ineq & 0xFF);
+       for (i = 0; i < bmap->n_ineq; ++i) {
                uint32_t c_hash;
-               c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
+               c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
                isl_hash_hash(hash, c_hash);
        }
-       isl_hash_byte(hash, bset->n_div & 0xFF);
-       for (i = 0; i < bset->n_div; ++i) {
+       isl_hash_byte(hash, bmap->n_div & 0xFF);
+       for (i = 0; i < bmap->n_div; ++i) {
                uint32_t c_hash;
-               if (isl_int_is_zero(bset->div[i][0]))
+               if (isl_int_is_zero(bmap->div[i][0]))
                        continue;
                isl_hash_byte(hash, i & 0xFF);
-               c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
+               c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
                isl_hash_hash(hash, c_hash);
        }
-       isl_basic_set_free(bset);
+       isl_basic_map_free(bmap);
        return hash;
 }
 
-uint32_t isl_set_get_hash(struct isl_set *set)
+uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
+{
+       return isl_basic_map_get_hash((isl_basic_map *)bset);
+}
+
+uint32_t isl_map_get_hash(__isl_keep isl_map *map)
 {
        int i;
        uint32_t hash;
 
-       if (!set)
+       if (!map)
                return 0;
-       set = isl_set_copy(set);
-       set = isl_set_normalize(set);
-       if (!set)
+       map = isl_map_copy(map);
+       map = isl_map_normalize(map);
+       if (!map)
                return 0;
 
        hash = isl_hash_init();
-       for (i = 0; i < set->n; ++i) {
-               uint32_t bset_hash;
-               bset_hash = isl_basic_set_get_hash(set->p[i]);
-               isl_hash_hash(hash, bset_hash);
+       for (i = 0; i < map->n; ++i) {
+               uint32_t bmap_hash;
+               bmap_hash = isl_basic_map_get_hash(map->p[i]);
+               isl_hash_hash(hash, bmap_hash);
        }
                
-       isl_set_free(set);
+       isl_map_free(map);
 
        return hash;
 }
 
+uint32_t isl_set_get_hash(__isl_keep isl_set *set)
+{
+       return isl_map_get_hash((isl_map *)set);
+}
+
 /* Check if the value for dimension dim is completely determined
  * by the values of the other parameters and variables.
  * That is, check if dimension dim is involved in an equality.
@@ -6901,6 +7348,11 @@ int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
        return 1;
 }
 
+int isl_set_n_basic_set(__isl_keep isl_set *set)
+{
+       return set ? set->n : 0;
+}
+
 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
        int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
 {
@@ -6951,6 +7403,7 @@ __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
                goto error;
        isl_dim_free(bset->dim);
        bset->dim = dim;
+       bset->extra -= bset->n_div;
        bset->n_div = 0;
 
        bset = isl_basic_set_finalize(bset);
@@ -7219,6 +7672,56 @@ int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
        return isl_basic_set_vars_get_sign(bset, first, n, signs);
 }
 
+/* Check if the given basic map is obviously single-valued.
+ * In particular, for each output dimension, check that there is
+ * an equality that defines the output dimension in terms of
+ * earlier dimensions.
+ */
+int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
+{
+       int i, j;
+       unsigned total;
+       unsigned n_out;
+       unsigned o_out;
+
+       if (!bmap)
+               return -1;
+
+       total = 1 + isl_basic_map_total_dim(bmap);
+       n_out = isl_basic_map_dim(bmap, isl_dim_out);
+       o_out = isl_basic_map_offset(bmap, isl_dim_out);
+
+       for (i = 0; i < n_out; ++i) {
+               for (j = 0; j < bmap->n_eq; ++j) {
+                       if (isl_int_is_zero(bmap->eq[j][o_out + i]))
+                               continue;
+                       if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
+                                               total - (o_out + i + 1)) == -1)
+                               break;
+               }
+               if (j >= bmap->n_eq)
+                       return 0;
+       }
+
+       return 1;
+}
+
+/* Check if the given map is obviously single-valued.
+ */
+int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
+{
+       int sv;
+
+       if (!map)
+               return -1;
+       if (map->n == 0)
+               return 1;
+       if (map->n >= 2)
+               return 0;
+
+       return isl_basic_map_fast_is_single_valued(map->p[0]);
+}
+
 /* Check if the given map is single-valued.
  * We simply compute
  *
@@ -7232,6 +7735,10 @@ int isl_map_is_single_valued(__isl_keep isl_map *map)
        isl_map *id;
        int sv;
 
+       sv = isl_map_fast_is_single_valued(map);
+       if (sv < 0 || sv)
+               return sv;
+
        test = isl_map_reverse(isl_map_copy(map));
        test = isl_map_apply_range(test, isl_map_copy(map));
 
@@ -7524,6 +8031,11 @@ error:
        return NULL;
 }
 
+__isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
+{
+       return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
+}
+
 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
 {
        int i;
@@ -7553,6 +8065,24 @@ error:
        return NULL;
 }
 
+__isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
+{
+       return (isl_set *)isl_map_flatten((isl_map *)set);
+}
+
+__isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
+{
+       isl_dim *dim, *flat_dim;
+       isl_map *map;
+
+       dim = isl_set_get_dim(set);
+       flat_dim = isl_dim_flatten(isl_dim_copy(dim));
+       map = map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
+       map = isl_map_intersect_domain(map, set);
+
+       return map;
+}
+
 /* Extend the given dim_map with mappings for the divs in bmap.
  */
 static __isl_give struct isl_dim_map *extend_dim_map(
@@ -7758,9 +8288,12 @@ __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
                                       eq->n_row, ineq->n_row);
        if (!bmap)
                goto error;
-       for (i = 0; i < extra; ++i)
-               if (isl_basic_map_alloc_div(bmap) < 0)
+       for (i = 0; i < extra; ++i) {
+               k = isl_basic_map_alloc_div(bmap);
+               if (k < 0)
                        goto error;
+               isl_int_set_si(bmap->div[k][0], 0);
+       }
        for (i = 0; i < eq->n_row; ++i) {
                l = isl_basic_map_alloc_equality(bmap);
                if (l < 0)
@@ -7800,6 +8333,22 @@ error:
        return NULL;
 }
 
+__isl_give isl_mat *isl_basic_set_equalities_matrix(
+       __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
+       enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
+{
+       return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
+                                               c1, c2, c3, c4, isl_dim_in);
+}
+
+__isl_give isl_mat *isl_basic_set_inequalities_matrix(
+       __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
+       enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
+{
+       return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
+                                                c1, c2, c3, c4, isl_dim_in);
+}
+
 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
        __isl_take isl_dim *dim,
        __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,