clean up isl_pw_qpolynomial_neg
[platform/upstream/isl.git] / isl_affine_hull.c
index cc99009..dcb3d18 100644 (file)
@@ -34,12 +34,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(
@@ -201,6 +206,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;
@@ -221,12 +229,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;
 }
 
@@ -251,7 +260,6 @@ static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
        struct isl_vec *sample = NULL;
        struct isl_tab_undo *snap;
        unsigned dim;
-       int k;
 
        if (!tab)
                return NULL;
@@ -287,7 +295,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;
@@ -318,6 +326,32 @@ 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
@@ -330,7 +364,7 @@ error:
 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)
@@ -358,8 +392,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)
@@ -370,6 +403,8 @@ static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
                        goto error;
                point = isl_basic_set_from_vec(sample);
                hull = affine_hull(hull, point);
+               if (!hull)
+                       return NULL;
        }
 
        return hull;
@@ -600,7 +635,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;
                }
        }
 
@@ -687,8 +723,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);
@@ -777,17 +816,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;
@@ -889,7 +932,8 @@ 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);
        return bmap;
 }
 
@@ -906,6 +950,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;
 
@@ -915,10 +962,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);