add isl_tab_product
[platform/upstream/isl.git] / isl_affine_hull.c
index 323255d..7035e9e 100644 (file)
@@ -6,58 +6,56 @@
 #include "isl_map_private.h"
 #include "isl_equalities.h"
 #include "isl_sample.h"
+#include "isl_tab.h"
 
 struct isl_basic_map *isl_basic_map_implicit_equalities(
                                                struct isl_basic_map *bmap)
 {
-       int i;
-       int rational;
-       isl_int opt;
-       isl_int opt_denom;
-       struct isl_ctx *ctx;
+       struct isl_tab *tab;
 
        if (!bmap)
                return bmap;
 
-       if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
+       bmap = isl_basic_map_gauss(bmap, NULL);
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
                return bmap;
-       if (F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
+               return bmap;
+       if (bmap->n_ineq <= 1)
                return bmap;
 
-       ctx = bmap->ctx;
-       rational = F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
-       isl_int_init(opt);
-       isl_int_init(opt_denom);
-       if (!rational)
-               isl_int_set_si(opt_denom, 1);
-       for (i = 0; i < bmap->n_ineq; ++i) {
-               enum isl_lp_result res;
-               res = isl_solve_lp(bmap, 1, bmap->ineq[i]+1, ctx->one,
-                                       &opt, rational ? &opt_denom : NULL);
-               if (res == isl_lp_unbounded)
-                       continue;
-               if (res == isl_lp_error)
+       tab = isl_tab_from_basic_map(bmap);
+       tab = isl_tab_detect_implicit_equalities(tab);
+       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;
+}
+
+struct isl_basic_set *isl_basic_set_implicit_equalities(
+                                               struct isl_basic_set *bset)
+{
+       return (struct isl_basic_set *)
+               isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
+}
+
+struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
+{
+       int i;
+
+       if (!map)
+               return map;
+
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
+               if (!map->p[i])
                        goto error;
-               if (res == isl_lp_empty) {
-                       bmap = isl_basic_map_set_to_empty(bmap);
-                       break;
-               }
-               if (!isl_int_is_one(opt_denom))
-                       continue;
-               isl_int_add(opt, opt, bmap->ineq[i][0]);
-               if (isl_int_is_zero(opt)) {
-                       isl_basic_map_inequality_to_equality(bmap, i);
-                       --i;
-               }
        }
-       isl_int_clear(opt_denom);
-       isl_int_clear(opt);
 
-       F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
-       return bmap;
+       return map;
 error:
-       isl_int_clear(opt);
-       isl_basic_map_free(bmap);
+       isl_map_free(map);
        return NULL;
 }
 
@@ -215,52 +213,27 @@ static struct isl_basic_set *affine_hull(
                }
        }
        isl_basic_set_free(bset2);
-       isl_assert(ctx, row == bset1->n_eq, goto error);
+       isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
+       bset1 = isl_basic_set_normalize_constraints(bset1);
        return bset1;
 error:
        isl_basic_set_free(bset1);
        return NULL;
 }
 
-static struct isl_basic_set *isl_basic_set_from_vec(struct isl_ctx *ctx,
-       struct isl_vec *vec)
-{
-       int i;
-       int k;
-       struct isl_basic_set *bset = NULL;
-       unsigned dim;
-
-       if (!vec)
-               return NULL;
-       isl_assert(ctx, vec->size != 0, goto error);
-
-       bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
-       if (!bset)
-               goto error;
-       dim = isl_basic_set_n_dim(bset);
-       for (i = dim - 1; i >= 0; --i) {
-               k = isl_basic_set_alloc_equality(bset);
-               if (k < 0)
-                       goto error;
-               isl_seq_clr(bset->eq[k], 1 + dim);
-               isl_int_neg(bset->eq[k][0], vec->block.data[1 + i]);
-               isl_int_set(bset->eq[k][1 + i], vec->block.data[0]);
-       }
-       isl_vec_free(ctx, vec);
-
-       return bset;
-error:
-       isl_basic_set_free(bset);
-       isl_vec_free(ctx, vec);
-       return NULL;
-}
-
 /* Find an integer point in "bset" that lies outside of the equality
  * "eq" e(x) = 0.
  * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
  * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
  * The point, if found, is returned as a singleton set.
  * If no point can be found, the empty set is returned.
+ *
+ * Before solving an ILP problem, we first check if simply
+ * adding the normal of the constraint to one of the known
+ * integer points in the basic set yields another point
+ * inside the basic set.
+ *
+ * The caller of this function ensures that "bset" is bounded.
  */
 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
        struct isl_basic_set *bset, isl_int *eq, int up)
@@ -271,10 +244,23 @@ static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
        unsigned dim;
        int k;
 
+       dim = isl_basic_set_n_dim(bset);
+       sample = isl_vec_alloc(ctx, 1 + dim);
+       if (!sample)
+               return NULL;
+       isl_int_set_si(sample->block.data[0], 1);
+       isl_seq_combine(sample->block.data + 1,
+               ctx->one, bset->sample->block.data + 1,
+               up ? ctx->one : ctx->negone, eq + 1, dim);
+       if (isl_basic_set_contains(bset, sample))
+               return isl_basic_set_from_vec(sample);
+       isl_vec_free(sample);
+       sample = NULL;
+
        slice = isl_basic_set_copy(bset);
        if (!slice)
                goto error;
-       dim = isl_basic_set_n_dim(slice);
+       slice = isl_basic_set_cow(slice);
        slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
        k = isl_basic_set_alloc_inequality(slice);
        if (k < 0)
@@ -285,14 +271,14 @@ static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
                isl_seq_neg(slice->ineq[k], eq, 1 + dim);
        isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
 
-       sample = isl_basic_set_sample(slice);
+       sample = isl_basic_set_sample_bounded(slice);
        if (!sample)
                goto error;
        if (sample->size == 0) {
-               isl_vec_free(ctx, sample);
+               isl_vec_free(sample);
                point = isl_basic_set_empty_like(bset);
        } else
-               point = isl_basic_set_from_vec(ctx, sample);
+               point = isl_basic_set_from_vec(sample);
 
        return point;
 error:
@@ -300,46 +286,43 @@ error:
        return NULL;
 }
 
-/* After computing the rational affine hull (by detecting the implicit
- * equalities), we remove all equalities found so far, compute
- * the integer affine hull of what is left, and then add the original
- * equalities back in.
- *
- * The integer affine hull is constructed by successively looking
- * a point that is affinely independent of the points found so far.
- * In particular, for each equality satisfied by the points so far,
- * we check if there is any point on the corresponding hyperplane
- * shifted by one (in either direction).
- */
-struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
+struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
 {
-       int i, j;
-       struct isl_mat *T2 = NULL;
-       struct isl_basic_set *bset = NULL;
-       struct isl_basic_set *hull = NULL;
-       struct isl_vec *sample;
-       struct isl_ctx *ctx;
-       unsigned dim;
+       int i;
 
-       bmap = isl_basic_map_implicit_equalities(bmap);
-       if (!bmap)
+       bset = isl_basic_set_cow(bset);
+       if (!bset)
                return NULL;
-       ctx = bmap->ctx;
-       if (bmap->n_ineq == 0)
-               return bmap;
+       isl_assert(bset->ctx, bset->n_div == 0, goto error);
 
-       if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
-               bmap = isl_basic_map_cow(bmap);
-               isl_basic_map_free_inequality(bmap, bmap->n_ineq);
-               return bmap;
-       }
+       for (i = 0; i < bset->n_eq; ++i)
+               isl_int_set_si(bset->eq[i][0], 0);
 
-       bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
-       bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
+       for (i = 0; i < bset->n_ineq; ++i)
+               isl_int_set_si(bset->ineq[i][0], 0);
 
-       sample = isl_basic_set_sample(isl_basic_set_copy(bset));
-       hull = isl_basic_set_from_vec(ctx, sample);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
+       return isl_basic_set_implicit_equalities(bset);
+error:
+       isl_basic_set_free(bset);
+       return NULL;
+}
+
+/* Extend an initial (under-)approximation of the affine hull of "bset"
+ * by looking for points that do not satisfy one of the equalities
+ * in the current approximation and adding them to that approximation
+ * until no such points can be found any more.
+ *
+ * The caller of this function ensures that "bset" is bounded.
+ */
+static struct isl_basic_set *extend_affine_hull(struct isl_basic_set *bset,
+       struct isl_basic_set *hull)
+{
+       int i, j, k;
+       struct isl_ctx *ctx;
+       unsigned dim;
 
+       ctx = bset->ctx;
        dim = isl_basic_set_n_dim(bset);
        for (i = 0; i < dim; ++i) {
                struct isl_basic_set *point;
@@ -347,42 +330,341 @@ struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
                        point = outside_point(ctx, bset, hull->eq[j], 1);
                        if (!point)
                                goto error;
-                       if (!F_ISSET(point, ISL_BASIC_SET_EMPTY))
+                       if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
                                break;
                        isl_basic_set_free(point);
                        point = outside_point(ctx, bset, hull->eq[j], 0);
                        if (!point)
                                goto error;
-                       if (!F_ISSET(point, ISL_BASIC_SET_EMPTY))
+                       if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
                                break;
                        isl_basic_set_free(point);
+
+                       bset = isl_basic_set_extend_constraints(bset, 1, 0);
+                       k = isl_basic_set_alloc_equality(bset);
+                       if (k < 0)
+                               goto error;
+                       isl_seq_cpy(bset->eq[k], hull->eq[j],
+                                       1 + isl_basic_set_total_dim(hull));
+                       bset = isl_basic_set_gauss(bset, NULL);
+                       if (!bset)
+                               goto error;
                }
                if (j == hull->n_eq)
                        break;
                hull = affine_hull(hull, point);
        }
+       isl_basic_set_free(bset);
 
+       return hull;
+error:
        isl_basic_set_free(bset);
-       bset = NULL;
-       bmap = isl_basic_map_cow(bmap);
-       if (!bmap)
+       isl_basic_set_free(hull);
+       return NULL;
+}
+
+/* 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)
+{
+       int i;
+
+       if (!bset)
+               return NULL;
+
+       bset = isl_basic_set_cow(bset);
+
+       for (i = bset->n_eq - 1; i >= 0; --i) {
+               if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
+                       continue;
+               isl_basic_set_drop_equality(bset, i);
+       }
+
+       for (i = bset->n_ineq - 1; i >= 0; --i) {
+               if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
+                       continue;
+               isl_basic_set_drop_inequality(bset, i);
+       }
+
+       return bset;
+}
+
+/* Compute the affine hull of "bset", where "hull" is an initial approximation
+ * with only a single point of "bset" and "cone" is the recession cone
+ * of "bset".
+ *
+ * We first compute a unimodular transformation that puts the unbounded
+ * directions in the last dimensions.  In particular, we take a transformation
+ * that maps all equalities to equalities (in HNF) on the first dimensions.
+ * Let x be the original dimensions and y the transformed, with y_1 bounded
+ * and y_2 unbounded.
+ *
+ *            [ y_1 ]                  [ y_1 ]   [ Q_1 ]
+ *     x = U  [ y_2 ]                  [ y_2 ] = [ Q_2 ] x
+ *
+ * Let's call the input basic set S and the initial hull H.
+ * We compute S' = preimage(S, U) and H' = preimage(H, U)
+ * and drop the final dimensions including any constraints involving them.
+ * This results in sets S'' and H''.
+ * Then we extend H'' to the affine hull A'' of S''.
+ * Let F y_1 >= g be the constraint system of A''.  In the transformed
+ * space the y_2 are unbounded, so we can add them back without any constraints,
+ * resulting in
+ *
+ *                     [ y_1 ]
+ *             [ F 0 ] [ y_2 ] >= g
+ * or
+ *                     [ Q_1 ]
+ *             [ F 0 ] [ Q_2 ] x >= g
+ * or
+ *             F Q_1 x >= g
+ *
+ * The affine hull in the original space is then obtained as
+ * A = preimage(A'', Q_1).
+ */
+static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
+       struct isl_basic_set *hull, struct isl_basic_set *cone)
+{
+       unsigned total;
+       unsigned cone_dim;
+       struct isl_mat *M, *U, *Q;
+
+       if (!bset || !hull || !cone)
                goto error;
-       isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+
+       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_left_hermite(M, 0, &U, &Q);
+       if (!M)
+               goto error;
+       isl_mat_free(M);
+
+       U = isl_mat_lin_to_aff(U);
+       bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
+       hull = isl_basic_set_preimage(hull, U);
+
+       bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
+       bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
+       hull = drop_constraints_involving(hull, total - cone_dim, cone_dim);
+       hull = isl_basic_set_drop_dims(hull, total - cone_dim, cone_dim);
+
+       Q = isl_mat_lin_to_aff(Q);
+       Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
+
+       if (bset && bset->sample)
+               bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
+
+       hull = extend_affine_hull(bset, hull);
+
+       hull = isl_basic_set_preimage(hull, Q);
+
+       isl_basic_set_free(cone);
+
+       return hull;
+error:
+       isl_basic_set_free(bset);
+       isl_basic_set_free(hull);
+       isl_basic_set_free(cone);
+       return NULL;
+}
+
+/* Look for all equalities satisfied by the integer points in bset,
+ * which is assumed not to have any explicit equalities.
+ *
+ * The equalities are obtained by successively looking for
+ * a point that is affinely independent of the points found so far.
+ * In particular, for each equality satisfied by the points so far,
+ * we check if there is any point on a hyperplane parallel to the
+ * corresponding hyperplane shifted by at least one (in either direction).
+ *
+ * Before looking for any outside points, we first compute the recession
+ * cone.  The directions of this recession cone will always be part
+ * of the affine hull, so there is no need for looking for any points
+ * in these directions.
+ * In particular, if the recession cone is full-dimensional, then
+ * the affine hull is simply the whole universe.
+ */
+static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
+{
+       struct isl_basic_set *hull = NULL;
+       struct isl_vec *sample = NULL;
+       struct isl_basic_set *cone;
+
+       if (isl_basic_set_is_empty(bset))
+               return bset;
+
+       sample = isl_basic_set_sample_vec(isl_basic_set_copy(bset));
+       if (!sample)
+               goto error;
+       if (sample->size == 0) {
+               isl_vec_free(sample);
+               hull = isl_basic_set_empty_like(bset);
+               isl_basic_set_free(bset);
+               return hull;
+       }
+       if (sample->size == 1) {
+               isl_vec_free(sample);
+               return bset;
+       }
+
+       cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
+       if (!cone)
+               goto error;
+       if (cone->n_eq == 0) {
+               isl_basic_set_free(cone);
+               isl_vec_free(sample);
+               hull = isl_basic_set_universe_like(bset);
+               isl_basic_set_free(bset);
+               return hull;
+       }
+
+       hull = isl_basic_set_from_vec(sample);
+       if (cone->n_eq < isl_basic_set_total_dim(cone))
+               return affine_hull_with_cone(bset, hull, cone);
+
+       isl_basic_set_free(cone);
+       return extend_affine_hull(bset, hull);
+error:
+       isl_vec_free(sample);
+       isl_basic_set_free(bset);
+       isl_basic_set_free(hull);
+       return NULL;
+}
+
+/* Look for all equalities satisfied by the integer points in bmap
+ * that are independent of the equalities already explicitly available
+ * in bmap.
+ *
+ * We first remove all equalities already explicitly available,
+ * then look for additional equalities in the reduced space
+ * and then transform the result to the original space.
+ * The original equalities are _not_ added to this set.  This is
+ * the responsibility of the calling function.
+ * The resulting basic set has all meaning about the dimensions removed.
+ * In particular, dimensions that correspond to existential variables
+ * in bmap and that are found to be fixed are not removed.
+ */
+static struct isl_basic_set *equalities_in_underlying_set(
+                                               struct isl_basic_map *bmap)
+{
+       struct isl_mat *T2 = NULL;
+       struct isl_basic_set *bset = NULL;
+       struct isl_basic_set *hull = NULL;
+
+       bset = isl_basic_map_underlying_set(bmap);
+       bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
+       if (!bset)
+               goto error;
+
+       hull = uset_affine_hull(bset);
        if (T2)
-               hull = isl_basic_set_preimage(ctx, hull, T2);
-       bmap = isl_basic_map_intersect(bmap,
-                                       isl_basic_map_overlying_set(hull,
-                                               isl_basic_map_copy(bmap)));
+               hull = isl_basic_set_preimage(hull, T2);
 
-       return isl_basic_map_finalize(bmap);
+       return hull;
 error:
-       isl_mat_free(ctx, T2);
+       isl_mat_free(T2);
        isl_basic_set_free(bset);
        isl_basic_set_free(hull);
+       return NULL;
+}
+
+/* Detect and make explicit all equalities satisfied by the (integer)
+ * points in bmap.
+ */
+struct isl_basic_map *isl_basic_map_detect_equalities(
+                                               struct isl_basic_map *bmap)
+{
+       int i, j;
+       struct isl_basic_set *hull = NULL;
+
+       if (!bmap)
+               return NULL;
+       if (bmap->n_ineq == 0)
+               return bmap;
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
+               return bmap;
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
+               return bmap;
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
+               return isl_basic_map_implicit_equalities(bmap);
+
+       hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
+       if (!hull)
+               goto error;
+       if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
+               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,
+                                       hull->n_eq, 0);
+       for (i = 0; i < hull->n_eq; ++i) {
+               j = isl_basic_map_alloc_equality(bmap);
+               if (j < 0)
+                       goto error;
+               isl_seq_cpy(bmap->eq[j], hull->eq[i],
+                               1 + isl_basic_set_total_dim(hull));
+       }
+       isl_basic_set_free(hull);
+       ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
+       bmap = isl_basic_map_simplify(bmap);
+       return isl_basic_map_finalize(bmap);
+error:
+       isl_basic_set_free(hull);
        isl_basic_map_free(bmap);
        return NULL;
 }
 
+__isl_give isl_basic_set *isl_basic_set_detect_equalities(
+                                               __isl_take isl_basic_set *bset)
+{
+       return (isl_basic_set *)
+               isl_basic_map_detect_equalities((isl_basic_map *)bset);
+}
+
+struct isl_map *isl_map_detect_equalities(struct isl_map *map)
+{
+       struct isl_basic_map *bmap;
+       int i;
+
+       if (!map)
+               return NULL;
+
+       for (i = 0; i < map->n; ++i) {
+               bmap = isl_basic_map_copy(map->p[i]);
+               bmap = isl_basic_map_detect_equalities(bmap);
+               if (!bmap)
+                       goto error;
+               isl_basic_map_free(map->p[i]);
+               map->p[i] = bmap;
+       }
+
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
+__isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
+{
+       return (isl_set *)isl_map_detect_equalities((isl_map *)set);
+}
+
+/* After computing the rational affine hull (by detecting the implicit
+ * equalities), we compute the additional equalities satisfied by
+ * the integer points (if any) and add the original equalities back in.
+ */
+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);
+       return bmap;
+}
+
 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
 {
        return (struct isl_basic_set *)
@@ -405,7 +687,10 @@ 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);