isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_convex_hull.c
index a644201..dc8ac91 100644 (file)
@@ -1,34 +1,25 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * 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/lp.h>
 #include <isl/map.h>
 #include <isl_mat_private.h>
 #include <isl/set.h>
 #include <isl/seq.h>
+#include <isl_options_private.h>
 #include "isl_equalities.h"
 #include "isl_tab.h"
 
 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
 
-static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
-{
-       isl_int *t;
-
-       if (i != j) {
-               t = bmap->ineq[i];
-               bmap->ineq[i] = bmap->ineq[j];
-               bmap->ineq[j] = t;
-       }
-}
-
 /* Return 1 if constraint c is redundant with respect to the constraints
  * in bmap.  If c is a lower [upper] bound in some variable and bmap
  * does not have a lower [upper] bound in that variable, then c cannot
@@ -103,7 +94,7 @@ __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
        if (bmap->n_ineq <= 1)
                return bmap;
 
-       tab = isl_tab_from_basic_map(bmap);
+       tab = isl_tab_from_basic_map(bmap, 0);
        if (isl_tab_detect_implicit_equalities(tab) < 0)
                goto error;
        if (isl_tab_detect_redundant(tab) < 0)
@@ -126,6 +117,19 @@ __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
                isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
 }
 
+/* Remove redundant constraints in each of the basic maps.
+ */
+__isl_give isl_map *isl_map_remove_redundancies(__isl_take isl_map *map)
+{
+       return isl_map_inline_foreach_basic_map(map,
+                                           &isl_basic_map_remove_redundancies);
+}
+
+__isl_give isl_set *isl_set_remove_redundancies(__isl_take isl_set *set)
+{
+       return isl_map_remove_redundancies(set);
+}
+
 /* Check if the set set is bound in the direction of the affine
  * constraint c and if so, set the constant term such that the
  * resulting constraint is a bounding constraint for the set.
@@ -174,41 +178,53 @@ error:
        return -1;
 }
 
-struct isl_basic_set *isl_basic_set_set_rational(struct isl_basic_set *bset)
+__isl_give isl_basic_map *isl_basic_map_set_rational(
+       __isl_take isl_basic_set *bmap)
 {
-       if (!bset)
+       if (!bmap)
                return NULL;
 
-       if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
-               return bset;
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
+               return bmap;
 
-       bset = isl_basic_set_cow(bset);
-       if (!bset)
+       bmap = isl_basic_map_cow(bmap);
+       if (!bmap)
                return NULL;
 
-       ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
+       ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
+
+       return isl_basic_map_finalize(bmap);
+}
 
-       return isl_basic_set_finalize(bset);
+__isl_give isl_basic_set *isl_basic_set_set_rational(
+       __isl_take isl_basic_set *bset)
+{
+       return isl_basic_map_set_rational(bset);
 }
 
-static struct isl_set *isl_set_set_rational(struct isl_set *set)
+__isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
 {
        int i;
 
-       set = isl_set_cow(set);
-       if (!set)
+       map = isl_map_cow(map);
+       if (!map)
                return NULL;
-       for (i = 0; i < set->n; ++i) {
-               set->p[i] = isl_basic_set_set_rational(set->p[i]);
-               if (!set->p[i])
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_set_rational(map->p[i]);
+               if (!map->p[i])
                        goto error;
        }
-       return set;
+       return map;
 error:
-       isl_set_free(set);
+       isl_map_free(map);
        return NULL;
 }
 
+__isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
+{
+       return isl_map_set_rational(set);
+}
+
 static struct isl_basic_set *isl_basic_set_add_equality(
        struct isl_basic_set *bset, isl_int *c)
 {
@@ -467,7 +483,7 @@ static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
        int i;
        unsigned dim = isl_set_n_dim(set);
        int is_bound;
-       isl_mat *bounds;
+       isl_mat *bounds = NULL;
 
        isl_assert(set->ctx, set->n > 0, goto error);
        bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
@@ -632,8 +648,8 @@ static struct isl_basic_set *extend(struct isl_basic_set *hull,
                if (!facet || !hull_facet)
                        goto error;
                hull = isl_basic_set_cow(hull);
-               hull = isl_basic_set_extend_dim(hull,
-                       isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
+               hull = isl_basic_set_extend_space(hull,
+                       isl_space_copy(hull->dim), 0, 0, facet->n_ineq);
                if (!hull)
                        goto error;
                for (j = 0; j < facet->n_ineq; ++j) {
@@ -787,13 +803,6 @@ error:
        return NULL;
 }
 
-/* Project out final n dimensions using Fourier-Motzkin */
-static struct isl_set *set_project_out(struct isl_ctx *ctx,
-       struct isl_set *set, unsigned n)
-{
-       return isl_set_remove_dims(set, isl_dim_set, isl_set_n_dim(set) - n, n);
-}
-
 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
 {
        struct isl_basic_set *convex_hull;
@@ -802,9 +811,9 @@ static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
                return NULL;
 
        if (isl_set_is_empty(set))
-               convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
+               convex_hull = isl_basic_set_empty(isl_space_copy(set->dim));
        else
-               convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
+               convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
        isl_set_free(set);
        return convex_hull;
 }
@@ -891,7 +900,7 @@ int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
 
        if (!bset)
                return -1;
-       if (isl_basic_set_fast_is_empty(bset))
+       if (isl_basic_set_plain_is_empty(bset))
                return 1;
 
        tab = isl_tab_from_recession_cone(bset, 1);
@@ -953,7 +962,7 @@ static struct isl_basic_set *induced_lineality_space(
                goto error;
 
        dim = isl_basic_set_total_dim(bset1);
-       lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
+       lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset1), 0,
                                        bset1->n_eq + bset2->n_eq,
                                        bset1->n_ineq + bset2->n_ineq);
        lin = isl_basic_set_set_rational(lin);
@@ -1031,7 +1040,7 @@ static struct isl_basic_set *modulo_lineality(struct isl_set *set,
        if (!set || !lin)
                goto error;
        lin_dim = total - lin->n_eq;
-       M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
+       M = isl_mat_sub_alloc6(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
        M = isl_mat_left_hermite(M, 0, &U, &Q);
        if (!M)
                goto error;
@@ -1067,7 +1076,7 @@ error:
 static struct isl_basic_set *valid_direction_lp(
        struct isl_basic_set *bset1, struct isl_basic_set *bset2)
 {
-       struct isl_dim *dim;
+       isl_space *dim;
        struct isl_basic_set *lp;
        unsigned d;
        int n;
@@ -1078,8 +1087,8 @@ static struct isl_basic_set *valid_direction_lp(
        d = 1 + isl_basic_set_total_dim(bset1);
        n = 2 +
            2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
-       dim = isl_dim_set_alloc(bset1->ctx, 0, n);
-       lp = isl_basic_set_alloc_dim(dim, 0, d, n);
+       dim = isl_space_set_alloc(bset1->ctx, 0, n);
+       lp = isl_basic_set_alloc_space(dim, 0, d, n);
        if (!lp)
                goto error;
        for (i = 0; i < n; ++i) {
@@ -1160,7 +1169,7 @@ static struct isl_vec *valid_direction(
                goto error;
        lp = valid_direction_lp(isl_basic_set_copy(bset1),
                                isl_basic_set_copy(bset2));
-       tab = isl_tab_from_basic_set(lp);
+       tab = isl_tab_from_basic_set(lp, 0);
        sample = isl_tab_get_sample_value(tab);
        isl_tab_free(tab);
        isl_basic_set_free(lp);
@@ -1314,7 +1323,7 @@ static struct isl_basic_set *convex_hull_pair_pointed(
 
        bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
        bset2 = homogeneous_map(bset2, T2);
-       set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
+       set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
        set = isl_set_add_basic_set(set, bset1);
        set = isl_set_add_basic_set(set, bset2);
        hull = uset_convex_hull(set);
@@ -1387,7 +1396,7 @@ static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
        }
        if (lin->n_eq < isl_basic_set_total_dim(lin)) {
                struct isl_set *set;
-               set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
+               set = isl_set_alloc_space(isl_basic_set_get_space(bset1), 2, 0);
                set = isl_set_add_basic_set(set, bset1);
                set = isl_set_add_basic_set(set, bset2);
                return modulo_lineality(set, lin);
@@ -1417,7 +1426,7 @@ struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
        isl_assert(bset->ctx, bset->n_div == 0, goto error);
        dim = isl_basic_set_total_dim(bset);
 
-       lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
+       lin = isl_basic_set_alloc_space(isl_basic_set_get_space(bset), 0, dim, 0);
        if (!lin)
                goto error;
        for (i = 0; i < bset->n_eq; ++i) {
@@ -1459,12 +1468,12 @@ static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
        if (!set)
                return NULL;
        if (set->n == 0) {
-               struct isl_dim *dim = isl_set_get_dim(set);
+               isl_space *dim = isl_set_get_space(set);
                isl_set_free(set);
                return isl_basic_set_empty(dim);
        }
 
-       lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
+       lin = isl_set_alloc_space(isl_set_get_space(set), set->n, 0);
        for (i = 0; i < set->n; ++i)
                lin = isl_set_add_basic_set(lin,
                    isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
@@ -1662,9 +1671,9 @@ static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
        if (isl_hash_table_init(hull->ctx, table, min_constraints))
                goto error;
 
-       total = isl_dim_total(set->dim);
+       total = isl_space_dim(set->dim, isl_dim_all);
        for (i = 0; i < set->p[best]->n_ineq; ++i) {
-               constraints[i].c = isl_mat_sub_alloc(hull->ctx,
+               constraints[i].c = isl_mat_sub_alloc6(hull->ctx,
                        set->p[best]->ineq + i, 0, 1, 0, 1 + total);
                if (!constraints[i].c)
                        goto error;
@@ -1759,7 +1768,7 @@ static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
                n_ineq += set->p[i]->n_eq;
                n_ineq += set->p[i]->n_ineq;
        }
-       hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
+       hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
        hull = isl_basic_set_set_rational(hull);
        if (!hull)
                return NULL;
@@ -1845,7 +1854,7 @@ static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
                goto error;
 
        if (isl_set_n_dim(set) == 0) {
-               convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
+               convex_hull = isl_basic_set_universe(isl_space_copy(set->dim));
                isl_set_free(set);
                convex_hull = isl_basic_set_set_rational(convex_hull);
                return convex_hull;
@@ -1858,6 +1867,7 @@ static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
        if (set->n == 1) {
                convex_hull = isl_basic_set_copy(set->p[0]);
                isl_set_free(set);
+               convex_hull = isl_basic_map_remove_redundancies(convex_hull);
                return convex_hull;
        }
        if (isl_set_n_dim(set) == 1)
@@ -2097,18 +2107,20 @@ error:
  * it can be relaxed (by increasing the constant term) to become
  * a bound for that basic set.  In the latter case, the constant
  * term is updated.
+ * Relaxation of the constant term is only allowed if "shift" is set.
+ *
  * Return 1 if "ineq" is a bound
  *       0 if "ineq" may attain arbitrarily small values on basic set "j"
  *      -1 if some error occurred
  */
 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
-                       isl_int *ineq)
+       isl_int *ineq, int shift)
 {
        enum isl_lp_result res;
        isl_int opt;
 
        if (!data->p[j].tab) {
-               data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
+               data->p[j].tab = isl_tab_from_basic_set(set->p[j], 0);
                if (!data->p[j].tab)
                        return -1;
        }
@@ -2117,8 +2129,12 @@ static int is_bound(struct sh_data *data, struct isl_set *set, int j,
 
        res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
                                &opt, NULL, 0);
-       if (res == isl_lp_ok && isl_int_is_neg(opt))
-               isl_int_sub(ineq[0], ineq[0], opt);
+       if (res == isl_lp_ok && isl_int_is_neg(opt)) {
+               if (shift)
+                       isl_int_sub(ineq[0], ineq[0], opt);
+               else
+                       res = isl_lp_unbounded;
+       }
 
        isl_int_clear(opt);
 
@@ -2126,9 +2142,9 @@ static int is_bound(struct sh_data *data, struct isl_set *set, int j,
               res == isl_lp_unbounded ? 0 : -1;
 }
 
-/* Check if inequality "ineq" from basic set "i" can be relaxed to
+/* Check if inequality "ineq" from basic set "i" is or can be relaxed to
  * become a bound on the whole set.  If so, add the (relaxed) inequality
- * to "hull".
+ * to "hull".  Relaxation is only allowed if "shift" is set.
  *
  * We first check if "hull" already contains a translate of the inequality.
  * If so, we are done.
@@ -2144,7 +2160,8 @@ static int is_bound(struct sh_data *data, struct isl_set *set, int j,
  * the inequality accordingly.
  */
 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
-       struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
+       struct sh_data *data, struct isl_set *set, int i, isl_int *ineq,
+       int shift)
 {
        uint32_t c_hash;
        struct ineq_cmp_data v;
@@ -2179,7 +2196,7 @@ static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
 
        for (j = 0; j < i; ++j) {
                int bound;
-               bound = is_bound(data, set, j, hull->ineq[k]);
+               bound = is_bound(data, set, j, hull->ineq[k], shift);
                if (bound < 0)
                        goto error;
                if (!bound)
@@ -2207,7 +2224,7 @@ static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
                                isl_int_neg(ineq_j[0], ineq_j[0]);
                        continue;
                }
-               bound = is_bound(data, set, j, hull->ineq[k]);
+               bound = is_bound(data, set, j, hull->ineq[k], shift);
                if (bound < 0)
                        goto error;
                if (!bound)
@@ -2230,12 +2247,12 @@ error:
        return NULL;
 }
 
-/* Check if any inequality from basic set "i" can be relaxed to
+/* Check if any inequality from basic set "i" is or can be relaxed to
  * become a bound on the whole set.  If so, add the (relaxed) inequality
- * to "hull".
+ * to "hull".  Relaxation is only allowed if "shift" is set.
  */
 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
-       struct sh_data *data, struct isl_set *set, int i)
+       struct sh_data *data, struct isl_set *set, int i, int shift)
 {
        int j, k;
        unsigned dim = isl_basic_set_total_dim(bset);
@@ -2243,18 +2260,21 @@ static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
        for (j = 0; j < set->p[i]->n_eq; ++j) {
                for (k = 0; k < 2; ++k) {
                        isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
-                       bset = add_bound(bset, data, set, i, set->p[i]->eq[j]);
+                       bset = add_bound(bset, data, set, i, set->p[i]->eq[j],
+                                           shift);
                }
        }
        for (j = 0; j < set->p[i]->n_ineq; ++j)
-               bset = add_bound(bset, data, set, i, set->p[i]->ineq[j]);
+               bset = add_bound(bset, data, set, i, set->p[i]->ineq[j], shift);
        return bset;
 }
 
 /* Compute a superset of the convex hull of set that is described
- * by only translates of the constraints in the constituents of set.
+ * by only (translates of) the constraints in the constituents of set.
+ * Translation is only allowed if "shift" is set.
  */
-static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
+static __isl_give isl_basic_set *uset_simple_hull(__isl_take isl_set *set,
+       int shift)
 {
        struct sh_data *data = NULL;
        struct isl_basic_set *hull = NULL;
@@ -2271,7 +2291,7 @@ static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
                n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
        }
 
-       hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
+       hull = isl_basic_set_alloc_space(isl_space_copy(set->dim), 0, 0, n_ineq);
        if (!hull)
                goto error;
 
@@ -2280,7 +2300,7 @@ static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
                goto error;
 
        for (i = 0; i < set->n; ++i)
-               hull = add_bounds(hull, data, set, i);
+               hull = add_bounds(hull, data, set, i, shift);
 
        sh_data_free(data);
        isl_set_free(set);
@@ -2294,9 +2314,11 @@ error:
 }
 
 /* Compute a superset of the convex hull of map that is described
- * by only translates of the constraints in the constituents of map.
+ * by only (translates of) the constraints in the constituents of map.
+ * Translation is only allowed if "shift" is set.
  */
-struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
+static __isl_give isl_basic_map *map_simple_hull(__isl_take isl_map *map,
+       int shift)
 {
        struct isl_set *set = NULL;
        struct isl_basic_map *model = NULL;
@@ -2320,28 +2342,54 @@ struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
        map = isl_map_detect_equalities(map);
        affine_hull = isl_map_affine_hull(isl_map_copy(map));
        map = isl_map_align_divs(map);
-       model = isl_basic_map_copy(map->p[0]);
+       model = map ? isl_basic_map_copy(map->p[0]) : NULL;
 
        set = isl_map_underlying_set(map);
 
-       bset = uset_simple_hull(set);
+       bset = uset_simple_hull(set, shift);
 
        hull = isl_basic_map_overlying_set(bset, model);
 
        hull = isl_basic_map_intersect(hull, affine_hull);
        hull = isl_basic_map_remove_redundancies(hull);
+
+       if (!hull)
+               return NULL;
        ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
        ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
 
        return hull;
 }
 
+/* Compute a superset of the convex hull of map that is described
+ * by only translates of the constraints in the constituents of map.
+ */
+__isl_give isl_basic_map *isl_map_simple_hull(__isl_take isl_map *map)
+{
+       return map_simple_hull(map, 1);
+}
+
 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
 {
        return (struct isl_basic_set *)
                isl_map_simple_hull((struct isl_map *)set);
 }
 
+/* Compute a superset of the convex hull of map that is described
+ * by only the constraints in the constituents of map.
+ */
+__isl_give isl_basic_map *isl_map_unshifted_simple_hull(
+       __isl_take isl_map *map)
+{
+       return map_simple_hull(map, 0);
+}
+
+__isl_give isl_basic_set *isl_set_unshifted_simple_hull(
+       __isl_take isl_set *set)
+{
+       return isl_map_unshifted_simple_hull(set);
+}
+
 /* Given a set "set", return parametric bounds on the dimension "dim".
  */
 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)