1 #include "isl_equalities.h"
3 #include "isl_map_private.h"
5 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
7 isl_int *t = bmap->eq[a];
8 bmap->eq[a] = bmap->eq[b];
12 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
15 isl_int *t = bmap->ineq[a];
16 bmap->ineq[a] = bmap->ineq[b];
21 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
23 swap_inequality((struct isl_basic_map *)bset, a, b);
26 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
28 isl_seq_cpy(c, c + n, rem);
29 isl_seq_clr(c + rem, n);
32 /* Drop n dimensions starting at first.
34 * In principle, this frees up some extra variables as the number
35 * of columns remains constant, but we would have to extend
36 * the div array too as the number of rows in this array is assumed
37 * to be equal to extra.
39 struct isl_basic_set *isl_basic_set_drop_dims(
40 struct isl_basic_set *bset, unsigned first, unsigned n)
47 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
52 bset = isl_basic_set_cow(bset);
56 for (i = 0; i < bset->n_eq; ++i)
57 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
58 (bset->dim->n_out-first-n)+bset->extra);
60 for (i = 0; i < bset->n_ineq; ++i)
61 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
62 (bset->dim->n_out-first-n)+bset->extra);
64 for (i = 0; i < bset->n_div; ++i)
65 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
66 (bset->dim->n_out-first-n)+bset->extra);
68 bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
72 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
73 bset = isl_basic_set_simplify(bset);
74 return isl_basic_set_finalize(bset);
76 isl_basic_set_free(bset);
80 struct isl_set *isl_set_drop_dims(
81 struct isl_set *set, unsigned first, unsigned n)
88 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
92 set = isl_set_cow(set);
95 set->dim = isl_dim_drop_outputs(set->dim, first, n);
99 for (i = 0; i < set->n; ++i) {
100 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
105 F_CLR(set, ISL_SET_NORMALIZED);
112 /* Drop n input dimensions starting at first.
114 * In principle, this frees up some extra variables as the number
115 * of columns remains constant, but we would have to extend
116 * the div array too as the number of rows in this array is assumed
117 * to be equal to extra.
119 struct isl_basic_map *isl_basic_map_drop_inputs(
120 struct isl_basic_map *bmap, unsigned first, unsigned n)
130 nparam = isl_basic_map_n_param(bmap);
131 n_in = isl_basic_map_n_in(bmap);
132 n_out = isl_basic_map_n_out(bmap);
133 isl_assert(bmap->ctx, first + n <= n_in, goto error);
138 bmap = isl_basic_map_cow(bmap);
142 for (i = 0; i < bmap->n_eq; ++i)
143 constraint_drop_vars(bmap->eq[i]+1+nparam+first, n,
144 (n_in-first-n)+n_out+bmap->extra);
146 for (i = 0; i < bmap->n_ineq; ++i)
147 constraint_drop_vars(bmap->ineq[i]+1+nparam+first, n,
148 (n_in-first-n)+n_out+bmap->extra);
150 for (i = 0; i < bmap->n_div; ++i)
151 constraint_drop_vars(bmap->div[i]+1+1+nparam+first, n,
152 (n_in-first-n)+n_out+bmap->extra);
154 bmap->dim = isl_dim_drop_inputs(bmap->dim, first, n);
158 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
159 bmap = isl_basic_map_simplify(bmap);
160 return isl_basic_map_finalize(bmap);
162 isl_basic_map_free(bmap);
166 struct isl_map *isl_map_drop_inputs(
167 struct isl_map *map, unsigned first, unsigned n)
174 isl_assert(map->ctx, first + n <= map->dim->n_in, goto error);
178 map = isl_map_cow(map);
181 map->dim = isl_dim_drop_inputs(map->dim, first, n);
185 for (i = 0; i < map->n; ++i) {
186 map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
190 F_CLR(map, ISL_MAP_NORMALIZED);
199 * We don't cow, as the div is assumed to be redundant.
201 static struct isl_basic_map *isl_basic_map_drop_div(
202 struct isl_basic_map *bmap, unsigned div)
210 pos = 1 + isl_dim_total(bmap->dim) + div;
212 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
214 for (i = 0; i < bmap->n_eq; ++i)
215 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
217 for (i = 0; i < bmap->n_ineq; ++i) {
218 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
219 isl_basic_map_drop_inequality(bmap, i);
223 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
226 for (i = 0; i < bmap->n_div; ++i)
227 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
229 if (div != bmap->n_div - 1) {
231 isl_int *t = bmap->div[div];
233 for (j = div; j < bmap->n_div - 1; ++j)
234 bmap->div[j] = bmap->div[j+1];
236 bmap->div[bmap->n_div - 1] = t;
238 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
239 isl_basic_map_free_div(bmap, 1);
243 isl_basic_map_free(bmap);
247 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
251 unsigned total = isl_basic_map_total_dim(bmap);
254 for (i = bmap->n_eq - 1; i >= 0; --i) {
255 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
256 if (isl_int_is_zero(gcd)) {
257 if (!isl_int_is_zero(bmap->eq[i][0])) {
258 bmap = isl_basic_map_set_to_empty(bmap);
261 isl_basic_map_drop_equality(bmap, i);
264 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
265 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
266 if (isl_int_is_one(gcd))
268 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
269 bmap = isl_basic_map_set_to_empty(bmap);
272 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
275 for (i = bmap->n_ineq - 1; i >= 0; --i) {
276 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
277 if (isl_int_is_zero(gcd)) {
278 if (isl_int_is_neg(bmap->ineq[i][0])) {
279 bmap = isl_basic_map_set_to_empty(bmap);
282 isl_basic_map_drop_inequality(bmap, i);
285 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
286 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
287 if (isl_int_is_one(gcd))
289 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
290 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
297 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
300 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
302 len = 1 + isl_basic_map_total_dim(bmap);
304 for (i = 0; i < bmap->n_eq; ++i)
305 if (bmap->eq[i] != eq)
306 isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
308 for (i = 0; i < bmap->n_ineq; ++i)
309 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
311 /* We need to be careful about circular definitions,
312 * so for now we just remove the definitions of other divs that
313 * depend on this div and (possibly) recompute them later.
315 for (i = 0; i < bmap->n_div; ++i)
316 if (!isl_int_is_zero(bmap->div[i][0]) &&
317 !isl_int_is_zero(bmap->div[i][1 + pos]))
318 isl_seq_clr(bmap->div[i], 1 + len);
320 isl_basic_map_drop_div(bmap, div);
323 /* Elimininate divs based on equalities
325 static struct isl_basic_map *eliminate_divs_eq(
326 struct isl_basic_map *bmap, int *progress)
336 off = 1 + isl_dim_total(bmap->dim);
338 for (d = bmap->n_div - 1; d >= 0 ; --d) {
339 for (i = 0; i < bmap->n_eq; ++i) {
340 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
341 !isl_int_is_negone(bmap->eq[i][off + d]))
345 eliminate_div(bmap, bmap->eq[i], d);
346 isl_basic_map_drop_equality(bmap, i);
351 return eliminate_divs_eq(bmap, progress);
355 /* Elimininate divs based on inequalities
357 static struct isl_basic_map *eliminate_divs_ineq(
358 struct isl_basic_map *bmap, int *progress)
369 off = 1 + isl_dim_total(bmap->dim);
371 for (d = bmap->n_div - 1; d >= 0 ; --d) {
372 for (i = 0; i < bmap->n_eq; ++i)
373 if (!isl_int_is_zero(bmap->eq[i][off + d]))
377 for (i = 0; i < bmap->n_ineq; ++i)
378 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
380 if (i < bmap->n_ineq)
383 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
384 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
386 bmap = isl_basic_map_drop_div(bmap, d);
393 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
394 unsigned pos, isl_int *eq, int *progress)
399 total = isl_basic_map_total_dim(bmap);
400 for (k = 0; k < bmap->n_eq; ++k) {
401 if (bmap->eq[k] == eq)
403 if (isl_int_is_zero(bmap->eq[k][1+pos]))
407 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
410 for (k = 0; k < bmap->n_ineq; ++k) {
411 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
415 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
416 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
419 for (k = 0; k < bmap->n_div; ++k) {
420 if (isl_int_is_zero(bmap->div[k][0]))
422 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
426 isl_seq_elim(bmap->div[k]+1, eq,
427 1+pos, 1+total, &bmap->div[k][0]);
428 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
432 struct isl_basic_map *isl_basic_map_gauss(
433 struct isl_basic_map *bmap, int *progress)
444 total = isl_basic_map_total_dim(bmap);
445 total_var = total - bmap->n_div;
447 last_var = total - 1;
448 for (done = 0; done < bmap->n_eq; ++done) {
449 for (; last_var >= 0; --last_var) {
450 for (k = done; k < bmap->n_eq; ++k)
451 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
459 swap_equality(bmap, k, done);
460 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
461 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
463 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
466 if (last_var >= total_var &&
467 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
468 unsigned div = last_var - total_var;
469 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
470 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
471 isl_int_set(bmap->div[div][0],
472 bmap->eq[done][1+last_var]);
473 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
476 if (done == bmap->n_eq)
478 for (k = done; k < bmap->n_eq; ++k) {
479 if (isl_int_is_zero(bmap->eq[k][0]))
481 return isl_basic_map_set_to_empty(bmap);
483 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
487 struct isl_basic_set *isl_basic_set_gauss(
488 struct isl_basic_set *bset, int *progress)
490 return (struct isl_basic_set*)isl_basic_map_gauss(
491 (struct isl_basic_map *)bset, progress);
495 static unsigned int round_up(unsigned int v)
506 static int hash_index(isl_int ***index, unsigned int size, int bits,
507 struct isl_basic_map *bmap, int k)
510 unsigned total = isl_basic_map_total_dim(bmap);
511 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
512 for (h = hash; index[h]; h = (h+1) % size)
513 if (&bmap->ineq[k] != index[h] &&
514 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
519 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
520 struct isl_basic_set *bset, int k)
522 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
525 static struct isl_basic_map *remove_duplicate_divs(
526 struct isl_basic_map *bmap, int *progress)
533 unsigned total_var = isl_dim_total(bmap->dim);
534 unsigned total = total_var + bmap->n_div;
537 if (bmap->n_div <= 1)
541 for (k = bmap->n_div - 1; k >= 0; --k)
542 if (!isl_int_is_zero(bmap->div[k][0]))
547 size = round_up(4 * bmap->n_div / 3 - 1);
548 bits = ffs(size) - 1;
549 index = isl_calloc_array(ctx, int, size);
552 eq = isl_blk_alloc(ctx, 1+total);
553 if (isl_blk_is_error(eq))
556 isl_seq_clr(eq.data, 1+total);
557 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
558 for (--k; k >= 0; --k) {
561 if (isl_int_is_zero(bmap->div[k][0]))
564 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
565 for (h = hash; index[h]; h = (h+1) % size)
566 if (isl_seq_eq(bmap->div[k],
567 bmap->div[index[h]-1], 2+total))
572 isl_int_set_si(eq.data[1+total_var+k], -1);
573 isl_int_set_si(eq.data[1+total_var+l], 1);
574 eliminate_div(bmap, eq.data, l);
575 isl_int_set_si(eq.data[1+total_var+k], 0);
576 isl_int_set_si(eq.data[1+total_var+l], 0);
581 isl_blk_free(ctx, eq);
587 /* Normalize divs that appear in equalities.
589 * In particular, we assume that bmap contains some equalities
594 * and we want to replace the set of e_i by a minimal set and
595 * such that the new e_i have a canonical representation in terms
597 * If any of the equalities involves more than one divs, then
598 * we currently simply bail out.
600 * Let us first additionally assume that all equalities involve
601 * a div. The equalities then express modulo constraints on the
602 * remaining variables and we can use "parameter compression"
603 * to find a minimal set of constraints. The result is a transformation
605 * x = T(x') = x_0 + G x'
607 * with G a lower-triangular matrix with all elements below the diagonal
608 * non-negative and smaller than the diagonal element on the same row.
609 * We first normalize x_0 by making the same property hold in the affine
611 * The rows i of G with a 1 on the diagonal do not impose any modulo
612 * constraint and simply express x_i = x'_i.
613 * For each of the remaining rows i, we introduce a div and a corresponding
614 * equality. In particular
616 * g_ii e_j = x_i - g_i(x')
618 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
619 * corresponding div (if g_kk != 1).
621 * If there are any equalities not involving any div, then we
622 * first apply a variable compression on the variables x:
624 * x = C x'' x'' = C_2 x
626 * and perform the above parameter compression on A C instead of on A.
627 * The resulting compression is then of the form
629 * x'' = T(x') = x_0 + G x'
631 * and in constructing the new divs and the corresponding equalities,
632 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
633 * by the corresponding row from C_2.
635 static struct isl_basic_map *normalize_divs(
636 struct isl_basic_map *bmap, int *progress)
643 struct isl_mat *T = NULL;
644 struct isl_mat *C = NULL;
645 struct isl_mat *C2 = NULL;
653 if (bmap->n_div == 0)
659 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
662 total = isl_dim_total(bmap->dim);
663 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
664 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
668 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
675 if (div_eq < bmap->n_eq) {
676 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
677 bmap->n_eq - div_eq, 0, 1 + total);
678 C = isl_mat_variable_compression(bmap->ctx, B, &C2);
682 bmap = isl_basic_map_set_to_empty(bmap);
683 isl_mat_free(bmap->ctx, C);
684 isl_mat_free(bmap->ctx, C2);
689 d = isl_vec_alloc(bmap->ctx, div_eq);
692 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
693 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
695 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
697 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
700 B = isl_mat_product(bmap->ctx, B, C);
704 T = isl_mat_parameter_compression(bmap->ctx, B, d);
708 bmap = isl_basic_map_set_to_empty(bmap);
709 isl_mat_free(bmap->ctx, C2);
710 isl_mat_free(bmap->ctx, T);
714 for (i = 0; i < T->n_row - 1; ++i) {
715 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
716 if (isl_int_is_zero(v))
718 isl_mat_col_submul(T, 0, v, 1 + i);
721 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
722 /* We have to be careful because dropping equalities may reorder them */
724 for (j = bmap->n_div - 1; j >= 0; --j) {
725 for (i = 0; i < bmap->n_eq; ++i)
726 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
728 if (i < bmap->n_eq) {
729 bmap = isl_basic_map_drop_div(bmap, j);
730 isl_basic_map_drop_equality(bmap, i);
736 for (i = 1; i < T->n_row; ++i) {
737 if (isl_int_is_one(T->row[i][i]))
742 if (needed > dropped) {
743 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
748 for (i = 1; i < T->n_row; ++i) {
749 if (isl_int_is_one(T->row[i][i]))
751 k = isl_basic_map_alloc_div(bmap);
752 pos[i] = 1 + total + k;
753 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
754 isl_int_set(bmap->div[k][0], T->row[i][i]);
756 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
758 isl_int_set_si(bmap->div[k][1 + i], 1);
759 for (j = 0; j < i; ++j) {
760 if (isl_int_is_zero(T->row[i][j]))
762 if (pos[j] < T->n_row && C2)
763 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
764 C2->row[pos[j]], 1 + total);
766 isl_int_neg(bmap->div[k][1 + pos[j]],
769 j = isl_basic_map_alloc_equality(bmap);
770 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
771 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
774 isl_mat_free(bmap->ctx, C2);
775 isl_mat_free(bmap->ctx, T);
779 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
783 isl_mat_free(bmap->ctx, C);
784 isl_mat_free(bmap->ctx, C2);
785 isl_mat_free(bmap->ctx, T);
789 static struct isl_basic_map *remove_duplicate_constraints(
790 struct isl_basic_map *bmap, int *progress)
796 unsigned total = isl_basic_map_total_dim(bmap);
799 if (bmap->n_ineq <= 1)
802 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
803 bits = ffs(size) - 1;
804 index = isl_calloc_array(ctx, isl_int **, size);
808 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
809 for (k = 1; k < bmap->n_ineq; ++k) {
810 h = hash_index(index, size, bits, bmap, k);
812 index[h] = &bmap->ineq[k];
817 l = index[h] - &bmap->ineq[0];
818 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
819 swap_inequality(bmap, k, l);
820 isl_basic_map_drop_inequality(bmap, k);
824 for (k = 0; k < bmap->n_ineq-1; ++k) {
825 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
826 h = hash_index(index, size, bits, bmap, k);
827 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
830 l = index[h] - &bmap->ineq[0];
831 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
832 if (isl_int_is_pos(sum))
834 if (isl_int_is_zero(sum)) {
835 /* We need to break out of the loop after these
836 * changes since the contents of the hash
837 * will no longer be valid.
838 * Plus, we probably we want to regauss first.
840 isl_basic_map_drop_inequality(bmap, l);
841 isl_basic_map_inequality_to_equality(bmap, k);
843 bmap = isl_basic_map_set_to_empty(bmap);
853 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
860 bmap = normalize_constraints(bmap);
861 bmap = eliminate_divs_eq(bmap, &progress);
862 bmap = eliminate_divs_ineq(bmap, &progress);
863 bmap = isl_basic_map_gauss(bmap, &progress);
864 /* requires equalities in normal form */
865 bmap = normalize_divs(bmap, &progress);
866 bmap = remove_duplicate_divs(bmap, &progress);
867 bmap = remove_duplicate_constraints(bmap, &progress);
872 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
874 return (struct isl_basic_set *)
875 isl_basic_map_simplify((struct isl_basic_map *)bset);
879 /* If the only constraints a div d=floor(f/m)
880 * appears in are its two defining constraints
883 * -(f - (m - 1)) + m d >= 0
885 * then it can safely be removed.
887 static int div_is_redundant(struct isl_basic_map *bmap, int div)
890 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
892 for (i = 0; i < bmap->n_eq; ++i)
893 if (!isl_int_is_zero(bmap->eq[i][pos]))
896 for (i = 0; i < bmap->n_ineq; ++i) {
897 if (isl_int_is_zero(bmap->ineq[i][pos]))
899 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
901 isl_int_sub(bmap->div[div][1],
902 bmap->div[div][1], bmap->div[div][0]);
903 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
904 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
905 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
906 isl_int_add(bmap->div[div][1],
907 bmap->div[div][1], bmap->div[div][0]);
910 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
911 bmap->n_div-div-1) != -1)
913 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
914 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
916 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
917 bmap->n_div-div-1) != -1)
923 for (i = 0; i < bmap->n_div; ++i)
924 if (!isl_int_is_zero(bmap->div[i][1+pos]))
931 * Remove divs that don't occur in any of the constraints or other divs.
932 * These can arise when dropping some of the variables in a quast
933 * returned by piplib.
935 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
942 for (i = bmap->n_div-1; i >= 0; --i) {
943 if (!div_is_redundant(bmap, i))
945 bmap = isl_basic_map_drop_div(bmap, i);
950 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
952 bmap = remove_redundant_divs(bmap);
955 F_SET(bmap, ISL_BASIC_SET_FINAL);
959 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
961 return (struct isl_basic_set *)
962 isl_basic_map_finalize((struct isl_basic_map *)bset);
965 struct isl_set *isl_set_finalize(struct isl_set *set)
971 for (i = 0; i < set->n; ++i) {
972 set->p[i] = isl_basic_set_finalize(set->p[i]);
982 struct isl_map *isl_map_finalize(struct isl_map *map)
988 for (i = 0; i < map->n; ++i) {
989 map->p[i] = isl_basic_map_finalize(map->p[i]);
993 F_CLR(map, ISL_MAP_NORMALIZED);
1001 /* Remove any div that is defined in terms of the given variable.
1003 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1007 unsigned dim = isl_dim_total(bmap->dim);
1009 for (i = 0; i < bmap->n_div; ++i) {
1010 if (isl_int_is_zero(bmap->div[i][0]))
1012 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1014 bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
1021 /* Eliminate the specified variables from the constraints using
1022 * Fourier-Motzkin. The variables themselves are not removed.
1024 struct isl_basic_map *isl_basic_map_eliminate_vars(
1025 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1035 total = isl_basic_map_total_dim(bmap);
1037 bmap = isl_basic_map_cow(bmap);
1038 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1039 int n_lower, n_upper;
1040 bmap = remove_dependent_vars(bmap, d);
1043 if (d >= total - bmap->n_div)
1044 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1045 for (i = 0; i < bmap->n_eq; ++i) {
1046 if (isl_int_is_zero(bmap->eq[i][1+d]))
1048 eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1049 isl_basic_map_drop_equality(bmap, i);
1056 for (i = 0; i < bmap->n_ineq; ++i) {
1057 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1059 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1062 bmap = isl_basic_map_extend_constraints(bmap,
1063 0, n_lower * n_upper);
1064 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1066 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1069 for (j = 0; j < i; ++j) {
1070 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1073 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1074 isl_int_sgn(bmap->ineq[j][1+d]))
1076 k = isl_basic_map_alloc_inequality(bmap);
1079 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1081 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1082 1+d, 1+total, NULL);
1084 isl_basic_map_drop_inequality(bmap, i);
1087 if (n_lower > 0 && n_upper > 0) {
1088 bmap = normalize_constraints(bmap);
1089 bmap = remove_duplicate_constraints(bmap, NULL);
1090 bmap = isl_basic_map_gauss(bmap, NULL);
1091 bmap = isl_basic_map_convex_hull(bmap);
1094 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1098 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1101 isl_basic_map_free(bmap);
1105 struct isl_basic_set *isl_basic_set_eliminate_vars(
1106 struct isl_basic_set *bset, unsigned pos, unsigned n)
1108 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1109 (struct isl_basic_map *)bset, pos, n);
1112 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1117 total = isl_dim_total(bmap->dim);
1118 for (d = 0; d < total; ++d)
1120 for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
1121 for (; d >= 0; --d) {
1122 if (isl_int_is_zero(bmap->eq[i][1+d]))
1130 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1132 return compute_elimination_index((struct isl_basic_map *)bset, elim);
1135 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1136 struct isl_basic_map *bmap, int *elim)
1142 total = isl_dim_total(bmap->dim);
1143 for (d = total - 1; d >= 0; --d) {
1144 if (isl_int_is_zero(src[1+d]))
1149 isl_seq_cpy(dst, src, 1 + total);
1152 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1157 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1158 struct isl_basic_set *bset, int *elim)
1160 return reduced_using_equalities(dst, src,
1161 (struct isl_basic_map *)bset, elim);
1164 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1165 struct isl_basic_set *bset, struct isl_basic_set *context)
1170 if (!bset || !context)
1173 bset = isl_basic_set_cow(bset);
1177 elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1180 set_compute_elimination_index(context, elim);
1181 for (i = 0; i < bset->n_eq; ++i)
1182 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1184 for (i = 0; i < bset->n_ineq; ++i)
1185 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1187 isl_basic_set_free(context);
1189 bset = isl_basic_set_simplify(bset);
1190 bset = isl_basic_set_finalize(bset);
1193 isl_basic_set_free(bset);
1194 isl_basic_set_free(context);
1198 static struct isl_basic_set *remove_shifted_constraints(
1199 struct isl_basic_set *bset, struct isl_basic_set *context)
1209 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1210 bits = ffs(size) - 1;
1211 index = isl_calloc_array(ctx, isl_int **, size);
1215 for (k = 0; k < context->n_ineq; ++k) {
1216 h = set_hash_index(index, size, bits, context, k);
1217 index[h] = &context->ineq[k];
1219 for (k = 0; k < bset->n_ineq; ++k) {
1220 h = set_hash_index(index, size, bits, bset, k);
1223 l = index[h] - &context->ineq[0];
1224 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1226 bset = isl_basic_set_cow(bset);
1229 isl_basic_set_drop_inequality(bset, k);
1239 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1240 struct isl_basic_set *context);
1242 static struct isl_basic_set *uset_gist_context_eq(struct isl_basic_set *bset,
1243 struct isl_basic_set *context)
1247 struct isl_ctx *ctx = context->ctx;
1248 struct isl_basic_set *reduced_context;
1249 reduced_context = isl_basic_set_remove_equalities(
1250 isl_basic_set_copy(context), &T, &T2);
1251 if (!reduced_context)
1253 bset = isl_basic_set_preimage(ctx, bset, T);
1254 bset = uset_gist(bset, reduced_context);
1255 bset = isl_basic_set_preimage(ctx, bset, T2);
1256 bset = isl_basic_set_reduce_using_equalities(bset, context);
1259 isl_basic_set_free(context);
1260 isl_basic_set_free(bset);
1264 static struct isl_basic_set *uset_gist_set_eq(struct isl_basic_set *bset,
1265 struct isl_basic_set *context)
1269 struct isl_ctx *ctx = context->ctx;
1270 struct isl_basic_set *affine_hull = NULL;
1272 affine_hull = isl_basic_set_copy(bset);
1273 affine_hull = isl_basic_set_cow(affine_hull);
1276 isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1278 bset = isl_basic_set_remove_equalities(bset, &T, &T2);
1281 context = isl_basic_set_preimage(ctx, context, T);
1282 bset = uset_gist(bset, context);
1283 bset = isl_basic_set_preimage(ctx, bset, T2);
1284 bset = isl_basic_set_intersect(bset, affine_hull);
1287 isl_basic_set_free(affine_hull);
1288 isl_basic_set_free(context);
1289 isl_basic_set_free(bset);
1293 /* Remove all information from bset that is redundant in the context
1294 * of context. In particular, equalities that are linear combinations
1295 * of those in context are removed. Then the inequalities that are
1296 * redundant in the context of the equalities and inequalities of
1297 * context are removed.
1299 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1300 struct isl_basic_set *context)
1304 struct isl_basic_set *combined;
1305 struct isl_ctx *ctx;
1307 if (!bset || !context)
1310 if (context->n_eq > 0)
1311 return uset_gist_context_eq(bset, context);
1312 if (!context->n_ineq)
1315 return uset_gist_set_eq(bset, context);
1316 bset = remove_shifted_constraints(bset, context);
1317 combined = isl_basic_set_extend_constraints(isl_basic_set_copy(bset),
1318 context->n_eq, context->n_ineq);
1319 context = isl_basic_set_add_constraints(combined, context, 0);
1324 for (i = bset->n_ineq-1; i >= 0; --i) {
1326 set_swap_inequality(context, i, context->n_ineq-1);
1328 redundant = isl_basic_set_constraint_is_redundant(&context,
1329 context->ineq[context->n_ineq], &opt, NULL);
1330 if (redundant == -1) {
1334 if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
1335 bset = isl_basic_set_set_to_empty(bset);
1339 set_swap_inequality(context, i, context->n_ineq-1);
1341 isl_basic_set_drop_inequality(context, i);
1342 isl_basic_set_drop_inequality(bset, i);
1347 isl_basic_set_free(context);
1350 isl_basic_set_free(context);
1351 isl_basic_set_free(bset);
1355 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1356 struct isl_basic_map *context)
1358 struct isl_basic_set *bset;
1360 if (!bmap || !context)
1363 context = isl_basic_map_align_divs(context, bmap);
1364 bmap = isl_basic_map_align_divs(bmap, context);
1366 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1367 isl_basic_map_underlying_set(context));
1369 return isl_basic_map_overlying_set(bset, bmap);
1371 isl_basic_map_free(bmap);
1372 isl_basic_map_free(context);
1377 * Assumes context has no implicit divs.
1379 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1383 map = isl_map_cow(map);
1384 if (!map || !context)
1386 isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1387 map = isl_map_compute_divs(map);
1388 for (i = 0; i < map->n; ++i)
1389 context = isl_basic_map_align_divs(context, map->p[i]);
1390 for (i = 0; i < map->n; ++i) {
1391 map->p[i] = isl_basic_map_gist(map->p[i],
1392 isl_basic_map_copy(context));
1396 isl_basic_map_free(context);
1397 F_CLR(map, ISL_MAP_NORMALIZED);
1401 isl_basic_map_free(context);
1405 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1406 struct isl_basic_set *context)
1408 return (struct isl_basic_set *)isl_basic_map_gist(
1409 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1412 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1414 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1415 (struct isl_basic_map *)context);
1418 /* Quick check to see if two basic maps are disjoint.
1419 * In particular, we reduce the equalities and inequalities of
1420 * one basic map in the context of the equalities of the other
1421 * basic map and check if we get a contradiction.
1423 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1424 struct isl_basic_map *bmap2)
1426 struct isl_vec *v = NULL;
1431 if (!bmap1 || !bmap2)
1433 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1435 if (bmap1->n_div || bmap2->n_div)
1437 if (!bmap1->n_eq && !bmap2->n_eq)
1440 total = isl_dim_total(bmap1->dim);
1443 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1446 elim = isl_alloc_array(bmap1->ctx, int, total);
1449 compute_elimination_index(bmap1, elim);
1450 for (i = 0; i < bmap2->n_eq; ++i) {
1452 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1454 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1455 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1458 for (i = 0; i < bmap2->n_ineq; ++i) {
1460 reduced = reduced_using_equalities(v->block.data,
1461 bmap2->ineq[i], bmap1, elim);
1462 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1463 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1466 compute_elimination_index(bmap2, elim);
1467 for (i = 0; i < bmap1->n_ineq; ++i) {
1469 reduced = reduced_using_equalities(v->block.data,
1470 bmap1->ineq[i], bmap2, elim);
1471 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1472 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1475 isl_vec_free(bmap1->ctx, v);
1479 isl_vec_free(bmap1->ctx, v);
1483 isl_vec_free(bmap1->ctx, v);
1488 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1489 struct isl_basic_set *bset2)
1491 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1492 (struct isl_basic_map *)bset2);
1495 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1502 if (isl_map_fast_is_equal(map1, map2))
1505 for (i = 0; i < map1->n; ++i) {
1506 for (j = 0; j < map2->n; ++j) {
1507 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1516 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1518 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1519 (struct isl_map *)set2);