isl_qpolynomial_drop_dims: properly handle nested spaces
[platform/upstream/isl.git] / isl_affine_hull.c
index fa987be..84123aa 100644 (file)
@@ -1,12 +1,22 @@
-#include "isl_ctx.h"
-#include "isl_seq.h"
-#include "isl_set.h"
-#include "isl_lp.h"
-#include "isl_map.h"
-#include "isl_map_private.h"
+/*
+ * 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 <isl_ctx_private.h>
+#include <isl_map_private.h>
+#include <isl/seq.h>
+#include <isl/set.h>
+#include <isl/lp.h>
+#include <isl/map.h>
 #include "isl_equalities.h"
 #include "isl_sample.h"
 #include "isl_tab.h"
+#include <isl_mat_private.h>
 
 struct isl_basic_map *isl_basic_map_implicit_equalities(
                                                struct isl_basic_map *bmap)
@@ -25,12 +35,17 @@ struct isl_basic_map *isl_basic_map_implicit_equalities(
                return bmap;
 
        tab = isl_tab_from_basic_map(bmap);
-       tab = isl_tab_detect_implicit_equalities(tab);
+       if (isl_tab_detect_implicit_equalities(tab) < 0)
+               goto error;
        bmap = isl_basic_map_update_from_tab(bmap, tab);
        isl_tab_free(tab);
        bmap = isl_basic_map_gauss(bmap, NULL);
        ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
        return bmap;
+error:
+       isl_tab_free(tab);
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 struct isl_basic_set *isl_basic_set_implicit_equalities(
@@ -192,6 +207,9 @@ static struct isl_basic_set *affine_hull(
        int col;
        int row;
 
+       if (!bset1 || !bset2)
+               goto error;
+
        total = 1 + isl_basic_set_n_dim(bset1);
 
        row = 0;
@@ -212,12 +230,13 @@ static struct isl_basic_set *affine_hull(
                                --row;
                }
        }
-       isl_basic_set_free(bset2);
        isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
+       isl_basic_set_free(bset2);
        bset1 = isl_basic_set_normalize_constraints(bset1);
        return bset1;
 error:
        isl_basic_set_free(bset1);
+       isl_basic_set_free(bset2);
        return NULL;
 }
 
@@ -233,15 +252,15 @@ error:
  * integer points in the basic set represented by "tab"
  * yields another point inside the basic set.
  *
- * The caller of this function ensures that the tableau is bounded.
+ * The caller of this function ensures that the tableau is bounded or
+ * that tab->basis and tab->n_unbounded have been set appropriately.
  */
 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
 {
        struct isl_ctx *ctx;
-       struct isl_vec *sample;
+       struct isl_vec *sample = NULL;
        struct isl_tab_undo *snap;
        unsigned dim;
-       int k;
 
        if (!tab)
                return NULL;
@@ -253,9 +272,9 @@ static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
                return NULL;
        isl_int_set_si(sample->el[0], 1);
        isl_seq_combine(sample->el + 1,
-               ctx->one, tab->bset->sample->el + 1,
+               ctx->one, tab->bmap->sample->el + 1,
                up ? ctx->one : ctx->negone, eq + 1, dim);
-       if (isl_basic_set_contains(tab->bset, sample))
+       if (isl_basic_map_contains(tab->bmap, sample))
                return sample;
        isl_vec_free(sample);
        sample = NULL;
@@ -268,7 +287,8 @@ static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
 
        if (isl_tab_extend_cons(tab, 1) < 0)
                goto error;
-       tab = isl_tab_add_ineq(tab, eq);
+       if (isl_tab_add_ineq(tab, eq) < 0)
+               goto error;
 
        sample = isl_tab_sample(tab);
 
@@ -276,7 +296,7 @@ static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
        if (!up)
                isl_seq_neg(eq, eq, 1 + dim);
 
-       if (isl_tab_rollback(tab, snap) < 0)
+       if (sample && isl_tab_rollback(tab, snap) < 0)
                goto error;
 
        return sample;
@@ -307,18 +327,45 @@ error:
        return NULL;
 }
 
+__isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
+{
+       int i;
+
+       if (!set)
+               return NULL;
+       if (set->n == 0)
+               return set;
+
+       set = isl_set_remove_divs(set);
+       set = isl_set_cow(set);
+       if (!set)
+               return NULL;
+
+       for (i = 0; i < set->n; ++i) {
+               set->p[i] = isl_basic_set_recession_cone(set->p[i]);
+               if (!set->p[i])
+                       goto error;
+       }
+
+       return set;
+error:
+       isl_set_free(set);
+       return NULL;
+}
+
 /* Extend an initial (under-)approximation of the affine hull of basic
  * set represented by the tableau "tab"
  * by looking for points that do not satisfy one of the equalities
  * in the current approximation and adding them to that approximation
  * until no such points can be found any more.
  *
- * The caller of this function ensures that "tab" is bounded.
+ * The caller of this function ensures that "tab" is bounded or
+ * that tab->basis and tab->n_unbounded have been set appropriately.
  */
 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
        struct isl_basic_set *hull)
 {
-       int i, j, k;
+       int i, j;
        unsigned dim;
 
        if (!tab || !hull)
@@ -346,14 +393,19 @@ static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
                                break;
                        isl_vec_free(sample);
 
-                       tab = isl_tab_add_eq(tab, hull->eq[j]);
-                       if (!tab)
+                       if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
                                goto error;
                }
                if (j == hull->n_eq)
                        break;
+               if (tab->samples)
+                       tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
+               if (!tab)
+                       goto error;
                point = isl_basic_set_from_vec(sample);
                hull = affine_hull(hull, point);
+               if (!hull)
+                       return NULL;
        }
 
        return hull;
@@ -365,16 +417,19 @@ error:
 /* Drop all constraints in bset that involve any of the dimensions
  * first to first+n-1.
  */
-static struct isl_basic_set *drop_constraints_involving
-       (struct isl_basic_set *bset, unsigned first, unsigned n)
+__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
+       __isl_take isl_basic_set *bset, unsigned first, unsigned n)
 {
        int i;
 
-       if (!bset)
-               return NULL;
+       if (n == 0)
+               return bset;
 
        bset = isl_basic_set_cow(bset);
 
+       if (!bset)
+               return NULL;
+
        for (i = bset->n_eq - 1; i >= 0; --i) {
                if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
                        continue;
@@ -428,7 +483,13 @@ static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset
        tab = isl_tab_from_basic_set(bset);
        if (!tab)
                goto error;
-       tab->bset = isl_basic_set_copy(bset);
+       if (tab->empty) {
+               isl_tab_free(tab);
+               isl_vec_free(sample);
+               return isl_basic_set_set_to_empty(bset);
+       }
+       if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
+               goto error;
 
        if (!sample) {
                struct isl_tab_undo *snap;
@@ -436,8 +497,8 @@ static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset
                sample = isl_tab_sample(tab);
                if (isl_tab_rollback(tab, snap) < 0)
                        goto error;
-               isl_vec_free(tab->bset->sample);
-               tab->bset->sample = isl_vec_copy(sample);
+               isl_vec_free(tab->bmap->sample);
+               tab->bmap->sample = isl_vec_copy(sample);
        }
 
        if (!sample)
@@ -462,6 +523,135 @@ error:
        return NULL;
 }
 
+/* Given an unbounded tableau and an integer point satisfying the tableau,
+ * construct an initial affine hull containing the recession cone
+ * shifted to the given point.
+ *
+ * The unbounded directions are taken from the last rows of the basis,
+ * which is assumed to have been initialized appropriately.
+ */
+static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
+       __isl_take isl_vec *vec)
+{
+       int i;
+       int k;
+       struct isl_basic_set *bset = NULL;
+       struct isl_ctx *ctx;
+       unsigned dim;
+
+       if (!vec || !tab)
+               return NULL;
+       ctx = vec->ctx;
+       isl_assert(ctx, vec->size != 0, goto error);
+
+       bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
+       if (!bset)
+               goto error;
+       dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
+       for (i = 0; i < dim; ++i) {
+               k = isl_basic_set_alloc_equality(bset);
+               if (k < 0)
+                       goto error;
+               isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
+                           vec->size - 1);
+               isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
+                                     vec->size - 1, &bset->eq[k][0]);
+               isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
+       }
+       bset->sample = vec;
+       bset = isl_basic_set_gauss(bset, NULL);
+
+       return bset;
+error:
+       isl_basic_set_free(bset);
+       isl_vec_free(vec);
+       return NULL;
+}
+
+/* Given a tableau of a set and a tableau of the corresponding
+ * recession cone, detect and add all equalities to the tableau.
+ * If the tableau is bounded, then we can simply keep the
+ * tableau in its state after the return from extend_affine_hull.
+ * However, if the tableau is unbounded, then
+ * isl_tab_set_initial_basis_with_cone will add some additional
+ * constraints to the tableau that have to be removed again.
+ * In this case, we therefore rollback to the state before
+ * any constraints were added and then add the equalities back in.
+ */
+struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
+       struct isl_tab *tab_cone)
+{
+       int j;
+       struct isl_vec *sample;
+       struct isl_basic_set *hull;
+       struct isl_tab_undo *snap;
+
+       if (!tab || !tab_cone)
+               goto error;
+
+       snap = isl_tab_snap(tab);
+
+       isl_mat_free(tab->basis);
+       tab->basis = NULL;
+
+       isl_assert(tab->mat->ctx, tab->bmap, goto error);
+       isl_assert(tab->mat->ctx, tab->samples, goto error);
+       isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
+       isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
+
+       if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
+               goto error;
+
+       sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
+       if (!sample)
+               goto error;
+
+       isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
+
+       isl_vec_free(tab->bmap->sample);
+       tab->bmap->sample = isl_vec_copy(sample);
+
+       if (tab->n_unbounded == 0)
+               hull = isl_basic_set_from_vec(isl_vec_copy(sample));
+       else
+               hull = initial_hull(tab, isl_vec_copy(sample));
+
+       for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
+               isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
+               hull = affine_hull(hull,
+                               isl_basic_set_from_vec(isl_vec_copy(sample)));
+       }
+
+       isl_vec_free(sample);
+
+       hull = extend_affine_hull(tab, hull);
+       if (!hull)
+               goto error;
+
+       if (tab->n_unbounded == 0) {
+               isl_basic_set_free(hull);
+               return tab;
+       }
+
+       if (isl_tab_rollback(tab, snap) < 0)
+               goto error;
+
+       if (hull->n_eq > tab->n_zero) {
+               for (j = 0; j < hull->n_eq; ++j) {
+                       isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
+                       if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
+                               goto error;
+               }
+       }
+
+       isl_basic_set_free(hull);
+
+       return tab;
+error:
+       isl_tab_free(tab);
+       return NULL;
+}
+
 /* Compute the affine hull of "bset", where "cone" is the recession cone
  * of "bset".
  *
@@ -516,7 +706,8 @@ static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
        U = isl_mat_lin_to_aff(U);
        bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
 
-       bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
+       bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
+                                                       cone_dim);
        bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
 
        Q = isl_mat_lin_to_aff(Q);
@@ -537,8 +728,11 @@ static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
                else
                        isl_mat_free(U);
                hull = isl_basic_set_preimage(hull, Q);
-               isl_vec_free(hull->sample);
-               hull->sample = sample;
+               if (hull) {
+                       isl_vec_free(hull->sample);
+                       hull->sample = sample;
+               } else
+                       isl_vec_free(sample);
        }
 
        isl_basic_set_free(cone);
@@ -627,17 +821,21 @@ static struct isl_basic_set *equalities_in_underlying_set(
        if (!T2)
                return hull;
 
-       if (!hull)
+       if (!hull) {
                isl_mat_free(T1);
-       else {
+               isl_mat_free(T2);
+       } else {
                struct isl_vec *sample = isl_vec_copy(hull->sample);
                if (sample && sample->size > 0)
                        sample = isl_mat_vec_product(T1, sample);
                else
                        isl_mat_free(T1);
                hull = isl_basic_set_preimage(hull, T2);
-               isl_vec_free(hull->sample);
-               hull->sample = sample;
+               if (hull) {
+                       isl_vec_free(hull->sample);
+                       hull->sample = sample;
+               } else
+                       isl_vec_free(sample);
        }
 
        return hull;
@@ -739,7 +937,9 @@ struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
 {
        bmap = isl_basic_map_detect_equalities(bmap);
        bmap = isl_basic_map_cow(bmap);
-       isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+       if (bmap)
+               isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+       bmap = isl_basic_map_finalize(bmap);
        return bmap;
 }
 
@@ -756,6 +956,9 @@ struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
        struct isl_basic_map *hull = NULL;
        struct isl_set *set;
 
+       map = isl_map_detect_equalities(map);
+       map = isl_map_align_divs(map);
+
        if (!map)
                return NULL;
 
@@ -765,10 +968,6 @@ struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
                return hull;
        }
 
-       map = isl_map_detect_equalities(map);
-       map = isl_map_align_divs(map);
-       if (!map)
-               return NULL;
        model = isl_basic_map_copy(map->p[0]);
        set = isl_map_underlying_set(map);
        set = isl_set_cow(set);