isl_convex_hull.c: uset_convex_hull_wrap_bounded: fix error handling
[platform/upstream/isl.git] / isl_convex_hull.c
index 17212c3..f8565e0 100644 (file)
@@ -103,7 +103,8 @@ struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
                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;
        if (isl_tab_detect_redundant(tab) < 0)
                goto error;
        bmap = isl_basic_map_update_from_tab(bmap, tab);
@@ -212,6 +213,9 @@ static struct isl_basic_set *isl_basic_set_add_equality(
        int i;
        unsigned dim;
 
+       if (!bset)
+               return NULL;
+
        if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
                return bset;
 
@@ -377,6 +381,7 @@ isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
        isl_int *facet, isl_int *ridge)
 {
        int i;
+       isl_ctx *ctx;
        struct isl_mat *T = NULL;
        struct isl_basic_set *lp = NULL;
        struct isl_vec *obj;
@@ -384,11 +389,14 @@ isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
        isl_int num, den;
        unsigned dim;
 
+       if (!set)
+               return NULL;
+       ctx = set->ctx;
        set = isl_set_copy(set);
        set = isl_set_set_rational(set);
 
        dim = 1 + isl_set_n_dim(set);
-       T = isl_mat_alloc(set->ctx, 3, dim);
+       T = isl_mat_alloc(ctx, 3, dim);
        if (!T)
                goto error;
        isl_int_set_si(T->row[0][0], 1);
@@ -401,7 +409,7 @@ isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
        if (!set)
                goto error;
        lp = wrap_constraints(set);
-       obj = isl_vec_alloc(set->ctx, 1 + dim*set->n);
+       obj = isl_vec_alloc(ctx, 1 + dim*set->n);
        if (!obj)
                goto error;
        isl_int_set_si(obj->block.data[0], 0);
@@ -413,7 +421,7 @@ isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
        isl_int_init(num);
        isl_int_init(den);
        res = isl_basic_set_solve_lp(lp, 0,
-                           obj->block.data, set->ctx->one, &num, &den, NULL);
+                           obj->block.data, ctx->one, &num, &den, NULL);
        if (res == isl_lp_ok) {
                isl_int_neg(num, num);
                isl_seq_combine(facet, num, facet, den, ridge, dim);
@@ -423,7 +431,9 @@ isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
        isl_vec_free(obj);
        isl_basic_set_free(lp);
        isl_set_free(set);
-       isl_assert(set->ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
+       if (res == isl_lp_error)
+               return NULL;
+       isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
                   return NULL);
        return facet;
 error:
@@ -450,7 +460,6 @@ static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
 {
        struct isl_set *slice = NULL;
        struct isl_basic_set *face = NULL;
-       struct isl_mat *m, *U, *Q;
        int i;
        unsigned dim = isl_set_n_dim(set);
        int is_bound;
@@ -559,7 +568,8 @@ static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
        set = isl_set_preimage(set, U);
        facet = uset_convex_hull_wrap_bounded(set);
        facet = isl_basic_set_preimage(facet, Q);
-       isl_assert(ctx, facet->n_eq == 0, goto error);
+       if (facet)
+               isl_assert(ctx, facet->n_eq == 0, goto error);
        return facet;
 error:
        isl_basic_set_free(facet);
@@ -864,7 +874,9 @@ error:
        return NULL;
 }
 
-static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
+/* Is the set bounded for each value of the parameters?
+ */
+int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
 {
        struct isl_tab *tab;
        int bounded;
@@ -874,16 +886,21 @@ static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
        if (isl_basic_set_fast_is_empty(bset))
                return 1;
 
-       tab = isl_tab_from_recession_cone(bset);
+       tab = isl_tab_from_recession_cone(bset, 1);
        bounded = isl_tab_cone_is_bounded(tab);
        isl_tab_free(tab);
        return bounded;
 }
 
+/* Is the set bounded for each value of the parameters?
+ */
 int isl_set_is_bounded(__isl_keep isl_set *set)
 {
        int i;
 
+       if (!set)
+               return -1;
+
        for (i = 0; i < set->n; ++i) {
                int bounded = isl_basic_set_is_bounded(set->p[i]);
                if (!bounded || bounded < 0)
@@ -1791,6 +1808,9 @@ static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
 {
        struct isl_basic_set *convex_hull = NULL;
 
+       if (!set)
+               goto error;
+
        if (isl_set_n_dim(set) == 0) {
                convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
                isl_set_free(set);
@@ -1799,9 +1819,6 @@ static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
        }
 
        set = isl_set_set_rational(set);
-
-       if (!set)
-               goto error;
        set = isl_set_coalesce(set);
        if (!set)
                goto error;
@@ -1890,6 +1907,8 @@ struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
        }
 
        convex_hull = isl_basic_map_overlying_set(bset, model);
+       if (!convex_hull)
+               return NULL;
 
        ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
        ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);