declare isl_basic_map_remove_divs_involving_dims
[platform/upstream/isl.git] / isl_affine_hull.c
index a133598..91bb365 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 MIT 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)
@@ -24,13 +34,18 @@ struct isl_basic_map *isl_basic_map_implicit_equalities(
        if (bmap->n_ineq <= 1)
                return bmap;
 
-       tab = isl_tab_from_basic_map(bmap);
-       tab = isl_tab_detect_implicit_equalities(tab);
+       tab = isl_tab_from_basic_map(bmap, 0);
+       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;
 }
 
@@ -239,10 +258,9 @@ error:
 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;
@@ -254,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;
@@ -269,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);
 
@@ -277,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;
@@ -308,6 +327,98 @@ 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;
+}
+
+/* Move "sample" to a point that is one up (or down) from the original
+ * point in dimension "pos".
+ */
+static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
+{
+       if (up)
+               isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
+       else
+               isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
+}
+
+/* Check if any points that are adjacent to "sample" also belong to "bset".
+ * If so, add them to "hull" and return the updated hull.
+ *
+ * Before checking whether and adjacent point belongs to "bset", we first
+ * check whether it already belongs to "hull" as this test is typically
+ * much cheaper.
+ */
+static __isl_give isl_basic_set *add_adjacent_points(
+       __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
+       __isl_keep isl_basic_set *bset)
+{
+       int i, up;
+       int dim;
+
+       if (!sample)
+               goto error;
+
+       dim = isl_basic_set_dim(hull, isl_dim_set);
+
+       for (i = 0; i < dim; ++i) {
+               for (up = 0; up <= 1; ++up) {
+                       int contains;
+                       isl_basic_set *point;
+
+                       adjacent_point(sample, i, up);
+                       contains = isl_basic_set_contains(hull, sample);
+                       if (contains < 0)
+                               goto error;
+                       if (contains) {
+                               adjacent_point(sample, i, !up);
+                               continue;
+                       }
+                       contains = isl_basic_set_contains(bset, sample);
+                       if (contains < 0)
+                               goto error;
+                       if (contains) {
+                               point = isl_basic_set_from_vec(
+                                                       isl_vec_copy(sample));
+                               hull = affine_hull(hull, point);
+                       }
+                       adjacent_point(sample, i, !up);
+                       if (contains)
+                               break;
+               }
+       }
+
+       isl_vec_free(sample);
+
+       return hull;
+error:
+       isl_vec_free(sample);
+       isl_basic_set_free(hull);
+       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
@@ -316,11 +427,15 @@ error:
  *
  * The caller of this function ensures that "tab" is bounded or
  * that tab->basis and tab->n_unbounded have been set appropriately.
+ *
+ * "bset" may be either NULL or the basic set represented by "tab".
+ * If "bset" is not NULL, we check for any point we find if any
+ * of its adjacent points also belong to "bset".
  */
-static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
-       struct isl_basic_set *hull)
+static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
+       __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
 {
-       int i, j, k;
+       int i, j;
        unsigned dim;
 
        if (!tab || !hull)
@@ -348,8 +463,7 @@ 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)
@@ -358,8 +472,13 @@ static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
                        tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
                if (!tab)
                        goto error;
+               if (bset)
+                       hull = add_adjacent_points(hull, isl_vec_copy(sample),
+                                                   bset);
                point = isl_basic_set_from_vec(sample);
                hull = affine_hull(hull, point);
+               if (!hull)
+                       return NULL;
        }
 
        return hull;
@@ -371,16 +490,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;
@@ -396,6 +518,20 @@ static struct isl_basic_set *drop_constraints_involving
        return bset;
 }
 
+/* Construct an initial underapproximatino of the hull of "bset"
+ * from "sample" and any of its adjacent points that also belong to "bset".
+ */
+static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
+       __isl_take isl_vec *sample)
+{
+       isl_basic_set *hull;
+
+       hull = isl_basic_set_from_vec(isl_vec_copy(sample));
+       hull = add_adjacent_points(hull, sample, bset);
+
+       return hull;
+}
+
 /* Look for all equalities satisfied by the integer points in bset,
  * which is assumed to be bounded.
  *
@@ -412,7 +548,7 @@ static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset
        struct isl_tab *tab = NULL;
        unsigned dim;
 
-       if (isl_basic_set_fast_is_empty(bset))
+       if (isl_basic_set_plain_is_empty(bset))
                return bset;
 
        dim = isl_basic_set_n_dim(bset);
@@ -431,10 +567,14 @@ static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset
                }
        }
 
-       tab = isl_tab_from_basic_set(bset);
+       tab = isl_tab_from_basic_set(bset, 1);
        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 (!sample) {
                struct isl_tab_undo *snap;
@@ -442,8 +582,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)
@@ -454,10 +594,10 @@ static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset
                return isl_basic_set_set_to_empty(bset);
        }
 
-       hull = isl_basic_set_from_vec(sample);
+       hull = initialize_hull(bset, sample);
 
+       hull = extend_affine_hull(tab, hull, bset);
        isl_basic_set_free(bset);
-       hull = extend_affine_hull(tab, hull);
        isl_tab_free(tab);
 
        return hull;
@@ -469,7 +609,7 @@ error:
 }
 
 /* Given an unbounded tableau and an integer point satisfying the tableau,
- * construct an intial affine hull containing the recession cone
+ * 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,
@@ -521,7 +661,7 @@ error:
  * 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 eqaulities back in.
+ * 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)
@@ -539,7 +679,7 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
        isl_mat_free(tab->basis);
        tab->basis = NULL;
 
-       isl_assert(tab->mat->ctx, tab->bset, goto error);
+       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);
@@ -553,8 +693,8 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
 
        isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
 
-       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 (tab->n_unbounded == 0)
                hull = isl_basic_set_from_vec(isl_vec_copy(sample));
@@ -569,7 +709,7 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
 
        isl_vec_free(sample);
 
-       hull = extend_affine_hull(tab, hull);
+       hull = extend_affine_hull(tab, hull, NULL);
        if (!hull)
                goto error;
 
@@ -584,7 +724,8 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
        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);
-                       tab = isl_tab_add_eq(tab, hull->eq[j]);
+                       if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
+                               goto error;
                }
        }
 
@@ -641,7 +782,7 @@ static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
        total = isl_basic_set_total_dim(cone);
        cone_dim = total - cone->n_eq;
 
-       M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
+       M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
        M = isl_mat_left_hermite(M, 0, &U, &Q);
        if (!M)
                goto error;
@@ -650,7 +791,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);
@@ -671,8 +813,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);
@@ -704,7 +849,7 @@ static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
 {
        struct isl_basic_set *cone;
 
-       if (isl_basic_set_fast_is_empty(bset))
+       if (isl_basic_set_plain_is_empty(bset))
                return bset;
 
        cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
@@ -761,17 +906,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;
@@ -809,7 +958,7 @@ struct isl_basic_map *isl_basic_map_detect_equalities(
                isl_basic_set_free(hull);
                return isl_basic_map_set_to_empty(bmap);
        }
-       bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
+       bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
                                        hull->n_eq, 0);
        for (i = 0; i < hull->n_eq; ++i) {
                j = isl_basic_map_alloc_equality(bmap);
@@ -837,7 +986,8 @@ __isl_give isl_basic_set *isl_basic_set_detect_equalities(
                isl_basic_map_detect_equalities((isl_basic_map *)bset);
 }
 
-struct isl_map *isl_map_detect_equalities(struct isl_map *map)
+__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
+       __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
 {
        struct isl_basic_map *bmap;
        int i;
@@ -847,7 +997,7 @@ struct isl_map *isl_map_detect_equalities(struct isl_map *map)
 
        for (i = 0; i < map->n; ++i) {
                bmap = isl_basic_map_copy(map->p[i]);
-               bmap = isl_basic_map_detect_equalities(bmap);
+               bmap = fn(bmap);
                if (!bmap)
                        goto error;
                isl_basic_map_free(map->p[i]);
@@ -860,6 +1010,12 @@ error:
        return NULL;
 }
 
+__isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
+{
+       return isl_map_inline_foreach_basic_map(map,
+                                           &isl_basic_map_detect_equalities);
+}
+
 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
 {
        return (isl_set *)isl_map_detect_equalities((isl_map *)set);
@@ -873,7 +1029,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;
 }
 
@@ -890,6 +1048,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;
 
@@ -899,10 +1060,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);