add generalized basis reduction based ILP solver
[platform/upstream/isl.git] / isl_map_simplify.c
index abc7695..38981aa 100644 (file)
@@ -760,13 +760,13 @@ static struct isl_basic_map *normalize_divs(
        if (div_eq < bmap->n_eq) {
                B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
                                        bmap->n_eq - div_eq, 0, 1 + total);
-               C = isl_mat_variable_compression(bmap->ctx, B, &C2);
+               C = isl_mat_variable_compression(B, &C2);
                if (!C || !C2)
                        goto error;
                if (C->n_col == 0) {
                        bmap = isl_basic_map_set_to_empty(bmap);
-                       isl_mat_free(bmap->ctx, C);
-                       isl_mat_free(bmap->ctx, C2);
+                       isl_mat_free(C);
+                       isl_mat_free(C2);
                        goto done;
                }
        }
@@ -782,17 +782,17 @@ static struct isl_basic_map *normalize_divs(
        B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
 
        if (C) {
-               B = isl_mat_product(bmap->ctx, B, C);
+               B = isl_mat_product(B, C);
                C = NULL;
        }
 
-       T = isl_mat_parameter_compression(bmap->ctx, B, d);
+       T = isl_mat_parameter_compression(B, d);
        if (!T)
                goto error;
        if (T->n_col == 0) {
                bmap = isl_basic_map_set_to_empty(bmap);
-               isl_mat_free(bmap->ctx, C2);
-               isl_mat_free(bmap->ctx, T);
+               isl_mat_free(C2);
+               isl_mat_free(T);
                goto done;
        }
        isl_int_init(v);
@@ -856,8 +856,8 @@ static struct isl_basic_map *normalize_divs(
                isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
        }
        free(pos);
-       isl_mat_free(bmap->ctx, C2);
-       isl_mat_free(bmap->ctx, T);
+       isl_mat_free(C2);
+       isl_mat_free(T);
 
        if (progress)
                *progress = 1;
@@ -866,9 +866,9 @@ done:
 
        return bmap;
 error:
-       isl_mat_free(bmap->ctx, C);
-       isl_mat_free(bmap->ctx, C2);
-       isl_mat_free(bmap->ctx, T);
+       isl_mat_free(C);
+       isl_mat_free(C2);
+       isl_mat_free(T);
        return bmap;
 }
 
@@ -1173,7 +1173,9 @@ error:
 }
 
 
-/* Remove any div that is defined in terms of the given variable.
+/* Remove definition of any div that is defined in terms of the given variable.
+ * The div itself is not removed.  Functions such as
+ * eliminate_divs_ineq depend on the other divs remaining in place.
  */
 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
                                                                        int pos)
@@ -1186,9 +1188,7 @@ static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
                        continue;
                if (isl_int_is_zero(bmap->div[i][1+1+pos]))
                        continue;
-               bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
-               if (!bmap)
-                       return NULL;
+               isl_int_set_si(bmap->div[i][0], 0);
        }
        return bmap;
 }
@@ -1477,16 +1477,16 @@ static struct isl_basic_set *normalize_constraints_in_compressed_space(
 
        total = isl_basic_set_total_dim(bset);
        B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
-       C = isl_mat_variable_compression(bset->ctx, B, NULL);
+       C = isl_mat_variable_compression(B, NULL);
        if (!C)
                return bset;
        if (C->n_col == 0) {
-               isl_mat_free(bset->ctx, C);
+               isl_mat_free(C);
                return isl_basic_set_set_to_empty(bset);
        }
        B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
                                                0, bset->n_ineq, 0, 1 + total);
-       C = isl_mat_product(bset->ctx, B, C);
+       C = isl_mat_product(B, C);
        if (!C)
                return bset;
 
@@ -1500,7 +1500,7 @@ static struct isl_basic_set *normalize_constraints_in_compressed_space(
        }
        isl_int_clear(gcd);
 
-       isl_mat_free(bset->ctx, C);
+       isl_mat_free(C);
 
        return bset;
 }
@@ -1581,14 +1581,14 @@ static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
                goto error;
        for (i = 0; i < context_ineq; ++i)
                tab->con[i].frozen = 1;
-       tab = isl_tab_extend(bset->ctx, tab, bset->n_ineq);
+       tab = isl_tab_extend(tab, bset->n_ineq);
        if (!tab)
                goto error;
        for (i = 0; i < bset->n_ineq; ++i)
-               tab = isl_tab_add_ineq(bset->ctx, tab, bset->ineq[i]);
+               tab = isl_tab_add_ineq(tab, bset->ineq[i]);
        bset = isl_basic_set_add_constraints(combined, bset, 0);
-       tab = isl_tab_detect_equalities(bset->ctx, tab);
-       tab = isl_tab_detect_redundant(bset->ctx, tab);
+       tab = isl_tab_detect_equalities(tab);
+       tab = isl_tab_detect_redundant(tab);
        if (!tab)
                goto error2;
        for (i = 0; i < context_ineq; ++i) {
@@ -1596,7 +1596,7 @@ static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
                tab->con[i].is_redundant = 1;
        }
        bset = isl_basic_set_update_from_tab(bset, tab);
-       isl_tab_free(bset->ctx, tab);
+       isl_tab_free(tab);
        ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
        ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
 done:
@@ -1861,8 +1861,8 @@ int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
  * Otherwise return -1.
  *
  * We first check that
- *     - the bounds are opposites of each other (expect for the constant
- *       term
+ *     - the bounds are opposites of each other (except for the constant
+ *       term)
  *     - the bounds do not reference any other div
  *     - no div is defined in terms of this div
  *
@@ -2013,7 +2013,6 @@ static void construct_test_ineq(struct isl_basic_map *bmap, int i,
 static struct isl_basic_map *drop_more_redundant_divs(
        struct isl_basic_map *bmap, int *pairs, int n)
 {
-       struct isl_ctx *ctx = NULL;
        struct isl_tab *tab = NULL;
        struct isl_vec *vec = NULL;
        unsigned dim;
@@ -2027,10 +2026,8 @@ static struct isl_basic_map *drop_more_redundant_divs(
        if (!bmap)
                goto error;
 
-       ctx = bmap->ctx;
-
        dim = isl_dim_total(bmap->dim);
-       vec = isl_vec_alloc(ctx, 1 + dim + bmap->n_div);
+       vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
        if (!vec)
                goto error;
 
@@ -2058,8 +2055,8 @@ static struct isl_basic_map *drop_more_redundant_divs(
                                        continue;
                                construct_test_ineq(bmap, i, l, u,
                                                    vec->el, g, fl, fu);
-                               res = isl_tab_min(ctx, tab, vec->el,
-                                                 ctx->one, &g, NULL);
+                               res = isl_tab_min(tab, vec->el,
+                                                 bmap->ctx->one, &g, NULL, 0);
                                if (res == isl_lp_error)
                                        goto error;
                                if (res == isl_lp_empty) {
@@ -2080,7 +2077,7 @@ static struct isl_basic_map *drop_more_redundant_divs(
                --n;
        }
 
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        isl_vec_free(vec);
 
        isl_int_clear(g);
@@ -2097,10 +2094,8 @@ static struct isl_basic_map *drop_more_redundant_divs(
 error:
        free(pairs);
        isl_basic_map_free(bmap);
-       if (ctx) {
-               isl_tab_free(ctx, tab);
-               isl_vec_free(vec);
-       }
+       isl_tab_free(tab);
+       isl_vec_free(vec);
        isl_int_clear(g);
        isl_int_clear(fl);
        isl_int_clear(fu);
@@ -2282,9 +2277,9 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs(
                int pos, neg;
                int last_pos, last_neg;
                int redundant;
+               int defined;
 
-               if (!isl_int_is_zero(bmap->div[i][0]))
-                       continue;
+               defined = !isl_int_is_zero(bmap->div[i][0]);
                for (j = 0; j < bmap->n_eq; ++j)
                        if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
                                break;
@@ -2329,7 +2324,8 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs(
                isl_int_sub(bmap->ineq[last_pos][0],
                            bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
                if (!redundant) {
-                       if (!ok_to_set_div_from_bound(bmap, i, last_pos)) {
+                       if (defined ||
+                           !ok_to_set_div_from_bound(bmap, i, last_pos)) {
                                pairs[i] = 0;
                                --n;
                                continue;
@@ -2361,3 +2357,34 @@ error:
        isl_basic_map_free(bmap);
        return NULL;
 }
+
+struct isl_basic_set *isl_basic_set_drop_redundant_divs(
+       struct isl_basic_set *bset)
+{
+       return (struct isl_basic_set *)
+           isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
+}
+
+struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
+{
+       int i;
+
+       if (!map)
+               return NULL;
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
+               if (!map->p[i])
+                       goto error;
+       }
+       ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
+struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
+{
+       return (struct isl_set *)
+           isl_map_drop_redundant_divs((struct isl_map *)set);
+}