1 #include "isl_equalities.h"
3 #include "isl_map_private.h"
7 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
9 isl_int *t = bmap->eq[a];
10 bmap->eq[a] = bmap->eq[b];
14 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
17 isl_int *t = bmap->ineq[a];
18 bmap->ineq[a] = bmap->ineq[b];
23 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
25 swap_inequality((struct isl_basic_map *)bset, a, b);
28 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
30 isl_seq_cpy(c, c + n, rem);
31 isl_seq_clr(c + rem, n);
34 /* Drop n dimensions starting at first.
36 * In principle, this frees up some extra variables as the number
37 * of columns remains constant, but we would have to extend
38 * the div array too as the number of rows in this array is assumed
39 * to be equal to extra.
41 struct isl_basic_set *isl_basic_set_drop_dims(
42 struct isl_basic_set *bset, unsigned first, unsigned n)
49 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
54 bset = isl_basic_set_cow(bset);
58 for (i = 0; i < bset->n_eq; ++i)
59 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
60 (bset->dim->n_out-first-n)+bset->extra);
62 for (i = 0; i < bset->n_ineq; ++i)
63 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
64 (bset->dim->n_out-first-n)+bset->extra);
66 for (i = 0; i < bset->n_div; ++i)
67 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
68 (bset->dim->n_out-first-n)+bset->extra);
70 bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
74 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
75 bset = isl_basic_set_simplify(bset);
76 return isl_basic_set_finalize(bset);
78 isl_basic_set_free(bset);
82 struct isl_set *isl_set_drop_dims(
83 struct isl_set *set, unsigned first, unsigned n)
90 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
94 set = isl_set_cow(set);
97 set->dim = isl_dim_drop_outputs(set->dim, first, n);
101 for (i = 0; i < set->n; ++i) {
102 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
107 ISL_F_CLR(set, ISL_SET_NORMALIZED);
114 /* Move "n" divs starting at "first" to the end of the list of divs.
116 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
117 unsigned first, unsigned n)
122 if (first + n == bmap->n_div)
125 div = isl_alloc_array(bmap->ctx, isl_int *, n);
128 for (i = 0; i < n; ++i)
129 div[i] = bmap->div[first + i];
130 for (i = 0; i < bmap->n_div - first - n; ++i)
131 bmap->div[first + i] = bmap->div[first + n + i];
132 for (i = 0; i < n; ++i)
133 bmap->div[bmap->n_div - n + i] = div[i];
137 isl_basic_map_free(bmap);
141 /* Drop "n" dimensions of type "type" starting at "first".
143 * In principle, this frees up some extra variables as the number
144 * of columns remains constant, but we would have to extend
145 * the div array too as the number of rows in this array is assumed
146 * to be equal to extra.
148 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
149 enum isl_dim_type type, unsigned first, unsigned n)
159 dim = isl_basic_map_dim(bmap, type);
160 isl_assert(bmap->ctx, first + n <= dim, goto error);
165 bmap = isl_basic_map_cow(bmap);
169 offset = isl_basic_map_offset(bmap, type) + first;
170 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
171 for (i = 0; i < bmap->n_eq; ++i)
172 constraint_drop_vars(bmap->eq[i]+offset, n, left);
174 for (i = 0; i < bmap->n_ineq; ++i)
175 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
177 for (i = 0; i < bmap->n_div; ++i)
178 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
180 if (type == isl_dim_div) {
181 bmap = move_divs_last(bmap, first, n);
184 isl_basic_map_free_div(bmap, n);
186 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
190 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
191 bmap = isl_basic_map_simplify(bmap);
192 return isl_basic_map_finalize(bmap);
194 isl_basic_map_free(bmap);
198 struct isl_basic_map *isl_basic_map_drop_inputs(
199 struct isl_basic_map *bmap, unsigned first, unsigned n)
201 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
204 struct isl_map *isl_map_drop(struct isl_map *map,
205 enum isl_dim_type type, unsigned first, unsigned n)
212 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
216 map = isl_map_cow(map);
219 map->dim = isl_dim_drop(map->dim, type, first, n);
223 for (i = 0; i < map->n; ++i) {
224 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
228 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
236 struct isl_map *isl_map_drop_inputs(
237 struct isl_map *map, unsigned first, unsigned n)
239 return isl_map_drop(map, isl_dim_in, first, n);
243 * We don't cow, as the div is assumed to be redundant.
245 static struct isl_basic_map *isl_basic_map_drop_div(
246 struct isl_basic_map *bmap, unsigned div)
254 pos = 1 + isl_dim_total(bmap->dim) + div;
256 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
258 for (i = 0; i < bmap->n_eq; ++i)
259 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
261 for (i = 0; i < bmap->n_ineq; ++i) {
262 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
263 isl_basic_map_drop_inequality(bmap, i);
267 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
270 for (i = 0; i < bmap->n_div; ++i)
271 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
273 if (div != bmap->n_div - 1) {
275 isl_int *t = bmap->div[div];
277 for (j = div; j < bmap->n_div - 1; ++j)
278 bmap->div[j] = bmap->div[j+1];
280 bmap->div[bmap->n_div - 1] = t;
282 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
283 isl_basic_map_free_div(bmap, 1);
287 isl_basic_map_free(bmap);
291 struct isl_basic_map *isl_basic_map_normalize_constraints(
292 struct isl_basic_map *bmap)
296 unsigned total = isl_basic_map_total_dim(bmap);
299 for (i = bmap->n_eq - 1; i >= 0; --i) {
300 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
301 if (isl_int_is_zero(gcd)) {
302 if (!isl_int_is_zero(bmap->eq[i][0])) {
303 bmap = isl_basic_map_set_to_empty(bmap);
306 isl_basic_map_drop_equality(bmap, i);
309 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
310 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
311 if (isl_int_is_one(gcd))
313 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
314 bmap = isl_basic_map_set_to_empty(bmap);
317 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
320 for (i = bmap->n_ineq - 1; i >= 0; --i) {
321 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
322 if (isl_int_is_zero(gcd)) {
323 if (isl_int_is_neg(bmap->ineq[i][0])) {
324 bmap = isl_basic_map_set_to_empty(bmap);
327 isl_basic_map_drop_inequality(bmap, i);
330 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
331 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
332 if (isl_int_is_one(gcd))
334 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
335 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
342 struct isl_basic_set *isl_basic_set_normalize_constraints(
343 struct isl_basic_set *bset)
345 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
346 (struct isl_basic_map *)bset);
349 /* Assumes divs have been ordered if keep_divs is set.
351 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
352 unsigned pos, isl_int *eq, int keep_divs, int *progress)
358 total = isl_basic_map_total_dim(bmap);
359 last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
361 for (k = 0; k < bmap->n_eq; ++k) {
362 if (bmap->eq[k] == eq)
364 if (isl_int_is_zero(bmap->eq[k][1+pos]))
368 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
371 for (k = 0; k < bmap->n_ineq; ++k) {
372 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
376 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
377 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
380 for (k = 0; k < bmap->n_div; ++k) {
381 if (isl_int_is_zero(bmap->div[k][0]))
383 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
387 /* We need to be careful about circular definitions,
388 * so for now we just remove the definition of div k
389 * if the equality contains any divs.
390 * If keep_divs is set, then the divs have been ordered
391 * and we can keep the definition as long as the result
394 if (last_div == -1 || (keep_divs && last_div < k))
395 isl_seq_elim(bmap->div[k]+1, eq,
396 1+pos, 1+total, &bmap->div[k][0]);
398 isl_seq_clr(bmap->div[k], 1 + total);
399 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
403 /* Assumes divs have been ordered if keep_divs is set.
405 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
406 unsigned div, int keep_divs)
408 unsigned pos = isl_dim_total(bmap->dim) + div;
410 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
412 isl_basic_map_drop_div(bmap, div);
415 /* Elimininate divs based on equalities
417 static struct isl_basic_map *eliminate_divs_eq(
418 struct isl_basic_map *bmap, int *progress)
425 bmap = isl_basic_map_order_divs(bmap);
430 off = 1 + isl_dim_total(bmap->dim);
432 for (d = bmap->n_div - 1; d >= 0 ; --d) {
433 for (i = 0; i < bmap->n_eq; ++i) {
434 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
435 !isl_int_is_negone(bmap->eq[i][off + d]))
439 eliminate_div(bmap, bmap->eq[i], d, 1);
440 isl_basic_map_drop_equality(bmap, i);
445 return eliminate_divs_eq(bmap, progress);
449 /* Elimininate divs based on inequalities
451 static struct isl_basic_map *eliminate_divs_ineq(
452 struct isl_basic_map *bmap, int *progress)
463 off = 1 + isl_dim_total(bmap->dim);
465 for (d = bmap->n_div - 1; d >= 0 ; --d) {
466 for (i = 0; i < bmap->n_eq; ++i)
467 if (!isl_int_is_zero(bmap->eq[i][off + d]))
471 for (i = 0; i < bmap->n_ineq; ++i)
472 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
474 if (i < bmap->n_ineq)
477 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
478 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
480 bmap = isl_basic_map_drop_div(bmap, d);
487 struct isl_basic_map *isl_basic_map_gauss(
488 struct isl_basic_map *bmap, int *progress)
496 bmap = isl_basic_map_order_divs(bmap);
501 total = isl_basic_map_total_dim(bmap);
502 total_var = total - bmap->n_div;
504 last_var = total - 1;
505 for (done = 0; done < bmap->n_eq; ++done) {
506 for (; last_var >= 0; --last_var) {
507 for (k = done; k < bmap->n_eq; ++k)
508 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
516 swap_equality(bmap, k, done);
517 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
518 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
520 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
523 if (last_var >= total_var &&
524 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
525 unsigned div = last_var - total_var;
526 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
527 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
528 isl_int_set(bmap->div[div][0],
529 bmap->eq[done][1+last_var]);
530 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
533 if (done == bmap->n_eq)
535 for (k = done; k < bmap->n_eq; ++k) {
536 if (isl_int_is_zero(bmap->eq[k][0]))
538 return isl_basic_map_set_to_empty(bmap);
540 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
544 struct isl_basic_set *isl_basic_set_gauss(
545 struct isl_basic_set *bset, int *progress)
547 return (struct isl_basic_set*)isl_basic_map_gauss(
548 (struct isl_basic_map *)bset, progress);
552 static unsigned int round_up(unsigned int v)
563 static int hash_index(isl_int ***index, unsigned int size, int bits,
564 struct isl_basic_map *bmap, int k)
567 unsigned total = isl_basic_map_total_dim(bmap);
568 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
569 for (h = hash; index[h]; h = (h+1) % size)
570 if (&bmap->ineq[k] != index[h] &&
571 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
576 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
577 struct isl_basic_set *bset, int k)
579 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
582 /* If we can eliminate more than one div, then we need to make
583 * sure we do it from last div to first div, in order not to
584 * change the position of the other divs that still need to
587 static struct isl_basic_map *remove_duplicate_divs(
588 struct isl_basic_map *bmap, int *progress)
596 unsigned total_var = isl_dim_total(bmap->dim);
597 unsigned total = total_var + bmap->n_div;
600 if (bmap->n_div <= 1)
604 for (k = bmap->n_div - 1; k >= 0; --k)
605 if (!isl_int_is_zero(bmap->div[k][0]))
610 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
611 size = round_up(4 * bmap->n_div / 3 - 1);
612 bits = ffs(size) - 1;
613 index = isl_calloc_array(ctx, int, size);
616 eq = isl_blk_alloc(ctx, 1+total);
617 if (isl_blk_is_error(eq))
620 isl_seq_clr(eq.data, 1+total);
621 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
622 for (--k; k >= 0; --k) {
625 if (isl_int_is_zero(bmap->div[k][0]))
628 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
629 for (h = hash; index[h]; h = (h+1) % size)
630 if (isl_seq_eq(bmap->div[k],
631 bmap->div[index[h]-1], 2+total))
640 for (l = bmap->n_div - 1; l >= 0; --l) {
644 isl_int_set_si(eq.data[1+total_var+k], -1);
645 isl_int_set_si(eq.data[1+total_var+l], 1);
646 eliminate_div(bmap, eq.data, l, 0);
647 isl_int_set_si(eq.data[1+total_var+k], 0);
648 isl_int_set_si(eq.data[1+total_var+l], 0);
651 isl_blk_free(ctx, eq);
658 static int n_pure_div_eq(struct isl_basic_map *bmap)
663 total = isl_dim_total(bmap->dim);
664 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
665 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
669 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
675 /* Normalize divs that appear in equalities.
677 * In particular, we assume that bmap contains some equalities
682 * and we want to replace the set of e_i by a minimal set and
683 * such that the new e_i have a canonical representation in terms
685 * If any of the equalities involves more than one divs, then
686 * we currently simply bail out.
688 * Let us first additionally assume that all equalities involve
689 * a div. The equalities then express modulo constraints on the
690 * remaining variables and we can use "parameter compression"
691 * to find a minimal set of constraints. The result is a transformation
693 * x = T(x') = x_0 + G x'
695 * with G a lower-triangular matrix with all elements below the diagonal
696 * non-negative and smaller than the diagonal element on the same row.
697 * We first normalize x_0 by making the same property hold in the affine
699 * The rows i of G with a 1 on the diagonal do not impose any modulo
700 * constraint and simply express x_i = x'_i.
701 * For each of the remaining rows i, we introduce a div and a corresponding
702 * equality. In particular
704 * g_ii e_j = x_i - g_i(x')
706 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
707 * corresponding div (if g_kk != 1).
709 * If there are any equalities not involving any div, then we
710 * first apply a variable compression on the variables x:
712 * x = C x'' x'' = C_2 x
714 * and perform the above parameter compression on A C instead of on A.
715 * The resulting compression is then of the form
717 * x'' = T(x') = x_0 + G x'
719 * and in constructing the new divs and the corresponding equalities,
720 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
721 * by the corresponding row from C_2.
723 static struct isl_basic_map *normalize_divs(
724 struct isl_basic_map *bmap, int *progress)
731 struct isl_mat *T = NULL;
732 struct isl_mat *C = NULL;
733 struct isl_mat *C2 = NULL;
741 if (bmap->n_div == 0)
747 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
750 total = isl_dim_total(bmap->dim);
751 div_eq = n_pure_div_eq(bmap);
755 if (div_eq < bmap->n_eq) {
756 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
757 bmap->n_eq - div_eq, 0, 1 + total);
758 C = isl_mat_variable_compression(B, &C2);
762 bmap = isl_basic_map_set_to_empty(bmap);
769 d = isl_vec_alloc(bmap->ctx, div_eq);
772 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
773 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
775 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
777 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
780 B = isl_mat_product(B, C);
784 T = isl_mat_parameter_compression(B, d);
788 bmap = isl_basic_map_set_to_empty(bmap);
794 for (i = 0; i < T->n_row - 1; ++i) {
795 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
796 if (isl_int_is_zero(v))
798 isl_mat_col_submul(T, 0, v, 1 + i);
801 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
802 /* We have to be careful because dropping equalities may reorder them */
804 for (j = bmap->n_div - 1; j >= 0; --j) {
805 for (i = 0; i < bmap->n_eq; ++i)
806 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
808 if (i < bmap->n_eq) {
809 bmap = isl_basic_map_drop_div(bmap, j);
810 isl_basic_map_drop_equality(bmap, i);
816 for (i = 1; i < T->n_row; ++i) {
817 if (isl_int_is_one(T->row[i][i]))
822 if (needed > dropped) {
823 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
828 for (i = 1; i < T->n_row; ++i) {
829 if (isl_int_is_one(T->row[i][i]))
831 k = isl_basic_map_alloc_div(bmap);
832 pos[i] = 1 + total + k;
833 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
834 isl_int_set(bmap->div[k][0], T->row[i][i]);
836 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
838 isl_int_set_si(bmap->div[k][1 + i], 1);
839 for (j = 0; j < i; ++j) {
840 if (isl_int_is_zero(T->row[i][j]))
842 if (pos[j] < T->n_row && C2)
843 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
844 C2->row[pos[j]], 1 + total);
846 isl_int_neg(bmap->div[k][1 + pos[j]],
849 j = isl_basic_map_alloc_equality(bmap);
850 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
851 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
860 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
870 static struct isl_basic_map *set_div_from_lower_bound(
871 struct isl_basic_map *bmap, int div, int ineq)
873 unsigned total = 1 + isl_dim_total(bmap->dim);
875 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
876 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
877 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
878 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
879 isl_int_set_si(bmap->div[div][1 + total + div], 0);
884 /* Check whether it is ok to define a div based on an inequality.
885 * To avoid the introduction of circular definitions of divs, we
886 * do not allow such a definition if the resulting expression would refer to
887 * any other undefined divs or if any known div is defined in
888 * terms of the unknown div.
890 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
894 unsigned total = 1 + isl_dim_total(bmap->dim);
896 /* Not defined in terms of unknown divs */
897 for (j = 0; j < bmap->n_div; ++j) {
900 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
902 if (isl_int_is_zero(bmap->div[j][0]))
906 /* No other div defined in terms of this one => avoid loops */
907 for (j = 0; j < bmap->n_div; ++j) {
910 if (isl_int_is_zero(bmap->div[j][0]))
912 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
919 /* Given two constraints "k" and "l" that are opposite to each other,
920 * except for the constant term, check if we can use them
921 * to obtain an expression for one of the hitherto unknown divs.
922 * "sum" is the sum of the constant terms of the constraints.
923 * If this sum is strictly smaller than the coefficient of one
924 * of the divs, then this pair can be used define the div.
925 * To avoid the introduction of circular definitions of divs, we
926 * do not use the pair if the resulting expression would refer to
927 * any other undefined divs or if any known div is defined in
928 * terms of the unknown div.
930 static struct isl_basic_map *check_for_div_constraints(
931 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
934 unsigned total = 1 + isl_dim_total(bmap->dim);
936 for (i = 0; i < bmap->n_div; ++i) {
937 if (!isl_int_is_zero(bmap->div[i][0]))
939 if (isl_int_is_zero(bmap->ineq[k][total + i]))
941 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
943 if (!ok_to_set_div_from_bound(bmap, i, k))
945 if (isl_int_is_pos(bmap->ineq[k][total + i]))
946 bmap = set_div_from_lower_bound(bmap, i, k);
948 bmap = set_div_from_lower_bound(bmap, i, l);
956 static struct isl_basic_map *remove_duplicate_constraints(
957 struct isl_basic_map *bmap, int *progress)
963 unsigned total = isl_basic_map_total_dim(bmap);
966 if (bmap->n_ineq <= 1)
969 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
970 bits = ffs(size) - 1;
971 index = isl_calloc_array(ctx, isl_int **, size);
975 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
976 for (k = 1; k < bmap->n_ineq; ++k) {
977 h = hash_index(index, size, bits, bmap, k);
979 index[h] = &bmap->ineq[k];
984 l = index[h] - &bmap->ineq[0];
985 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
986 swap_inequality(bmap, k, l);
987 isl_basic_map_drop_inequality(bmap, k);
991 for (k = 0; k < bmap->n_ineq-1; ++k) {
992 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
993 h = hash_index(index, size, bits, bmap, k);
994 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
997 l = index[h] - &bmap->ineq[0];
998 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
999 if (isl_int_is_pos(sum)) {
1000 bmap = check_for_div_constraints(bmap, k, l, sum,
1004 if (isl_int_is_zero(sum)) {
1005 /* We need to break out of the loop after these
1006 * changes since the contents of the hash
1007 * will no longer be valid.
1008 * Plus, we probably we want to regauss first.
1010 isl_basic_map_drop_inequality(bmap, l);
1011 isl_basic_map_inequality_to_equality(bmap, k);
1013 bmap = isl_basic_map_set_to_empty(bmap);
1023 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1030 bmap = isl_basic_map_normalize_constraints(bmap);
1031 bmap = remove_duplicate_divs(bmap, &progress);
1032 bmap = eliminate_divs_eq(bmap, &progress);
1033 bmap = eliminate_divs_ineq(bmap, &progress);
1034 bmap = isl_basic_map_gauss(bmap, &progress);
1035 /* requires equalities in normal form */
1036 bmap = normalize_divs(bmap, &progress);
1037 bmap = remove_duplicate_constraints(bmap, &progress);
1042 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1044 return (struct isl_basic_set *)
1045 isl_basic_map_simplify((struct isl_basic_map *)bset);
1049 /* If the only constraints a div d=floor(f/m)
1050 * appears in are its two defining constraints
1053 * -(f - (m - 1)) + m d >= 0
1055 * then it can safely be removed.
1057 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1060 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
1062 for (i = 0; i < bmap->n_eq; ++i)
1063 if (!isl_int_is_zero(bmap->eq[i][pos]))
1066 for (i = 0; i < bmap->n_ineq; ++i) {
1067 if (isl_int_is_zero(bmap->ineq[i][pos]))
1069 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1071 isl_int_sub(bmap->div[div][1],
1072 bmap->div[div][1], bmap->div[div][0]);
1073 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1074 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
1075 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1076 isl_int_add(bmap->div[div][1],
1077 bmap->div[div][1], bmap->div[div][0]);
1080 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1081 bmap->n_div-div-1) != -1)
1083 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1084 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
1086 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1087 bmap->n_div-div-1) != -1)
1093 for (i = 0; i < bmap->n_div; ++i)
1094 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1101 * Remove divs that don't occur in any of the constraints or other divs.
1102 * These can arise when dropping some of the variables in a quast
1103 * returned by piplib.
1105 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1112 for (i = bmap->n_div-1; i >= 0; --i) {
1113 if (!div_is_redundant(bmap, i))
1115 bmap = isl_basic_map_drop_div(bmap, i);
1120 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1122 bmap = remove_redundant_divs(bmap);
1125 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1129 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1131 return (struct isl_basic_set *)
1132 isl_basic_map_finalize((struct isl_basic_map *)bset);
1135 struct isl_set *isl_set_finalize(struct isl_set *set)
1141 for (i = 0; i < set->n; ++i) {
1142 set->p[i] = isl_basic_set_finalize(set->p[i]);
1152 struct isl_map *isl_map_finalize(struct isl_map *map)
1158 for (i = 0; i < map->n; ++i) {
1159 map->p[i] = isl_basic_map_finalize(map->p[i]);
1163 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1171 /* Remove definition of any div that is defined in terms of the given variable.
1172 * The div itself is not removed. Functions such as
1173 * eliminate_divs_ineq depend on the other divs remaining in place.
1175 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1180 for (i = 0; i < bmap->n_div; ++i) {
1181 if (isl_int_is_zero(bmap->div[i][0]))
1183 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1185 isl_int_set_si(bmap->div[i][0], 0);
1190 /* Eliminate the specified variables from the constraints using
1191 * Fourier-Motzkin. The variables themselves are not removed.
1193 struct isl_basic_map *isl_basic_map_eliminate_vars(
1194 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1204 total = isl_basic_map_total_dim(bmap);
1206 bmap = isl_basic_map_cow(bmap);
1207 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1208 bmap = remove_dependent_vars(bmap, d);
1210 for (d = pos + n - 1;
1211 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1212 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1213 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1214 int n_lower, n_upper;
1217 for (i = 0; i < bmap->n_eq; ++i) {
1218 if (isl_int_is_zero(bmap->eq[i][1+d]))
1220 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1221 isl_basic_map_drop_equality(bmap, i);
1228 for (i = 0; i < bmap->n_ineq; ++i) {
1229 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1231 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1234 bmap = isl_basic_map_extend_constraints(bmap,
1235 0, n_lower * n_upper);
1236 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1238 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1241 for (j = 0; j < i; ++j) {
1242 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1245 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1246 isl_int_sgn(bmap->ineq[j][1+d]))
1248 k = isl_basic_map_alloc_inequality(bmap);
1251 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1253 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1254 1+d, 1+total, NULL);
1256 isl_basic_map_drop_inequality(bmap, i);
1259 if (n_lower > 0 && n_upper > 0) {
1260 bmap = isl_basic_map_normalize_constraints(bmap);
1261 bmap = remove_duplicate_constraints(bmap, NULL);
1262 bmap = isl_basic_map_gauss(bmap, NULL);
1263 bmap = isl_basic_map_convex_hull(bmap);
1266 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1273 isl_basic_map_free(bmap);
1277 struct isl_basic_set *isl_basic_set_eliminate_vars(
1278 struct isl_basic_set *bset, unsigned pos, unsigned n)
1280 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1281 (struct isl_basic_map *)bset, pos, n);
1284 /* Don't assume equalities are in order, because align_divs
1285 * may have changed the order of the divs.
1287 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1292 total = isl_dim_total(bmap->dim);
1293 for (d = 0; d < total; ++d)
1295 for (i = 0; i < bmap->n_eq; ++i) {
1296 for (d = total - 1; d >= 0; --d) {
1297 if (isl_int_is_zero(bmap->eq[i][1+d]))
1305 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1307 compute_elimination_index((struct isl_basic_map *)bset, elim);
1310 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1311 struct isl_basic_map *bmap, int *elim)
1317 total = isl_dim_total(bmap->dim);
1318 for (d = total - 1; d >= 0; --d) {
1319 if (isl_int_is_zero(src[1+d]))
1324 isl_seq_cpy(dst, src, 1 + total);
1327 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1332 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1333 struct isl_basic_set *bset, int *elim)
1335 return reduced_using_equalities(dst, src,
1336 (struct isl_basic_map *)bset, elim);
1339 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1340 struct isl_basic_set *bset, struct isl_basic_set *context)
1345 if (!bset || !context)
1348 bset = isl_basic_set_cow(bset);
1352 elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1355 set_compute_elimination_index(context, elim);
1356 for (i = 0; i < bset->n_eq; ++i)
1357 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1359 for (i = 0; i < bset->n_ineq; ++i)
1360 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1362 isl_basic_set_free(context);
1364 bset = isl_basic_set_simplify(bset);
1365 bset = isl_basic_set_finalize(bset);
1368 isl_basic_set_free(bset);
1369 isl_basic_set_free(context);
1373 static struct isl_basic_set *remove_shifted_constraints(
1374 struct isl_basic_set *bset, struct isl_basic_set *context)
1384 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1385 bits = ffs(size) - 1;
1386 index = isl_calloc_array(ctx, isl_int **, size);
1390 for (k = 0; k < context->n_ineq; ++k) {
1391 h = set_hash_index(index, size, bits, context, k);
1392 index[h] = &context->ineq[k];
1394 for (k = 0; k < bset->n_ineq; ++k) {
1395 h = set_hash_index(index, size, bits, bset, k);
1398 l = index[h] - &context->ineq[0];
1399 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1401 bset = isl_basic_set_cow(bset);
1404 isl_basic_set_drop_inequality(bset, k);
1414 /* Tighten (decrease) the constant terms of the inequalities based
1415 * on the equalities, without removing any integer points.
1416 * For example, if there is an equality
1424 * then we want to replace the inequality by
1428 * We do this by computing a variable compression and translating
1429 * the constraints to the compressed space.
1430 * If any constraint has coefficients (except the contant term)
1431 * with a common factor "f", then we can replace the constant term "c"
1438 * f * floor(c/f) - c = -fract(c/f)
1440 * and we can add the same value to the original constraint.
1442 * In the example, the compressed space only contains "j",
1443 * and the inequality translates to
1447 * We add -fract(-1/3) = -2 to the original constraint to obtain
1451 static struct isl_basic_set *normalize_constraints_in_compressed_space(
1452 struct isl_basic_set *bset)
1456 struct isl_mat *B, *C;
1462 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1468 bset = isl_basic_set_cow(bset);
1472 total = isl_basic_set_total_dim(bset);
1473 B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
1474 C = isl_mat_variable_compression(B, NULL);
1477 if (C->n_col == 0) {
1479 return isl_basic_set_set_to_empty(bset);
1481 B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
1482 0, bset->n_ineq, 0, 1 + total);
1483 C = isl_mat_product(B, C);
1488 for (i = 0; i < bset->n_ineq; ++i) {
1489 isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd);
1490 if (isl_int_is_one(gcd))
1492 isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd);
1493 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]);
1502 /* Remove all information from bset that is redundant in the context
1503 * of context. In particular, equalities that are linear combinations
1504 * of those in context are removed. Then the inequalities that are
1505 * redundant in the context of the equalities and inequalities of
1506 * context are removed.
1508 * We first simplify the constraints of "bset" in the context of the
1509 * equalities of "context".
1510 * Then we simplify the inequalities of the context in the context
1511 * of the equalities of bset and remove the inequalities from "bset"
1512 * that are obviously redundant with respect to some inequality in "context".
1514 * If there are any inequalities left, we construct a tableau for
1515 * the context and then add the inequalities of "bset".
1516 * Before adding these equalities, we freeze all constraints such that
1517 * they won't be considered redundant in terms of the constraints of "bset".
1518 * Then we detect all equalities and redundant constraints (among the
1519 * constraints that weren't frozen) and update bset according to the results.
1520 * We have to be careful here because we don't want any of the context
1521 * constraints to remain and because we haven't added the equalities of "bset"
1522 * to the tableau so we temporarily have to pretend that there were no
1525 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1526 struct isl_basic_set *context)
1529 struct isl_tab *tab;
1530 unsigned context_ineq;
1531 struct isl_basic_set *combined = NULL;
1533 if (!context || !bset)
1536 if (context->n_eq > 0)
1537 bset = isl_basic_set_reduce_using_equalities(bset,
1538 isl_basic_set_copy(context));
1541 if (isl_basic_set_fast_is_empty(bset))
1546 if (bset->n_eq > 0) {
1547 struct isl_basic_set *affine_hull;
1548 affine_hull = isl_basic_set_copy(bset);
1549 affine_hull = isl_basic_set_cow(affine_hull);
1552 isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1553 context = isl_basic_set_intersect(context, affine_hull);
1554 context = isl_basic_set_gauss(context, NULL);
1555 context = normalize_constraints_in_compressed_space(context);
1559 if (ISL_F_ISSET(context, ISL_BASIC_SET_EMPTY)) {
1560 isl_basic_set_free(bset);
1563 if (!context->n_ineq)
1565 bset = remove_shifted_constraints(bset, context);
1568 isl_basic_set_free_equality(context, context->n_eq);
1569 context_ineq = context->n_ineq;
1570 combined = isl_basic_set_cow(isl_basic_set_copy(context));
1571 combined = isl_basic_set_extend_constraints(combined,
1572 bset->n_eq, bset->n_ineq);
1573 tab = isl_tab_from_basic_set(combined);
1576 for (i = 0; i < context_ineq; ++i)
1577 tab->con[i].frozen = 1;
1578 tab = isl_tab_extend(tab, bset->n_ineq);
1581 for (i = 0; i < bset->n_ineq; ++i)
1582 tab = isl_tab_add_ineq(tab, bset->ineq[i]);
1583 bset = isl_basic_set_add_constraints(combined, bset, 0);
1584 tab = isl_tab_detect_implicit_equalities(tab);
1585 tab = isl_tab_detect_redundant(tab);
1588 for (i = 0; i < context_ineq; ++i) {
1589 tab->con[i].is_zero = 0;
1590 tab->con[i].is_redundant = 1;
1592 bset = isl_basic_set_update_from_tab(bset, tab);
1594 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1595 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1597 bset = isl_basic_set_simplify(bset);
1598 bset = isl_basic_set_finalize(bset);
1599 isl_basic_set_free(context);
1602 isl_basic_set_free(combined);
1604 isl_basic_set_free(bset);
1605 isl_basic_set_free(context);
1609 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1610 * We simply add the equalities in context to bmap and then do a regular
1611 * div normalizations. Better results can be obtained by normalizing
1612 * only the divs in bmap than do not also appear in context.
1613 * We need to be careful to reduce the divs using the equalities
1614 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1615 * spurious constraints.
1617 static struct isl_basic_map *normalize_divs_in_context(
1618 struct isl_basic_map *bmap, struct isl_basic_map *context)
1621 unsigned total_context;
1624 div_eq = n_pure_div_eq(bmap);
1628 if (context->n_div > 0)
1629 bmap = isl_basic_map_align_divs(bmap, context);
1631 total_context = isl_basic_map_total_dim(context);
1632 bmap = isl_basic_map_extend_constraints(bmap, context->n_eq, 0);
1633 for (i = 0; i < context->n_eq; ++i) {
1635 k = isl_basic_map_alloc_equality(bmap);
1636 isl_seq_cpy(bmap->eq[k], context->eq[i], 1 + total_context);
1637 isl_seq_clr(bmap->eq[k] + 1 + total_context,
1638 isl_basic_map_total_dim(bmap) - total_context);
1640 bmap = isl_basic_map_gauss(bmap, NULL);
1641 bmap = normalize_divs(bmap, NULL);
1642 bmap = isl_basic_map_gauss(bmap, NULL);
1646 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1647 struct isl_basic_map *context)
1649 struct isl_basic_set *bset;
1651 if (!bmap || !context)
1654 if (isl_basic_map_is_universe(context)) {
1655 isl_basic_map_free(context);
1658 if (isl_basic_map_is_universe(bmap)) {
1659 isl_basic_map_free(context);
1662 if (isl_basic_map_fast_is_empty(context)) {
1663 struct isl_dim *dim = isl_dim_copy(bmap->dim);
1664 isl_basic_map_free(context);
1665 isl_basic_map_free(bmap);
1666 return isl_basic_map_universe(dim);
1668 if (isl_basic_map_fast_is_empty(bmap)) {
1669 isl_basic_map_free(context);
1673 bmap = isl_basic_map_convex_hull(bmap);
1674 context = isl_basic_map_convex_hull(context);
1677 bmap = normalize_divs_in_context(bmap, context);
1679 context = isl_basic_map_align_divs(context, bmap);
1680 bmap = isl_basic_map_align_divs(bmap, context);
1682 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1683 isl_basic_map_underlying_set(context));
1685 return isl_basic_map_overlying_set(bset, bmap);
1687 isl_basic_map_free(bmap);
1688 isl_basic_map_free(context);
1693 * Assumes context has no implicit divs.
1695 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1699 if (!map || !context)
1702 if (isl_basic_map_is_universe(context)) {
1703 isl_basic_map_free(context);
1706 if (isl_basic_map_fast_is_empty(context)) {
1707 struct isl_dim *dim = isl_dim_copy(map->dim);
1708 isl_basic_map_free(context);
1710 return isl_map_universe(dim);
1713 context = isl_basic_map_convex_hull(context);
1714 map = isl_map_cow(map);
1715 if (!map || !context)
1717 isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1718 map = isl_map_compute_divs(map);
1719 for (i = 0; i < map->n; ++i)
1720 context = isl_basic_map_align_divs(context, map->p[i]);
1721 for (i = 0; i < map->n; ++i) {
1722 map->p[i] = isl_basic_map_gist(map->p[i],
1723 isl_basic_map_copy(context));
1727 isl_basic_map_free(context);
1728 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1732 isl_basic_map_free(context);
1736 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1737 struct isl_basic_set *context)
1739 return (struct isl_basic_set *)isl_basic_map_gist(
1740 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1743 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1745 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1746 (struct isl_basic_map *)context);
1749 /* Quick check to see if two basic maps are disjoint.
1750 * In particular, we reduce the equalities and inequalities of
1751 * one basic map in the context of the equalities of the other
1752 * basic map and check if we get a contradiction.
1754 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1755 struct isl_basic_map *bmap2)
1757 struct isl_vec *v = NULL;
1762 if (!bmap1 || !bmap2)
1764 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1766 if (bmap1->n_div || bmap2->n_div)
1768 if (!bmap1->n_eq && !bmap2->n_eq)
1771 total = isl_dim_total(bmap1->dim);
1774 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1777 elim = isl_alloc_array(bmap1->ctx, int, total);
1780 compute_elimination_index(bmap1, elim);
1781 for (i = 0; i < bmap2->n_eq; ++i) {
1783 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1785 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1786 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1789 for (i = 0; i < bmap2->n_ineq; ++i) {
1791 reduced = reduced_using_equalities(v->block.data,
1792 bmap2->ineq[i], bmap1, elim);
1793 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1794 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1797 compute_elimination_index(bmap2, elim);
1798 for (i = 0; i < bmap1->n_ineq; ++i) {
1800 reduced = reduced_using_equalities(v->block.data,
1801 bmap1->ineq[i], bmap2, elim);
1802 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1803 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1819 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1820 struct isl_basic_set *bset2)
1822 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1823 (struct isl_basic_map *)bset2);
1826 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1833 if (isl_map_fast_is_equal(map1, map2))
1836 for (i = 0; i < map1->n; ++i) {
1837 for (j = 0; j < map2->n; ++j) {
1838 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1847 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1849 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1850 (struct isl_map *)set2);
1853 /* Check if we can combine a given div with lower bound l and upper
1854 * bound u with some other div and if so return that other div.
1855 * Otherwise return -1.
1857 * We first check that
1858 * - the bounds are opposites of each other (except for the constant
1860 * - the bounds do not reference any other div
1861 * - no div is defined in terms of this div
1863 * Let m be the size of the range allowed on the div by the bounds.
1864 * That is, the bounds are of the form
1866 * e <= a <= e + m - 1
1868 * with e some expression in the other variables.
1869 * We look for another div b such that no third div is defined in terms
1870 * of this second div b and such that in any constraint that contains
1871 * a (except for the given lower and upper bound), also contains b
1872 * with a coefficient that is m times that of b.
1873 * That is, all constraints (execpt for the lower and upper bound)
1876 * e + f (a + m b) >= 0
1878 * If so, we return b so that "a + m b" can be replaced by
1879 * a single div "c = a + m b".
1881 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
1882 unsigned div, unsigned l, unsigned u)
1888 if (bmap->n_div <= 1)
1890 dim = isl_dim_total(bmap->dim);
1891 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
1893 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
1894 bmap->n_div - div - 1) != -1)
1896 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
1900 for (i = 0; i < bmap->n_div; ++i) {
1901 if (isl_int_is_zero(bmap->div[i][0]))
1903 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
1907 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
1908 if (isl_int_is_neg(bmap->ineq[l][0])) {
1909 isl_int_sub(bmap->ineq[l][0],
1910 bmap->ineq[l][0], bmap->ineq[u][0]);
1911 bmap = isl_basic_map_copy(bmap);
1912 bmap = isl_basic_map_set_to_empty(bmap);
1913 isl_basic_map_free(bmap);
1916 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
1917 for (i = 0; i < bmap->n_div; ++i) {
1922 for (j = 0; j < bmap->n_div; ++j) {
1923 if (isl_int_is_zero(bmap->div[j][0]))
1925 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
1928 if (j < bmap->n_div)
1930 for (j = 0; j < bmap->n_ineq; ++j) {
1932 if (j == l || j == u)
1934 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
1936 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
1938 isl_int_mul(bmap->ineq[j][1 + dim + div],
1939 bmap->ineq[j][1 + dim + div],
1941 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
1942 bmap->ineq[j][1 + dim + i]);
1943 isl_int_divexact(bmap->ineq[j][1 + dim + div],
1944 bmap->ineq[j][1 + dim + div],
1949 if (j < bmap->n_ineq)
1954 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
1955 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
1959 /* Given a lower and an upper bound on div i, construct an inequality
1960 * that when nonnegative ensures that this pair of bounds always allows
1961 * for an integer value of the given div.
1962 * The lower bound is inequality l, while the upper bound is inequality u.
1963 * The constructed inequality is stored in ineq.
1964 * g, fl, fu are temporary scalars.
1966 * Let the upper bound be
1970 * and the lower bound
1974 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
1977 * - f_u e_l <= f_u f_l g a <= f_l e_u
1979 * Since all variables are integer valued, this is equivalent to
1981 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
1983 * If this interval is at least f_u f_l g, then it contains at least
1984 * one integer value for a.
1985 * That is, the test constraint is
1987 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
1989 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
1990 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
1993 dim = isl_dim_total(bmap->dim);
1995 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
1996 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
1997 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
1998 isl_int_neg(fu, fu);
1999 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
2000 1 + dim + bmap->n_div);
2001 isl_int_add(ineq[0], ineq[0], fl);
2002 isl_int_add(ineq[0], ineq[0], fu);
2003 isl_int_sub_ui(ineq[0], ineq[0], 1);
2004 isl_int_mul(g, g, fl);
2005 isl_int_mul(g, g, fu);
2006 isl_int_sub(ineq[0], ineq[0], g);
2009 /* Remove more kinds of divs that are not strictly needed.
2010 * In particular, if all pairs of lower and upper bounds on a div
2011 * are such that they allow at least one integer value of the div,
2012 * the we can eliminate the div using Fourier-Motzkin without
2013 * introducing any spurious solutions.
2015 static struct isl_basic_map *drop_more_redundant_divs(
2016 struct isl_basic_map *bmap, int *pairs, int n)
2018 struct isl_tab *tab = NULL;
2019 struct isl_vec *vec = NULL;
2031 dim = isl_dim_total(bmap->dim);
2032 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
2036 tab = isl_tab_from_basic_map(bmap);
2041 enum isl_lp_result res;
2043 for (i = 0; i < bmap->n_div; ++i) {
2046 if (best >= 0 && pairs[best] <= pairs[i])
2052 for (l = 0; l < bmap->n_ineq; ++l) {
2053 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
2055 for (u = 0; u < bmap->n_ineq; ++u) {
2056 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
2058 construct_test_ineq(bmap, i, l, u,
2059 vec->el, g, fl, fu);
2060 res = isl_tab_min(tab, vec->el,
2061 bmap->ctx->one, &g, NULL, 0);
2062 if (res == isl_lp_error)
2064 if (res == isl_lp_empty) {
2065 bmap = isl_basic_map_set_to_empty(bmap);
2068 if (res != isl_lp_ok || isl_int_is_neg(g))
2071 if (u < bmap->n_ineq)
2074 if (l == bmap->n_ineq) {
2094 bmap = isl_basic_map_remove(bmap, isl_dim_div, remove, 1);
2095 return isl_basic_map_drop_redundant_divs(bmap);
2098 isl_basic_map_free(bmap);
2107 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2108 * and the upper bound u, div1 always occurs together with div2 in the form
2109 * (div1 + m div2), where m is the constant range on the variable div1
2110 * allowed by l and u, replace the pair div1 and div2 by a single
2111 * div that is equal to div1 + m div2.
2113 * The new div will appear in the location that contains div2.
2114 * We need to modify all constraints that contain
2115 * div2 = (div - div1) / m
2116 * (If a constraint does not contain div2, it will also not contain div1.)
2117 * If the constraint also contains div1, then we know they appear
2118 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2119 * i.e., the coefficient of div is f.
2121 * Otherwise, we first need to introduce div1 into the constraint.
2130 * A lower bound on div2
2134 * can be replaced by
2136 * (n * (m div 2 + div1) + m t + n f)/g >= 0
2138 * with g = gcd(m,n).
2143 * can be replaced by
2145 * (-n * (m div2 + div1) + m t + n f')/g >= 0
2147 * These constraint are those that we would obtain from eliminating
2148 * div1 using Fourier-Motzkin.
2150 * After all constraints have been modified, we drop the lower and upper
2151 * bound and then drop div1.
2153 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
2154 unsigned div1, unsigned div2, unsigned l, unsigned u)
2159 unsigned dim, total;
2162 dim = isl_dim_total(bmap->dim);
2163 total = 1 + dim + bmap->n_div;
2168 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
2169 isl_int_add_ui(m, m, 1);
2171 for (i = 0; i < bmap->n_ineq; ++i) {
2172 if (i == l || i == u)
2174 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
2176 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
2177 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
2178 isl_int_divexact(a, m, b);
2179 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
2180 if (isl_int_is_pos(b)) {
2181 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2182 b, bmap->ineq[l], total);
2185 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2186 b, bmap->ineq[u], total);
2189 isl_int_set(bmap->ineq[i][1 + dim + div2],
2190 bmap->ineq[i][1 + dim + div1]);
2191 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
2198 isl_basic_map_drop_inequality(bmap, l);
2199 isl_basic_map_drop_inequality(bmap, u);
2201 isl_basic_map_drop_inequality(bmap, u);
2202 isl_basic_map_drop_inequality(bmap, l);
2204 bmap = isl_basic_map_drop_div(bmap, div1);
2208 /* First check if we can coalesce any pair of divs and
2209 * then continue with dropping more redundant divs.
2211 * We loop over all pairs of lower and upper bounds on a div
2212 * with coefficient 1 and -1, respectively, check if there
2213 * is any other div "c" with which we can coalesce the div
2214 * and if so, perform the coalescing.
2216 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
2217 struct isl_basic_map *bmap, int *pairs, int n)
2222 dim = isl_dim_total(bmap->dim);
2224 for (i = 0; i < bmap->n_div; ++i) {
2227 for (l = 0; l < bmap->n_ineq; ++l) {
2228 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
2230 for (u = 0; u < bmap->n_ineq; ++u) {
2233 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
2235 c = div_find_coalesce(bmap, pairs, i, l, u);
2239 bmap = coalesce_divs(bmap, i, c, l, u);
2240 return isl_basic_map_drop_redundant_divs(bmap);
2245 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
2248 return drop_more_redundant_divs(bmap, pairs, n);
2251 /* Remove divs that are not strictly needed.
2252 * In particular, if a div only occurs positively (or negatively)
2253 * in constraints, then it can simply be dropped.
2254 * Also, if a div occurs only occurs in two constraints and if moreover
2255 * those two constraints are opposite to each other, except for the constant
2256 * term and if the sum of the constant terms is such that for any value
2257 * of the other values, there is always at least one integer value of the
2258 * div, i.e., if one plus this sum is greater than or equal to
2259 * the (absolute value) of the coefficent of the div in the constraints,
2260 * then we can also simply drop the div.
2262 * If any divs are left after these simple checks then we move on
2263 * to more complicated cases in drop_more_redundant_divs.
2265 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
2266 struct isl_basic_map *bmap)
2276 off = isl_dim_total(bmap->dim);
2277 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
2281 for (i = 0; i < bmap->n_div; ++i) {
2283 int last_pos, last_neg;
2287 defined = !isl_int_is_zero(bmap->div[i][0]);
2288 for (j = 0; j < bmap->n_eq; ++j)
2289 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
2295 for (j = 0; j < bmap->n_ineq; ++j) {
2296 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
2300 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
2305 pairs[i] = pos * neg;
2306 if (pairs[i] == 0) {
2307 for (j = bmap->n_ineq - 1; j >= 0; --j)
2308 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
2309 isl_basic_map_drop_inequality(bmap, j);
2310 bmap = isl_basic_map_drop_div(bmap, i);
2312 return isl_basic_map_drop_redundant_divs(bmap);
2316 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
2317 bmap->ineq[last_neg] + 1,
2321 isl_int_add(bmap->ineq[last_pos][0],
2322 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2323 isl_int_add_ui(bmap->ineq[last_pos][0],
2324 bmap->ineq[last_pos][0], 1);
2325 redundant = isl_int_ge(bmap->ineq[last_pos][0],
2326 bmap->ineq[last_pos][1+off+i]);
2327 isl_int_sub_ui(bmap->ineq[last_pos][0],
2328 bmap->ineq[last_pos][0], 1);
2329 isl_int_sub(bmap->ineq[last_pos][0],
2330 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2333 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
2338 bmap = set_div_from_lower_bound(bmap, i, last_pos);
2339 bmap = isl_basic_map_simplify(bmap);
2341 return isl_basic_map_drop_redundant_divs(bmap);
2343 if (last_pos > last_neg) {
2344 isl_basic_map_drop_inequality(bmap, last_pos);
2345 isl_basic_map_drop_inequality(bmap, last_neg);
2347 isl_basic_map_drop_inequality(bmap, last_neg);
2348 isl_basic_map_drop_inequality(bmap, last_pos);
2350 bmap = isl_basic_map_drop_div(bmap, i);
2352 return isl_basic_map_drop_redundant_divs(bmap);
2356 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
2362 isl_basic_map_free(bmap);
2366 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
2367 struct isl_basic_set *bset)
2369 return (struct isl_basic_set *)
2370 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
2373 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
2379 for (i = 0; i < map->n; ++i) {
2380 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
2384 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2391 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
2393 return (struct isl_set *)
2394 isl_map_drop_redundant_divs((struct isl_map *)set);