isl_qpolynomial_drop_dims: properly handle nested spaces
[platform/upstream/isl.git] / isl_affine_hull.c
index 8356446..84123aa 100644 (file)
@@ -7,15 +7,16 @@
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  */
 
-#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"
+#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)
@@ -295,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;
@@ -403,6 +404,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;
@@ -414,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;
@@ -518,7 +524,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,
@@ -570,7 +576,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)
@@ -700,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);
@@ -721,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);
@@ -811,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;
@@ -925,6 +939,7 @@ struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
        bmap = isl_basic_map_cow(bmap);
        if (bmap)
                isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+       bmap = isl_basic_map_finalize(bmap);
        return bmap;
 }